Hopefully implemented a proper workaround for new Mojave permissions required for the Media Key event hook. We shouldn't crash any more.

CQTexperiment
Christopher Snowhill 2018-06-27 20:50:26 -07:00
parent a7eee36cde
commit 6e6648624d
3 changed files with 29 additions and 10 deletions

View File

@ -32,8 +32,18 @@
context:nil];
keyTap = [[SPMediaKeyTap alloc] initWithDelegate:self];
if([SPMediaKeyTap usesGlobalMediaKeyTap])
[keyTap startWatchingMediaKeys];
if([SPMediaKeyTap usesGlobalMediaKeyTap]) {
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"Retry"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:@"Enable Media Key access?"];
[alert setInformativeText:@"Media Key support requires the \"Accessibility\" permission."];
[alert setAlertStyle:NSInformationalAlertStyle];
while (![keyTap startWatchingMediaKeys]) {
if ([alert runModal] == NSAlertFirstButtonReturn) continue;
else break;
}
}
else
ALog(@"Media key monitoring disabled");
}

View File

@ -23,7 +23,7 @@
-(id)initWithDelegate:(id)delegate;
+(BOOL)usesGlobalMediaKeyTap;
-(void)startWatchingMediaKeys;
-(BOOL)startWatchingMediaKeys;
-(void)stopWatchingMediaKeys;
-(void)handleAndReleaseMediaKeyEvent:(NSEvent *)event;

View File

@ -60,7 +60,7 @@ static CGEventRef tapEventCallback(CGEventTapProxy proxy, CGEventType type, CGEv
_app_switching_ref = NULL;
}
-(void)startWatchingMediaKeys;{
-(BOOL)startWatchingMediaKeys;{
// Prevent having multiple mediaKeys threads
[self stopWatchingMediaKeys];
@ -74,13 +74,22 @@ static CGEventRef tapEventCallback(CGEventTapProxy proxy, CGEventType type, CGEv
CGEventMaskBit(NX_SYSDEFINED),
tapEventCallback,
(__bridge void*)self);
assert(_eventPort != NULL);
if (_eventPort != NULL){
_eventPortSource = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, _eventPort, 0);
if (_eventPortSource != NULL){
_eventPortSource = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, _eventPort, 0);
assert(_eventPortSource != NULL);
// Let's do this in a separate thread so that a slow app doesn't lag the event tap
[NSThread detachNewThreadSelector:@selector(eventTapThread) toTarget:self withObject:nil];
// Let's do this in a separate thread so that a slow app doesn't lag the event tap
[NSThread detachNewThreadSelector:@selector(eventTapThread) toTarget:self withObject:nil];
return YES;
}
}
[self setShouldInterceptMediaKeyEvents:NO];
return NO;
}
}
-(void)stopWatchingMediaKeys;