Browse Source

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

master
Christopher Snowhill 2 years ago
parent
commit
37a8e4ea3a
3 changed files with 30 additions and 11 deletions
  1. +12
    -2
      Application/MediaKeysApplication.m
  2. +1
    -1
      ThirdParty/SPMediaKeyTap/SPMediaKeyTap.h
  3. +17
    -8
      ThirdParty/SPMediaKeyTap/SPMediaKeyTap.m

+ 12
- 2
Application/MediaKeysApplication.m 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");
}


+ 1
- 1
ThirdParty/SPMediaKeyTap/SPMediaKeyTap.h View File

@@ -23,7 +23,7 @@
-(id)initWithDelegate:(id)delegate;

+(BOOL)usesGlobalMediaKeyTap;
-(void)startWatchingMediaKeys;
-(BOOL)startWatchingMediaKeys;
-(void)stopWatchingMediaKeys;
-(void)handleAndReleaseMediaKeyEvent:(NSEvent *)event;



+ 17
- 8
ThirdParty/SPMediaKeyTap/SPMediaKeyTap.m 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);

_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];
if (_eventPort != NULL){
_eventPortSource = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, _eventPort, 0);
if (_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];
return YES;
}
}
[self setShouldInterceptMediaKeyEvents:NO];
return NO;
}
}
-(void)stopWatchingMediaKeys;


Loading…
Cancel
Save