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