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-2009 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 /* this was added by the VideoLAN team to ensure Leopard-compatibility and is VLC-only */
59 const char* AppleRemoteDeviceName = "AppleIRController";
60 const int REMOTE_SWITCH_COOKIE=19;
61 const NSTimeInterval DEFAULT_MAXIMUM_CLICK_TIME_DIFFERENCE=0.35;
62 const NSTimeInterval HOLD_RECOGNITION_TIME_INTERVAL=0.4;
64 @implementation AppleRemote
66 #pragma public interface
69 if(( self = [super init])) {
70 openInExclusiveMode = YES;
72 hidDeviceInterface = NULL;
73 cookieToButtonMapping = [[NSMutableDictionary alloc] init];
75 if( NSAppKitVersionNumber < 1038.13 )
77 /* Leopard and early Snow Leopard Cookies */
78 msg_Dbg( VLCIntf, "using Leopard AR cookies" );
79 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Plus] forKey:@"31_29_28_18_"];
80 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Minus] forKey:@"31_30_28_18_"];
81 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu] forKey:@"31_20_18_31_20_18_"];
82 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay] forKey:@"31_21_18_31_21_18_"];
83 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight] forKey:@"31_22_18_31_22_18_"];
84 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft] forKey:@"31_23_18_31_23_18_"];
85 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold] forKey:@"31_18_4_2_"];
86 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold] forKey:@"31_18_3_2_"];
87 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold] forKey:@"31_18_31_18_"];
88 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Sleep] forKey:@"35_31_18_35_31_18_"];
89 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched] forKey:@"19_"];
93 /* current Snow Leopard cookies */
94 msg_Dbg( VLCIntf, "using Snow Leopard AR cookies" );
95 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Plus] forKey:@"33_31_30_21_20_2_"];
96 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Minus] forKey:@"33_32_30_21_20_2_"];
97 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu] forKey:@"33_22_21_20_2_33_22_21_20_2_"];
98 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay] forKey:@"33_23_21_20_2_33_23_21_20_2_"];
99 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight] forKey:@"33_24_21_20_2_33_24_21_20_2_"];
100 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft] forKey:@"33_25_21_20_2_33_25_21_20_2_"];
101 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold] forKey:@"33_21_20_14_12_2_"];
102 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold] forKey:@"33_21_20_13_12_2_"];
103 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold] forKey:@"33_21_20_2_33_21_20_2_"];
104 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Sleep] forKey:@"37_33_21_20_2_37_33_21_20_2_"];
105 [cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched] forKey:@"19_"];
106 [cookieToButtonMapping setObject:[NSNumber numberWithInt:k2009RemoteButtonPlay] forKey:@"33_21_20_8_2_33_21_20_8_2_"];
107 [cookieToButtonMapping setObject:[NSNumber numberWithInt:k2009RemoteButtonFullscreen] forKey:@"33_21_20_3_2_33_21_20_3_2_"];
112 [self setSimulatesPlusMinusHold: YES];
113 maxClickTimeDifference = DEFAULT_MAXIMUM_CLICK_TIME_DIFFERENCE;
119 [self stopListening:self];
120 [cookieToButtonMapping release];
128 - (BOOL) isRemoteAvailable {
129 io_object_t hidDevice = [self findAppleRemoteDevice];
130 if (hidDevice != 0) {
131 IOObjectRelease(hidDevice);
138 - (BOOL) isListeningToRemote {
139 return (hidDeviceInterface != NULL && allCookies != NULL && queue != NULL);
142 - (void) setListeningToRemote: (BOOL) value {
144 [self stopListening:self];
146 [self startListening:self];
150 /* Delegates are not retained!
151 * http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_4.html
152 * Delegating objects do not (and should not) retain their delegates.
153 * However, clients of delegating objects (applications, usually) are responsible for ensuring that their delegates are around
154 * to receive delegation messages. To do this, they may have to retain the delegate. */
155 - (void) setDelegate: (id) _delegate {
156 if (_delegate && [_delegate respondsToSelector:@selector(appleRemoteButton:pressedDown:clickCount:)]==NO) return;
158 delegate = _delegate;
164 - (BOOL) isOpenInExclusiveMode {
165 return openInExclusiveMode;
167 - (void) setOpenInExclusiveMode: (BOOL) value {
168 openInExclusiveMode = value;
171 - (BOOL) clickCountingEnabled {
172 return clickCountEnabledButtons != 0;
174 - (void) setClickCountingEnabled: (BOOL) value {
176 [self setClickCountEnabledButtons: kRemoteButtonVolume_Plus | kRemoteButtonVolume_Minus | kRemoteButtonPlay | kRemoteButtonLeft | kRemoteButtonRight | kRemoteButtonMenu | k2009RemoteButtonPlay | k2009RemoteButtonFullscreen];
178 [self setClickCountEnabledButtons: 0];
182 - (unsigned int) clickCountEnabledButtons {
183 return clickCountEnabledButtons;
185 - (void) setClickCountEnabledButtons: (unsigned int)value {
186 clickCountEnabledButtons = value;
189 - (NSTimeInterval) maximumClickCountTimeDifference {
190 return maxClickTimeDifference;
192 - (void) setMaximumClickCountTimeDifference: (NSTimeInterval) timeDiff {
193 maxClickTimeDifference = timeDiff;
196 - (BOOL) processesBacklog {
197 return processesBacklog;
199 - (void) setProcessesBacklog: (BOOL) value {
200 processesBacklog = value;
203 - (BOOL) listeningOnAppActivate {
204 id appDelegate = [NSApp delegate];
205 return (appDelegate!=nil && [appDelegate isKindOfClass: [AppleRemoteApplicationDelegate class]]);
207 - (void) setListeningOnAppActivate: (BOOL) value {
209 if ([self listeningOnAppActivate]) return;
210 AppleRemoteApplicationDelegate* appDelegate = [[AppleRemoteApplicationDelegate alloc] initWithApplicationDelegate: [NSApp delegate]];
211 /* NSApp does not retain its delegate therefore we keep retain count on 1 */
212 [NSApp setDelegate: appDelegate];
214 if ([self listeningOnAppActivate]==NO) return;
215 AppleRemoteApplicationDelegate* appDelegate = (AppleRemoteApplicationDelegate*)[NSApp delegate];
216 id previousAppDelegate = [appDelegate applicationDelegate];
217 [NSApp setDelegate: previousAppDelegate];
218 [appDelegate release];
222 - (BOOL) simulatesPlusMinusHold {
223 return simulatePlusMinusHold;
225 - (void) setSimulatesPlusMinusHold: (BOOL) value {
226 simulatePlusMinusHold = value;
229 - (IBAction) startListening: (id) sender {
230 if ([self isListeningToRemote]) return;
232 io_object_t hidDevice = [self findAppleRemoteDevice];
233 if (hidDevice == 0) return;
235 if ([self createInterfaceForDevice:hidDevice] == NULL) {
239 if ([self initializeCookies]==NO) {
243 if ([self openDevice]==NO) {
249 [self stopListening:self];
252 IOObjectRelease(hidDevice);
255 - (IBAction) stopListening: (id) sender {
256 if (eventSource != NULL) {
257 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
258 CFRelease(eventSource);
262 (*queue)->stop(queue);
265 (*queue)->dispose(queue);
267 //release the queue we allocated
268 (*queue)->Release(queue);
273 if (allCookies != nil) {
274 [allCookies autorelease];
278 if (hidDeviceInterface != NULL) {
280 (*hidDeviceInterface)->close(hidDeviceInterface);
282 //release the interface
283 (*hidDeviceInterface)->Release(hidDeviceInterface);
285 hidDeviceInterface = NULL;
291 @implementation AppleRemote (Singleton)
293 static AppleRemote* sharedInstance=nil;
295 + (AppleRemote*) sharedRemote {
296 @synchronized(self) {
297 if (sharedInstance == nil) {
298 sharedInstance = [[self alloc] init];
301 return sharedInstance;
303 + (id)allocWithZone:(NSZone *)zone {
304 @synchronized(self) {
305 if (sharedInstance == nil) {
306 return [super allocWithZone:zone];
309 return sharedInstance;
311 - (id)copyWithZone:(NSZone *)zone {
317 - (NSUInteger)retainCount {
318 return UINT_MAX; //denotes an object that cannot be released
329 @implementation AppleRemote (PrivateMethods)
331 - (void) setRemoteId: (int) value {
335 - (IOHIDQueueInterface**) queue {
339 - (IOHIDDeviceInterface**) hidDeviceInterface {
340 return hidDeviceInterface;
344 - (NSDictionary*) cookieToButtonMapping {
345 return cookieToButtonMapping;
348 - (NSString*) validCookieSubstring: (NSString*) cookieString {
349 if (cookieString == nil || [cookieString length] == 0) return nil;
350 NSEnumerator* keyEnum = [[self cookieToButtonMapping] keyEnumerator];
352 while((key = [keyEnum nextObject])) {
353 NSRange range = [cookieString rangeOfString:key];
354 if (range.location == 0) return key;
359 - (void) sendSimulatedPlusMinusEvent: (id) time {
360 BOOL startSimulateHold = NO;
361 AppleRemoteEventIdentifier event = lastPlusMinusEvent;
362 @synchronized(self) {
363 startSimulateHold = (lastPlusMinusEvent>0 && lastPlusMinusEventTime == [time doubleValue]);
365 if (startSimulateHold) {
366 lastEventSimulatedHold = YES;
367 event = (event==kRemoteButtonVolume_Plus) ? kRemoteButtonVolume_Plus_Hold : kRemoteButtonVolume_Minus_Hold;
368 [delegate appleRemoteButton:event pressedDown: YES clickCount: 1];
372 - (void) sendRemoteButtonEvent: (AppleRemoteEventIdentifier) event pressedDown: (BOOL) pressedDown {
374 if (simulatePlusMinusHold) {
375 if (event == kRemoteButtonVolume_Plus || event == kRemoteButtonVolume_Minus) {
377 lastPlusMinusEvent = event;
378 lastPlusMinusEventTime = [NSDate timeIntervalSinceReferenceDate];
379 [self performSelector:@selector(sendSimulatedPlusMinusEvent:)
380 withObject:[NSNumber numberWithDouble:lastPlusMinusEventTime]
381 afterDelay:HOLD_RECOGNITION_TIME_INTERVAL];
384 if (lastEventSimulatedHold) {
385 event = (event==kRemoteButtonVolume_Plus) ? kRemoteButtonVolume_Plus_Hold : kRemoteButtonVolume_Minus_Hold;
386 lastPlusMinusEvent = 0;
387 lastEventSimulatedHold = NO;
389 @synchronized(self) {
390 lastPlusMinusEvent = 0;
398 if (([self clickCountEnabledButtons] & event) == event) {
399 if (pressedDown==NO && (event == kRemoteButtonVolume_Minus || event == kRemoteButtonVolume_Plus)) {
400 return; // this one is triggered automatically by the handler
402 NSNumber* eventNumber;
403 NSNumber* timeNumber;
404 @synchronized(self) {
405 lastClickCountEventTime = [NSDate timeIntervalSinceReferenceDate];
406 if (lastClickCountEvent == event) {
407 eventClickCount = eventClickCount + 1;
411 lastClickCountEvent = event;
412 timeNumber = [NSNumber numberWithDouble:lastClickCountEventTime];
413 eventNumber= [NSNumber numberWithUnsignedInt:event];
415 [self performSelector: @selector(executeClickCountEvent:)
416 withObject: [NSArray arrayWithObjects:eventNumber, timeNumber, nil]
417 afterDelay: maxClickTimeDifference];
419 [delegate appleRemoteButton:event pressedDown: pressedDown clickCount:1];
424 - (void) executeClickCountEvent: (NSArray*) values {
425 AppleRemoteEventIdentifier event = [[values objectAtIndex: 0] unsignedIntValue];
426 NSTimeInterval eventTimePoint = [[values objectAtIndex: 1] doubleValue];
428 BOOL finishedClicking = NO;
429 int finalClickCount = eventClickCount;
431 @synchronized(self) {
432 finishedClicking = (event != lastClickCountEvent || eventTimePoint == lastClickCountEventTime);
433 if (finishedClicking) eventClickCount = 0;
436 if (finishedClicking) {
437 [delegate appleRemoteButton:event pressedDown: YES clickCount:finalClickCount];
438 if ([self simulatesPlusMinusHold]==NO && (event == kRemoteButtonVolume_Minus || event == kRemoteButtonVolume_Plus)) {
439 // trigger a button release event, too
440 [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow:0.1]];
441 [delegate appleRemoteButton:event pressedDown: NO clickCount:finalClickCount];
447 - (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues {
449 if (previousRemainingCookieString) {
450 cookieString = [previousRemainingCookieString stringByAppendingString: cookieString];
451 NSLog(@"New cookie string is %@", cookieString);
452 [previousRemainingCookieString release], previousRemainingCookieString=nil;
454 if (cookieString == nil || [cookieString length] == 0) return;
455 NSNumber* buttonId = [[self cookieToButtonMapping] objectForKey: cookieString];
456 if (buttonId != nil) {
457 [self sendRemoteButtonEvent: [buttonId intValue] pressedDown: (sumOfValues>0)];
459 // let's see if a number of events are stored in the cookie string. this does
460 // happen when the main thread is too busy to handle all incoming events in time.
461 NSString* subCookieString;
462 NSString* lastSubCookieString=nil;
463 while((subCookieString = [self validCookieSubstring: cookieString])) {
464 cookieString = [cookieString substringFromIndex: [subCookieString length]];
465 lastSubCookieString = subCookieString;
466 if (processesBacklog) [self handleEventWithCookieString: subCookieString sumOfValues:sumOfValues];
468 if (processesBacklog == NO && lastSubCookieString != nil) {
469 // process the last event of the backlog and assume that the button is not pressed down any longer.
470 // The events in the backlog do not seem to be in order and therefore (in rare cases) the last event might be
471 // a button pressed down event while in reality the user has released it.
472 // NSLog(@"processing last event of backlog");
473 [self handleEventWithCookieString: lastSubCookieString sumOfValues:0];
475 if ([cookieString length] > 0) {
476 msg_Warn( VLCIntf, "Unknown AR button for cookiestring %s", [cookieString UTF8String]);
483 /* Callback method for the device queue
484 Will be called for any event of any type (cookie) to which we subscribe
486 static void QueueCallbackFunction(void* target, IOReturn result, void* refcon, void* sender) {
487 AppleRemote* remote = (AppleRemote*)target;
489 IOHIDEventStruct event;
490 AbsoluteTime zeroTime = {0,0};
491 NSMutableString* cookieString = [NSMutableString string];
492 SInt32 sumOfValues = 0;
493 while (result == kIOReturnSuccess)
495 result = (*[remote queue])->getNextEvent([remote queue], &event, zeroTime, 0);
496 if ( result != kIOReturnSuccess )
499 //printf("%d %d %d\n", event.elementCookie, event.value, event.longValue);
501 if (REMOTE_SWITCH_COOKIE == (int)event.elementCookie) {
502 [remote setRemoteId: event.value];
503 [remote handleEventWithCookieString: @"19_" sumOfValues: 0];
505 if (((int)event.elementCookie)!=5) {
506 sumOfValues+=event.value;
507 [cookieString appendString:[NSString stringWithFormat:@"%d_", event.elementCookie]];
512 [remote handleEventWithCookieString: cookieString sumOfValues: sumOfValues];
515 @implementation AppleRemote (IOKitMethods)
517 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice {
519 IOCFPlugInInterface** plugInInterface = NULL;
520 HRESULT plugInResult = S_OK;
522 IOReturn ioReturnValue = kIOReturnSuccess;
524 hidDeviceInterface = NULL;
526 ioReturnValue = IOObjectGetClass(hidDevice, className);
528 if (ioReturnValue != kIOReturnSuccess) {
529 msg_Err( VLCIntf, "Failed to get IOKit class name.");
533 ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice,
534 kIOHIDDeviceUserClientTypeID,
535 kIOCFPlugInInterfaceID,
538 if (ioReturnValue == kIOReturnSuccess)
540 //Call a method of the intermediate plug-in to create the device interface
541 plugInResult = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &hidDeviceInterface);
543 if (plugInResult != S_OK) {
544 msg_Err( VLCIntf, "Couldn't create HID class device interface");
547 if (plugInInterface) (*plugInInterface)->Release(plugInInterface);
549 return hidDeviceInterface;
552 - (io_object_t) findAppleRemoteDevice {
553 CFMutableDictionaryRef hidMatchDictionary = NULL;
554 IOReturn ioReturnValue = kIOReturnSuccess;
555 io_iterator_t hidObjectIterator = 0;
556 io_object_t hidDevice = 0;
558 // Set up a matching dictionary to search the I/O Registry by class
559 // name for all HID class devices
560 hidMatchDictionary = IOServiceMatching(AppleRemoteDeviceName);
562 // Now search I/O Registry for matching devices.
563 ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
565 if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0)) {
566 hidDevice = IOIteratorNext(hidObjectIterator);
569 // release the iterator
570 IOObjectRelease(hidObjectIterator);
575 - (BOOL) initializeCookies {
576 IOHIDDeviceInterface122** handle = (IOHIDDeviceInterface122**)hidDeviceInterface;
577 IOHIDElementCookie cookie;
581 NSArray* elements = nil;
582 NSDictionary* element;
585 if (!handle || !(*handle)) return NO;
587 /* Copy all elements, since we're grabbing most of the elements
588 * for this device anyway, and thus, it's faster to iterate them
589 * ourselves. When grabbing only one or two elements, a matching
590 * dictionary should be passed in here instead of NULL. */
591 success = (*handle)->copyMatchingElements(handle, NULL, (CFArrayRef*)&elements);
593 if (success == kIOReturnSuccess) {
595 [elements autorelease];
597 cookies = calloc(NUMBER_OF_APPLE_REMOTE_ACTIONS, sizeof(IOHIDElementCookie));
598 memset(cookies, 0, sizeof(IOHIDElementCookie) * NUMBER_OF_APPLE_REMOTE_ACTIONS);
600 allCookies = [[NSMutableArray alloc] init];
602 for (i=0; i< [elements count]; i++) {
603 element = [elements objectAtIndex:i];
606 object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementCookieKey) ];
607 if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
608 if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) continue;
609 cookie = (IOHIDElementCookie) [object longValue];
612 object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsageKey) ];
613 if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
614 usage = [object longValue];
617 object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsagePageKey) ];
618 if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
619 usagePage = [object longValue];
621 [allCookies addObject: [NSNumber numberWithInt:(int)cookie]];
630 - (BOOL) openDevice {
633 IOHIDOptionsType openMode = kIOHIDOptionsTypeNone;
634 if ([self isOpenInExclusiveMode]) openMode = kIOHIDOptionsTypeSeizeDevice;
635 IOReturn ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, openMode);
637 if (ioReturnValue == KERN_SUCCESS) {
638 queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
640 result = (*queue)->create(queue, 0, 12); //depth: maximum number of elements in queue before oldest elements in queue begin to be lost.
643 for(i=0; i<[allCookies count]; i++) {
644 IOHIDElementCookie cookie = (IOHIDElementCookie)[[allCookies objectAtIndex:i] intValue];
645 (*queue)->addElement(queue, cookie, 0);
648 // add callback for async events
649 ioReturnValue = (*queue)->createAsyncEventSource(queue, &eventSource);
650 if (ioReturnValue == KERN_SUCCESS) {
651 ioReturnValue = (*queue)->setEventCallout(queue,QueueCallbackFunction, self, NULL);
652 if (ioReturnValue == KERN_SUCCESS) {
653 CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
654 //start data delivery to queue
655 (*queue)->start(queue);
658 msg_Err( VLCIntf, "Error when setting event callout");
661 msg_Err( VLCIntf, "Error when creating async event source");
664 msg_Err( VLCIntf, "Error when opening HUD device");
672 @implementation AppleRemoteApplicationDelegate
674 - (id) initWithApplicationDelegate: (id) delegate {
675 if((self = [super init]))
676 applicationDelegate = [delegate retain];
681 [applicationDelegate release];
685 - (id) applicationDelegate {
686 return applicationDelegate;
689 - (void)applicationWillBecomeActive:(NSNotification *)aNotification {
690 if ([applicationDelegate respondsToSelector: @selector(applicationWillBecomeActive:)]) {
691 [applicationDelegate applicationWillBecomeActive: aNotification];
694 - (void)applicationDidBecomeActive:(NSNotification *)aNotification {
695 [[AppleRemote sharedRemote] setListeningToRemote: YES];
697 if ([applicationDelegate respondsToSelector: @selector(applicationDidBecomeActive:)]) {
698 [applicationDelegate applicationDidBecomeActive: aNotification];
701 - (void)applicationWillResignActive:(NSNotification *)aNotification {
702 [[AppleRemote sharedRemote] setListeningToRemote: NO];
704 if ([applicationDelegate respondsToSelector: @selector(applicationWillResignActive:)]) {
705 [applicationDelegate applicationWillResignActive: aNotification];
708 - (void)applicationDidResignActive:(NSNotification *)aNotification {
709 if ([applicationDelegate respondsToSelector: @selector(applicationDidResignActive:)]) {
710 [applicationDelegate applicationDidResignActive: aNotification];
714 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
715 NSMethodSignature* signature = [super methodSignatureForSelector: aSelector];
716 if (signature == nil && applicationDelegate != nil) {
717 signature = [applicationDelegate methodSignatureForSelector: aSelector];
722 - (void)forwardInvocation:(NSInvocation *)invocation {
723 SEL aSelector = [invocation selector];
725 if (applicationDelegate==nil || [applicationDelegate respondsToSelector:aSelector]==NO) {
726 [super forwardInvocation: invocation];
730 [invocation invokeWithTarget:applicationDelegate];