bump product version to 4.1.6.2
[LibreOffice.git] / apple_remote / source / HIDRemoteControlDevice.m
blobc7c62635d137266e3caa68a69ab6046796e3ce55
1 /* -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*****************************************************************************
3  * HIDRemoteControlDevice.m
4  * RemoteControlWrapper
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  * Code modified and adapted to OpenOffice.org
10  * by Eric Bachard on 11.08.2008 under the same license
11  *
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:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
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
28  * THE SOFTWARE.
29  *
30  *****************************************************************************/
32 #import "HIDRemoteControlDevice.h"
34 #import <mach/mach.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) removeNotifcationObserver;
47 - (void) remoteControlAvailable:(NSNotification *)notification;
49 @end
51 @interface HIDRemoteControlDevice (IOKitMethods)
52 + (io_object_t) findRemoteDevice;
53 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice;
54 - (BOOL) initializeCookies;
55 - (BOOL) openDevice;
56 @end
58 @implementation HIDRemoteControlDevice
60 + (const char*) remoteControlDeviceName {
61         return "";
64 + (BOOL) isRemoteAvailable {
65         io_object_t hidDevice = [self findRemoteDevice];
66         if (hidDevice != 0) {
67                 IOObjectRelease(hidDevice);
68                 return YES;
69         } else {
70                 return NO;
71         }
74 - (id) initWithDelegate: (id) _remoteControlDelegate {
75         if ([[self class] isRemoteAvailable] == NO) return nil;
77         if ( (self = [super initWithDelegate: _remoteControlDelegate]) ) {
78                 openInExclusiveMode = YES;
79                 queue = NULL;
80                 hidDeviceInterface = NULL;
81                 cookieToButtonMapping = [[NSMutableDictionary alloc] init];
83                 [self setCookieMappingInDictionary: cookieToButtonMapping];
85                 NSEnumerator* enumerator = [cookieToButtonMapping objectEnumerator];
86                 NSNumber* identifier;
87                 supportedButtonEvents = 0;
88                 while( (identifier = [enumerator nextObject]) ) {
89                         supportedButtonEvents |= [identifier intValue];
90                 }
92                 fixSecureEventInputBug = [[NSUserDefaults standardUserDefaults] boolForKey: @"remoteControlWrapperFixSecureEventInputBug"];
93         }
95         return self;
98 - (void) dealloc {
99         [self removeNotifcationObserver];
100         [self stopListening:self];
101         [cookieToButtonMapping release];
102         [super dealloc];
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 {
114         return 0;
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 {
126         if (value == NO) {
127                 [self stopListening:self];
128         } else {
129                 [self startListening:self];
130         }
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 {
148     (void)sender;
149         if ([self isListeningToRemote]) return;
151         // 4th July 2007
152         //
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.
158         //
159         // Enabling the SecureEventInput and keeping it enabled does the trick.
160         //
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.
166         //
167         // Note that there is a corresponding DisableSecureEventInput in the stopListening method below.
168         //
169         if ([self isOpenInExclusiveMode] && fixSecureEventInputBug) EnableSecureEventInput();
171         [self removeNotifcationObserver];
173         io_object_t hidDevice = [[self class] findRemoteDevice];
174         if (hidDevice == 0) return;
176         if ([self createInterfaceForDevice:hidDevice] == NULL) {
177                 goto error;
178         }
180         if ([self initializeCookies]==NO) {
181                 goto error;
182         }
184         if ([self openDevice]==NO) {
185                 goto error;
186         }
187         // be KVO friendly
188         [self willChangeValueForKey:@"listeningToRemote"];
189         [self didChangeValueForKey:@"listeningToRemote"];
190         goto cleanup;
192 error:
193         [self stopListening:self];
194         DisableSecureEventInput();
196 cleanup:
197         IOObjectRelease(hidDevice);
200 - (void) stopListening: (id) sender {
201     (void)sender;
202         if ([self isListeningToRemote]==NO) return;
204         BOOL sendNotification = NO;
206         if (eventSource != NULL) {
207                 CFRunLoopRemoveSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
208                 CFRelease(eventSource);
209                 eventSource = NULL;
210         }
211         if (queue != NULL) {
212                 (*queue)->stop(queue);
214                 //dispose of queue
215                 (*queue)->dispose(queue);
217                 //release the queue we allocated
218                 (*queue)->Release(queue);
220                 queue = NULL;
222                 sendNotification = YES;
223         }
225         if (allCookies != nil) {
226                 [allCookies autorelease];
227                 allCookies = nil;
228         }
230         if (hidDeviceInterface != NULL) {
231                 //close the device
232                 (*hidDeviceInterface)->close(hidDeviceInterface);
234                 //release the interface
235                 (*hidDeviceInterface)->Release(hidDeviceInterface);
237                 hidDeviceInterface = NULL;
238         }
240         if ([self isOpenInExclusiveMode] && fixSecureEventInputBug) DisableSecureEventInput();
242         if ([self isOpenInExclusiveMode] && sendNotification) {
243                 [[self class] sendFinishedNotifcationForAppIdentifier: nil];
244         }
245         // be KVO friendly
246         [self willChangeValueForKey:@"listeningToRemote"];
247         [self didChangeValueForKey:@"listeningToRemote"];
250 @end
252 @implementation HIDRemoteControlDevice (PrivateMethods)
254 - (IOHIDQueueInterface**) queue {
255         return 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];
270         NSString* key;
271         while( (key = [keyEnum nextObject]) ) {
272                 NSRange range = [cookieString rangeOfString:key];
273                 if (range.location == 0) return key;
274         }
275         return nil;
278 - (void) handleEventWithCookieString: (NSString*) cookieString sumOfValues: (SInt32) sumOfValues {
279         /*
280         if (previousRemainingCookieString) {
281                 cookieString = [previousRemainingCookieString stringByAppendingString: cookieString];
282                 NSLog(@"New cookie string is %@", cookieString);
283                 [previousRemainingCookieString release], previousRemainingCookieString=nil;
284         }*/
285         if (cookieString == nil || [cookieString length] == 0) return;
287         NSNumber* buttonId = [[self cookieToButtonMapping] objectForKey: cookieString];
288         if (buttonId != nil) {
289        switch ( (int)buttonId )
290        {
291        case kMetallicRemote2009ButtonPlay:
292        case kMetallicRemote2009ButtonMiddlePlay:
293            buttonId = [NSNumber numberWithInt:kRemoteButtonPlay];
294            break;
295        default:
296            break;
297        }
298        [self sendRemoteButtonEvent: [buttonId intValue] pressedDown: (sumOfValues>0)];
300         } else {
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];
309                 }
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];
316                 }
317                 if ([cookieString length] > 0) {
318                         NSLog(@"Unknown button for cookiestring %@", cookieString);
319                 }
320         }
323 - (void) removeNotifcationObserver {
324         [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION object:nil];
327 - (void) remoteControlAvailable:(NSNotification *)notification {
328     (void)notification;
329         [self removeNotifcationObserver];
330         [self startListening: self];
333 @end
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) {
339     (void)refcon;
340     (void)sender;
341         if ((intptr_t)target < 0) {
342                 NSLog(@"QueueCallbackFunction called with invalid target!");
343                 return;
344         }
345         NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
347         HIDRemoteControlDevice* remote = (HIDRemoteControlDevice*)target;
348         IOHIDEventStruct event;
349         AbsoluteTime     zeroTime = {0,0};
350         NSMutableString* cookieString = [NSMutableString string];
351         SInt32                   sumOfValues = 0;
352         while (result == kIOReturnSuccess)
353         {
354                 result = (*[remote queue])->getNextEvent([remote queue], &event, zeroTime, 0);
355                 if ( result != kIOReturnSuccess )
356                         continue;
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]];
363                 }
364         }
365         [remote handleEventWithCookieString: cookieString sumOfValues: sumOfValues];
367         [pool release];
370 @implementation HIDRemoteControlDevice (IOKitMethods)
372 - (IOHIDDeviceInterface**) createInterfaceForDevice: (io_object_t) hidDevice {
373         io_name_t                               className;
374         IOCFPlugInInterface**   plugInInterface = NULL;
375         HRESULT                                 plugInResult = S_OK;
376         SInt32                                  score = 0;
377         IOReturn                                ioReturnValue = kIOReturnSuccess;
379         hidDeviceInterface = NULL;
381         ioReturnValue = IOObjectGetClass(hidDevice, className);
383         if (ioReturnValue != kIOReturnSuccess) {
384                 NSLog(@"Error: Failed to get class name.");
385                 return NULL;
386         }
388         ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice,
389                                                                                                           kIOHIDDeviceUserClientTypeID,
390                                                                                                           kIOCFPlugInInterfaceID,
391                                                                                                           &plugInInterface,
392                                                                                                           &score);
393         if (ioReturnValue == kIOReturnSuccess)
394         {
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(@"Error: Couldn't create HID class device interface");
400                 }
401                 // Release
402                 if (plugInInterface) (*plugInInterface)->Release(plugInInterface);
403         }
404         return hidDeviceInterface;
407 - (BOOL) initializeCookies {
408         IOHIDDeviceInterface122** handle = (IOHIDDeviceInterface122**)hidDeviceInterface;
409         IOHIDElementCookie              cookie;
410         long                                    usage;
411         long                                    usagePage;
412         id                                              object;
413         NSArray*                                elements = nil;
414         NSDictionary*                   element;
415         IOReturn success;
417         if (!handle || !(*handle)) return NO;
419         // Copy all elements, since we're grabbing most of the elements
420         // for this device anyway, and thus, it's faster to iterate them
421         // ourselves. When grabbing only one or two elements, a matching
422         // dictionary should be passed in here instead of NULL.
423         success = (*handle)->copyMatchingElements(handle, NULL, (CFArrayRef*)&elements);
425         if (success == kIOReturnSuccess) {
427                 [elements autorelease];
428                 /*
429                 cookies = calloc(NUMBER_OF_APPLE_REMOTE_ACTIONS, sizeof(IOHIDElementCookie));
430                 memset(cookies, 0, sizeof(IOHIDElementCookie) * NUMBER_OF_APPLE_REMOTE_ACTIONS);
431                 */
432                 allCookies = [[NSMutableArray alloc] init];
434                 NSEnumerator *elementsEnumerator = [elements objectEnumerator];
436                 while ( (element = [elementsEnumerator nextObject]) ) {
437                         //Get cookie
438                         object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementCookieKey) ];
439                         if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
440                         if (object == 0 || CFGetTypeID(object) != CFNumberGetTypeID()) continue;
441                         cookie = (IOHIDElementCookie) [object longValue];
443                         //Get usage
444                         object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsageKey) ];
445                         if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
446                         usage = [object longValue];
448                         //Get usage page
449                         object = [element valueForKey: (NSString*)CFSTR(kIOHIDElementUsagePageKey) ];
450                         if (object == nil || ![object isKindOfClass:[NSNumber class]]) continue;
451                         usagePage = [object longValue];
453                         [allCookies addObject: [NSNumber numberWithInt:(int)cookie]];
454                 }
455         } else {
456                 return NO;
457         }
459         return YES;
462 - (BOOL) openDevice {
463         HRESULT  result;
465         IOHIDOptionsType openMode = kIOHIDOptionsTypeNone;
466         if ([self isOpenInExclusiveMode]) openMode = kIOHIDOptionsTypeSeizeDevice;
467         IOReturn ioReturnValue = (*hidDeviceInterface)->open(hidDeviceInterface, openMode);
469         if (ioReturnValue == KERN_SUCCESS) {
470                 queue = (*hidDeviceInterface)->allocQueue(hidDeviceInterface);
471                 if (queue) {
472                         result = (*queue)->create(queue, 0, 12);        //depth: maximum number of elements in queue before oldest elements in queue begin to be lost.
474                         IOHIDElementCookie cookie;
475                         NSEnumerator *allCookiesEnumerator = [allCookies objectEnumerator];
477                         while ( (cookie = (IOHIDElementCookie)[[allCookiesEnumerator nextObject] intValue]) ) {
478                                 (*queue)->addElement(queue, cookie, 0);
479                         }
481                         // add callback for async events
482                         ioReturnValue = (*queue)->createAsyncEventSource(queue, &eventSource);
483                         if (ioReturnValue == KERN_SUCCESS) {
484                                 ioReturnValue = (*queue)->setEventCallout(queue,QueueCallbackFunction, self, NULL);
485                                 if (ioReturnValue == KERN_SUCCESS) {
486                                         CFRunLoopAddSource(CFRunLoopGetCurrent(), eventSource, kCFRunLoopDefaultMode);
488                                         //start data delivery to queue
489                                         (*queue)->start(queue);
490                                         return YES;
491                                 } else {
492                                         NSLog(@"Error when setting event callback");
493                                 }
494                         } else {
495                                 NSLog(@"Error when creating async event source");
496                         }
497                 } else {
498                         NSLog(@"Error when opening device");
499                 }
500         } else if (ioReturnValue == kIOReturnExclusiveAccess) {
501                 // the device is used exclusive by another application
503                 // 1. we register for the FINISHED_USING_REMOTE_CONTROL_NOTIFICATION notification
504                 [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(remoteControlAvailable:) name:FINISHED_USING_REMOTE_CONTROL_NOTIFICATION object:nil];
506                 // 2. send a distributed notification that we wanted to use the remote control
507                 [[self class] sendRequestForRemoteControlNotification];
508         }
509         return NO;
512 + (io_object_t) findRemoteDevice {
513         CFMutableDictionaryRef hidMatchDictionary = NULL;
514         IOReturn ioReturnValue = kIOReturnSuccess;
515         io_iterator_t hidObjectIterator = 0;
516         io_object_t     hidDevice = 0;
518         // Set up a matching dictionary to search the I/O Registry by class
519         // name for all HID class devices
520         hidMatchDictionary = IOServiceMatching([self remoteControlDeviceName]);
522         // Now search I/O Registry for matching devices.
523         ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
525         if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0)) {
526                 hidDevice = IOIteratorNext(hidObjectIterator);
527         }
529         // release the iterator
530         IOObjectRelease(hidObjectIterator);
532         return hidDevice;
535 @end
537 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */