]> git.saurik.com Git - veency.git/blob - Tweak.mm
1b99b5ba467d52588cd634d3746c20bf01ecaf6c
[veency.git] / Tweak.mm
1 /* Veency - VNC Remote Access Server for iPhoneOS
2 * Copyright (C) 2008-2012 Jay Freeman (saurik)
3 */
4
5 /* GNU Affero General Public License, Version 3 {{{ */
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #define _trace() \
23 fprintf(stderr, "_trace()@%s:%u[%s]\n", __FILE__, __LINE__, __FUNCTION__)
24 #define _likely(expr) \
25 __builtin_expect(expr, 1)
26 #define _unlikely(expr) \
27 __builtin_expect(expr, 0)
28
29 #include <substrate.h>
30
31 #include <rfb/rfb.h>
32 #include <rfb/keysym.h>
33
34 #include <mach/mach.h>
35 #include <mach/mach_time.h>
36
37 #include <sys/mman.h>
38 #include <sys/sysctl.h>
39
40 #undef assert
41
42 #include <CoreFoundation/CFUserNotification.h>
43 #import <CoreGraphics/CGGeometry.h>
44 #import <GraphicsServices/GraphicsServices.h>
45 #import <Foundation/Foundation.h>
46 #import <UIKit/UIKit.h>
47
48 #include <IOKit/hid/IOHIDEventTypes.h>
49 #include <IOKit/hidsystem/IOHIDUsageTables.h>
50
51 extern "C" {
52 #include "SpringBoardAccess.h"
53 }
54
55 MSClassHook(BKAccessibility)
56 MSClassHook(UIApplication)
57
58 @interface UIApplication (Apple)
59 - (void) addStatusBarImageNamed:(NSString *)name;
60 - (void) removeStatusBarImageNamed:(NSString *)name;
61 @end
62
63 @interface CAWindowServerDisplay : NSObject
64 - (mach_port_t) clientPortAtPosition:(CGPoint)position;
65 - (unsigned) contextIdAtPosition:(CGPoint)position;
66 - (mach_port_t) taskPortOfContextId:(unsigned)context;
67 @end
68
69 @interface CAWindowServer : NSObject
70 + (CAWindowServer *) serverIfRunning;
71 - (NSArray *) displays;
72 @end
73
74 @interface UIModalView : UIView
75 - (id) addButtonWithTitle:(NSString *)title;
76 - (void) setBodyText:(NSString *)text;
77 - (void) setDelegate:(id)delegate;
78 - (void) setTitle:(NSString *)title;
79 @end
80
81 @interface SBAlertItem : NSObject
82 - (void) dismiss;
83 - (UIModalView *) alertSheet;
84 @end
85
86 @interface SBAlertItemsController : NSObject
87 + (SBAlertItemsController *) sharedInstance;
88 - (void) activateAlertItem:(SBAlertItem *)item;
89 @end
90
91 @interface SBStatusBarController : NSObject
92 + (SBStatusBarController *) sharedStatusBarController;
93 - (void) addStatusBarItem:(NSString *)item;
94 - (void) removeStatusBarItem:(NSString *)item;
95 @end
96
97 @interface BKHIDClientConnectionManager : NSObject
98 - () clientForTaskPort:(mach_port_t)port;
99 @end
100
101 @interface BKAccessibility : NSObject
102 + (BKHIDClientConnectionManager *) _eventRoutingClientConnectionManager;
103 @end
104
105 typedef void *CoreSurfaceBufferRef;
106
107 extern CFStringRef kCoreSurfaceBufferGlobal;
108 extern CFStringRef kCoreSurfaceBufferMemoryRegion;
109 extern CFStringRef kCoreSurfaceBufferPitch;
110 extern CFStringRef kCoreSurfaceBufferWidth;
111 extern CFStringRef kCoreSurfaceBufferHeight;
112 extern CFStringRef kCoreSurfaceBufferPixelFormat;
113 extern CFStringRef kCoreSurfaceBufferAllocSize;
114
115 extern "C" CoreSurfaceBufferRef CoreSurfaceBufferCreate(CFDictionaryRef dict);
116 extern "C" int CoreSurfaceBufferLock(CoreSurfaceBufferRef surface, unsigned int lockType);
117 extern "C" int CoreSurfaceBufferUnlock(CoreSurfaceBufferRef surface);
118 extern "C" void *CoreSurfaceBufferGetBaseAddress(CoreSurfaceBufferRef surface);
119
120 extern "C" void CoreSurfaceBufferFlushProcessorCaches(CoreSurfaceBufferRef buffer);
121
122 typedef void *CoreSurfaceAcceleratorRef;
123
124 extern "C" int CoreSurfaceAcceleratorCreate(CFAllocatorRef allocator, void *type, CoreSurfaceAcceleratorRef *accel);
125 extern "C" unsigned int CoreSurfaceAcceleratorTransferSurface(CoreSurfaceAcceleratorRef accelerator, CoreSurfaceBufferRef dest, CoreSurfaceBufferRef src, CFDictionaryRef options/*, void *, void *, void **/);
126
127 typedef void *IOMobileFramebufferRef;
128
129 extern "C" kern_return_t IOMobileFramebufferSwapSetLayer(
130 IOMobileFramebufferRef fb,
131 int layer,
132 CoreSurfaceBufferRef buffer,
133 CGRect bounds,
134 CGRect frame,
135 int flags
136 );
137
138 extern "C" void IOMobileFramebufferGetDisplaySize(IOMobileFramebufferRef connect, CGSize *size);
139 extern "C" void IOMobileFramebufferIsMainDisplay(IOMobileFramebufferRef connect, int *main);
140
141 typedef CFTypeRef IOHIDEventRef;
142 typedef CFTypeRef IOHIDEventSystemClientRef;
143 typedef CFTypeRef IOHIDEventSystemConnectionRef;
144
145 extern "C" {
146 IOHIDEventRef IOHIDEventCreateKeyboardEvent(CFAllocatorRef allocator, uint64_t time, uint16_t page, uint16_t usage, Boolean down, IOHIDEventOptionBits flags);
147
148 IOHIDEventRef IOHIDEventCreateDigitizerEvent(CFAllocatorRef allocator, uint64_t timeStamp, IOHIDDigitizerTransducerType type, uint32_t index, uint32_t identity, uint32_t eventMask, uint32_t buttonMask, IOHIDFloat x, IOHIDFloat y, IOHIDFloat z, IOHIDFloat tipPressure, IOHIDFloat barrelPressure, Boolean range, Boolean touch, IOOptionBits options);
149 IOHIDEventRef IOHIDEventCreateDigitizerFingerEvent(CFAllocatorRef allocator, uint64_t timeStamp, uint32_t index, uint32_t identity, uint32_t eventMask, IOHIDFloat x, IOHIDFloat y, IOHIDFloat z, IOHIDFloat tipPressure, IOHIDFloat twist, Boolean range, Boolean touch, IOOptionBits options);
150
151 IOHIDEventSystemClientRef IOHIDEventSystemClientCreate(CFAllocatorRef allocator);
152
153 void IOHIDEventAppendEvent(IOHIDEventRef parent, IOHIDEventRef child);
154 void IOHIDEventSetIntegerValue(IOHIDEventRef event, IOHIDEventField field, int value);
155 void IOHIDEventSetSenderID(IOHIDEventRef event, uint64_t sender);
156
157 void IOHIDEventSystemClientDispatchEvent(IOHIDEventSystemClientRef client, IOHIDEventRef event);
158 void IOHIDEventSystemConnectionDispatchEvent(IOHIDEventSystemConnectionRef connection, IOHIDEventRef event);
159 }
160
161 static size_t width_;
162 static size_t height_;
163 static NSUInteger ratio_ = 0;
164
165 static const size_t BytesPerPixel = 4;
166 static const size_t BitsPerSample = 8;
167
168 static CoreSurfaceAcceleratorRef accelerator_;
169 static CoreSurfaceBufferRef buffer_;
170 static CFDictionaryRef options_;
171
172 static NSMutableSet *handlers_;
173 static rfbScreenInfoPtr screen_;
174 static bool running_;
175 static int buttons_;
176 static int x_, y_;
177
178 static unsigned clients_;
179
180 static CFMessagePortRef ashikase_;
181 static bool cursor_;
182
183 static rfbPixel *black_;
184
185 static void VNCBlack() {
186 if (_unlikely(black_ == NULL))
187 black_ = reinterpret_cast<rfbPixel *>(mmap(NULL, sizeof(rfbPixel) * width_ * height_, PROT_READ, MAP_ANON | MAP_PRIVATE | MAP_NOCACHE, VM_FLAGS_PURGABLE, 0));
188 screen_->frameBuffer = reinterpret_cast<char *>(black_);
189 }
190
191 static bool Ashikase(bool always) {
192 if (!always && !cursor_)
193 return false;
194
195 if (ashikase_ == NULL)
196 ashikase_ = CFMessagePortCreateRemote(kCFAllocatorDefault, CFSTR("jp.ashikase.mousesupport"));
197 if (ashikase_ != NULL)
198 return true;
199
200 cursor_ = false;
201 return false;
202 }
203
204 static CFDataRef cfTrue_;
205 static CFDataRef cfFalse_;
206
207 typedef struct {
208 float x, y;
209 int buttons;
210 BOOL absolute;
211 } MouseEvent;
212
213 static MouseEvent event_;
214 static CFDataRef cfEvent_;
215
216 typedef enum {
217 MouseMessageTypeEvent,
218 MouseMessageTypeSetEnabled
219 } MouseMessageType;
220
221 static void AshikaseSendEvent(float x, float y, int buttons = 0) {
222 event_.x = x;
223 event_.y = y;
224 event_.buttons = buttons;
225 event_.absolute = true;
226
227 CFMessagePortSendRequest(ashikase_, MouseMessageTypeEvent, cfEvent_, 0, 0, NULL, NULL);
228 }
229
230 static void AshikaseSetEnabled(bool enabled, bool always) {
231 if (!Ashikase(always))
232 return;
233
234 CFMessagePortSendRequest(ashikase_, MouseMessageTypeSetEnabled, enabled ? cfTrue_ : cfFalse_, 0, 0, NULL, NULL);
235
236 if (enabled)
237 AshikaseSendEvent(x_, y_);
238 }
239
240 MSClassHook(SBAlertItem)
241 MSClassHook(SBAlertItemsController)
242 MSClassHook(SBStatusBarController)
243
244 @interface VNCAlertItem : SBAlertItem
245 @end
246
247 static Class $VNCAlertItem;
248
249 static NSString *DialogTitle(@"Remote Access Request");
250 static NSString *DialogFormat(@"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!");
251 static NSString *DialogAccept(@"Accept");
252 static NSString *DialogReject(@"Reject");
253
254 static volatile rfbNewClientAction action_ = RFB_CLIENT_ON_HOLD;
255 static NSCondition *condition_;
256 static NSLock *lock_;
257
258 static rfbClientPtr client_;
259
260 static void VNCSetup();
261 static void VNCEnabled();
262
263 float (*$GSMainScreenScaleFactor)();
264
265 static void OnUserNotification(CFUserNotificationRef notification, CFOptionFlags flags) {
266 [condition_ lock];
267
268 if ((flags & 0x3) == 1)
269 action_ = RFB_CLIENT_ACCEPT;
270 else
271 action_ = RFB_CLIENT_REFUSE;
272
273 [condition_ signal];
274 [condition_ unlock];
275
276 CFRelease(notification);
277 }
278
279 @interface VNCBridge : NSObject {
280 }
281
282 + (void) askForConnection;
283 + (void) removeStatusBarItem;
284 + (void) registerClient;
285
286 @end
287
288 @implementation VNCBridge
289
290 + (void) askForConnection {
291 if ($VNCAlertItem != nil) {
292 [[$SBAlertItemsController sharedInstance] activateAlertItem:[[[$VNCAlertItem alloc] init] autorelease]];
293 return;
294 }
295
296 SInt32 error;
297 CFUserNotificationRef notification(CFUserNotificationCreate(kCFAllocatorDefault, 0, kCFUserNotificationPlainAlertLevel, &error, (CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:
298 DialogTitle, kCFUserNotificationAlertHeaderKey,
299 [NSString stringWithFormat:DialogFormat, client_->host], kCFUserNotificationAlertMessageKey,
300 DialogAccept, kCFUserNotificationAlternateButtonTitleKey,
301 DialogReject, kCFUserNotificationDefaultButtonTitleKey,
302 nil]));
303
304 if (error != 0) {
305 CFRelease(notification);
306 notification = NULL;
307 }
308
309 if (notification == NULL) {
310 [condition_ lock];
311 action_ = RFB_CLIENT_REFUSE;
312 [condition_ signal];
313 [condition_ unlock];
314 return;
315 }
316
317 CFRunLoopSourceRef source(CFUserNotificationCreateRunLoopSource(kCFAllocatorDefault, notification, &OnUserNotification, 0));
318 CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
319 }
320
321 + (void) removeStatusBarItem {
322 AshikaseSetEnabled(false, false);
323
324 if (SBA_available())
325 SBA_removeStatusBarImage(const_cast<char *>("Veency"));
326 else if ($SBStatusBarController != nil)
327 [[$SBStatusBarController sharedStatusBarController] removeStatusBarItem:@"Veency"];
328 else if (UIApplication *app = [$UIApplication sharedApplication])
329 [app removeStatusBarImageNamed:@"Veency"];
330 }
331
332 + (void) registerClient {
333 // XXX: this could find a better home
334 if (ratio_ == 0) {
335 if ($GSMainScreenScaleFactor == NULL)
336 ratio_ = 1.0f;
337 else
338 ratio_ = $GSMainScreenScaleFactor();
339 }
340
341 ++clients_;
342 AshikaseSetEnabled(true, false);
343
344 if (SBA_available())
345 SBA_addStatusBarImage(const_cast<char *>("Veency"));
346 else if ($SBStatusBarController != nil)
347 [[$SBStatusBarController sharedStatusBarController] addStatusBarItem:@"Veency"];
348 else if (UIApplication *app = [$UIApplication sharedApplication])
349 [app addStatusBarImageNamed:@"Veency"];
350 }
351
352 + (void) performSetup:(NSThread *)thread {
353 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
354 [thread autorelease];
355 VNCSetup();
356 VNCEnabled();
357 [pool release];
358 }
359
360 @end
361
362 MSInstanceMessage2(void, VNCAlertItem, alertSheet,buttonClicked, id, sheet, int, button) {
363 [condition_ lock];
364
365 switch (button) {
366 case 1:
367 action_ = RFB_CLIENT_ACCEPT;
368
369 @synchronized (condition_) {
370 [VNCBridge registerClient];
371 }
372 break;
373
374 case 2:
375 action_ = RFB_CLIENT_REFUSE;
376 break;
377 }
378
379 [condition_ signal];
380 [condition_ unlock];
381 [self dismiss];
382 }
383
384 MSInstanceMessage2(void, VNCAlertItem, configure,requirePasscodeForActions, BOOL, configure, BOOL, require) {
385 UIModalView *sheet([self alertSheet]);
386 [sheet setDelegate:self];
387 [sheet setTitle:DialogTitle];
388 [sheet setBodyText:[NSString stringWithFormat:DialogFormat, client_->host]];
389 [sheet addButtonWithTitle:DialogAccept];
390 [sheet addButtonWithTitle:DialogReject];
391 }
392
393 MSInstanceMessage0(void, VNCAlertItem, performUnlockAction) {
394 [[$SBAlertItemsController sharedInstance] activateAlertItem:self];
395 }
396
397 static mach_port_t (*GSTakePurpleSystemEventPort)(void);
398 static bool PurpleAllocated;
399 static int Level_;
400
401 static void FixRecord(GSEventRecord *record) {
402 if (Level_ < 1)
403 memmove(&record->windowContextId, &record->windowContextId + 1, sizeof(*record) - (reinterpret_cast<uint8_t *>(&record->windowContextId + 1) - reinterpret_cast<uint8_t *>(record)) + record->size);
404 }
405
406 static void VNCSettings() {
407 NSDictionary *settings([NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Veency.plist", NSHomeDirectory()]]);
408
409 @synchronized (lock_) {
410 for (NSValue *handler in handlers_)
411 rfbUnregisterSecurityHandler(reinterpret_cast<rfbSecurityHandler *>([handler pointerValue]));
412 [handlers_ removeAllObjects];
413 }
414
415 @synchronized (condition_) {
416 if (screen_ == NULL)
417 return;
418
419 [reinterpret_cast<NSString *>(screen_->authPasswdData) release];
420 screen_->authPasswdData = NULL;
421
422 if (settings != nil)
423 if (NSString *password = [settings objectForKey:@"Password"])
424 if ([password length] != 0)
425 screen_->authPasswdData = [password retain];
426
427 NSNumber *cursor = [settings objectForKey:@"ShowCursor"];
428 cursor_ = cursor == nil ? true : [cursor boolValue];
429
430 if (clients_ != 0)
431 AshikaseSetEnabled(cursor_, true);
432 }
433 }
434
435 static void VNCNotifySettings(
436 CFNotificationCenterRef center,
437 void *observer,
438 CFStringRef name,
439 const void *object,
440 CFDictionaryRef info
441 ) {
442 VNCSettings();
443 }
444
445 static rfbBool VNCCheck(rfbClientPtr client, const char *data, int size) {
446 @synchronized (condition_) {
447 if (NSString *password = reinterpret_cast<NSString *>(screen_->authPasswdData)) {
448 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
449 rfbEncryptBytes(client->authChallenge, const_cast<char *>([password UTF8String]));
450 bool good(memcmp(client->authChallenge, data, size) == 0);
451 [pool release];
452 return good;
453 } return TRUE;
454 }
455 }
456
457 static bool iPad1_;
458
459 struct VeencyEvent {
460 struct GSEventRecord record;
461 struct {
462 struct GSEventRecordInfo info;
463 struct GSPathInfo path;
464 } data;
465 };
466
467 static void VNCPointerOld(int buttons, int x, int y, CGPoint location, int diff, bool twas, bool tis);
468 static void VNCPointerNew(int buttons, int x, int y, CGPoint location, int diff, bool twas, bool tis);
469
470 static void VNCPointer(int buttons, int x, int y, rfbClientPtr client) {
471 if (ratio_ == 0)
472 return;
473
474 CGPoint location = {x, y};
475
476 if (width_ > height_) {
477 int t(x);
478 x = height_ - 1 - y;
479 y = t;
480
481 if (!iPad1_) {
482 x = height_ - 1 - x;
483 y = width_ - 1 - y;
484 }
485 }
486
487 x /= ratio_;
488 y /= ratio_;
489
490 x_ = x; y_ = y;
491 int diff = buttons_ ^ buttons;
492 bool twas((buttons_ & 0x1) != 0);
493 bool tis((buttons & 0x1) != 0);
494 buttons_ = buttons;
495
496 rfbDefaultPtrAddEvent(buttons, x, y, client);
497
498 if (Ashikase(false)) {
499 AshikaseSendEvent(x, y, buttons);
500 return;
501 }
502
503 if (kCFCoreFoundationVersionNumber >= 800)
504 return VNCPointerNew(buttons, x, y, location, diff, twas, tis);
505 else
506 return VNCPointerOld(buttons, x, y, location, diff, twas, tis);
507 }
508
509 static void VNCPointerOld(int buttons, int x, int y, CGPoint location, int diff, bool twas, bool tis) {
510 mach_port_t purple(0);
511
512 if ((diff & 0x10) != 0) {
513 struct GSEventRecord record;
514
515 memset(&record, 0, sizeof(record));
516
517 record.type = (buttons & 0x10) != 0 ?
518 GSEventTypeHeadsetButtonDown :
519 GSEventTypeHeadsetButtonUp;
520
521 record.timestamp = GSCurrentEventTimestamp();
522
523 FixRecord(&record);
524 GSSendSystemEvent(&record);
525 }
526
527 if ((diff & 0x04) != 0) {
528 struct GSEventRecord record;
529
530 memset(&record, 0, sizeof(record));
531
532 record.type = (buttons & 0x04) != 0 ?
533 GSEventTypeMenuButtonDown :
534 GSEventTypeMenuButtonUp;
535
536 record.timestamp = GSCurrentEventTimestamp();
537
538 FixRecord(&record);
539 GSSendSystemEvent(&record);
540 }
541
542 if ((diff & 0x02) != 0) {
543 struct GSEventRecord record;
544
545 memset(&record, 0, sizeof(record));
546
547 record.type = (buttons & 0x02) != 0 ?
548 GSEventTypeLockButtonDown :
549 GSEventTypeLockButtonUp;
550
551 record.timestamp = GSCurrentEventTimestamp();
552
553 FixRecord(&record);
554 GSSendSystemEvent(&record);
555 }
556
557 if (twas != tis || tis) {
558 struct VeencyEvent event;
559
560 memset(&event, 0, sizeof(event));
561
562 event.record.type = GSEventTypeMouse;
563 event.record.locationInWindow.x = x;
564 event.record.locationInWindow.y = y;
565 event.record.timestamp = GSCurrentEventTimestamp();
566 event.record.size = sizeof(event.data);
567
568 event.data.info.handInfo.type = twas == tis ?
569 GSMouseEventTypeDragged :
570 tis ?
571 GSMouseEventTypeDown :
572 GSMouseEventTypeUp;
573
574 event.data.info.handInfo.x34 = 0x1;
575 event.data.info.handInfo.x38 = tis ? 0x1 : 0x0;
576
577 if (Level_ < 3)
578 event.data.info.pathPositions = 1;
579 else
580 event.data.info.x52 = 1;
581
582 event.data.path.x00 = 0x01;
583 event.data.path.x01 = 0x02;
584 event.data.path.x02 = tis ? 0x03 : 0x00;
585 event.data.path.position = event.record.locationInWindow;
586
587 mach_port_t port(0);
588
589 if (CAWindowServer *server = [CAWindowServer serverIfRunning]) {
590 NSArray *displays([server displays]);
591 if (displays != nil && [displays count] != 0)
592 if (CAWindowServerDisplay *display = [displays objectAtIndex:0])
593 port = [display clientPortAtPosition:location];
594 }
595
596 if (port == 0) {
597 if (purple == 0)
598 purple = (*GSTakePurpleSystemEventPort)();
599 port = purple;
600 }
601
602 FixRecord(&event.record);
603 GSSendEvent(&event.record, port);
604 }
605
606 if (purple != 0 && PurpleAllocated)
607 mach_port_deallocate(mach_task_self(), purple);
608 }
609
610 static void VNCSetSender(IOHIDEventRef event) {
611 IOHIDEventSetSenderID(event, 0xDEFACEDBEEFFECE5);
612 }
613
614 static void VNCSendHIDEvent(IOHIDEventRef event) {
615 static IOHIDEventSystemClientRef client_(NULL);
616 if (client_ == NULL)
617 client_ = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
618
619 VNCSetSender(event);
620 IOHIDEventSystemClientDispatchEvent(client_, event);
621 CFRelease(event);
622 }
623
624 static void VNCPointerNew(int buttons, int x, int y, CGPoint location, int diff, bool twas, bool tis) {
625 if ((diff & 0x10) != 0)
626 VNCSendHIDEvent(IOHIDEventCreateKeyboardEvent(kCFAllocatorDefault, mach_absolute_time(), kHIDPage_Telephony, kHIDUsage_Tfon_Flash, (buttons & 0x10) != 0, 0));
627 if ((diff & 0x04) != 0)
628 VNCSendHIDEvent(IOHIDEventCreateKeyboardEvent(kCFAllocatorDefault, mach_absolute_time(), kHIDPage_Consumer, kHIDUsage_Csmr_Menu, (buttons & 0x04) != 0, 0));
629 if ((diff & 0x02) != 0)
630 VNCSendHIDEvent(IOHIDEventCreateKeyboardEvent(kCFAllocatorDefault, mach_absolute_time(), kHIDPage_Consumer, kHIDUsage_Csmr_Power, (buttons & 0x02) != 0, 0));
631
632 uint32_t handm;
633 uint32_t fingerm;
634
635 if (twas == 0 && tis == 1) {
636 handm = kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch | kIOHIDDigitizerEventIdentity;
637 fingerm = kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch;
638 } else if (twas == 1 && tis == 1) {
639 handm = kIOHIDDigitizerEventPosition;
640 fingerm = kIOHIDDigitizerEventPosition;
641 } else if (twas == 1 && tis == 0) {
642 handm = kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch | kIOHIDDigitizerEventIdentity | kIOHIDDigitizerEventPosition;
643 fingerm = kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch;
644 } else return;
645
646 CAWindowServer *server([CAWindowServer serverIfRunning]);
647 if (server == nil)
648 return;
649
650 CAWindowServerDisplay *display([[server displays] objectAtIndex:0]);
651 if (display == nil)
652 return;
653
654 unsigned context([display contextIdAtPosition:CGPointMake(x, y)]);
655 mach_port_t port([display taskPortOfContextId:context]);
656 if (port == MACH_PORT_NULL)
657 return;
658
659 IOHIDEventSystemConnectionRef connection([[$BKAccessibility _eventRoutingClientConnectionManager] clientForTaskPort:port]);
660 if (connection == NULL)
661 return;
662
663 // XXX: I guess this isn't ambiguous, and it works
664 IOHIDFloat xf(x);
665 IOHIDFloat yf(y);
666
667 IOHIDEventRef hand(IOHIDEventCreateDigitizerEvent(kCFAllocatorDefault, mach_absolute_time(), kIOHIDDigitizerTransducerTypeHand, 1<<22, 1, handm, 0, xf, yf, 0, 0, 0, 0, 0, 0));
668 IOHIDEventSetIntegerValue(hand, kIOHIDEventFieldIsBuiltIn, true);
669 IOHIDEventSetIntegerValue(hand, kIOHIDEventFieldDigitizerIsDisplayIntegrated, true);
670
671 IOHIDEventRef finger(IOHIDEventCreateDigitizerFingerEvent(kCFAllocatorDefault, mach_absolute_time(), 3, 2, fingerm, xf, yf, 0, 0, 0, tis, tis, 0));
672 IOHIDEventAppendEvent(hand, finger);
673 CFRelease(finger);
674
675 VNCSetSender(hand);
676 IOHIDEventSystemConnectionDispatchEvent(connection, hand);
677 CFRelease(hand);
678 }
679
680 GSEventRef (*$GSEventCreateKeyEvent)(int, CGPoint, CFStringRef, CFStringRef, id, UniChar, short, short);
681 GSEventRef (*$GSCreateSyntheticKeyEvent)(UniChar, BOOL, BOOL);
682
683 static void VNCKeyboardNew(rfbBool down, rfbKeySym key, rfbClientPtr client) {
684 //NSLog(@"VNC d:%u k:%04x", down, key);
685
686 uint16_t usage;
687
688 switch (key) {
689 case XK_exclam: case XK_1: usage = kHIDUsage_Keyboard1; break;
690 case XK_at: case XK_2: usage = kHIDUsage_Keyboard2; break;
691 case XK_numbersign: case XK_3: usage = kHIDUsage_Keyboard3; break;
692 case XK_dollar: case XK_4: usage = kHIDUsage_Keyboard4; break;
693 case XK_percent: case XK_5: usage = kHIDUsage_Keyboard5; break;
694 case XK_asciicircum: case XK_6: usage = kHIDUsage_Keyboard6; break;
695 case XK_ampersand: case XK_7: usage = kHIDUsage_Keyboard7; break;
696 case XK_asterisk: case XK_8: usage = kHIDUsage_Keyboard8; break;
697 case XK_parenleft: case XK_9: usage = kHIDUsage_Keyboard9; break;
698 case XK_parenright: case XK_0: usage = kHIDUsage_Keyboard0; break;
699
700 case XK_A: case XK_a: usage = kHIDUsage_KeyboardA; break;
701 case XK_B: case XK_b: usage = kHIDUsage_KeyboardB; break;
702 case XK_C: case XK_c: usage = kHIDUsage_KeyboardC; break;
703 case XK_D: case XK_d: usage = kHIDUsage_KeyboardD; break;
704 case XK_E: case XK_e: usage = kHIDUsage_KeyboardE; break;
705 case XK_F: case XK_f: usage = kHIDUsage_KeyboardF; break;
706 case XK_G: case XK_g: usage = kHIDUsage_KeyboardG; break;
707 case XK_H: case XK_h: usage = kHIDUsage_KeyboardH; break;
708 case XK_I: case XK_i: usage = kHIDUsage_KeyboardI; break;
709 case XK_J: case XK_j: usage = kHIDUsage_KeyboardJ; break;
710 case XK_K: case XK_k: usage = kHIDUsage_KeyboardK; break;
711 case XK_L: case XK_l: usage = kHIDUsage_KeyboardL; break;
712 case XK_M: case XK_m: usage = kHIDUsage_KeyboardM; break;
713 case XK_N: case XK_n: usage = kHIDUsage_KeyboardN; break;
714 case XK_O: case XK_o: usage = kHIDUsage_KeyboardO; break;
715 case XK_P: case XK_p: usage = kHIDUsage_KeyboardP; break;
716 case XK_Q: case XK_q: usage = kHIDUsage_KeyboardQ; break;
717 case XK_R: case XK_r: usage = kHIDUsage_KeyboardR; break;
718 case XK_S: case XK_s: usage = kHIDUsage_KeyboardS; break;
719 case XK_T: case XK_t: usage = kHIDUsage_KeyboardT; break;
720 case XK_U: case XK_u: usage = kHIDUsage_KeyboardU; break;
721 case XK_V: case XK_v: usage = kHIDUsage_KeyboardV; break;
722 case XK_W: case XK_w: usage = kHIDUsage_KeyboardW; break;
723 case XK_X: case XK_x: usage = kHIDUsage_KeyboardX; break;
724 case XK_Y: case XK_y: usage = kHIDUsage_KeyboardY; break;
725 case XK_Z: case XK_z: usage = kHIDUsage_KeyboardZ; break;
726
727 case XK_underscore: case XK_minus: usage = kHIDUsage_KeyboardHyphen; break;
728 case XK_plus: case XK_equal: usage = kHIDUsage_KeyboardEqualSign; break;
729 case XK_braceleft: case XK_bracketleft: usage = kHIDUsage_KeyboardOpenBracket; break;
730 case XK_braceright: case XK_bracketright: usage = kHIDUsage_KeyboardCloseBracket; break;
731 case XK_bar: case XK_backslash: usage = kHIDUsage_KeyboardBackslash; break;
732 case XK_colon: case XK_semicolon: usage = kHIDUsage_KeyboardSemicolon; break;
733 case XK_quotedbl: case XK_apostrophe: usage = kHIDUsage_KeyboardQuote; break;
734 case XK_asciitilde: case XK_grave: usage = kHIDUsage_KeyboardGraveAccentAndTilde; break;
735 case XK_less: case XK_comma: usage = kHIDUsage_KeyboardComma; break;
736 case XK_greater: case XK_period: usage = kHIDUsage_KeyboardPeriod; break;
737 case XK_question: case XK_slash: usage = kHIDUsage_KeyboardSlash; break;
738
739 case XK_Return: usage = kHIDUsage_KeyboardReturnOrEnter; break;
740 case XK_BackSpace: usage = kHIDUsage_KeyboardDeleteOrBackspace; break;
741 case XK_Tab: usage = kHIDUsage_KeyboardTab; break;
742 case XK_space: usage = kHIDUsage_KeyboardSpacebar; break;
743
744 case XK_Shift_L: usage = kHIDUsage_KeyboardLeftShift; break;
745 case XK_Shift_R: usage = kHIDUsage_KeyboardRightShift; break;
746 case XK_Control_L: usage = kHIDUsage_KeyboardLeftControl; break;
747 case XK_Control_R: usage = kHIDUsage_KeyboardRightControl; break;
748 case XK_Meta_L: usage = kHIDUsage_KeyboardLeftAlt; break;
749 case XK_Meta_R: usage = kHIDUsage_KeyboardRightAlt; break;
750 case XK_Alt_L: usage = kHIDUsage_KeyboardLeftGUI; break;
751 case XK_Alt_R: usage = kHIDUsage_KeyboardRightGUI; break;
752
753 case XK_Up: usage = kHIDUsage_KeyboardUpArrow; break;
754 case XK_Down: usage = kHIDUsage_KeyboardDownArrow; break;
755 case XK_Left: usage = kHIDUsage_KeyboardLeftArrow; break;
756 case XK_Right: usage = kHIDUsage_KeyboardRightArrow; break;
757
758 case XK_Home: case XK_Begin: usage = kHIDUsage_KeyboardHome; break;
759 case XK_End: usage = kHIDUsage_KeyboardEnd; break;
760 case XK_Page_Up: usage = kHIDUsage_KeyboardPageUp; break;
761 case XK_Page_Down: usage = kHIDUsage_KeyboardPageDown; break;
762
763 default: return;
764 }
765
766 VNCSendHIDEvent(IOHIDEventCreateKeyboardEvent(kCFAllocatorDefault, mach_absolute_time(), kHIDPage_KeyboardOrKeypad, usage, down, 0));
767 }
768
769 static void VNCKeyboard(rfbBool down, rfbKeySym key, rfbClientPtr client) {
770 if (kCFCoreFoundationVersionNumber >= 800)
771 return VNCKeyboardNew(down, key, client);
772
773 if (!down)
774 return;
775
776 switch (key) {
777 case XK_Return: key = '\r'; break;
778 case XK_BackSpace: key = 0x7f; break;
779 }
780
781 if (key > 0xfff)
782 return;
783
784 CGPoint point(CGPointMake(x_, y_));
785
786 UniChar unicode(key);
787 CFStringRef string(NULL);
788
789 GSEventRef event0, event1(NULL);
790 if ($GSEventCreateKeyEvent != NULL) {
791 string = CFStringCreateWithCharacters(kCFAllocatorDefault, &unicode, 1);
792 event0 = (*$GSEventCreateKeyEvent)(10, point, string, string, nil, 0, 0, 1);
793 event1 = (*$GSEventCreateKeyEvent)(11, point, string, string, nil, 0, 0, 1);
794 } else if ($GSCreateSyntheticKeyEvent != NULL) {
795 event0 = (*$GSCreateSyntheticKeyEvent)(unicode, YES, YES);
796 GSEventRecord *record(_GSEventGetGSEventRecord(event0));
797 record->type = GSEventTypeKeyDown;
798 } else return;
799
800 mach_port_t port(0);
801
802 if (CAWindowServer *server = [CAWindowServer serverIfRunning]) {
803 NSArray *displays([server displays]);
804 if (displays != nil && [displays count] != 0)
805 if (CAWindowServerDisplay *display = [displays objectAtIndex:0])
806 port = [display clientPortAtPosition:point];
807 }
808
809 mach_port_t purple(0);
810
811 if (port == 0) {
812 if (purple == 0)
813 purple = (*GSTakePurpleSystemEventPort)();
814 port = purple;
815 }
816
817 if (port != 0) {
818 GSSendEvent(_GSEventGetGSEventRecord(event0), port);
819 if (event1 != NULL)
820 GSSendEvent(_GSEventGetGSEventRecord(event1), port);
821 }
822
823 if (purple != 0 && PurpleAllocated)
824 mach_port_deallocate(mach_task_self(), purple);
825
826 CFRelease(event0);
827 if (event1 != NULL)
828 CFRelease(event1);
829 if (string != NULL)
830 CFRelease(string);
831 }
832
833 static void VNCDisconnect(rfbClientPtr client) {
834 @synchronized (condition_) {
835 if (--clients_ == 0)
836 [VNCBridge performSelectorOnMainThread:@selector(removeStatusBarItem) withObject:nil waitUntilDone:YES];
837 }
838 }
839
840 static rfbNewClientAction VNCClient(rfbClientPtr client) {
841 @synchronized (condition_) {
842 if (screen_->authPasswdData != NULL) {
843 [VNCBridge performSelectorOnMainThread:@selector(registerClient) withObject:nil waitUntilDone:YES];
844 client->clientGoneHook = &VNCDisconnect;
845 return RFB_CLIENT_ACCEPT;
846 }
847 }
848
849 [condition_ lock];
850 client_ = client;
851 [VNCBridge performSelectorOnMainThread:@selector(askForConnection) withObject:nil waitUntilDone:NO];
852 while (action_ == RFB_CLIENT_ON_HOLD)
853 [condition_ wait];
854 rfbNewClientAction action(action_);
855 action_ = RFB_CLIENT_ON_HOLD;
856 [condition_ unlock];
857
858 if (action == RFB_CLIENT_ACCEPT)
859 client->clientGoneHook = &VNCDisconnect;
860 return action;
861 }
862
863 extern "C" bool GSSystemHasCapability(NSString *);
864
865 static CFTypeRef (*$GSSystemCopyCapability)(CFStringRef);
866 static CFTypeRef (*$GSSystemGetCapability)(CFStringRef);
867 static BOOL (*$MGGetBoolAnswer)(CFStringRef);
868
869 static void VNCSetup() {
870 rfbLogEnable(false);
871
872 @synchronized (condition_) {
873 int argc(1);
874 char *arg0(strdup("VNCServer"));
875 char *argv[] = {arg0, NULL};
876 screen_ = rfbGetScreen(&argc, argv, width_, height_, BitsPerSample, 3, BytesPerPixel);
877 free(arg0);
878
879 VNCSettings();
880 }
881
882 screen_->desktopName = strdup([[[NSProcessInfo processInfo] hostName] UTF8String]);
883
884 screen_->alwaysShared = TRUE;
885 screen_->handleEventsEagerly = TRUE;
886 screen_->deferUpdateTime = 1000 / 25;
887
888 screen_->serverFormat.redShift = BitsPerSample * 2;
889 screen_->serverFormat.greenShift = BitsPerSample * 1;
890 screen_->serverFormat.blueShift = BitsPerSample * 0;
891
892 $GSSystemCopyCapability = reinterpret_cast<CFTypeRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemCopyCapability"));
893 $GSSystemGetCapability = reinterpret_cast<CFTypeRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemGetCapability"));
894 $MGGetBoolAnswer = reinterpret_cast<BOOL (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "MGGetBoolAnswer"));
895
896 CFTypeRef opengles2;
897
898 if ($GSSystemCopyCapability != NULL) {
899 opengles2 = (*$GSSystemCopyCapability)(CFSTR("opengles-2"));
900 } else if ($GSSystemGetCapability != NULL) {
901 opengles2 = (*$GSSystemGetCapability)(CFSTR("opengles-2"));
902 if (opengles2 != NULL)
903 CFRetain(opengles2);
904 } else if ($MGGetBoolAnswer != NULL) {
905 opengles2 = $MGGetBoolAnswer(CFSTR("opengles-2")) ? kCFBooleanTrue : kCFBooleanFalse;
906 CFRetain(opengles2);
907 } else
908 opengles2 = NULL;
909
910 bool accelerated(opengles2 != NULL && [(NSNumber *)opengles2 boolValue]);
911
912 if (accelerated)
913 CoreSurfaceAcceleratorCreate(NULL, NULL, &accelerator_);
914
915 if (opengles2 != NULL)
916 CFRelease(opengles2);
917
918 if (accelerator_ != NULL)
919 buffer_ = CoreSurfaceBufferCreate((CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:
920 @"PurpleEDRAM", kCoreSurfaceBufferMemoryRegion,
921 [NSNumber numberWithBool:YES], kCoreSurfaceBufferGlobal,
922 [NSNumber numberWithInt:(width_ * BytesPerPixel)], kCoreSurfaceBufferPitch,
923 [NSNumber numberWithInt:width_], kCoreSurfaceBufferWidth,
924 [NSNumber numberWithInt:height_], kCoreSurfaceBufferHeight,
925 [NSNumber numberWithInt:'BGRA'], kCoreSurfaceBufferPixelFormat,
926 [NSNumber numberWithInt:(width_ * height_ * BytesPerPixel)], kCoreSurfaceBufferAllocSize,
927 nil]);
928 else
929 VNCBlack();
930
931 //screen_->frameBuffer = reinterpret_cast<char *>(mmap(NULL, sizeof(rfbPixel) * width_ * height_, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE | MAP_NOCACHE, VM_FLAGS_PURGABLE, 0));
932
933 CoreSurfaceBufferLock(buffer_, 3);
934 screen_->frameBuffer = reinterpret_cast<char *>(CoreSurfaceBufferGetBaseAddress(buffer_));
935 CoreSurfaceBufferUnlock(buffer_);
936
937 screen_->kbdAddEvent = &VNCKeyboard;
938 screen_->ptrAddEvent = &VNCPointer;
939
940 screen_->newClientHook = &VNCClient;
941 screen_->passwordCheck = &VNCCheck;
942
943 screen_->cursor = NULL;
944 }
945
946 static void VNCEnabled() {
947 if (screen_ == NULL)
948 return;
949
950 [lock_ lock];
951
952 bool enabled(true);
953 if (NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Veency.plist", NSHomeDirectory()]])
954 if (NSNumber *number = [settings objectForKey:@"Enabled"])
955 enabled = [number boolValue];
956
957 if (enabled != running_)
958 if (enabled) {
959 running_ = true;
960 screen_->socketState = RFB_SOCKET_INIT;
961 rfbInitServer(screen_);
962 rfbRunEventLoop(screen_, -1, true);
963 } else {
964 rfbShutdownServer(screen_, true);
965 running_ = false;
966 }
967
968 [lock_ unlock];
969 }
970
971 static void VNCNotifyEnabled(
972 CFNotificationCenterRef center,
973 void *observer,
974 CFStringRef name,
975 const void *object,
976 CFDictionaryRef info
977 ) {
978 VNCEnabled();
979 }
980
981 void (*$IOMobileFramebufferIsMainDisplay)(IOMobileFramebufferRef, int *);
982
983 static IOMobileFramebufferRef main_;
984 static CoreSurfaceBufferRef layer_;
985
986 static void OnLayer(IOMobileFramebufferRef fb, CoreSurfaceBufferRef layer) {
987 if (_unlikely(width_ == 0 || height_ == 0)) {
988 CGSize size;
989 IOMobileFramebufferGetDisplaySize(fb, &size);
990
991 width_ = size.width;
992 height_ = size.height;
993
994 if (width_ == 0 || height_ == 0)
995 return;
996
997 NSThread *thread([NSThread alloc]);
998
999 [thread
1000 initWithTarget:[VNCBridge class]
1001 selector:@selector(performSetup:)
1002 object:thread
1003 ];
1004
1005 [thread start];
1006 } else if (_unlikely(clients_ != 0)) {
1007 if (layer == NULL) {
1008 if (accelerator_ != NULL)
1009 memset(screen_->frameBuffer, 0, sizeof(rfbPixel) * width_ * height_);
1010 else
1011 VNCBlack();
1012 } else {
1013 if (accelerator_ != NULL)
1014 CoreSurfaceAcceleratorTransferSurface(accelerator_, layer, buffer_, options_);
1015 else {
1016 CoreSurfaceBufferLock(layer, 2);
1017 rfbPixel *data(reinterpret_cast<rfbPixel *>(CoreSurfaceBufferGetBaseAddress(layer)));
1018
1019 CoreSurfaceBufferFlushProcessorCaches(layer);
1020
1021 /*rfbPixel corner(data[0]);
1022 data[0] = 0;
1023 data[0] = corner;*/
1024
1025 screen_->frameBuffer = const_cast<char *>(reinterpret_cast<volatile char *>(data));
1026 CoreSurfaceBufferUnlock(layer);
1027 }
1028 }
1029
1030 rfbMarkRectAsModified(screen_, 0, 0, width_, height_);
1031 }
1032 }
1033
1034 static bool wait_ = false;
1035
1036 MSHook(kern_return_t, IOMobileFramebufferSwapSetLayer,
1037 IOMobileFramebufferRef fb,
1038 int layer,
1039 CoreSurfaceBufferRef buffer,
1040 CGRect bounds,
1041 CGRect frame,
1042 int flags
1043 ) {
1044 int main(false);
1045
1046 if (_unlikely(buffer == NULL))
1047 main = fb == main_;
1048 else if (_unlikely(fb == NULL))
1049 main = false;
1050 else if ($IOMobileFramebufferIsMainDisplay == NULL)
1051 main = true;
1052 else
1053 (*$IOMobileFramebufferIsMainDisplay)(fb, &main);
1054
1055 if (_likely(main)) {
1056 main_ = fb;
1057 if (wait_)
1058 layer_ = buffer;
1059 else
1060 OnLayer(fb, buffer);
1061 }
1062
1063 return _IOMobileFramebufferSwapSetLayer(fb, layer, buffer, bounds, frame, flags);
1064 }
1065
1066 // XXX: beg rpetrich for the type of this function
1067 extern "C" void *IOMobileFramebufferSwapWait(IOMobileFramebufferRef, void *, unsigned);
1068
1069 MSHook(void *, IOMobileFramebufferSwapWait, IOMobileFramebufferRef fb, void *arg1, unsigned flags) {
1070 void *value(_IOMobileFramebufferSwapWait(fb, arg1, flags));
1071 if (fb == main_)
1072 OnLayer(fb, layer_);
1073 return value;
1074 }
1075
1076 MSHook(void, rfbRegisterSecurityHandler, rfbSecurityHandler *handler) {
1077 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
1078
1079 @synchronized (lock_) {
1080 [handlers_ addObject:[NSValue valueWithPointer:handler]];
1081 _rfbRegisterSecurityHandler(handler);
1082 }
1083
1084 [pool release];
1085 }
1086
1087 template <typename Type_>
1088 static void dlset(Type_ &function, const char *name) {
1089 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
1090 }
1091
1092 MSInitialize {
1093 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
1094
1095 MSHookSymbol(GSTakePurpleSystemEventPort, "_GSGetPurpleSystemEventPort");
1096 if (GSTakePurpleSystemEventPort == NULL) {
1097 MSHookSymbol(GSTakePurpleSystemEventPort, "_GSCopyPurpleSystemEventPort");
1098 PurpleAllocated = true;
1099 }
1100
1101 if (dlsym(RTLD_DEFAULT, "GSLibraryCopyGenerationInfoValueForKey") != NULL)
1102 Level_ = 3;
1103 else if (dlsym(RTLD_DEFAULT, "GSKeyboardCreate") != NULL)
1104 Level_ = 2;
1105 else if (dlsym(RTLD_DEFAULT, "GSEventGetWindowContextId") != NULL)
1106 Level_ = 1;
1107 else
1108 Level_ = 0;
1109
1110 size_t size;
1111 sysctlbyname("hw.machine", NULL, &size, NULL, 0);
1112 char machine[size];
1113 sysctlbyname("hw.machine", machine, &size, NULL, 0);
1114 iPad1_ = strcmp(machine, "iPad1,1") == 0;
1115
1116 dlset($GSMainScreenScaleFactor, "GSMainScreenScaleFactor");
1117 dlset($GSEventCreateKeyEvent, "GSEventCreateKeyEvent");
1118 dlset($GSCreateSyntheticKeyEvent, "_GSCreateSyntheticKeyEvent");
1119 dlset($IOMobileFramebufferIsMainDisplay, "IOMobileFramebufferIsMainDisplay");
1120
1121 MSHookFunction(&IOMobileFramebufferSwapSetLayer, MSHake(IOMobileFramebufferSwapSetLayer));
1122 MSHookFunction(&rfbRegisterSecurityHandler, MSHake(rfbRegisterSecurityHandler));
1123
1124 if (wait_)
1125 MSHookFunction(&IOMobileFramebufferSwapWait, MSHake(IOMobileFramebufferSwapWait));
1126
1127 if ($SBAlertItem != nil) {
1128 $VNCAlertItem = objc_allocateClassPair($SBAlertItem, "VNCAlertItem", 0);
1129 MSAddMessage2(VNCAlertItem, "v@:@i", alertSheet,buttonClicked);
1130 MSAddMessage2(VNCAlertItem, "v@:cc", configure,requirePasscodeForActions);
1131 MSAddMessage0(VNCAlertItem, "v@:", performUnlockAction);
1132 objc_registerClassPair($VNCAlertItem);
1133 }
1134
1135 CFNotificationCenterAddObserver(
1136 CFNotificationCenterGetDarwinNotifyCenter(),
1137 NULL, &VNCNotifyEnabled, CFSTR("com.saurik.Veency-Enabled"), NULL, 0
1138 );
1139
1140 CFNotificationCenterAddObserver(
1141 CFNotificationCenterGetDarwinNotifyCenter(),
1142 NULL, &VNCNotifySettings, CFSTR("com.saurik.Veency-Settings"), NULL, 0
1143 );
1144
1145 condition_ = [[NSCondition alloc] init];
1146 lock_ = [[NSLock alloc] init];
1147 handlers_ = [[NSMutableSet alloc] init];
1148
1149 bool value;
1150
1151 value = true;
1152 cfTrue_ = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&value), sizeof(value));
1153
1154 value = false;
1155 cfFalse_ = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&value), sizeof(value));
1156
1157 cfEvent_ = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&event_), sizeof(event_), kCFAllocatorNull);
1158
1159 options_ = (CFDictionaryRef) [[NSDictionary dictionaryWithObjectsAndKeys:
1160 nil] retain];
1161
1162 [pool release];
1163 }