Added capabilities NSDictionary prototype, as well as a completed one for
[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 - (NSString *)classOfPlaylistAtIndex:(int)index
129 {
130     int realResult = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
131     
132     if (realResult == 'cRTP') {
133         return @"radio tuner playlist";
134     } else {
135         return @"playlist";
136     }
137 }
138
139 - (int)currentPlaylistIndex
140 {
141     int result;
142     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
143     return result;
144 }
145
146 - (NSString *)songTitleAtIndex:(int)index
147 {
148     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];
149 }
150
151 - (int)currentSongIndex
152 {
153     int result;
154     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
155     return result;
156 }
157
158 - (NSString *)currentSongTitle
159 {
160     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
161 }
162
163 - (NSString *)currentSongArtist
164 {
165     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
166 }
167
168 - (NSString *)currentSongAlbum
169 {
170     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
171 }
172
173 - (NSString *)currentSongGenre
174 {
175     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
176 }
177
178 - (NSString *)currentSongLength
179 {
180     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
181 }
182
183 - (NSString *)currentSongRemaining
184 {
185     long duration = [[ITAppleEventCenter sharedCenter]
186                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
187     long current = [[ITAppleEventCenter sharedCenter]
188                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
189
190     return [[NSNumber numberWithLong:duration - current] stringValue];
191 }
192
193 - (float)currentSongRating
194 {
195     int realResult = [[ITAppleEventCenter sharedCenter]
196                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
197
198     return realResult / 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:'prop', want:type('prop'), seld:type('pTrk'), from:'null'() } ",(long)rating*100] eventClass:@"core" eventID:@"setd" appPSN:iTunesPSN];
204     return YES;
205 }
206
207 - (BOOL)equalizerEnabled
208 {
209     return NO;
210 }
211
212 - (BOOL)setEqualizerEnabled:(BOOL)enabled
213 {
214     return NO;
215 }
216
217 - (NSArray *)eqPresets
218 {
219     int i;
220     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
221     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
222     
223     for (i = 1; i <= numPresets; i++) {
224         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];
225         if (theObj) {
226             [presets addObject:theObj];
227         }
228     }
229     return [presets autorelease];
230 }
231
232 - (int)currentEQPresetIndex
233 {
234     int result;
235     result = [[ITAppleEventCenter sharedCenter]
236                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
237     return result;
238 }
239
240 - (float)volume
241 {
242     long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
243     return vol / 100;
244 }
245
246 - (BOOL)setVolume:(float)volume
247 {
248     [[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];
249     return NO;
250 }
251
252 - (BOOL)shuffleEnabled
253 {
254     return NO;
255 }
256
257 - (BOOL)setShuffleEnabled:(BOOL)enabled
258 {
259     return NO;
260 }
261
262 - (ITMTRemotePlayerRepeatMode)repeatMode
263 {
264     return ITMTRemotePlayerRepeatOff;
265 }
266
267 - (BOOL)setRepeatMode:(ITMTRemotePlayerRepeatMode)repeatMode
268 {
269     return NO;
270 }
271
272 - (BOOL)play
273 {
274     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
275     return YES;
276 }
277
278 - (BOOL)pause
279 {
280     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
281     return YES;
282 }
283
284 - (BOOL)goToNextSong
285 {
286     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
287     return YES;
288 }
289
290 - (BOOL)goToPreviousSong
291 {
292     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
293     return YES;
294 }
295
296 - (BOOL)forward
297 {
298     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
299     return YES;
300 }
301
302 - (BOOL)rewind
303 {
304     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
305     return YES;
306 }
307
308 - (BOOL)switchToPlaylistAtIndex:(int)index
309 {
310     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
311     return YES;
312 }
313
314 - (BOOL)switchToSongAtIndex:(int)index
315 {
316     [[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];
317     return YES;
318 }
319
320 - (BOOL)switchToEQAtIndex:(int)index
321 {
322     [[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];
323     return YES;
324 }
325
326 - (ProcessSerialNumber)iTunesPSN
327 {
328     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
329     ProcessSerialNumber number;
330     int i;
331     int count = [apps count];
332     
333     number.highLongOfPSN = kNoProcess;
334     
335     for (i = 0; i < count; i++)
336     {
337         NSDictionary *curApp = [apps objectAtIndex:i];
338         
339         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
340         {
341             number.highLongOfPSN = [[curApp objectForKey:
342                 @"NSApplicationProcessSerialNumberHigh"] intValue];
343             number.lowLongOfPSN = [[curApp objectForKey:
344                 @"NSApplicationProcessSerialNumberLow"] intValue];
345         }
346     }
347     return number;
348 }
349
350 - (void)applicationLaunched:(NSNotification *)note
351 {
352     NSDictionary *info = [note userInfo];
353
354     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
355         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
356         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
357
358         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
359     }
360 }
361
362 - (void)applicationTerminated:(NSNotification *)note
363 {
364     NSDictionary *info = [note userInfo];
365
366     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
367         iTunesPSN.highLongOfPSN = kNoProcess;
368         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
369     }
370 }
371
372 @end