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