1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/evtloop.mm
3 // Purpose: implementation of wxEventLoop for OS X
4 // Author: Vadim Zeitlin, Stefan Csomor
7 // RCS-ID: $Id: evtloop.cpp 54845 2008-07-30 14:52:41Z SC $
8 // Copyright: (c) 2006 Vadim Zeitlin <vadim@wxwindows.org>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/evtloop.h"
35 #include "wx/osx/private.h"
37 // ============================================================================
38 // wxEventLoop implementation
39 // ============================================================================
42 static int CalculateNSEventMaskFromEventCategory(wxEventCategory cat)
46 NSRightMouseDownMask |
47 NSRightMouseUpMask = 1 << NSRightMouseUp,
48 NSMouseMovedMask = 1 << NSMouseMoved,
49 NSLeftMouseDraggedMask = 1 << NSLeftMouseDragged,
50 NSRightMouseDraggedMask = 1 << NSRightMouseDragged,
51 NSMouseEnteredMask = 1 << NSMouseEntered,
52 NSMouseExitedMask = 1 << NSMouseExited,
53 NSScrollWheelMask = 1 << NSScrollWheel,
54 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
55 NSTabletPointMask = 1 << NSTabletPoint,
56 NSTabletProximityMask = 1 << NSTabletProximity,
58 NSOtherMouseDownMask = 1 << NSOtherMouseDown,
59 NSOtherMouseUpMask = 1 << NSOtherMouseUp,
60 NSOtherMouseDraggedMask = 1 << NSOtherMouseDragged,
64 NSKeyDownMask = 1 << NSKeyDown,
65 NSKeyUpMask = 1 << NSKeyUp,
66 NSFlagsChangedMask = 1 << NSFlagsChanged,
68 NSAppKitDefinedMask = 1 << NSAppKitDefined,
69 NSSystemDefinedMask = 1 << NSSystemDefined,
70 NSApplicationDefinedMask = 1 << NSApplicationDefined,
71 NSPeriodicMask = 1 << NSPeriodic,
72 NSCursorUpdateMask = 1 << NSCursorUpdate,
74 NSAnyEventMask = 0xffffffffU
78 wxGUIEventLoop::wxGUIEventLoop()
83 void wxGUIEventLoop::WakeUp()
85 extern void wxMacWakeUp();
90 //-----------------------------------------------------------------------------
91 // event loop sources operations
92 //-----------------------------------------------------------------------------
94 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
99 wxEventLoopSourceHandler* handler;
103 static void wx_cffiledescriptor_callback(CFFileDescriptorRef cffd,
104 CFOptionFlags flags, void* ctxData)
106 wxLogTrace(wxTRACE_EVT_SOURCE, "CFFileDescriptor Callback");
108 wx_cffd_data* data = static_cast<wx_cffd_data*>(ctxData);
109 wxEventLoopSourceHandler* handler = data->handler;
110 if (flags & kCFFileDescriptorReadCallBack)
111 handler->OnReadWaiting();
112 if (flags & kCFFileDescriptorWriteCallBack)
113 handler->OnWriteWaiting();
115 // reenable callbacks
116 if (data->flags & wxEVENT_SOURCE_INPUT)
117 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
118 if (data->flags & wxEVENT_SOURCE_OUTPUT)
119 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorWriteCallBack);
123 wxMacEventLoopSource* wxGUIEventLoop::CreateSource(int fd,
124 wxEventLoopSourceHandler* handler,
127 wxMacEventLoopSource* source = new wxMacEventLoopSource();
128 // FIXME this is currently a leak :-)
129 wx_cffd_data* data = new wx_cffd_data;
130 data->handler = handler;
132 CFFileDescriptorContext ctx = { 0, data, NULL, NULL, NULL };
133 CFFileDescriptorRef cffd = CFFileDescriptorCreate(kCFAllocatorDefault, fd,
134 true, wx_cffiledescriptor_callback, &ctx);
136 if (flags & wxEVENT_SOURCE_INPUT)
137 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
138 if (flags & wxEVENT_SOURCE_OUTPUT)
139 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorWriteCallBack);
142 CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, cffd, 0));
143 source->SetHandler(handler);
144 source->SetFlags(flags);
148 wxMacEventLoopSource* wxGUIEventLoop::CreateSource(int fd,
149 wxEventLoopSourceHandler* handler,
156 bool wxGUIEventLoop::DoAddSource(wxAbstractEventLoopSource* src)
158 Source* source = dynamic_cast<Source*>(src);
159 wxCHECK_MSG( source, false, "Invalid source type" );
161 wxLogTrace(wxTRACE_EVT_SOURCE,
162 "wxGUIEventLoop::AddSource() source=%d",
163 source->GetResource());
165 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
166 CFRunLoopRef cfloop = [nsloop getCFRunLoop];
167 CFRunLoopAddSource(cfloop, source->GetResource(), kCFRunLoopDefaultMode);
172 bool wxGUIEventLoop::DoRemoveSource(wxAbstractEventLoopSource* src)
174 Source* source = dynamic_cast<Source*>(src);
175 wxCHECK_MSG( source, false, "Invalid source type" );
177 wxLogTrace(wxTRACE_EVT_SOURCE,
178 "wxGUIEventLoop::RemoveSource() source=%d",
179 source->GetResource());
181 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
182 CFRunLoopRef cfloop = [nsloop getCFRunLoop];
183 CFRunLoopRemoveSource(cfloop, source->GetResource(), kCFRunLoopDefaultMode);
188 //-----------------------------------------------------------------------------
189 // events dispatch and loop handling
190 //-----------------------------------------------------------------------------
192 bool wxGUIEventLoop::Pending() const
194 wxMacAutoreleasePool autoreleasepool;
195 // a pointer to the event is returned if there is one, or nil if not
196 return [[NSApplication sharedApplication]
197 nextEventMatchingMask: NSAnyEventMask
199 inMode: NSDefaultRunLoopMode
203 bool wxGUIEventLoop::Dispatch()
208 wxMacAutoreleasePool autoreleasepool;
210 if(NSEvent *event = [NSApp
211 nextEventMatchingMask:NSAnyEventMask
212 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
213 inMode:NSDefaultRunLoopMode
217 [NSApp sendEvent: event];
222 wxTheApp->ProcessPendingEvents();
224 if ( wxTheApp->ProcessIdle() )
240 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
243 // Yielding from a non-gui thread needs to bail out, otherwise we end up
244 // possibly sending events in the thread too.
245 if ( !wxThread::IsMain() )
249 #endif // wxUSE_THREADS
251 m_isInsideYield = true;
252 m_eventsToProcessInsideYield = eventsToProcess;
255 // disable log flushing from here because a call to wxYield() shouldn't
256 // normally result in message boxes popping up &c
260 // process all pending events:
264 // it's necessary to call ProcessIdle() to update the frames sizes which
265 // might have been changed (it also will update other things set from
266 // OnUpdateUI() which is a nice (and desired) side effect)
267 while ( ProcessIdle() ) {}
269 // if there are pending events, we must process them.
271 wxTheApp->ProcessPendingEvents();
276 m_isInsideYield = false;
281 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
283 wxMacAutoreleasePool autoreleasepool;
285 NSEvent *event = [NSApp
286 nextEventMatchingMask:NSAnyEventMask
287 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
288 inMode:NSDefaultRunLoopMode
293 [NSApp sendEvent: event];