Shuffled around a bit of code to remove asserts during init to make debugging easier.

This commit is contained in:
casey langen 2019-01-19 10:21:46 -08:00
parent b2c6b34a7b
commit 06d2e51367
3 changed files with 46 additions and 16 deletions

View File

@ -22,7 +22,7 @@
-(id)initWithDelegate:(id)delegate;
+(BOOL)usesGlobalMediaKeyTap;
-(void)startWatchingMediaKeys;
-(BOOL)startWatchingMediaKeys;
-(void)stopWatchingMediaKeys;
-(void)handleAndReleaseMediaKeyEvent:(NSEvent *)event;
@end

View File

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

View File

@ -35,12 +35,16 @@
#include <core/sdk/constants.h>
#include <core/sdk/IPlugin.h>
#include <core/sdk/IPlaybackRemote.h>
#include <core/sdk/IDebug.h>
#include <mutex>
#include <iostream>
#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<decltype(mutex)> 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;
}