did what I could to implement showPrimaryInterface... Alex, could you try
[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     // Make this into AppleEvents... shouldn't be too hard, I'm just too tired to do it right now.
75     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pisf'), from:'null'() }",YES] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
76     // Still have to convert these to AEs:
77     //  set visible of browser window 1 to true
78     //  set minimized of browser window 1 to false
79     //  set view of browser window 1 to (playlist (index of current playlist))
80     return NO;
81 }
82
83 - (ITMTRemotePlayerRunningState)playerRunningState
84 {
85     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
86     int i;
87     int count = [apps count];
88     
89     for (i = 0; i < count; i++) {
90         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
91             return ITMTRemotePlayerRunning;
92         }
93     }
94     return ITMTRemotePlayerNotRunning;
95 }
96
97 - (ITMTRemotePlayerPlayingState)playerPlayingState
98 {
99     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
100     
101     switch (result)
102     {
103         default:
104         case 'kPSS':
105             return ITMTRemotePlayerStopped;
106         case 'kPSP':
107             return ITMTRemotePlayerPlaying;
108         case 'kPSp':
109             return ITMTRemotePlayerPaused;
110         case 'kPSR':
111             return ITMTRemotePlayerRewinding;
112         case 'kPSF':
113             return ITMTRemotePlayerForwarding;
114     }
115     
116     return ITMTRemotePlayerStopped;
117 }
118
119 - (NSArray *)playlists
120 {
121     long i = 0;
122     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
123     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
124     
125     for (i = 1; i <= numPlaylists; i++) {
126         const long j = i;
127         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];
128         NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
129         [playlists addObject:theObj];
130     }
131     return [playlists autorelease];
132 }
133
134 - (int)numberOfSongsInPlaylistAtIndex:(int)index
135 {
136     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];
137 }
138
139 - (ITMTRemotePlayerPlaylistClass)classOfPlaylistAtIndex:(int)index
140 {
141     int realResult = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
142     
143
144     switch (realResult)
145            {
146            case 'cLiP':
147                   return ITMTRemotePlayerLibraryPlaylist;
148                   break;
149            case 'cRTP':
150                   return ITMTRemotePlayerRadioPlaylist;
151                   break;
152            default:
153                   return ITMTRemotePlayerPlaylist;
154            }
155 }
156
157 - (int)currentPlaylistIndex
158 {
159     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
160 }
161
162 - (NSString *)songTitleAtIndex:(int)index
163 {
164     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];
165 }
166
167 - (NSString *)currentSongUniqueIdentifier
168 {
169     return [NSString stringWithFormat:@"%i-%i", [self currentPlaylistIndex], [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pDID" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN]];
170 }
171
172 - (int)currentSongIndex
173 {
174     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
175 }
176
177 - (NSString *)currentSongTitle
178 {
179     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
180 }
181
182 - (NSString *)currentSongArtist
183 {
184     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
185 }
186
187 - (NSString *)currentSongAlbum
188 {
189     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
190 }
191
192 - (NSString *)currentSongGenre
193 {
194     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
195 }
196
197 - (NSString *)currentSongLength
198 {
199     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
200 }
201
202 - (NSString *)currentSongRemaining
203 {
204     long duration = [[ITAppleEventCenter sharedCenter]
205                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
206     long current = [[ITAppleEventCenter sharedCenter]
207                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
208
209     return [[NSNumber numberWithLong:duration - current] stringValue];
210 }
211
212 - (float)currentSongRating
213 {
214     return [[ITAppleEventCenter sharedCenter]
215                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN] / 100;
216 }
217
218 - (BOOL)setCurrentSongRating:(float)rating
219 {
220     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pRte'), from:obj { form:'indx', want:type('cTrk'), seld:long(%lu), from:obj { form:'prop', want:type('prop'), seld:type('pPla'), from:'null'() } } }",(long)(rating*100),[self currentSongIndex]] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
221     return YES;
222 }
223
224 - (BOOL)equalizerEnabled
225 {
226     int thingy = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:type('prop'), want:type('prop'), seld:type('pEQ '), from:() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
227     NSLog(@"%i", thingy);
228     return thingy;    
229 }
230
231 - (BOOL)setEqualizerEnabled:(BOOL)enabled
232 {
233 [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"data:long(%lu), '----':obj { form:'prop', want:type('prop'), seld:type('pEQ '), from:'null'() }",enabled] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
234 }
235
236 - (NSArray *)eqPresets
237 {
238     int i;
239     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
240     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
241     
242     for (i = 1; i <= numPresets; i++) {
243         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];
244         if (theObj) {
245             [presets addObject:theObj];
246         }
247     }
248     return [presets autorelease];
249 }
250
251 - (int)currentEQPresetIndex
252 {
253     int result;
254     result = [[ITAppleEventCenter sharedCenter]
255                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
256     return result;
257 }
258
259 - (float)volume
260 {
261     long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
262     return vol / 100;
263 }
264
265 - (BOOL)setVolume:(float)volume
266 {
267     [[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];
268     return NO;
269 }
270
271 - (BOOL)shuffleEnabled
272 {
273     int result = [[ITAppleEventCenter sharedCenter]
274                 sendTwoTierAEWithRequestedKeyForNumber:@"pShf" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
275     return result;
276 }
277
278 - (BOOL)setShuffleEnabled:(BOOL)enabled
279 {
280     [[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];
281 }
282
283 - (ITMTRemotePlayerRepeatMode)repeatMode
284 {
285     FourCharCode m00f;
286     int result;
287     m00f = [[ITAppleEventCenter sharedCenter]
288                 sendTwoTierAEWithRequestedKeyForNumber:@"pRpt" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
289
290     switch (m00f)
291            {
292            case 'kRp0':
293                   result = ITMTRemotePlayerRepeatOff;
294                   break;
295            case 'kRp1':
296                   result = ITMTRemotePlayerRepeatOne;
297                   break;
298            case 'kRpA':
299                   result = ITMTRemotePlayerRepeatAll;
300                   break;
301            }
302     
303     return result;
304 }
305
306 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
307 {
308     FourCharCode m00f;
309     switch (repeatMode)
310            {
311            case ITMTRemotePlayerRepeatOff:
312                   m00f = 'kRp0';
313                   break;
314            case ITMTRemotePlayerRepeatOne:
315                   m00f = 'kRp1';
316                   break;
317            case ITMTRemotePlayerRepeatAll:
318                   m00f = 'kRpA';
319                   break;
320            }
321
322     [[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];
323
324 }
325
326 - (BOOL)play
327 {
328     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
329     return YES;
330 }
331
332 - (BOOL)pause
333 {
334     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
335     return YES;
336 }
337
338 - (BOOL)goToNextSong
339 {
340     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
341     return YES;
342 }
343
344 - (BOOL)goToPreviousSong
345 {
346     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
347     return YES;
348 }
349
350 - (BOOL)forward
351 {
352     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
353     return YES;
354 }
355
356 - (BOOL)rewind
357 {
358     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
359     return YES;
360 }
361
362 - (BOOL)switchToPlaylistAtIndex:(int)index
363 {
364     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
365     return YES;
366 }
367
368 - (BOOL)switchToSongAtIndex:(int)index
369 {
370     [[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];
371     return YES;
372 }
373
374 - (BOOL)switchToEQAtIndex:(int)index
375 {
376     // index should count from 0, but itunes counts from 1, so let's add 1.
377     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('pEQP'), from:'null'() }, data:obj { form:'indx', want:type('cEQP'), seld:long(%lu), from:'null'() }",(index+1)] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
378     return YES;
379 }
380
381 - (ProcessSerialNumber)iTunesPSN
382 {
383     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
384     ProcessSerialNumber number;
385     int i;
386     int count = [apps count];
387     
388     number.highLongOfPSN = kNoProcess;
389     
390     for (i = 0; i < count; i++)
391     {
392         NSDictionary *curApp = [apps objectAtIndex:i];
393         
394         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
395         {
396             number.highLongOfPSN = [[curApp objectForKey:
397                 @"NSApplicationProcessSerialNumberHigh"] intValue];
398             number.lowLongOfPSN = [[curApp objectForKey:
399                 @"NSApplicationProcessSerialNumberLow"] intValue];
400         }
401     }
402     return number;
403 }
404
405 - (void)applicationLaunched:(NSNotification *)note
406 {
407     NSDictionary *info = [note userInfo];
408
409     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
410         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
411         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
412
413         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
414     }
415 }
416
417 - (void)applicationTerminated:(NSNotification *)note
418 {
419     NSDictionary *info = [note userInfo];
420
421     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
422         iTunesPSN.highLongOfPSN = kNoProcess;
423         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
424     }
425 }
426
427 @end