diff --git a/Application/MediaKeysApplication.m b/Application/MediaKeysApplication.m index 57d0df88f..ef7f58b72 100644 --- a/Application/MediaKeysApplication.m +++ b/Application/MediaKeysApplication.m @@ -19,16 +19,6 @@ @implementation MediaKeysApplication -+(void)initialize; -{ - if([self class] != [MediaKeysApplication class]) return; - - // Register defaults for the whitelist of apps that want to use media keys - [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys: - [SPMediaKeyTap defaultMediaKeyUserBundleIdentifiers], kMediaKeyUsingBundleIdentifiersDefaultsKey, - nil]]; -} - - (void)finishLaunching { [super finishLaunching]; @@ -36,35 +26,24 @@ forKeyPath:@"allowLastfmMediaKeys" options:NSKeyValueObservingOptionNew context:nil]; - - if (@available(macOS 10.14, *)) { - MPRemoteCommandCenter *remoteCommandCenter = [MPRemoteCommandCenter sharedCommandCenter]; - - [remoteCommandCenter.playCommand setEnabled:YES]; - [remoteCommandCenter.pauseCommand setEnabled:YES]; - [remoteCommandCenter.togglePlayPauseCommand setEnabled:YES]; - [remoteCommandCenter.stopCommand setEnabled:YES]; - [remoteCommandCenter.changePlaybackPositionCommand setEnabled:YES]; - [remoteCommandCenter.nextTrackCommand setEnabled:YES]; - [remoteCommandCenter.previousTrackCommand setEnabled:YES]; - - [[remoteCommandCenter playCommand] addTarget:self action:@selector(clickPlay)]; - [[remoteCommandCenter pauseCommand] addTarget:self action:@selector(clickPause)]; - [[remoteCommandCenter togglePlayPauseCommand] addTarget:self action:@selector(clickPlay)]; - [[remoteCommandCenter stopCommand] addTarget:self action:@selector(clickStop)]; - [[remoteCommandCenter changePlaybackPositionCommand] addTarget:self action:@selector(clickSeek:)]; - [[remoteCommandCenter nextTrackCommand] addTarget:self action:@selector(clickNext)]; - [[remoteCommandCenter previousTrackCommand] addTarget:self action:@selector(clickPrev)]; - } - else - { - keyTap = [[SPMediaKeyTap alloc] initWithDelegate:self]; - if([SPMediaKeyTap usesGlobalMediaKeyTap]) { - [keyTap startWatchingMediaKeys]; - } - else - ALog(@"Media key monitoring disabled"); - } + + MPRemoteCommandCenter *remoteCommandCenter = [MPRemoteCommandCenter sharedCommandCenter]; + + [remoteCommandCenter.playCommand setEnabled:YES]; + [remoteCommandCenter.pauseCommand setEnabled:YES]; + [remoteCommandCenter.togglePlayPauseCommand setEnabled:YES]; + [remoteCommandCenter.stopCommand setEnabled:YES]; + [remoteCommandCenter.changePlaybackPositionCommand setEnabled:YES]; + [remoteCommandCenter.nextTrackCommand setEnabled:YES]; + [remoteCommandCenter.previousTrackCommand setEnabled:YES]; + + [[remoteCommandCenter playCommand] addTarget:self action:@selector(clickPlay)]; + [[remoteCommandCenter pauseCommand] addTarget:self action:@selector(clickPause)]; + [[remoteCommandCenter togglePlayPauseCommand] addTarget:self action:@selector(clickPlay)]; + [[remoteCommandCenter stopCommand] addTarget:self action:@selector(clickStop)]; + [[remoteCommandCenter changePlaybackPositionCommand] addTarget:self action:@selector(clickSeek:)]; + [[remoteCommandCenter nextTrackCommand] addTarget:self action:@selector(clickNext)]; + [[remoteCommandCenter previousTrackCommand] addTarget:self action:@selector(clickPrev)]; } - (MPRemoteCommandHandlerStatus)clickPlay { @@ -97,72 +76,5 @@ return MPRemoteCommandHandlerStatusSuccess; } -- (void)sendEvent: (NSEvent*)event -{ - BOOL shouldHandleMediaKeyEventLocally = ![SPMediaKeyTap usesGlobalMediaKeyTap]; - - if(shouldHandleMediaKeyEventLocally && [event type] == NSEventTypeSystemDefined && [event subtype] == 8 ) - { - [self mediaKeyTap:nil receivedMediaKeyEvent:event]; - } - - [super sendEvent: event]; -} - --(void)mediaKeyTap:(SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event; -{ - NSAssert([event type] == NSEventTypeSystemDefined && [event subtype] == SPSystemDefinedEventMediaKeys, @"Unexpected NSEvent in mediaKeyTap:receivedMediaKeyEvent:"); - - int keyCode = (([event data1] & 0xFFFF0000) >> 16); - int keyFlags = ([event data1] & 0x0000FFFF); - BOOL keyIsPressed = (((keyFlags & 0xFF00) >> 8)) == 0xA; - - if (!keyIsPressed) // pressed and released - { - switch( keyCode ) - { - case NX_KEYTYPE_PLAY: - [self clickPlay]; - break; - - case NX_KEYTYPE_NEXT: - case NX_KEYTYPE_FAST: - [self clickNext]; - break; - - case NX_KEYTYPE_PREVIOUS: - case NX_KEYTYPE_REWIND: - [self clickPrev]; - break; - } - } -} - -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - if ([keyPath isEqualToString:@"allowLastfmMediaKeys"]) - { - NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; - BOOL allowLastfmMediaKeys = [defs boolForKey:@"allowLastfmMediaKeys"]; - NSArray *old = [defs arrayForKey:kMediaKeyUsingBundleIdentifiersDefaultsKey]; - - NSMutableArray *new = [old mutableCopy]; - NSArray *lastfmIds = [NSArray arrayWithObjects:@"fm.last.Last.fm", @"fm.last.Scrobbler", nil]; - if (allowLastfmMediaKeys) - { - [new addObjectsFromArray:lastfmIds]; - } - else - { - [new removeObjectsInArray:lastfmIds]; - } - - [defs setObject:new forKey:kMediaKeyUsingBundleIdentifiersDefaultsKey]; - } - else - { - [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; - } -} - @end diff --git a/Application/PlaybackController.m b/Application/PlaybackController.m index c6950734a..5a2482334 100644 --- a/Application/PlaybackController.m +++ b/Application/PlaybackController.m @@ -630,39 +630,37 @@ NSDictionary * makeRGInfo(PlaylistEntry *pe) } - (void)sendMetaData { - if (NSClassFromString(@"MPNowPlayingInfoCenter")) { - MPNowPlayingInfoCenter * defaultCenter = [MPNowPlayingInfoCenter defaultCenter]; + MPNowPlayingInfoCenter * defaultCenter = [MPNowPlayingInfoCenter defaultCenter]; - PlaylistEntry * entry = [playlistController currentEntry]; - NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init]; - - if (entry) { - if ([entry title]) - [songInfo setObject:[entry title] forKey:MPMediaItemPropertyTitle]; - if ([entry artist]) - [songInfo setObject:[entry artist] forKey:MPMediaItemPropertyArtist]; - if ([entry album]) - [songInfo setObject:[entry album] forKey:MPMediaItemPropertyAlbumTitle]; - [songInfo setObject:[NSNumber numberWithFloat:[entry currentPosition]] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; - [songInfo setObject:[entry length] forKey:MPMediaItemPropertyPlaybackDuration]; - [songInfo setObject:[NSNumber numberWithFloat:[entry index]] forKey:MPMediaItemPropertyPersistentID]; - } - - switch (playbackStatus) { - case CogStatusPlaying: - defaultCenter.playbackState = MPNowPlayingPlaybackStatePlaying; - break; - case CogStatusPaused: - defaultCenter.playbackState = MPNowPlayingPlaybackStatePaused; - break; - - default: - defaultCenter.playbackState = MPNowPlayingPlaybackStateStopped; - break; - } - - [defaultCenter setNowPlayingInfo:songInfo]; + PlaylistEntry * entry = [playlistController currentEntry]; + NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init]; + + if (entry) { + if ([entry title]) + [songInfo setObject:[entry title] forKey:MPMediaItemPropertyTitle]; + if ([entry artist]) + [songInfo setObject:[entry artist] forKey:MPMediaItemPropertyArtist]; + if ([entry album]) + [songInfo setObject:[entry album] forKey:MPMediaItemPropertyAlbumTitle]; + [songInfo setObject:[NSNumber numberWithFloat:[entry currentPosition]] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; + [songInfo setObject:[entry length] forKey:MPMediaItemPropertyPlaybackDuration]; + [songInfo setObject:[NSNumber numberWithFloat:[entry index]] forKey:MPMediaItemPropertyPersistentID]; } + + switch (playbackStatus) { + case CogStatusPlaying: + defaultCenter.playbackState = MPNowPlayingPlaybackStatePlaying; + break; + case CogStatusPaused: + defaultCenter.playbackState = MPNowPlayingPlaybackStatePaused; + break; + + default: + defaultCenter.playbackState = MPNowPlayingPlaybackStateStopped; + break; + } + + [defaultCenter setNowPlayingInfo:songInfo]; }