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