From b8f2811f8d180d4c2a39aabc046440c900652bb4 Mon Sep 17 00:00:00 2001 From: der richter Date: Sat, 15 Feb 2020 16:13:37 +0100 Subject: mac: fix media key support for libmpv users this basically moves the remote command center to our mac events instead of keeping it our Application, which is only available when started from mpv itself. also make it independent of the NSApplication. this also prevents a runtime crash --- osdep/macos/remote_command_center.swift | 23 ++++++++++++++--------- osdep/macosx_application.m | 24 ------------------------ osdep/macosx_application_objc.h | 3 --- osdep/macosx_events.m | 23 ++++++++++++++++------- osdep/macosx_events_objc.h | 3 +++ 5 files changed, 33 insertions(+), 43 deletions(-) diff --git a/osdep/macos/remote_command_center.swift b/osdep/macos/remote_command_center.swift index b08a726183..a5dd662737 100644 --- a/osdep/macos/remote_command_center.swift +++ b/osdep/macos/remote_command_center.swift @@ -87,14 +87,10 @@ class RemoteCommandCenter: NSObject { MPRemoteCommandCenter.shared().bookmarkCommand, ] - let application: Application - var mpInfoCenter: MPNowPlayingInfoCenter { get { return MPNowPlayingInfoCenter.default() } } var isPaused: Bool = false { didSet { updatePlaybackState() } } - @objc init(app: Application) { - application = app - + @objc override init() { super.init() for cmd in disabledCommands { @@ -110,8 +106,10 @@ class RemoteCommandCenter: NSObject { } } - if let icon = application.getMPVIcon(), #available(macOS 10.13.2, *) { - let albumArt = MPMediaItemArtwork(boundsSize:icon.size) { _ in + if let app = NSApp as? Application, let icon = app.getMPVIcon(), + #available(macOS 10.13.2, *) + { + let albumArt = MPMediaItemArtwork(boundsSize: icon.size) { _ in return icon } nowPlayingInfo[MPMediaItemPropertyArtwork] = albumArt @@ -119,6 +117,13 @@ class RemoteCommandCenter: NSObject { mpInfoCenter.nowPlayingInfo = nowPlayingInfo mpInfoCenter.playbackState = .playing + + NotificationCenter.default.addObserver( + self, + selector: #selector(self.makeCurrent), + name: NSApplication.willBecomeActiveNotification, + object: nil + ) } @objc func stop() { @@ -131,7 +136,7 @@ class RemoteCommandCenter: NSObject { mpInfoCenter.playbackState = .unknown } - @objc func makeCurrent() { + @objc func makeCurrent(notification: NSNotification) { mpInfoCenter.playbackState = .paused mpInfoCenter.playbackState = .playing updatePlaybackState() @@ -160,7 +165,7 @@ class RemoteCommandCenter: NSObject { } } - application.handleMPKey(mpKey, withMask: Int32(state)); + EventsResponder.sharedInstance().handleMPKey(mpKey, withMask: Int32(state)) return .success } diff --git a/osdep/macosx_application.m b/osdep/macosx_application.m index 7d42ca2a41..958b67b948 100644 --- a/osdep/macosx_application.m +++ b/osdep/macosx_application.m @@ -107,7 +107,6 @@ static void terminate_cocoa_application(void) @synthesize menuBar = _menu_bar; @synthesize openCount = _open_count; @synthesize cocoaCB = _cocoa_cb; -@synthesize remoteCommandCenter = _remoteCommandCenter; - (void)sendEvent:(NSEvent *)event { @@ -174,12 +173,6 @@ static const char macosx_icon[] = #if HAVE_MACOS_TOUCHBAR if ([self respondsToSelector:@selector(touchBar)]) [(TouchBar *)self.touchBar processEvent:event]; -#endif -#if HAVE_MACOS_MEDIA_PLAYER - // 10.12.2 runtime availability check - if ([self respondsToSelector:@selector(touchBar)]) { - [_remoteCommandCenter processEvent:event]; - } #endif if (_cocoa_cb) { [_cocoa_cb processEvent:event]; @@ -208,11 +201,6 @@ static const char macosx_icon[] = [_eventsResponder queueCommand:cmd]; } -- (void)handleMPKey:(int)key withMask:(int)mask -{ - [_eventsResponder handleMPKey:key withMask:mask]; -} - - (void)stopMPV:(char *)cmd { if (![_eventsResponder queueCommand:cmd]) @@ -228,11 +216,6 @@ static const char macosx_icon[] = andEventID:kAEQuitApplication]; } -- (void)applicationWillBecomeActive:(NSNotification *)notification -{ - [_remoteCommandCenter makeCurrent]; -} - - (void)handleQuitEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent { @@ -307,13 +290,6 @@ static void init_cocoa_application(bool regular) [NSApp setDelegate:NSApp]; [NSApp setMenuBar:[[MenuBar alloc] init]]; -#if HAVE_MACOS_MEDIA_PLAYER - // 10.12.2 runtime availability check - if ([NSApp respondsToSelector:@selector(touchBar)]) { - [NSApp setRemoteCommandCenter:[[RemoteCommandCenter alloc] initWithApp:NSApp]]; - } -#endif - // Will be set to Regular from cocoa_common during UI creation so that we // don't create an icon when playing audio only files. [NSApp setActivationPolicy: regular ? diff --git a/osdep/macosx_application_objc.h b/osdep/macosx_application_objc.h index 4bd5b55b1a..11959a83ea 100644 --- a/osdep/macosx_application_objc.h +++ b/osdep/macosx_application_objc.h @@ -20,7 +20,6 @@ #import "osdep/macosx_menubar_objc.h" @class CocoaCB; -@class RemoteCommandCenter; struct mpv_event; struct mpv_handle; @@ -29,7 +28,6 @@ struct mpv_handle; - (NSImage *)getMPVIcon; - (void)processEvent:(struct mpv_event *)event; - (void)queueCommand:(char *)cmd; -- (void)handleMPKey:(int)key withMask:(int)mask; - (void)stopMPV:(char *)cmd; - (void)openFiles:(NSArray *)filenames; - (void)setMpvHandle:(struct mpv_handle *)ctx; @@ -39,5 +37,4 @@ struct mpv_handle; @property(nonatomic, retain) MenuBar *menuBar; @property(nonatomic, assign) size_t openCount; @property(nonatomic, retain) CocoaCB *cocoaCB; -@property(nonatomic, retain) RemoteCommandCenter *remoteCommandCenter; @end diff --git a/osdep/macosx_events.m b/osdep/macosx_events.m index 3f40e41f6c..e2ae7aa162 100644 --- a/osdep/macosx_events.m +++ b/osdep/macosx_events.m @@ -161,6 +161,8 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx) @implementation EventsResponder +@synthesize remoteCommandCenter = _remoteCommandCenter; + + (EventsResponder *)sharedInstance { static EventsResponder *responder = nil; @@ -272,14 +274,18 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx) [NSApp processEvent:event]; } + if (_remoteCommandCenter) { + [_remoteCommandCenter processEvent:event]; + } + switch (event->event_id) { case MPV_EVENT_SHUTDOWN: { - #if HAVE_MACOS_COCOA_CB +#if HAVE_MACOS_COCOA_CB if ([(Application *)NSApp cocoaCB].isShuttingDown) { _ctx = nil; return; } - #endif +#endif mpv_destroy(_ctx); _ctx = nil; break; @@ -289,16 +295,19 @@ void cocoa_set_mpv_handle(struct mpv_handle *ctx) - (void)startMediaKeys { - if ([(Application *)NSApp remoteCommandCenter]) { - [[(Application *)NSApp remoteCommandCenter] start]; +#if HAVE_MACOS_MEDIA_PLAYER + // 10.12.2 runtime availability check + if (_remoteCommandCenter == nil && [NSApp respondsToSelector:@selector(touchBar)]) { + _remoteCommandCenter = [[RemoteCommandCenter alloc] init]; } +#endif + + [_remoteCommandCenter start]; } - (void)stopMediaKeys { - if ([(Application *)NSApp remoteCommandCenter]) { - [[(Application *)NSApp remoteCommandCenter] stop]; - } + [_remoteCommandCenter stop]; } - (int)mapKeyModifiers:(int)cocoaModifiers diff --git a/osdep/macosx_events_objc.h b/osdep/macosx_events_objc.h index efc4e8f7ec..9394fe7160 100644 --- a/osdep/macosx_events_objc.h +++ b/osdep/macosx_events_objc.h @@ -20,6 +20,7 @@ #import #include "osdep/macosx_events.h" +@class RemoteCommandCenter; struct input_ctx; @interface EventsResponder : NSObject @@ -39,4 +40,6 @@ struct input_ctx; - (BOOL)handleMPKey:(int)key withMask:(int)mask; +@property(nonatomic, retain) RemoteCommandCenter *remoteCommandCenter; + @end -- cgit v1.2.3