]> git.saurik.com Git - wxWidgets.git/blob - src/osx/core/evtloop_cf.cpp
Fix harmless unused parameter warning in wxOSX.
[wxWidgets.git] / src / osx / core / evtloop_cf.cpp
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
6 // RCS-ID: $Id$
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // (c) 2013 Rob Bresalier
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
29 #ifndef WX_PRECOMP
30 #include "wx/log.h"
31 #include "wx/app.h"
32 #endif
33
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"
40 #include "wx/thread.h"
41
42 #if wxUSE_GUI
43 #include "wx/nonownedwnd.h"
44 #endif
45
46 #include <CoreFoundation/CFSocket.h>
47
48 // ============================================================================
49 // wxCFEventLoopSource and wxCFEventLoop implementation
50 // ============================================================================
51
52 #if wxUSE_EVENTLOOP_SOURCE
53
54 void wxCFEventLoopSource::InitSourceSocket(CFSocketRef cfSocket)
55 {
56 wxASSERT_MSG( !m_cfSocket, "shouldn't be called more than once" );
57
58 m_cfSocket = cfSocket;
59 }
60
61 wxCFEventLoopSource::~wxCFEventLoopSource()
62 {
63 if ( m_cfSocket )
64 {
65 CFSocketInvalidate(m_cfSocket);
66 CFRelease(m_cfSocket);
67 }
68 }
69
70 #endif // wxUSE_EVENTLOOP_SOURCE
71
72 void wxCFEventLoop::OSXCommonModeObserverCallBack(CFRunLoopObserverRef observer, int activity, void *info)
73 {
74 wxCFEventLoop * eventloop = static_cast<wxCFEventLoop *>(info);
75 if ( eventloop )
76 eventloop->CommonModeObserverCallBack(observer, activity);
77 }
78
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)
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
98 if ( wxTheApp && ShouldProcessIdleEvents() )
99 wxTheApp->ProcessPendingEvents();
100 }
101
102 if ( activity & kCFRunLoopBeforeWaiting )
103 {
104 if ( ShouldProcessIdleEvents() && ProcessIdle() )
105 {
106 WakeUp();
107 }
108 else
109 {
110 #if wxUSE_THREADS
111 wxMutexGuiLeaveOrEnter();
112 #endif
113 }
114 }
115 }
116
117 void
118 wxCFEventLoop::DefaultModeObserverCallBack(CFRunLoopObserverRef WXUNUSED(observer),
119 int WXUNUSED(activity))
120 {
121 /*
122 if ( activity & kCFRunLoopBeforeTimers )
123 {
124 }
125
126 if ( activity & kCFRunLoopBeforeWaiting )
127 {
128 }
129 */
130 }
131
132
133 wxCFEventLoop::wxCFEventLoop()
134 {
135 m_shouldExit = false;
136 m_processIdleEvents = true;
137
138 #if wxUSE_UIACTIONSIMULATOR
139 m_shouldWaitForEvent = false;
140 #endif
141
142 m_runLoop = CFGetCurrentRunLoop();
143
144 CFRunLoopObserverContext ctxt;
145 bzero( &ctxt, sizeof(ctxt) );
146 ctxt.info = this;
147 m_commonModeRunLoopObserver = CFRunLoopObserverCreate( kCFAllocatorDefault, kCFRunLoopBeforeTimers | kCFRunLoopBeforeWaiting , true /* repeats */, 0,
148 (CFRunLoopObserverCallBack) wxCFEventLoop::OSXCommonModeObserverCallBack, &ctxt );
149 CFRunLoopAddObserver(m_runLoop, m_commonModeRunLoopObserver, kCFRunLoopCommonModes);
150
151 m_defaultModeRunLoopObserver = CFRunLoopObserverCreate( kCFAllocatorDefault, kCFRunLoopBeforeTimers | kCFRunLoopBeforeWaiting , true /* repeats */, 0,
152 (CFRunLoopObserverCallBack) wxCFEventLoop::OSXDefaultModeObserverCallBack, &ctxt );
153 CFRunLoopAddObserver(m_runLoop, m_defaultModeRunLoopObserver, kCFRunLoopDefaultMode);
154 }
155
156 wxCFEventLoop::~wxCFEventLoop()
157 {
158 CFRunLoopRemoveObserver(m_runLoop, m_commonModeRunLoopObserver, kCFRunLoopCommonModes);
159 CFRunLoopRemoveObserver(m_runLoop, m_defaultModeRunLoopObserver, kCFRunLoopDefaultMode);
160
161 CFRelease(m_defaultModeRunLoopObserver);
162 CFRelease(m_commonModeRunLoopObserver);
163 }
164
165
166 CFRunLoopRef wxCFEventLoop::CFGetCurrentRunLoop() const
167 {
168 return CFRunLoopGetCurrent();
169 }
170
171 void wxCFEventLoop::WakeUp()
172 {
173 CFRunLoopWakeUp(m_runLoop);
174 }
175
176 #if wxUSE_BASE
177
178 void wxMacWakeUp()
179 {
180 wxEventLoopBase * const loop = wxEventLoopBase::GetActive();
181
182 if ( loop )
183 loop->WakeUp();
184 }
185
186 #endif
187
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
198
199 m_isInsideYield = true;
200 m_eventsToProcessInsideYield = eventsToProcess;
201
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
207
208 // process all pending events:
209 while ( DoProcessEvents() == 1 )
210 ;
211
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() ) {}
216
217 // if there are pending events, we must process them.
218 if (wxTheApp)
219 wxTheApp->ProcessPendingEvents();
220
221 #if wxUSE_LOG
222 wxLog::Resume();
223 #endif // wxUSE_LOG
224 m_isInsideYield = false;
225
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 {
237 #if wxUSE_UIACTIONSIMULATOR
238 if ( m_shouldWaitForEvent )
239 {
240 int handled = DispatchTimeout( 1000 );
241 wxASSERT_MSG( handled == 1, "No Event Available");
242 m_shouldWaitForEvent = false;
243 return handled;
244 }
245 else
246 #endif
247 return DispatchTimeout( 0 );
248 }
249
250 bool wxCFEventLoop::Dispatch()
251 {
252 return DoProcessEvents() != 0;
253 }
254
255 int wxCFEventLoop::DispatchTimeout(unsigned long timeout)
256 {
257 if ( !wxTheApp )
258 return 0;
259
260 int status = DoDispatchTimeout(timeout);
261
262 switch( status )
263 {
264 case 0:
265 break;
266 case -1:
267 if ( m_shouldExit )
268 return 0;
269
270 break;
271 case 1:
272 break;
273 }
274
275 return status;
276 }
277
278 int wxCFEventLoop::DoDispatchTimeout(unsigned long timeout)
279 {
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
299 void wxCFEventLoop::OSXDoRun()
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();
306
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 ;
314
315 break;
316 }
317 }
318 }
319
320 void wxCFEventLoop::OSXDoStop()
321 {
322 CFRunLoopStop(CFGetCurrentRunLoop());
323 }
324
325 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
326 // terminating when Exit() is called
327 int wxCFEventLoop::DoRun()
328 {
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
340
341 OSXDoRun();
342
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
369
370 return m_exitcode;
371 }
372
373 // sets the "should exit" flag and wakes up the loop so that it terminates
374 // soon
375 void wxCFEventLoop::ScheduleExit(int rc)
376 {
377 m_exitcode = rc;
378 m_shouldExit = true;
379 OSXDoStop();
380 }
381
382 wxCFEventLoopPauseIdleEvents::wxCFEventLoopPauseIdleEvents()
383 {
384 wxCFEventLoop* cfl = dynamic_cast<wxCFEventLoop*>(wxEventLoopBase::GetActive());
385 if ( cfl )
386 {
387 m_formerState = cfl->ShouldProcessIdleEvents();
388 cfl->SetProcessIdleEvents(false);
389 }
390 else
391 m_formerState = true;
392 }
393
394 wxCFEventLoopPauseIdleEvents::~wxCFEventLoopPauseIdleEvents()
395 {
396 wxCFEventLoop* cfl = dynamic_cast<wxCFEventLoop*>(wxEventLoopBase::GetActive());
397 if ( cfl )
398 cfl->SetProcessIdleEvents(m_formerState);
399 }
400
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 {
425 gs_critsectWaitingForGui = new wxCriticalSection();
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 }
440
441 gs_critsectGui->Leave();
442 wxDELETE(gs_critsectGui);
443 }
444
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()!") );
460
461 // the order in which we enter the critical sections here is crucial!!
462
463 // set the flag telling to the main thread that we want to do some GUI
464 {
465 wxCriticalSectionLocker enter(*gs_critsectWaitingForGui);
466
467 gs_nWaitingForGui++;
468 }
469
470 wxWakeUpMainThread();
471
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);
480
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?") );
490
491 gs_nWaitingForGui--;
492
493 wxWakeUpMainThread();
494 }
495
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()!") );
503
504 if ( !gs_critsectWaitingForGui )
505 return;
506
507 wxCriticalSectionLocker enter(*gs_critsectWaitingForGui);
508
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();
516
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