xref: /AOO41X/main/apple_remote/RemoteMainController.m (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir/*****************************************************************************
2*cdf0e10cSrcweir * RemoteMainController.m
3*cdf0e10cSrcweir *
4*cdf0e10cSrcweir * Created by Martin Kahr on 11.03.06 under a MIT-style license.
5*cdf0e10cSrcweir * Copyright (c) 2006 martinkahr.com. All rights reserved.
6*cdf0e10cSrcweir *
7*cdf0e10cSrcweir * Code modified and adapted to OpenOffice.org
8*cdf0e10cSrcweir * by Eric Bachard on 11.08.2008 under the same License
9*cdf0e10cSrcweir *
10*cdf0e10cSrcweir * Permission is hereby granted, free of charge, to any person obtaining a
11*cdf0e10cSrcweir * copy of this software and associated documentation files (the "Software"),
12*cdf0e10cSrcweir * to deal in the Software without restriction, including without limitation
13*cdf0e10cSrcweir * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14*cdf0e10cSrcweir * and/or sell copies of the Software, and to permit persons to whom the
15*cdf0e10cSrcweir * Software is furnished to do so, subject to the following conditions:
16*cdf0e10cSrcweir *
17*cdf0e10cSrcweir * The above copyright notice and this permission notice shall be included
18*cdf0e10cSrcweir * in all copies or substantial portions of the Software.
19*cdf0e10cSrcweir *
20*cdf0e10cSrcweir * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21*cdf0e10cSrcweir * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22*cdf0e10cSrcweir * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23*cdf0e10cSrcweir * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24*cdf0e10cSrcweir * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25*cdf0e10cSrcweir * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26*cdf0e10cSrcweir * THE SOFTWARE.
27*cdf0e10cSrcweir *
28*cdf0e10cSrcweir *****************************************************************************/
29*cdf0e10cSrcweir
30*cdf0e10cSrcweir#import "RemoteMainController.h"
31*cdf0e10cSrcweir#import "AppleRemote.h"
32*cdf0e10cSrcweir#import "KeyspanFrontRowControl.h"
33*cdf0e10cSrcweir#import "GlobalKeyboardDevice.h"
34*cdf0e10cSrcweir#import "RemoteControlContainer.h"
35*cdf0e10cSrcweir#import "MultiClickRemoteBehavior.h"
36*cdf0e10cSrcweir
37*cdf0e10cSrcweir// -------------------------------------------------------------------------------------------
38*cdf0e10cSrcweir// Sample Code 3: Multi Click Behavior and Hold Event Simulation
39*cdf0e10cSrcweir// -------------------------------------------------------------------------------------------
40*cdf0e10cSrcweir
41*cdf0e10cSrcweir@implementation MainController
42*cdf0e10cSrcweir
43*cdf0e10cSrcweir- (id) init {
44*cdf0e10cSrcweir    self = [super init];  // because we redefined our own init instead of use the fu..nny awakeFromNib
45*cdf0e10cSrcweir    if (self != nil) {
46*cdf0e10cSrcweir
47*cdf0e10cSrcweir        // 1. instantiate the desired behavior for the remote control device
48*cdf0e10cSrcweir        remoteControlBehavior = [[MultiClickRemoteBehavior alloc] init];
49*cdf0e10cSrcweir
50*cdf0e10cSrcweir        // 2. configure the behavior
51*cdf0e10cSrcweir        [remoteControlBehavior setDelegate: self];
52*cdf0e10cSrcweir
53*cdf0e10cSrcweir        // 3. a Remote Control Container manages a number of devices and conforms to the RemoteControl interface
54*cdf0e10cSrcweir        //    Therefore you can enable or disable all the devices of the container with a single "startListening:" call.
55*cdf0e10cSrcweir        RemoteControlContainer* container = [[RemoteControlContainer alloc] initWithDelegate: remoteControlBehavior];
56*cdf0e10cSrcweir
57*cdf0e10cSrcweir        if ( [container instantiateAndAddRemoteControlDeviceWithClass: [AppleRemote class]] != 0 ) {
58*cdf0e10cSrcweir#ifdef DEBUG
59*cdf0e10cSrcweir            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [AppleRemote class]] successfull");
60*cdf0e10cSrcweir        }
61*cdf0e10cSrcweir        else {
62*cdf0e10cSrcweir            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [AppleRemote class]] failed");
63*cdf0e10cSrcweir#endif
64*cdf0e10cSrcweir        }
65*cdf0e10cSrcweir
66*cdf0e10cSrcweir        if ( [container instantiateAndAddRemoteControlDeviceWithClass: [KeyspanFrontRowControl class]] != 0 ) {
67*cdf0e10cSrcweir#ifdef DEBUG
68*cdf0e10cSrcweir            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [KeyspanFrontRowControl class]] successfull");
69*cdf0e10cSrcweir        }
70*cdf0e10cSrcweir        else {
71*cdf0e10cSrcweir            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [KeyspanFrontRowControl class]] failed");
72*cdf0e10cSrcweir#endif
73*cdf0e10cSrcweir        }
74*cdf0e10cSrcweir
75*cdf0e10cSrcweir        if ( [container instantiateAndAddRemoteControlDeviceWithClass: [GlobalKeyboardDevice class]] != 0 ) {
76*cdf0e10cSrcweir#ifdef DEBUG
77*cdf0e10cSrcweir            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [GlobalKeyboardDevice class]] successfull");
78*cdf0e10cSrcweir        }
79*cdf0e10cSrcweir        else {
80*cdf0e10cSrcweir            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [GlobalKeyboardDevice class]] failed");
81*cdf0e10cSrcweir#endif
82*cdf0e10cSrcweir        }
83*cdf0e10cSrcweir        // to give the binding mechanism a chance to see the change of the attribute
84*cdf0e10cSrcweir        [self setValue: container forKey: @"remoteControl"];
85*cdf0e10cSrcweir#ifdef DEBUG
86*cdf0e10cSrcweir            NSLog(@"MainController init done");
87*cdf0e10cSrcweir#endif
88*cdf0e10cSrcweir    }
89*cdf0e10cSrcweir    else
90*cdf0e10cSrcweir        NSLog(@"MainController init failed");
91*cdf0e10cSrcweir    return self;
92*cdf0e10cSrcweir}
93*cdf0e10cSrcweir
94*cdf0e10cSrcweir- (void) postTheEvent: (short int)buttonIdentifier modifierFlags:(int)modifierFlags
95*cdf0e10cSrcweir{
96*cdf0e10cSrcweir    [NSApp postEvent:
97*cdf0e10cSrcweir    [NSEvent    otherEventWithType:NSApplicationDefined
98*cdf0e10cSrcweir                location:NSZeroPoint
99*cdf0e10cSrcweir                modifierFlags:modifierFlags
100*cdf0e10cSrcweir                timestamp: 0
101*cdf0e10cSrcweir                windowNumber:[[NSApp keyWindow] windowNumber]
102*cdf0e10cSrcweir                context:nil
103*cdf0e10cSrcweir                subtype:AppleRemoteControlEvent
104*cdf0e10cSrcweir                data1: buttonIdentifier
105*cdf0e10cSrcweir                data2: 0]
106*cdf0e10cSrcweir    atStart: NO];
107*cdf0e10cSrcweir}
108*cdf0e10cSrcweir
109*cdf0e10cSrcweir
110*cdf0e10cSrcweir- (void) remoteButton: (RemoteControlEventIdentifier)buttonIdentifier pressedDown: (BOOL) pressedDown clickCount: (unsigned int)clickCount
111*cdf0e10cSrcweir{
112*cdf0e10cSrcweir    NSString* pressed = @"";
113*cdf0e10cSrcweir#ifdef DEBUG
114*cdf0e10cSrcweir    NSString* buttonName = nil;
115*cdf0e10cSrcweir#endif
116*cdf0e10cSrcweir    if (pressedDown)
117*cdf0e10cSrcweir    {
118*cdf0e10cSrcweir        pressed = @"(pressed)";
119*cdf0e10cSrcweir
120*cdf0e10cSrcweir#ifdef DEBUG
121*cdf0e10cSrcweir        switch(buttonIdentifier)
122*cdf0e10cSrcweir        {
123*cdf0e10cSrcweir            case kRemoteButtonPlus:         buttonName = @"Volume up";              break;  // MEDIA_COMMAND_VOLUME_UP  ( see vcl/inc/vcl/cmdevt.hxx )
124*cdf0e10cSrcweir            case kRemoteButtonMinus:        buttonName = @"Volume down";            break;  // MEDIA_COMMAND_VOLUME_DOWN
125*cdf0e10cSrcweir            case kRemoteButtonMenu:         buttonName = @"Menu";                   break;  // MEDIA_COMMAND_MENU
126*cdf0e10cSrcweir            case kRemoteButtonPlay:         buttonName = @"Play";                   break;  // MEDIA_COMMAND_PLAY
127*cdf0e10cSrcweir            case kRemoteButtonRight:        buttonName = @"Next slide";             break;  // MEDIA_COMMAND_NEXTTRACK
128*cdf0e10cSrcweir            case kRemoteButtonLeft:         buttonName = @"Left";                   break;  // MEDIA_COMMAND_PREVIOUSTRACK
129*cdf0e10cSrcweir            case kRemoteButtonRight_Hold:   buttonName = @"Last slide";             break;  // MEDIA_COMMAND_NEXTTRACK_HOLD
130*cdf0e10cSrcweir            case kRemoteButtonLeft_Hold:    buttonName = @"First slide";            break;  // MEDIA_COMMAND_PREVIOUSTRACK_HOLD
131*cdf0e10cSrcweir            case kRemoteButtonPlus_Hold:    buttonName = @"Volume up holding";      break;
132*cdf0e10cSrcweir            case kRemoteButtonMinus_Hold:   buttonName = @"Volume down holding";    break;
133*cdf0e10cSrcweir            case kRemoteButtonPlay_Hold:    buttonName = @"Play (sleep mode)";      break;  // MEDIA_COMMAND_PLAY_HOLD
134*cdf0e10cSrcweir            case kRemoteButtonMenu_Hold:    buttonName = @"Menu (long)";            break;  // MEDIA_COMMAND_MENU_HOLD
135*cdf0e10cSrcweir            case kRemoteControl_Switched:   buttonName = @"Remote Control Switched";break;
136*cdf0e10cSrcweir
137*cdf0e10cSrcweir            default:    NSLog(@"Unmapped event for button %d", buttonIdentifier);   break;
138*cdf0e10cSrcweir        }
139*cdf0e10cSrcweir#endif
140*cdf0e10cSrcweir        [ self postTheEvent:buttonIdentifier modifierFlags: 0 ];
141*cdf0e10cSrcweir    }
142*cdf0e10cSrcweir    else // not pressed
143*cdf0e10cSrcweir    {
144*cdf0e10cSrcweir        pressed = @"(released)";
145*cdf0e10cSrcweir    }
146*cdf0e10cSrcweir
147*cdf0e10cSrcweir#ifdef DEBUG
148*cdf0e10cSrcweir	//NSLog(@"Button %@ pressed %@", buttonName, pressed);
149*cdf0e10cSrcweir	NSString* clickCountString = @"";
150*cdf0e10cSrcweir	if (clickCount > 1) clickCountString = [NSString stringWithFormat: @"%d clicks", clickCount];
151*cdf0e10cSrcweir	NSString* feedbackString = [NSString stringWithFormat:@"(Value:%4d) %@  %@ %@",buttonIdentifier, buttonName, pressed, clickCountString];
152*cdf0e10cSrcweir
153*cdf0e10cSrcweir	// print out events
154*cdf0e10cSrcweir	NSLog(@"%@", feedbackString);
155*cdf0e10cSrcweir
156*cdf0e10cSrcweir    if (pressedDown == NO) printf("\n");
157*cdf0e10cSrcweir	// simulate slow processing of events
158*cdf0e10cSrcweir	// [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.5]];
159*cdf0e10cSrcweir#endif
160*cdf0e10cSrcweir}
161*cdf0e10cSrcweir
162*cdf0e10cSrcweir- (void) dealloc {
163*cdf0e10cSrcweir    [remoteControl autorelease];
164*cdf0e10cSrcweir	[remoteControlBehavior autorelease];
165*cdf0e10cSrcweir	[super dealloc];
166*cdf0e10cSrcweir}
167*cdf0e10cSrcweir
168*cdf0e10cSrcweir// for bindings access
169*cdf0e10cSrcweir- (RemoteControl*) remoteControl {
170*cdf0e10cSrcweir	return remoteControl;
171*cdf0e10cSrcweir}
172*cdf0e10cSrcweir
173*cdf0e10cSrcweir- (MultiClickRemoteBehavior*) remoteBehavior {
174*cdf0e10cSrcweir	return remoteControlBehavior;
175*cdf0e10cSrcweir}
176*cdf0e10cSrcweir
177*cdf0e10cSrcweir@end