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 "GlobalKeyboardDevice.h"
33#import "RemoteControlContainer.h"
34#import "MultiClickRemoteBehavior.h"
35
36// -------------------------------------------------------------------------------------------
37// Sample Code 3: Multi Click Behavior and Hold Event Simulation
38// -------------------------------------------------------------------------------------------
39
40@implementation AppleRemoteMainController
41
42- (id) init {
43    self = [super init];  // because we redefined our own init instead of use the fu..nny awakeFromNib
44    if (self != nil) {
45
46        // 1. instantiate the desired behavior for the remote control device
47        remoteControlBehavior = [[MultiClickRemoteBehavior alloc] init];
48
49        // 2. configure the behavior
50        [remoteControlBehavior setDelegate: self];
51
52        // 3. a Remote Control Container manages a number of devices and conforms to the RemoteControl interface
53        //    Therefore you can enable or disable all the devices of the container with a single "startListening:" call.
54        RemoteControlContainer* container = [[RemoteControlContainer alloc] initWithDelegate: remoteControlBehavior];
55
56        if ( [container instantiateAndAddRemoteControlDeviceWithClass: [AppleRemote class]] != 0 ) {
57#ifdef DEBUG
58            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [AppleRemote class]] successfull");
59        }
60        else {
61            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [AppleRemote class]] failed");
62#endif
63        }
64
65        if ( [container instantiateAndAddRemoteControlDeviceWithClass: [GlobalKeyboardDevice class]] != 0 ) {
66#ifdef DEBUG
67            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [GlobalKeyboardDevice class]] successfull");
68        }
69        else {
70            NSLog(@"[container instantiateAndAddRemoteControlDeviceWithClass: [GlobalKeyboardDevice class]] failed");
71#endif
72        }
73        // to give the binding mechanism a chance to see the change of the attribute
74        [self setValue: container forKey: @"remoteControl"];
75#ifdef DEBUG
76            NSLog(@"AppleRemoteMainController init done");
77#endif
78    }
79    else
80        NSLog(@"AppleRemoteMainController init failed");
81    return self;
82}
83
84- (void) postTheEvent: (short int)buttonIdentifier modifierFlags:(int)modifierFlags
85{
86    [NSApp postEvent:
87    [NSEvent    otherEventWithType:NSApplicationDefined
88                location:NSZeroPoint
89                modifierFlags:modifierFlags
90                timestamp: 0
91                windowNumber:[[NSApp keyWindow] windowNumber]
92                context:nil
93                subtype:AppleRemoteControlEvent
94                data1: buttonIdentifier
95                data2: 0]
96    atStart: NO];
97}
98
99
100- (void) remoteButton: (RemoteControlEventIdentifier)buttonIdentifier pressedDown: (BOOL) pressedDown clickCount: (unsigned int)clickCount
101{
102    NSString* pressed = @"";
103#ifdef DEBUG
104    NSString* buttonName = nil;
105#endif
106    if (pressedDown)
107    {
108        pressed = @"(pressed)";
109
110#ifdef DEBUG
111        switch(buttonIdentifier)
112        {
113            case kRemoteButtonPlus:         buttonName = @"Volume up";              break;  // MEDIA_COMMAND_VOLUME_UP  ( see vcl/inc/vcl/cmdevt.hxx )
114            case kRemoteButtonMinus:        buttonName = @"Volume down";            break;  // MEDIA_COMMAND_VOLUME_DOWN
115            case kRemoteButtonMenu:         buttonName = @"Menu";                   break;  // MEDIA_COMMAND_MENU
116            case kRemoteButtonPlay:         buttonName = @"Play";                   break;  // MEDIA_COMMAND_PLAY
117            case kRemoteButtonRight:        buttonName = @"Next slide";             break;  // MEDIA_COMMAND_NEXTTRACK
118            case kRemoteButtonLeft:         buttonName = @"Left";                   break;  // MEDIA_COMMAND_PREVIOUSTRACK
119            case kRemoteButtonRight_Hold:   buttonName = @"Last slide";             break;  // MEDIA_COMMAND_NEXTTRACK_HOLD
120            case kRemoteButtonLeft_Hold:    buttonName = @"First slide";            break;  // MEDIA_COMMAND_PREVIOUSTRACK_HOLD
121            case kRemoteButtonPlus_Hold:    buttonName = @"Volume up holding";      break;
122            case kRemoteButtonMinus_Hold:   buttonName = @"Volume down holding";    break;
123            case kRemoteButtonPlay_Hold:    buttonName = @"Play (sleep mode)";      break;  // MEDIA_COMMAND_PLAY_HOLD
124            case kRemoteButtonMenu_Hold:    buttonName = @"Menu (long)";            break;  // MEDIA_COMMAND_MENU_HOLD
125            case kRemoteControl_Switched:   buttonName = @"Remote Control Switched";break;
126
127            default:    NSLog(@"Unmapped event for button %d", buttonIdentifier);   break;
128        }
129#endif
130        [ self postTheEvent:buttonIdentifier modifierFlags: 0 ];
131    }
132    else // not pressed
133    {
134        pressed = @"(released)";
135    }
136
137#ifdef DEBUG
138	//NSLog(@"Button %@ pressed %@", buttonName, pressed);
139	NSString* clickCountString = @"";
140	if (clickCount > 1) clickCountString = [NSString stringWithFormat: @"%d clicks", clickCount];
141	NSString* feedbackString = [NSString stringWithFormat:@"(Value:%4d) %@  %@ %@",buttonIdentifier, buttonName, pressed, clickCountString];
142
143	// print out events
144	NSLog(@"%@", feedbackString);
145
146    if (pressedDown == NO) printf("\n");
147	// simulate slow processing of events
148	// [NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow: 0.5]];
149#endif
150}
151
152- (void) dealloc {
153    [remoteControl autorelease];
154	[remoteControlBehavior autorelease];
155	[super dealloc];
156}
157
158// for bindings access
159- (RemoteControl*) remoteControl {
160	return remoteControl;
161}
162
163- (MultiClickRemoteBehavior*) remoteBehavior {
164	return remoteControlBehavior;
165}
166
167@end
168