1cdf0e10cSrcweir/*****************************************************************************
2cdf0e10cSrcweir * RemoteControlWrapper.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 "AppleRemote.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
39cdf0e10cSrcweirconst char* AppleRemoteDeviceName = "AppleIRController";
40cdf0e10cSrcweir
41cdf0e10cSrcweir// the WWDC 07 Leopard Build is missing the constant
42cdf0e10cSrcweir#ifndef NSAppKitVersionNumber10_4
43cdf0e10cSrcweir	#define NSAppKitVersionNumber10_4 824
44cdf0e10cSrcweir#endif
45ddd5933dSEric Bachard
46cdf0e10cSrcweir#ifndef NSAppKitVersionNumber10_5
47ddd5933dSEric Bachard    #define NSAppKitVersionNumber10_5 949
48cdf0e10cSrcweir#endif
49cdf0e10cSrcweir
50cdf0e10cSrcweir@implementation AppleRemote
51cdf0e10cSrcweir
52cdf0e10cSrcweir+ (const char*) remoteControlDeviceName {
53cdf0e10cSrcweir	return AppleRemoteDeviceName;
54cdf0e10cSrcweir}
55cdf0e10cSrcweir
56ddd5933dSEric Bachard- (void) setCookieMappingInDictionary: (NSMutableDictionary*) _cookieToButtonMapping
57ddd5933dSEric Bachard{
58cdf0e10cSrcweir    // TODO : avoid such magics
59ddd5933dSEric Bachard	if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_4)
60ddd5933dSEric Bachard    {
61ddd5933dSEric Bachard        #ifdef DEBUG
6253e04a5dSHerbert Dürr            NSLog( @"Apple Remote: setting 10.4 cookies" );
63ddd5933dSEric Bachard        #endif
64ddd5933dSEric Bachard
65cdf0e10cSrcweir		// 10.4.x Tiger
66cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlus]		forKey:@"14_12_11_6_"];
67cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMinus]		forKey:@"14_13_11_6_"];
68cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu]		forKey:@"14_7_6_14_7_6_"];
69cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay]		forKey:@"14_8_6_14_8_6_"];
70cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight]		forKey:@"14_9_6_14_9_6_"];
71cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft]		forKey:@"14_10_6_14_10_6_"];
72cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold]	forKey:@"14_6_4_2_"];
73cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold]	forKey:@"14_6_3_2_"];
74cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold]	forKey:@"14_6_14_6_"];
75cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Hold]	forKey:@"18_14_6_18_14_6_"];
76cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]	forKey:@"19_"];
77ddd5933dSEric Bachard	}
78ddd5933dSEric Bachard    else if( floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_5 )
79ddd5933dSEric Bachard    {
80ddd5933dSEric Bachard        #ifdef DEBUG
8153e04a5dSHerbert Dürr            NSLog( @"Apple Remote: setting 10.5 cookies" );
82ddd5933dSEric Bachard        #endif
83cdf0e10cSrcweir		// 10.5.x Leopard
84cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlus]		forKey:@"31_29_28_19_18_"];
85cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMinus]		forKey:@"31_30_28_19_18_"];
86cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu]		forKey:@"31_20_19_18_31_20_19_18_"];
87cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay]		forKey:@"31_21_19_18_31_21_19_18_"];
88cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight]		forKey:@"31_22_19_18_31_22_19_18_"];
89cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft]		forKey:@"31_23_19_18_31_23_19_18_"];
90cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold]	forKey:@"31_19_18_4_2_"];
91cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold]	forKey:@"31_19_18_3_2_"];
92cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold]	forKey:@"31_19_18_31_19_18_"];
93cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Hold]	forKey:@"35_31_19_18_35_31_19_18_"];
94cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]	forKey:@"19_"];
95ddd5933dSEric Bachard    }
96ddd5933dSEric Bachard    else
97ddd5933dSEric Bachard    {
98ddd5933dSEric Bachard        #ifdef DEBUG
9953e04a5dSHerbert Dürr            NSLog( @"Apple Remote: setting OSX>=10.6 cookies" );
100ddd5933dSEric Bachard        #endif
101ddd5933dSEric Bachard        // 10.6.x Snow Leopard
102ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlus]		forKey:@"33_31_30_21_20_2_"];
103ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMinus]		forKey:@"33_32_30_21_20_2_"];
104ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu]		forKey:@"33_22_21_20_2_33_22_21_20_2_"];
105ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay]		forKey:@"33_23_21_20_2_33_23_21_20_2_"];
106ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight]		forKey:@"33_24_21_20_2_33_24_21_20_2_"];
107ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft]		forKey:@"33_25_21_20_2_33_25_21_20_2_"];
108ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold]	forKey:@"33_21_20_14_12_2_"];
109ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold]	forKey:@"33_21_20_13_12_2_"];
110ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold]	forKey:@"33_21_20_2_33_21_20_2_"];
111ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Hold]	forKey:@"37_33_21_20_2_37_33_21_20_2_"];
11229b8a8ebSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kMetallicRemote2009ButtonPlay]       forKey:@"33_21_20_8_2_33_21_20_8_2_"];
11329b8a8ebSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kMetallicRemote2009ButtonMiddlePlay] forKey:@"33_21_20_3_2_33_21_20_3_2_"];
114ddd5933dSEric Bachard        [_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]	forKey:@"19_"];
115cdf0e10cSrcweir	}
116cdf0e10cSrcweir}
117cdf0e10cSrcweir
118ddd5933dSEric Bachard- (void) sendRemoteButtonEvent: (RemoteControlEventIdentifier) event pressedDown: (BOOL) pressedDown
119ddd5933dSEric Bachard{
120cdf0e10cSrcweir	if (pressedDown == NO && event == kRemoteButtonMenu_Hold) {
12107a3d7f1SPedro Giffuni		// There is no separate event for pressed down on menu hold. We are simulating that event here
122cdf0e10cSrcweir		[super sendRemoteButtonEvent:event pressedDown:YES];
123cdf0e10cSrcweir	}
124cdf0e10cSrcweir
125cdf0e10cSrcweir	[super sendRemoteButtonEvent:event pressedDown:pressedDown];
126cdf0e10cSrcweir
127cdf0e10cSrcweir	if (pressedDown && (event == kRemoteButtonRight || event == kRemoteButtonLeft || event == kRemoteButtonPlay || event == kRemoteButtonMenu || event == kRemoteButtonPlay_Hold)) {
12807a3d7f1SPedro Giffuni		// There is no separate event when the button is being released. We are simulating that event here
129cdf0e10cSrcweir		[super sendRemoteButtonEvent:event pressedDown:NO];
130cdf0e10cSrcweir	}
131cdf0e10cSrcweir}
132cdf0e10cSrcweir
133cdf0e10cSrcweir@end
134