forked from samiamwork/Movist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppController_Remote.m
289 lines (259 loc) · 9.91 KB
/
AppController_Remote.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// Movist
//
// Copyright 2006 ~ 2008 Yong-Hoe Kim. All rights reserved.
// Yong-Hoe Kim <[email protected]>
//
// This file is part of Movist.
//
// Movist is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Movist is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#import "AppController.h"
#import "UserDefaults.h"
#import "FullScreener.h"
#import "MainWindow.h"
#import "MMovie.h"
#import "MMovieView.h"
#import "AppleRemote/MultiClickRemoteBehavior.h"
#import "AppleRemote/RemoteControlContainer.h"
#import "AppleRemote/AppleRemote.h"
#import "AppleRemote/GlobalKeyboardDevice.h"
#import "AppleRemote/KeyspanFrontRowControl.h"
//#import <IOKit/pwr_mgt/IOPMKeys.h>
@implementation AppController (Remote)
- (void)initRemoteControl
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
_remoteControlBehavior = [[MultiClickRemoteBehavior alloc] init];
[_remoteControlBehavior setDelegate:self];
[_remoteControlBehavior setSimulateHoldEvent:TRUE];
[_remoteControlBehavior setClickCountingEnabled:TRUE];
_remoteControlContainer = [[RemoteControlContainer alloc] initWithDelegate:self];
[_remoteControlContainer instantiateAndAddRemoteControlDeviceWithClass:[AppleRemote class]];
[_remoteControlContainer instantiateAndAddRemoteControlDeviceWithClass:[KeyspanFrontRowControl class]];
[_remoteControlContainer instantiateAndAddRemoteControlDeviceWithClass:[GlobalKeyboardDevice class]];
[_remoteControlContainer setOpenInExclusiveMode:TRUE];
//[self setValue:_remoteControlContainer forKey:@"remoteControl"];
_remoteControlRepeatTimerLock = [[NSLock alloc] init];
}
- (void)cleanupRemoteControl
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self stopRemoteControl];
[_remoteControlRepeatTimerLock release];
[_remoteControlContainer release];
[_remoteControlBehavior release];
}
- (void)startRemoteControl
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
if (![_remoteControlContainer isListeningToRemote]) {
[_remoteControlContainer startListening:self];
}
}
- (void)stopRemoteControl
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
if ([_remoteControlContainer isListeningToRemote]) {
[_remoteControlContainer stopListening:self];
}
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
- (void)sendRemoteButtonEvent:(RemoteControlEventIdentifier)buttonIdentifier
pressedDown:(BOOL)pressedDown remoteControl:(RemoteControl*)remoteControl
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self remoteButton:buttonIdentifier pressedDown:pressedDown clickCount:1];
}
- (void)startRemoteControlRepeatTimer:(NSTimeInterval)interval
{
[_remoteControlRepeatTimerLock lock];
[_remoteControlRepeatTimer invalidate];
_remoteControlRepeatTimer = [NSTimer scheduledTimerWithTimeInterval:interval
target:self selector:@selector(remoteControlRepeat:)
userInfo:nil repeats:TRUE];
[_remoteControlRepeatTimerLock unlock];
}
- (void)stopRemoteControlRepeatTimer
{
[_remoteControlRepeatTimerLock lock];
if (_remoteControlRepeatTimer) {
[_remoteControlRepeatTimer invalidate];
_remoteControlRepeatTimer = nil;
}
[_remoteControlRepeatTimerLock unlock];
}
- (void)remoteControlRepeat:(NSTimer*)timer
{
if (timer && 0.01 < [timer timeInterval]) {
[self startRemoteControlRepeatTimer:0.01];
}
switch (_remoteControlRepeatButtonID) {
case kRemoteButtonPlus : [self remoteControlPlusAction:self]; break;
case kRemoteButtonMinus : [self remoteControlMinusAction:self]; break;
case kRemoteButtonLeft_Hold : [self remoteControlLeftAction:self]; break;
case kRemoteButtonRight_Hold: [self remoteControlRightAction:self]; break;
default:
break;
}
}
#if defined(DEBUG)
- (void)traceRemoteButton:(RemoteControlEventIdentifier)buttonIdentifier
pressedDown:(BOOL)pressed clickCount:(unsigned int)clickCount
{
NSString* button;
switch (buttonIdentifier) {
case kRemoteButtonPlus : button = @"PLUS"; break;
case kRemoteButtonPlus_Hold : button = @"PLUS hold"; break;
case kRemoteButtonMinus : button = @"MINUS"; break;
case kRemoteButtonMinus_Hold : button = @"MINUS hold"; break;
case kRemoteButtonLeft : button = @"LEFT"; break;
case kRemoteButtonLeft_Hold : button = @"LEFT hold"; break;
case kRemoteButtonRight : button = @"RIGHT"; break;
case kRemoteButtonRight_Hold : button = @"RIGHT hold"; break;
case kRemoteButtonPlay : button = @"PLAY"; break;
case kRemoteButtonPlay_Hold : button = @"PLAY hold"; break;
case kRemoteButtonMenu : button = @"MENU"; break;
case kRemoteButtonMenu_Hold : button = @"MENU hold"; break;
case kRemoteControl_Switched : button = @"SWITCHED"; break;
default : button = [NSString stringWithFormat:@"UNMAPPED[%d]", buttonIdentifier]; break;
}
TRACE(@"RemoteControl %@ %@(%d)", button, pressed ? @"pressed" : @"released", clickCount);
}
#endif
- (void)remoteButton:(RemoteControlEventIdentifier)buttonIdentifier
pressedDown:(BOOL)pressed clickCount:(unsigned int)clickCount
{
//TRACE(@"%s pressed=%d, clickCount=%d", __PRETTY_FUNCTION__, pressed, clickCount);
#if defined(DEBUG)
[self traceRemoteButton:buttonIdentifier pressedDown:pressed clickCount:clickCount];
#endif
if (pressed) {
// pressed only buttons
switch (buttonIdentifier) {
case kRemoteButtonLeft : [self remoteControlLeftAction:self]; break;
case kRemoteButtonRight : [self remoteControlRightAction:self]; break;
case kRemoteButtonPlay : [self remoteControlPlayAction:self]; break;
case kRemoteButtonPlay_Hold : [self remoteControlPlayHoldAction:self]; break;
case kRemoteButtonMenu : [self remoteControlMenuAction:self]; break;
case kRemoteButtonMenu_Hold : [self remoteControlMenuHoldAction:self]; break;
default:
break;
}
}
// repeat-needed buttons
switch (buttonIdentifier) {
case kRemoteButtonPlus :
case kRemoteButtonMinus :
case kRemoteButtonLeft_Hold :
case kRemoteButtonRight_Hold :
if (pressed) {
_remoteControlRepeatButtonID = buttonIdentifier;
[self remoteControlRepeat:nil]; // for current event
[self startRemoteControlRepeatTimer:0.5];
}
else {
[self stopRemoteControlRepeatTimer];
}
break;
default:
break;
}
}
////////////////////////////////////////////////////////////////////////////////
- (IBAction)remoteControlPlusAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
if ([[NSApp keyWindow] firstResponder] == _movieView) {
[self volumeUp];
}
else { // full navigation mode
[_fullScreener selectUpper];
}
}
- (IBAction)remoteControlMinusAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
if ([[NSApp keyWindow] firstResponder] == _movieView) {
[self volumeDown];
}
else { // full navigation mode
[_fullScreener selectLower];
}
}
- (IBAction)remoteControlLeftAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self seekBackward:0];
}
- (IBAction)remoteControlRightAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self seekForward:0];
}
- (IBAction)remoteControlPlayAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
if ([NSApp keyWindow] == _mainWindow) { // window mode
if (!_movie) {
// go to folder navigation directly
[self beginFullNavigation];
}
else {
if ([_movie rate] == 0.0) {
[self beginFullScreen]; // auto full-screen
[self play];
}
else {
[self pause];
}
}
}
else if (![_fullScreener isNavigating]) { // full play mode
[self playAction:self];
}
else { // full navigation mode
[_fullScreener openCurrent];
}
}
- (IBAction)remoteControlPlayHoldAction:(id)sender
{
TRACE(@"%s", __PRETTY_FUNCTION__);
/* I don't know how to get a privilege to sleep machine.
// go to sleep (emulating normal play-hold action)
NSCalendarDate* date = [NSCalendarDate dateWithTimeIntervalSinceNow:100];
IOReturn ret = IOPMSchedulePowerEvent((CFDateRef)date, 0, CFSTR(kIOPMAutoSleep));
if (ret != kIOReturnSuccess) {
TRACE(@"%s error=0x%x", __PRETTY_FUNCTION__, ret);
}
*/
// quit movist as temp-impl.
[NSApp terminate:self];
}
- (IBAction)remoteControlMenuAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self fullScreenAction:sender];
}
- (IBAction)remoteControlMenuHoldAction:(id)sender
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self fullNavigationAction:self];
}
- (IBAction)remoteControlPlusHoldAction:(id)sender {} // currently not used
- (IBAction)remoteControlMinusHoldAction:(id)sender {} // currently not used
- (IBAction)remoteControlLeftHoldAction:(id)sender {} // currently not used
- (IBAction)remoteControlRightHoldAction:(id)sender {} // currently not used
@end