From 06d2e51367cdf182a836bdca1be0e7c7c5f4512d Mon Sep 17 00:00:00 2001 From: casey langen Date: Sat, 19 Jan 2019 10:21:46 -0800 Subject: [PATCH] Shuffled around a bit of code to remove asserts during init to make debugging easier. --- src/plugins/macosmediakeys/SPMediaKeyTap.h | 2 +- src/plugins/macosmediakeys/SPMediaKeyTap.m | 16 +++++--- src/plugins/macosmediakeys/plugin.mm | 44 +++++++++++++++++----- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/plugins/macosmediakeys/SPMediaKeyTap.h b/src/plugins/macosmediakeys/SPMediaKeyTap.h index aa974d238..53b22f44e 100644 --- a/src/plugins/macosmediakeys/SPMediaKeyTap.h +++ b/src/plugins/macosmediakeys/SPMediaKeyTap.h @@ -22,7 +22,7 @@ -(id)initWithDelegate:(id)delegate; +(BOOL)usesGlobalMediaKeyTap; --(void)startWatchingMediaKeys; +-(BOOL)startWatchingMediaKeys; -(void)stopWatchingMediaKeys; -(void)handleAndReleaseMediaKeyEvent:(NSEvent *)event; @end diff --git a/src/plugins/macosmediakeys/SPMediaKeyTap.m b/src/plugins/macosmediakeys/SPMediaKeyTap.m index 97d259418..0ead2bd2f 100644 --- a/src/plugins/macosmediakeys/SPMediaKeyTap.m +++ b/src/plugins/macosmediakeys/SPMediaKeyTap.m @@ -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]; @@ -73,13 +73,17 @@ static CGEventRef tapEventCallback(CGEventTapProxy proxy, CGEventType type, CGEv CGEventMaskBit(NX_SYSDEFINED), tapEventCallback, self); - assert(_eventPort != NULL); - _eventPortSource = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, _eventPort, 0); - assert(_eventPortSource != NULL); + if (_eventPort) { + _eventPortSource = CFMachPortCreateRunLoopSource(kCFAllocatorSystemDefault, _eventPort, 0); + if (_eventPortSource) { + // 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; + } + } - // 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 NO; } -(void)stopWatchingMediaKeys; { diff --git a/src/plugins/macosmediakeys/plugin.mm b/src/plugins/macosmediakeys/plugin.mm index 88a4c59aa..e59e51c6a 100644 --- a/src/plugins/macosmediakeys/plugin.mm +++ b/src/plugins/macosmediakeys/plugin.mm @@ -35,12 +35,16 @@ #include #include #include +#include #include #include #include "SPMediaKeyTap.h" using namespace musik::core::sdk; +static const char* TAG = "macosmediakeys"; +static IDebug* debug = nullptr; + struct IKeyProcessor { virtual void ProcessKeyCode(int keyCode) = 0; }; @@ -49,6 +53,7 @@ struct IKeyProcessor { SPMediaKeyTap* keyTap; IKeyProcessor* processor; } +- (BOOL) register; - (void) unregister; - (void) setKeyProcessor: (IKeyProcessor*) processor; @end @@ -70,16 +75,30 @@ class PlaybackRemote: public IPlaybackRemote, IKeyProcessor { public: PlaybackRemote() { this->mediaKeys = [[MediaKeys alloc] init]; - [this->mediaKeys setKeyProcessor: this]; + if ([this->mediaKeys register]) { + [this->mediaKeys setKeyProcessor: this]; + debug->Info(TAG, "listening to media keys"); + } + else { + debug->Error(TAG, "failed to register media keys listener"); + this->Unregister(); + } } virtual void Release() override { - [this->mediaKeys unregister]; - [this->mediaKeys release]; - this->mediaKeys = nullptr; + this->Unregister(); delete this; } + void Unregister() { + if (this->mediaKeys) { + [this->mediaKeys unregister]; + [this->mediaKeys release]; + this->mediaKeys = nullptr; + debug->Info(TAG, "unregistered media keys"); + } + } + virtual void SetPlaybackService(IPlaybackService* playback) override { std::unique_lock lock(mutex); this->playback = playback; @@ -135,10 +154,6 @@ class PlaybackRemote: public IPlaybackRemote, IKeyProcessor { [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey, nil]]; - if ([SPMediaKeyTap usesGlobalMediaKeyTap]) { - [keyTap startWatchingMediaKeys]; - } - return self; } @@ -158,7 +173,14 @@ class PlaybackRemote: public IPlaybackRemote, IKeyProcessor { } } -- (void) unregister; { +- (BOOL) register { + if ([SPMediaKeyTap usesGlobalMediaKeyTap]) { + return [keyTap startWatchingMediaKeys]; + } + return NO; +} + +- (void) unregister { if (keyTap) { [keyTap stopWatchingMediaKeys]; [keyTap release]; @@ -173,3 +195,7 @@ extern "C" IPlugin* GetPlugin() { extern "C" IPlaybackRemote* GetPlaybackRemote() { return new PlaybackRemote(); } + +extern "C" void SetDebug(IDebug* debug) { + ::debug = debug; +} \ No newline at end of file