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 @implementation NSEvent (WineExtensions)
45 static BOOL wine_commandKeyDown(NSUInteger flags)
47 return ((flags & (NSEventModifierFlagShift |
48 NSEventModifierFlagControl |
49 NSEventModifierFlagOption |
50 NSEventModifierFlagCommand)) == NSEventModifierFlagCommand);
53 + (BOOL) wine_commandKeyDown
55 return wine_commandKeyDown([self modifierFlags]);
58 - (BOOL) wine_commandKeyDown
60 return wine_commandKeyDown([self modifierFlags]);
66 @interface MacDrvEvent : NSObject
72 - (id) initWithEvent:(macdrv_event*)event;
76 @implementation MacDrvEvent
78 - (id) initWithEvent:(macdrv_event*)inEvent
83 event = macdrv_retain_event(inEvent);
90 if (event) macdrv_release_event(event);
97 @implementation WineEventQueue
101 [self doesNotRecognizeSelector:_cmd];
106 - (id) initWithEventHandler:(macdrv_event_handler)handler
108 NSParameterAssert(handler != nil);
116 fds[0] = fds[1] = kq = -1;
118 event_handler = handler;
119 events = [[NSMutableArray alloc] init];
120 eventsLock = [[NSLock alloc] init];
122 if (!events || !eventsLock)
129 fcntl(fds[0], F_SETFD, 1) == -1 ||
130 fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
131 fcntl(fds[1], F_SETFD, 1) == -1 ||
132 fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1)
145 EV_SET(&kev, fds[0], EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
148 rc = kevent(kq, &kev, 1, NULL, 0, NULL);
149 } while (rc == -1 && errno == EINTR);
161 NSNumber* hotKeyMacID;
163 for (hotKeyMacID in hotKeysByMacID)
165 NSDictionary* hotKeyDict = [hotKeysByMacID objectForKey:hotKeyMacID];
166 EventHotKeyRef hotKeyRef = [[hotKeyDict objectForKey:WineHotKeyCarbonRefKey] pointerValue];
167 UnregisterEventHotKey(hotKeyRef);
169 [hotKeysByMacID release];
170 [hotKeysByWinID release];
172 [eventsLock release];
174 if (kq != -1) close(kq);
175 if (fds[0] != -1) close(fds[0]);
176 if (fds[1] != -1) close(fds[1]);
181 - (void) signalEventAvailable
188 rc = write(fds[1], &junk, 1);
189 } while (rc < 0 && errno == EINTR);
191 if (rc < 0 && errno != EAGAIN)
192 ERR(@"%@: got error writing to event queue signaling pipe: %s\n", self, strerror(errno));
195 - (void) postEventObject:(MacDrvEvent*)event
198 MacDrvEvent* lastEvent;
202 indexes = [events indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
203 return ((MacDrvEvent*)obj)->event->deliver <= 0;
205 [events removeObjectsAtIndexes:indexes];
207 if ((event->event->type == MOUSE_MOVED ||
208 event->event->type == MOUSE_MOVED_ABSOLUTE) &&
209 event->event->deliver == INT_MAX &&
210 (lastEvent = [events lastObject]) &&
211 (lastEvent->event->type == MOUSE_MOVED ||
212 lastEvent->event->type == MOUSE_MOVED_ABSOLUTE) &&
213 lastEvent->event->deliver == INT_MAX &&
214 lastEvent->event->window == event->event->window &&
215 lastEvent->event->mouse_moved.drag == event->event->mouse_moved.drag)
217 if (event->event->type == MOUSE_MOVED)
219 lastEvent->event->mouse_moved.x += event->event->mouse_moved.x;
220 lastEvent->event->mouse_moved.y += event->event->mouse_moved.y;
224 lastEvent->event->type = MOUSE_MOVED_ABSOLUTE;
225 lastEvent->event->mouse_moved.x = event->event->mouse_moved.x;
226 lastEvent->event->mouse_moved.y = event->event->mouse_moved.y;
229 lastEvent->event->mouse_moved.time_ms = event->event->mouse_moved.time_ms;
232 [events addObject:event];
236 [self signalEventAvailable];
239 - (void) postEvent:(macdrv_event*)inEvent
241 MacDrvEvent* event = [[MacDrvEvent alloc] initWithEvent:inEvent];
242 [self postEventObject:event];
246 - (MacDrvEvent*) getEventMatchingMask:(macdrv_event_mask)mask
251 MacDrvEvent* ret = nil;
253 /* Clear the pipe which signals there are pending events. */
256 rc = read(fds[0], buf, sizeof(buf));
257 } while (rc > 0 || (rc < 0 && errno == EINTR));
258 if (rc == 0 || (rc < 0 && errno != EAGAIN))
261 ERR(@"%@: event queue signaling pipe unexpectedly closed\n", self);
263 ERR(@"%@: got error reading from event queue signaling pipe: %s\n", self, strerror(errno));
270 while (index < [events count])
272 MacDrvEvent* event = [events objectAtIndex:index];
273 if (event_mask_for_type(event->event->type) & mask)
275 [[event retain] autorelease];
276 [events removeObjectAtIndex:index];
278 if (event->event->deliver == INT_MAX ||
279 OSAtomicDecrement32Barrier(&event->event->deliver) >= 0)
293 - (void) discardEventsPassingTest:(BOOL (^)(macdrv_event* event))block
295 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
300 indexes = [events indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
301 MacDrvEvent* event = obj;
302 return block(event->event);
305 [events removeObjectsAtIndexes:indexes];
312 - (void) discardEventsMatchingMask:(macdrv_event_mask)mask forWindow:(NSWindow*)window
314 [self discardEventsPassingTest:^BOOL (macdrv_event* event){
315 return ((event_mask_for_type(event->type) & mask) &&
316 (!window || event->window == (macdrv_window)window));
320 - (BOOL) query:(macdrv_query*)query timeout:(NSTimeInterval)timeout flags:(NSUInteger)flags
324 NSDate* timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
327 type = (flags & WineQueryNoPreemptWait) ? QUERY_EVENT_NO_PREEMPT_WAIT : QUERY_EVENT;
328 event = macdrv_create_event(type, (WineWindow*)query->window);
329 event->query_event.query = macdrv_retain_query(query);
332 [self postEvent:event];
333 macdrv_release_event(event);
334 timedout = ![[WineApplicationController sharedController] waitUntilQueryDone:&query->done
336 processEvents:(flags & WineQueryProcessEvents) != 0];
337 return !timedout && query->status;
340 - (BOOL) query:(macdrv_query*)query timeout:(NSTimeInterval)timeout
342 return [self query:query timeout:timeout flags:0];
345 - (void) resetMouseEventPositions:(CGPoint)pos
349 pos = cgpoint_win_from_mac(pos);
353 for (event in events)
355 if (event->event->type == MOUSE_BUTTON)
357 event->event->mouse_button.x = pos.x;
358 event->event->mouse_button.y = pos.y;
360 else if (event->event->type == MOUSE_SCROLL)
362 event->event->mouse_scroll.x = pos.x;
363 event->event->mouse_scroll.y = pos.y;
370 - (BOOL) postHotKeyEvent:(UInt32)hotKeyNumber time:(double)time
372 NSDictionary* hotKeyDict = [hotKeysByMacID objectForKey:[NSNumber numberWithUnsignedInt:hotKeyNumber]];
377 event = macdrv_create_event(HOTKEY_PRESS, nil);
378 event->hotkey_press.vkey = [[hotKeyDict objectForKey:WineHotKeyVkeyKey] unsignedIntValue];
379 event->hotkey_press.mod_flags = [[hotKeyDict objectForKey:WineHotKeyModFlagsKey] unsignedIntValue];
380 event->hotkey_press.keycode = [[hotKeyDict objectForKey:WineHotKeyKeyCodeKey] unsignedIntValue];
381 event->hotkey_press.time_ms = [[WineApplicationController sharedController] ticksForEventTime:time];
383 [self postEvent:event];
385 macdrv_release_event(event);
388 return hotKeyDict != nil;
391 static OSStatus HotKeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
393 WineEventQueue* self = userData;
395 EventHotKeyID hotKeyID;
397 status = GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL,
398 sizeof(hotKeyID), NULL, &hotKeyID);
401 if (hotKeyID.signature != WineHotKeySignature ||
402 ![self postHotKeyEvent:hotKeyID.id time:GetEventTime(theEvent)])
403 status = eventNotHandledErr;
409 - (void) unregisterHotKey:(unsigned int)vkey modFlags:(unsigned int)modFlags
411 NSNumber* vkeyNumber = [NSNumber numberWithUnsignedInt:vkey];
412 NSNumber* modFlagsNumber = [NSNumber numberWithUnsignedInt:modFlags];
413 NSArray* winIDPair = [NSArray arrayWithObjects:vkeyNumber, modFlagsNumber, nil];
414 NSDictionary* hotKeyDict = [hotKeysByWinID objectForKey:winIDPair];
417 EventHotKeyRef hotKeyRef = [[hotKeyDict objectForKey:WineHotKeyCarbonRefKey] pointerValue];
418 NSNumber* macID = [hotKeyDict objectForKey:WineHotKeyMacIDKey];
420 UnregisterEventHotKey(hotKeyRef);
421 [hotKeysByMacID removeObjectForKey:macID];
422 [hotKeysByWinID removeObjectForKey:winIDPair];
426 - (int) registerHotKey:(UInt32)keyCode modifiers:(UInt32)modifiers vkey:(unsigned int)vkey modFlags:(unsigned int)modFlags
428 static EventHandlerRef handler;
429 static UInt32 hotKeyNumber;
431 NSNumber* vkeyNumber;
432 NSNumber* modFlagsNumber;
434 EventHotKeyID hotKeyID;
435 EventHotKeyRef hotKeyRef;
436 NSNumber* macIDNumber;
437 NSDictionary* hotKeyDict;
441 EventTypeSpec eventType = { kEventClassKeyboard, kEventHotKeyPressed };
442 status = InstallApplicationEventHandler(HotKeyHandler, 1, &eventType, self, &handler);
445 ERR(@"InstallApplicationEventHandler() failed: %d\n", status);
447 return MACDRV_HOTKEY_FAILURE;
451 if (!hotKeysByMacID && !(hotKeysByMacID = [[NSMutableDictionary alloc] init]))
452 return MACDRV_HOTKEY_FAILURE;
453 if (!hotKeysByWinID && !(hotKeysByWinID = [[NSMutableDictionary alloc] init]))
454 return MACDRV_HOTKEY_FAILURE;
456 vkeyNumber = [NSNumber numberWithUnsignedInt:vkey];
457 modFlagsNumber = [NSNumber numberWithUnsignedInt:modFlags];
458 winIDPair = [NSArray arrayWithObjects:vkeyNumber, modFlagsNumber, nil];
459 if ([hotKeysByWinID objectForKey:winIDPair])
460 return MACDRV_HOTKEY_ALREADY_REGISTERED;
462 hotKeyID.signature = WineHotKeySignature;
463 hotKeyID.id = hotKeyNumber++;
465 status = RegisterEventHotKey(keyCode, modifiers, hotKeyID, GetApplicationEventTarget(),
466 kEventHotKeyExclusive, &hotKeyRef);
467 if (status == eventHotKeyExistsErr)
468 return MACDRV_HOTKEY_ALREADY_REGISTERED;
471 ERR(@"RegisterEventHotKey() failed: %d\n", status);
472 return MACDRV_HOTKEY_FAILURE;
475 macIDNumber = [NSNumber numberWithUnsignedInt:hotKeyID.id];
476 hotKeyDict = [NSDictionary dictionaryWithObjectsAndKeys:
477 macIDNumber, WineHotKeyMacIDKey,
478 vkeyNumber, WineHotKeyVkeyKey,
479 modFlagsNumber, WineHotKeyModFlagsKey,
480 [NSNumber numberWithUnsignedInt:keyCode], WineHotKeyKeyCodeKey,
481 [NSValue valueWithPointer:hotKeyRef], WineHotKeyCarbonRefKey,
483 [hotKeysByMacID setObject:hotKeyDict forKey:macIDNumber];
484 [hotKeysByWinID setObject:hotKeyDict forKey:winIDPair];
486 return MACDRV_HOTKEY_SUCCESS;
490 /***********************************************************************
493 * Run a block on the main thread synchronously.
495 void OnMainThread(dispatch_block_t block)
497 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
498 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
499 WineEventQueue* queue = [threadDict objectForKey:WineEventQueueThreadDictionaryKey];
500 dispatch_semaphore_t semaphore = NULL;
501 __block BOOL finished;
505 semaphore = dispatch_semaphore_create(0);
506 dispatch_retain(semaphore);
514 [queue signalEventAvailable];
517 dispatch_semaphore_signal(semaphore);
518 dispatch_release(semaphore);
526 MacDrvEvent* macDrvEvent;
530 (macDrvEvent = [queue getEventMatchingMask:event_mask_for_type(QUERY_EVENT)]))
532 queue->event_handler(macDrvEvent->event);
538 pool = [[NSAutoreleasePool alloc] init];
540 kevent(queue->kq, NULL, 0, &kev, 1, NULL);
546 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
547 dispatch_release(semaphore);
554 /***********************************************************************
555 * macdrv_create_event_queue
557 * Register this thread with the application on the main thread, and set
558 * up an event queue on which it can deliver events to this thread.
560 macdrv_event_queue macdrv_create_event_queue(macdrv_event_handler handler)
562 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
563 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
565 WineEventQueue* queue = [threadDict objectForKey:WineEventQueueThreadDictionaryKey];
568 queue = [[[WineEventQueue alloc] initWithEventHandler:handler] autorelease];
571 if ([[WineApplicationController sharedController] registerEventQueue:queue])
572 [threadDict setObject:queue forKey:WineEventQueueThreadDictionaryKey];
579 return (macdrv_event_queue)queue;
582 /***********************************************************************
583 * macdrv_destroy_event_queue
585 * Tell the application that this thread is exiting and destroy the
586 * associated event queue.
588 void macdrv_destroy_event_queue(macdrv_event_queue queue)
590 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
591 WineEventQueue* q = (WineEventQueue*)queue;
592 NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
594 [[WineApplicationController sharedController] unregisterEventQueue:q];
595 [threadDict removeObjectForKey:WineEventQueueThreadDictionaryKey];
600 /***********************************************************************
601 * macdrv_get_event_queue_fd
603 * Get the file descriptor whose readability signals that there are
604 * events on the event queue.
606 int macdrv_get_event_queue_fd(macdrv_event_queue queue)
608 WineEventQueue* q = (WineEventQueue*)queue;
612 /***********************************************************************
613 * macdrv_copy_event_from_queue
615 * Pull an event matching the event mask from the event queue and store
616 * it in the event record pointed to by the event parameter. If a
617 * matching event was found, return non-zero; otherwise, return 0.
619 * The caller is responsible for calling macdrv_release_event on any
620 * event returned by this function.
622 int macdrv_copy_event_from_queue(macdrv_event_queue queue,
623 macdrv_event_mask mask, macdrv_event **event)
625 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
626 WineEventQueue* q = (WineEventQueue*)queue;
628 MacDrvEvent* macDrvEvent = [q getEventMatchingMask:mask];
630 *event = macdrv_retain_event(macDrvEvent->event);
633 return (macDrvEvent != nil);
636 /***********************************************************************
637 * macdrv_create_event
639 macdrv_event* macdrv_create_event(int type, WineWindow* window)
643 event = calloc(1, sizeof(*event));
645 event->deliver = INT_MAX;
647 event->window = (macdrv_window)[window retain];
651 /***********************************************************************
652 * macdrv_retain_event
654 macdrv_event* macdrv_retain_event(macdrv_event *event)
656 OSAtomicIncrement32Barrier(&event->refs);
660 /***********************************************************************
661 * macdrv_release_event
663 * Decrements the reference count of an event. If the count falls to
664 * zero, cleans up any resources, such as allocated memory or retained
665 * objects, held by the event and deallocates it
667 void macdrv_release_event(macdrv_event *event)
669 if (OSAtomicDecrement32Barrier(&event->refs) <= 0)
671 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
676 if (event->im_set_text.text)
677 CFRelease(event->im_set_text.text);
679 case KEYBOARD_CHANGED:
680 CFRelease(event->keyboard_changed.uchr);
681 CFRelease(event->keyboard_changed.input_source);
684 case QUERY_EVENT_NO_PREEMPT_WAIT:
685 macdrv_release_query(event->query_event.query);
687 case WINDOW_GOT_FOCUS:
688 [(NSMutableSet*)event->window_got_focus.tried_windows release];
692 [(WineWindow*)event->window release];
699 /***********************************************************************
700 * macdrv_create_query
702 macdrv_query* macdrv_create_query(void)
706 query = calloc(1, sizeof(*query));
711 /***********************************************************************
712 * macdrv_retain_query
714 macdrv_query* macdrv_retain_query(macdrv_query *query)
716 OSAtomicIncrement32Barrier(&query->refs);
720 /***********************************************************************
721 * macdrv_release_query
723 void macdrv_release_query(macdrv_query *query)
725 if (OSAtomicDecrement32Barrier(&query->refs) <= 0)
729 case QUERY_DRAG_OPERATION:
730 if (query->drag_operation.pasteboard)
731 CFRelease(query->drag_operation.pasteboard);
733 case QUERY_DRAG_DROP:
734 if (query->drag_drop.pasteboard)
735 CFRelease(query->drag_drop.pasteboard);
737 case QUERY_PASTEBOARD_DATA:
738 if (query->pasteboard_data.type)
739 CFRelease(query->pasteboard_data.type);
742 [(WineWindow*)query->window release];
747 /***********************************************************************
748 * macdrv_set_query_done
750 void macdrv_set_query_done(macdrv_query *query)
752 macdrv_retain_query(query);
758 macdrv_release_query(query);
760 event = [NSEvent otherEventWithType:NSEventTypeApplicationDefined
763 timestamp:[[NSProcessInfo processInfo] systemUptime]
766 subtype:WineApplicationEventWakeQuery
769 [NSApp postEvent:event atStart:TRUE];
776 /***********************************************************************
777 * macdrv_register_hot_key
779 int macdrv_register_hot_key(macdrv_event_queue q, unsigned int vkey, unsigned int mod_flags,
780 unsigned int keycode, unsigned int modifiers)
782 WineEventQueue* queue = (WineEventQueue*)q;
786 ret = [queue registerHotKey:keycode modifiers:modifiers vkey:vkey modFlags:mod_flags];
793 /***********************************************************************
794 * macdrv_unregister_hot_key
796 void macdrv_unregister_hot_key(macdrv_event_queue q, unsigned int vkey, unsigned int mod_flags)
798 WineEventQueue* queue = (WineEventQueue*)q;
801 [queue unregisterHotKey:vkey modFlags:mod_flags];