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