1 /*****************************************************************************
6 * Created by Martin Kahr on 11.03.06 under a MIT-style license.
7 * Copyright (c) 2006 martinkahr.com. All rights reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 *****************************************************************************
29 * Note that changes made by any members or contributors of the VideoLAN team
30 * (i.e. changes that were exclusively checked in to one of VideoLAN's source code
31 * repositories) are licensed under the GNU General Public License version 2,
32 * or (at your option) any later version.
33 * Thus, the following statements apply to our changes:
35 * Copyright (C) 2006-2007 the VideoLAN team
36 * Authors: Eric Petit <titer@m0k.org>
37 * Felix Kühne <fkuehne at videolan dot org>
39 * This program is free software; you can redistribute it and/or modify
40 * it under the terms of the GNU General Public License as published by
41 * the Free Software Foundation; either version 2 of the License, or
42 * (at your option) any later version.
44 * This program is distributed in the hope that it will be useful,
45 * but WITHOUT ANY WARRANTY; without even the implied warranty of
46 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
47 * GNU General Public License for more details.
49 * You should have received a copy of the GNU General Public License
50 * along with this program; if not, write to the Free Software
51 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
52 *****************************************************************************/
54 #import "AppleRemote.h"
56 #define MACOS_VERSION [[[NSDictionary dictionaryWithContentsOfFile: \
57 @"/System/Library/CoreServices/SystemVersion.plist"] \
58 objectForKey: @"ProductVersion"] floatValue]
60 const char* AppleRemoteDeviceName = "AppleIRController";
61 const int REMOTE_SWITCH_COOKIE=19;
62 const NSTimeInterval DEFAULT_MAXIMUM_CLICK_TIME_DIFFERENCE=0.35;
63 const NSTimeInterval HOLD_RECOGNITION_TIME_INTERVAL=0.4;
65 @implementation AppleRemote
67 #pragma public interface
70 if ( self = [super init] ) {
71 openInExclusiveMode = YES;
73 hidDeviceInterface = NULL;
74 cookieToButtonMapping = [[NSMutableDictionary alloc] init];
76 if( MACOS_VERSION < 10.5f )
78 /* use the traditional cookies for Tiger (and Panther, if it is supported by the frame app) */
79 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Plus] forKey:@"14_12_11_6_"];
80 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Minus] forKey:@"14_13_11_6_"];
81 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu] forKey:@"14_7_6_14_7_6_"];
82 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay] forKey:@"14_8_6_14_8_6_"];
83 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight] forKey:@"14_9_6_14_9_6_"];
84 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft] forKey:@"14_10_6_14_10_6_"];
85 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold] forKey:@"14_6_4_2_"];
86 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold] forKey:@"14_6_3_2_"];
87 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold] forKey:@"14_6_14_6_"];
88 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Sleep] forKey:@"18_14_6_18_14_6_"];
89 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched] forKey:@"19_"];
93 /* we're on Leopard and need to use a new set of cookies */
94 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Plus] forKey:@"31_29_28_18_"];
95 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Minus] forKey:@"31_30_28_18_"];
96 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu] forKey:@"31_20_18_31_20_18_"];
97 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay] forKey:@"31_21_18_31_21_18_"];
98 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight] forKey:@"31_22_18_31_22_18_"];
99 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft] forKey:@"31_23_18_31_23_18_"];
100 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold] forKey:@"31_18_4_2_"];
101 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold] forKey:@"31_18_3_2_"];
102 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold] forKey:@"31_18_31_18_"];
103 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Sleep] forKey:@"35_31_18_35_31_18_"];
104 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched] forKey:@"19_"];
108 [self setSimulatesPlusMinusHold: YES];
109 maxClickTimeDifference = DEFAULT_MAXIMUM_CLICK_TIME_DIFFERENCE;
116 [self stopListening:self];
117 [cookieToButtonMapping release];
125 - (BOOL) isRemoteAvailable {
126 io_object_t hidDevice = [self findAppleRemoteDevice];
127 if (hidDevice != 0) {
128 IOObjectRelease(hidDevice);
135 - (BOOL) isListeningToRemote {
136 return (hidDeviceInterface != NULL && allCookies != NULL && queue != NULL);
139 - (void) setListeningToRemote: (BOOL) value {
141 [self stopListening:self];
143 [self startListening:self];
147 /* Delegates are not retained!
148 * http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_4.html
149 * Delegating objects do not (and should not) retain their delegates.
150 * However, clients of delegating objects (applications, usually) are responsible for ensuring that their delegates are around
151 * to receive delegation messages. To do this, they may have to retain the delegate. */
152 - (void) setDelegate: (id) _delegate {
153 if (_delegate && [_delegate respondsToSelector:@selector(appleRemoteButton:pressedDown:clickCount:)]==NO) return;
155 delegate = _delegate;
161 - (BOOL) isOpenInExclusiveMode {
162 return openInExclusiveMode;
164 - (void) setOpenInExclusiveMode: (BOOL) value {
165 openInExclusiveMode = value;
168 - (BOOL) clickCountingEnabled {
169 return clickCountEnabledButtons != 0;
171 - (void) setClickCountingEnabled: (BOOL) value {
173 [self setClickCountEnabledButtons: kRemoteButtonVolume_Plus | kRemoteButtonVolume_Minus | kRemoteButtonPlay | kRemoteButtonLeft | kRemoteButtonRight | kRemoteButtonMenu];
175 [self setClickCountEnabledButtons: 0];
179 - (unsigned int) clickCountEnabledButtons {
180 return clickCountEnabledButtons;
182 - (void) setClickCountEnabledButtons: (unsigned int)value {
183 clickCountEnabledButtons = value;
186 - (NSTimeInterval) maximumClickCountTimeDifference {
187 return maxClickTimeDifference;
189 - (void) setMaximumClickCountTimeDifference: (NSTimeInterval) timeDiff {
190 maxClickTimeDifference = timeDiff;
193 - (BOOL) processesBacklog {
194 return processesBacklog;
196 - (void) setProcessesBacklog: (BOOL) value {
197 processesBacklog = value;
200 - (BOOL) listeningOnAppActivate {
201 id appDelegate = [NSApp delegate];
202 return (appDelegate!=nil && [appDelegate isKindOfClass: [AppleRemoteApplicationDelegate class]]);
204 - (void) setListeningOnAppActivate: (BOOL) value {
206 if ([self listeningOnAppActivate]) return;
207 AppleRemoteApplicationDelegate* appDelegate = [[AppleRemoteApplicationDelegate alloc] initWithApplicationDelegate: [NSApp delegate]];
208 /* NSApp does not retain its delegate therefore we keep retain count on 1 */
209 [NSApp setDelegate: appDelegate];
211 if ([self listeningOnAppActivate]==NO) return;
212 AppleRemoteApplicationDelegate* appDelegate = (AppleRemoteApplicationDelegate*)[NSApp delegate];
213 id previousAppDelegate = [appDelegate applicationDelegate];
214 [NSApp setDelegate: previousAppDelegate];
215 [appDelegate release];
219 - (BOOL) simulatesPlusMinusHold {
220 return simulatePlusMinusHold;
222 - (void) setSimulatesPlusMinusHold: (BOOL) value {
223 simulatePlusMinusHold = value;
226 - (IBAction) startListening: (id) sender {
227 if ([self isListeningToRemote]) return;
229 io_object_t hidDevice = [self findAppleRemoteDevice];
230 if (hidDevice == 0) return;
232 if ([self createInterfaceForDevice:hidDevice] == NULL) {
236 if ([self initializeCookies]==NO) {
240 if ([self openDevice]==NO) {
246 [self stopListening:self];
249 IOObjectRelease(hidDevice);
252 - (IBAction) stopListening: (id) sender {
254 (*queue)->stop(queue);
257 (*queue)->dispose(queue);
259 //release the queue we allocated
260 (*queue)->Release(queue);
265 if (allCookies != nil) {
266 [allCookies autorelease];
270 if (hidDeviceInterface != NULL) {
272 (*hidDeviceInterface)->close(hidDeviceInterface);
274 //release the interface
275 (*hidDeviceInterface)->Release(hidDeviceInterface);
277 hidDeviceInterface = NULL;
283 @implementation AppleRemote (Singleton)
285 static AppleRemote* sharedInstance=nil;
287 + (AppleRemote*) sharedRemote {
288 @synchronized(self) {
289 if (sharedInstance == nil) {
290 sharedInstance = [[self alloc] init];
293 return sharedInstance;
295 + (id)allocWithZone:(NSZone *)zone {
296 @synchronized(self) {
297 if (sharedInstance == nil) {
298 return [super allocWithZone:zone];
301 return sharedInstance;
303 - (id)copyWithZone:(NSZone *)zone {
309 - (unsigned)retainCount {
310 return UINT_MAX; //denotes an object that cannot be released
321 @implementation AppleRemote (PrivateMethods)
323 - (void) setRemoteId: (int) value {
327 - (IOHIDQueueInterface**) queue {
331 - (IOHIDDeviceInterface**) hidDeviceInterface {
332 return hidDeviceInterface;
336 - (NSDictionary*) cookieToButtonMapping {
337 return cookieToButtonMapping;
340 - (NSString*) validCookieSubstring: (NSString*) cookieString {
341 if (cookieString == nil || [cookieString length] == 0) return nil;
342 NSEnumerator* keyEnum = [[self cookieToButtonMapping] keyEnumerator];
344 while(key = [keyEnum nextObject]) {
345 NSRange range = [cookieString rangeOfString:key];
346 if (range.location == 0) return key;
351 - (void) sendSimulatedPlusMinusEvent: (id) time {
352 BOOL startSimulateHold = NO;
353 AppleRemoteEventIdentifier event = lastPlusMinusEvent;
354 @synchronized(self) {
355 startSimulateHold = (lastPlusMinusEvent>0 && lastPlusMinusEventTime == [time doubleValue]);
357 if (startSimulateHold) {
358 lastEventSimulatedHold = YES;
359 event = (event==kRemoteButtonVolume_Plus) ? kRemoteButtonVolume_Plus_Hold : kRemoteButtonVolume_Minus_Hold;
360 [delegate appleRemoteButton:event pressedDown: YES clickCount: 1];
364 - (void) sendRemoteButtonEvent: (AppleRemoteEventIdentifier) event pressedDown: (BOOL) pressedDown {
366 if (simulatePlusMinusHold) {
367 if (event == kRemoteButtonVolume_Plus || event == kRemoteButtonVolume_Minus) {
369 lastPlusMinusEvent = event;
370 lastPlusMinusEventTime = [NSDate timeIntervalSinceReferenceDate];
371 [self performSelector:@selector(sendSimulatedPlusMinusEvent:)
372 withObject:[NSNumber numberWithDouble:lastPlusMinusEventTime]
373 afterDelay:HOLD_RECOGNITION_TIME_INTERVAL];
376 if (lastEventSimulatedHold) {
377 event = (event==kRemoteButtonVolume_Plus) ? kRemoteButtonVolume_Plus_Hold : kRemoteButtonVolume_Minus_Hold;
378 lastPlusMinusEvent = 0;
379 lastEventSimulatedHold = NO;
381 @synchronized(self) {
382 lastPlusMinusEvent = 0;
390 if (([self clickCountEnabledButtons] & event) == event) {
391 if (pressedDown==NO && (event == kRemoteButtonVolume_Minus || event == kRemoteButtonVolume_Plus)) {
392 return; // this one is triggered automatically by the handler
394 NSNumber* eventNumber;
395 NSNumber* timeNumber;
396 @synchronized(self) {
397 lastClickCountEventTime = [NSDate timeIntervalSinceReferenceDate];
398 if (lastClickCountEvent == event) {
399 eventClickCount = eventClickCount + 1;
403 lastClickCountEvent = event;
404 timeNumber = [NSNumber numberWithDouble:lastClickCountEventTime];
405 eventNumber= [NSNumber numberWithUnsignedInt:event];
407 [self performSelector: @selector(executeClickCountEvent:)
408 withObject: [NSArray arrayWithObjects:eventNumber, timeNumber, nil]
409 afterDelay: maxClickTimeDifference];
411 [delegate appleRemoteButton:event pressedDown: pressedDown clickCount:1];
416 - (void) executeClickCountEvent: (NSArray*) values {
417 AppleRemoteEventIdentifier event = [[values objectAtIndex: 0] unsignedIntValue];
418 NSTimeInterval eventTimePoint = [[values objectAtIndex: 1] doubleValue];
420 BOOL finishedClicking = NO;
421 int finalClickCount = eventClickCount;
423 @synchronized(self) {
424 finishedClicking = (event != lastClickCountEvent || eventTimePoint == lastClickCountEventTime);
425 if (finishedClicking) eventClickCount = 0;
428 if (finishedClicking) {
429 [delegate appleRemoteButton:event pressedDown: YES clickCount:finalClickCount];
430 if ([self simulatesPlusMinusHold]==NO && (event == kRemoteButtonVolume_Minus || event == kRemoteButtonVolume_Plus)) {
431 // trigger a button release event, too
432 [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow:0.1]];
433 [delegate appleRemoteButton:event pressedDown: NO clickCount:finalClickCount];
439 - (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues {
441 if (previousRemainingCookieString) {
442 cookieString = [previousRemainingCookieString stringByAppendingString: cookieString];
443 NSLog(@"New cookie string is %@", cookieString);
444 [previousRemainingCookieString release], previousRemainingCookieString=nil;
446 if (cookieString == nil || [cookieString length] == 0) return;
447 NSNumber* buttonId = [[self cookieToButtonMapping] objectForKey: cookieString];
448 if (buttonId != nil) {
449 [self sendRemoteButtonEvent: [buttonId intValue] pressedDown: (sumOfValues>0)];
451 // let's see if a number of events are stored in the cookie string. this does
452 // happen when the main thread is too busy to handle all incoming events in time.
453 NSString* subCookieString;
454 NSString* lastSubCookieString=nil;
455 while(subCookieString = [self validCookieSubstring: cookieString]) {
456 cookieString = [cookieString substringFromIndex: [subCookieString length]];
457 lastSubCookieString = subCookieString;
458 if (processesBacklog) [self handleEventWithCookieString: subCookieString sumOfValues:sumOfValues];
460 if (processesBacklog == NO && lastSubCookieString != nil) {
461 // process the last event of the backlog and assume that the button is not pressed down any longer.
462 // The events in the backlog do not seem to be in order and therefore (in rare cases) the last event might be
463 // a button pressed down event while in reality the user has released it.
464 // NSLog(@"processing last event of backlog");
465 [self handleEventWithCookieString: lastSubCookieString sumOfValues:0];
467 if ([cookieString length] > 0) {
468 NSLog(@"Unknown button for cookiestring %@", cookieString);
475 /* Callback method for the device queue
476 Will be called for any event of any type (cookie) to which we subscribe
478 static void QueueCallbackFunction(void* target, IOReturn result, void* refcon, void* sender) {
479 AppleRemote* remote = (AppleRemote*)target;
481 IOHIDEventStruct event;
482 AbsoluteTime zeroTime = {0,0};
483 NSMutableString* cookieString = [NSMutableString string];
484 SInt32 sumOfValues = 0;
485 while (result == kIOReturnSuccess)
487 result = (*[remote queue])->getNextEvent([remote queue], &event, zeroTime, 0);
488 if ( result != kIOReturnSuccess )
491 //printf("%d %d %d\n", event.elementCookie, event.value, event.longValue);
493 if (REMOTE_SWITCH_COOKIE == (int)event.elementCookie) {
494 [remote setRemoteId: event.value];
495 [remote handleEventWithCookieString: @"19_" sumOfValues: 0];
497 if (((int)event.elementCookie)!=5) {
498 sumOfValues+=event.value;
499 [cookieString appendString:[NSString stringWithFormat:@"%d_", event.elementCookie]];
504 [remote handleEventWithCookieString: cookieString sumOfValues: sumOfValues];
507 @implementation AppleRemote (IOKitMethods)
509 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice {
511 IOCFPlugInInterface** plugInInterface = NULL;
512 HRESULT plugInResult = S_OK;
514 IOReturn ioReturnValue = kIOReturnSuccess;
516 hidDeviceInterface = NULL;
518 ioReturnValue = IOObjectGetClass(hidDevice, className);
520 if (ioReturnValue != kIOReturnSuccess) {
521 NSLog(@"Error: Failed to get class name.");
525 ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice,
526 kIOHIDDeviceUserClientTypeID,
527 kIOCFPlugInInterfaceID,
530 if (ioReturnValue == kIOReturnSuccess)
532 //Call a method of the intermediate plug-in to create the device interface
533 plugInResult = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &hidDeviceInterface);
535 if (plugInResult != S_OK) {
536 NSLog(@"Error: Couldn't create HID class device interface");
539 if (plugInInterface) (*plugInInterface)->Release(plugInInterface);
541 return hidDeviceInterface;
544 - (io_object_t) findAppleRemoteDevice {
545 CFMutableDictionaryRef hidMatchDictionary = NULL;
546 IOReturn ioReturnValue = kIOReturnSuccess;
547 io_iterator_t hidObjectIterator = 0;
548 io_object_t hidDevice = 0;
550 // Set up a matching dictionary to search the I/O Registry by class
551 // name for all HID class devices
552 hidMatchDictionary = IOServiceMatching(AppleRemoteDeviceName);
554 // Now search I/O Registry for matching devices.
555 ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
557 if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0)) {
558 hidDevice = IOIteratorNext(hidObjectIterator);
561 // release the iterator
562 IOObjectRelease(hidObjectIterator);
567 - (BOOL) initializeCookies {
568 IOHIDDeviceInterface122** handle = (IOHIDDeviceInterface122**)hidDeviceInterface;
569 IOHIDElementCookie cookie;
573 NSArray* elements = nil;
574 NSDictionary* element;
577 if (!handle || !(*handle)) return NO;
579 /* Copy all elements, since we're grabbing most of the elements
580 * for this device anyway, and thus, it's faster to iterate them
581 * ourselves. When grabbing only one or two elements, a matching
582 * dictionary should be passed in here instead of NULL. */
583 success = (*handle)->copyMatchingElements(handle, NULL, (CFArrayRef*)&elements);
585 if (success == kIOReturnSuccess) {
587 [elements autorelease];
589 cookies = calloc(NUMBER_OF_APPLE_REMOTE_ACTIONS, sizeof(IOHIDElementCookie));
590 memset(cookies, 0, sizeof(IOHIDElementCookie) * NUMBER_OF_APPLE_REMOTE_ACTIONS);
592 allCookies = [[NSMutableArray alloc] init];
594 for (i=0; i< [elements count]; i++) {
595 element = [elements objectAtIndex:i];
598 object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementCookieKey) ];
599 if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
600 if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) continue;
601 cookie = (IOHIDElementCookie) [object longValue];
604 object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsageKey) ];
605 if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
606 usage = [object longValue];
609 object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsagePageKey) ];
610 if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
611 usagePage = [object longValue];
613 [allCookies addObject: [NSNumber numberWithInt:(int)cookie]];
622 - (BOOL) openDevice {
625 IOHIDOptionsType openMode = kIOHIDOptionsTypeNone;
626 if ([self isOpenInExclusiveMode]) openMode = kIOHIDOptionsTypeSeizeDevice;
627 IOReturn ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, openMode);
629 if (ioReturnValue == KERN_SUCCESS) {
630 queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
632 result = (*queue)->create(queue, 0, 12); //depth: maximum number of elements in queue before oldest elements in queue begin to be lost.
635 for(i=0; i<[allCookies count]; i++) {
636 IOHIDElementCookie cookie = (IOHIDElementCookie)[[allCookies objectAtIndex:i] intValue];
637 (*queue)->addElement(queue, cookie, 0);
640 // add callback for async events
641 CFRunLoopSourceRef eventSource;
642 ioReturnValue = (*queue)->createAsyncEventSource(queue, &eventSource);
643 if (ioReturnValue == KERN_SUCCESS) {
644 ioReturnValue = (*queue)->setEventCallout(queue,QueueCallbackFunction, self, NULL);
645 if (ioReturnValue == KERN_SUCCESS) {
646 CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
647 //start data delivery to queue
648 (*queue)->start(queue);
651 NSLog(@"Error when setting event callout");
654 NSLog(@"Error when creating async event source");
657 NSLog(@"Error when opening device");
665 @implementation AppleRemoteApplicationDelegate
667 - (id) initWithApplicationDelegate: (id) delegate {
668 if (self = [super init]) {
669 applicationDelegate = [delegate retain];
675 [applicationDelegate release];
679 - (id) applicationDelegate {
680 return applicationDelegate;
683 - (void)applicationWillBecomeActive:(NSNotification *)aNotification {
684 if ([applicationDelegate respondsToSelector: @selector(applicationWillBecomeActive:)]) {
685 [applicationDelegate applicationWillBecomeActive: aNotification];
688 - (void)applicationDidBecomeActive:(NSNotification *)aNotification {
689 [[AppleRemote sharedRemote] setListeningToRemote: YES];
691 if ([applicationDelegate respondsToSelector: @selector(applicationDidBecomeActive:)]) {
692 [applicationDelegate applicationDidBecomeActive: aNotification];
695 - (void)applicationWillResignActive:(NSNotification *)aNotification {
696 [[AppleRemote sharedRemote] setListeningToRemote: NO];
698 if ([applicationDelegate respondsToSelector: @selector(applicationWillResignActive:)]) {
699 [applicationDelegate applicationWillResignActive: aNotification];
702 - (void)applicationDidResignActive:(NSNotification *)aNotification {
703 if ([applicationDelegate respondsToSelector: @selector(applicationDidResignActive:)]) {
704 [applicationDelegate applicationDidResignActive: aNotification];
708 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
709 NSMethodSignature* signature = [super methodSignatureForSelector: aSelector];
710 if (signature == nil && applicationDelegate != nil) {
711 signature = [applicationDelegate methodSignatureForSelector: aSelector];
716 - (void)forwardInvocation:(NSInvocation *)invocation {
717 SEL aSelector = [invocation selector];
719 if (applicationDelegate==nil || [applicationDelegate respondsToSelector:aSelector]==NO) {
720 [super forwardInvocation: invocation];
724 [invocation invokeWithTarget:applicationDelegate];