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