5 // Created by Martin Kahr on 11.03.06.
6 // Copyright 2006 martinkahr.com. All rights reserved.
9 #import "AppleRemote.h"
11 const char* AppleRemoteDeviceName = "AppleIRController";
12 const int REMOTE_SWITCH_COOKIE=19;
14 @implementation AppleRemote
16 #pragma public interface
19 if ( self = [super init] ) {
20 openInExclusiveMode = YES;
22 hidDeviceInterface = NULL;
23 cookieToButtonMapping = [[NSMutableDictionary alloc] init];
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_"];
42 [self stopListening:self];
43 [cookieToButtonMapping release];
47 - (void) setRemoteId: (int) value {
54 - (BOOL) isRemoteAvailable {
55 io_object_t hidDevice = [self findAppleRemoteDevice];
57 IOObjectRelease(hidDevice);
64 - (BOOL) isListeningToRemote {
65 return (hidDeviceInterface != NULL && allCookies != NULL && queue != NULL);
68 - (void) setListeningToRemote: (BOOL) value {
70 [self stopListening:self];
72 [self startListening:self];
76 - (void) setDelegate: (id) _delegate {
77 if ([_delegate respondsToSelector:@selector(appleRemoteButton:pressedDown:)]==NO) return;
87 - (BOOL) isOpenInExclusiveMode {
88 return openInExclusiveMode;
90 - (void) setOpenInExclusiveMode: (BOOL) value {
91 openInExclusiveMode = value;
94 - (IBAction) startListening: (id) sender {
95 if ([self isListeningToRemote]) return;
97 io_object_t hidDevice = [self findAppleRemoteDevice];
98 if (hidDevice == 0) return;
100 if ([self createInterfaceForDevice:hidDevice] == NULL) {
104 if ([self initializeCookies]==NO) {
108 if ([self openDevice]==NO) {
114 [self stopListening:self];
117 IOObjectRelease(hidDevice);
120 - (IBAction) stopListening: (id) sender {
122 (*queue)->stop(queue);
125 (*queue)->dispose(queue);
127 //release the queue we allocated
128 (*queue)->Release(queue);
133 if (allCookies != nil) {
134 [allCookies autorelease];
138 if (hidDeviceInterface != NULL) {
140 (*hidDeviceInterface)->close(hidDeviceInterface);
142 //release the interface
143 (*hidDeviceInterface)->Release(hidDeviceInterface);
145 hidDeviceInterface = NULL;
151 @implementation AppleRemote (Singleton)
153 static AppleRemote* sharedInstance=nil;
155 + (AppleRemote*) sharedRemote {
156 @synchronized(self) {
157 if (sharedInstance == nil) {
158 sharedInstance = [[self alloc] init];
161 return sharedInstance;
163 + (id)allocWithZone:(NSZone *)zone {
164 @synchronized(self) {
165 if (sharedInstance == nil) {
166 return [super allocWithZone:zone];
169 return sharedInstance;
171 - (id)copyWithZone:(NSZone *)zone {
177 - (unsigned)retainCount {
178 return UINT_MAX; //denotes an object that cannot be released
189 @implementation AppleRemote (PrivateMethods)
191 - (IOHIDQueueInterface**) queue {
195 - (IOHIDDeviceInterface**) hidDeviceInterface {
196 return hidDeviceInterface;
200 - (NSDictionary*) cookieToButtonMapping {
201 return cookieToButtonMapping;
204 - (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues {
205 NSNumber* buttonId = [[self cookieToButtonMapping] objectForKey: cookieString];
206 if (buttonId != nil) {
208 [delegate appleRemoteButton:[buttonId intValue] pressedDown: (sumOfValues>0)];
211 NSLog(@"Unknown button for cookiestring %@", cookieString);
217 /* Callback method for the device queue
218 Will be called for any event of any type (cookie) to which we subscribe
220 static void QueueCallbackFunction(void* target, IOReturn result, void* refcon, void* sender) {
221 AppleRemote* remote = (AppleRemote*)target;
223 IOHIDEventStruct event;
224 AbsoluteTime zeroTime = {0,0};
225 NSMutableString* cookieString = [NSMutableString string];
226 SInt32 sumOfValues = 0;
227 while (result == kIOReturnSuccess)
229 result = (*[remote queue])->getNextEvent([remote queue], &event, zeroTime, 0);
230 if ( result != kIOReturnSuccess )
233 if (REMOTE_SWITCH_COOKIE == (int)event.elementCookie) {
234 [remote setRemoteId: event.value];
235 [remote handleEventWithCookieString: @"19_" sumOfValues: 0];
237 sumOfValues+=event.value;
238 [cookieString appendString:[NSString stringWithFormat:@"%d_", event.elementCookie]];
241 //printf("%d %d %d\n", event.elementCookie, event.value, event.longValue);
244 [remote handleEventWithCookieString: cookieString sumOfValues: sumOfValues];
248 @implementation AppleRemote (IOKitMethods)
250 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice {
252 IOCFPlugInInterface** plugInInterface = NULL;
253 HRESULT plugInResult = S_OK;
255 IOReturn ioReturnValue = kIOReturnSuccess;
257 hidDeviceInterface = NULL;
259 ioReturnValue = IOObjectGetClass(hidDevice, className);
261 if (ioReturnValue != kIOReturnSuccess) {
262 NSLog(@"Error: Failed to get class name.");
266 ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice,
267 kIOHIDDeviceUserClientTypeID,
268 kIOCFPlugInInterfaceID,
271 if (ioReturnValue == kIOReturnSuccess)
273 //Call a method of the intermediate plug-in to create the device interface
274 plugInResult = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &hidDeviceInterface);
276 if (plugInResult != S_OK) {
277 NSLog(@"Error: Couldn't create HID class device interface");
280 if (plugInInterface) (*plugInInterface)->Release(plugInInterface);
282 return hidDeviceInterface;
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;
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);
295 // Now search I/O Registry for matching devices.
296 ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
298 if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0)) {
299 hidDevice = IOIteratorNext(hidObjectIterator);
302 // release the iterator
303 IOObjectRelease(hidObjectIterator);
308 - (BOOL) initializeCookies {
309 IOHIDDeviceInterface122** handle = (IOHIDDeviceInterface122**)hidDeviceInterface;
310 IOHIDElementCookie cookie;
314 NSArray* elements = nil;
315 NSDictionary* element;
318 if (!handle || !(*handle)) return NO;
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);
326 if (success == kIOReturnSuccess) {
328 [elements autorelease];
330 cookies = calloc(NUMBER_OF_APPLE_REMOTE_ACTIONS, sizeof(IOHIDElementCookie));
331 memset(cookies, 0, sizeof(IOHIDElementCookie) * NUMBER_OF_APPLE_REMOTE_ACTIONS);
333 allCookies = [[NSMutableArray alloc] init];
335 for (i=0; i< [elements count]; i++) {
336 element = [elements objectAtIndex:i];
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];
345 object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsageKey) ];
346 if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
347 usage = [object longValue];
350 object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsagePageKey) ];
351 if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
352 usagePage = [object longValue];
354 [allCookies addObject: [NSNumber numberWithInt:(int)cookie]];
363 - (BOOL) openDevice {
366 IOHIDOptionsType openMode = kIOHIDOptionsTypeNone;
367 if ([self isOpenInExclusiveMode]) openMode = kIOHIDOptionsTypeSeizeDevice;
368 IOReturn ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, openMode);
370 if (ioReturnValue == KERN_SUCCESS) {
371 queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
373 result = (*queue)->create(queue, 0, 12); //depth: maximum number of elements in queue before oldest elements in queue begin to be lost.
376 for(i=0; i<[allCookies count]; i++) {
377 IOHIDElementCookie cookie = (IOHIDElementCookie)[[allCookies objectAtIndex:i] intValue];
378 (*queue)->addElement(queue, cookie, 0);
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);
392 NSLog(@"Error when setting event callout");
395 NSLog(@"Error when creating async event source");
398 NSLog(@"Error when opening device");