2 * MACDRV Cocoa event queue code
4 * Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <sys/types.h>
22 #include <sys/event.h>
24 #include <libkern/OSAtomic.h>
26 #include "macdrv_cocoa.h"
27 #import "cocoa_event.h"
29 #import "cocoa_window.h"
32 static NSString* const WineEventQueueThreadDictionaryKey = @"WineEventQueueThreadDictionaryKey";
35 @interface MacDrvEvent : NSObject
41 - (id) initWithEvent:(macdrv_event*)event;
45 @implementation MacDrvEvent
47 - (id) initWithEvent:(macdrv_event*)inEvent
52 event = macdrv_retain_event(inEvent);
59 if (event) macdrv_release_event(event);
66 @implementation WineEventQueue
70 [self doesNotRecognizeSelector:_cmd];
75 - (id) initWithEventHandler:(macdrv_event_handler)handler
77 NSParameterAssert(handler != nil);
85 fds[0] = fds[1] = kq = -1;
87 event_handler = handler;
88 events = [[NSMutableArray alloc] init];
89 eventsLock = [[NSLock alloc] init];
91 if (!events || !eventsLock)
98 fcntl(fds[0], F_SETFD, 1) == -1 ||
99 fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
100 fcntl(fds[1], F_SETFD, 1) == -1 ||
101 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1)
114 EV_SET(&kev, fds[0], EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
117 rc = kevent(kq, &kev, 1, NULL, 0, NULL);
118 } while (rc == -1 && errno == EINTR);
131 [eventsLock release];
133 if (kq != -1) close(kq);
134 if (fds[0] != -1) close(fds[0]);
135 if (fds[1] != -1) close(fds[1]);
140 - (void) signalEventAvailable
147 rc = write(fds[1], &junk, 1);
148 } while (rc < 0 && errno == EINTR);
150 if (rc < 0 && errno != EAGAIN)
151 ERR(@"%@: got error writing to event queue signaling pipe: %s\n", self, strerror(errno));
154 - (void) postEventObject:(MacDrvEvent*)event
156 MacDrvEvent* lastEvent;
160 if ((event->event->type == MOUSE_MOVED ||
161 event->event->type == MOUSE_MOVED_ABSOLUTE) &&
162 (lastEvent = [events lastObject]) &&
163 (lastEvent->event->type == MOUSE_MOVED ||
164 lastEvent->event->type == MOUSE_MOVED_ABSOLUTE) &&
165 lastEvent->event->window == event->event->window)
167 if (event->event->type == MOUSE_MOVED)
169 lastEvent->event->mouse_moved.x += event->event->mouse_moved.x;
170 lastEvent->event->mouse_moved.y += event->event->mouse_moved.y;
174 lastEvent->event->type = MOUSE_MOVED_ABSOLUTE;
175 lastEvent->event->mouse_moved.x = event->event->mouse_moved.x;
176 lastEvent->event->mouse_moved.y = event->event->mouse_moved.y;
179 lastEvent->event->mouse_moved.time_ms = event->event->mouse_moved.time_ms;
182 [events addObject:event];
186 [self signalEventAvailable];
189 - (void) postEvent:(macdrv_event*)inEvent
191 MacDrvEvent* event = [[MacDrvEvent alloc] initWithEvent:inEvent];
192 [self postEventObject:event];
196 - (MacDrvEvent*) getEventMatchingMask:(macdrv_event_mask)mask
201 MacDrvEvent* ret = nil;
203 /* Clear the pipe which signals there are pending events. */
206 rc = read(fds[0], buf, sizeof(buf));
207 } while (rc > 0 || (rc < 0 && errno == EINTR));
208 if (rc == 0 || (rc < 0 && errno != EAGAIN))
211 ERR(@"%@: event queue signaling pipe unexpectedly closed\n", self);
213 ERR(@"%@: got error reading from event queue signaling pipe: %s\n", self, strerror(errno));
220 while (index < [events count])
222 MacDrvEvent* event = [events objectAtIndex:index];
223 if (event_mask_for_type(event->event->type) & mask)
225 [[event retain] autorelease];
226 [events removeObjectAtIndex:index];
228 if (event->event->deliver == INT_MAX ||
229 OSAtomicDecrement32Barrier(&event->event->deliver) >= 0)
243 - (void) discardEventsMatchingMask:(macdrv_event_mask)mask forWindow:(NSWindow*)window
249 indexes = [events indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
250 MacDrvEvent* event = obj;
251 return ((event_mask_for_type(event->event->type) & mask) &&
252 (!window || event->event->window == (macdrv_window)window));
255 [events removeObjectsAtIndexes:indexes];
260 - (BOOL) query:(macdrv_query*)query timeout:(NSTimeInterval)timeout processEvents:(BOOL)processEvents
263 NSDate* timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
266 event = macdrv_create_event(QUERY_EVENT, (WineWindow*)query->window);
267 event->query_event.query = macdrv_retain_query(query);
270 [self postEvent:event];
271 macdrv_release_event(event);
272 timedout = ![[WineApplicationController sharedController] waitUntilQueryDone:&query->done
274 processEvents:processEvents];
275 return !timedout && query->status;
278 - (BOOL) query:(macdrv_query*)query timeout:(NSTimeInterval)timeout
280 return [self query:query timeout:timeout processEvents:FALSE];
284 /***********************************************************************
287 * Run a block on the main thread synchronously.
289 void OnMainThread(dispatch_block_t block)
291 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
292 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
293 WineEventQueue* queue = [threadDict objectForKey:WineEventQueueThreadDictionaryKey];
294 __block BOOL finished;
298 /* Fall back to synchronous dispatch without handling query events. */
299 dispatch_sync(dispatch_get_main_queue(), block);
308 [queue signalEventAvailable];
313 MacDrvEvent* macDrvEvent;
317 (macDrvEvent = [queue getEventMatchingMask:event_mask_for_type(QUERY_EVENT)]))
319 queue->event_handler(macDrvEvent->event);
325 pool = [[NSAutoreleasePool alloc] init];
327 kevent(queue->kq, NULL, 0, &kev, 1, NULL);
335 /***********************************************************************
336 * macdrv_create_event_queue
338 * Register this thread with the application on the main thread, and set
339 * up an event queue on which it can deliver events to this thread.
341 macdrv_event_queue macdrv_create_event_queue(macdrv_event_handler handler)
343 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
344 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
346 WineEventQueue* queue = [threadDict objectForKey:WineEventQueueThreadDictionaryKey];
349 queue = [[[WineEventQueue alloc] initWithEventHandler:handler] autorelease];
352 if ([[WineApplicationController sharedController] registerEventQueue:queue])
353 [threadDict setObject:queue forKey:WineEventQueueThreadDictionaryKey];
360 return (macdrv_event_queue)queue;
363 /***********************************************************************
364 * macdrv_destroy_event_queue
366 * Tell the application that this thread is exiting and destroy the
367 * associated event queue.
369 void macdrv_destroy_event_queue(macdrv_event_queue queue)
371 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
372 WineEventQueue* q = (WineEventQueue*)queue;
373 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
375 [[WineApplicationController sharedController] unregisterEventQueue:q];
376 [threadDict removeObjectForKey:WineEventQueueThreadDictionaryKey];
381 /***********************************************************************
382 * macdrv_get_event_queue_fd
384 * Get the file descriptor whose readability signals that there are
385 * events on the event queue.
387 int macdrv_get_event_queue_fd(macdrv_event_queue queue)
389 WineEventQueue* q = (WineEventQueue*)queue;
393 /***********************************************************************
394 * macdrv_copy_event_from_queue
396 * Pull an event matching the event mask from the event queue and store
397 * it in the event record pointed to by the event parameter. If a
398 * matching event was found, return non-zero; otherwise, return 0.
400 * The caller is responsible for calling macdrv_release_event on any
401 * event returned by this function.
403 int macdrv_copy_event_from_queue(macdrv_event_queue queue,
404 macdrv_event_mask mask, macdrv_event **event)
406 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
407 WineEventQueue* q = (WineEventQueue*)queue;
409 MacDrvEvent* macDrvEvent = [q getEventMatchingMask:mask];
411 *event = macdrv_retain_event(macDrvEvent->event);
414 return (macDrvEvent != nil);
417 /***********************************************************************
418 * macdrv_create_event
420 macdrv_event* macdrv_create_event(int type, WineWindow* window)
424 event = calloc(1, sizeof(*event));
426 event->deliver = INT_MAX;
428 event->window = (macdrv_window)[window retain];
432 /***********************************************************************
433 * macdrv_retain_event
435 macdrv_event* macdrv_retain_event(macdrv_event *event)
437 OSAtomicIncrement32Barrier(&event->refs);
441 /***********************************************************************
442 * macdrv_release_event
444 * Decrements the reference count of an event. If the count falls to
445 * zero, cleans up any resources, such as allocated memory or retained
446 * objects, held by the event and deallocates it
448 void macdrv_release_event(macdrv_event *event)
450 if (OSAtomicDecrement32Barrier(&event->refs) <= 0)
452 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
457 if (event->im_set_text.text)
458 CFRelease(event->im_set_text.text);
460 case KEYBOARD_CHANGED:
461 CFRelease(event->keyboard_changed.uchr);
464 macdrv_release_query(event->query_event.query);
466 case WINDOW_GOT_FOCUS:
467 [(NSMutableSet*)event->window_got_focus.tried_windows release];
471 [(WineWindow*)event->window release];
478 /***********************************************************************
479 * macdrv_create_query
481 macdrv_query* macdrv_create_query(void)
485 query = calloc(1, sizeof(*query));
490 /***********************************************************************
491 * macdrv_retain_query
493 macdrv_query* macdrv_retain_query(macdrv_query *query)
495 OSAtomicIncrement32Barrier(&query->refs);
499 /***********************************************************************
500 * macdrv_release_query
502 void macdrv_release_query(macdrv_query *query)
504 if (OSAtomicDecrement32Barrier(&query->refs) <= 0)
508 case QUERY_DRAG_OPERATION:
509 if (query->drag_operation.pasteboard)
510 CFRelease(query->drag_operation.pasteboard);
512 case QUERY_DRAG_DROP:
513 if (query->drag_drop.pasteboard)
514 CFRelease(query->drag_drop.pasteboard);
516 case QUERY_PASTEBOARD_DATA:
517 if (query->pasteboard_data.type)
518 CFRelease(query->pasteboard_data.type);
521 [(WineWindow*)query->window release];
526 /***********************************************************************
527 * macdrv_set_query_done
529 void macdrv_set_query_done(macdrv_query *query)
531 macdrv_retain_query(query);
537 macdrv_release_query(query);
539 event = [NSEvent otherEventWithType:NSApplicationDefined
542 timestamp:[[NSProcessInfo processInfo] systemUptime]
545 subtype:WineApplicationEventWakeQuery
548 [NSApp postEvent:event atStart:TRUE];