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