]> git.sesse.net Git - vlc/blob - modules/gui/macosx/AppleRemote.m
Adds support for Apple's remote (only handles Play/Pause at the moment).
[vlc] / modules / gui / macosx / AppleRemote.m
1 //
2 //  AppleRemote.m
3 //  AppleRemote
4 //
5 //  Created by Martin Kahr on 11.03.06.
6 //  Copyright 2006 martinkahr.com. All rights reserved.
7 //
8
9 #import "AppleRemote.h"
10
11 const char* AppleRemoteDeviceName = "AppleIRController";
12 const int REMOTE_SWITCH_COOKIE=19;
13
14 @implementation AppleRemote
15
16 #pragma public interface
17
18 - (id) init {   
19         if ( self = [super init] ) {
20                 openInExclusiveMode = YES;
21                 queue = NULL;
22                 hidDeviceInterface = NULL;
23                 cookieToButtonMapping = [[NSMutableDictionary alloc] init];
24                 
25                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Plus]      forKey:@"14_12_11_6_5_"];
26                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Minus] forKey:@"14_13_11_6_5_"];           
27                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu]                     forKey:@"14_7_6_5_14_7_6_5_"];          
28                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay]                     forKey:@"14_8_6_5_14_8_6_5_"];
29                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight]            forKey:@"14_9_6_5_14_9_6_5_"];
30                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft]                     forKey:@"14_10_6_5_14_10_6_5_"];
31                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold]       forKey:@"14_6_5_4_2_"];
32                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold]        forKey:@"14_6_5_3_2_"];
33                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold]        forKey:@"14_6_5_14_6_5_"];
34                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Sleep]       forKey:@"18_14_6_5_18_14_6_5_"];
35                 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]       forKey:@"19_"];         
36         }
37         
38         return self;
39 }
40
41 - (void) dealloc {
42         [self stopListening:self];
43         [cookieToButtonMapping release];
44         [super dealloc];
45 }
46
47 - (void) setRemoteId: (int) value {
48         remoteId = value;
49 }
50 - (int) remoteId {
51         return remoteId;
52 }
53
54 - (BOOL) isRemoteAvailable {    
55         io_object_t hidDevice = [self findAppleRemoteDevice];
56         if (hidDevice != 0) {
57                 IOObjectRelease(hidDevice);
58                 return YES;
59         } else {
60                 return NO;              
61         }
62 }
63
64 - (BOOL) isListeningToRemote {
65         return (hidDeviceInterface != NULL && allCookies != NULL && queue != NULL);     
66 }
67
68 - (void) setListeningToRemote: (BOOL) value {
69         if (value == NO) {
70                 [self stopListening:self];
71         } else {
72                 [self startListening:self];
73         }
74 }
75
76 - (void) setDelegate: (id) _delegate {
77         if ([_delegate respondsToSelector:@selector(appleRemoteButton:pressedDown:)]==NO) return;
78         
79         [_delegate retain];
80         [delegate release];
81         delegate = _delegate;
82 }
83 - (id) delegate {
84         return delegate;
85 }
86
87 - (BOOL) isOpenInExclusiveMode {
88         return openInExclusiveMode;
89 }
90 - (void) setOpenInExclusiveMode: (BOOL) value {
91         openInExclusiveMode = value;
92 }
93
94 - (IBAction) startListening: (id) sender {      
95         if ([self isListeningToRemote]) return;
96         
97         io_object_t hidDevice = [self findAppleRemoteDevice];
98         if (hidDevice == 0) return;
99         
100         if ([self createInterfaceForDevice:hidDevice] == NULL) {
101                 goto error;
102         }
103         
104         if ([self initializeCookies]==NO) {
105                 goto error;
106         }
107
108         if ([self openDevice]==NO) {
109                 goto error;
110         }
111         goto cleanup;
112         
113 error:
114         [self stopListening:self];
115         
116 cleanup:        
117         IOObjectRelease(hidDevice);
118 }
119
120 - (IBAction) stopListening: (id) sender {
121         if (queue != NULL) {
122                 (*queue)->stop(queue);          
123                 
124                 //dispose of queue
125                 (*queue)->dispose(queue);               
126                 
127                 //release the queue we allocated
128                 (*queue)->Release(queue);       
129                 
130                 queue = NULL;
131         }
132         
133         if (allCookies != nil) {
134                 [allCookies autorelease];
135                 allCookies = nil;
136         }
137         
138         if (hidDeviceInterface != NULL) {
139                 //close the device
140                 (*hidDeviceInterface)->close(hidDeviceInterface);
141                 
142                 //release the interface 
143                 (*hidDeviceInterface)->Release(hidDeviceInterface);
144                 
145                 hidDeviceInterface = NULL;              
146         }       
147 }
148
149 @end
150
151 @implementation AppleRemote (Singleton) 
152
153 static AppleRemote* sharedInstance=nil;
154
155 + (AppleRemote*) sharedRemote { 
156         @synchronized(self) {
157         if (sharedInstance == nil) {
158             sharedInstance = [[self alloc] init];
159         }
160     }
161         return sharedInstance;
162 }
163 + (id)allocWithZone:(NSZone *)zone {
164     @synchronized(self) {
165         if (sharedInstance == nil) {
166             return [super allocWithZone:zone];
167         }
168     }   
169     return sharedInstance;
170 }
171 - (id)copyWithZone:(NSZone *)zone {
172     return self;
173 }
174 - (id)retain {
175     return self;
176 }
177 - (unsigned)retainCount {
178     return UINT_MAX;  //denotes an object that cannot be released
179 }
180 - (void)release {
181     //do nothing
182 }
183 - (id)autorelease {
184     return self;
185 }
186
187 @end
188
189 @implementation AppleRemote (PrivateMethods) 
190
191 - (IOHIDQueueInterface**) queue {
192         return queue;
193 }
194
195 - (IOHIDDeviceInterface**) hidDeviceInterface {
196         return hidDeviceInterface;
197 }
198
199
200 - (NSDictionary*) cookieToButtonMapping {
201         return cookieToButtonMapping;
202 }
203
204 - (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues {
205         NSNumber* buttonId = [[self cookieToButtonMapping] objectForKey: cookieString];
206         if (buttonId != nil) {
207                 if (delegate) {         
208                         [delegate appleRemoteButton:[buttonId intValue] pressedDown: (sumOfValues>0)];
209                 }               
210         } else {
211                 NSLog(@"Unknown button for cookiestring %@", cookieString);
212         }
213 }
214
215 @end
216
217 /*      Callback method for the device queue
218 Will be called for any event of any type (cookie) to which we subscribe
219 */
220 static void QueueCallbackFunction(void* target,  IOReturn result, void* refcon, void* sender) { 
221         AppleRemote* remote = (AppleRemote*)target;
222         
223         IOHIDEventStruct event; 
224         AbsoluteTime     zeroTime = {0,0};
225         NSMutableString* cookieString = [NSMutableString string];
226         SInt32                   sumOfValues = 0;
227         while (result == kIOReturnSuccess)
228         {
229                 result = (*[remote queue])->getNextEvent([remote queue], &event, zeroTime, 0);          
230                 if ( result != kIOReturnSuccess )
231                         continue;
232                 
233                 if (REMOTE_SWITCH_COOKIE == (int)event.elementCookie) {
234                         [remote setRemoteId: event.value];
235                         [remote handleEventWithCookieString: @"19_" sumOfValues: 0];
236                 } else {
237                         sumOfValues+=event.value;
238                         [cookieString appendString:[NSString stringWithFormat:@"%d_", event.elementCookie]];                                    
239                 }
240                 
241                 //printf("%d %d %d\n", event.elementCookie, event.value, event.longValue);              
242         }
243         
244         [remote handleEventWithCookieString: cookieString sumOfValues: sumOfValues];    
245         
246 }
247
248 @implementation AppleRemote (IOKitMethods)
249
250 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice {
251         io_name_t                               className;
252         IOCFPlugInInterface**   plugInInterface = NULL;
253         HRESULT                                 plugInResult = S_OK;
254         SInt32                                  score = 0;
255         IOReturn                                ioReturnValue = kIOReturnSuccess;
256         
257         hidDeviceInterface = NULL;
258         
259         ioReturnValue = IOObjectGetClass(hidDevice, className);
260         
261         if (ioReturnValue != kIOReturnSuccess) {
262                 NSLog(@"Error: Failed to get class name.");
263                 return NULL;
264         }
265         
266         ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice,
267                                                                                                           kIOHIDDeviceUserClientTypeID,
268                                                                                                           kIOCFPlugInInterfaceID,
269                                                                                                           &plugInInterface,
270                                                                                                           &score);
271         if (ioReturnValue == kIOReturnSuccess)
272         {
273                 //Call a method of the intermediate plug-in to create the device interface
274                 plugInResult = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &hidDeviceInterface);
275                 
276                 if (plugInResult != S_OK) {
277                         NSLog(@"Error: Couldn't create HID class device interface");
278                 }
279                 // Release
280                 if (plugInInterface) (*plugInInterface)->Release(plugInInterface);
281         }
282         return hidDeviceInterface;
283 }
284
285 - (io_object_t) findAppleRemoteDevice {
286         CFMutableDictionaryRef hidMatchDictionary = NULL;
287         IOReturn ioReturnValue = kIOReturnSuccess;      
288         io_iterator_t hidObjectIterator = 0;
289         io_object_t     hidDevice = 0;
290         
291         // Set up a matching dictionary to search the I/O Registry by class
292         // name for all HID class devices
293         hidMatchDictionary = IOServiceMatching(AppleRemoteDeviceName);
294         
295         // Now search I/O Registry for matching devices.
296         ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
297         
298         if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0)) {
299                 hidDevice = IOIteratorNext(hidObjectIterator);
300         }
301         
302         // release the iterator
303         IOObjectRelease(hidObjectIterator);
304         
305         return hidDevice;
306 }
307
308 - (BOOL) initializeCookies {
309         IOHIDDeviceInterface122** handle = (IOHIDDeviceInterface122**)hidDeviceInterface;
310         IOHIDElementCookie              cookie;
311         long                                    usage;
312         long                                    usagePage;
313         id                                              object;
314         NSArray*                                elements = nil;
315         NSDictionary*                   element;
316         IOReturn success;
317         
318         if (!handle || !(*handle)) return NO;
319         
320         // Copy all elements, since we're grabbing most of the elements
321         // for this device anyway, and thus, it's faster to iterate them
322         // ourselves. When grabbing only one or two elements, a matching
323         // dictionary should be passed in here instead of NULL.
324         success = (*handle)->copyMatchingElements(handle, NULL, (CFArrayRef*)&elements);
325         
326         if (success == kIOReturnSuccess) {
327                 
328                 [elements autorelease];         
329                 /*
330                 cookies = calloc(NUMBER_OF_APPLE_REMOTE_ACTIONS, sizeof(IOHIDElementCookie)); 
331                 memset(cookies, 0, sizeof(IOHIDElementCookie) * NUMBER_OF_APPLE_REMOTE_ACTIONS);
332                 */
333                 allCookies = [[NSMutableArray alloc] init];
334                 int i;
335                 for (i=0; i< [elements count]; i++) {
336                         element = [elements objectAtIndex:i];
337                                                 
338                         //Get cookie
339                         object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementCookieKey) ];
340                         if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
341                         if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) continue;
342                         cookie = (IOHIDElementCookie) [object longValue];
343                         
344                         //Get usage
345                         object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsageKey) ];
346                         if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;                        
347                         usage = [object longValue];
348                         
349                         //Get usage page
350                         object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsagePageKey) ];
351                         if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;                        
352                         usagePage = [object longValue];
353
354                         [allCookies addObject: [NSNumber numberWithInt:(int)cookie]];
355                 }
356         } else {
357                 return NO;
358         }
359         
360         return YES;
361 }
362
363 - (BOOL) openDevice {
364         HRESULT  result;
365         
366         IOHIDOptionsType openMode = kIOHIDOptionsTypeNone;
367         if ([self isOpenInExclusiveMode]) openMode = kIOHIDOptionsTypeSeizeDevice;      
368         IOReturn ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, openMode);     
369         
370         if (ioReturnValue == KERN_SUCCESS) {
371                 queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
372                 if (queue) {
373                         result = (*queue)->create(queue, 0, 12);        //depth: maximum number of elements in queue before oldest elements in queue begin to be lost.
374
375                         int i=0;
376                         for(i=0; i<[allCookies count]; i++) {
377                                 IOHIDElementCookie cookie = (IOHIDElementCookie)[[allCookies objectAtIndex:i] intValue];
378                                 (*queue)->addElement(queue, cookie, 0);
379                         }
380                                                                           
381                         // add callback for async events
382                         CFRunLoopSourceRef eventSource;
383                         ioReturnValue = (*queue)->createAsyncEventSource(queue, &eventSource);
384                         if (ioReturnValue == KERN_SUCCESS) {
385                                 ioReturnValue = (*queue)->setEventCallout(queue,QueueCallbackFunction, self, NULL);
386                                 if (ioReturnValue == KERN_SUCCESS) {
387                                         CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);                                  
388                                         //start data delivery to queue
389                                         (*queue)->start(queue); 
390                                         return YES;
391                                 } else {
392                                         NSLog(@"Error when setting event callout");
393                                 }
394                         } else {
395                                 NSLog(@"Error when creating async event source");
396                         }
397                 } else {
398                         NSLog(@"Error when opening device");
399                 }
400         }
401         return NO;                              
402 }
403
404 @end