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