forked from martinkahr/apple_remote_control
-
Notifications
You must be signed in to change notification settings - Fork 3
/
MultiClickRemoteBehavior.m
212 lines (184 loc) · 7.53 KB
/
MultiClickRemoteBehavior.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
/*****************************************************************************
* MultiClickRemoteBehavior.m
* RemoteControlWrapper
*
* Created by Martin Kahr on 11.03.06 under a MIT-style license.
* Copyright (c) 2006-2014 martinkahr.com. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*****************************************************************************/
#import "MultiClickRemoteBehavior.h"
static const NSTimeInterval DEFAULT_MAXIMUM_CLICK_TIME_DIFFERENCE=0.35;
static const NSTimeInterval HOLD_RECOGNITION_TIME_INTERVAL=0.4;
@implementation MultiClickRemoteBehavior
// Designated initializer
- (instancetype) init {
if ((self = [super init])) {
_maximumClickCountTimeDifference = DEFAULT_MAXIMUM_CLICK_TIME_DIFFERENCE;
}
return self;
}
- (void) setDelegate: (nullable id<MultiClickRemoteBehaviorDelegate>) inDelegate {
if (inDelegate && [inDelegate respondsToSelector:@selector(remoteButton:pressedDown:clickCount:)]==NO) {
return;
}
_delegate = inDelegate;
}
- (nullable id<MultiClickRemoteBehaviorDelegate>) delegate {
return _delegate;
}
@synthesize simulateHoldEvent = _simulateHoldEvent;
- (BOOL) simulatesHoldForButtonIdentifier: (RemoteControlEventIdentifier) identifier remoteControl: (RemoteControl*) remoteControl {
assert(remoteControl);
// we do that check only for the normal button identifiers as we would check for hold support for hold events instead
if (identifier > (1 << EVENT_TO_HOLD_EVENT_OFFSET)) {
return NO;
}
return [self simulateHoldEvent] && [remoteControl sendsEventForButtonIdentifier: (identifier << EVENT_TO_HOLD_EVENT_OFFSET)]==NO;
}
- (BOOL) clickCountingEnabled {
return _clickCountEnabledButtons != 0;
}
- (void) setClickCountingEnabled: (BOOL) value {
if (value) {
[self setClickCountEnabledButtons: kRemoteButtonPlus | kRemoteButtonMinus | kRemoteButtonPlay | kRemoteButtonLeft | kRemoteButtonRight | kRemoteButtonMenu];
} else {
[self setClickCountEnabledButtons: 0];
}
}
@synthesize clickCountEnabledButtons = _clickCountEnabledButtons;
@synthesize maximumClickCountTimeDifference = _maximumClickCountTimeDifference;
- (void) sendPressedDownEventToMainThread: (NSNumber*) event {
assert(event);
id<MultiClickRemoteBehaviorDelegate> strongDelegate = [self delegate];
[strongDelegate remoteButton:[event intValue] pressedDown:YES clickCount:1];
}
- (void) sendSimulatedHoldEvent: (NSNumber*) time {
assert(time);
BOOL startSimulateHold = NO;
RemoteControlEventIdentifier event = _lastHoldEvent;
@synchronized(self) {
startSimulateHold = (_lastHoldEvent>0 && _lastHoldEventTime == [time doubleValue]);
}
if (startSimulateHold) {
_lastEventSimulatedHold = YES;
event = (event << EVENT_TO_HOLD_EVENT_OFFSET);
[self performSelectorOnMainThread:@selector(sendPressedDownEventToMainThread:) withObject:@(event) waitUntilDone:NO];
}
}
- (void) executeClickCountEvent: (NSArray*) values {
assert(values);
RemoteControlEventIdentifier event = [[values objectAtIndex: 0] intValue];
NSTimeInterval eventTimePoint = [[values objectAtIndex: 1] doubleValue];
BOOL finishedClicking = NO;
unsigned int finalClickCount = _eventClickCount;
@synchronized(self) {
finishedClicking = (event != _lastClickCountEvent || eventTimePoint == _lastClickCountEventTime);
if (finishedClicking) {
_eventClickCount = 0;
_lastClickCountEvent = 0;
_lastClickCountEventTime = 0;
}
}
if (finishedClicking) {
id<MultiClickRemoteBehaviorDelegate> strongDelegate = [self delegate];
[strongDelegate remoteButton:event pressedDown: YES clickCount:finalClickCount];
// trigger a button release event, too
[NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow:0.1]];
[strongDelegate remoteButton:event pressedDown: NO clickCount:finalClickCount];
}
}
- (void) sendRemoteButtonEvent: (RemoteControlEventIdentifier) event pressedDown: (BOOL) pressedDown remoteControl: (RemoteControl*) remoteControl {
assert(remoteControl);
id<MultiClickRemoteBehaviorDelegate> strongDelegate = [self delegate];
if (!strongDelegate) {
return;
}
BOOL clickCountingForEvent = ([self clickCountEnabledButtons] & event) == event;
if ([self simulatesHoldForButtonIdentifier: event remoteControl: remoteControl] && _lastClickCountEvent==0) {
if (pressedDown) {
// wait to see if it is a hold
_lastHoldEvent = event;
_lastHoldEventTime = [NSDate timeIntervalSinceReferenceDate];
[self performSelector:@selector(sendSimulatedHoldEvent:)
withObject:@(_lastHoldEventTime)
afterDelay:HOLD_RECOGNITION_TIME_INTERVAL];
return;
} else {
if (_lastEventSimulatedHold) {
// it was a hold
// send an event for "hold release"
event = (event << EVENT_TO_HOLD_EVENT_OFFSET);
_lastHoldEvent = 0;
_lastEventSimulatedHold = NO;
[strongDelegate remoteButton:event pressedDown: pressedDown clickCount:1];
return;
} else {
RemoteControlEventIdentifier previousEvent = _lastHoldEvent;
@synchronized(self) {
_lastHoldEvent = 0;
}
// in case click counting is enabled we have to setup the state for that, too
if (clickCountingForEvent) {
_lastClickCountEvent = previousEvent;
_lastClickCountEventTime = _lastHoldEventTime;
NSNumber* eventNumber;
NSNumber* timeNumber;
_eventClickCount = 1;
timeNumber = @(_lastClickCountEventTime);
eventNumber= @(previousEvent);
NSTimeInterval diffTime = _maximumClickCountTimeDifference-([NSDate timeIntervalSinceReferenceDate]-_lastHoldEventTime);
[self performSelector: @selector(executeClickCountEvent:)
withObject: @[eventNumber, timeNumber]
afterDelay: diffTime];
// we do not return here because we are still in the press-release event
// that will be consumed below
} else {
// trigger the pressed down event that we consumed first
[strongDelegate remoteButton:event pressedDown: YES clickCount:1];
}
}
}
}
if (clickCountingForEvent) {
if (pressedDown == NO) {
return;
}
NSNumber* eventNumber;
NSNumber* timeNumber;
@synchronized(self) {
_lastClickCountEventTime = [NSDate timeIntervalSinceReferenceDate];
if (_lastClickCountEvent == event) {
_eventClickCount = _eventClickCount + 1;
} else {
_eventClickCount = 1;
}
_lastClickCountEvent = event;
timeNumber = @(_lastClickCountEventTime);
eventNumber= @(event);
}
[self performSelector: @selector(executeClickCountEvent:)
withObject: @[eventNumber, timeNumber]
afterDelay: _maximumClickCountTimeDifference];
} else {
[strongDelegate remoteButton:event pressedDown: pressedDown clickCount:1];
}
}
@end