Changed the player state names to the new ones. Increased refresh rate fire.
[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 *)title
11 {
12     return @"iTunes";
13 }
14
15 - (NSString *)information;
16 {
17     return @"Default MenuTunes plugin to control iTunes. Written by iThink Software.";
18 }
19
20 - (NSImage *)icon
21 {
22     return nil;
23 }
24
25 - (BOOL)begin
26 {
27     iTunesPSN = [self iTunesPSN];
28
29     //Register for application termination in NSWorkspace
30     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
31     [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(applicationTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
32
33     return YES;
34 }
35
36 - (BOOL)halt
37 {
38     iTunesPSN.highLongOfPSN = kNoProcess;
39
40     //Unregister for application termination in NSWorkspace
41     [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
42
43     return YES;
44 }
45
46 - (ITMTRemotePlayerRunningStatus)playerRunningStatus
47 {
48     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
49     int i;
50     int count = [apps count];
51
52     for (i = 0; i < count; i++) {
53         if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
54                 isEqualToString:@"iTunes"]) {
55             return ITMTRemotePlayerRunning;
56         }
57     }
58     return ITMTRemotePlayerNotRunning;
59 }
60
61 - (ITMTRemotePlayerState)playerState
62 {
63     long result = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"'----':obj { form:'prop', want:type('prop'), seld:type('pPlS'), from:'null'() }" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
64     
65     switch (result)
66     {
67         default:
68         case 'kPSS':
69             return ITMTRemotePlayerStopped;
70         case 'kPSP':
71             return ITMTRemotePlayerPlaying;
72         case 'kPSp':
73             return ITMTRemotePlayerPaused;
74         case 'kPSR':
75             return ITMTRemotePlayerRewinding;
76         case 'kPSF':
77             return ITMTRemotePlayerForwarding;
78     }
79     
80     return ITMTRemotePlayerStopped;
81 }
82
83 - (NSArray *)playlists
84 {
85     long i = 0;
86     const signed long numPlaylists = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cPly'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
87     NSMutableArray *playlists = [[NSMutableArray alloc] initWithCapacity:numPlaylists];
88
89
90            for (i = 1; i <= numPlaylists; i++) {
91                   const long j = i;
92                   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];
93                   NSString *theObj = [[ITAppleEventCenter sharedCenter] sendAEWithSendString:sendStr eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
94                   //NSLog(@"sent event cur %d max %d",i,numPlaylists);
95                   [playlists addObject:theObj];
96            }
97            return [playlists autorelease];
98 }
99
100 - (int)numberOfSongsInPlaylistAtIndex:(int)index
101 {
102     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];
103 }
104
105 - (NSString *)classOfPlaylistAtIndex:(int)index
106 {
107     int realResult = [[ITAppleEventCenter sharedCenter]
108                 sendTwoTierAEWithRequestedKeyForNumber:@"pcls" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
109
110     if (realResult == 'cRTP') return @"radio tuner playlist";
111     else return @"playlist";
112 }
113
114 - (int)currentPlaylistIndex
115 {
116     int result;
117     result = [[ITAppleEventCenter sharedCenter]
118                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
119     return result;
120 }
121
122 - (NSString *)songTitleAtIndex:(int)index
123 {
124     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];
125 }
126
127 - (int)currentSongIndex
128 {
129     int result;
130     result = [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKeyForNumber:@"pidx" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
131     return result;
132 }
133
134 - (NSString *)currentSongTitle
135 {
136     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pnam" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
137 }
138
139 - (NSString *)currentSongArtist
140 {
141     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pArt" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
142 }
143
144 - (NSString *)currentSongAlbum
145 {
146     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pAlb" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
147 }
148
149 - (NSString *)currentSongGenre
150 {
151     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pGen" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
152 }
153
154 - (NSString *)currentSongLength
155 {
156     return [[ITAppleEventCenter sharedCenter] sendTwoTierAEWithRequestedKey:@"pTim" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
157 }
158
159 - (NSString *)currentSongRemaining
160 {
161     long duration = [[ITAppleEventCenter sharedCenter]
162                         sendTwoTierAEWithRequestedKeyForNumber:@"pDur" fromObjectByKey:@"pTrk" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
163     long current = [[ITAppleEventCenter sharedCenter]
164                         sendAEWithRequestedKeyForNumber:@"pPos" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
165
166     return [[NSNumber numberWithLong:duration - current] stringValue];
167 }
168
169 - (float)currentSongRating
170 {
171     int realResult = [[ITAppleEventCenter sharedCenter]
172                 sendTwoTierAEWithRequestedKeyForNumber:@"pRte" fromObjectByKey:@"pPla" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
173
174     return realResult / 100;
175 }
176
177 - (BOOL)setCurrentSongRating:(float)rating
178 {
179     [[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];
180     return YES;
181 }
182
183 - (float)volume
184 {
185     long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
186     return vol / 100;
187 }
188
189 - (BOOL)setVolume:(float)volume
190 {
191     [[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];
192     return NO;
193 }
194
195 - (NSArray *)eqPresets;
196 {
197     int i;
198     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
199     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
200
201            for (i = 1; i <= numPresets; i++) {
202                   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];
203                   if (theObj) [presets addObject:theObj];
204            }
205            return [presets autorelease];
206 }
207
208 - (int)currentEQPresetIndex
209 {
210     int result;
211     result = [[ITAppleEventCenter sharedCenter]
212                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"appPSN:iTunesPSN];
213     return result;
214 }
215
216 - (BOOL)play
217 {
218     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
219     return YES;
220 }
221
222 - (BOOL)pause
223 {
224     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
225     return YES;
226 }
227
228 - (BOOL)goToNextSong
229 {
230     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
231     return YES;
232 }
233
234 - (BOOL)goToPreviousSong
235 {
236     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
237     return YES;
238 }
239
240 - (BOOL)fastForward
241 {
242     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
243     return YES;
244 }
245
246 - (BOOL)rewind
247 {
248     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
249     return YES;
250 }
251
252
253 - (BOOL)switchToPlaylistAtIndex:(int)index
254 {
255     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
256     return YES;
257 }
258
259 - (BOOL)switchToSongAtIndex:(int)index
260 {
261     [[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];
262     return YES;
263 }
264
265 - (BOOL)switchToEQAtIndex:(int)index
266 {
267     [[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];
268     return YES;
269 }
270
271 - (ProcessSerialNumber)iTunesPSN
272 {
273     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
274     ProcessSerialNumber number;
275     int i;
276     int count = [apps count];
277     
278     number.highLongOfPSN = kNoProcess;
279     
280     for (i = 0; i < count; i++)
281     {
282         NSDictionary *curApp = [apps objectAtIndex:i];
283         
284         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
285         {
286             number.highLongOfPSN = [[curApp objectForKey:
287                 @"NSApplicationProcessSerialNumberHigh"] intValue];
288             number.lowLongOfPSN = [[curApp objectForKey:
289                 @"NSApplicationProcessSerialNumberLow"] intValue];
290         }
291     }
292     return number;
293 }
294
295 - (void)applicationLaunched:(NSNotification *)note
296 {
297     NSDictionary *info = [note userInfo];
298
299     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
300         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
301         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
302
303         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
304     }
305 }
306
307 - (void)applicationTerminated:(NSNotification *)note
308 {
309     NSDictionary *info = [note userInfo];
310
311     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
312         iTunesPSN.highLongOfPSN = kNoProcess;
313         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
314     }
315 }
316
317 @end