-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
TPEventsController.m
276 lines (226 loc) · 6.58 KB
/
TPEventsController.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
//
// TPEventsController.m
// teleport
//
// Created by JuL on 09/11/05.
// Copyright 2005 abyssoft. All rights reserved.
//
#import "TPEventsController.h"
#import "TPDirectEventTapsController.h"
#import "TPEventTapsController.h"
#import "TPPreferencesManager.h"
#import "TPRemoteHost.h"
#import "TPLocalHost.h"
#define THREADED_EVENT_POSTING 0
@implementation TPEventsController
+ (TPEventsController*)defaultController
{
return [TPEventTapsController defaultController];
}
+ (TPEventsController*)eventsControllerForRemoteHost:(TPRemoteHost*)remoteHost
{
if([[TPLocalHost localHost] pairWithHost:remoteHost hasCapability:TPHostDirectEventTapsCapability]) {
SInt32 localMajorVersion = [[TPLocalHost localHost] osVersion] & 0xFFF0;
SInt32 remoteMajorVersion = [remoteHost osVersion] & 0xFFF0;
// only allow direct event taps between same major OS versions
if(localMajorVersion == remoteMajorVersion) {
DebugLog(@"Using TPDirectEventTapsController");
return [TPDirectEventTapsController defaultController];
}
else {
DebugLog(@"Using TPEventTapsController");
return [TPEventTapsController defaultController];
}
}
else if([[TPLocalHost localHost] pairWithHost:remoteHost hasCapability:TPHostEventTapsCapability]) {
DebugLog(@"Using TPEventTapsController");
return [TPEventTapsController defaultController];
}
else {
return nil;
}
}
- (void)startGettingEventsForListener:(id<TPEventsListener>)eventsListener onScreen:(NSScreen*)screen
{
_lastPostedEventDate = 0.0;
_eventsListener = eventsListener;
[self _startGettingEventsOnScreen:screen];
}
- (void)_sendEventToListener:(id)event
{
id<TPEventsListener> eventsListener = [self eventsListener];
if((_lastPostedEventDate == 0.0) && [self _shouldSkipFirstEvent:event]) {
DebugLog(@"skipping first event");
}
else if(eventsListener != nil) {
if([self _isEmergencyStopEvent:event]) {
NSLog(@"emergency stop key sequence detected, disconnecting.");
[eventsListener gotEmergencyStopEvent];
}
else {
NSEvent * nsEvent = [self _nsEventFromEvent:event];
if(![eventsListener shouldStopWithEvent:nsEvent]) {
NSData * eventData = [self _eventDataFromEvent:event];
[eventsListener gotEventWithEventData:eventData];
}
}
}
_lastPostedEventDate = [NSDate timeIntervalSinceReferenceDate];
}
- (void)stopGettingEvents
{
[self _stopGettingEvents];
_eventsListener = nil;
}
- (void)cleanupGettingEvents
{
}
- (id<TPEventsListener>)eventsListener
{
return _eventsListener;
}
- (void)_startGettingEventsOnScreen:(NSScreen*)screen
{
// overloaded
}
- (void)_stopGettingEvents
{
// overloaded
}
- (void)startPostingEvents
{
_lastPostedEventDate = 0.0;
}
- (void)stopPostingEvents
{
}
- (void)_threadedPostEventWithEventData:(NSData*)eventData
{
@autoreleasepool {
[self _postEventWithEventData:eventData];
}
}
- (void)postEventWithEventData:(NSData*)eventData
{
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if (now - _lastPostedEventDate > 60.0) { // if last event occurred more than a minute ago
[[TPLocalHost localHost] wakeUpScreen];
}
_lastPostedEventDate = now;
#if THREADED_EVENT_POSTING
[NSThread detachNewThreadSelector:@selector(_threadedPostEventWithEventData:) toTarget:self withObject:eventData];
#else
[self _postEventWithEventData:eventData];
#endif
}
- (void)_postEventWithEventData:(NSData*)eventData
{
// overloaded
}
- (void)warpMouseToCenter
{
NSArray * screens = [[TPLocalHost localHost] screens];
NSRect mainScreenRect = [screens[0] frame];
NSPoint centerPoint = NSMakePoint(NSMidX(mainScreenRect), NSMidY(mainScreenRect));
return [self warpMouseToPosition:centerPoint];
}
- (void)warpMouseToPosition:(NSPoint)position
{
_currentMouseLocation = *(CGPoint*)&position;
}
- (void)mouseDownAtPosition:(NSPoint)position
{
_currentMouseLocation = *(CGPoint*)&position;
}
- (void)mouseUpAtPosition:(NSPoint)position
{
_currentMouseLocation = *(CGPoint*)&position;
}
- (BOOL)_shouldSkipFirstEvent:(id)event
{
return NO;
}
- (BOOL)_isEmergencyStopEvent:(id)event
{
return NO;
}
- (NSEvent*)_nsEventFromEvent:(id)event
{
return (NSEvent*)event;
}
- (NSData*)_eventDataFromEvent:(id)event
{
return nil;
}
- (void)_updateMouseLocationWithMouseDelta:(TPMouseDelta)mouseDelta
{
NSArray * screens = [[TPLocalHost localHost] screens];
NSRect mainScreenRect = [screens[0] frame];
_currentMouseLocation = [NSEvent mouseLocation];
_currentMouseLocation.x += mouseDelta.x;
_currentMouseLocation.y = (NSHeight(mainScreenRect) - _currentMouseLocation.y);
_currentMouseLocation.y += mouseDelta.y;
NSEnumerator * screenEnum = [screens objectEnumerator];
NSScreen * screen;
unsigned i = 0;
while((screen = [screenEnum nextObject])) {
NSRect screenFrame = [screen frame];
// DebugLog(@"point=(%d,%d), screenFrame=%@", point.x, point.y, NSStringFromRect(screenFrame));
CGPoint relativePoint = _currentMouseLocation;
relativePoint.x -= NSMinX(screenFrame);
relativePoint.y -= (NSHeight(mainScreenRect) - NSMaxY(screenFrame));
if(relativePoint.x >= 0 && relativePoint.y >= 0 && relativePoint.x < NSWidth(screenFrame) && relativePoint.y < NSHeight(screenFrame)) {
_currentScreenIndex = i;
return;
}
i++;
}
/* Mouse cursor is outside */
screen = screens[_currentScreenIndex];
NSRect screenFrame = [screen frame];
CGPoint relativePoint = _currentMouseLocation;
relativePoint.x -= NSMinX(screenFrame);
relativePoint.y -= (NSHeight(mainScreenRect) - NSMaxY(screenFrame));
relativePoint.x = MAX(0, MIN(relativePoint.x, NSWidth(screenFrame) - 1.0));
relativePoint.y = MAX(0, MIN(relativePoint.y, NSHeight(screenFrame) - 1.0));
_currentMouseLocation = relativePoint;
_currentMouseLocation.x += NSMinX(screenFrame);
_currentMouseLocation.y += (NSHeight(mainScreenRect) - NSMaxY(screenFrame));
}
- (BOOL)event:(NSEvent*)event hasRequiredKeyIfNeeded:(BOOL)needed withTag:(NSEventType)tag
{
if(!needed)
return YES;
unsigned int keyMask = NSEventMaskFromType(tag);
DebugLog(@"keyMask: %d modifiersFlags: %d", keyMask, (int)[event modifierFlags]);
if(([event modifierFlags] & keyMask) != 0)
return YES;
NSEventModifierFlags flags = [NSEvent modifierFlags];
DebugLog(@"flags: %lu", flags);
switch(tag) {
case COMMAND_KEY_TAG:
if((flags & NSCommandKeyMask) != 0)
return YES;
break;
case ALT_KEY_TAG:
if((flags & NSAlternateKeyMask) != 0)
return YES;
break;
case CTRL_KEY_TAG:
if((flags & NSControlKeyMask) != 0)
return YES;
break;
case SHIFT_KEY_TAG:
if((flags & NSShiftKeyMask) != 0)
return YES;
break;
case CAPSLOCK_KEY_TAG:
if((flags & NSAlphaShiftKeyMask) != 0)
return YES;
break;
default:
break;
}
return NO;
}
@end