X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/f063b37f95587561483eac59c8a13709bd20beef..fce120b94a1adc6b9c6e1796217d8065bc105fbb:/iTunesRemote.m diff --git a/iTunesRemote.m b/iTunesRemote.m index a5e45be..35768cd 100755 --- a/iTunesRemote.m +++ b/iTunesRemote.m @@ -26,12 +26,14 @@ { ITDebugLog(@"iTunesRemote begun"); savedPSN = [self iTunesPSN]; + [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler:) name:@"com.apple.iTunes.playerInfo" object:@"com.apple.iTunes.player"]; return YES; } - (BOOL)halt { ITDebugLog(@"iTunesRemote halted"); + [[NSDistributedNotificationCenter defaultCenter] removeObserver:self]; return YES; } @@ -120,7 +122,7 @@ SInt32 result; ITDebugLog(@"Getting player playing state"); - result = [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }", 'core', 'getd', &savedPSN) int32Value]; + result = [ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }", 'core', 'getd', &savedPSN) typeCodeValue]; switch (result) { case 'kPSP': @@ -388,8 +390,10 @@ NSString *temp1; ITDebugLog(@"Getting current unique identifier."); NSAppleEventDescriptor *descriptor = ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pcls'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN); - if ((descriptor == nil) || ([descriptor int32Value] == 'prop')) { + if ([descriptor int32Value] == 'prop') { return @"0-0"; + } else if (descriptor == nil) { + return nil; } SInt32 cls = [descriptor int32Value]; if ( ([self currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist) || (cls == 'cURT') ) { @@ -626,22 +630,27 @@ - (BOOL)shuffleEnabled { + int result; ITDebugLog(@"Getting shuffle enabled status."); - BOOL final; - int result = (int)[ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pShf'), from:obj { form:'prop', want:type('pPla'), seld:type('pEQP'), from:'null'() } }", 'core', 'getd', &savedPSN) int32Value]; - if (result != 0) { - final = YES; - } else { - final = NO; - } + if ([[self playerStateUniqueIdentifier] isEqualToString:@"0-0"]) { + ITDebugLog(@"No current playlist, getting shuffle status from visible playlist."); + result = (int)[ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPly'), from:obj { form:'indx', want:type('cBrW'), seld:1, from:'null'() } } }", 'core', 'getd', &savedPSN) int32Value]; + } else { + result = (int)[ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }", 'core', 'getd', &savedPSN) int32Value]; + } ITDebugLog(@"Getting shuffle enabled status done."); - return final; + return (result != 0); } - (BOOL)setShuffleEnabled:(BOOL)enabled { ITDebugLog(@"Set shuffle enabled to %i", enabled); - ITSendAEWithString([NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }", (unsigned long)enabled], 'core', 'setd', &savedPSN); + if ([[self playerStateUniqueIdentifier] isEqualToString:@"0-0"]) { + ITDebugLog(@"No current playlist, setting shuffle status on visible playlist."); + ITSendAEWithString([NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPly'), from:obj { form:'indx', want:type('cBrW'), seld:1, from:'null'() } } }", (unsigned long)enabled], 'core', 'setd', &savedPSN); + } else { + ITSendAEWithString([NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }", (unsigned long)enabled], 'core', 'setd', &savedPSN); + } ITDebugLog(@"Set shuffle enabled to %i done", enabled); return YES; } @@ -650,8 +659,14 @@ { FourCharCode m00f = 0; int result = 0; - m00f = (FourCharCode)[ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pRpt'), from:obj { form:'prop', want:type('pPla'), seld:type('pEQP'), from:'null'() } }", 'core', 'getd', &savedPSN) int32Value]; - ITDebugLog(@"Getting repeat mode."); + ITDebugLog(@"Getting repeat mode."); + m00f = (FourCharCode)[ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pRpt'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }", 'core', 'getd', &savedPSN) typeCodeValue]; + + if (m00f == 0) { + ITDebugLog(@"No current playlist, getting repeat mode from visible playlist."); + m00f = (FourCharCode)[ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pRpt'), from:obj { form:'prop', want:type('prop'), seld:type('pPly'), from:obj { form:'indx', want:type('cBrW'), seld:1, from:'null'() } } }", 'core', 'getd', &savedPSN) typeCodeValue]; + } + switch (m00f) { //case 'kRp0': @@ -689,7 +704,12 @@ m00f = "kRp0"; break; } - ITSendAEWithString([NSString stringWithFormat:@"data:'%s', '----':obj { form:'prop', want:type('prop'), seld:type('pRpt'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:() } }", m00f], 'core', 'setd', &savedPSN); + if ([[self playerStateUniqueIdentifier] isEqualToString:@"0-0"]) { + ITDebugLog(@"No current playlist, setting repeat mode on visible playlist."); + ITSendAEWithString([NSString stringWithFormat:@"data:'%s', '----':obj { form:'prop', want:type('prop'), seld:type('pRpt'), from:obj { form:'prop', want:type('prop'), seld:type('pPly'), from:obj { form:'indx', want:type('cBrW'), seld:1, from:'null'() } } }", m00f], 'core', 'setd', &savedPSN); + } else { + ITSendAEWithString([NSString stringWithFormat:@"data:'%s', '----':obj { form:'prop', want:type('prop'), seld:type('pRpt'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:() } }", m00f], 'core', 'setd', &savedPSN); + } ITDebugLog(@"Setting repeat mode to %c done", m00f); return YES; } @@ -803,6 +823,8 @@ //Duplicate search results to playlist for (i = 1; i <= [searchResults numberOfItems]; i++) { + //NSLog(@"%@", ITSendAEWithStringAndParameter(@"'----':obj { form:'prop', want:type('prop'), seld:prop('pnam'), from:aevt(@) }", *[[searchResults descriptorAtIndex:i] aeDesc], 'core', 'getd', &savedPSN)); + ITSendAEWithStringAndObject(@"insh:obj { form:'name', want:type('cPly'), seld:\"MenuTunes\", from:'null'() }", [[searchResults descriptorAtIndex:i] aeDesc], 'core', 'clon', &savedPSN); } //Reset fixed indexing @@ -819,6 +841,13 @@ return ([ITSendAEWithString(@"'----':obj { form:'prop', want:type('prop'), seld:type('pcls'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }", 'core', 'getd', &savedPSN) int32Value] != 'prop'); } +- (void)notificationHandler:(NSNotification *)note +{ + ITDebugLog(@"Received notification: %@", note); + [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTTrackChanged" object:self userInfo:[note userInfo]]; + ITDebugLog(@"Handled notification."); +} + - (ProcessSerialNumber)iTunesPSN { /*NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];