]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/evtloop.mm
78f7749d7ea39c386ae6a892dab057a1bbce94e3
[wxWidgets.git] / src / osx / cocoa / evtloop.mm
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/evtloop.mm
3 // Purpose: implementation of wxEventLoop for OS X
4 // Author: Vadim Zeitlin, Stefan Csomor
5 // Modified by:
6 // Created: 2006-01-12
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 ///////////////////////////////////////////////////////////////////////////////
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/app.h"
31 #endif // WX_PRECOMP
32
33 #include "wx/log.h"
34
35 #include "wx/osx/private.h"
36
37 // ============================================================================
38 // wxEventLoop implementation
39 // ============================================================================
40
41 /*
42 static int CalculateNSEventMaskFromEventCategory(wxEventCategory cat)
43 {
44 NSLeftMouseDownMask |
45 NSLeftMouseUpMask |
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,
57 #endif
58 NSOtherMouseDownMask = 1 << NSOtherMouseDown,
59 NSOtherMouseUpMask = 1 << NSOtherMouseUp,
60 NSOtherMouseDraggedMask = 1 << NSOtherMouseDragged,
61
62
63
64 NSKeyDownMask = 1 << NSKeyDown,
65 NSKeyUpMask = 1 << NSKeyUp,
66 NSFlagsChangedMask = 1 << NSFlagsChanged,
67
68 NSAppKitDefinedMask = 1 << NSAppKitDefined,
69 NSSystemDefinedMask = 1 << NSSystemDefined,
70 NSApplicationDefinedMask = 1 << NSApplicationDefined,
71 NSPeriodicMask = 1 << NSPeriodic,
72 NSCursorUpdateMask = 1 << NSCursorUpdate,
73
74 NSAnyEventMask = 0xffffffffU
75 }
76 */
77
78 wxGUIEventLoop::wxGUIEventLoop()
79 {
80 m_sleepTime = 0.0;
81 }
82
83 void wxGUIEventLoop::WakeUp()
84 {
85 extern void wxMacWakeUp();
86
87 wxMacWakeUp();
88 }
89
90 //-----------------------------------------------------------------------------
91 // event loop sources operations
92 //-----------------------------------------------------------------------------
93
94 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
95 extern "C"
96 {
97 struct wx_cffd_data
98 {
99 wxEventLoopSourceHandler* handler;
100 int flags;
101 };
102
103 static void wx_cffiledescriptor_callback(CFFileDescriptorRef cffd,
104 CFOptionFlags flags, void* ctxData)
105 {
106 wxLogTrace(wxTRACE_EVT_SOURCE, "CFFileDescriptor Callback");
107
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();
114
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);
120 }
121 }
122
123 wxMacEventLoopSource* wxGUIEventLoop::CreateSource(int fd,
124 wxEventLoopSourceHandler* handler,
125 int flags) const
126 {
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;
131 data->flags = flags;
132 CFFileDescriptorContext ctx = { 0, data, NULL, NULL, NULL };
133 CFFileDescriptorRef cffd = CFFileDescriptorCreate(kCFAllocatorDefault, fd,
134 true, wx_cffiledescriptor_callback, &ctx);
135
136 if (flags & wxEVENT_SOURCE_INPUT)
137 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
138 if (flags & wxEVENT_SOURCE_OUTPUT)
139 CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorWriteCallBack);
140
141 source->SetResource(
142 CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, cffd, 0));
143 source->SetHandler(handler);
144 source->SetFlags(flags);
145 return source;
146 }
147 #elif
148 wxMacEventLoopSource* wxGUIEventLoop::CreateSource(int fd,
149 wxEventLoopSourceHandler* handler,
150 int flags) const
151 {
152 return NULL;
153 }
154 #endif
155
156 bool wxGUIEventLoop::DoAddSource(wxAbstractEventLoopSource* src)
157 {
158 Source* source = dynamic_cast<Source*>(src);
159 wxCHECK_MSG( source, false, "Invalid source type" );
160
161 wxLogTrace(wxTRACE_EVT_SOURCE,
162 "wxGUIEventLoop::AddSource() source=%d",
163 source->GetResource());
164
165 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
166 CFRunLoopRef cfloop = [nsloop getCFRunLoop];
167 CFRunLoopAddSource(cfloop, source->GetResource(), kCFRunLoopDefaultMode);
168
169 return true;
170 }
171
172 bool wxGUIEventLoop::DoRemoveSource(wxAbstractEventLoopSource* src)
173 {
174 Source* source = dynamic_cast<Source*>(src);
175 wxCHECK_MSG( source, false, "Invalid source type" );
176
177 wxLogTrace(wxTRACE_EVT_SOURCE,
178 "wxGUIEventLoop::RemoveSource() source=%d",
179 source->GetResource());
180
181 NSRunLoop* nsloop = [NSRunLoop currentRunLoop];
182 CFRunLoopRef cfloop = [nsloop getCFRunLoop];
183 CFRunLoopRemoveSource(cfloop, source->GetResource(), kCFRunLoopDefaultMode);
184
185 return true;
186 }
187
188 //-----------------------------------------------------------------------------
189 // events dispatch and loop handling
190 //-----------------------------------------------------------------------------
191
192 bool wxGUIEventLoop::Pending() const
193 {
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
198 untilDate: nil
199 inMode: NSDefaultRunLoopMode
200 dequeue: NO];
201 }
202
203 bool wxGUIEventLoop::Dispatch()
204 {
205 if ( !wxTheApp )
206 return false;
207
208 wxMacAutoreleasePool autoreleasepool;
209
210 if(NSEvent *event = [NSApp
211 nextEventMatchingMask:NSAnyEventMask
212 untilDate:[NSDate dateWithTimeIntervalSinceNow: m_sleepTime]
213 inMode:NSDefaultRunLoopMode
214 dequeue: YES])
215 {
216 m_sleepTime = 0.0;
217 [NSApp sendEvent: event];
218 }
219 else
220 {
221 if (wxTheApp)
222 wxTheApp->ProcessPendingEvents();
223
224 if ( wxTheApp->ProcessIdle() )
225 m_sleepTime = 0.0 ;
226 else
227 {
228 m_sleepTime = 1.0;
229 #if wxUSE_THREADS
230 wxMutexGuiLeave();
231 wxMilliSleep(20);
232 wxMutexGuiEnter();
233 #endif
234 }
235 }
236
237 return true;
238 }
239
240 bool wxGUIEventLoop::YieldFor(long eventsToProcess)
241 {
242 #if wxUSE_THREADS
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() )
246 {
247 return true;
248 }
249 #endif // wxUSE_THREADS
250
251 m_isInsideYield = true;
252 m_eventsToProcessInsideYield = eventsToProcess;
253
254 #if wxUSE_LOG
255 // disable log flushing from here because a call to wxYield() shouldn't
256 // normally result in message boxes popping up &c
257 wxLog::Suspend();
258 #endif // wxUSE_LOG
259
260 // process all pending events:
261 while ( Pending() )
262 Dispatch();
263
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() ) {}
268
269 // if there are pending events, we must process them.
270 if (wxTheApp)
271 wxTheApp->ProcessPendingEvents();
272
273 #if wxUSE_LOG
274 wxLog::Resume();
275 #endif // wxUSE_LOG
276 m_isInsideYield = false;
277
278 return true;
279 }
280
281 int wxGUIEventLoop::DispatchTimeout(unsigned long timeout)
282 {
283 wxMacAutoreleasePool autoreleasepool;
284
285 NSEvent *event = [NSApp
286 nextEventMatchingMask:NSAnyEventMask
287 untilDate:[NSDate dateWithTimeIntervalSinceNow: timeout/1000]
288 inMode:NSDefaultRunLoopMode
289 dequeue: YES];
290 if ( !event )
291 return -1;
292
293 [NSApp sendEvent: event];
294
295 return true;
296 }