w00t. If player application is not running, instead of simply saying so,
[MenuTunes.git] / iTunesRemote.m
1 #import "iTunesRemote.h"
2
3 @implementation iTunesRemote
4
5 + (id)remote
6 {
7     return [[[iTunesRemote alloc] init] autorelease];
8 }
9
10 - (NSString *)remoteTitle
11 {
12     return @"iTunes Remote";
13 }
14
15 - (NSString *)remoteInformation
16 {
17     return @"Default MenuTunes plugin to control iTunes, by iThink Software.";
18 }
19
20 - (NSImage *)remoteIcon
21 {
22     return nil;
23 }
24
25 - (BOOL)begin
26 {
27     iTunesPSN = [self iTunesPSN];
28     
29     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
30     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
31     
32     return YES;
33 }
34
35 - (BOOL)halt
36 {
37     iTunesPSN.highLongOfPSN = kNoProcess;
38
39     //Unregister for application termination in NSWorkspace
40     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
41
42     return YES;
43 }
44
45 - (NSString *)playerFullName
46 {
47     return @"iTunes";
48 }
49
50 - (NSString *)playerSimpleName
51 {
52     return @"iTunes";
53 }
54
55 - (NSDictionary *)capabilities
56 {
57     return [NSDictionary dictionaryWithObjectsAndKeys:
58                 [NSNumber numberWithBool: YES], @"Remote",
59                 [NSNumber numberWithBool: YES], @"Basic Track Control",
60                 [NSNumber numberWithBool: YES], @"Track Information",
61                 [NSNumber numberWithBool: YES], @"Track Navigation",
62                 [NSNumber numberWithBool: YES], @"Upcoming Songs",
63                 [NSNumber numberWithBool: YES], @"Playlists",
64                 [NSNumber numberWithBool: YES], @"Volume",
65                 [NSNumber numberWithBool: YES], @"Shuffle",
66                 [NSNumber numberWithBool: YES], @"Repeat Modes",
67                 [NSNumber numberWithBool: YES], @"Equalizer",
68                 [NSNumber numberWithBool: YES], @"Track Rating",
69                 nil];
70 }
71
72 - (BOOL)showPrimaryInterface
73 {
74     return NO;
75 }
76
77 - (ITMTRemotePlayerRunningState)playerRunningState
78 {
79     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
80     int i;
81     int count = [apps count];
82     
83     for (i = 0; i < count; i++) {
84         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
85             return ITMTRemotePlayerRunning;
86         }
87     }
88     return ITMTRemotePlayerNotRunning;
89 }
90
91 - (ITMTRemotePlayerPlayingState)playerPlayingState
92 {
93     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
94     
95     switch (result)
96     {
97         default:
98         case 'kPSS':
99             return ITMTRemotePlayerStopped;
100         case 'kPSP':
101             return ITMTRemotePlayerPlaying;
102         case 'kPSp':
103             return ITMTRemotePlayerPaused;
104         case 'kPSR':
105             return ITMTRemotePlayerRewinding;
106         case 'kPSF':
107             return ITMTRemotePlayerForwarding;
108     }
109     
110     return ITMTRemotePlayerStopped;
111 }
112
113 - (NSArray *)playlists
114 {
115     long i = 0;
116     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
117     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
118     
119     for (i = 1; i <= numPlaylists; i++) {
120         const long j = i;
121         NSString *sendStr = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() } }",(unsigned long)j];
122         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
123         [playlists addObject:theObj];
124     }
125     return [playlists autorelease];
126 }
127
128 - (int)numberOfSongsInPlaylistAtIndex:(int)index
129 {
130     return [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:[NSString stringWithFormat:@"kocl:type('cTrk'), '----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:'null'() }",index] eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
131 }
132
133 - (ITMTRemotePlayerPlaylistClass)classOfPlaylistAtIndex:(int)index
134 {
135     int realResult = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
136     
137
138     switch (realResult)
139            {
140            case 'cLiP':
141                   return ITMTRemotePlayerLibraryPlaylist;
142                   break;
143            case 'cRTP':
144                   return ITMTRemotePlayerRadioPlaylist;
145                   break;
146            default:
147                   return ITMTRemotePlayerPlaylist;
148            }
149 }
150
151 - (int)currentPlaylistIndex
152 {
153     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
154 }
155
156 - (NSString *)songTitleAtIndex:(int)index
157 {
158     return [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }",index] eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
159 }
160
161 - (int)currentSongIndex
162 {
163     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
164 }
165
166 - (NSString *)currentSongTitle
167 {
168     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
169 }
170
171 - (NSString *)currentSongArtist
172 {
173     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
174 }
175
176 - (NSString *)currentSongAlbum
177 {
178     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
179 }
180
181 - (NSString *)currentSongGenre
182 {
183     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
184 }
185
186 - (NSString *)currentSongLength
187 {
188     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
189 }
190
191 - (NSString *)currentSongRemaining
192 {
193     long duration = [[ITAppleEventCenter sharedCenter]
194                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
195     long current = [[ITAppleEventCenter sharedCenter]
196                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
197
198     return [[NSNumber numberWithLong:duration - current] stringValue];
199 }
200
201 - (float)currentSongRating
202 {
203     return [[ITAppleEventCenter sharedCenter]
204                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN] / 100;
205 }
206
207 - (BOOL)setCurrentSongRating:(float)rating
208 {
209     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu) ----:obj { form:'prop', want:type('prop'), seld:type('pRte'), from:obj { form:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } }",(long)rating*100] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
210     return YES;
211 }
212
213 - (BOOL)equalizerEnabled
214 {
215     return [[ITAppleEventCenter sharedCenter]
216                         sendAEWithRequestedKeyForNumber:@"pEQ " eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
217 }
218
219 - (BOOL)setEqualizerEnabled:(BOOL)enabled
220 {
221 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu) ----:obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",enabled] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
222 }
223
224 - (NSArray *)eqPresets
225 {
226     int i;
227     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
228     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
229     
230     for (i = 1; i <= numPresets; i++) {
231         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pnam'), from:obj { form:'indx', want:type('cEQP'), seld:long(%lu), from:'null'() } }",i] eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
232         if (theObj) {
233             [presets addObject:theObj];
234         }
235     }
236     return [presets autorelease];
237 }
238
239 - (int)currentEQPresetIndex
240 {
241     int result;
242     result = [[ITAppleEventCenter sharedCenter]
243                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
244     return result;
245 }
246
247 - (float)volume
248 {
249     long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
250     return vol / 100;
251 }
252
253 - (BOOL)setVolume:(float)volume
254 {
255     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), ----:obj { form:'prop', want:type('prop'), seld:type('pVol'), from:'null'() }",(long)volume*100] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
256     return NO;
257 }
258
259 - (BOOL)shuffleEnabled
260 {
261     int result = [[ITAppleEventCenter sharedCenter]
262                 sendTwoTierAEWithRequestedKeyForNumber:@"pShf" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
263     return result;
264 }
265
266 - (BOOL)setShuffleEnabled:(BOOL)enabled
267 {
268     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[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'() } }",enabled] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
269 }
270
271 - (ITMTRemotePlayerRepeatMode)repeatMode
272 {
273     FourCharCode m00f;
274     int result;
275     m00f = [[ITAppleEventCenter sharedCenter]
276                 sendTwoTierAEWithRequestedKeyForNumber:@"pRpt" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
277
278     switch (m00f)
279            {
280            case 'kRp0':
281                   result = ITMTRemotePlayerRepeatOff;
282                   break;
283            case 'kRp1':
284                   result = ITMTRemotePlayerRepeatOne;
285                   break;
286            case 'kRpA':
287                   result = ITMTRemotePlayerRepeatAll;
288                   break;
289            }
290     
291     return result;
292 }
293
294 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
295 {
296     FourCharCode m00f;
297     switch (repeatMode)
298            {
299            case ITMTRemotePlayerRepeatOff:
300                   m00f = 'kRp0';
301                   break;
302            case ITMTRemotePlayerRepeatOne:
303                   m00f = 'kRp1';
304                   break;
305            case ITMTRemotePlayerRepeatAll:
306                   m00f = 'kRpA';
307                   break;
308            }
309
310     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu) ----:obj { form:'prop', want:type('pRpt'), seld:type('pShf'), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } }",m00f] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
311
312 }
313
314 - (BOOL)play
315 {
316     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
317     return YES;
318 }
319
320 - (BOOL)pause
321 {
322     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
323     return YES;
324 }
325
326 - (BOOL)goToNextSong
327 {
328     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
329     return YES;
330 }
331
332 - (BOOL)goToPreviousSong
333 {
334     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
335     return YES;
336 }
337
338 - (BOOL)forward
339 {
340     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
341     return YES;
342 }
343
344 - (BOOL)rewind
345 {
346     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
347     return YES;
348 }
349
350 - (BOOL)switchToPlaylistAtIndex:(int)index
351 {
352     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
353     return YES;
354 }
355
356 - (BOOL)switchToSongAtIndex:(int)index
357 {
358     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:() } }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
359     return YES;
360 }
361
362 - (BOOL)switchToEQAtIndex:(int)index
363 {
364     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:obj { form:'ID  ', want:type('cEQP'), seld:long(%lu), from:'null'() }, ----:obj { form:'prop', want:type('prop'), seld:type('pEQP'), from:'null'() }",index] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
365     return YES;
366 }
367
368 - (ProcessSerialNumber)iTunesPSN
369 {
370     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
371     ProcessSerialNumber number;
372     int i;
373     int count = [apps count];
374     
375     number.highLongOfPSN = kNoProcess;
376     
377     for (i = 0; i < count; i++)
378     {
379         NSDictionary *curApp = [apps objectAtIndex:i];
380         
381         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
382         {
383             number.highLongOfPSN = [[curApp objectForKey:
384                 @"NSApplicationProcessSerialNumberHigh"] intValue];
385             number.lowLongOfPSN = [[curApp objectForKey:
386                 @"NSApplicationProcessSerialNumberLow"] intValue];
387         }
388     }
389     return number;
390 }
391
392 - (void)applicationLaunched:(NSNotification *)note
393 {
394     NSDictionary *info = [note userInfo];
395
396     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
397         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
398         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
399
400         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
401     }
402 }
403
404 - (void)applicationTerminated:(NSNotification *)note
405 {
406     NSDictionary *info = [note userInfo];
407
408     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
409         iTunesPSN.highLongOfPSN = kNoProcess;
410         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
411     }
412 }
413
414 @end