-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRemoteController.m
executable file
·241 lines (207 loc) · 8.83 KB
/
RemoteController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//
// RemoteController.m
// Remote
//
// Created by Alex on 06/05/2009.
// Copyright 2009 Alex Price.
//
#import "RemoteController.h"
#import <CoreAudio/CoreAudio.h>
@implementation RemoteController
+ (void)load
{
[RemoteController startRemoteControl];
}
+ (BOOL)startRemoteControl
{
HIDRemoteMode remoteMode;
HIDRemote *hidRemote;
hidRemote = [HIDRemote sharedHIDRemote];
remoteMode = kHIDRemoteModeExclusive;
// Check whether the installation of Candelair is required to reliably operate in this mode
if ([HIDRemote isCandelairInstallationRequiredForRemoteMode:remoteMode])
{
// Reliable usage of the remote in this mode under this operating system version
// requires the Candelair driver to be installed. Let's inform the user about it.
NSAlert *alert;
if ((alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Candelair driver installation necessary", @"")
defaultButton:NSLocalizedString(@"Download", @"")
alternateButton:NSLocalizedString(@"More information", @"")
otherButton:NSLocalizedString(@"Cancel", @"")
informativeTextWithFormat:NSLocalizedString(@"An additional driver needs to be installed before %@ can reliably access the remote under the OS version installed on your computer.", @""), [[NSBundle mainBundle] objectForInfoDictionaryKey:(id)kCFBundleNameKey]]) != nil)
{
switch ([alert runModal])
{
case NSAlertDefaultReturn:
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.candelair.com/download/"]];
break;
case NSAlertAlternateReturn:
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.candelair.com/"]];
break;
}
}
}
else
{
[[HIDRemote sharedHIDRemote] setUnusedButtonCodes:[NSArray arrayWithObjects:
/*[NSNumber numberWithInt:(int)kHIDRemoteButtonCodeMenuHold],*/
[NSNumber numberWithInt:(int)kHIDRemoteButtonCodePlayHold],
[NSNumber numberWithInt:(int)kHIDRemoteButtonCodeRightHold],
[NSNumber numberWithInt:(int)kHIDRemoteButtonCodeLeftHold],
[NSNumber numberWithInt:(int)kHIDRemoteButtonCodeCenterHold],
nil]
];
[hidRemote setExclusiveLockLendingEnabled:YES];
// Start remote control
if ([hidRemote startRemoteControl:remoteMode])
{
// Start was successful
NSLog(@"HIDRemote started successfully");
[[HIDRemote sharedHIDRemote] setDelegate:self];
return (YES);
}
else
{
// Start failed
NSLog(@"Couldn't start HIDRemote");
}
}
return (NO);
}
- (BOOL)hidRemote:(HIDRemote *)hidRemote lendExclusiveLockToApplicationWithInfo:(NSDictionary *)applicationInfo
{
NSNumber *remoteModeNumber;
if ((remoteModeNumber = [applicationInfo objectForKey:kHIDRemoteDNStatusModeKey]) != nil)
{
switch ((HIDRemoteMode)[remoteModeNumber intValue])
{
// Lend exclusive lock to all applications operating in shared or
// exclusive-auto mode
case kHIDRemoteModeShared:
case kHIDRemoteModeExclusiveAuto:
return (YES);
break;
default:
break;
}
}
// Don't lend the lock to applications operating in other modes
return (NO);
}
- (void)hidRemote:(HIDRemote *)hidRemote eventWithButton:(HIDRemoteButtonCode)buttonCode isPressed:(BOOL)isPressed fromHardwareWithAttributes:(NSMutableDictionary *)attributes
{
[RemoteController hidRemote:hidRemote eventWithButton:buttonCode isPressed:isPressed fromHardwareWithAttributes:attributes];
}
+ (void)hidRemote:(HIDRemote *)hidRemote eventWithButton:(HIDRemoteButtonCode)buttonCode isPressed:(BOOL)isPressed fromHardwareWithAttributes:(NSMutableDictionary *)attributes
{
NSAppleScript *script;
NSLog(@"Remote button pressed");
if(!isPressed){
switch (buttonCode)
{
case kHIDRemoteButtonCodeUp:
[RemoteController volumeUp];
script = [[NSAppleScript alloc] initWithSource:
@"tell application \"Spotify\"\n\
if sound volume > 95 then\n\
set the sound volume to 100\n\
else\n\
set the sound volume to sound volume + 5\n\
end if\n\
end tell\n\
"];
[script performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[script release];
break;
case kHIDRemoteButtonCodeUpHold:
script = [[NSAppleScript alloc] initWithSource:
@"tell application \"Spotify\"\n\
set the sound volume to 80\n\
end tell\n\
"];
[script performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[script release];
break;
case kHIDRemoteButtonCodeDown:
script = [[NSAppleScript alloc] initWithSource:
@"tell application \"Spotify\"\n\
if sound volume < 5 then\n\
set the sound volume to 0\n\
else\n\
set the sound volume to sound volume - 5\n\
end if\n\
end tell\n\
"];
[script performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[script release];
break;
case kHIDRemoteButtonCodeDownHold:
script = [[NSAppleScript alloc] initWithSource:
@"tell application \"Spotify\"\n\
set the sound volume to 20\n\
end tell\n\
"];
[script performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[script release];
break;
case kHIDRemoteButtonCodeLeft:
script = [[NSAppleScript alloc] initWithSource:@"tell application \"Spotify\" to previous track"];
[script performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[script release];
break;
case kHIDRemoteButtonCodeLeftHold:
break;
case kHIDRemoteButtonCodeRight:
script = [[NSAppleScript alloc] initWithSource:@"tell application \"Spotify\" to next track"];
[script performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[script release];
break;
case kHIDRemoteButtonCodeRightHold:
break;
case kHIDRemoteButtonCodeCenter:
script = [[NSAppleScript alloc] initWithSource:@"tell application \"Spotify\" to playpause"];
[script performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[script release];
break;
case kHIDRemoteButtonCodeCenterHold:
break;
case kHIDRemoteButtonCodeMenu:
break;
case kHIDRemoteButtonCodeMenuHold:
script = [[NSAppleScript alloc] initWithSource:@"tell application \"Spotify\" to quit"];
[script performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[script release];
break;
case kHIDRemoteButtonCodePlay:
script = [[NSAppleScript alloc] initWithSource:@"tell application \"Spotify\" to playpause"];
[script performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[script release];
break;
case kHIDRemoteButtonCodePlayHold:
break;
default:
break;
}
}
}
+(void)playVolumeSound
{
NSSound *vol = [[NSSound alloc] initWithContentsOfFile:@"/System/Library/LoginPlugins/BezelServices.loginPlugin/Contents/Resources/volume.aiff" byReference:YES];
[vol play];
[vol autorelease];
}
+(void)volumeUp
{
NSAppleScript *as = [[NSAppleScript alloc] initWithSource:@"\nset curVolume to output volume of (get volume settings)\nif curVolume < 90 then\nset newVolume to curVolume + 10\nelse\nset newVolume to 100\nend if\nset volume output volume newVolume\nset volume output muted false\n"];
[as performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[as release];
//[RemoteController playVolumeSound];
}
+(void)volumeDown
{
NSAppleScript *as = [[NSAppleScript alloc] initWithSource:@"\nset curVolume to output volume of (get volume settings)\nif curVolume > 10 then\nset newVolume to curVolume - 10\nset volume output volume newVolume\nelse\nset newVolume to 1\nset volume output volume newVolume\nset volume output muted true\nend if\n"];
[as performSelectorOnMainThread:@selector(executeAndReturnError:) withObject:nil waitUntilDone:YES];
[as release];
//[RemoteController playVolumeSound];
}
@end