Fix various mouse cursor position issues.
[veency.git] / Tweak.mm
1 /* Veency - VNC Remote Access Server for iPhoneOS
2 * Copyright (C) 2008-2010 Jay Freeman (saurik)
3 */
4
5 /*
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
17 * distribution.
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #define _trace() \
39 fprintf(stderr, "_trace()@%s:%u[%s]\n", __FILE__, __LINE__, __FUNCTION__)
40 #define _unlikely(expr) \
41 __builtin_expect(expr, 0)
42
43 #include <substrate.h>
44
45 #include <rfb/rfb.h>
46 #include <rfb/keysym.h>
47
48 #include <mach/mach_port.h>
49
50 #import <QuartzCore/CAWindowServer.h>
51 #import <QuartzCore/CAWindowServerDisplay.h>
52
53 #import <CoreGraphics/CGGeometry.h>
54 #import <GraphicsServices/GraphicsServices.h>
55 #import <Foundation/Foundation.h>
56 #import <IOMobileFramebuffer/IOMobileFramebuffer.h>
57 #import <IOKit/IOKitLib.h>
58 #import <UIKit/UIKit.h>
59
60 #import <SpringBoard/SBAlertItemsController.h>
61 #import <SpringBoard/SBDismissOnlyAlertItem.h>
62 #import <SpringBoard/SBStatusBarController.h>
63
64 static size_t Width = 320;
65 static size_t Height = 480;
66
67 static const size_t BytesPerPixel = 4;
68 static const size_t BitsPerSample = 8;
69
70 static NSMutableSet *handlers_;
71 static rfbScreenInfoPtr screen_;
72 static bool running_;
73 static int buttons_;
74 static int x_, y_;
75
76 static unsigned clients_;
77
78 static CFMessagePortRef ashikase_;
79 static bool cursor_;
80
81 static bool Ashikase(bool always) {
82 if (ashikase_ == NULL)
83 ashikase_ = CFMessagePortCreateRemote(kCFAllocatorDefault, CFSTR("jp.ashikase.mousesupport"));
84 return ashikase_ != NULL && (always || cursor_);
85 }
86
87 static CFDataRef cfTrue_;
88 static CFDataRef cfFalse_;
89
90 typedef struct {
91 float x, y;
92 int buttons;
93 BOOL absolute;
94 } MouseEvent;
95
96 static MouseEvent event_;
97 static CFDataRef cfEvent_;
98
99 typedef enum {
100 MouseMessageTypeEvent,
101 MouseMessageTypeSetEnabled
102 } MouseMessageType;
103
104 static void AshikaseSendEvent(float x, float y, int buttons = 0) {
105 event_.x = x;
106 event_.y = y;
107 event_.buttons = buttons;
108 event_.absolute = true;
109
110 CFMessagePortSendRequest(ashikase_, MouseMessageTypeEvent, cfEvent_, 0, 0, NULL, NULL);
111 }
112
113 static void AshikaseSetEnabled(bool enabled, bool always) {
114 if (!Ashikase(always))
115 return;
116
117 CFMessagePortSendRequest(ashikase_, MouseMessageTypeSetEnabled, enabled ? cfTrue_ : cfFalse_, 0, 0, NULL, NULL);
118
119 if (enabled)
120 AshikaseSendEvent(x_, y_);
121 }
122
123 MSClassHook(SBAlertItemsController)
124 MSClassHook(SBStatusBarController)
125
126 @class VNCAlertItem;
127 static Class $VNCAlertItem;
128
129 static rfbNewClientAction action_ = RFB_CLIENT_ON_HOLD;
130 static NSCondition *condition_;
131 static NSLock *lock_;
132
133 static rfbClientPtr client_;
134
135 @interface VNCBridge : NSObject {
136 }
137
138 + (void) askForConnection;
139 + (void) removeStatusBarItem;
140 + (void) registerClient;
141
142 @end
143
144 @implementation VNCBridge
145
146 + (void) askForConnection {
147 [[$SBAlertItemsController sharedInstance] activateAlertItem:[[[$VNCAlertItem alloc] init] autorelease]];
148 }
149
150 + (void) removeStatusBarItem {
151 AshikaseSetEnabled(false, false);
152 [[$SBStatusBarController sharedStatusBarController] removeStatusBarItem:@"Veency"];
153 }
154
155 + (void) registerClient {
156 ++clients_;
157 AshikaseSetEnabled(true, false);
158 [[$SBStatusBarController sharedStatusBarController] addStatusBarItem:@"Veency"];
159 }
160
161 @end
162
163 MSInstanceMessage2(void, VNCAlertItem, alertSheet,buttonClicked, id, sheet, int, button) {
164 [condition_ lock];
165
166 switch (button) {
167 case 1:
168 action_ = RFB_CLIENT_ACCEPT;
169
170 @synchronized (condition_) {
171 [VNCBridge registerClient];
172 }
173 break;
174
175 case 2:
176 action_ = RFB_CLIENT_REFUSE;
177 break;
178 }
179
180 [condition_ signal];
181 [condition_ unlock];
182 [self dismiss];
183 }
184
185 MSInstanceMessage2(void, VNCAlertItem, configure,requirePasscodeForActions, BOOL, configure, BOOL, require) {
186 UIModalView *sheet([self alertSheet]);
187 [sheet setDelegate:self];
188 [sheet setTitle:@"Remote Access Request"];
189 [sheet setBodyText:[NSString stringWithFormat:@"Accept connection from\n%s?\n\nVeency VNC Server\nby Jay Freeman (saurik)\nsaurik@saurik.com\nhttp://www.saurik.com/\n\nSet a VNC password in Settings!", client_->host]];
190 [sheet addButtonWithTitle:@"Accept"];
191 [sheet addButtonWithTitle:@"Reject"];
192 }
193
194 MSInstanceMessage0(void, VNCAlertItem, performUnlockAction) {
195 [[$SBAlertItemsController sharedInstance] activateAlertItem:self];
196 }
197
198 static mach_port_t (*GSTakePurpleSystemEventPort)(void);
199 static bool PurpleAllocated;
200 static bool Two_;
201
202 static void FixRecord(GSEventRecord *record) {
203 if (Two_)
204 memmove(&record->windowContextId, &record->windowContextId + 1, sizeof(*record) - (reinterpret_cast<uint8_t *>(&record->windowContextId + 1) - reinterpret_cast<uint8_t *>(record)) + record->size);
205 }
206
207 static void VNCSettings() {
208 NSDictionary *settings([NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Veency.plist", NSHomeDirectory()]]);
209
210 @synchronized (lock_) {
211 for (NSValue *handler in handlers_)
212 rfbUnregisterSecurityHandler(reinterpret_cast<rfbSecurityHandler *>([handler pointerValue]));
213 [handlers_ removeAllObjects];
214 }
215
216 @synchronized (condition_) {
217 if (screen_ == NULL)
218 return;
219
220 [reinterpret_cast<NSString *>(screen_->authPasswdData) release];
221 screen_->authPasswdData = NULL;
222
223 if (settings != nil)
224 if (NSString *password = [settings objectForKey:@"Password"])
225 if ([password length] != 0)
226 screen_->authPasswdData = [password retain];
227
228 NSNumber *cursor = [settings objectForKey:@"ShowCursor"];
229 cursor_ = cursor == nil ? true : [cursor boolValue];
230
231 if (clients_ != 0)
232 AshikaseSetEnabled(cursor_, true);
233 }
234 }
235
236 static void VNCNotifySettings(
237 CFNotificationCenterRef center,
238 void *observer,
239 CFStringRef name,
240 const void *object,
241 CFDictionaryRef info
242 ) {
243 VNCSettings();
244 }
245
246 static rfbBool VNCCheck(rfbClientPtr client, const char *data, int size) {
247 @synchronized (condition_) {
248 if (NSString *password = reinterpret_cast<NSString *>(screen_->authPasswdData)) {
249 rfbEncryptBytes(client->authChallenge, const_cast<char *>([password UTF8String]));
250 return memcmp(client->authChallenge, data, size) == 0;
251 } return TRUE;
252 }
253 }
254
255 static void VNCPointer(int buttons, int x, int y, rfbClientPtr client) {
256 x_ = x; y_ = y;
257 int diff = buttons_ ^ buttons;
258 bool twas((buttons_ & 0x1) != 0);
259 bool tis((buttons & 0x1) != 0);
260 buttons_ = buttons;
261
262 rfbDefaultPtrAddEvent(buttons, x, y, client);
263
264 if (Ashikase(false)) {
265 AshikaseSendEvent(x, y, buttons);
266 return;
267 }
268
269 mach_port_t purple(0);
270
271 if ((diff & 0x10) != 0) {
272 struct GSEventRecord record;
273
274 memset(&record, 0, sizeof(record));
275
276 record.type = (buttons & 0x4) != 0 ?
277 GSEventTypeHeadsetButtonDown :
278 GSEventTypeHeadsetButtonUp;
279
280 record.timestamp = GSCurrentEventTimestamp();
281
282 FixRecord(&record);
283 GSSendSystemEvent(&record);
284 }
285
286 if ((diff & 0x04) != 0) {
287 struct GSEventRecord record;
288
289 memset(&record, 0, sizeof(record));
290
291 record.type = (buttons & 0x4) != 0 ?
292 GSEventTypeMenuButtonDown :
293 GSEventTypeMenuButtonUp;
294
295 record.timestamp = GSCurrentEventTimestamp();
296
297 FixRecord(&record);
298 GSSendSystemEvent(&record);
299 }
300
301 if ((diff & 0x02) != 0) {
302 struct GSEventRecord record;
303
304 memset(&record, 0, sizeof(record));
305
306 record.type = (buttons & 0x2) != 0 ?
307 GSEventTypeLockButtonDown :
308 GSEventTypeLockButtonUp;
309
310 record.timestamp = GSCurrentEventTimestamp();
311
312 FixRecord(&record);
313 GSSendSystemEvent(&record);
314 }
315
316 if (twas != tis || tis) {
317 struct {
318 struct GSEventRecord record;
319 struct {
320 struct GSEventRecordInfo info;
321 struct GSPathInfo path;
322 } data;
323 } event;
324
325 memset(&event, 0, sizeof(event));
326
327 event.record.type = GSEventTypeMouse;
328 event.record.locationInWindow.x = x;
329 event.record.locationInWindow.y = y;
330 event.record.timestamp = GSCurrentEventTimestamp();
331 event.record.size = sizeof(event.data);
332
333 event.data.info.handInfo.type = twas == tis ?
334 GSMouseEventTypeDragged :
335 tis ?
336 GSMouseEventTypeDown :
337 GSMouseEventTypeUp;
338
339 event.data.info.handInfo.x34 = 0x1;
340 event.data.info.handInfo.x38 = tis ? 0x1 : 0x0;
341
342 event.data.info.pathPositions = 1;
343
344 event.data.path.x00 = 0x01;
345 event.data.path.x01 = 0x02;
346 event.data.path.x02 = tis ? 0x03 : 0x00;
347 event.data.path.position = event.record.locationInWindow;
348
349 mach_port_t port(0);
350
351 if (CAWindowServer *server = [CAWindowServer serverIfRunning]) {
352 NSArray *displays([server displays]);
353 if (displays != nil && [displays count] != 0)
354 if (CAWindowServerDisplay *display = [displays objectAtIndex:0])
355 port = [display clientPortAtPosition:event.record.locationInWindow];
356 }
357
358 if (port == 0) {
359 if (purple == 0)
360 purple = (*GSTakePurpleSystemEventPort)();
361 port = purple;
362 }
363
364 FixRecord(&event.record);
365 GSSendEvent(&event.record, port);
366 }
367
368 if (purple != 0 && PurpleAllocated)
369 mach_port_deallocate(mach_task_self(), purple);
370 }
371
372 static void VNCKeyboard(rfbBool down, rfbKeySym key, rfbClientPtr client) {
373 if (!down)
374 return;
375
376 switch (key) {
377 case XK_Return: key = '\r'; break;
378 case XK_BackSpace: key = 0x7f; break;
379 }
380
381 if (key > 0xfff)
382 return;
383
384 GSEventRef event(_GSCreateSyntheticKeyEvent(key, YES, YES));
385 GSEventRecord *record(_GSEventGetGSEventRecord(event));
386 record->type = GSEventTypeKeyDown;
387
388 mach_port_t port(0);
389
390 if (CAWindowServer *server = [CAWindowServer serverIfRunning]) {
391 NSArray *displays([server displays]);
392 if (displays != nil && [displays count] != 0)
393 if (CAWindowServerDisplay *display = [displays objectAtIndex:0])
394 port = [display clientPortAtPosition:CGPointMake(x_, y_)];
395 }
396
397 mach_port_t purple(0);
398
399 if (port == 0) {
400 if (purple == 0)
401 purple = (*GSTakePurpleSystemEventPort)();
402 port = purple;
403 }
404
405 if (port != 0)
406 GSSendEvent(record, port);
407
408 if (purple != 0 && PurpleAllocated)
409 mach_port_deallocate(mach_task_self(), purple);
410
411 CFRelease(event);
412 }
413
414 static void VNCDisconnect(rfbClientPtr client) {
415 @synchronized (condition_) {
416 if (--clients_ == 0)
417 [VNCBridge performSelectorOnMainThread:@selector(removeStatusBarItem) withObject:nil waitUntilDone:YES];
418 }
419 }
420
421 static rfbNewClientAction VNCClient(rfbClientPtr client) {
422 @synchronized (condition_) {
423 if (screen_->authPasswdData != NULL) {
424 [VNCBridge performSelectorOnMainThread:@selector(registerClient) withObject:nil waitUntilDone:YES];
425 client->clientGoneHook = &VNCDisconnect;
426 return RFB_CLIENT_ACCEPT;
427 }
428 }
429
430 [condition_ lock];
431 client_ = client;
432 [VNCBridge performSelectorOnMainThread:@selector(askForConnection) withObject:nil waitUntilDone:NO];
433 while (action_ == RFB_CLIENT_ON_HOLD)
434 [condition_ wait];
435 rfbNewClientAction action(action_);
436 action_ = RFB_CLIENT_ON_HOLD;
437 [condition_ unlock];
438
439 if (action == RFB_CLIENT_ACCEPT)
440 client->clientGoneHook = &VNCDisconnect;
441 return action;
442 }
443
444 static rfbPixel black_[320][480];
445
446 static void VNCSetup() {
447 @synchronized (condition_) {
448 int argc(1);
449 char *arg0(strdup("VNCServer"));
450 char *argv[] = {arg0, NULL};
451 screen_ = rfbGetScreen(&argc, argv, Width, Height, BitsPerSample, 3, BytesPerPixel);
452 free(arg0);
453
454 VNCSettings();
455 }
456
457 screen_->desktopName = strdup([[[NSProcessInfo processInfo] hostName] UTF8String]);
458
459 screen_->alwaysShared = TRUE;
460 screen_->handleEventsEagerly = TRUE;
461 screen_->deferUpdateTime = 5;
462
463 screen_->serverFormat.redShift = BitsPerSample * 2;
464 screen_->serverFormat.greenShift = BitsPerSample * 1;
465 screen_->serverFormat.blueShift = BitsPerSample * 0;
466
467 screen_->frameBuffer = reinterpret_cast<char *>(black_);
468
469 screen_->kbdAddEvent = &VNCKeyboard;
470 screen_->ptrAddEvent = &VNCPointer;
471
472 screen_->newClientHook = &VNCClient;
473 screen_->passwordCheck = &VNCCheck;
474
475 char data[0], mask[0];
476 rfbCursorPtr cursor = rfbMakeXCursor(0, 0, data, mask);
477 rfbSetCursor(screen_, cursor);
478 }
479
480 static void VNCEnabled() {
481 [lock_ lock];
482
483 bool enabled(true);
484 if (NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Veency.plist", NSHomeDirectory()]])
485 if (NSNumber *number = [settings objectForKey:@"Enabled"])
486 enabled = [number boolValue];
487
488 if (enabled != running_)
489 if (enabled) {
490 running_ = true;
491 screen_->socketState = RFB_SOCKET_INIT;
492 rfbInitServer(screen_);
493 rfbRunEventLoop(screen_, -1, true);
494 } else {
495 rfbShutdownServer(screen_, true);
496 running_ = false;
497 }
498
499 [lock_ unlock];
500 }
501
502 static void VNCNotifyEnabled(
503 CFNotificationCenterRef center,
504 void *observer,
505 CFStringRef name,
506 const void *object,
507 CFDictionaryRef info
508 ) {
509 VNCEnabled();
510 }
511
512 MSHook(kern_return_t, IOMobileFramebufferSwapSetLayer,
513 IOMobileFramebufferRef fb,
514 int layer,
515 CoreSurfaceBufferRef buffer,
516 CGRect bounds,
517 CGRect frame,
518 int flags
519 ) {
520 if (_unlikely(screen_ == NULL)) {
521 VNCSetup();
522 VNCEnabled();
523 } else if (_unlikely(clients_ != 0)) {
524 if (buffer == NULL)
525 screen_->frameBuffer = reinterpret_cast<char *>(black_);
526 else {
527 CoreSurfaceBufferLock(buffer, 2);
528 rfbPixel (*data)[480] = reinterpret_cast<rfbPixel (*)[480]>(CoreSurfaceBufferGetBaseAddress(buffer));
529 screen_->frameBuffer = const_cast<char *>(reinterpret_cast<volatile char *>(data));
530 CoreSurfaceBufferUnlock(buffer);
531 }
532
533 rfbMarkRectAsModified(screen_, 0, 0, Width, Height);
534 }
535
536 return _IOMobileFramebufferSwapSetLayer(fb, layer, buffer, bounds, frame, flags);
537 }
538
539 MSHook(void, rfbRegisterSecurityHandler, rfbSecurityHandler *handler) {
540 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
541
542 @synchronized (lock_) {
543 [handlers_ addObject:[NSValue valueWithPointer:handler]];
544 _rfbRegisterSecurityHandler(handler);
545 }
546
547 [pool release];
548 }
549
550 MSInitialize {
551 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
552
553 MSHookSymbol(GSTakePurpleSystemEventPort, "GSGetPurpleSystemEventPort");
554 if (GSTakePurpleSystemEventPort == NULL) {
555 MSHookSymbol(GSTakePurpleSystemEventPort, "GSCopyPurpleSystemEventPort");
556 PurpleAllocated = true;
557 }
558
559 Two_ = dlsym(RTLD_DEFAULT, "GSEventGetWindowContextId") == NULL;
560
561 MSHookFunction(&IOMobileFramebufferSwapSetLayer, MSHake(IOMobileFramebufferSwapSetLayer));
562 MSHookFunction(&rfbRegisterSecurityHandler, MSHake(rfbRegisterSecurityHandler));
563
564 $VNCAlertItem = objc_allocateClassPair(objc_getClass("SBAlertItem"), "VNCAlertItem", 0);
565 MSAddMessage2(VNCAlertItem, "v@:@i", alertSheet,buttonClicked);
566 MSAddMessage2(VNCAlertItem, "v@:cc", configure,requirePasscodeForActions);
567 MSAddMessage0(VNCAlertItem, "v@:", performUnlockAction);
568 objc_registerClassPair($VNCAlertItem);
569
570 CFNotificationCenterAddObserver(
571 CFNotificationCenterGetDarwinNotifyCenter(),
572 NULL, &VNCNotifyEnabled, CFSTR("com.saurik.Veency-Enabled"), NULL, 0
573 );
574
575 CFNotificationCenterAddObserver(
576 CFNotificationCenterGetDarwinNotifyCenter(),
577 NULL, &VNCNotifySettings, CFSTR("com.saurik.Veency-Settings"), NULL, 0
578 );
579
580 condition_ = [[NSCondition alloc] init];
581 lock_ = [[NSLock alloc] init];
582 handlers_ = [[NSMutableSet alloc] init];
583
584 [pool release];
585
586 bool value;
587
588 value = true;
589 cfTrue_ = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&value), sizeof(value));
590
591 value = false;
592 cfFalse_ = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&value), sizeof(value));
593
594 cfEvent_ = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&event_), sizeof(event_), kCFAllocatorNull);
595 }