1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/evtloop_cf.cpp
3 // Purpose: wxEventLoop implementation common to both Carbon and Cocoa
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/evtloop.h"
28 #if wxUSE_EVENTLOOP_SOURCE
35 #include "wx/evtloopsrc.h"
37 #include "wx/scopedptr.h"
39 #include "wx/osx/private.h"
40 #include "wx/osx/core/cfref.h"
42 // ============================================================================
43 // wxCFEventLoopSource and wxCFEventLoop implementation
44 // ============================================================================
46 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
50 void EnableDescriptorCallBacks(CFFileDescriptorRef cffd
, int flags
)
52 if ( flags
& wxEVENT_SOURCE_INPUT
)
53 CFFileDescriptorEnableCallBacks(cffd
, kCFFileDescriptorReadCallBack
);
54 if ( flags
& wxEVENT_SOURCE_OUTPUT
)
55 CFFileDescriptorEnableCallBacks(cffd
, kCFFileDescriptorWriteCallBack
);
59 wx_cffiledescriptor_callback(CFFileDescriptorRef cffd
,
63 wxLogTrace(wxTRACE_EVT_SOURCE
,
64 "CFFileDescriptor callback, flags=%d", flags
);
66 wxCFEventLoopSource
* const
67 source
= static_cast<wxCFEventLoopSource
*>(ctxData
);
69 wxEventLoopSourceHandler
* const
70 handler
= source
->GetHandler();
71 if ( flags
& kCFFileDescriptorReadCallBack
)
72 handler
->OnReadWaiting();
73 if ( flags
& kCFFileDescriptorWriteCallBack
)
74 handler
->OnWriteWaiting();
76 // we need to re-enable callbacks to be called again
77 EnableDescriptorCallBacks(cffd
, source
->GetFlags());
80 } // anonymous namespace
83 wxCFEventLoop::AddSourceForFD(int fd
,
84 wxEventLoopSourceHandler
*handler
,
87 wxCHECK_MSG( fd
!= -1, NULL
, "can't monitor invalid fd" );
89 wxScopedPtr
<wxCFEventLoopSource
>
90 source(new wxCFEventLoopSource(handler
, flags
));
92 CFFileDescriptorContext ctx
= { 0, source
.get(), NULL
, NULL
, NULL
};
93 wxCFRef
<CFFileDescriptorRef
>
94 cffd(CFFileDescriptorCreate
98 true, // close on invalidate
99 wx_cffiledescriptor_callback
,
105 source
->SetFileDescriptor(cffd
.release());
107 wxCFRef
<CFRunLoopSourceRef
>
108 cfsrc(CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault
, cffd
, 0));
112 CFRunLoopRef cfloop
= CFGetCurrentRunLoop();
113 CFRunLoopAddSource(cfloop
, cfsrc
, kCFRunLoopDefaultMode
);
115 return source
.release();
118 void wxCFEventLoopSource::SetFileDescriptor(CFFileDescriptorRef cffd
)
120 wxASSERT_MSG( !m_cffd
, "shouldn't be called more than once" );
125 wxCFEventLoopSource::~wxCFEventLoopSource()
134 wxCFEventLoop::AddSourceForFD(int WXUNUSED(fd
),
135 wxEventLoopSourceHandler
* WXUNUSED(handler
),
141 #endif // MAC_OS_X_VERSION_MAX_ALLOWED
143 #endif // wxUSE_EVENTLOOP_SOURCE
145 void wxObserverCallBack(CFRunLoopObserverRef observer
, CFRunLoopActivity activity
, void *info
)
147 wxCFEventLoop
* eventloop
= static_cast<wxCFEventLoop
*>(info
);
149 eventloop
->ObserverCallBack(observer
, activity
);
152 void wxCFEventLoop::ObserverCallBack(CFRunLoopObserverRef observer
, int activity
)
154 if ( activity
& kCFRunLoopBeforeTimers
)
156 // process pending wx events first as they correspond to low-level events
157 // which happened before, i.e. typically pending events were queued by a
158 // previous call to Dispatch() and if we didn't process them now the next
159 // call to it might enqueue them again (as happens with e.g. socket events
160 // which would be generated as long as there is input available on socket
161 // and this input is only removed from it when pending event handlers are
165 wxTheApp
->ProcessPendingEvents();
168 if ( activity
& kCFRunLoopBeforeWaiting
)
185 wxCFEventLoop::wxCFEventLoop()
187 m_shouldExit
= false;
188 CFRunLoopObserverContext ctxt
;
189 bzero( &ctxt
, sizeof(ctxt
) );
191 m_runLoopObserver
= CFRunLoopObserverCreate( kCFAllocatorDefault
, kCFRunLoopBeforeTimers
| kCFRunLoopBeforeWaiting
, true /* repeats */, 0,
192 wxObserverCallBack
, &ctxt
);
193 CFRunLoopAddObserver(CFGetCurrentRunLoop(), m_runLoopObserver
, kCFRunLoopDefaultMode
);
196 wxCFEventLoop::~wxCFEventLoop()
201 CFRunLoopRef
wxCFEventLoop::CFGetCurrentRunLoop() const
203 return CFRunLoopGetCurrent();
206 void wxCFEventLoop::WakeUp()
208 extern void wxMacWakeUp();
213 bool wxCFEventLoop::YieldFor(long eventsToProcess
)
216 // Yielding from a non-gui thread needs to bail out, otherwise we end up
217 // possibly sending events in the thread too.
218 if ( !wxThread::IsMain() )
222 #endif // wxUSE_THREADS
224 m_isInsideYield
= true;
225 m_eventsToProcessInsideYield
= eventsToProcess
;
228 // disable log flushing from here because a call to wxYield() shouldn't
229 // normally result in message boxes popping up &c
233 // process all pending events:
234 while ( DoProcessEvents() == 1 )
237 // it's necessary to call ProcessIdle() to update the frames sizes which
238 // might have been changed (it also will update other things set from
239 // OnUpdateUI() which is a nice (and desired) side effect)
240 while ( ProcessIdle() ) {}
242 // if there are pending events, we must process them.
244 wxTheApp
->ProcessPendingEvents();
249 m_isInsideYield
= false;
254 // implement/override base class pure virtual
255 bool wxCFEventLoop::Pending() const
260 int wxCFEventLoop::DoProcessEvents()
262 return DispatchTimeout( 1000 );
265 bool wxCFEventLoop::Dispatch()
267 return DoProcessEvents() != 0;
270 int wxCFEventLoop::DispatchTimeout(unsigned long timeout
)
275 wxMacAutoreleasePool autoreleasepool
;
277 int status
= DoDispatchTimeout(timeout
);
295 int wxCFEventLoop::DoDispatchTimeout(unsigned long timeout
)
297 SInt32 status
= CFRunLoopRunInMode(kCFRunLoopDefaultMode
, timeout
/ 1000.0 , true);
300 case kCFRunLoopRunFinished
:
301 wxFAIL_MSG( "incorrect run loop state" );
303 case kCFRunLoopRunStopped
:
306 case kCFRunLoopRunTimedOut
:
309 case kCFRunLoopRunHandledSource
:
316 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
317 // terminating when Exit() is called
318 int wxCFEventLoop::Run()
320 // event loops are not recursive, you need to create another loop!
321 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
323 // ProcessIdle() and ProcessEvents() below may throw so the code here should
324 // be exception-safe, hence we must use local objects for all actions we
326 wxEventLoopActivator
activate(this);
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)
338 #endif // wxUSE_EXCEPTIONS
341 // generate and process idle events for as long as we don't
342 // have anything else to do
345 // if the "should exit" flag is set, the loop should terminate
346 // but not before processing any remaining messages so while
347 // Pending() returns true, do process them
350 while ( DoProcessEvents() == 1 )
358 // exit the outer loop as well
365 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
370 //else: continue running the event loop
374 // OnException() throwed, possibly rethrowing the same
375 // exception again: very good, but we still need OnExit() to
382 #endif // wxUSE_EXCEPTIONS
387 // sets the "should exit" flag and wakes up the loop so that it terminates
389 void wxCFEventLoop::Exit(int rc
)