xref: /AOO41X/main/apple_remote/HIDRemoteControlDevice.m (revision 53e04a5dbf8709463ce908ac78248c6a889a9b40)
1cdf0e10cSrcweir/*****************************************************************************
2cdf0e10cSrcweir * HIDRemoteControlDevice.m
3cdf0e10cSrcweir * RemoteControlWrapper
4cdf0e10cSrcweir *
5cdf0e10cSrcweir * Created by Martin Kahr on 11.03.06 under a MIT-style license.
6cdf0e10cSrcweir * Copyright (c) 2006 martinkahr.com. All rights reserved.
7cdf0e10cSrcweir *
8cdf0e10cSrcweir * Code modified and adapted to OpenOffice.org
9cdf0e10cSrcweir * by Eric Bachard on 11.08.2008 under the same license
10cdf0e10cSrcweir *
11cdf0e10cSrcweir * Permission is hereby granted, free of charge, to any person obtaining a
12cdf0e10cSrcweir * copy of this software and associated documentation files (the "Software"),
13cdf0e10cSrcweir * to deal in the Software without restriction, including without limitation
14cdf0e10cSrcweir * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15cdf0e10cSrcweir * and/or sell copies of the Software, and to permit persons to whom the
16cdf0e10cSrcweir * Software is furnished to do so, subject to the following conditions:
17cdf0e10cSrcweir *
18cdf0e10cSrcweir * The above copyright notice and this permission notice shall be included
19cdf0e10cSrcweir * in all copies or substantial portions of the Software.
20cdf0e10cSrcweir *
21cdf0e10cSrcweir * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22cdf0e10cSrcweir * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23cdf0e10cSrcweir * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24cdf0e10cSrcweir * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25cdf0e10cSrcweir * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26cdf0e10cSrcweir * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27cdf0e10cSrcweir * THE SOFTWARE.
28cdf0e10cSrcweir *
29cdf0e10cSrcweir *****************************************************************************/
30cdf0e10cSrcweir
31cdf0e10cSrcweir#import "HIDRemoteControlDevice.h"
32cdf0e10cSrcweir
33cdf0e10cSrcweir#import <mach/mach.h>
34cdf0e10cSrcweir#import <mach/mach_error.h>
35cdf0e10cSrcweir#import <IOKit/IOKitLib.h>
36cdf0e10cSrcweir#import <IOKit/IOCFPlugIn.h>
37cdf0e10cSrcweir#import <IOKit/hid/IOHIDKeys.h>
38cdf0e10cSrcweir#import <Carbon/Carbon.h>
39cdf0e10cSrcweir
40cdf0e10cSrcweir@interface HIDRemoteControlDevice (PrivateMethods)
41cdf0e10cSrcweir- (NSDictionary*) cookieToButtonMapping; // Creates the dictionary using the magics, depending on the remote
42cdf0e10cSrcweir- (IOHIDQueueInterface**) queue;
43cdf0e10cSrcweir- (IOHIDDeviceInterface**) hidDeviceInterface;
44cdf0e10cSrcweir- (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues;
45cdf0e10cSrcweir- (void) removeNotifcationObserver;
46cdf0e10cSrcweir- (void) remoteControlAvailable:(NSNotification *)notification;
47cdf0e10cSrcweir
48cdf0e10cSrcweir@end
49cdf0e10cSrcweir
50cdf0e10cSrcweir@interface HIDRemoteControlDevice (IOKitMethods)
51cdf0e10cSrcweir+ (io_object_t) findRemoteDevice;
52cdf0e10cSrcweir- (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice;
53cdf0e10cSrcweir- (BOOL) initializeCookies;
54cdf0e10cSrcweir- (BOOL) openDevice;
55cdf0e10cSrcweir@end
56cdf0e10cSrcweir
57cdf0e10cSrcweir@implementation HIDRemoteControlDevice
58cdf0e10cSrcweir
59cdf0e10cSrcweir+ (const char*) remoteControlDeviceName {
60cdf0e10cSrcweir    return "";
61cdf0e10cSrcweir}
62cdf0e10cSrcweir
63cdf0e10cSrcweir+ (BOOL) isRemoteAvailable {
64cdf0e10cSrcweir    io_object_t hidDevice = [self findRemoteDevice];
65cdf0e10cSrcweir    if (hidDevice != 0) {
66cdf0e10cSrcweir        IOObjectRelease(hidDevice);
67cdf0e10cSrcweir        return YES;
68cdf0e10cSrcweir    } else {
69cdf0e10cSrcweir        return NO;
70cdf0e10cSrcweir    }
71cdf0e10cSrcweir}
72cdf0e10cSrcweir
73cdf0e10cSrcweir- (id) initWithDelegate: (id) _remoteControlDelegate {
74cdf0e10cSrcweir    if ([[self class] isRemoteAvailable] == NO) return nil;
75cdf0e10cSrcweir
76cdf0e10cSrcweir    if ( (self = [super initWithDelegate: _remoteControlDelegate]) ) {
77cdf0e10cSrcweir        openInExclusiveMode = YES;
78cdf0e10cSrcweir        queue = NULL;
79cdf0e10cSrcweir        hidDeviceInterface = NULL;
80cdf0e10cSrcweir        cookieToButtonMapping = [[NSMutableDictionary alloc] init];
81cdf0e10cSrcweir
82cdf0e10cSrcweir        [self setCookieMappingInDictionary: cookieToButtonMapping];
83cdf0e10cSrcweir
84cdf0e10cSrcweir        NSEnumerator* enumerator = [cookieToButtonMapping objectEnumerator];
85cdf0e10cSrcweir        NSNumber* identifier;
86cdf0e10cSrcweir        supportedButtonEvents = 0;
87cdf0e10cSrcweir        while( (identifier = [enumerator nextObject]) ) {
88cdf0e10cSrcweir            supportedButtonEvents |= [identifier intValue];
89cdf0e10cSrcweir        }
90cdf0e10cSrcweir
91cdf0e10cSrcweir        fixSecureEventInputBug = [[NSUserDefaults standardUserDefaults] boolForKey: @"remoteControlWrapperFixSecureEventInputBug"];
92cdf0e10cSrcweir    }
93cdf0e10cSrcweir
94cdf0e10cSrcweir    return self;
95cdf0e10cSrcweir}
96cdf0e10cSrcweir
97cdf0e10cSrcweir- (void) dealloc {
98cdf0e10cSrcweir    [self removeNotifcationObserver];
99cdf0e10cSrcweir    [self stopListening:self];
100cdf0e10cSrcweir    [cookieToButtonMapping release];
101cdf0e10cSrcweir    [super dealloc];
102cdf0e10cSrcweir}
103cdf0e10cSrcweir
104cdf0e10cSrcweir- (void) sendRemoteButtonEvent: (RemoteControlEventIdentifier) event pressedDown: (BOOL) pressedDown {
105cdf0e10cSrcweir    [delegate sendRemoteButtonEvent: event pressedDown: pressedDown remoteControl:self];
106cdf0e10cSrcweir}
107cdf0e10cSrcweir
108cdf0e10cSrcweir- (void) setCookieMappingInDictionary: (NSMutableDictionary*) cookieToButtonMapping {
109cdf0e10cSrcweir}
110cdf0e10cSrcweir- (int) remoteIdSwitchCookie {
111cdf0e10cSrcweir    return 0;
112cdf0e10cSrcweir}
113cdf0e10cSrcweir
114cdf0e10cSrcweir- (BOOL) sendsEventForButtonIdentifier: (RemoteControlEventIdentifier) identifier {
115cdf0e10cSrcweir    return (supportedButtonEvents & identifier) == identifier;
116cdf0e10cSrcweir}
117cdf0e10cSrcweir
118cdf0e10cSrcweir- (BOOL) isListeningToRemote {
119cdf0e10cSrcweir    return (hidDeviceInterface != NULL && allCookies != NULL && queue != NULL);
120cdf0e10cSrcweir}
121cdf0e10cSrcweir
122cdf0e10cSrcweir- (void) setListeningToRemote: (BOOL) value {
123cdf0e10cSrcweir    if (value == NO) {
124cdf0e10cSrcweir        [self stopListening:self];
125cdf0e10cSrcweir    } else {
126cdf0e10cSrcweir        [self startListening:self];
127cdf0e10cSrcweir    }
128cdf0e10cSrcweir}
129cdf0e10cSrcweir
130cdf0e10cSrcweir- (BOOL) isOpenInExclusiveMode {
131cdf0e10cSrcweir    return openInExclusiveMode;
132cdf0e10cSrcweir}
133cdf0e10cSrcweir- (void) setOpenInExclusiveMode: (BOOL) value {
134cdf0e10cSrcweir    openInExclusiveMode = value;
135cdf0e10cSrcweir}
136cdf0e10cSrcweir
137cdf0e10cSrcweir- (BOOL) processesBacklog {
138cdf0e10cSrcweir    return processesBacklog;
139cdf0e10cSrcweir}
140cdf0e10cSrcweir- (void) setProcessesBacklog: (BOOL) value {
141cdf0e10cSrcweir    processesBacklog = value;
142cdf0e10cSrcweir}
143cdf0e10cSrcweir
144cdf0e10cSrcweir- (void) startListening: (id) sender {
145cdf0e10cSrcweir    if ([self isListeningToRemote]) return;
146cdf0e10cSrcweir
147cdf0e10cSrcweir    // 4th July 2007
148cdf0e10cSrcweir    //
149cdf0e10cSrcweir    // A security update in february of 2007 introduced an odd behavior.
150cdf0e10cSrcweir    // Whenever SecureEventInput is activated or deactivated the exclusive access
151cdf0e10cSrcweir    // to the remote control device is lost. This leads to very strange behavior where
152cdf0e10cSrcweir    // a press on the Menu button activates FrontRow while your app still gets the event.
153cdf0e10cSrcweir    // A great number of people have complained about this.
154cdf0e10cSrcweir    //
155cdf0e10cSrcweir    // Enabling the SecureEventInput and keeping it enabled does the trick.
156cdf0e10cSrcweir    //
157cdf0e10cSrcweir    // I'm pretty sure this is a kind of bug at Apple and I'm in contact with the responsible
158cdf0e10cSrcweir    // Apple Engineer. This solution is not a perfect one - I know.
159cdf0e10cSrcweir    // One of the side effects is that applications that listen for special global keyboard shortcuts (like Quicksilver)
160cdf0e10cSrcweir    // may get into problems as they no longer get the events.
161cdf0e10cSrcweir    // As there is no official Apple Remote API from Apple I also failed to open a technical incident on this.
162cdf0e10cSrcweir    //
163cdf0e10cSrcweir    // Note that there is a corresponding DisableSecureEventInput in the stopListening method below.
164cdf0e10cSrcweir    //
165cdf0e10cSrcweir    if ([self isOpenInExclusiveMode] && fixSecureEventInputBug) EnableSecureEventInput();
166cdf0e10cSrcweir
167cdf0e10cSrcweir    [self removeNotifcationObserver];
168cdf0e10cSrcweir
169cdf0e10cSrcweir    io_object_t hidDevice = [[self class] findRemoteDevice];
170cdf0e10cSrcweir    if (hidDevice == 0) return;
171cdf0e10cSrcweir
172cdf0e10cSrcweir    if ([self createInterfaceForDevice:hidDevice] == NULL) {
173cdf0e10cSrcweir        goto error;
174cdf0e10cSrcweir    }
175cdf0e10cSrcweir
176cdf0e10cSrcweir    if ([self initializeCookies]==NO) {
177cdf0e10cSrcweir        goto error;
178cdf0e10cSrcweir    }
179cdf0e10cSrcweir
180cdf0e10cSrcweir    if ([self openDevice]==NO) {
181cdf0e10cSrcweir        goto error;
182cdf0e10cSrcweir    }
183cdf0e10cSrcweir    // be KVO friendly
184cdf0e10cSrcweir    [self willChangeValueForKey:@"listeningToRemote"];
185cdf0e10cSrcweir    [self didChangeValueForKey:@"listeningToRemote"];
186cdf0e10cSrcweir    goto cleanup;
187cdf0e10cSrcweir
188cdf0e10cSrcweirerror:
189cdf0e10cSrcweir    [self stopListening:self];
190cdf0e10cSrcweir    DisableSecureEventInput();
191cdf0e10cSrcweir
192cdf0e10cSrcweircleanup:
193cdf0e10cSrcweir    IOObjectRelease(hidDevice);
194cdf0e10cSrcweir}
195cdf0e10cSrcweir
196cdf0e10cSrcweir- (void) stopListening: (id) sender {
197cdf0e10cSrcweir    if ([self isListeningToRemote]==NO) return;
198cdf0e10cSrcweir
199cdf0e10cSrcweir    BOOL sendNotification = NO;
200cdf0e10cSrcweir
201cdf0e10cSrcweir    if (eventSource != NULL) {
202cdf0e10cSrcweir        CFRunLoopRemoveSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
203cdf0e10cSrcweir        CFRelease(eventSource);
204cdf0e10cSrcweir        eventSource = NULL;
205cdf0e10cSrcweir    }
206cdf0e10cSrcweir    if (queue != NULL) {
207cdf0e10cSrcweir        (*queue)->stop(queue);
208cdf0e10cSrcweir
209cdf0e10cSrcweir        //dispose of queue
210cdf0e10cSrcweir        (*queue)->dispose(queue);
211cdf0e10cSrcweir
212cdf0e10cSrcweir        //release the queue we allocated
213cdf0e10cSrcweir        (*queue)->Release(queue);
214cdf0e10cSrcweir
215cdf0e10cSrcweir        queue = NULL;
216cdf0e10cSrcweir
217cdf0e10cSrcweir        sendNotification = YES;
218cdf0e10cSrcweir    }
219cdf0e10cSrcweir
220cdf0e10cSrcweir    if (allCookies != nil) {
221cdf0e10cSrcweir        [allCookies autorelease];
222cdf0e10cSrcweir        allCookies = nil;
223cdf0e10cSrcweir    }
224cdf0e10cSrcweir
225cdf0e10cSrcweir    if (hidDeviceInterface != NULL) {
226cdf0e10cSrcweir        //close the device
227cdf0e10cSrcweir        (*hidDeviceInterface)->close(hidDeviceInterface);
228cdf0e10cSrcweir
229cdf0e10cSrcweir        //release the interface
230cdf0e10cSrcweir        (*hidDeviceInterface)->Release(hidDeviceInterface);
231cdf0e10cSrcweir
232cdf0e10cSrcweir        hidDeviceInterface = NULL;
233cdf0e10cSrcweir    }
234cdf0e10cSrcweir
235cdf0e10cSrcweir    if ([self isOpenInExclusiveMode] && fixSecureEventInputBug) DisableSecureEventInput();
236cdf0e10cSrcweir
237cdf0e10cSrcweir    if ([self isOpenInExclusiveMode] && sendNotification) {
238cdf0e10cSrcweir        [[self class] sendFinishedNotifcationForAppIdentifier: nil];
239cdf0e10cSrcweir    }
240cdf0e10cSrcweir    // be KVO friendly
241cdf0e10cSrcweir    [self willChangeValueForKey:@"listeningToRemote"];
242cdf0e10cSrcweir    [self didChangeValueForKey:@"listeningToRemote"];
243cdf0e10cSrcweir}
244cdf0e10cSrcweir
245cdf0e10cSrcweir@end
246cdf0e10cSrcweir
247cdf0e10cSrcweir@implementation HIDRemoteControlDevice (PrivateMethods)
248cdf0e10cSrcweir
249cdf0e10cSrcweir- (IOHIDQueueInterface**) queue {
250cdf0e10cSrcweir    return queue;
251cdf0e10cSrcweir}
252cdf0e10cSrcweir
253cdf0e10cSrcweir- (IOHIDDeviceInterface**) hidDeviceInterface {
254cdf0e10cSrcweir    return hidDeviceInterface;
255cdf0e10cSrcweir}
256cdf0e10cSrcweir
257cdf0e10cSrcweir
258cdf0e10cSrcweir- (NSDictionary*) cookieToButtonMapping {
259cdf0e10cSrcweir    return cookieToButtonMapping;
260cdf0e10cSrcweir}
261cdf0e10cSrcweir
262cdf0e10cSrcweir- (NSString*) validCookieSubstring: (NSString*) cookieString {
263cdf0e10cSrcweir    if (cookieString == nil || [cookieString length] == 0) return nil;
264cdf0e10cSrcweir    NSEnumerator* keyEnum = [[self cookieToButtonMapping] keyEnumerator];
265cdf0e10cSrcweir    NSString* key;
266cdf0e10cSrcweir    while( (key = [keyEnum nextObject]) ) {
267cdf0e10cSrcweir        NSRange range = [cookieString rangeOfString:key];
268cdf0e10cSrcweir        if (range.location == 0) return key;
269cdf0e10cSrcweir    }
270cdf0e10cSrcweir    return nil;
271cdf0e10cSrcweir}
272cdf0e10cSrcweir
273cdf0e10cSrcweir- (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues {
274cdf0e10cSrcweir    /*
275cdf0e10cSrcweir    if (previousRemainingCookieString) {
276cdf0e10cSrcweir        cookieString = [previousRemainingCookieString stringByAppendingString: cookieString];
277*53e04a5dSHerbert Dürr        NSLog( @"Apple Remote: New cookie string is %@", cookieString);
278cdf0e10cSrcweir        [previousRemainingCookieString release], previousRemainingCookieString=nil;
279cdf0e10cSrcweir    }*/
280cdf0e10cSrcweir    if (cookieString == nil || [cookieString length] == 0) return;
281cdf0e10cSrcweir
282cdf0e10cSrcweir    NSNumber* buttonId = [[self cookieToButtonMapping] objectForKey: cookieString];
283cdf0e10cSrcweir    if (buttonId != nil) {
284eace47cdSEric Bachard        switch ( (int)buttonId )
285eace47cdSEric Bachard        {
286eace47cdSEric Bachard        case kMetallicRemote2009ButtonPlay:
287eace47cdSEric Bachard        case kMetallicRemote2009ButtonMiddlePlay:
288eace47cdSEric Bachard            buttonId = [NSNumber numberWithInt:kRemoteButtonPlay];
289eace47cdSEric Bachard            break;
290eace47cdSEric Bachard        default:
291eace47cdSEric Bachard            break;
292eace47cdSEric Bachard        }
293cdf0e10cSrcweir        [self sendRemoteButtonEvent: [buttonId intValue] pressedDown: (sumOfValues>0)];
294eace47cdSEric Bachard
295cdf0e10cSrcweir    } else {
296cdf0e10cSrcweir        // let's see if a number of events are stored in the cookie string. this does
297cdf0e10cSrcweir        // happen when the main thread is too busy to handle all incoming events in time.
298cdf0e10cSrcweir        NSString* subCookieString;
299cdf0e10cSrcweir        NSString* lastSubCookieString=nil;
300cdf0e10cSrcweir        while( (subCookieString = [self validCookieSubstring: cookieString]) ) {
301cdf0e10cSrcweir        cookieString = [cookieString substringFromIndex: [subCookieString length]];
302cdf0e10cSrcweir        lastSubCookieString = subCookieString;
303cdf0e10cSrcweir        if (processesBacklog) [self handleEventWithCookieString: subCookieString sumOfValues:sumOfValues];
304cdf0e10cSrcweir        }
305cdf0e10cSrcweir        if (processesBacklog == NO && lastSubCookieString != nil) {
306cdf0e10cSrcweir        // process the last event of the backlog and assume that the button is not pressed down any longer.
307cdf0e10cSrcweir        // The events in the backlog do not seem to be in order and therefore (in rare cases) the last event might be
308cdf0e10cSrcweir        // a button pressed down event while in reality the user has released it.
309cdf0e10cSrcweir        // NSLog(@"processing last event of backlog");
310cdf0e10cSrcweir        [self handleEventWithCookieString: lastSubCookieString sumOfValues:0];
311cdf0e10cSrcweir        }
312cdf0e10cSrcweir        if ([cookieString length] > 0) {
313*53e04a5dSHerbert Dürr        NSLog( @"Apple Remote: Unknown button for cookiestring %@", cookieString);
314cdf0e10cSrcweir        }
315cdf0e10cSrcweir    }
316cdf0e10cSrcweir}
317cdf0e10cSrcweir
318cdf0e10cSrcweir- (void) removeNotifcationObserver {
319cdf0e10cSrcweir    [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION object:nil];
320cdf0e10cSrcweir}
321cdf0e10cSrcweir
322cdf0e10cSrcweir- (void) remoteControlAvailable:(NSNotification *)notification {
323cdf0e10cSrcweir    [self removeNotifcationObserver];
324cdf0e10cSrcweir    [self startListening: self];
325cdf0e10cSrcweir}
326cdf0e10cSrcweir
327cdf0e10cSrcweir@end
328cdf0e10cSrcweir
329cdf0e10cSrcweir/*  Callback method for the device queue
330cdf0e10cSrcweirWill be called for any event of any type (cookie) to which we subscribe
331cdf0e10cSrcweir*/
332cdf0e10cSrcweirstatic void QueueCallbackFunction(void* target,  IOReturn result, void* refcon, void* sender) {
333cdf0e10cSrcweir    if (target < 0) {
334*53e04a5dSHerbert Dürr        NSLog( @"Apple Remote: QueueCallbackFunction called with invalid target!");
335cdf0e10cSrcweir        return;
336cdf0e10cSrcweir    }
337cdf0e10cSrcweir    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
338cdf0e10cSrcweir
339cdf0e10cSrcweir    HIDRemoteControlDevice* remote = (HIDRemoteControlDevice*)target;
340cdf0e10cSrcweir    IOHIDEventStruct event;
341cdf0e10cSrcweir    AbsoluteTime     zeroTime = {0,0};
342cdf0e10cSrcweir    NSMutableString* cookieString = [NSMutableString string];
343cdf0e10cSrcweir    SInt32           sumOfValues = 0;
344cdf0e10cSrcweir    while (result == kIOReturnSuccess)
345cdf0e10cSrcweir    {
346cdf0e10cSrcweir        result = (*[remote queue])->getNextEvent([remote queue], &event, zeroTime, 0);
347cdf0e10cSrcweir        if ( result != kIOReturnSuccess )
348cdf0e10cSrcweir            continue;
349cdf0e10cSrcweir
350cdf0e10cSrcweir        //printf("%d %d %d\n", event.elementCookie, event.value, event.longValue);
351cdf0e10cSrcweir
352cdf0e10cSrcweir        if (((int)event.elementCookie)!=5) {
353cdf0e10cSrcweir            sumOfValues+=event.value;
354cdf0e10cSrcweir            [cookieString appendString:[NSString stringWithFormat:@"%d_", event.elementCookie]];
355cdf0e10cSrcweir        }
356cdf0e10cSrcweir    }
357cdf0e10cSrcweir    [remote handleEventWithCookieString: cookieString sumOfValues: sumOfValues];
358cdf0e10cSrcweir
359cdf0e10cSrcweir    [pool release];
360cdf0e10cSrcweir}
361cdf0e10cSrcweir
362cdf0e10cSrcweir@implementation HIDRemoteControlDevice (IOKitMethods)
363cdf0e10cSrcweir
364cdf0e10cSrcweir- (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice {
365cdf0e10cSrcweir    io_name_t               className;
366cdf0e10cSrcweir    IOCFPlugInInterface**   plugInInterface = NULL;
367cdf0e10cSrcweir    HRESULT                 plugInResult = S_OK;
368cdf0e10cSrcweir    SInt32                  score = 0;
369cdf0e10cSrcweir    IOReturn                ioReturnValue = kIOReturnSuccess;
370cdf0e10cSrcweir
371cdf0e10cSrcweir    hidDeviceInterface = NULL;
372cdf0e10cSrcweir
373cdf0e10cSrcweir    ioReturnValue = IOObjectGetClass(hidDevice, className);
374cdf0e10cSrcweir
375cdf0e10cSrcweir    if (ioReturnValue != kIOReturnSuccess) {
376*53e04a5dSHerbert Dürr        NSLog( @"Apple Remote: Error: Failed to get RemoteControlDevice class name.");
377cdf0e10cSrcweir        return NULL;
378cdf0e10cSrcweir    }
379cdf0e10cSrcweir
380cdf0e10cSrcweir    ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice,
381cdf0e10cSrcweir                                                      kIOHIDDeviceUserClientTypeID,
382cdf0e10cSrcweir                                                      kIOCFPlugInInterfaceID,
383cdf0e10cSrcweir                                                      &plugInInterface,
384cdf0e10cSrcweir                                                      &score);
385cdf0e10cSrcweir    if (ioReturnValue == kIOReturnSuccess)
386cdf0e10cSrcweir    {
387cdf0e10cSrcweir        //Call a method of the intermediate plug-in to create the device interface
388cdf0e10cSrcweir        plugInResult = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &hidDeviceInterface);
389cdf0e10cSrcweir
390cdf0e10cSrcweir        if (plugInResult != S_OK) {
391*53e04a5dSHerbert Dürr            NSLog( @"Apple Remote: Error: Couldn't create HID class device interface");
392cdf0e10cSrcweir        }
393cdf0e10cSrcweir        // Release
394cdf0e10cSrcweir        if (plugInInterface) (*plugInInterface)->Release(plugInInterface);
395cdf0e10cSrcweir    }
396cdf0e10cSrcweir    return hidDeviceInterface;
397cdf0e10cSrcweir}
398cdf0e10cSrcweir
399cdf0e10cSrcweir- (BOOL) initializeCookies {
400cdf0e10cSrcweir    IOHIDDeviceInterface122** handle = (IOHIDDeviceInterface122**)hidDeviceInterface;
401cdf0e10cSrcweir    IOHIDElementCookie      cookie;
402cdf0e10cSrcweir    long                    usage;
403cdf0e10cSrcweir    long                    usagePage;
404cdf0e10cSrcweir    id                      object;
405cdf0e10cSrcweir    NSArray*                elements = nil;
406cdf0e10cSrcweir    NSDictionary*           element;
407cdf0e10cSrcweir    IOReturn success;
408cdf0e10cSrcweir
409cdf0e10cSrcweir    if (!handle || !(*handle)) return NO;
410cdf0e10cSrcweir
411cdf0e10cSrcweir    // Copy all elements, since we're grabbing most of the elements
412cdf0e10cSrcweir    // for this device anyway, and thus, it's faster to iterate them
413cdf0e10cSrcweir    // ourselves. When grabbing only one or two elements, a matching
414cdf0e10cSrcweir    // dictionary should be passed in here instead of NULL.
415cdf0e10cSrcweir    success = (*handle)->copyMatchingElements(handle, NULL, (CFArrayRef*)&elements);
416cdf0e10cSrcweir
417cdf0e10cSrcweir    if (success == kIOReturnSuccess) {
418cdf0e10cSrcweir
419cdf0e10cSrcweir        [elements autorelease];
420cdf0e10cSrcweir        /*
421cdf0e10cSrcweir        cookies = calloc(NUMBER_OF_APPLE_REMOTE_ACTIONS, sizeof(IOHIDElementCookie));
422cdf0e10cSrcweir        memset(cookies, 0, sizeof(IOHIDElementCookie) * NUMBER_OF_APPLE_REMOTE_ACTIONS);
423cdf0e10cSrcweir        */
424cdf0e10cSrcweir        allCookies = [[NSMutableArray alloc] init];
425cdf0e10cSrcweir
426cdf0e10cSrcweir        NSEnumerator *elementsEnumerator = [elements objectEnumerator];
427cdf0e10cSrcweir
428cdf0e10cSrcweir        while ( (element = [elementsEnumerator nextObject]) ) {
429cdf0e10cSrcweir            //Get cookie
430cdf0e10cSrcweir            object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementCookieKey) ];
431cdf0e10cSrcweir            if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
432cdf0e10cSrcweir            if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) continue;
433cdf0e10cSrcweir            cookie = (IOHIDElementCookie) [object longValue];
434cdf0e10cSrcweir
435cdf0e10cSrcweir            //Get usage
436cdf0e10cSrcweir            object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsageKey) ];
437cdf0e10cSrcweir            if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
438cdf0e10cSrcweir            usage = [object longValue];
439cdf0e10cSrcweir
440cdf0e10cSrcweir            //Get usage page
441cdf0e10cSrcweir            object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsagePageKey) ];
442cdf0e10cSrcweir            if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
443cdf0e10cSrcweir            usagePage = [object longValue];
444cdf0e10cSrcweir
445cdf0e10cSrcweir            [allCookies addObject: [NSNumber numberWithInt:(int)cookie]];
446cdf0e10cSrcweir        }
447cdf0e10cSrcweir    } else {
448cdf0e10cSrcweir        return NO;
449cdf0e10cSrcweir    }
450cdf0e10cSrcweir
451cdf0e10cSrcweir    return YES;
452cdf0e10cSrcweir}
453cdf0e10cSrcweir
454cdf0e10cSrcweir- (BOOL) openDevice {
455cdf0e10cSrcweir    HRESULT  result;
456cdf0e10cSrcweir
457cdf0e10cSrcweir    IOHIDOptionsType openMode = kIOHIDOptionsTypeNone;
458cdf0e10cSrcweir    if ([self isOpenInExclusiveMode]) openMode = kIOHIDOptionsTypeSeizeDevice;
459cdf0e10cSrcweir    IOReturn ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, openMode);
460cdf0e10cSrcweir
461cdf0e10cSrcweir    if (ioReturnValue == KERN_SUCCESS) {
462cdf0e10cSrcweir        queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
463cdf0e10cSrcweir        if (queue) {
464cdf0e10cSrcweir            result = (*queue)->create(queue, 0, 12);    //depth: maximum number of elements in queue before oldest elements in queue begin to be lost.
465cdf0e10cSrcweir
466cdf0e10cSrcweir            IOHIDElementCookie cookie;
467cdf0e10cSrcweir            NSEnumerator *allCookiesEnumerator = [allCookies objectEnumerator];
468cdf0e10cSrcweir
469cdf0e10cSrcweir            while ( (cookie = (IOHIDElementCookie)[[allCookiesEnumerator nextObject] intValue]) ) {
470cdf0e10cSrcweir                (*queue)->addElement(queue, cookie, 0);
471cdf0e10cSrcweir            }
472cdf0e10cSrcweir
473cdf0e10cSrcweir            // add callback for async events
474cdf0e10cSrcweir            ioReturnValue = (*queue)->createAsyncEventSource(queue, &eventSource);
475cdf0e10cSrcweir            if (ioReturnValue == KERN_SUCCESS) {
476cdf0e10cSrcweir                ioReturnValue = (*queue)->setEventCallout(queue,QueueCallbackFunction, self, NULL);
477cdf0e10cSrcweir                if (ioReturnValue == KERN_SUCCESS) {
478cdf0e10cSrcweir                    CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
479cdf0e10cSrcweir
480cdf0e10cSrcweir                    //start data delivery to queue
481cdf0e10cSrcweir                    (*queue)->start(queue);
482cdf0e10cSrcweir                    return YES;
483cdf0e10cSrcweir                } else {
484*53e04a5dSHerbert Dürr                    NSLog( @"Apple Remote: Error when setting event callback");
485cdf0e10cSrcweir                }
486cdf0e10cSrcweir            } else {
487*53e04a5dSHerbert Dürr                NSLog( @"Apple Remote: Error when creating async event source");
488cdf0e10cSrcweir            }
489cdf0e10cSrcweir        } else {
490*53e04a5dSHerbert Dürr            NSLog( @"Apple Remote: Error when opening device");
491cdf0e10cSrcweir        }
492cdf0e10cSrcweir    } else if (ioReturnValue == kIOReturnExclusiveAccess) {
493cdf0e10cSrcweir        // the device is used exclusive by another application
494cdf0e10cSrcweir
495cdf0e10cSrcweir        // 1. we register for the FINISHED_USING_REMOTE_CONTROL_NOTIFICATION notification
496cdf0e10cSrcweir        [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(remoteControlAvailable:) name:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION object:nil];
497cdf0e10cSrcweir
498cdf0e10cSrcweir        // 2. send a distributed notification that we wanted to use the remote control
499cdf0e10cSrcweir        [[self class] sendRequestForRemoteControlNotification];
500cdf0e10cSrcweir    }
501cdf0e10cSrcweir    return NO;
502cdf0e10cSrcweir}
503cdf0e10cSrcweir
504cdf0e10cSrcweir+ (io_object_t) findRemoteDevice {
505cdf0e10cSrcweir    CFMutableDictionaryRef hidMatchDictionary = NULL;
506cdf0e10cSrcweir    IOReturn ioReturnValue = kIOReturnSuccess;
507cdf0e10cSrcweir    io_iterator_t hidObjectIterator = 0;
508cdf0e10cSrcweir    io_object_t hidDevice = 0;
509cdf0e10cSrcweir
510cdf0e10cSrcweir    // Set up a matching dictionary to search the I/O Registry by class
511cdf0e10cSrcweir    // name for all HID class devices
512cdf0e10cSrcweir    hidMatchDictionary = IOServiceMatching([self remoteControlDeviceName]);
513cdf0e10cSrcweir
514cdf0e10cSrcweir    // Now search I/O Registry for matching devices.
515cdf0e10cSrcweir    ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
516cdf0e10cSrcweir
517cdf0e10cSrcweir    if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0)) {
518cdf0e10cSrcweir        hidDevice = IOIteratorNext(hidObjectIterator);
519cdf0e10cSrcweir    }
520cdf0e10cSrcweir
521cdf0e10cSrcweir    // release the iterator
522cdf0e10cSrcweir    IOObjectRelease(hidObjectIterator);
523cdf0e10cSrcweir
524cdf0e10cSrcweir    return hidDevice;
525cdf0e10cSrcweir}
526cdf0e10cSrcweir
527cdf0e10cSrcweir@end
528cdf0e10cSrcweir
529