Adding methods for Equalizer enabled nonsense to the iTunes remote,
[MenuTunes.git] / iTunesRemote.m
1 op#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:@"pTrk" 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 - (BOOL)equalizerEnabled
184 {
185     return NO;
186 }
187
188 - (BOOL)setEqualizerEnabled:(BOOL)enabled
189 {
190     return NO;
191 }
192
193 - (float)volume
194 {
195     long vol = [[ITAppleEventCenter sharedCenter] sendAEWithRequestedKeyForNumber:@"pVol" eventClass:@"core" eventID:@"getd" appPSN:iTunesPSN];
196     return vol / 100;
197 }
198
199 - (BOOL)setVolume:(float)volume
200 {
201     [[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];
202     return NO;
203 }
204
205 - (NSArray *)eqPresets;
206 {
207     int i;
208     long numPresets = [[ITAppleEventCenter sharedCenter] sendAEWithSendStringForNumber:@"kocl:type('cEQP'), '----':(), &subj:()" eventClass:@"core" eventID:@"cnte" appPSN:iTunesPSN];
209     NSMutableArray *presets = [[NSMutableArray alloc] initWithCapacity:numPresets];
210
211            for (i = 1; i <= numPresets; i++) {
212                   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];
213                   if (theObj) [presets addObject:theObj];
214            }
215            return [presets autorelease];
216 }
217
218 - (int)currentEQPresetIndex
219 {
220     int result;
221     result = [[ITAppleEventCenter sharedCenter]
222                 sendTwoTierAEWithRequestedKeyForNumber:@"pidx"fromObjectByKey:@"pEQP" eventClass:@"core" eventID:@"getd"appPSN:iTunesPSN];
223     return result;
224 }
225
226 - (BOOL)play
227 {
228     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
229     return YES;
230 }
231
232 - (BOOL)pause
233 {
234     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Paus" appPSN:iTunesPSN];
235     return YES;
236 }
237
238 - (BOOL)goToNextSong
239 {
240     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Next" appPSN:iTunesPSN];
241     return YES;
242 }
243
244 - (BOOL)goToPreviousSong
245 {
246     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Prev" appPSN:iTunesPSN];
247     return YES;
248 }
249
250 - (BOOL)fastForward
251 {
252     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Fast" appPSN:iTunesPSN];
253     return YES;
254 }
255
256 - (BOOL)rewind
257 {
258     [[ITAppleEventCenter sharedCenter] sendAEWithEventClass:@"hook" eventID:@"Rwnd" appPSN:iTunesPSN];
259     return YES;
260 }
261
262
263 - (BOOL)switchToPlaylistAtIndex:(int)index
264 {
265     [[ITAppleEventCenter sharedCenter] sendAEWithSendString:[NSString stringWithFormat:@"'----':obj { form:'indx', want:type('cPly'), seld:long(%lu), from:() }",index] eventClass:@"hook" eventID:@"Play" appPSN:iTunesPSN];
266     return YES;
267 }
268
269 - (BOOL)switchToSongAtIndex:(int)index
270 {
271     [[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];
272     return YES;
273 }
274
275 - (BOOL)switchToEQAtIndex:(int)index
276 {
277     [[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];
278     return YES;
279 }
280
281 - (ProcessSerialNumber)iTunesPSN
282 {
283     NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
284     ProcessSerialNumber number;
285     int i;
286     int count = [apps count];
287     
288     number.highLongOfPSN = kNoProcess;
289     
290     for (i = 0; i < count; i++)
291     {
292         NSDictionary *curApp = [apps objectAtIndex:i];
293         
294         if ([[curApp objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"])
295         {
296             number.highLongOfPSN = [[curApp objectForKey:
297                 @"NSApplicationProcessSerialNumberHigh"] intValue];
298             number.lowLongOfPSN = [[curApp objectForKey:
299                 @"NSApplicationProcessSerialNumberLow"] intValue];
300         }
301     }
302     return number;
303 }
304
305 - (void)applicationLaunched:(NSNotification *)note
306 {
307     NSDictionary *info = [note userInfo];
308
309     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
310         iTunesPSN.highLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
311         iTunesPSN.lowLongOfPSN = [[info objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
312
313         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidLaunchNotification" object:nil];
314     }
315 }
316
317 - (void)applicationTerminated:(NSNotification *)note
318 {
319     NSDictionary *info = [note userInfo];
320
321     if ([[info objectForKey:@"NSApplicationName"] isEqualToString:@"iTunes"]) {
322         iTunesPSN.highLongOfPSN = kNoProcess;
323         [[NSNotificationCenter defaultCenter] postNotificationName:@"ITMTRemoteAppDidTerminateNotification" object:nil];
324     }
325 }
326
327 @end