1 /* -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*****************************************************************************
3 * RemoteMainController.m
5 * Created by Martin Kahr on 11.03.06 under a MIT-style license.
6 * Copyright (c) 2006 martinkahr.com. All rights reserved.
8 * Code modified and adapted to OpenOffice.org
9 * by Eric Bachard on 11.08.2008 under the same License
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:
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
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
29 *****************************************************************************/
31 #import <apple_remote/RemoteMainController.h>
32 #import "AppleRemote.h"
33 #import "KeyspanFrontRowControl.h"
34 #import "GlobalKeyboardDevice.h"
35 #import "RemoteControlContainer.h"
36 #import "MultiClickRemoteBehavior.h"
39 // Sample Code 3: Multi Click Behavior and Hold Event Simulation
42 @implementation AppleRemoteMainController
45 self = [super init]; // because we redefined our own init instead of use the fu..nny awakeFromNib
48 // 1. instantiate the desired behavior for the remote control device
49 remoteControlBehavior = [[MultiClickRemoteBehavior alloc] init];
51 // 2. configure the behavior
52 [remoteControlBehavior setDelegate: self];
54 // 3. a Remote Control Container manages a number of devices and conforms to the RemoteControl interface
55 // Therefore you can enable or disable all the devices of the container with a single "startListening:" call.
56 RemoteControlContainer* container = [[RemoteControlContainer alloc] initWithDelegate: remoteControlBehavior];
58 if ( [container instantiateAndAddRemoteControlDeviceWithClass: [AppleRemote class]] != 0 ) {
60 NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [AppleRemote class]] successful");
63 NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [AppleRemote class]] failed");
67 if ( [container instantiateAndAddRemoteControlDeviceWithClass: [GlobalKeyboardDevice class]] != 0 ) {
69 NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [GlobalKeyboardDevice class]] successful");
72 NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [GlobalKeyboardDevice class]] failed");
75 // to give the binding mechanism a chance to see the change of the attribute
76 [self setValue: container forKey: @"remoteControl"];
78 NSLog(@"AppleRemoteMainController init done");
82 NSLog(@"AppleRemoteMainController init failed");
86 - (void) postTheEvent: (short int)buttonIdentifier modifierFlags:(int)modifierFlags
89 [NSEvent otherEventWithType:NSApplicationDefined
91 modifierFlags:modifierFlags
93 windowNumber:[[NSApp keyWindow] windowNumber]
95 subtype:AppleRemoteControlEvent
96 data1: buttonIdentifier
102 - (void) remoteButton: (RemoteControlEventIdentifier)buttonIdentifier pressedDown: (BOOL) pressedDown clickCount: (unsigned int)clickCount
105 NSString* pressed = @"";
107 NSString* buttonName = nil;
111 pressed = @"(AppleRemoteMainController: button pressed)";
114 switch(buttonIdentifier)
116 case kRemoteButtonPlus: buttonName = @"Volume up"; break; // MEDIA_COMMAND_VOLUME_UP ( see vcl/inc/vcl/cmdevt.hxx )
117 case kRemoteButtonMinus: buttonName = @"Volume down"; break; // MEDIA_COMMAND_VOLUME_DOWN
118 case kRemoteButtonMenu: buttonName = @"Menu"; break; // MEDIA_COMMAND_MENU
119 case kRemoteButtonPlay: buttonName = @"Play"; break; // MEDIA_COMMAND_PLAY
120 case kRemoteButtonRight: buttonName = @"Next slide"; break; // MEDIA_COMMAND_NEXTTRACK
121 case kRemoteButtonLeft: buttonName = @"Left"; break; // MEDIA_COMMAND_PREVIOUSTRACK
122 case kRemoteButtonRight_Hold: buttonName = @"Last slide"; break; // MEDIA_COMMAND_NEXTTRACK_HOLD
123 case kRemoteButtonLeft_Hold: buttonName = @"First slide"; break; // MEDIA_COMMAND_PREVIOUSTRACK_HOLD
124 case kRemoteButtonPlus_Hold: buttonName = @"Volume up holding"; break;
125 case kRemoteButtonMinus_Hold: buttonName = @"Volume down holding"; break;
126 case kRemoteButtonPlay_Hold: buttonName = @"Play (sleep mode)"; break; // MEDIA_COMMAND_PLAY_HOLD
127 case kRemoteButtonMenu_Hold: buttonName = @"Menu (long)"; break; // MEDIA_COMMAND_MENU_HOLD
128 case kRemoteControl_Switched: buttonName = @"Remote Control Switched";break;
130 default: NSLog( @"AppleRemoteMainController: Unmapped event for button %d", buttonIdentifier); break;
133 [ self postTheEvent:buttonIdentifier modifierFlags: 0 ];
137 pressed = @"(AppleRemoteMainController: button released)";
141 //NSLog(@"Button %@ pressed %@", buttonName, pressed);
142 NSString* clickCountString = @"";
143 if (clickCount > 1) clickCountString = [NSString stringWithFormat: @"%d clicks", clickCount];
144 NSString* feedbackString = [NSString stringWithFormat:@"(Value:%4d) %@ %@ %@",buttonIdentifier, buttonName, pressed, clickCountString];
147 NSLog(@"%@", feedbackString);
149 if (pressedDown == NO) printf("\n");
150 // simulate slow processing of events
151 // [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.5]];
156 [remoteControl autorelease];
157 [remoteControlBehavior autorelease];
161 // for bindings access
162 - (RemoteControl*) remoteControl {
163 return remoteControl;
166 - (MultiClickRemoteBehavior*) remoteBehavior {
167 return remoteControlBehavior;
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */