-
Notifications
You must be signed in to change notification settings - Fork 0
/
CouchMouseController.m
221 lines (176 loc) · 5.17 KB
/
CouchMouseController.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
//
// CouchMouseController.m
// CouchMouse
//
// Created by Friedrich Gräter on 03.10.10.
// Published under the terms of the GNU General Public License v2.
//
#import "event-trigger.h"
#import "CouchMouseController.h"
#define TIMER_RESOLUTION 0.001f
@implementation CouchMouseController
#pragma mark Initialization functions
-(void) initializeAppleRemote
{
remoteControl = [[[AppleRemote alloc] initWithDelegate: self] retain];
remoteBehavior = [MultiClickRemoteBehavior new];
[remoteBehavior setDelegate: self];
[remoteControl setDelegate: remoteBehavior];
[remoteBehavior setSimulateHoldEvent: YES];
}
-(void) awakeFromNib
{
[self initializeAppleRemote];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(inputModeChangeListener:)
name:InputModeChangedEvent
object:configuration];
}
#pragma mark Input mode handling
-(void) inputModeChangeListener:(NSNotification *)notification
{
switch (configuration.inputMode) {
case DisabledState:
[remoteControl stopListening: self];
break;
case KeyboardState:
[remoteControl startListening: self];
break;
case MouseState:
[remoteControl startListening: self];
break;
}
}
#pragma mark Update timer
-(void) timerHandler:(NSTimer *) timer
{
CouchEventDescription *eventDescription = [timer userInfo];
eventDescription.cyclesSinceStarted += 1;
[self dispatchRemoteButtonEvent: eventDescription];
}
-(void) setupRepeatTimerWithEventDescription: (CouchEventDescription*)eventDescription
{
if (eventDescription.pressedDown) {
float interval;
switch (configuration.inputMode) {
case DisabledState:
interval = 0.0f;
break;
case KeyboardState:
interval = 1.0f;
break;
case MouseState:
interval = TIMER_RESOLUTION * (10.0f - configuration.mouseVelocity);
break;
}
if (repeatTimer == nil) {
repeatTimer = [NSTimer scheduledTimerWithTimeInterval: interval target: self selector: @selector(timerHandler:) userInfo: eventDescription repeats: YES ];
}
}
else {
if (repeatTimer != nil) {
[repeatTimer invalidate];
repeatTimer = nil;
}
}
}
#pragma mark Remote button delegate
- (void) remoteButton: (RemoteControlEventIdentifier)buttonIdentifier pressedDown: (BOOL) pressedDown clickCount: (unsigned int)clickCount
{
CouchEventDescription *eventDescription = [[CouchEventDescription alloc] init];
eventDescription.buttonPressed = buttonIdentifier;
eventDescription.pressedDown = pressedDown;
eventDescription.clickCount = clickCount;
eventDescription.cyclesSinceStarted = 0;
[self setupRepeatTimerWithEventDescription: eventDescription];
[self dispatchRemoteButtonEvent: eventDescription];
}
- (void) dispatchRemoteButtonEvent: (CouchEventDescription*)eventDescription
{
switch (configuration.inputMode) {
case DisabledState:
break;
case KeyboardState:
[self handleKeyboardRequest: eventDescription];
break;
case MouseState:
[self handleMouseRequest: eventDescription];
break;
}
}
#pragma mark Handling keyboard requests
-(void) handleKeyboardRequest: (CouchEventDescription*)eventDescription
{
NSNumber *pressedKey = [NSNumber numberWithInteger: eventDescription.buttonPressed];
int keyCode = [configuration getKeyCodeForRemoteButton: [pressedKey intValue]];
postKeyEvent(keyCode, eventDescription.pressedDown);
}
#pragma mark Handling Mouse requests
-(void) handleMouseRequest: (CouchEventDescription*)eventDescription
{
switch (eventDescription.buttonPressed) {
case kRemoteButtonRight:
case kRemoteButtonRight_Hold:
if (eventDescription.pressedDown)
[self moveMouseToX: 1 toY: 0];
break;
case kRemoteButtonLeft:
case kRemoteButtonLeft_Hold:
if (eventDescription.pressedDown)
[self moveMouseToX: -1 toY: 0];
break;
case kRemoteButtonPlus:
case kRemoteButtonPlus_Hold:
if (eventDescription.pressedDown)
[self moveMouseToX: 0 toY: -1];
break;
case kRemoteButtonMinus:
case kRemoteButtonMinus_Hold:
if (eventDescription.pressedDown)
[self moveMouseToX: 0 toY: 1];
break;
case kRemoteButtonPlay:
case kRemoteButtonPlay_Hold:
if (eventDescription.pressedDown)
[self downMouse: kCGMouseButtonLeft];
else
[self upMouse: kCGMouseButtonLeft];
break;
case kRemoteButtonMenu:
case kRemoteButtonMenu_Hold:
if (eventDescription.pressedDown)
[self downMouse: kCGMouseButtonRight];
else
[self upMouse: kCGMouseButtonRight];
break;
default:
break;
}
}
-(void) moveMouseToX: (int)toX toY:(int)toY
{
float newX, newY;
CGEventRef ourEvent = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(ourEvent);
newX = point.x + toX * 1;
newY = point.y + toY * 1;
if (newX < 0) newX = 0;
if (newY < 0) newY = 0;
postMouseMove(CGPointMake(newX, newY));
}
- (void) downMouse: (CGMouseButton)button
{
if (button == kCGMouseButtonLeft)
postMouseButtonEvent(kCGEventLeftMouseDown, kCGMouseButtonLeft);
else
postMouseButtonEvent(kCGEventRightMouseDown, kCGMouseButtonRight);
}
- (void) upMouse: (CGMouseButton) button
{
if (button == kCGMouseButtonLeft)
postMouseButtonEvent(kCGEventLeftMouseUp, kCGMouseButtonLeft);
else
postMouseButtonEvent(kCGEventRightMouseUp, kCGMouseButtonRight);
}
@end