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