1/*****************************************************************************
2 * KeyspanFrontRowControl.m
3 * RemoteControlWrapper
4 *
5 * Created by Martin Kahr on 11.03.06 under a MIT-style license.
6 * Copyright (c) 2006 martinkahr.com. All rights reserved.
7 *
8 * Code modified and adapted to OpenOffice.org
9 * by Eric Bachard on 11.08.2008 under the same License
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 *****************************************************************************/
30
31#import "KeyspanFrontRowControl.h"
32#import <mach/mach.h>
33#import <mach/mach_error.h>
34#import <IOKit/IOKitLib.h>
35#import <IOKit/IOCFPlugIn.h>
36#import <IOKit/hid/IOHIDKeys.h>
37
38@implementation KeyspanFrontRowControl
39
40- (void) setCookieMappingInDictionary: (NSMutableDictionary*) _cookieToButtonMapping	{
41	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlus]		 forKey:@"11_18_99_10_"];
42	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMinus]		 forKey:@"11_18_98_10_"];
43	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu]		 forKey:@"11_18_58_10_"];
44	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay]		 forKey:@"11_18_61_10_"];
45	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight]		 forKey:@"11_18_96_10_"];
46	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft]		 forKey:@"11_18_97_10_"];
47	/* hold events are not being send by this device
48	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold]	forKey:@"14_6_4_2_"];
49	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold]	forKey:@"14_6_3_2_"];
50	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold]	forKey:@"14_6_14_6_"];
51	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Sleep]	forKey:@"18_14_6_18_14_6_"];
52	[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]	forKey:@"19_"];
53	*/
54}
55
56+ (io_object_t) findRemoteDevice {
57	CFMutableDictionaryRef hidMatchDictionary = NULL;
58	IOReturn ioReturnValue = kIOReturnSuccess;
59	io_iterator_t hidObjectIterator = 0;
60	io_object_t	hidDevice = 0;
61	SInt32                  idVendor = 1741;
62	SInt32                  idProduct = 0x420;
63
64	// Set up a matching dictionary to search the I/O Registry by class
65	// name for all HID class devices
66	hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
67
68	CFNumberRef numberRefVendor = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idVendor);
69    if ( numberRefVendor )
70    {
71        CFDictionaryAddValue(hidMatchDictionary, CFSTR(kIOHIDVendorIDKey), numberRefVendor);
72        CFRelease(numberRefVendor);
73	}
74
75    CFNumberRef numberRefProduct = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &idProduct);
76    if ( numberRefProduct )
77    {
78        CFDictionaryAddValue(hidMatchDictionary, CFSTR(kIOHIDProductIDKey), numberRefProduct);
79        CFRelease(numberRefProduct);
80	}
81
82	// Now search I/O Registry for matching devices.
83	ioReturnValue = IOServiceGetMatchingServices(kIOMasterPortDefault, hidMatchDictionary, &hidObjectIterator);
84
85	if ((ioReturnValue == kIOReturnSuccess) && (hidObjectIterator != 0)) {
86		hidDevice = IOIteratorNext(hidObjectIterator);
87	}
88
89	// release the iterator
90    if ( hidObjectIterator )
91        IOObjectRelease(hidObjectIterator);
92
93	return hidDevice;
94
95}
96
97@end
98