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"
43 #include "wx/nonownedwnd.h"
46 // ============================================================================
47 // wxCFEventLoopSource and wxCFEventLoop implementation
48 // ============================================================================
50 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
54 void EnableDescriptorCallBacks(CFFileDescriptorRef cffd
, int flags
)
56 if ( flags
& wxEVENT_SOURCE_INPUT
)
57 CFFileDescriptorEnableCallBacks(cffd
, kCFFileDescriptorReadCallBack
);
58 if ( flags
& wxEVENT_SOURCE_OUTPUT
)
59 CFFileDescriptorEnableCallBacks(cffd
, kCFFileDescriptorWriteCallBack
);
63 wx_cffiledescriptor_callback(CFFileDescriptorRef cffd
,
67 wxLogTrace(wxTRACE_EVT_SOURCE
,
68 "CFFileDescriptor callback, flags=%d", flags
);
70 wxCFEventLoopSource
* const
71 source
= static_cast<wxCFEventLoopSource
*>(ctxData
);
73 wxEventLoopSourceHandler
* const
74 handler
= source
->GetHandler();
75 if ( flags
& kCFFileDescriptorReadCallBack
)
76 handler
->OnReadWaiting();
77 if ( flags
& kCFFileDescriptorWriteCallBack
)
78 handler
->OnWriteWaiting();
80 // we need to re-enable callbacks to be called again
81 EnableDescriptorCallBacks(cffd
, source
->GetFlags());
84 } // anonymous namespace
87 wxCFEventLoop::AddSourceForFD(int fd
,
88 wxEventLoopSourceHandler
*handler
,
91 wxCHECK_MSG( fd
!= -1, NULL
, "can't monitor invalid fd" );
93 wxScopedPtr
<wxCFEventLoopSource
>
94 source(new wxCFEventLoopSource(handler
, flags
));
96 CFFileDescriptorContext ctx
= { 0, source
.get(), NULL
, NULL
, NULL
};
97 wxCFRef
<CFFileDescriptorRef
>
98 cffd(CFFileDescriptorCreate
102 true, // close on invalidate
103 wx_cffiledescriptor_callback
,
109 source
->SetFileDescriptor(cffd
.release());
111 wxCFRef
<CFRunLoopSourceRef
>
112 cfsrc(CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault
, cffd
, 0));
116 CFRunLoopRef cfloop
= CFGetCurrentRunLoop();
117 CFRunLoopAddSource(cfloop
, cfsrc
, kCFRunLoopDefaultMode
);
119 return source
.release();
122 void wxCFEventLoopSource::SetFileDescriptor(CFFileDescriptorRef cffd
)
124 wxASSERT_MSG( !m_cffd
, "shouldn't be called more than once" );
129 wxCFEventLoopSource::~wxCFEventLoopSource()
138 wxCFEventLoop::AddSourceForFD(int WXUNUSED(fd
),
139 wxEventLoopSourceHandler
* WXUNUSED(handler
),
145 #endif // MAC_OS_X_VERSION_MAX_ALLOWED
147 #endif // wxUSE_EVENTLOOP_SOURCE
149 extern "C" void wxObserverCallBack(CFRunLoopObserverRef observer
, CFRunLoopActivity activity
, void *info
)
151 wxCFEventLoop
* eventloop
= static_cast<wxCFEventLoop
*>(info
);
153 eventloop
->ObserverCallBack(observer
, activity
);
156 void wxCFEventLoop::ObserverCallBack(CFRunLoopObserverRef
WXUNUSED(observer
), int activity
)
158 if ( activity
& kCFRunLoopBeforeTimers
)
160 // process pending wx events first as they correspond to low-level events
161 // which happened before, i.e. typically pending events were queued by a
162 // previous call to Dispatch() and if we didn't process them now the next
163 // call to it might enqueue them again (as happens with e.g. socket events
164 // which would be generated as long as there is input available on socket
165 // and this input is only removed from it when pending event handlers are
169 wxTheApp
->ProcessPendingEvents();
172 if ( activity
& kCFRunLoopBeforeWaiting
)
189 wxCFEventLoop::wxCFEventLoop()
191 m_shouldExit
= false;
193 m_runLoop
= CFGetCurrentRunLoop();
195 CFRunLoopObserverContext ctxt
;
196 bzero( &ctxt
, sizeof(ctxt
) );
198 m_runLoopObserver
= CFRunLoopObserverCreate( kCFAllocatorDefault
, kCFRunLoopBeforeTimers
| kCFRunLoopBeforeWaiting
, true /* repeats */, 0,
199 wxObserverCallBack
, &ctxt
);
200 CFRunLoopAddObserver(m_runLoop
, m_runLoopObserver
, kCFRunLoopCommonModes
);
201 CFRelease(m_runLoopObserver
);
204 wxCFEventLoop::~wxCFEventLoop()
206 CFRunLoopRemoveObserver(m_runLoop
, m_runLoopObserver
, kCFRunLoopCommonModes
);
210 CFRunLoopRef
wxCFEventLoop::CFGetCurrentRunLoop() const
212 return CFRunLoopGetCurrent();
215 void wxCFEventLoop::WakeUp()
217 CFRunLoopWakeUp(m_runLoop
);
224 wxEventLoopBase
* const loop
= wxEventLoopBase::GetActive();
232 bool wxCFEventLoop::YieldFor(long eventsToProcess
)
235 // Yielding from a non-gui thread needs to bail out, otherwise we end up
236 // possibly sending events in the thread too.
237 if ( !wxThread::IsMain() )
241 #endif // wxUSE_THREADS
243 m_isInsideYield
= true;
244 m_eventsToProcessInsideYield
= eventsToProcess
;
247 // disable log flushing from here because a call to wxYield() shouldn't
248 // normally result in message boxes popping up &c
252 // process all pending events:
253 while ( DoProcessEvents() == 1 )
256 // it's necessary to call ProcessIdle() to update the frames sizes which
257 // might have been changed (it also will update other things set from
258 // OnUpdateUI() which is a nice (and desired) side effect)
259 while ( ProcessIdle() ) {}
261 // if there are pending events, we must process them.
263 wxTheApp
->ProcessPendingEvents();
268 m_isInsideYield
= false;
273 // implement/override base class pure virtual
274 bool wxCFEventLoop::Pending() const
279 int wxCFEventLoop::DoProcessEvents()
281 return DispatchTimeout( 0 );
284 bool wxCFEventLoop::Dispatch()
286 return DoProcessEvents() != 0;
289 int wxCFEventLoop::DispatchTimeout(unsigned long timeout
)
294 int status
= DoDispatchTimeout(timeout
);
312 int wxCFEventLoop::DoDispatchTimeout(unsigned long timeout
)
314 SInt32 status
= CFRunLoopRunInMode(kCFRunLoopDefaultMode
, timeout
/ 1000.0 , true);
317 case kCFRunLoopRunFinished
:
318 wxFAIL_MSG( "incorrect run loop state" );
320 case kCFRunLoopRunStopped
:
323 case kCFRunLoopRunTimedOut
:
326 case kCFRunLoopRunHandledSource
:
333 void wxCFEventLoop::DoRun()
337 // generate and process idle events for as long as we don't
338 // have anything else to do
341 // if the "should exit" flag is set, the loop should terminate
342 // but not before processing any remaining messages so while
343 // Pending() returns true, do process them
346 while ( DoProcessEvents() == 1 )
354 void wxCFEventLoop::DoStop()
356 CFRunLoopStop(CFGetCurrentRunLoop());
359 // enters a loop calling OnNextIteration(), Pending() and Dispatch() and
360 // terminating when Exit() is called
361 int wxCFEventLoop::Run()
363 // event loops are not recursive, you need to create another loop!
364 wxCHECK_MSG( !IsRunning(), -1, wxT("can't reenter a message loop") );
366 // ProcessIdle() and ProcessEvents() below may throw so the code here should
367 // be exception-safe, hence we must use local objects for all actions we
369 wxEventLoopActivator
activate(this);
371 // we must ensure that OnExit() is called even if an exception is thrown
372 // from inside ProcessEvents() but we must call it from Exit() in normal
373 // situations because it is supposed to be called synchronously,
374 // wxModalEventLoop depends on this (so we can't just use ON_BLOCK_EXIT or
375 // something similar here)
381 #endif // wxUSE_EXCEPTIONS
386 // exit the outer loop as well
393 if ( !wxTheApp
|| !wxTheApp
->OnExceptionInMainLoop() )
398 //else: continue running the event loop
402 // OnException() throwed, possibly rethrowing the same
403 // exception again: very good, but we still need OnExit() to
410 #endif // wxUSE_EXCEPTIONS
415 // sets the "should exit" flag and wakes up the loop so that it terminates
417 void wxCFEventLoop::Exit(int rc
)