xref: /aoo41x/main/apple_remote/AppleRemote.m (revision cdf0e10c)
1*cdf0e10cSrcweir/*****************************************************************************
2*cdf0e10cSrcweir * RemoteControlWrapper.m
3*cdf0e10cSrcweir * RemoteControlWrapper
4*cdf0e10cSrcweir *
5*cdf0e10cSrcweir * Created by Martin Kahr on 11.03.06 under a MIT-style license.
6*cdf0e10cSrcweir * Copyright (c) 2006 martinkahr.com. All rights reserved.
7*cdf0e10cSrcweir *
8*cdf0e10cSrcweir * Code modified and adapted to OpenOffice.org
9*cdf0e10cSrcweir * by Eric Bachard on 11.08.2008 under the same license
10*cdf0e10cSrcweir *
11*cdf0e10cSrcweir * Permission is hereby granted, free of charge, to any person obtaining a
12*cdf0e10cSrcweir * copy of this software and associated documentation files (the "Software"),
13*cdf0e10cSrcweir * to deal in the Software without restriction, including without limitation
14*cdf0e10cSrcweir * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15*cdf0e10cSrcweir * and/or sell copies of the Software, and to permit persons to whom the
16*cdf0e10cSrcweir * Software is furnished to do so, subject to the following conditions:
17*cdf0e10cSrcweir *
18*cdf0e10cSrcweir * The above copyright notice and this permission notice shall be included
19*cdf0e10cSrcweir * in all copies or substantial portions of the Software.
20*cdf0e10cSrcweir *
21*cdf0e10cSrcweir * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22*cdf0e10cSrcweir * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23*cdf0e10cSrcweir * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24*cdf0e10cSrcweir * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25*cdf0e10cSrcweir * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26*cdf0e10cSrcweir * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27*cdf0e10cSrcweir * THE SOFTWARE.
28*cdf0e10cSrcweir *
29*cdf0e10cSrcweir *****************************************************************************/
30*cdf0e10cSrcweir
31*cdf0e10cSrcweir#import "AppleRemote.h"
32*cdf0e10cSrcweir
33*cdf0e10cSrcweir#import <mach/mach.h>
34*cdf0e10cSrcweir#import <mach/mach_error.h>
35*cdf0e10cSrcweir#import <IOKit/IOKitLib.h>
36*cdf0e10cSrcweir#import <IOKit/IOCFPlugIn.h>
37*cdf0e10cSrcweir#import <IOKit/hid/IOHIDKeys.h>
38*cdf0e10cSrcweir
39*cdf0e10cSrcweirconst char* AppleRemoteDeviceName = "AppleIRController";
40*cdf0e10cSrcweir
41*cdf0e10cSrcweir// the WWDC 07 Leopard Build is missing the constant
42*cdf0e10cSrcweir#ifndef NSAppKitVersionNumber10_4
43*cdf0e10cSrcweir	#define NSAppKitVersionNumber10_4 824
44*cdf0e10cSrcweir#endif
45*cdf0e10cSrcweir#ifndef NSAppKitVersionNumber10_5
46*cdf0e10cSrcweir	#define NSAppKitVersionNumber10_5 949
47*cdf0e10cSrcweir#endif
48*cdf0e10cSrcweir
49*cdf0e10cSrcweir@implementation AppleRemote
50*cdf0e10cSrcweir
51*cdf0e10cSrcweir+ (const char*) remoteControlDeviceName {
52*cdf0e10cSrcweir	return AppleRemoteDeviceName;
53*cdf0e10cSrcweir}
54*cdf0e10cSrcweir
55*cdf0e10cSrcweir- (void) setCookieMappingInDictionary: (NSMutableDictionary*) _cookieToButtonMapping	{
56*cdf0e10cSrcweir
57*cdf0e10cSrcweir    // TODO : avoid such magics
58*cdf0e10cSrcweir	if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_4) {
59*cdf0e10cSrcweir	    #ifdef DEBUG
60*cdf0e10cSrcweir	    NSLog( @"setting 10.4 cookies" );
61*cdf0e10cSrcweir	    #endif
62*cdf0e10cSrcweir		// 10.4.x Tiger
63*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlus]		forKey:@"14_12_11_6_"];
64*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMinus]		forKey:@"14_13_11_6_"];
65*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu]		forKey:@"14_7_6_14_7_6_"];
66*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay]		forKey:@"14_8_6_14_8_6_"];
67*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight]		forKey:@"14_9_6_14_9_6_"];
68*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft]		forKey:@"14_10_6_14_10_6_"];
69*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold]	forKey:@"14_6_4_2_"];
70*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold]	forKey:@"14_6_3_2_"];
71*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold]	forKey:@"14_6_14_6_"];
72*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Hold]	forKey:@"18_14_6_18_14_6_"];
73*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]	forKey:@"19_"];
74*cdf0e10cSrcweir	} else if( floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_5 ) {
75*cdf0e10cSrcweir	    #ifdef DEBUG
76*cdf0e10cSrcweir	    NSLog( @"setting 10.5 cookies" );
77*cdf0e10cSrcweir	    #endif
78*cdf0e10cSrcweir		// 10.5.x Leopard
79*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlus]		forKey:@"31_29_28_19_18_"];
80*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMinus]		forKey:@"31_30_28_19_18_"];
81*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu]		forKey:@"31_20_19_18_31_20_19_18_"];
82*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay]		forKey:@"31_21_19_18_31_21_19_18_"];
83*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight]		forKey:@"31_22_19_18_31_22_19_18_"];
84*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft]		forKey:@"31_23_19_18_31_23_19_18_"];
85*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold]	forKey:@"31_19_18_4_2_"];
86*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold]	forKey:@"31_19_18_3_2_"];
87*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold]	forKey:@"31_19_18_31_19_18_"];
88*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Hold]	forKey:@"35_31_19_18_35_31_19_18_"];
89*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]	forKey:@"19_"];
90*cdf0e10cSrcweir	}
91*cdf0e10cSrcweir	else
92*cdf0e10cSrcweir	{
93*cdf0e10cSrcweir	    #ifdef DEBUG
94*cdf0e10cSrcweir	    NSLog( @"setting 10.6 cookies" );
95*cdf0e10cSrcweir	    #endif
96*cdf0e10cSrcweir	    // 10.6.x Snow Leopard
97*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlus]		forKey:@"33_31_30_21_20_2_"];
98*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMinus]		forKey:@"33_32_30_21_20_2_"];
99*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu]		forKey:@"33_22_21_20_2_33_22_21_20_2_"];
100*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay]		forKey:@"33_23_21_20_2_33_23_21_20_2_"];
101*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight]		forKey:@"33_24_21_20_2_33_24_21_20_2_"];
102*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft]		forKey:@"33_25_21_20_2_33_25_21_20_2_"];
103*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonRight_Hold]	forKey:@"33_21_20_14_12_2_"];
104*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonLeft_Hold]	forKey:@"33_21_20_13_12_2_"];
105*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonMenu_Hold]	forKey:@"33_21_20_2_33_21_20_2_"];
106*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteButtonPlay_Hold]	forKey:@"37_33_21_20_2_37_33_21_20_2_"];
107*cdf0e10cSrcweir		[_cookieToButtonMapping setObject:[NSNumber numberWithInt:kRemoteControl_Switched]	forKey:@"19_"];
108*cdf0e10cSrcweir	}
109*cdf0e10cSrcweir}
110*cdf0e10cSrcweir
111*cdf0e10cSrcweir- (void) sendRemoteButtonEvent: (RemoteControlEventIdentifier) event pressedDown: (BOOL) pressedDown {
112*cdf0e10cSrcweir	if (pressedDown == NO && event == kRemoteButtonMenu_Hold) {
113*cdf0e10cSrcweir		// There is no seperate event for pressed down on menu hold. We are simulating that event here
114*cdf0e10cSrcweir		[super sendRemoteButtonEvent:event pressedDown:YES];
115*cdf0e10cSrcweir	}
116*cdf0e10cSrcweir
117*cdf0e10cSrcweir	[super sendRemoteButtonEvent:event pressedDown:pressedDown];
118*cdf0e10cSrcweir
119*cdf0e10cSrcweir	if (pressedDown && (event == kRemoteButtonRight || event == kRemoteButtonLeft || event == kRemoteButtonPlay || event == kRemoteButtonMenu || event == kRemoteButtonPlay_Hold)) {
120*cdf0e10cSrcweir		// There is no seperate event when the button is being released. We are simulating that event here
121*cdf0e10cSrcweir		[super sendRemoteButtonEvent:event pressedDown:NO];
122*cdf0e10cSrcweir	}
123*cdf0e10cSrcweir}
124*cdf0e10cSrcweir
125*cdf0e10cSrcweir@end
126