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>
25 #import <Carbon/Carbon.h>
27 #include "macdrv_cocoa.h"
28 #import "cocoa_event.h"
30 #import "cocoa_window.h"
33 static NSString* const WineEventQueueThreadDictionaryKey = @"WineEventQueueThreadDictionaryKey";
35 static NSString* const WineHotKeyMacIDKey = @"macID";
36 static NSString* const WineHotKeyVkeyKey = @"vkey";
37 static NSString* const WineHotKeyModFlagsKey = @"modFlags";
38 static NSString* const WineHotKeyKeyCodeKey = @"keyCode";
39 static NSString* const WineHotKeyCarbonRefKey = @"hotKeyRef";
40 static const OSType WineHotKeySignature = 'Wine';
43 @interface MacDrvEvent : NSObject
49 - (id) initWithEvent:(macdrv_event*)event;
53 @implementation MacDrvEvent
55 - (id) initWithEvent:(macdrv_event*)inEvent
60 event = macdrv_retain_event(inEvent);
67 if (event) macdrv_release_event(event);
74 @implementation WineEventQueue
78 [self doesNotRecognizeSelector:_cmd];
83 - (id) initWithEventHandler:(macdrv_event_handler)handler
85 NSParameterAssert(handler != nil);
93 fds[0] = fds[1] = kq = -1;
95 event_handler = handler;
96 events = [[NSMutableArray alloc] init];
97 eventsLock = [[NSLock alloc] init];
99 if (!events || !eventsLock)
106 fcntl(fds[0], F_SETFD, 1) == -1 ||
107 fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
108 fcntl(fds[1], F_SETFD, 1) == -1 ||
109 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1)
122 EV_SET(&kev, fds[0], EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
125 rc = kevent(kq, &kev, 1, NULL, 0, NULL);
126 } while (rc == -1 && errno == EINTR);
138 NSNumber* hotKeyMacID;
140 for (hotKeyMacID in hotKeysByMacID)
142 NSDictionary* hotKeyDict = [hotKeysByMacID objectForKey:hotKeyMacID];
143 EventHotKeyRef hotKeyRef = [[hotKeyDict objectForKey:WineHotKeyCarbonRefKey] pointerValue];
144 UnregisterEventHotKey(hotKeyRef);
146 [hotKeysByMacID release];
147 [hotKeysByWinID release];
149 [eventsLock release];
151 if (kq != -1) close(kq);
152 if (fds[0] != -1) close(fds[0]);
153 if (fds[1] != -1) close(fds[1]);
158 - (void) signalEventAvailable
165 rc = write(fds[1], &junk, 1);
166 } while (rc < 0 && errno == EINTR);
168 if (rc < 0 && errno != EAGAIN)
169 ERR(@"%@: got error writing to event queue signaling pipe: %s\n", self, strerror(errno));
172 - (void) postEventObject:(MacDrvEvent*)event
175 MacDrvEvent* lastEvent;
179 indexes = [events indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
180 return ((MacDrvEvent*)obj)->event->deliver <= 0;
182 [events removeObjectsAtIndexes:indexes];
184 if ((event->event->type == MOUSE_MOVED ||
185 event->event->type == MOUSE_MOVED_ABSOLUTE) &&
186 event->event->deliver == INT_MAX &&
187 (lastEvent = [events lastObject]) &&
188 (lastEvent->event->type == MOUSE_MOVED ||
189 lastEvent->event->type == MOUSE_MOVED_ABSOLUTE) &&
190 lastEvent->event->deliver == INT_MAX &&
191 lastEvent->event->window == event->event->window &&
192 lastEvent->event->mouse_moved.drag == event->event->mouse_moved.drag)
194 if (event->event->type == MOUSE_MOVED)
196 lastEvent->event->mouse_moved.x += event->event->mouse_moved.x;
197 lastEvent->event->mouse_moved.y += event->event->mouse_moved.y;
201 lastEvent->event->type = MOUSE_MOVED_ABSOLUTE;
202 lastEvent->event->mouse_moved.x = event->event->mouse_moved.x;
203 lastEvent->event->mouse_moved.y = event->event->mouse_moved.y;
206 lastEvent->event->mouse_moved.time_ms = event->event->mouse_moved.time_ms;
209 [events addObject:event];
213 [self signalEventAvailable];
216 - (void) postEvent:(macdrv_event*)inEvent
218 MacDrvEvent* event = [[MacDrvEvent alloc] initWithEvent:inEvent];
219 [self postEventObject:event];
223 - (MacDrvEvent*) getEventMatchingMask:(macdrv_event_mask)mask
228 MacDrvEvent* ret = nil;
230 /* Clear the pipe which signals there are pending events. */
233 rc = read(fds[0], buf, sizeof(buf));
234 } while (rc > 0 || (rc < 0 && errno == EINTR));
235 if (rc == 0 || (rc < 0 && errno != EAGAIN))
238 ERR(@"%@: event queue signaling pipe unexpectedly closed\n", self);
240 ERR(@"%@: got error reading from event queue signaling pipe: %s\n", self, strerror(errno));
247 while (index < [events count])
249 MacDrvEvent* event = [events objectAtIndex:index];
250 if (event_mask_for_type(event->event->type) & mask)
252 [[event retain] autorelease];
253 [events removeObjectAtIndex:index];
255 if (event->event->deliver == INT_MAX ||
256 OSAtomicDecrement32Barrier(&event->event->deliver) >= 0)
270 - (void) discardEventsPassingTest:(BOOL (^)(macdrv_event* event))block
272 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
277 indexes = [events indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
278 MacDrvEvent* event = obj;
279 return block(event->event);
282 [events removeObjectsAtIndexes:indexes];
289 - (void) discardEventsMatchingMask:(macdrv_event_mask)mask forWindow:(NSWindow*)window
291 [self discardEventsPassingTest:^BOOL (macdrv_event* event){
292 return ((event_mask_for_type(event->type) & mask) &&
293 (!window || event->window == (macdrv_window)window));
297 - (BOOL) query:(macdrv_query*)query timeout:(NSTimeInterval)timeout flags:(NSUInteger)flags
301 NSDate* timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
304 type = (flags & WineQueryNoPreemptWait) ? QUERY_EVENT_NO_PREEMPT_WAIT : QUERY_EVENT;
305 event = macdrv_create_event(type, (WineWindow*)query->window);
306 event->query_event.query = macdrv_retain_query(query);
309 [self postEvent:event];
310 macdrv_release_event(event);
311 timedout = ![[WineApplicationController sharedController] waitUntilQueryDone:&query->done
313 processEvents:(flags & WineQueryProcessEvents) != 0];
314 return !timedout && query->status;
317 - (BOOL) query:(macdrv_query*)query timeout:(NSTimeInterval)timeout
319 return [self query:query timeout:timeout flags:0];
322 - (void) resetMouseEventPositions:(CGPoint)pos
328 for (event in events)
330 if (event->event->type == MOUSE_BUTTON)
332 event->event->mouse_button.x = pos.x;
333 event->event->mouse_button.y = pos.y;
335 else if (event->event->type == MOUSE_SCROLL)
337 event->event->mouse_scroll.x = pos.x;
338 event->event->mouse_scroll.y = pos.y;
345 - (BOOL) postHotKeyEvent:(UInt32)hotKeyNumber time:(double)time
347 NSDictionary* hotKeyDict = [hotKeysByMacID objectForKey:[NSNumber numberWithUnsignedInt:hotKeyNumber]];
352 event = macdrv_create_event(HOTKEY_PRESS, nil);
353 event->hotkey_press.vkey = [[hotKeyDict objectForKey:WineHotKeyVkeyKey] unsignedIntValue];
354 event->hotkey_press.mod_flags = [[hotKeyDict objectForKey:WineHotKeyModFlagsKey] unsignedIntValue];
355 event->hotkey_press.keycode = [[hotKeyDict objectForKey:WineHotKeyKeyCodeKey] unsignedIntValue];
356 event->hotkey_press.time_ms = [[WineApplicationController sharedController] ticksForEventTime:time];
358 [self postEvent:event];
360 macdrv_release_event(event);
363 return hotKeyDict != nil;
366 static OSStatus HotKeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
368 WineEventQueue* self = userData;
370 EventHotKeyID hotKeyID;
372 status = GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL,
373 sizeof(hotKeyID), NULL, &hotKeyID);
376 if (hotKeyID.signature != WineHotKeySignature ||
377 ![self postHotKeyEvent:hotKeyID.id time:GetEventTime(theEvent)])
378 status = eventNotHandledErr;
384 - (void) unregisterHotKey:(unsigned int)vkey modFlags:(unsigned int)modFlags
386 NSNumber* vkeyNumber = [NSNumber numberWithUnsignedInt:vkey];
387 NSNumber* modFlagsNumber = [NSNumber numberWithUnsignedInt:modFlags];
388 NSArray* winIDPair = [NSArray arrayWithObjects:vkeyNumber, modFlagsNumber, nil];
389 NSDictionary* hotKeyDict = [hotKeysByWinID objectForKey:winIDPair];
392 EventHotKeyRef hotKeyRef = [[hotKeyDict objectForKey:WineHotKeyCarbonRefKey] pointerValue];
393 NSNumber* macID = [hotKeyDict objectForKey:WineHotKeyMacIDKey];
395 UnregisterEventHotKey(hotKeyRef);
396 [hotKeysByMacID removeObjectForKey:macID];
397 [hotKeysByWinID removeObjectForKey:winIDPair];
401 - (int) registerHotKey:(UInt32)keyCode modifiers:(UInt32)modifiers vkey:(unsigned int)vkey modFlags:(unsigned int)modFlags
403 static EventHandlerRef handler;
404 static UInt32 hotKeyNumber;
406 NSNumber* vkeyNumber;
407 NSNumber* modFlagsNumber;
409 EventHotKeyID hotKeyID;
410 EventHotKeyRef hotKeyRef;
411 NSNumber* macIDNumber;
412 NSDictionary* hotKeyDict;
416 EventTypeSpec eventType = { kEventClassKeyboard, kEventHotKeyPressed };
417 status = InstallApplicationEventHandler(HotKeyHandler, 1, &eventType, self, &handler);
420 ERR(@"InstallApplicationEventHandler() failed: %d\n", status);
422 return MACDRV_HOTKEY_FAILURE;
426 if (!hotKeysByMacID && !(hotKeysByMacID = [[NSMutableDictionary alloc] init]))
427 return MACDRV_HOTKEY_FAILURE;
428 if (!hotKeysByWinID && !(hotKeysByWinID = [[NSMutableDictionary alloc] init]))
429 return MACDRV_HOTKEY_FAILURE;
431 vkeyNumber = [NSNumber numberWithUnsignedInt:vkey];
432 modFlagsNumber = [NSNumber numberWithUnsignedInt:modFlags];
433 winIDPair = [NSArray arrayWithObjects:vkeyNumber, modFlagsNumber, nil];
434 if ([hotKeysByWinID objectForKey:winIDPair])
435 return MACDRV_HOTKEY_ALREADY_REGISTERED;
437 hotKeyID.signature = WineHotKeySignature;
438 hotKeyID.id = hotKeyNumber++;
440 status = RegisterEventHotKey(keyCode, modifiers, hotKeyID, GetApplicationEventTarget(),
441 kEventHotKeyExclusive, &hotKeyRef);
442 if (status == eventHotKeyExistsErr)
443 return MACDRV_HOTKEY_ALREADY_REGISTERED;
446 ERR(@"RegisterEventHotKey() failed: %d\n", status);
447 return MACDRV_HOTKEY_FAILURE;
450 macIDNumber = [NSNumber numberWithUnsignedInt:hotKeyID.id];
451 hotKeyDict = [NSDictionary dictionaryWithObjectsAndKeys:
452 macIDNumber, WineHotKeyMacIDKey,
453 vkeyNumber, WineHotKeyVkeyKey,
454 modFlagsNumber, WineHotKeyModFlagsKey,
455 [NSNumber numberWithUnsignedInt:keyCode], WineHotKeyKeyCodeKey,
456 [NSValue valueWithPointer:hotKeyRef], WineHotKeyCarbonRefKey,
458 [hotKeysByMacID setObject:hotKeyDict forKey:macIDNumber];
459 [hotKeysByWinID setObject:hotKeyDict forKey:winIDPair];
461 return MACDRV_HOTKEY_SUCCESS;
465 /***********************************************************************
468 * Run a block on the main thread synchronously.
470 void OnMainThread(dispatch_block_t block)
472 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
473 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
474 WineEventQueue* queue = [threadDict objectForKey:WineEventQueueThreadDictionaryKey];
475 dispatch_semaphore_t semaphore = NULL;
476 __block BOOL finished;
480 semaphore = dispatch_semaphore_create(0);
481 dispatch_retain(semaphore);
489 [queue signalEventAvailable];
492 dispatch_semaphore_signal(semaphore);
493 dispatch_release(semaphore);
501 MacDrvEvent* macDrvEvent;
505 (macDrvEvent = [queue getEventMatchingMask:event_mask_for_type(QUERY_EVENT)]))
507 queue->event_handler(macDrvEvent->event);
513 pool = [[NSAutoreleasePool alloc] init];
515 kevent(queue->kq, NULL, 0, &kev, 1, NULL);
521 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
522 dispatch_release(semaphore);
529 /***********************************************************************
530 * macdrv_create_event_queue
532 * Register this thread with the application on the main thread, and set
533 * up an event queue on which it can deliver events to this thread.
535 macdrv_event_queue macdrv_create_event_queue(macdrv_event_handler handler)
537 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
538 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
540 WineEventQueue* queue = [threadDict objectForKey:WineEventQueueThreadDictionaryKey];
543 queue = [[[WineEventQueue alloc] initWithEventHandler:handler] autorelease];
546 if ([[WineApplicationController sharedController] registerEventQueue:queue])
547 [threadDict setObject:queue forKey:WineEventQueueThreadDictionaryKey];
554 return (macdrv_event_queue)queue;
557 /***********************************************************************
558 * macdrv_destroy_event_queue
560 * Tell the application that this thread is exiting and destroy the
561 * associated event queue.
563 void macdrv_destroy_event_queue(macdrv_event_queue queue)
565 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
566 WineEventQueue* q = (WineEventQueue*)queue;
567 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
569 [[WineApplicationController sharedController] unregisterEventQueue:q];
570 [threadDict removeObjectForKey:WineEventQueueThreadDictionaryKey];
575 /***********************************************************************
576 * macdrv_get_event_queue_fd
578 * Get the file descriptor whose readability signals that there are
579 * events on the event queue.
581 int macdrv_get_event_queue_fd(macdrv_event_queue queue)
583 WineEventQueue* q = (WineEventQueue*)queue;
587 /***********************************************************************
588 * macdrv_copy_event_from_queue
590 * Pull an event matching the event mask from the event queue and store
591 * it in the event record pointed to by the event parameter. If a
592 * matching event was found, return non-zero; otherwise, return 0.
594 * The caller is responsible for calling macdrv_release_event on any
595 * event returned by this function.
597 int macdrv_copy_event_from_queue(macdrv_event_queue queue,
598 macdrv_event_mask mask, macdrv_event **event)
600 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
601 WineEventQueue* q = (WineEventQueue*)queue;
603 MacDrvEvent* macDrvEvent = [q getEventMatchingMask:mask];
605 *event = macdrv_retain_event(macDrvEvent->event);
608 return (macDrvEvent != nil);
611 /***********************************************************************
612 * macdrv_create_event
614 macdrv_event* macdrv_create_event(int type, WineWindow* window)
618 event = calloc(1, sizeof(*event));
620 event->deliver = INT_MAX;
622 event->window = (macdrv_window)[window retain];
626 /***********************************************************************
627 * macdrv_retain_event
629 macdrv_event* macdrv_retain_event(macdrv_event *event)
631 OSAtomicIncrement32Barrier(&event->refs);
635 /***********************************************************************
636 * macdrv_release_event
638 * Decrements the reference count of an event. If the count falls to
639 * zero, cleans up any resources, such as allocated memory or retained
640 * objects, held by the event and deallocates it
642 void macdrv_release_event(macdrv_event *event)
644 if (OSAtomicDecrement32Barrier(&event->refs) <= 0)
646 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
651 if (event->im_set_text.text)
652 CFRelease(event->im_set_text.text);
654 case KEYBOARD_CHANGED:
655 CFRelease(event->keyboard_changed.uchr);
656 CFRelease(event->keyboard_changed.input_source);
659 case QUERY_EVENT_NO_PREEMPT_WAIT:
660 macdrv_release_query(event->query_event.query);
662 case WINDOW_GOT_FOCUS:
663 [(NSMutableSet*)event->window_got_focus.tried_windows release];
667 [(WineWindow*)event->window release];
674 /***********************************************************************
675 * macdrv_create_query
677 macdrv_query* macdrv_create_query(void)
681 query = calloc(1, sizeof(*query));
686 /***********************************************************************
687 * macdrv_retain_query
689 macdrv_query* macdrv_retain_query(macdrv_query *query)
691 OSAtomicIncrement32Barrier(&query->refs);
695 /***********************************************************************
696 * macdrv_release_query
698 void macdrv_release_query(macdrv_query *query)
700 if (OSAtomicDecrement32Barrier(&query->refs) <= 0)
704 case QUERY_DRAG_OPERATION:
705 if (query->drag_operation.pasteboard)
706 CFRelease(query->drag_operation.pasteboard);
708 case QUERY_DRAG_DROP:
709 if (query->drag_drop.pasteboard)
710 CFRelease(query->drag_drop.pasteboard);
712 case QUERY_PASTEBOARD_DATA:
713 if (query->pasteboard_data.type)
714 CFRelease(query->pasteboard_data.type);
717 [(WineWindow*)query->window release];
722 /***********************************************************************
723 * macdrv_set_query_done
725 void macdrv_set_query_done(macdrv_query *query)
727 macdrv_retain_query(query);
733 macdrv_release_query(query);
735 event = [NSEvent otherEventWithType:NSApplicationDefined
738 timestamp:[[NSProcessInfo processInfo] systemUptime]
741 subtype:WineApplicationEventWakeQuery
744 [NSApp postEvent:event atStart:TRUE];
751 /***********************************************************************
752 * macdrv_register_hot_key
754 int macdrv_register_hot_key(macdrv_event_queue q, unsigned int vkey, unsigned int mod_flags,
755 unsigned int keycode, unsigned int modifiers)
757 WineEventQueue* queue = (WineEventQueue*)q;
761 ret = [queue registerHotKey:keycode modifiers:modifiers vkey:vkey modFlags:mod_flags];
768 /***********************************************************************
769 * macdrv_unregister_hot_key
771 void macdrv_unregister_hot_key(macdrv_event_queue q, unsigned int vkey, unsigned int mod_flags)
773 WineEventQueue* queue = (WineEventQueue*)q;
776 [queue unregisterHotKey:vkey modFlags:mod_flags];