1 /* -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*****************************************************************************
3 * HIDRemoteControlDevice.m
6 * Created by Martin Kahr on 11.03.06 under a MIT-style license.
7 * Copyright (c) 2006 martinkahr.com. All rights reserved.
9 * Code modified and adapted to OpenOffice.org
10 * by Eric Bachard on 11.08.2008 under the same license
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included
20 * in all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 *****************************************************************************/
32 #import "HIDRemoteControlDevice.h"
35 #import <mach/mach_error.h>
36 #import <IOKit/IOKitLib.h>
37 #import <IOKit/IOCFPlugIn.h>
38 #import <IOKit/hid/IOHIDKeys.h>
39 #import <Carbon/Carbon.h>
41 @interface HIDRemoteControlDevice (PrivateMethods)
42 - (NSDictionary*) cookieToButtonMapping; // Creates the dictionary using the magics, depending on the remote
43 - (IOHIDQueueInterface**) queue;
44 - (IOHIDDeviceInterface**) hidDeviceInterface;
45 - (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues;
46 - (void) removeNotificationObserver;
47 - (void) remoteControlAvailable:(NSNotification *)notification;
51 @interface HIDRemoteControlDevice (IOKitMethods)
52 + (io_object_t) findRemoteDevice;
53 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice;
54 - (BOOL) initializeCookies;
58 @implementation HIDRemoteControlDevice
60 + (const char*) remoteControlDeviceName {
64 + (BOOL) isRemoteAvailable {
65 io_object_t hidDevice = [self findRemoteDevice];
67 IOObjectRelease(hidDevice);
74 - (id) initWithDelegate: (id) _remoteControlDelegate {
75 if ([[self class] isRemoteAvailable] == NO) return nil;
77 if ( (self = [super initWithDelegate: _remoteControlDelegate]) ) {
78 openInExclusiveMode = YES;
80 hidDeviceInterface = NULL;
81 cookieToButtonMapping = [[NSMutableDictionary alloc] init];
83 [self setCookieMappingInDictionary: cookieToButtonMapping];
85 NSEnumerator* enumerator = [cookieToButtonMapping objectEnumerator];
87 supportedButtonEvents = 0;
88 while( (identifier = [enumerator nextObject]) ) {
89 supportedButtonEvents |= [identifier intValue];
92 fixSecureEventInputBug = [[NSUserDefaults standardUserDefaults] boolForKey: @"remoteControlWrapperFixSecureEventInputBug"];
99 [self removeNotificationObserver];
100 [self stopListening:self];
101 [cookieToButtonMapping release];
105 - (void) sendRemoteButtonEvent: (RemoteControlEventIdentifier) event pressedDown: (BOOL) pressedDown {
106 [delegate sendRemoteButtonEvent: event pressedDown: pressedDown remoteControl:self];
109 - (void) setCookieMappingInDictionary: (NSMutableDictionary*) cookieToButtonMap {
110 (void)cookieToButtonMap;
113 - (int) remoteIdSwitchCookie {
117 - (BOOL) sendsEventForButtonIdentifier: (RemoteControlEventIdentifier) identifier {
118 return (supportedButtonEvents & identifier) == identifier;
121 - (BOOL) isListeningToRemote {
122 return (hidDeviceInterface != NULL && allCookies != NULL && queue != NULL);
125 - (void) setListeningToRemote: (BOOL) value {
127 [self stopListening:self];
129 [self startListening:self];
133 - (BOOL) isOpenInExclusiveMode {
134 return openInExclusiveMode;
136 - (void) setOpenInExclusiveMode: (BOOL) value {
137 openInExclusiveMode = value;
140 - (BOOL) processesBacklog {
141 return processesBacklog;
143 - (void) setProcessesBacklog: (BOOL) value {
144 processesBacklog = value;
147 - (void) startListening: (id) sender {
149 if ([self isListeningToRemote]) return;
153 // A security update in february of 2007 introduced an odd behavior.
154 // Whenever SecureEventInput is activated or deactivated the exclusive access
155 // to the remote control device is lost. This leads to very strange behavior where
156 // a press on the Menu button activates FrontRow while your app still gets the event.
157 // A great number of people have complained about this.
159 // Enabling the SecureEventInput and keeping it enabled does the trick.
161 // I'm pretty sure this is a kind of bug at Apple and I'm in contact with the responsible
162 // Apple Engineer. This solution is not a perfect one - I know.
163 // One of the side effects is that applications that listen for special global keyboard shortcuts (like Quicksilver)
164 // may get into problems as they no longer get the events.
165 // As there is no official Apple Remote API from Apple I also failed to open a technical incident on this.
167 // Note that there is a corresponding DisableSecureEventInput in the stopListening method below.
169 if ([self isOpenInExclusiveMode] && fixSecureEventInputBug) EnableSecureEventInput();
171 [self removeNotificationObserver];
173 io_object_t hidDevice = [[self class] findRemoteDevice];
174 if (hidDevice == 0) return;
176 if ([self createInterfaceForDevice:hidDevice] == NULL) {
180 if ([self initializeCookies]==NO) {
184 if ([self openDevice]==NO) {
188 [self willChangeValueForKey:@"listeningToRemote"];
189 [self didChangeValueForKey:@"listeningToRemote"];
193 [self stopListening:self];
194 DisableSecureEventInput();
197 IOObjectRelease(hidDevice);
200 - (void) stopListening: (id) sender {
202 if ([self isListeningToRemote]==NO) return;
204 BOOL sendNotification = NO;
206 if (eventSource != NULL) {
207 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
208 CFRelease(eventSource);
212 (*queue)->stop(queue);
215 (*queue)->dispose(queue);
217 //release the queue we allocated
218 (*queue)->Release(queue);
222 sendNotification = YES;
225 if (allCookies != nil) {
226 [allCookies autorelease];
230 if (hidDeviceInterface != NULL) {
232 (*hidDeviceInterface)->close(hidDeviceInterface);
234 //release the interface
235 (*hidDeviceInterface)->Release(hidDeviceInterface);
237 hidDeviceInterface = NULL;
240 if ([self isOpenInExclusiveMode] && fixSecureEventInputBug) DisableSecureEventInput();
242 if ([self isOpenInExclusiveMode] && sendNotification) {
243 [[self class] sendFinishedNotificationForAppIdentifier: nil];
246 [self willChangeValueForKey:@"listeningToRemote"];
247 [self didChangeValueForKey:@"listeningToRemote"];
252 @implementation HIDRemoteControlDevice (PrivateMethods)
254 - (IOHIDQueueInterface**) queue {
258 - (IOHIDDeviceInterface**) hidDeviceInterface {
259 return hidDeviceInterface;
263 - (NSDictionary*) cookieToButtonMapping {
264 return cookieToButtonMapping;
267 - (NSString*) validCookieSubstring: (NSString*) cookieString {
268 if (cookieString == nil || [cookieString length] == 0) return nil;
269 NSEnumerator* keyEnum = [[self cookieToButtonMapping] keyEnumerator];
271 while( (key = [keyEnum nextObject]) ) {
272 NSRange range = [cookieString rangeOfString:key];
273 if (range.location == 0) return key;
278 - (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues {
280 if (previousRemainingCookieString) {
281 cookieString = [previousRemainingCookieString stringByAppendingString: cookieString];
282 NSLog( @"Apple Remote: New cookie string is %@", cookieString);
283 [previousRemainingCookieString release], previousRemainingCookieString=nil;
285 if (cookieString == nil || [cookieString length] == 0) return;
287 NSNumber* buttonId = [[self cookieToButtonMapping] objectForKey: cookieString];
288 if (buttonId != nil) {
289 switch ( [buttonId intValue] )
291 case kMetallicRemote2009ButtonPlay:
292 case kMetallicRemote2009ButtonMiddlePlay:
293 buttonId = [NSNumber numberWithInt:kRemoteButtonPlay];
298 [self sendRemoteButtonEvent: [buttonId intValue] pressedDown: (sumOfValues>0)];
301 // let's see if a number of events are stored in the cookie string. this does
302 // happen when the main thread is too busy to handle all incoming events in time.
303 NSString* subCookieString;
304 NSString* lastSubCookieString=nil;
305 while( (subCookieString = [self validCookieSubstring: cookieString]) ) {
306 cookieString = [cookieString substringFromIndex: [subCookieString length]];
307 lastSubCookieString = subCookieString;
308 if (processesBacklog) [self handleEventWithCookieString: subCookieString sumOfValues:sumOfValues];
310 if (processesBacklog == NO && lastSubCookieString != nil) {
311 // process the last event of the backlog and assume that the button is not pressed down any longer.
312 // The events in the backlog do not seem to be in order and therefore (in rare cases) the last event might be
313 // a button pressed down event while in reality the user has released it.
314 // NSLog(@"processing last event of backlog");
315 [self handleEventWithCookieString: lastSubCookieString sumOfValues:0];
317 if ([cookieString length] > 0) {
318 NSLog( @"Apple Remote: Unknown button for cookiestring %@", cookieString);
323 - (void) removeNotificationObserver {
324 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION object:nil];
327 - (void) remoteControlAvailable:(NSNotification *)notification {
329 [self removeNotificationObserver];
330 [self startListening: self];
335 /* Callback method for the device queue
336 Will be called for any event of any type (cookie) to which we subscribe
338 static void QueueCallbackFunction(void* target, IOReturn result, void* refcon, void* sender) {
341 if ((intptr_t)target < 0) {
342 NSLog( @"Apple Remote: QueueCallbackFunction called with invalid target!");
345 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
347 HIDRemoteControlDevice* remote = (HIDRemoteControlDevice*)target;
348 IOHIDEventStruct event;
349 AbsoluteTime const zeroTime = {0,0};
350 NSMutableString* cookieString = [NSMutableString string];
351 SInt32 sumOfValues = 0;
352 while (result == kIOReturnSuccess)
354 result = (*[remote queue])->getNextEvent([remote queue], &event, zeroTime, 0);
355 if ( result != kIOReturnSuccess )
358 //printf("%d %d %d\n", event.elementCookie, event.value, event.longValue);
360 if (((int)event.elementCookie)!=5) {
361 sumOfValues+=event.value;
362 [cookieString appendString:[NSString stringWithFormat:@"%lld_", (long long) (intptr_t) event.elementCookie]];
365 [remote handleEventWithCookieString: cookieString sumOfValues: sumOfValues];
370 @implementation HIDRemoteControlDevice (IOKitMethods)
372 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice {
374 IOCFPlugInInterface** plugInInterface = NULL;
375 HRESULT plugInResult = S_OK;
377 IOReturn ioReturnValue = kIOReturnSuccess;
379 hidDeviceInterface = NULL;
381 ioReturnValue = IOObjectGetClass(hidDevice, className);
383 if (ioReturnValue != kIOReturnSuccess) {
384 NSLog( @"Apple Remote: Error: Failed to get RemoteControlDevice class name.");
388 ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice,
389 kIOHIDDeviceUserClientTypeID,
390 kIOCFPlugInInterfaceID,
393 if (ioReturnValue == kIOReturnSuccess)
395 //Call a method of the intermediate plug-in to create the device interface
396 plugInResult = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &hidDeviceInterface);
398 if (plugInResult != S_OK) {
399 NSLog( @"Apple Remote: Error: Couldn't create HID class device interface");
402 if (plugInInterface) (*plugInInterface)->Release(plugInInterface);
404 return hidDeviceInterface;
407 - (BOOL) initializeCookies {
408 IOHIDDeviceInterface122** handle = (IOHIDDeviceInterface122**)hidDeviceInterface;
409 IOHIDElementCookie cookie;
411 NSArray* elements = nil;
412 NSDictionary* element;
415 if (!handle || !(*handle)) return NO;
417 // Copy all elements, since we're grabbing most of the elements
418 // for this device anyway, and thus, it's faster to iterate them
419 // ourselves. When grabbing only one or two elements, a matching
420 // dictionary should be passed in here instead of NULL.
421 success = (*handle)->copyMatchingElements(handle, NULL, (CFArrayRef*)&elements);
423 if (success == kIOReturnSuccess) {
426 cookies = calloc(NUMBER_OF_APPLE_REMOTE_ACTIONS, sizeof(IOHIDElementCookie));
427 memset(cookies, 0, sizeof(IOHIDElementCookie) * NUMBER_OF_APPLE_REMOTE_ACTIONS);
429 allCookies = [[NSMutableArray alloc] init];
431 NSEnumerator *elementsEnumerator = [elements objectEnumerator];
433 while ( (element = [elementsEnumerator nextObject]) ) {
435 object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementCookieKey) ];
436 if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
437 if (object == NULL || CFGetTypeID(object) != CFNumberGetTypeID()) continue;
438 cookie = (IOHIDElementCookie) [object longValue];
440 [allCookies addObject: [NSNumber numberWithInt:(int)cookie]];
451 - (BOOL) openDevice {
452 IOHIDOptionsType openMode = kIOHIDOptionsTypeNone;
453 if ([self isOpenInExclusiveMode]) openMode = kIOHIDOptionsTypeSeizeDevice;
454 IOReturn ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, openMode);
456 if (ioReturnValue == KERN_SUCCESS) {
457 queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
459 (*queue)->create(queue, 0, 12); //depth: maximum number of elements in queue before oldest elements in queue begin to be lost.
461 IOHIDElementCookie cookie;
462 NSEnumerator *allCookiesEnumerator = [allCookies objectEnumerator];
464 while ( (cookie = (IOHIDElementCookie)[[allCookiesEnumerator nextObject] intValue]) ) {
465 (*queue)->addElement(queue, cookie, 0);
468 // add callback for async events
469 ioReturnValue = (*queue)->createAsyncEventSource(queue, &eventSource);
470 if (ioReturnValue == KERN_SUCCESS) {
471 ioReturnValue = (*queue)->setEventCallout(queue,QueueCallbackFunction, self, NULL);
472 if (ioReturnValue == KERN_SUCCESS) {
473 CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
475 //start data delivery to queue
476 (*queue)->start(queue);
479 NSLog( @"Apple Remote: Error when setting event callback");
482 NSLog( @"Apple Remote: Error when creating async event source");
485 NSLog( @"Apple Remote: Error when opening device");
487 } else if (ioReturnValue == kIOReturnExclusiveAccess) {
488 // the device is used exclusive by another application
490 // 1. we register for the FINISHED_USING_REMOTE_CONTROL_NOTIFICATION notification
491 [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(remoteControlAvailable:) name:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION object:nil];
493 // 2. send a distributed notification that we wanted to use the remote control
494 [[self class] sendRequestForRemoteControlNotification];
499 + (io_object_t) findRemoteDevice {
500 CFMutableDictionaryRef hidMatchDictionary = NULL;
501 IOReturn ioReturnValue = kIOReturnSuccess;
502 io_iterator_t hidObjectIterator = 0;
503 io_object_t hidDevice = 0;
505 // Set up a matching dictionary to search the I/O Registry by class
506 // name for all HID class devices
507 hidMatchDictionary = IOServiceMatching([self remoteControlDeviceName]);
509 // Now search I/O Registry for matching devices.
510 SAL_WNODEPRECATED_DECLARATIONS_PUSH // kIOMasterPortDefault (12.0)
511 ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
512 SAL_WNODEPRECATED_DECLARATIONS_POP
514 if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0)) {
515 hidDevice = IOIteratorNext(hidObjectIterator);
518 // release the iterator
519 IOObjectRelease(hidObjectIterator);
526 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */