summaryrefslogtreecommitdiffstats
path: root/osdep/macosx_events.m
diff options
context:
space:
mode:
authorder richter <der.richter@gmx.de>2019-11-30 16:08:32 +0100
committerder richter <der.richter@gmx.de>2019-12-15 20:07:31 +0100
commita32db637b5c66c0304147caadf02d7b28083bcee (patch)
treea078fd47fd36cc853a1422ea05a949fbc9afde60 /osdep/macosx_events.m
parent8a6ee7fe947bb01a49beb38152cedb1e1b206ed2 (diff)
downloadmpv-a32db637b5c66c0304147caadf02d7b28083bcee.tar.bz2
mpv-a32db637b5c66c0304147caadf02d7b28083bcee.tar.xz
mac: replace old event tap for media key support with MediaPlayer
the old event tap has several problems, like no proper priority support or having to set accessibility permissions for mpv or the terminal. it is now replaced by the new MediaPlayer which has proper priority support and isn't as greedy as previously. this only includes Media Key support and not any of the other features included in the MediaPlayer framework, like proper Now Playing data (only set dummy data for now). this is only available on macOS 10.12.2 and higher. also removes some unnecessary redefines. Fixes #6389
Diffstat (limited to 'osdep/macosx_events.m')
-rw-r--r--osdep/macosx_events.m127
1 files changed, 6 insertions, 121 deletions
diff --git a/osdep/macosx_events.m b/osdep/macosx_events.m
index 14aa7063fb..3f40e41f6c 100644
--- a/osdep/macosx_events.m
+++ b/osdep/macosx_events.m
@@ -49,15 +49,12 @@
struct mpv_handle *_ctx;
BOOL _is_application;
NSCondition *_input_lock;
- CFMachPortRef _mk_tap_port;
}
-- (BOOL)handleMediaKey:(NSEvent *)event;
- (NSEvent *)handleKey:(NSEvent *)event;
- (BOOL)setMpvHandle:(struct mpv_handle *)ctx;
- (void)readEvents;
- (void)startMediaKeys;
-- (void)restartMediaKeys;
- (void)stopMediaKeys;
- (int)mapKeyModifiers:(int)cocoaModifiers;
- (int)keyModifierMask:(NSEvent *)event;
@@ -121,53 +118,6 @@ static int convert_key(unsigned key, unsigned charcode)
return charcode;
}
-static int mk_code(NSEvent *event)
-{
- return (([event data1] & 0xFFFF0000) >> 16);
-}
-
-static int mk_flags(NSEvent *event)
-{
- return ([event data1] & 0x0000FFFF);
-}
-
-static int mk_down(NSEvent *event)
-{
- return (((mk_flags(event) & 0xFF00) >> 8)) == 0xA;
-}
-
-static CGEventRef tap_event_callback(CGEventTapProxy proxy, CGEventType type,
- CGEventRef event, void *ctx)
-{
- EventsResponder *responder = ctx;
-
- if (type == kCGEventTapDisabledByTimeout) {
- // The Mach Port receiving the taps became unresponsive for some
- // reason, restart listening on it.
- [responder restartMediaKeys];
- return event;
- }
-
- if (type == kCGEventTapDisabledByUserInput)
- return event;
-
- NSEvent *nse = [NSEvent eventWithCGEvent:event];
-
- if ([nse type] != NSEventTypeSystemDefined || [nse subtype] != 8)
- // This is not a media key
- return event;
-
- if (mk_down(nse) && [responder handleMediaKey:nse]) {
- // Handled this event, return nil so that it is removed from the
- // global queue.
- return nil;
- } else {
- // Was a media key but we were not interested in it. Leave it in the
- // global queue by returning the original event.
- return event;
- }
-}
-
void cocoa_init_media_keys(void)
{
[[EventsResponder sharedInstance] startMediaKeys];
@@ -337,77 +287,18 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx)
}
}
-- (void)restartMediaKeys
-{
- if (self->_mk_tap_port)
- 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(), ^{
- // Install a Quartz Event Tap. This will notify mpv through the
- // returned Mach Port and cause mpv to execute the `tap_event_callback`
- // function.
- self->_mk_tap_port = CGEventTapCreate(kCGSessionEventTap,
- kCGHeadInsertEventTap,
- kCGEventTapOptionDefault,
- CGEventMaskBit(NX_SYSDEFINED),
- tap_event_callback,
- self);
-
- if (self->_mk_tap_port) {
- NSMachPort *port = (NSMachPort *)self->_mk_tap_port;
- [[NSRunLoop mainRunLoop] addPort:port forMode:NSRunLoopCommonModes];
- }
- });
+ if ([(Application *)NSApp remoteCommandCenter]) {
+ [[(Application *)NSApp remoteCommandCenter] start];
+ }
}
- (void)stopMediaKeys
{
- dispatch_async(dispatch_get_main_queue(), ^{
- NSMachPort *port = (NSMachPort *)self->_mk_tap_port;
- if (port) {
- CGEventTapEnable(self->_mk_tap_port, false);
- [[NSRunLoop mainRunLoop] removePort:port forMode:NSRunLoopCommonModes];
- CFRelease(self->_mk_tap_port);
- self->_mk_tap_port = nil;
- }
- });
-}
-
-- (BOOL)handleMediaKey:(NSEvent *)event
-{
- NSDictionary *keymapd = @{
- @(NX_KEYTYPE_PLAY): @(MP_KEY_PLAY),
- @(NX_KEYTYPE_REWIND): @(MP_KEY_PREV),
- @(NX_KEYTYPE_FAST): @(MP_KEY_NEXT),
- @(NX_KEYTYPE_PREVIOUS): @(MP_KEY_REWIND),
- @(NX_KEYTYPE_NEXT): @(MP_KEY_FORWARD),
- };
-
- return [self handleKey:mk_code(event)
- withMask:[self keyModifierMask:event]
- andMapping:keymapd];
+ if ([(Application *)NSApp remoteCommandCenter]) {
+ [[(Application *)NSApp remoteCommandCenter] stop];
+ }
}
- (int)mapKeyModifiers:(int)cocoaModifiers
@@ -452,12 +343,6 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx)
}
}
--(BOOL)handleKey:(int)key withMask:(int)mask andMapping:(NSDictionary *)mapping
-{
- int mpkey = [mapping[@(key)] intValue];
- return [self handleMPKey:mpkey withMask:mask];
-}
-
- (NSEvent*)handleKey:(NSEvent *)event
{
if ([event isARepeat]) return nil;