2013-01-21 07:07:51 +01:00
|
|
|
/*
|
|
|
|
* MACDRV Cocoa event queue code
|
|
|
|
*
|
|
|
|
* Copyright 2011, 2012, 2013 Ken Thomases for CodeWeavers Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/event.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <libkern/OSAtomic.h>
|
2013-09-26 00:10:37 +02:00
|
|
|
#import <Carbon/Carbon.h>
|
2013-01-21 07:07:51 +01:00
|
|
|
|
|
|
|
#include "macdrv_cocoa.h"
|
|
|
|
#import "cocoa_event.h"
|
|
|
|
#import "cocoa_app.h"
|
|
|
|
#import "cocoa_window.h"
|
|
|
|
|
|
|
|
|
2013-03-11 04:58:26 +01:00
|
|
|
static NSString* const WineEventQueueThreadDictionaryKey = @"WineEventQueueThreadDictionaryKey";
|
|
|
|
|
2013-09-26 00:10:37 +02:00
|
|
|
static NSString* const WineHotKeyMacIDKey = @"macID";
|
|
|
|
static NSString* const WineHotKeyVkeyKey = @"vkey";
|
|
|
|
static NSString* const WineHotKeyModFlagsKey = @"modFlags";
|
|
|
|
static NSString* const WineHotKeyKeyCodeKey = @"keyCode";
|
|
|
|
static NSString* const WineHotKeyCarbonRefKey = @"hotKeyRef";
|
|
|
|
static const OSType WineHotKeySignature = 'Wine';
|
|
|
|
|
2013-03-11 04:58:26 +01:00
|
|
|
|
2013-01-21 07:07:51 +01:00
|
|
|
@interface MacDrvEvent : NSObject
|
|
|
|
{
|
|
|
|
@public
|
2013-04-04 01:56:35 +02:00
|
|
|
macdrv_event* event;
|
2013-01-21 07:07:51 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
- (id) initWithEvent:(macdrv_event*)event;
|
2013-01-21 07:07:51 +01:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MacDrvEvent
|
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
- (id) initWithEvent:(macdrv_event*)inEvent
|
2013-01-21 07:07:51 +01:00
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self)
|
|
|
|
{
|
2013-04-04 01:56:35 +02:00
|
|
|
event = macdrv_retain_event(inEvent);
|
2013-01-21 07:07:51 +01:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
if (event) macdrv_release_event(event);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2013-01-21 07:07:51 +01:00
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
@implementation WineEventQueue
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
2013-03-11 04:58:26 +01:00
|
|
|
[self doesNotRecognizeSelector:_cmd];
|
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initWithEventHandler:(macdrv_event_handler)handler
|
|
|
|
{
|
|
|
|
NSParameterAssert(handler != nil);
|
|
|
|
|
2013-01-21 07:07:51 +01:00
|
|
|
self = [super init];
|
|
|
|
if (self != nil)
|
|
|
|
{
|
2013-03-11 04:58:26 +01:00
|
|
|
struct kevent kev;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
fds[0] = fds[1] = kq = -1;
|
2013-01-21 07:07:51 +01:00
|
|
|
|
2013-03-11 04:58:26 +01:00
|
|
|
event_handler = handler;
|
2013-01-21 07:07:51 +01:00
|
|
|
events = [[NSMutableArray alloc] init];
|
|
|
|
eventsLock = [[NSLock alloc] init];
|
|
|
|
|
|
|
|
if (!events || !eventsLock)
|
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pipe(fds) ||
|
|
|
|
fcntl(fds[0], F_SETFD, 1) == -1 ||
|
|
|
|
fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
|
|
|
|
fcntl(fds[1], F_SETFD, 1) == -1 ||
|
|
|
|
fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1)
|
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
2013-03-11 04:58:26 +01:00
|
|
|
|
|
|
|
kq = kqueue();
|
|
|
|
if (kq < 0)
|
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
EV_SET(&kev, fds[0], EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
rc = kevent(kq, &kev, 1, NULL, 0, NULL);
|
|
|
|
} while (rc == -1 && errno == EINTR);
|
|
|
|
if (rc == -1)
|
|
|
|
{
|
|
|
|
[self release];
|
|
|
|
return nil;
|
|
|
|
}
|
2013-01-21 07:07:51 +01:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2013-09-26 00:10:37 +02:00
|
|
|
NSNumber* hotKeyMacID;
|
|
|
|
|
|
|
|
for (hotKeyMacID in hotKeysByMacID)
|
|
|
|
{
|
|
|
|
NSDictionary* hotKeyDict = [hotKeysByMacID objectForKey:hotKeyMacID];
|
|
|
|
EventHotKeyRef hotKeyRef = [[hotKeyDict objectForKey:WineHotKeyCarbonRefKey] pointerValue];
|
|
|
|
UnregisterEventHotKey(hotKeyRef);
|
|
|
|
}
|
|
|
|
[hotKeysByMacID release];
|
|
|
|
[hotKeysByWinID release];
|
2013-01-21 07:07:51 +01:00
|
|
|
[events release];
|
|
|
|
[eventsLock release];
|
|
|
|
|
2013-03-11 04:58:26 +01:00
|
|
|
if (kq != -1) close(kq);
|
2013-01-21 07:07:51 +01:00
|
|
|
if (fds[0] != -1) close(fds[0]);
|
|
|
|
if (fds[1] != -1) close(fds[1]);
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) signalEventAvailable
|
|
|
|
{
|
|
|
|
char junk = 1;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
rc = write(fds[1], &junk, 1);
|
|
|
|
} while (rc < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
if (rc < 0 && errno != EAGAIN)
|
|
|
|
ERR(@"%@: got error writing to event queue signaling pipe: %s\n", self, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) postEventObject:(MacDrvEvent*)event
|
|
|
|
{
|
2013-10-18 06:43:49 +02:00
|
|
|
NSIndexSet* indexes;
|
2013-02-07 02:32:26 +01:00
|
|
|
MacDrvEvent* lastEvent;
|
|
|
|
|
2013-01-21 07:07:51 +01:00
|
|
|
[eventsLock lock];
|
2013-02-07 02:32:26 +01:00
|
|
|
|
2013-10-18 06:43:49 +02:00
|
|
|
indexes = [events indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
|
|
|
|
return ((MacDrvEvent*)obj)->event->deliver <= 0;
|
|
|
|
}];
|
|
|
|
[events removeObjectsAtIndexes:indexes];
|
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
if ((event->event->type == MOUSE_MOVED ||
|
|
|
|
event->event->type == MOUSE_MOVED_ABSOLUTE) &&
|
2013-10-18 06:43:31 +02:00
|
|
|
event->event->deliver == INT_MAX &&
|
2013-02-07 02:32:26 +01:00
|
|
|
(lastEvent = [events lastObject]) &&
|
2013-04-04 01:56:35 +02:00
|
|
|
(lastEvent->event->type == MOUSE_MOVED ||
|
|
|
|
lastEvent->event->type == MOUSE_MOVED_ABSOLUTE) &&
|
2013-10-18 06:43:31 +02:00
|
|
|
lastEvent->event->deliver == INT_MAX &&
|
2013-05-07 10:00:52 +02:00
|
|
|
lastEvent->event->window == event->event->window &&
|
|
|
|
lastEvent->event->mouse_moved.drag == event->event->mouse_moved.drag)
|
2013-02-07 02:32:26 +01:00
|
|
|
{
|
2013-04-04 01:56:35 +02:00
|
|
|
if (event->event->type == MOUSE_MOVED)
|
2013-02-07 02:32:26 +01:00
|
|
|
{
|
2013-04-04 01:56:35 +02:00
|
|
|
lastEvent->event->mouse_moved.x += event->event->mouse_moved.x;
|
|
|
|
lastEvent->event->mouse_moved.y += event->event->mouse_moved.y;
|
2013-02-07 02:32:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-04 01:56:35 +02:00
|
|
|
lastEvent->event->type = MOUSE_MOVED_ABSOLUTE;
|
|
|
|
lastEvent->event->mouse_moved.x = event->event->mouse_moved.x;
|
|
|
|
lastEvent->event->mouse_moved.y = event->event->mouse_moved.y;
|
2013-02-07 02:32:26 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
lastEvent->event->mouse_moved.time_ms = event->event->mouse_moved.time_ms;
|
2013-02-07 02:32:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
[events addObject:event];
|
|
|
|
|
2013-01-21 07:07:51 +01:00
|
|
|
[eventsLock unlock];
|
|
|
|
|
|
|
|
[self signalEventAvailable];
|
|
|
|
}
|
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
- (void) postEvent:(macdrv_event*)inEvent
|
2013-01-21 07:07:51 +01:00
|
|
|
{
|
|
|
|
MacDrvEvent* event = [[MacDrvEvent alloc] initWithEvent:inEvent];
|
|
|
|
[self postEventObject:event];
|
|
|
|
[event release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (MacDrvEvent*) getEventMatchingMask:(macdrv_event_mask)mask
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
int rc;
|
|
|
|
NSUInteger index;
|
2013-04-04 01:56:41 +02:00
|
|
|
MacDrvEvent* ret = nil;
|
2013-01-21 07:07:51 +01:00
|
|
|
|
|
|
|
/* Clear the pipe which signals there are pending events. */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
rc = read(fds[0], buf, sizeof(buf));
|
|
|
|
} while (rc > 0 || (rc < 0 && errno == EINTR));
|
|
|
|
if (rc == 0 || (rc < 0 && errno != EAGAIN))
|
|
|
|
{
|
|
|
|
if (rc == 0)
|
|
|
|
ERR(@"%@: event queue signaling pipe unexpectedly closed\n", self);
|
|
|
|
else
|
|
|
|
ERR(@"%@: got error reading from event queue signaling pipe: %s\n", self, strerror(errno));
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
[eventsLock lock];
|
|
|
|
|
|
|
|
index = 0;
|
2013-04-04 01:56:41 +02:00
|
|
|
while (index < [events count])
|
2013-01-21 07:07:51 +01:00
|
|
|
{
|
2013-04-04 01:56:41 +02:00
|
|
|
MacDrvEvent* event = [events objectAtIndex:index];
|
2013-04-04 01:56:35 +02:00
|
|
|
if (event_mask_for_type(event->event->type) & mask)
|
2013-04-04 01:56:41 +02:00
|
|
|
{
|
|
|
|
[[event retain] autorelease];
|
|
|
|
[events removeObjectAtIndex:index];
|
|
|
|
|
|
|
|
if (event->event->deliver == INT_MAX ||
|
|
|
|
OSAtomicDecrement32Barrier(&event->event->deliver) >= 0)
|
|
|
|
{
|
|
|
|
ret = event;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
index++;
|
2013-01-21 07:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[eventsLock unlock];
|
2013-04-04 01:56:41 +02:00
|
|
|
return ret;
|
2013-01-21 07:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) discardEventsMatchingMask:(macdrv_event_mask)mask forWindow:(NSWindow*)window
|
|
|
|
{
|
2013-12-31 08:05:18 +01:00
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
2013-04-04 01:56:35 +02:00
|
|
|
NSIndexSet* indexes;
|
2013-01-21 07:07:51 +01:00
|
|
|
|
|
|
|
[eventsLock lock];
|
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
indexes = [events indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop){
|
2013-01-21 07:07:51 +01:00
|
|
|
MacDrvEvent* event = obj;
|
2013-04-04 01:56:35 +02:00
|
|
|
return ((event_mask_for_type(event->event->type) & mask) &&
|
|
|
|
(!window || event->event->window == (macdrv_window)window));
|
2013-01-21 07:07:51 +01:00
|
|
|
}];
|
|
|
|
|
|
|
|
[events removeObjectsAtIndexes:indexes];
|
|
|
|
|
|
|
|
[eventsLock unlock];
|
2013-12-31 08:05:18 +01:00
|
|
|
|
|
|
|
[pool release];
|
2013-01-21 07:07:51 +01:00
|
|
|
}
|
|
|
|
|
2013-03-13 22:52:57 +01:00
|
|
|
- (BOOL) query:(macdrv_query*)query timeout:(NSTimeInterval)timeout processEvents:(BOOL)processEvents
|
2013-03-11 04:58:26 +01:00
|
|
|
{
|
2013-04-04 01:56:35 +02:00
|
|
|
macdrv_event* event;
|
2013-03-11 04:58:26 +01:00
|
|
|
NSDate* timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
|
|
|
|
BOOL timedout;
|
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
event = macdrv_create_event(QUERY_EVENT, (WineWindow*)query->window);
|
|
|
|
event->query_event.query = macdrv_retain_query(query);
|
2013-03-11 04:58:26 +01:00
|
|
|
query->done = FALSE;
|
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
[self postEvent:event];
|
|
|
|
macdrv_release_event(event);
|
2013-04-16 07:38:01 +02:00
|
|
|
timedout = ![[WineApplicationController sharedController] waitUntilQueryDone:&query->done
|
|
|
|
timeout:timeoutDate
|
|
|
|
processEvents:processEvents];
|
2013-03-11 04:58:26 +01:00
|
|
|
return !timedout && query->status;
|
|
|
|
}
|
|
|
|
|
2013-03-13 22:52:57 +01:00
|
|
|
- (BOOL) query:(macdrv_query*)query timeout:(NSTimeInterval)timeout
|
|
|
|
{
|
|
|
|
return [self query:query timeout:timeout processEvents:FALSE];
|
|
|
|
}
|
|
|
|
|
2013-04-26 11:06:12 +02:00
|
|
|
- (void) resetMouseEventPositions:(CGPoint)pos
|
|
|
|
{
|
|
|
|
MacDrvEvent* event;
|
|
|
|
|
|
|
|
[eventsLock lock];
|
|
|
|
|
|
|
|
for (event in events)
|
|
|
|
{
|
|
|
|
if (event->event->type == MOUSE_BUTTON)
|
|
|
|
{
|
|
|
|
event->event->mouse_button.x = pos.x;
|
|
|
|
event->event->mouse_button.y = pos.y;
|
|
|
|
}
|
|
|
|
else if (event->event->type == MOUSE_SCROLL)
|
|
|
|
{
|
|
|
|
event->event->mouse_scroll.x = pos.x;
|
|
|
|
event->event->mouse_scroll.y = pos.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[eventsLock unlock];
|
|
|
|
}
|
|
|
|
|
2013-09-26 00:10:37 +02:00
|
|
|
- (BOOL) postHotKeyEvent:(UInt32)hotKeyNumber time:(double)time
|
|
|
|
{
|
|
|
|
NSDictionary* hotKeyDict = [hotKeysByMacID objectForKey:[NSNumber numberWithUnsignedInt:hotKeyNumber]];
|
|
|
|
if (hotKeyDict)
|
|
|
|
{
|
|
|
|
macdrv_event* event;
|
|
|
|
|
|
|
|
event = macdrv_create_event(HOTKEY_PRESS, nil);
|
|
|
|
event->hotkey_press.vkey = [[hotKeyDict objectForKey:WineHotKeyVkeyKey] unsignedIntValue];
|
|
|
|
event->hotkey_press.mod_flags = [[hotKeyDict objectForKey:WineHotKeyModFlagsKey] unsignedIntValue];
|
|
|
|
event->hotkey_press.keycode = [[hotKeyDict objectForKey:WineHotKeyKeyCodeKey] unsignedIntValue];
|
|
|
|
event->hotkey_press.time_ms = [[WineApplicationController sharedController] ticksForEventTime:time];
|
|
|
|
|
|
|
|
[self postEvent:event];
|
|
|
|
|
|
|
|
macdrv_release_event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hotKeyDict != nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus HotKeyHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData)
|
|
|
|
{
|
|
|
|
WineEventQueue* self = userData;
|
|
|
|
OSStatus status;
|
|
|
|
EventHotKeyID hotKeyID;
|
|
|
|
|
|
|
|
status = GetEventParameter(theEvent, kEventParamDirectObject, typeEventHotKeyID, NULL,
|
|
|
|
sizeof(hotKeyID), NULL, &hotKeyID);
|
|
|
|
if (status == noErr)
|
|
|
|
{
|
|
|
|
if (hotKeyID.signature != WineHotKeySignature ||
|
|
|
|
![self postHotKeyEvent:hotKeyID.id time:GetEventTime(theEvent)])
|
|
|
|
status = eventNotHandledErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) unregisterHotKey:(unsigned int)vkey modFlags:(unsigned int)modFlags
|
|
|
|
{
|
|
|
|
NSNumber* vkeyNumber = [NSNumber numberWithUnsignedInt:vkey];
|
|
|
|
NSNumber* modFlagsNumber = [NSNumber numberWithUnsignedInt:modFlags];
|
|
|
|
NSArray* winIDPair = [NSArray arrayWithObjects:vkeyNumber, modFlagsNumber, nil];
|
|
|
|
NSDictionary* hotKeyDict = [hotKeysByWinID objectForKey:winIDPair];
|
|
|
|
if (hotKeyDict)
|
|
|
|
{
|
|
|
|
EventHotKeyRef hotKeyRef = [[hotKeyDict objectForKey:WineHotKeyCarbonRefKey] pointerValue];
|
|
|
|
NSNumber* macID = [hotKeyDict objectForKey:WineHotKeyMacIDKey];
|
|
|
|
|
|
|
|
UnregisterEventHotKey(hotKeyRef);
|
|
|
|
[hotKeysByMacID removeObjectForKey:macID];
|
|
|
|
[hotKeysByWinID removeObjectForKey:winIDPair];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int) registerHotKey:(UInt32)keyCode modifiers:(UInt32)modifiers vkey:(unsigned int)vkey modFlags:(unsigned int)modFlags
|
|
|
|
{
|
|
|
|
static EventHandlerRef handler;
|
|
|
|
static UInt32 hotKeyNumber;
|
|
|
|
OSStatus status;
|
|
|
|
NSNumber* vkeyNumber;
|
|
|
|
NSNumber* modFlagsNumber;
|
|
|
|
NSArray* winIDPair;
|
|
|
|
EventHotKeyID hotKeyID;
|
|
|
|
EventHotKeyRef hotKeyRef;
|
|
|
|
NSNumber* macIDNumber;
|
|
|
|
NSDictionary* hotKeyDict;
|
|
|
|
|
|
|
|
if (!handler)
|
|
|
|
{
|
|
|
|
EventTypeSpec eventType = { kEventClassKeyboard, kEventHotKeyPressed };
|
|
|
|
status = InstallApplicationEventHandler(HotKeyHandler, 1, &eventType, self, &handler);
|
|
|
|
if (status != noErr)
|
|
|
|
{
|
|
|
|
ERR(@"InstallApplicationEventHandler() failed: %d\n", status);
|
|
|
|
handler = NULL;
|
|
|
|
return MACDRV_HOTKEY_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hotKeysByMacID && !(hotKeysByMacID = [[NSMutableDictionary alloc] init]))
|
|
|
|
return MACDRV_HOTKEY_FAILURE;
|
|
|
|
if (!hotKeysByWinID && !(hotKeysByWinID = [[NSMutableDictionary alloc] init]))
|
|
|
|
return MACDRV_HOTKEY_FAILURE;
|
|
|
|
|
|
|
|
vkeyNumber = [NSNumber numberWithUnsignedInt:vkey];
|
|
|
|
modFlagsNumber = [NSNumber numberWithUnsignedInt:modFlags];
|
|
|
|
winIDPair = [NSArray arrayWithObjects:vkeyNumber, modFlagsNumber, nil];
|
|
|
|
if ([hotKeysByWinID objectForKey:winIDPair])
|
|
|
|
return MACDRV_HOTKEY_ALREADY_REGISTERED;
|
|
|
|
|
|
|
|
hotKeyID.signature = WineHotKeySignature;
|
|
|
|
hotKeyID.id = hotKeyNumber++;
|
|
|
|
|
|
|
|
status = RegisterEventHotKey(keyCode, modifiers, hotKeyID, GetApplicationEventTarget(),
|
|
|
|
kEventHotKeyExclusive, &hotKeyRef);
|
|
|
|
if (status == eventHotKeyExistsErr)
|
|
|
|
return MACDRV_HOTKEY_ALREADY_REGISTERED;
|
|
|
|
if (status != noErr)
|
|
|
|
{
|
|
|
|
ERR(@"RegisterEventHotKey() failed: %d\n", status);
|
|
|
|
return MACDRV_HOTKEY_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
macIDNumber = [NSNumber numberWithUnsignedInt:hotKeyID.id];
|
|
|
|
hotKeyDict = [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
macIDNumber, WineHotKeyMacIDKey,
|
|
|
|
vkeyNumber, WineHotKeyVkeyKey,
|
|
|
|
modFlagsNumber, WineHotKeyModFlagsKey,
|
|
|
|
[NSNumber numberWithUnsignedInt:keyCode], WineHotKeyKeyCodeKey,
|
|
|
|
[NSValue valueWithPointer:hotKeyRef], WineHotKeyCarbonRefKey,
|
|
|
|
nil];
|
|
|
|
[hotKeysByMacID setObject:hotKeyDict forKey:macIDNumber];
|
|
|
|
[hotKeysByWinID setObject:hotKeyDict forKey:winIDPair];
|
|
|
|
|
|
|
|
return MACDRV_HOTKEY_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-03-11 04:58:26 +01:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* OnMainThread
|
|
|
|
*
|
|
|
|
* Run a block on the main thread synchronously.
|
|
|
|
*/
|
|
|
|
void OnMainThread(dispatch_block_t block)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
|
|
|
|
WineEventQueue* queue = [threadDict objectForKey:WineEventQueueThreadDictionaryKey];
|
2013-10-30 11:52:58 +01:00
|
|
|
dispatch_semaphore_t semaphore;
|
2013-03-11 04:58:26 +01:00
|
|
|
__block BOOL finished;
|
|
|
|
|
|
|
|
if (!queue)
|
|
|
|
{
|
2013-10-30 11:52:58 +01:00
|
|
|
semaphore = dispatch_semaphore_create(0);
|
|
|
|
dispatch_retain(semaphore);
|
2013-03-11 04:58:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
finished = FALSE;
|
|
|
|
OnMainThreadAsync(^{
|
|
|
|
block();
|
|
|
|
finished = TRUE;
|
2013-10-30 11:52:58 +01:00
|
|
|
if (queue)
|
|
|
|
[queue signalEventAvailable];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dispatch_semaphore_signal(semaphore);
|
|
|
|
dispatch_release(semaphore);
|
|
|
|
}
|
2013-03-11 04:58:26 +01:00
|
|
|
});
|
|
|
|
|
2013-10-30 11:52:58 +01:00
|
|
|
if (queue)
|
2013-03-11 04:58:26 +01:00
|
|
|
{
|
2013-10-30 11:52:58 +01:00
|
|
|
while (!finished)
|
2013-03-11 04:58:26 +01:00
|
|
|
{
|
2013-10-30 11:52:58 +01:00
|
|
|
MacDrvEvent* macDrvEvent;
|
|
|
|
struct kevent kev;
|
2013-03-11 04:58:26 +01:00
|
|
|
|
2013-10-30 11:52:58 +01:00
|
|
|
while (!finished &&
|
|
|
|
(macDrvEvent = [queue getEventMatchingMask:event_mask_for_type(QUERY_EVENT)]))
|
|
|
|
{
|
|
|
|
queue->event_handler(macDrvEvent->event);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!finished)
|
|
|
|
{
|
|
|
|
[pool release];
|
|
|
|
pool = [[NSAutoreleasePool alloc] init];
|
2013-04-04 01:56:11 +02:00
|
|
|
|
2013-10-30 11:52:58 +01:00
|
|
|
kevent(queue->kq, NULL, 0, &kev, 1, NULL);
|
|
|
|
}
|
2013-04-04 01:56:11 +02:00
|
|
|
}
|
2013-03-11 04:58:26 +01:00
|
|
|
}
|
2013-10-30 11:52:58 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
|
|
|
dispatch_release(semaphore);
|
|
|
|
}
|
2013-03-11 04:58:26 +01:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
2013-01-21 07:07:51 +01:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_create_event_queue
|
|
|
|
*
|
|
|
|
* Register this thread with the application on the main thread, and set
|
|
|
|
* up an event queue on which it can deliver events to this thread.
|
|
|
|
*/
|
2013-03-11 04:58:26 +01:00
|
|
|
macdrv_event_queue macdrv_create_event_queue(macdrv_event_handler handler)
|
2013-01-21 07:07:51 +01:00
|
|
|
{
|
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
2013-03-11 04:58:26 +01:00
|
|
|
NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
|
2013-01-21 07:07:51 +01:00
|
|
|
|
2013-03-11 04:58:26 +01:00
|
|
|
WineEventQueue* queue = [threadDict objectForKey:WineEventQueueThreadDictionaryKey];
|
|
|
|
if (!queue)
|
2013-01-21 07:07:51 +01:00
|
|
|
{
|
2013-03-11 04:58:26 +01:00
|
|
|
queue = [[[WineEventQueue alloc] initWithEventHandler:handler] autorelease];
|
|
|
|
if (queue)
|
|
|
|
{
|
2013-04-16 07:38:01 +02:00
|
|
|
if ([[WineApplicationController sharedController] registerEventQueue:queue])
|
2013-03-11 04:58:26 +01:00
|
|
|
[threadDict setObject:queue forKey:WineEventQueueThreadDictionaryKey];
|
|
|
|
else
|
|
|
|
queue = nil;
|
|
|
|
}
|
2013-01-21 07:07:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
return (macdrv_event_queue)queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_destroy_event_queue
|
|
|
|
*
|
|
|
|
* Tell the application that this thread is exiting and destroy the
|
|
|
|
* associated event queue.
|
|
|
|
*/
|
|
|
|
void macdrv_destroy_event_queue(macdrv_event_queue queue)
|
|
|
|
{
|
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
WineEventQueue* q = (WineEventQueue*)queue;
|
2013-03-11 04:58:26 +01:00
|
|
|
NSMutableDictionary* threadDict = [[NSThread currentThread] threadDictionary];
|
2013-01-21 07:07:51 +01:00
|
|
|
|
2013-04-16 07:38:01 +02:00
|
|
|
[[WineApplicationController sharedController] unregisterEventQueue:q];
|
2013-03-11 04:58:26 +01:00
|
|
|
[threadDict removeObjectForKey:WineEventQueueThreadDictionaryKey];
|
2013-01-21 07:07:51 +01:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_get_event_queue_fd
|
|
|
|
*
|
|
|
|
* Get the file descriptor whose readability signals that there are
|
|
|
|
* events on the event queue.
|
|
|
|
*/
|
|
|
|
int macdrv_get_event_queue_fd(macdrv_event_queue queue)
|
|
|
|
{
|
|
|
|
WineEventQueue* q = (WineEventQueue*)queue;
|
|
|
|
return q->fds[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2013-04-04 01:56:35 +02:00
|
|
|
* macdrv_copy_event_from_queue
|
2013-01-21 07:07:51 +01:00
|
|
|
*
|
|
|
|
* Pull an event matching the event mask from the event queue and store
|
|
|
|
* it in the event record pointed to by the event parameter. If a
|
|
|
|
* matching event was found, return non-zero; otherwise, return 0.
|
|
|
|
*
|
2013-04-04 01:56:35 +02:00
|
|
|
* The caller is responsible for calling macdrv_release_event on any
|
2013-01-21 07:07:51 +01:00
|
|
|
* event returned by this function.
|
|
|
|
*/
|
2013-04-04 01:56:35 +02:00
|
|
|
int macdrv_copy_event_from_queue(macdrv_event_queue queue,
|
|
|
|
macdrv_event_mask mask, macdrv_event **event)
|
2013-01-21 07:07:51 +01:00
|
|
|
{
|
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
WineEventQueue* q = (WineEventQueue*)queue;
|
|
|
|
|
|
|
|
MacDrvEvent* macDrvEvent = [q getEventMatchingMask:mask];
|
|
|
|
if (macDrvEvent)
|
2013-04-04 01:56:35 +02:00
|
|
|
*event = macdrv_retain_event(macDrvEvent->event);
|
2013-01-21 07:07:51 +01:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
return (macDrvEvent != nil);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2013-04-04 01:56:35 +02:00
|
|
|
* macdrv_create_event
|
2013-01-21 07:07:51 +01:00
|
|
|
*/
|
2013-04-04 01:56:35 +02:00
|
|
|
macdrv_event* macdrv_create_event(int type, WineWindow* window)
|
2013-01-21 07:07:51 +01:00
|
|
|
{
|
2013-04-04 01:56:35 +02:00
|
|
|
macdrv_event *event;
|
|
|
|
|
|
|
|
event = calloc(1, sizeof(*event));
|
|
|
|
event->refs = 1;
|
2013-04-04 01:56:41 +02:00
|
|
|
event->deliver = INT_MAX;
|
2013-04-04 01:56:35 +02:00
|
|
|
event->type = type;
|
|
|
|
event->window = (macdrv_window)[window retain];
|
|
|
|
return event;
|
|
|
|
}
|
2013-01-21 07:07:51 +01:00
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_retain_event
|
|
|
|
*/
|
|
|
|
macdrv_event* macdrv_retain_event(macdrv_event *event)
|
|
|
|
{
|
|
|
|
OSAtomicIncrement32Barrier(&event->refs);
|
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_release_event
|
|
|
|
*
|
|
|
|
* Decrements the reference count of an event. If the count falls to
|
|
|
|
* zero, cleans up any resources, such as allocated memory or retained
|
|
|
|
* objects, held by the event and deallocates it
|
|
|
|
*/
|
|
|
|
void macdrv_release_event(macdrv_event *event)
|
|
|
|
{
|
|
|
|
if (OSAtomicDecrement32Barrier(&event->refs) <= 0)
|
2013-01-27 23:19:48 +01:00
|
|
|
{
|
2013-04-04 01:56:35 +02:00
|
|
|
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
|
2013-01-27 23:19:48 +01:00
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
switch (event->type)
|
|
|
|
{
|
2013-04-22 04:32:26 +02:00
|
|
|
case IM_SET_TEXT:
|
|
|
|
if (event->im_set_text.text)
|
|
|
|
CFRelease(event->im_set_text.text);
|
|
|
|
break;
|
2013-04-04 01:56:35 +02:00
|
|
|
case KEYBOARD_CHANGED:
|
|
|
|
CFRelease(event->keyboard_changed.uchr);
|
2013-11-20 16:30:36 +01:00
|
|
|
CFRelease(event->keyboard_changed.input_source);
|
2013-04-04 01:56:35 +02:00
|
|
|
break;
|
|
|
|
case QUERY_EVENT:
|
|
|
|
macdrv_release_query(event->query_event.query);
|
|
|
|
break;
|
|
|
|
case WINDOW_GOT_FOCUS:
|
|
|
|
[(NSMutableSet*)event->window_got_focus.tried_windows release];
|
|
|
|
break;
|
|
|
|
}
|
2013-01-21 07:07:51 +01:00
|
|
|
|
2013-04-04 01:56:35 +02:00
|
|
|
[(WineWindow*)event->window release];
|
|
|
|
free(event);
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
2013-01-21 07:07:51 +01:00
|
|
|
}
|
|
|
|
|
2013-03-11 04:58:26 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_create_query
|
|
|
|
*/
|
|
|
|
macdrv_query* macdrv_create_query(void)
|
|
|
|
{
|
|
|
|
macdrv_query *query;
|
|
|
|
|
|
|
|
query = calloc(1, sizeof(*query));
|
|
|
|
query->refs = 1;
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_retain_query
|
|
|
|
*/
|
|
|
|
macdrv_query* macdrv_retain_query(macdrv_query *query)
|
|
|
|
{
|
|
|
|
OSAtomicIncrement32Barrier(&query->refs);
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_release_query
|
|
|
|
*/
|
|
|
|
void macdrv_release_query(macdrv_query *query)
|
|
|
|
{
|
|
|
|
if (OSAtomicDecrement32Barrier(&query->refs) <= 0)
|
|
|
|
{
|
2013-03-13 22:53:32 +01:00
|
|
|
switch (query->type)
|
|
|
|
{
|
|
|
|
case QUERY_DRAG_OPERATION:
|
|
|
|
if (query->drag_operation.pasteboard)
|
|
|
|
CFRelease(query->drag_operation.pasteboard);
|
|
|
|
break;
|
|
|
|
case QUERY_DRAG_DROP:
|
|
|
|
if (query->drag_drop.pasteboard)
|
|
|
|
CFRelease(query->drag_drop.pasteboard);
|
|
|
|
break;
|
|
|
|
case QUERY_PASTEBOARD_DATA:
|
|
|
|
if (query->pasteboard_data.type)
|
|
|
|
CFRelease(query->pasteboard_data.type);
|
|
|
|
break;
|
|
|
|
}
|
2013-03-11 04:58:26 +01:00
|
|
|
[(WineWindow*)query->window release];
|
|
|
|
free(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_set_query_done
|
|
|
|
*/
|
|
|
|
void macdrv_set_query_done(macdrv_query *query)
|
|
|
|
{
|
|
|
|
macdrv_retain_query(query);
|
|
|
|
|
|
|
|
OnMainThreadAsync(^{
|
2013-03-13 22:52:57 +01:00
|
|
|
NSEvent* event;
|
|
|
|
|
2013-03-11 04:58:26 +01:00
|
|
|
query->done = TRUE;
|
|
|
|
macdrv_release_query(query);
|
2013-03-13 22:52:57 +01:00
|
|
|
|
|
|
|
event = [NSEvent otherEventWithType:NSApplicationDefined
|
|
|
|
location:NSZeroPoint
|
|
|
|
modifierFlags:0
|
|
|
|
timestamp:[[NSProcessInfo processInfo] systemUptime]
|
|
|
|
windowNumber:0
|
|
|
|
context:nil
|
|
|
|
subtype:WineApplicationEventWakeQuery
|
|
|
|
data1:0
|
|
|
|
data2:0];
|
|
|
|
[NSApp postEvent:event atStart:TRUE];
|
2013-03-11 04:58:26 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-21 07:07:51 +01:00
|
|
|
@end
|
2013-09-26 00:10:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_register_hot_key
|
|
|
|
*/
|
|
|
|
int macdrv_register_hot_key(macdrv_event_queue q, unsigned int vkey, unsigned int mod_flags,
|
|
|
|
unsigned int keycode, unsigned int modifiers)
|
|
|
|
{
|
|
|
|
WineEventQueue* queue = (WineEventQueue*)q;
|
|
|
|
__block int ret;
|
|
|
|
|
|
|
|
OnMainThread(^{
|
|
|
|
ret = [queue registerHotKey:keycode modifiers:modifiers vkey:vkey modFlags:mod_flags];
|
|
|
|
});
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* macdrv_unregister_hot_key
|
|
|
|
*/
|
|
|
|
void macdrv_unregister_hot_key(macdrv_event_queue q, unsigned int vkey, unsigned int mod_flags)
|
|
|
|
{
|
|
|
|
WineEventQueue* queue = (WineEventQueue*)q;
|
|
|
|
|
|
|
|
OnMainThreadAsync(^{
|
|
|
|
[queue unregisterHotKey:vkey modFlags:mod_flags];
|
|
|
|
});
|
|
|
|
}
|