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