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
This commit is contained in:
der richter 2020-02-15 16:13:37 +01:00
parent c1d744328e
commit b8f2811f8d
5 changed files with 33 additions and 43 deletions

View File

@ -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
}

View File

@ -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 ?

View File

@ -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

View File

@ -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

View File

@ -20,6 +20,7 @@
#import <Cocoa/Cocoa.h>
#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