]> git.vomp.tv Git - vompclient.git/blob - eventdispatcher.cc
Mouse code for windows
[vompclient.git] / eventdispatcher.cc
1 /*
2     Copyright 2007 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 */
20
21 #include "eventdispatcher.h"
22
23 EventDispatcher::EventDispatcher()
24 {
25 #ifndef WIN32
26   pthread_mutex_init(&mutex, NULL);
27 #else
28   mutex = CreateMutex(NULL, FALSE, NULL);
29 #endif
30 }
31
32 void EventDispatcher::edRegister(EDReceiver* edr)
33 {
34   edLock();
35   receivers.push_back(edr);
36   edUnlock();
37 }  
38
39 bool EventDispatcher::edFindAndCall(void* userTag)
40 {
41   edLock();
42   
43   EDReceiver* edr = NULL;
44   EDRL::iterator i;
45   for(i = receivers.begin(); i != receivers.end(); i++)
46   {
47     if (ed_cb_find(*i, userTag))
48     {
49       edr = *i;
50       break; // found (by asking the EventDispatcher implementor to check if userTag is for *i
51     }
52   }
53   
54   if ((i == receivers.end()) || edr->callinprogress || edr->nomorecalls)
55   {
56     edUnlock();
57     return false;
58   }
59   
60   edr->callinprogress = true;
61   edUnlock();  
62   bool edrType = edr->call(userTag);
63   edLock();
64   edr->callinprogress = false;
65
66   if (edrType == false) // it's a multicall
67   {
68     if (edr->nomorecalls) // External has called unRegister - probably the receiver
69     {
70       // wake up the thread waiting in unregister
71       #ifndef WIN32
72       pthread_cond_signal(&edr->cond);
73       #else
74       SetEvent(edr->cond);
75       #endif
76     }
77   }
78   else // It's a single call. The receiver should be removed from the list. There will be a thread to wake up
79   {
80     for(i = receivers.begin(); i != receivers.end(); i++)
81     {
82       if (*i == edr)
83       {
84         receivers.erase(i);
85         break;
86       }
87     }
88     #ifndef WIN32
89     if (i == receivers.end()) abort(); // should never happen
90     // but it can happen under windows ... how??
91     #endif
92     
93     #ifndef WIN32
94     pthread_cond_signal(&edr->cond);
95     #else
96     SetEvent(edr->cond);
97     #endif
98   }
99   
100   edUnlock();
101   return true;
102 }
103
104 void EventDispatcher::edUnregister(EDReceiver* edr)
105 {
106   edLock();
107
108   EDRL::iterator i;
109   for(i = receivers.begin(); i != receivers.end(); i++)
110   {
111     if (*i == edr) break; // found
112   }
113
114   if (i == receivers.end()) abort(); // should never happen
115
116   if (!edr->callinprogress)
117   {
118     receivers.erase(i);
119     edUnlock();
120     return;
121   }
122
123   edr->nomorecalls = true;
124   
125   // edUnlock, wait for callinprogres=false (cond to be signalled), lock  
126 #ifndef WIN32
127   pthread_cond_wait(&edr->cond, &mutex);
128 #else
129   ReleaseMutex(mutex);
130   WaitForSingleObject(edr->cond,INFINITE);
131   WaitForSingleObject(mutex, INFINITE);
132   ResetEvent(edr->cond);
133
134 #endif
135
136   for(i = receivers.begin(); i != receivers.end(); i++)
137   {
138     if (*i == edr) break; // found
139   }
140
141   if (i == receivers.end()) abort(); // should never happen
142
143   receivers.erase(i);
144   edUnlock();
145 }
146
147 // ---------------------------------------
148
149 void EventDispatcher::edLock()
150 {
151 #ifndef WIN32
152   pthread_mutex_lock(&mutex);
153 #else
154   WaitForSingleObject(mutex, INFINITE);
155 #endif
156 }
157
158 void EventDispatcher::edUnlock()
159 {
160 #ifndef WIN32
161   pthread_mutex_unlock(&mutex);
162 #else
163   ReleaseMutex(mutex);
164 #endif
165 }
166
167 // ---------------------------------------
168
169 void EventDispatcher::edSleepThisReceiver(EDReceiver* edr)
170 {
171   // For blocking version, not callback version. Call with edLock locked
172   
173 #ifndef WIN32
174   pthread_cond_init(&edr->cond, NULL);
175   pthread_cond_wait(&edr->cond, &mutex);
176 #else
177    edr->cond = CreateEvent(NULL,/*FALSE*/TRUE,FALSE,NULL);
178    ReleaseMutex(mutex);
179    WaitForSingleObject(edr->cond,INFINITE);
180    ResetEvent(edr->cond);
181    WaitForSingleObject(mutex, INFINITE);
182 #endif
183 }
184
185 // -------------- EDReceiver implementation
186
187 EDReceiver::EDReceiver()
188 {
189   nomorecalls = false;
190   callinprogress = false;
191 #ifdef WIN32
192   cond=NULL;
193 #endif
194 }
195
196 EDReceiver::~EDReceiver()
197 {
198 #ifdef WIN32
199   if (cond!=NULL) CloseHandle(cond);
200 #endif
201 }