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