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
88 SAL_WNODEPRECATED_DECLARATIONS_PUSH
89 // 'NSApplicationDefined' is deprecated: first deprecated in macOS 10.12
91 [NSEvent otherEventWithType:NSApplicationDefined
93 modifierFlags:modifierFlags
95 windowNumber:[[NSApp keyWindow] windowNumber]
97 subtype:AppleRemoteControlEvent
98 data1: buttonIdentifier
101 SAL_WNODEPRECATED_DECLARATIONS_POP
105 - (void) remoteButton: (RemoteControlEventIdentifier)buttonIdentifier pressedDown: (BOOL) pressedDown clickCount: (unsigned int)clickCount
109 NSString* pressed = @"";
110 NSString* buttonName = nil;
115 pressed = @"(AppleRemoteMainController: button pressed)";
117 switch(buttonIdentifier)
119 case kRemoteButtonPlus: buttonName = @"Volume up"; break; // MEDIA_COMMAND_VOLUME_UP ( see include/vcl/commandevent.hxx )
120 case kRemoteButtonMinus: buttonName = @"Volume down"; break; // MEDIA_COMMAND_VOLUME_DOWN
121 case kRemoteButtonMenu: buttonName = @"Menu"; break; // MEDIA_COMMAND_MENU
122 case kRemoteButtonPlay: buttonName = @"Play"; break; // MEDIA_COMMAND_PLAY
123 case kRemoteButtonRight: buttonName = @"Next slide"; break; // MEDIA_COMMAND_NEXTTRACK
124 case kRemoteButtonLeft: buttonName = @"Left"; break; // MEDIA_COMMAND_PREVIOUSTRACK
125 case kRemoteButtonRight_Hold: buttonName = @"Last slide"; break; // MEDIA_COMMAND_NEXTTRACK_HOLD
126 case kRemoteButtonLeft_Hold: buttonName = @"First slide"; break; // MEDIA_COMMAND_PREVIOUSTRACK_HOLD
127 case kRemoteButtonPlus_Hold: buttonName = @"Volume up holding"; break;
128 case kRemoteButtonMinus_Hold: buttonName = @"Volume down holding"; break;
129 case kRemoteButtonPlay_Hold: buttonName = @"Play (sleep mode)"; break; // MEDIA_COMMAND_PLAY_HOLD
130 case kRemoteButtonMenu_Hold: buttonName = @"Menu (long)"; break; // MEDIA_COMMAND_MENU_HOLD
131 case kRemoteControl_Switched: buttonName = @"Remote Control Switched";break;
133 default: NSLog( @"AppleRemoteMainController: Unmapped event for button %d", buttonIdentifier); break;
136 [ self postTheEvent:buttonIdentifier modifierFlags: 0 ];
141 pressed = @"(AppleRemoteMainController: button released)";
146 //NSLog(@"Button %@ pressed %@", buttonName, pressed);
147 NSString* clickCountString = @"";
148 if (clickCount > 1) clickCountString = [NSString stringWithFormat: @"%d clicks", clickCount];
149 NSString* feedbackString = [NSString stringWithFormat:@"(Value:%4d) %@ %@ %@", buttonIdentifier, buttonName, pressed, clickCountString];
152 NSLog(@"%@", feedbackString);
154 if (pressedDown == NO) printf("\n");
155 // simulate slow processing of events
156 // [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.5]];
161 [ remoteControl release ]; remoteControl = nil;
162 [ remoteControlBehavior release ]; remoteControlBehavior = nil;
166 // for bindings access
167 - (RemoteControl*) remoteControl {
168 return remoteControl;
171 - (MultiClickRemoteBehavior*) remoteBehavior {
172 return remoteControlBehavior;
177 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */