From 9cb7f87b862883f5858315ff641d86703972e8ca Mon Sep 17 00:00:00 2001 From: Akemi Date: Tue, 5 Sep 2017 23:29:06 +0200 Subject: osx: fix media keys input when other Apps steal the priority other Apps do the same as mpv and tap into the global event chain. events that are handled are not being propagated down the event chain. that can lead to mpv not getting any media key events anymore when they are held back by other Apps. we will just move mpv back to the top of the event list when mpv is refocused and is not at the top of the list any more. Fixes #4834 --- osdep/macosx_application.m | 5 +++++ osdep/macosx_events.m | 20 ++++++++++++++++++++ osdep/macosx_events_objc.h | 2 ++ 3 files changed, 27 insertions(+) (limited to 'osdep') diff --git a/osdep/macosx_application.m b/osdep/macosx_application.m index 702292908a..0379491d79 100644 --- a/osdep/macosx_application.m +++ b/osdep/macosx_application.m @@ -139,6 +139,11 @@ static void terminate_cocoa_application(void) andEventID:kAEQuitApplication]; } +- (void)applicationWillBecomeActive:(NSNotification *)notification +{ + [_eventsResponder setHighestPriotityMediaKeysTap]; +} + - (void)handleQuitEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent { diff --git a/osdep/macosx_events.m b/osdep/macosx_events.m index 25c9948a98..e94718c3d1 100644 --- a/osdep/macosx_events.m +++ b/osdep/macosx_events.m @@ -372,6 +372,25 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx) CGEventTapEnable(self->_mk_tap_port, true); } +- (void)setHighestPriotityMediaKeysTap +{ + if (self->_mk_tap_port == nil) + return; + + CGEventTapInformation *taps = ta_alloc_size(nil, sizeof(CGEventTapInformation)); + uint32_t numTaps = 0; + CGError err = CGGetEventTapList(1, taps, &numTaps); + + if (err == kCGErrorSuccess && numTaps > 0) { + pid_t processID = [NSProcessInfo processInfo].processIdentifier; + if (taps[0].tappingProcess != processID) { + [self stopMediaKeys]; + [self startMediaKeys]; + } + } + talloc_free(taps); +} + - (void)startMediaKeys { dispatch_async(dispatch_get_main_queue(), ^{ @@ -396,6 +415,7 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx) { dispatch_async(dispatch_get_main_queue(), ^{ NSMachPort *port = (NSMachPort *)self->_mk_tap_port; + CGEventTapEnable(self->_mk_tap_port, false); [[NSRunLoop mainRunLoop] removePort:port forMode:NSRunLoopCommonModes]; CFRelease(self->_mk_tap_port); self->_mk_tap_port = nil; diff --git a/osdep/macosx_events_objc.h b/osdep/macosx_events_objc.h index 36de66dc88..f3210f2580 100644 --- a/osdep/macosx_events_objc.h +++ b/osdep/macosx_events_objc.h @@ -40,6 +40,8 @@ struct input_ctx; - (void)putKey:(int)keycode; +- (void)setHighestPriotityMediaKeysTap; + - (void)handleFilesArray:(NSArray *)files; - (bool)processKeyEvent:(NSEvent *)event; -- cgit v1.2.3