]>
Commit | Line | Data |
---|---|---|
5cd99866 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/osx/core/evtloop_cf.cpp | |
3 | // Purpose: wxEventLoop implementation common to both Carbon and Cocoa | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2009-10-18 | |
5cd99866 | 6 | // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org> |
821d856a | 7 | // (c) 2013 Rob Bresalier |
5cd99866 VZ |
8 | // Licence: wxWindows licence |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #include "wx/evtloop.h" | |
27 | ||
fe5816f0 VZ |
28 | #ifndef WX_PRECOMP |
29 | #include "wx/log.h" | |
0056673c | 30 | #include "wx/app.h" |
fe5816f0 VZ |
31 | #endif |
32 | ||
5cd99866 VZ |
33 | #include "wx/evtloopsrc.h" |
34 | ||
35 | #include "wx/scopedptr.h" | |
36 | ||
37 | #include "wx/osx/private.h" | |
38 | #include "wx/osx/core/cfref.h" | |
a94c4b85 | 39 | #include "wx/thread.h" |
5cd99866 | 40 | |
976e63a7 VZ |
41 | #if wxUSE_GUI |
42 | #include "wx/nonownedwnd.h" | |
43 | #endif | |
44 | ||
a25b76f5 VZ |
45 | #include <CoreFoundation/CFSocket.h> |
46 | ||
5cd99866 VZ |
47 | // ============================================================================ |
48 | // wxCFEventLoopSource and wxCFEventLoop implementation | |
49 | // ============================================================================ | |
50 | ||
a6956131 VZ |
51 | #if wxUSE_EVENTLOOP_SOURCE |
52 | ||
a25b76f5 | 53 | void wxCFEventLoopSource::InitSourceSocket(CFSocketRef cfSocket) |
5cd99866 | 54 | { |
a25b76f5 | 55 | wxASSERT_MSG( !m_cfSocket, "shouldn't be called more than once" ); |
5cd99866 | 56 | |
a25b76f5 | 57 | m_cfSocket = cfSocket; |
5cd99866 VZ |
58 | } |
59 | ||
60 | wxCFEventLoopSource::~wxCFEventLoopSource() | |
61 | { | |
a25b76f5 VZ |
62 | if ( m_cfSocket ) |
63 | { | |
64 | CFSocketInvalidate(m_cfSocket); | |
65 | CFRelease(m_cfSocket); | |
66 | } | |
5cd99866 VZ |
67 | } |
68 | ||
5cd99866 | 69 | #endif // wxUSE_EVENTLOOP_SOURCE |
0056673c | 70 | |
3cac3654 | 71 | void wxCFEventLoop::OSXCommonModeObserverCallBack(CFRunLoopObserverRef observer, int activity, void *info) |
293a13ba SC |
72 | { |
73 | wxCFEventLoop * eventloop = static_cast<wxCFEventLoop *>(info); | |
74 | if ( eventloop ) | |
3cac3654 | 75 | eventloop->CommonModeObserverCallBack(observer, activity); |
976e63a7 | 76 | } |
293a13ba | 77 | |
3cac3654 SC |
78 | void wxCFEventLoop::OSXDefaultModeObserverCallBack(CFRunLoopObserverRef observer, int activity, void *info) |
79 | { | |
80 | wxCFEventLoop * eventloop = static_cast<wxCFEventLoop *>(info); | |
81 | if ( eventloop ) | |
82 | eventloop->DefaultModeObserverCallBack(observer, activity); | |
83 | } | |
84 | ||
85 | void wxCFEventLoop::CommonModeObserverCallBack(CFRunLoopObserverRef WXUNUSED(observer), int activity) | |
293a13ba SC |
86 | { |
87 | if ( activity & kCFRunLoopBeforeTimers ) | |
88 | { | |
89 | // process pending wx events first as they correspond to low-level events | |
90 | // which happened before, i.e. typically pending events were queued by a | |
91 | // previous call to Dispatch() and if we didn't process them now the next | |
92 | // call to it might enqueue them again (as happens with e.g. socket events | |
93 | // which would be generated as long as there is input available on socket | |
94 | // and this input is only removed from it when pending event handlers are | |
95 | // executed) | |
96 | ||
19736e64 | 97 | if ( wxTheApp && ShouldProcessIdleEvents() ) |
293a13ba SC |
98 | wxTheApp->ProcessPendingEvents(); |
99 | } | |
976e63a7 | 100 | |
3cac3654 SC |
101 | if ( activity & kCFRunLoopBeforeWaiting ) |
102 | { | |
19736e64 | 103 | if ( ShouldProcessIdleEvents() && ProcessIdle() ) |
592a3c6e SC |
104 | { |
105 | WakeUp(); | |
106 | } | |
107 | else | |
108 | { | |
3cac3654 | 109 | #if wxUSE_THREADS |
592a3c6e | 110 | wxMutexGuiLeaveOrEnter(); |
3cac3654 | 111 | #endif |
592a3c6e | 112 | } |
3cac3654 SC |
113 | } |
114 | } | |
115 | ||
6a06eecf VZ |
116 | void |
117 | wxCFEventLoop::DefaultModeObserverCallBack(CFRunLoopObserverRef WXUNUSED(observer), | |
118 | int WXUNUSED(activity)) | |
3cac3654 | 119 | { |
592a3c6e | 120 | /* |
3cac3654 SC |
121 | if ( activity & kCFRunLoopBeforeTimers ) |
122 | { | |
123 | } | |
124 | ||
293a13ba SC |
125 | if ( activity & kCFRunLoopBeforeWaiting ) |
126 | { | |
293a13ba | 127 | } |
592a3c6e | 128 | */ |
293a13ba SC |
129 | } |
130 | ||
3cac3654 | 131 | |
0056673c SC |
132 | wxCFEventLoop::wxCFEventLoop() |
133 | { | |
134 | m_shouldExit = false; | |
4c4cbded | 135 | m_processIdleEvents = true; |
976e63a7 | 136 | |
d60957aa SC |
137 | #if wxUSE_UIACTIONSIMULATOR |
138 | m_shouldWaitForEvent = false; | |
139 | #endif | |
140 | ||
9aee1212 | 141 | m_runLoop = CFGetCurrentRunLoop(); |
976e63a7 | 142 | |
293a13ba SC |
143 | CFRunLoopObserverContext ctxt; |
144 | bzero( &ctxt, sizeof(ctxt) ); | |
145 | ctxt.info = this; | |
3cac3654 SC |
146 | m_commonModeRunLoopObserver = CFRunLoopObserverCreate( kCFAllocatorDefault, kCFRunLoopBeforeTimers | kCFRunLoopBeforeWaiting , true /* repeats */, 0, |
147 | (CFRunLoopObserverCallBack) wxCFEventLoop::OSXCommonModeObserverCallBack, &ctxt ); | |
148 | CFRunLoopAddObserver(m_runLoop, m_commonModeRunLoopObserver, kCFRunLoopCommonModes); | |
3cac3654 SC |
149 | |
150 | m_defaultModeRunLoopObserver = CFRunLoopObserverCreate( kCFAllocatorDefault, kCFRunLoopBeforeTimers | kCFRunLoopBeforeWaiting , true /* repeats */, 0, | |
151 | (CFRunLoopObserverCallBack) wxCFEventLoop::OSXDefaultModeObserverCallBack, &ctxt ); | |
152 | CFRunLoopAddObserver(m_runLoop, m_defaultModeRunLoopObserver, kCFRunLoopDefaultMode); | |
0056673c SC |
153 | } |
154 | ||
155 | wxCFEventLoop::~wxCFEventLoop() | |
156 | { | |
3cac3654 SC |
157 | CFRunLoopRemoveObserver(m_runLoop, m_commonModeRunLoopObserver, kCFRunLoopCommonModes); |
158 | CFRunLoopRemoveObserver(m_runLoop, m_defaultModeRunLoopObserver, kCFRunLoopDefaultMode); | |
4c4cbded SC |
159 | |
160 | CFRelease(m_defaultModeRunLoopObserver); | |
161 | CFRelease(m_commonModeRunLoopObserver); | |
0056673c | 162 | } |
976e63a7 | 163 | |
0056673c SC |
164 | |
165 | CFRunLoopRef wxCFEventLoop::CFGetCurrentRunLoop() const | |
166 | { | |
293a13ba | 167 | return CFRunLoopGetCurrent(); |
0056673c SC |
168 | } |
169 | ||
170 | void wxCFEventLoop::WakeUp() | |
171 | { | |
58e74b83 | 172 | CFRunLoopWakeUp(m_runLoop); |
0056673c SC |
173 | } |
174 | ||
3bfba421 SC |
175 | #if wxUSE_BASE |
176 | ||
177 | void wxMacWakeUp() | |
178 | { | |
179 | wxEventLoopBase * const loop = wxEventLoopBase::GetActive(); | |
ce00f59b | 180 | |
3bfba421 SC |
181 | if ( loop ) |
182 | loop->WakeUp(); | |
183 | } | |
184 | ||
185 | #endif | |
186 | ||
0056673c SC |
187 | bool wxCFEventLoop::YieldFor(long eventsToProcess) |
188 | { | |
189 | #if wxUSE_THREADS | |
190 | // Yielding from a non-gui thread needs to bail out, otherwise we end up | |
191 | // possibly sending events in the thread too. | |
192 | if ( !wxThread::IsMain() ) | |
193 | { | |
194 | return true; | |
195 | } | |
196 | #endif // wxUSE_THREADS | |
976e63a7 | 197 | |
0056673c SC |
198 | m_isInsideYield = true; |
199 | m_eventsToProcessInsideYield = eventsToProcess; | |
976e63a7 | 200 | |
0056673c SC |
201 | #if wxUSE_LOG |
202 | // disable log flushing from here because a call to wxYield() shouldn't | |
203 | // normally result in message boxes popping up &c | |
204 | wxLog::Suspend(); | |
205 | #endif // wxUSE_LOG | |
976e63a7 | 206 | |
0056673c SC |
207 | // process all pending events: |
208 | while ( DoProcessEvents() == 1 ) | |
209 | ; | |
976e63a7 | 210 | |
0056673c SC |
211 | // it's necessary to call ProcessIdle() to update the frames sizes which |
212 | // might have been changed (it also will update other things set from | |
213 | // OnUpdateUI() which is a nice (and desired) side effect) | |
214 | while ( ProcessIdle() ) {} | |
976e63a7 | 215 | |
0056673c SC |
216 | // if there are pending events, we must process them. |
217 | if (wxTheApp) | |
218 | wxTheApp->ProcessPendingEvents(); | |
976e63a7 | 219 | |
0056673c SC |
220 | #if wxUSE_LOG |
221 | wxLog::Resume(); | |
222 | #endif // wxUSE_LOG | |
223 | m_isInsideYield = false; | |
976e63a7 | 224 | |
0056673c SC |
225 | return true; |
226 | } | |
227 | ||
228 | // implement/override base class pure virtual | |
229 | bool wxCFEventLoop::Pending() const | |
230 | { | |
231 | return true; | |
232 | } | |
233 | ||
234 | int wxCFEventLoop::DoProcessEvents() | |
235 | { | |
ea075402 | 236 | #if wxUSE_UIACTIONSIMULATOR |
d60957aa SC |
237 | if ( m_shouldWaitForEvent ) |
238 | { | |
57fe8307 | 239 | int handled = DispatchTimeout( 1000 ); |
d60957aa SC |
240 | wxASSERT_MSG( handled == 1, "No Event Available"); |
241 | m_shouldWaitForEvent = false; | |
57fe8307 | 242 | return handled; |
d60957aa | 243 | } |
57fe8307 | 244 | else |
ea075402 | 245 | #endif |
57fe8307 | 246 | return DispatchTimeout( 0 ); |
0056673c SC |
247 | } |
248 | ||
249 | bool wxCFEventLoop::Dispatch() | |
250 | { | |
293a13ba | 251 | return DoProcessEvents() != 0; |
0056673c SC |
252 | } |
253 | ||
254 | int wxCFEventLoop::DispatchTimeout(unsigned long timeout) | |
255 | { | |
256 | if ( !wxTheApp ) | |
257 | return 0; | |
258 | ||
0056673c | 259 | int status = DoDispatchTimeout(timeout); |
976e63a7 | 260 | |
0056673c SC |
261 | switch( status ) |
262 | { | |
263 | case 0: | |
264 | break; | |
265 | case -1: | |
266 | if ( m_shouldExit ) | |
267 | return 0; | |
976e63a7 | 268 | |
0056673c SC |
269 | break; |
270 | case 1: | |
0056673c SC |
271 | break; |
272 | } | |
976e63a7 | 273 | |
0056673c SC |
274 | return status; |
275 | } | |
276 | ||
277 | int wxCFEventLoop::DoDispatchTimeout(unsigned long timeout) | |
976e63a7 | 278 | { |
0056673c SC |
279 | SInt32 status = CFRunLoopRunInMode(kCFRunLoopDefaultMode, timeout / 1000.0 , true); |
280 | switch( status ) | |
281 | { | |
282 | case kCFRunLoopRunFinished: | |
283 | wxFAIL_MSG( "incorrect run loop state" ); | |
284 | break; | |
285 | case kCFRunLoopRunStopped: | |
286 | return 0; | |
287 | break; | |
288 | case kCFRunLoopRunTimedOut: | |
289 | return -1; | |
290 | break; | |
291 | case kCFRunLoopRunHandledSource: | |
292 | default: | |
293 | break; | |
294 | } | |
295 | return 1; | |
296 | } | |
297 | ||
8d40c05f | 298 | void wxCFEventLoop::OSXDoRun() |
80eee837 SC |
299 | { |
300 | for ( ;; ) | |
301 | { | |
302 | // generate and process idle events for as long as we don't | |
303 | // have anything else to do | |
304 | DoProcessEvents(); | |
976e63a7 | 305 | |
80eee837 SC |
306 | // if the "should exit" flag is set, the loop should terminate |
307 | // but not before processing any remaining messages so while | |
308 | // Pending() returns true, do process them | |
309 | if ( m_shouldExit ) | |
310 | { | |
311 | while ( DoProcessEvents() == 1 ) | |
312 | ; | |
976e63a7 | 313 | |
80eee837 SC |
314 | break; |
315 | } | |
316 | } | |
317 | } | |
318 | ||
8d40c05f | 319 | void wxCFEventLoop::OSXDoStop() |
80eee837 SC |
320 | { |
321 | CFRunLoopStop(CFGetCurrentRunLoop()); | |
322 | } | |
323 | ||
0056673c SC |
324 | // enters a loop calling OnNextIteration(), Pending() and Dispatch() and |
325 | // terminating when Exit() is called | |
c738d187 | 326 | int wxCFEventLoop::DoRun() |
0056673c | 327 | { |
0056673c SC |
328 | // we must ensure that OnExit() is called even if an exception is thrown |
329 | // from inside ProcessEvents() but we must call it from Exit() in normal | |
330 | // situations because it is supposed to be called synchronously, | |
331 | // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or | |
332 | // something similar here) | |
333 | #if wxUSE_EXCEPTIONS | |
334 | for ( ;; ) | |
335 | { | |
336 | try | |
337 | { | |
338 | #endif // wxUSE_EXCEPTIONS | |
976e63a7 | 339 | |
8d40c05f | 340 | OSXDoRun(); |
976e63a7 | 341 | |
0056673c SC |
342 | #if wxUSE_EXCEPTIONS |
343 | // exit the outer loop as well | |
344 | break; | |
345 | } | |
346 | catch ( ... ) | |
347 | { | |
348 | try | |
349 | { | |
350 | if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() ) | |
351 | { | |
352 | OnExit(); | |
353 | break; | |
354 | } | |
355 | //else: continue running the event loop | |
356 | } | |
357 | catch ( ... ) | |
358 | { | |
359 | // OnException() throwed, possibly rethrowing the same | |
360 | // exception again: very good, but we still need OnExit() to | |
361 | // be called | |
362 | OnExit(); | |
363 | throw; | |
364 | } | |
365 | } | |
366 | } | |
367 | #endif // wxUSE_EXCEPTIONS | |
976e63a7 | 368 | |
0056673c SC |
369 | return m_exitcode; |
370 | } | |
371 | ||
372 | // sets the "should exit" flag and wakes up the loop so that it terminates | |
373 | // soon | |
d3ad22bd | 374 | void wxCFEventLoop::ScheduleExit(int rc) |
0056673c SC |
375 | { |
376 | m_exitcode = rc; | |
377 | m_shouldExit = true; | |
8d40c05f | 378 | OSXDoStop(); |
80eee837 | 379 | } |
a94c4b85 | 380 | |
c657294b | 381 | wxCFEventLoopPauseIdleEvents::wxCFEventLoopPauseIdleEvents() |
4c4cbded SC |
382 | { |
383 | wxCFEventLoop* cfl = dynamic_cast<wxCFEventLoop*>(wxEventLoopBase::GetActive()); | |
384 | if ( cfl ) | |
19736e64 SC |
385 | { |
386 | m_formerState = cfl->ShouldProcessIdleEvents(); | |
4c4cbded | 387 | cfl->SetProcessIdleEvents(false); |
19736e64 SC |
388 | } |
389 | else | |
390 | m_formerState = true; | |
4c4cbded SC |
391 | } |
392 | ||
c657294b | 393 | wxCFEventLoopPauseIdleEvents::~wxCFEventLoopPauseIdleEvents() |
4c4cbded SC |
394 | { |
395 | wxCFEventLoop* cfl = dynamic_cast<wxCFEventLoop*>(wxEventLoopBase::GetActive()); | |
396 | if ( cfl ) | |
19736e64 | 397 | cfl->SetProcessIdleEvents(m_formerState); |
4c4cbded SC |
398 | } |
399 | ||
a94c4b85 SC |
400 | // TODO Move to thread_osx.cpp |
401 | ||
402 | #if wxUSE_THREADS | |
403 | ||
404 | // ---------------------------------------------------------------------------- | |
405 | // GUI Serialization copied from MSW implementation | |
406 | // ---------------------------------------------------------------------------- | |
407 | ||
408 | // if it's false, some secondary thread is holding the GUI lock | |
409 | static bool gs_bGuiOwnedByMainThread = true; | |
410 | ||
411 | // critical section which controls access to all GUI functions: any secondary | |
412 | // thread (i.e. except the main one) must enter this crit section before doing | |
413 | // any GUI calls | |
414 | static wxCriticalSection *gs_critsectGui = NULL; | |
415 | ||
416 | // critical section which protects gs_nWaitingForGui variable | |
417 | static wxCriticalSection *gs_critsectWaitingForGui = NULL; | |
418 | ||
419 | // number of threads waiting for GUI in wxMutexGuiEnter() | |
420 | static size_t gs_nWaitingForGui = 0; | |
421 | ||
422 | void wxOSXThreadModuleOnInit() | |
423 | { | |
ce00f59b | 424 | gs_critsectWaitingForGui = new wxCriticalSection(); |
a94c4b85 SC |
425 | gs_critsectGui = new wxCriticalSection(); |
426 | gs_critsectGui->Enter(); | |
427 | } | |
428 | ||
429 | ||
430 | void wxOSXThreadModuleOnExit() | |
431 | { | |
432 | if ( gs_critsectGui ) | |
433 | { | |
434 | if ( !wxGuiOwnedByMainThread() ) | |
435 | { | |
436 | gs_critsectGui->Enter(); | |
437 | gs_bGuiOwnedByMainThread = true; | |
438 | } | |
ce00f59b | 439 | |
a94c4b85 SC |
440 | gs_critsectGui->Leave(); |
441 | wxDELETE(gs_critsectGui); | |
442 | } | |
ce00f59b | 443 | |
a94c4b85 SC |
444 | wxDELETE(gs_critsectWaitingForGui); |
445 | } | |
446 | ||
447 | ||
448 | // wake up the main thread | |
449 | void WXDLLIMPEXP_BASE wxWakeUpMainThread() | |
450 | { | |
451 | wxMacWakeUp(); | |
452 | } | |
453 | ||
454 | void wxMutexGuiEnterImpl() | |
455 | { | |
456 | // this would dead lock everything... | |
457 | wxASSERT_MSG( !wxThread::IsMain(), | |
458 | wxT("main thread doesn't want to block in wxMutexGuiEnter()!") ); | |
ce00f59b | 459 | |
a94c4b85 | 460 | // the order in which we enter the critical sections here is crucial!! |
ce00f59b | 461 | |
a94c4b85 SC |
462 | // set the flag telling to the main thread that we want to do some GUI |
463 | { | |
464 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); | |
ce00f59b | 465 | |
a94c4b85 SC |
466 | gs_nWaitingForGui++; |
467 | } | |
ce00f59b | 468 | |
a94c4b85 | 469 | wxWakeUpMainThread(); |
ce00f59b | 470 | |
a94c4b85 SC |
471 | // now we may block here because the main thread will soon let us in |
472 | // (during the next iteration of OnIdle()) | |
473 | gs_critsectGui->Enter(); | |
474 | } | |
475 | ||
476 | void wxMutexGuiLeaveImpl() | |
477 | { | |
478 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); | |
ce00f59b | 479 | |
a94c4b85 SC |
480 | if ( wxThread::IsMain() ) |
481 | { | |
482 | gs_bGuiOwnedByMainThread = false; | |
483 | } | |
484 | else | |
485 | { | |
486 | // decrement the number of threads waiting for GUI access now | |
487 | wxASSERT_MSG( gs_nWaitingForGui > 0, | |
488 | wxT("calling wxMutexGuiLeave() without entering it first?") ); | |
ce00f59b | 489 | |
a94c4b85 | 490 | gs_nWaitingForGui--; |
ce00f59b | 491 | |
a94c4b85 SC |
492 | wxWakeUpMainThread(); |
493 | } | |
ce00f59b | 494 | |
a94c4b85 SC |
495 | gs_critsectGui->Leave(); |
496 | } | |
497 | ||
498 | void WXDLLIMPEXP_BASE wxMutexGuiLeaveOrEnter() | |
499 | { | |
500 | wxASSERT_MSG( wxThread::IsMain(), | |
501 | wxT("only main thread may call wxMutexGuiLeaveOrEnter()!") ); | |
ce00f59b | 502 | |
a94c4b85 SC |
503 | if ( !gs_critsectWaitingForGui ) |
504 | return; | |
ce00f59b | 505 | |
a94c4b85 | 506 | wxCriticalSectionLocker enter(*gs_critsectWaitingForGui); |
ce00f59b | 507 | |
a94c4b85 SC |
508 | if ( gs_nWaitingForGui == 0 ) |
509 | { | |
510 | // no threads are waiting for GUI - so we may acquire the lock without | |
511 | // any danger (but only if we don't already have it) | |
512 | if ( !wxGuiOwnedByMainThread() ) | |
513 | { | |
514 | gs_critsectGui->Enter(); | |
ce00f59b | 515 | |
a94c4b85 SC |
516 | gs_bGuiOwnedByMainThread = true; |
517 | } | |
518 | //else: already have it, nothing to do | |
519 | } | |
520 | else | |
521 | { | |
522 | // some threads are waiting, release the GUI lock if we have it | |
523 | if ( wxGuiOwnedByMainThread() ) | |
524 | wxMutexGuiLeave(); | |
525 | //else: some other worker thread is doing GUI | |
526 | } | |
527 | } | |
528 | ||
529 | bool WXDLLIMPEXP_BASE wxGuiOwnedByMainThread() | |
530 | { | |
531 | return gs_bGuiOwnedByMainThread; | |
532 | } | |
533 | ||
534 | #endif |