]> git.sesse.net Git - vlc/blob - modules/gui/macosx/AppleRemote.m
macosx: remove interfaces which are now declared in .m file
[vlc] / modules / gui / macosx / AppleRemote.m
1 /*****************************************************************************
2  * AppleRemote.m
3  * AppleRemote
4  * $Id$
5  *
6  * Created by Martin Kahr on 11.03.06 under a MIT-style license.
7  * Copyright (c) 2006 martinkahr.com. All rights reserved.
8  *
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:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
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
25  * THE SOFTWARE.
26  *
27  *****************************************************************************
28  *
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:
34  *
35  * Copyright (C) 2006-2009 VLC authors and VideoLAN
36  * Authors: Eric Petit <titer@m0k.org>
37  *          Felix Kühne <fkuehne at videolan dot org>
38  *
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.
43  *
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.
48  *
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  *****************************************************************************/
53
54 #import "AppleRemote.h"
55
56 /* this was added by the VideoLAN team to ensure Leopard-compatibility and is VLC-only */
57 #import "intf.h"
58 #import "CompatibilityFixes.h"
59
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;
64
65 @implementation AppleRemote
66
67 @synthesize openInExclusiveMode = _openInExclusiveMode, clickCountEnabledButtons = _clickCountEnabledButtons, maximumClickCountTimeDifference = _maxClickTimeDifference, processesBacklog=_processesBacklog, simulatesPlusMinusHold = _simulatePlusMinusHold;
68
69 #pragma public interface
70
71 static AppleRemote *_o_sharedInstance = nil;
72
73 + (AppleRemote *)sharedInstance
74 {
75     return _o_sharedInstance ? _o_sharedInstance : [[self alloc] init];
76 }
77
78 - (id)init
79 {
80     if (_o_sharedInstance) {
81         [self dealloc];
82     } else {
83         _o_sharedInstance = [super init];
84         _openInExclusiveMode = YES;
85         queue = NULL;
86         hidDeviceInterface = NULL;
87         NSMutableDictionary * mutableCookieToButtonMapping = [[NSMutableDictionary alloc] init];
88
89         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Plus]    forKey:@"33_31_30_21_20_2_"];
90         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonVolume_Minus]   forKey:@"33_32_30_21_20_2_"];
91         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu]           forKey:@"33_22_21_20_2_33_22_21_20_2_"];
92         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay]           forKey:@"33_23_21_20_2_33_23_21_20_2_"];
93         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight]          forKey:@"33_24_21_20_2_33_24_21_20_2_"];
94         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft]           forKey:@"33_25_21_20_2_33_25_21_20_2_"];
95         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold]     forKey:@"33_21_20_14_12_2_"];
96         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold]      forKey:@"33_21_20_13_12_2_"];
97         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold]      forKey:@"33_21_20_2_33_21_20_2_"];
98         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Sleep]     forKey:@"37_33_21_20_2_37_33_21_20_2_"];
99         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:k2009RemoteButtonPlay]       forKey:@"33_21_20_8_2_33_21_20_8_2_"];
100         [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:k2009RemoteButtonFullscreen] forKey:@"33_21_20_3_2_33_21_20_3_2_"];
101
102         if( OSX_SNOW_LEOPARD )
103             /* 10.6.2+ Snow Leopard cookies */
104             [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]     forKey:@"19_"];
105         else
106         /* Lion cookies */
107             [mutableCookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]     forKey:@"42_33_23_21_20_2_33_23_21_20_2_"];
108
109         _cookieToButtonMapping = [[NSDictionary alloc] initWithDictionary: mutableCookieToButtonMapping];
110         [mutableCookieToButtonMapping release];
111
112         /* defaults */
113         _simulatePlusMinusHold = YES;
114         _maxClickTimeDifference = DEFAULT_MAXIMUM_CLICK_TIME_DIFFERENCE;
115     }
116
117     return _o_sharedInstance;
118 }
119
120 - (void) dealloc {
121     [self stopListening:self];
122     [_cookieToButtonMapping release];
123     [super dealloc];
124 }
125
126 - (int) remoteId {
127     return remoteId;
128 }
129
130 - (BOOL) remoteAvailable {
131     io_object_t hidDevice = [self findAppleRemoteDevice];
132     if (hidDevice != 0) {
133         IOObjectRelease(hidDevice);
134         return YES;
135     } else {
136         return NO;
137     }
138 }
139
140 - (BOOL) listeningToRemote {
141     return (hidDeviceInterface != NULL && _allCookies != NULL && queue != NULL);
142 }
143
144 - (void) setListeningToRemote: (BOOL) value {
145     if (value == NO) {
146         [self stopListening:self];
147     } else {
148         [self startListening:self];
149     }
150 }
151
152 /* Delegates are not retained!
153  * http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_4.html
154  * Delegating objects do not (and should not) retain their delegates.
155  * However, clients of delegating objects (applications, usually) are responsible for ensuring that their delegates are around
156  * to receive delegation messages. To do this, they may have to retain the delegate. */
157 - (void) setDelegate: (id) _delegate {
158     if (_delegate && [_delegate respondsToSelector:@selector(appleRemoteButton:pressedDown:clickCount:)]==NO) return;
159
160     delegate = _delegate;
161 }
162 - (id) delegate {
163     return delegate;
164 }
165
166 - (BOOL) clickCountingEnabled {
167     return self.clickCountEnabledButtons != 0;
168 }
169 - (void) setClickCountingEnabled: (BOOL) value {
170     if (value) {
171         [self setClickCountEnabledButtons: kRemoteButtonVolume_Plus | kRemoteButtonVolume_Minus | kRemoteButtonPlay | kRemoteButtonLeft | kRemoteButtonRight | kRemoteButtonMenu | k2009RemoteButtonPlay | k2009RemoteButtonFullscreen];
172     } else {
173         [self setClickCountEnabledButtons: 0];
174     }
175 }
176
177 - (BOOL) listeningOnAppActivate {
178     id appDelegate = [NSApp delegate];
179     return (appDelegate!=nil && [appDelegate isKindOfClass: [AppleRemoteApplicationDelegate class]]);
180 }
181 - (void) setListeningOnAppActivate: (BOOL) value {
182     if (value) {
183         if ([self listeningOnAppActivate]) return;
184         AppleRemoteApplicationDelegate* appDelegate = [[AppleRemoteApplicationDelegate alloc] initWithApplicationDelegate: [NSApp delegate]];
185         /* NSApp does not retain its delegate therefore we keep retain count on 1 */
186         [NSApp setDelegate: appDelegate];
187     } else {
188         if ([self listeningOnAppActivate]==NO) return;
189         AppleRemoteApplicationDelegate* appDelegate = (AppleRemoteApplicationDelegate*)[NSApp delegate];
190         id previousAppDelegate = [appDelegate applicationDelegate];
191         [NSApp setDelegate: previousAppDelegate];
192         [appDelegate release];
193     }
194 }
195
196 - (IBAction) startListening: (id) sender {
197     if ([self listeningToRemote]) return;
198
199     io_object_t hidDevice = [self findAppleRemoteDevice];
200     if (hidDevice == 0) return;
201
202     if ([self createInterfaceForDevice:hidDevice] == NULL) {
203         goto error;
204     }
205
206     if ([self initializeCookies]==NO) {
207         goto error;
208     }
209
210     if ([self openDevice]==NO) {
211         goto error;
212     }
213     goto cleanup;
214
215 error:
216     [self stopListening:self];
217
218 cleanup:
219     IOObjectRelease(hidDevice);
220 }
221
222 - (IBAction) stopListening: (id) sender {
223     if (eventSource != NULL) {
224         CFRunLoopRemoveSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
225         CFRelease(eventSource);
226         eventSource = NULL;
227     }
228     if (queue != NULL) {
229         (*queue)->stop(queue);
230
231         //dispose of queue
232         (*queue)->dispose(queue);
233
234         //release the queue we allocated
235         (*queue)->Release(queue);
236
237         queue = NULL;
238     }
239
240     if (_allCookies != nil) {
241         [_allCookies autorelease];
242         _allCookies = nil;
243     }
244
245     if (hidDeviceInterface != NULL) {
246         //close the device
247         (*hidDeviceInterface)->close(hidDeviceInterface);
248
249         //release the interface
250         (*hidDeviceInterface)->Release(hidDeviceInterface);
251
252         hidDeviceInterface = NULL;
253     }
254 }
255
256 @end
257
258 @implementation AppleRemote (Singleton)
259
260 static AppleRemote* sharedInstance=nil;
261
262 + (AppleRemote*) sharedRemote {
263     @synchronized(self) {
264         if (sharedInstance == nil) {
265             sharedInstance = [[self alloc] init];
266         }
267     }
268     return sharedInstance;
269 }
270 + (id)allocWithZone:(NSZone *)zone {
271     @synchronized(self) {
272         if (sharedInstance == nil) {
273             return [super allocWithZone:zone];
274         }
275     }
276     return sharedInstance;
277 }
278 - (id)copyWithZone:(NSZone *)zone {
279     return self;
280 }
281 - (id)retain {
282     return self;
283 }
284 - (NSUInteger)retainCount {
285     return UINT_MAX;  //denotes an object that cannot be released
286 }
287 - (void)release {
288     //do nothing
289 }
290 - (id)autorelease {
291     return self;
292 }
293
294 @end
295
296 @implementation AppleRemote (PrivateMethods)
297
298 - (void) setRemoteId: (int) value {
299     remoteId = value;
300 }
301
302 - (IOHIDQueueInterface**) queue {
303     return queue;
304 }
305
306 - (IOHIDDeviceInterface**) hidDeviceInterface {
307     return hidDeviceInterface;
308 }
309
310 - (NSDictionary*) cookieToButtonMapping {
311     return _cookieToButtonMapping;
312 }
313
314 - (NSString*) validCookieSubstring: (NSString*) cookieString {
315     if (cookieString == nil || [cookieString length] == 0) return nil;
316     NSEnumerator* keyEnum = [[self cookieToButtonMapping] keyEnumerator];
317     NSString* key;
318     while((key = [keyEnum nextObject])) {
319         NSRange range = [cookieString rangeOfString:key];
320         if (range.location == 0) return key;
321     }
322     return nil;
323 }
324
325 - (void) sendSimulatedPlusMinusEvent: (id) time {
326     BOOL startSimulateHold = NO;
327     AppleRemoteEventIdentifier event = lastPlusMinusEvent;
328     @synchronized(self) {
329         startSimulateHold = (lastPlusMinusEvent>0 && lastPlusMinusEventTime == [time doubleValue]);
330     }
331     if (startSimulateHold) {
332         lastEventSimulatedHold = YES;
333         event = (event==kRemoteButtonVolume_Plus) ? kRemoteButtonVolume_Plus_Hold : kRemoteButtonVolume_Minus_Hold;
334         [delegate appleRemoteButton:event pressedDown: YES clickCount: 1];
335     }
336 }
337
338 - (void) sendRemoteButtonEvent: (AppleRemoteEventIdentifier) event pressedDown: (BOOL) pressedDown {
339     if (delegate) {
340         if (self.simulatesPlusMinusHold) {
341             if (event == kRemoteButtonVolume_Plus || event == kRemoteButtonVolume_Minus) {
342                 if (pressedDown) {
343                     lastPlusMinusEvent = event;
344                     lastPlusMinusEventTime = [NSDate timeIntervalSinceReferenceDate];
345                     [self performSelector:@selector(sendSimulatedPlusMinusEvent:)
346                                withObject:[NSNumber numberWithDouble:lastPlusMinusEventTime]
347                                afterDelay:HOLD_RECOGNITION_TIME_INTERVAL];
348                     return;
349                 } else {
350                     if (lastEventSimulatedHold) {
351                         event = (event==kRemoteButtonVolume_Plus) ? kRemoteButtonVolume_Plus_Hold : kRemoteButtonVolume_Minus_Hold;
352                         lastPlusMinusEvent = 0;
353                         lastEventSimulatedHold = NO;
354                     } else {
355                         @synchronized(self) {
356                             lastPlusMinusEvent = 0;
357                         }
358                         pressedDown = YES;
359                     }
360                 }
361             }
362         }
363
364         if ((self.clickCountEnabledButtons & event) == event) {
365             if (pressedDown==NO && (event == kRemoteButtonVolume_Minus || event == kRemoteButtonVolume_Plus)) {
366                 return; // this one is triggered automatically by the handler
367             }
368             NSNumber* eventNumber;
369             NSNumber* timeNumber;
370             @synchronized(self) {
371                 lastClickCountEventTime = [NSDate timeIntervalSinceReferenceDate];
372                 if (lastClickCountEvent == event) {
373                     eventClickCount = eventClickCount + 1;
374                 } else {
375                     eventClickCount = 1;
376                 }
377                 lastClickCountEvent = event;
378                 timeNumber = [NSNumber numberWithDouble:lastClickCountEventTime];
379                 eventNumber= [NSNumber numberWithUnsignedInt:event];
380             }
381             [self performSelector: @selector(executeClickCountEvent:)
382                        withObject: [NSArray arrayWithObjects:eventNumber, timeNumber, nil]
383                        afterDelay: _maxClickTimeDifference];
384         } else {
385             [delegate appleRemoteButton:event pressedDown: pressedDown clickCount:1];
386         }
387     }
388 }
389
390 - (void) executeClickCountEvent: (NSArray*) values {
391     AppleRemoteEventIdentifier event = [[values objectAtIndex: 0] unsignedIntValue];
392     NSTimeInterval eventTimePoint = [[values objectAtIndex: 1] doubleValue];
393
394     BOOL finishedClicking = NO;
395     int finalClickCount = eventClickCount;
396
397     @synchronized(self) {
398         finishedClicking = (event != lastClickCountEvent || eventTimePoint == lastClickCountEventTime);
399         if (finishedClicking) eventClickCount = 0;
400     }
401
402     if (finishedClicking) {
403         [delegate appleRemoteButton:event pressedDown: YES clickCount:finalClickCount];
404         if ([self simulatesPlusMinusHold]==NO && (event == kRemoteButtonVolume_Minus || event == kRemoteButtonVolume_Plus)) {
405             // trigger a button release event, too
406             [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow:0.1]];
407             [delegate appleRemoteButton:event pressedDown: NO clickCount:finalClickCount];
408         }
409     }
410
411 }
412
413 - (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues {
414     /*
415     if (previousRemainingCookieString) {
416         cookieString = [previousRemainingCookieString stringByAppendingString: cookieString];
417         NSLog(@"New cookie string is %@", cookieString);
418         [previousRemainingCookieString release], previousRemainingCookieString=nil;
419     }*/
420     if (cookieString == nil || [cookieString length] == 0) return;
421     NSNumber* buttonId = [[self cookieToButtonMapping] objectForKey: cookieString];
422     if (buttonId != nil) {
423         [self sendRemoteButtonEvent: [buttonId intValue] pressedDown: (sumOfValues>0)];
424     } else {
425         // let's see if a number of events are stored in the cookie string. this does
426         // happen when the main thread is too busy to handle all incoming events in time.
427         NSString* subCookieString;
428         NSString* lastSubCookieString=nil;
429         while((subCookieString = [self validCookieSubstring: cookieString])) {
430             cookieString = [cookieString substringFromIndex: [subCookieString length]];
431             lastSubCookieString = subCookieString;
432             if (self.processesBacklog) [self handleEventWithCookieString: subCookieString sumOfValues:sumOfValues];
433         }
434         if (self.processesBacklog == NO && lastSubCookieString != nil) {
435             // process the last event of the backlog and assume that the button is not pressed down any longer.
436             // The events in the backlog do not seem to be in order and therefore (in rare cases) the last event might be
437             // a button pressed down event while in reality the user has released it.
438             // NSLog(@"processing last event of backlog");
439             [self handleEventWithCookieString: lastSubCookieString sumOfValues:0];
440         }
441         if ([cookieString length] > 0) {
442             msg_Warn( VLCIntf, "Unknown AR button for cookiestring %s", [cookieString UTF8String]);
443         }
444     }
445 }
446
447 @end
448
449 /*  Callback method for the device queue
450 Will be called for any event of any type (cookie) to which we subscribe
451 */
452 static void QueueCallbackFunction(void* target,  IOReturn result, void* refcon, void* sender) {
453     AppleRemote* remote = (AppleRemote*)target;
454
455     IOHIDEventStruct event;
456     AbsoluteTime     zeroTime = {0,0};
457     NSMutableString* cookieString = [NSMutableString string];
458     SInt32           sumOfValues = 0;
459     while (result == kIOReturnSuccess)
460     {
461         result = (*[remote queue])->getNextEvent([remote queue], &event, zeroTime, 0);
462         if ( result != kIOReturnSuccess )
463             continue;
464
465         //printf("%d %d %d\n", event.elementCookie, event.value, event.longValue);
466
467         if (REMOTE_SWITCH_COOKIE == (int)event.elementCookie) {
468             [remote setRemoteId: event.value];
469             [remote handleEventWithCookieString: @"19_" sumOfValues: 0];
470         } else {
471             if (((int)event.elementCookie)!=5) {
472                 sumOfValues+=event.value;
473                 [cookieString appendString:[NSString stringWithFormat:@"%d_", event.elementCookie]];
474             }
475         }
476     }
477
478     [remote handleEventWithCookieString: cookieString sumOfValues: sumOfValues];
479 }
480
481 @implementation AppleRemote (IOKitMethods)
482
483 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice {
484     io_name_t               className;
485     IOCFPlugInInterface**   plugInInterface = NULL;
486     HRESULT                 plugInResult = S_OK;
487     SInt32                  score = 0;
488     IOReturn                ioReturnValue = kIOReturnSuccess;
489
490     hidDeviceInterface = NULL;
491
492     ioReturnValue = IOObjectGetClass(hidDevice, className);
493
494     if (ioReturnValue != kIOReturnSuccess) {
495         msg_Err( VLCIntf, "Failed to get IOKit class name.");
496         return NULL;
497     }
498
499     ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice,
500                                                       kIOHIDDeviceUserClientTypeID,
501                                                       kIOCFPlugInInterfaceID,
502                                                       &plugInInterface,
503                                                       &score);
504     if (ioReturnValue == kIOReturnSuccess)
505     {
506         //Call a method of the intermediate plug-in to create the device interface
507         plugInResult = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &hidDeviceInterface);
508
509         if (plugInResult != S_OK) {
510             msg_Err( VLCIntf, "Couldn't create HID class device interface");
511         }
512         // Release
513         if (plugInInterface) (*plugInInterface)->Release(plugInInterface);
514     }
515     return hidDeviceInterface;
516 }
517
518 - (io_object_t) findAppleRemoteDevice {
519     CFMutableDictionaryRef hidMatchDictionary = NULL;
520     IOReturn ioReturnValue = kIOReturnSuccess;
521     io_iterator_t hidObjectIterator = 0;
522     io_object_t hidDevice = 0;
523
524     // Set up a matching dictionary to search the I/O Registry by class
525     // name for all HID class devices
526     hidMatchDictionary = IOServiceMatching(AppleRemoteDeviceName);
527
528     // Now search I/O Registry for matching devices.
529     ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
530
531     if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0)) {
532         hidDevice = IOIteratorNext(hidObjectIterator);
533     }
534
535     // release the iterator
536     IOObjectRelease(hidObjectIterator);
537
538     return hidDevice;
539 }
540
541 - (BOOL) initializeCookies {
542     IOHIDDeviceInterface122** handle = (IOHIDDeviceInterface122**)hidDeviceInterface;
543     IOHIDElementCookie      cookie;
544     long                    usage;
545     long                    usagePage;
546     id                      object;
547     NSArray*                elements = nil;
548     NSDictionary*           element;
549     IOReturn success;
550
551     if (!handle || !(*handle)) return NO;
552
553     /* Copy all elements, since we're grabbing most of the elements
554      * for this device anyway, and thus, it's faster to iterate them
555      * ourselves. When grabbing only one or two elements, a matching
556      * dictionary should be passed in here instead of NULL. */
557     success = (*handle)->copyMatchingElements(handle, NULL, (CFArrayRef*)&elements);
558
559     if (success == kIOReturnSuccess) {
560
561         [elements autorelease];
562         /*
563         cookies = calloc(NUMBER_OF_APPLE_REMOTE_ACTIONS, sizeof(IOHIDElementCookie));
564         memset(cookies, 0, sizeof(IOHIDElementCookie) * NUMBER_OF_APPLE_REMOTE_ACTIONS);
565         */
566         NSMutableArray *mutableAllCookies = [[NSMutableArray alloc] init];
567         NSUInteger elementCount = [elements count];
568         for (NSUInteger i=0; i< elementCount; i++) {
569             element = [elements objectAtIndex:i];
570
571             //Get cookie
572             object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementCookieKey) ];
573             if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
574             if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) continue;
575             cookie = (IOHIDElementCookie) [object longValue];
576
577             //Get usage
578             object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsageKey) ];
579             if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
580             usage = [object longValue];
581
582             //Get usage page
583             object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsagePageKey) ];
584             if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
585             usagePage = [object longValue];
586
587             [mutableAllCookies addObject: [NSNumber numberWithInt:(int)cookie]];
588         }
589         _allCookies = [[NSArray alloc] initWithArray: mutableAllCookies];
590         [mutableAllCookies release];
591     } else {
592         return NO;
593     }
594
595     return YES;
596 }
597
598 - (BOOL) openDevice {
599     HRESULT  result;
600
601     IOHIDOptionsType openMode = kIOHIDOptionsTypeNone;
602     if ([self openInExclusiveMode]) openMode = kIOHIDOptionsTypeSeizeDevice;
603     IOReturn ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, openMode);
604
605     if (ioReturnValue == KERN_SUCCESS) {
606         queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
607         if (queue) {
608             result = (*queue)->create(queue, 0, 12);    //depth: maximum number of elements in queue before oldest elements in queue begin to be lost.
609
610             NSUInteger cookieCount = [_allCookies count];
611             for(NSUInteger i=0; i<cookieCount; i++) {
612                 IOHIDElementCookie cookie = (IOHIDElementCookie)[[_allCookies objectAtIndex:i] intValue];
613                 (*queue)->addElement(queue, cookie, 0);
614             }
615
616             // add callback for async events
617             ioReturnValue = (*queue)->createAsyncEventSource(queue, &eventSource);
618             if (ioReturnValue == KERN_SUCCESS) {
619                 ioReturnValue = (*queue)->setEventCallout(queue,QueueCallbackFunction, self, NULL);
620                 if (ioReturnValue == KERN_SUCCESS) {
621                     CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
622                     //start data delivery to queue
623                     (*queue)->start(queue);
624                     return YES;
625                 } else {
626                     msg_Err( VLCIntf, "Error when setting event callout");
627                 }
628             } else {
629                 msg_Err( VLCIntf, "Error when creating async event source");
630             }
631         } else {
632             msg_Err( VLCIntf, "Error when opening HUD device");
633         }
634     }
635     return NO;
636 }
637
638 @end
639
640 @implementation AppleRemoteApplicationDelegate
641
642 - (id) initWithApplicationDelegate: (id) delegate {
643     if((self = [super init]))
644         applicationDelegate = [delegate retain];
645     return self;
646 }
647
648 - (void) dealloc {
649     [applicationDelegate release];
650     [super dealloc];
651 }
652
653 - (id) applicationDelegate {
654     return applicationDelegate;
655 }
656
657 - (void)applicationWillBecomeActive:(NSNotification *)aNotification {
658     if ([applicationDelegate respondsToSelector: @selector(applicationWillBecomeActive:)]) {
659         [applicationDelegate applicationWillBecomeActive: aNotification];
660     }
661 }
662 - (void)applicationDidBecomeActive:(NSNotification *)aNotification {
663     [[AppleRemote sharedRemote] setListeningToRemote: YES];
664
665     if ([applicationDelegate respondsToSelector: @selector(applicationDidBecomeActive:)]) {
666         [applicationDelegate applicationDidBecomeActive: aNotification];
667     }
668 }
669 - (void)applicationWillResignActive:(NSNotification *)aNotification {
670     [[AppleRemote sharedRemote] setListeningToRemote: NO];
671
672     if ([applicationDelegate respondsToSelector: @selector(applicationWillResignActive:)]) {
673         [applicationDelegate applicationWillResignActive: aNotification];
674     }
675 }
676 - (void)applicationDidResignActive:(NSNotification *)aNotification {
677     if ([applicationDelegate respondsToSelector: @selector(applicationDidResignActive:)]) {
678         [applicationDelegate applicationDidResignActive: aNotification];
679     }
680 }
681
682 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
683     NSMethodSignature* signature = [super methodSignatureForSelector: aSelector];
684     if (signature == nil && applicationDelegate != nil) {
685         signature = [applicationDelegate methodSignatureForSelector: aSelector];
686     }
687     return signature;
688 }
689
690 - (void)forwardInvocation:(NSInvocation *)invocation {
691     SEL aSelector = [invocation selector];
692
693     if (applicationDelegate==nil || [applicationDelegate respondsToSelector:aSelector]==NO) {
694         [super forwardInvocation: invocation];
695         return;
696     }
697
698     [invocation invokeWithTarget:applicationDelegate];
699 }
700 @end