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