-
Notifications
You must be signed in to change notification settings - Fork 45
/
Tweak.mm
882 lines (684 loc) · 26.4 KB
/
Tweak.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/* Veency - VNC Remote Access Server for iPhoneOS
* Copyright (C) 2008-2012 Jay Freeman (saurik)
*/
/* GNU Affero General Public License, Version 3 {{{ */
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**/
/* }}} */
#define _trace() \
fprintf(stderr, "_trace()@%s:%u[%s]\n", __FILE__, __LINE__, __FUNCTION__)
#define _likely(expr) \
__builtin_expect(expr, 1)
#define _unlikely(expr) \
__builtin_expect(expr, 0)
#include <CydiaSubstrate.h>
#include <rfb/rfb.h>
#include <rfb/keysym.h>
#include <mach/mach_port.h>
#include <sys/mman.h>
#include <sys/sysctl.h>
#import <QuartzCore/CAWindowServer.h>
#import <QuartzCore/CAWindowServerDisplay.h>
#import <CoreGraphics/CGGeometry.h>
#import <GraphicsServices/GraphicsServices.h>
#import <Foundation/Foundation.h>
#import <IOMobileFramebuffer/IOMobileFramebuffer.h>
#import <IOKit/IOKitLib.h>
#import <UIKit/UIKit.h>
#import <SpringBoard/SBAlertItemsController.h>
#import <SpringBoard/SBDismissOnlyAlertItem.h>
#import <SpringBoard/SBStatusBarController.h>
#include "SpringBoardAccess.h"
extern "C" void CoreSurfaceBufferFlushProcessorCaches(CoreSurfaceBufferRef buffer);
extern "C" int CoreSurfaceAcceleratorTransferSurface(CoreSurfaceAcceleratorRef accel, CoreSurfaceBufferRef src, CoreSurfaceBufferRef dst, CFDictionaryRef dict);
static size_t width_;
static size_t height_;
static NSUInteger ratio_ = 0;
static const size_t BytesPerPixel = 4;
static const size_t BitsPerSample = 8;
static CoreSurfaceAcceleratorRef accelerator_;
static CoreSurfaceBufferRef buffer_;
static CFDictionaryRef options_;
static NSMutableSet *handlers_;
static rfbScreenInfoPtr screen_;
static bool running_;
static int buttons_;
static int x_, y_;
static unsigned clients_;
static CFMessagePortRef ashikase_;
static bool cursor_;
static rfbPixel *black_;
static void VNCBlack() {
if (_unlikely(black_ == NULL))
black_ = reinterpret_cast<rfbPixel *>(mmap(NULL, sizeof(rfbPixel) * width_ * height_, PROT_READ, MAP_ANON | MAP_PRIVATE | MAP_NOCACHE, VM_FLAGS_PURGABLE, 0));
screen_->frameBuffer = reinterpret_cast<char *>(black_);
}
static bool Ashikase(bool always) {
if (!always && !cursor_)
return false;
if (ashikase_ == NULL)
ashikase_ = CFMessagePortCreateRemote(kCFAllocatorDefault, CFSTR("jp.ashikase.mousesupport"));
if (ashikase_ != NULL)
return true;
cursor_ = false;
return false;
}
static CFDataRef cfTrue_;
static CFDataRef cfFalse_;
typedef struct {
float x, y;
int buttons;
BOOL absolute;
} MouseEvent;
static MouseEvent event_;
static CFDataRef cfEvent_;
typedef enum {
MouseMessageTypeEvent,
MouseMessageTypeSetEnabled
} MouseMessageType;
static void AshikaseSendEvent(float x, float y, int buttons = 0) {
event_.x = x;
event_.y = y;
event_.buttons = buttons;
event_.absolute = true;
CFMessagePortSendRequest(ashikase_, MouseMessageTypeEvent, cfEvent_, 0, 0, NULL, NULL);
}
static void AshikaseSetEnabled(bool enabled, bool always) {
if (!Ashikase(always))
return;
CFMessagePortSendRequest(ashikase_, MouseMessageTypeSetEnabled, enabled ? cfTrue_ : cfFalse_, 0, 0, NULL, NULL);
if (enabled)
AshikaseSendEvent(x_, y_);
}
MSClassHook(SBAlertItem)
MSClassHook(SBAlertItemsController)
MSClassHook(SBStatusBarController)
//@class VNCAlertItem;
@interface VNCAlertItem : SBAlertItem {
}
@end
static Class $VNCAlertItem;
static NSString *DialogTitle(@"Remote Access Request");
static NSString *DialogFormat(@"Accept connection from\n%s?\n\nVeency VNC Server\nby Jay Freeman (saurik)\[email protected]\nhttp://www.saurik.com/\n\nSet a VNC password in Settings!");
static NSString *DialogAccept(@"Accept");
static NSString *DialogReject(@"Reject");
static volatile rfbNewClientAction action_ = RFB_CLIENT_ON_HOLD;
static NSCondition *condition_;
static NSLock *lock_;
static rfbClientPtr client_;
static void VNCSetup();
static void VNCEnabled();
static void OnUserNotification(CFUserNotificationRef notification, CFOptionFlags flags) {
[condition_ lock];
if ((flags & 0x3) == 1)
action_ = RFB_CLIENT_ACCEPT;
else
action_ = RFB_CLIENT_REFUSE;
[condition_ signal];
[condition_ unlock];
CFRelease(notification);
}
@interface VNCBridge : NSObject {
}
+ (void) askForConnection;
+ (void) removeStatusBarItem;
+ (void) registerClient;
@end
@implementation VNCBridge
+ (void) askForConnection {
if ($VNCAlertItem != nil) {
[[$SBAlertItemsController sharedInstance] activateAlertItem:[[[$VNCAlertItem alloc] init] autorelease]];
return;
}
SInt32 error;
CFUserNotificationRef notification(CFUserNotificationCreate(kCFAllocatorDefault, 0, kCFUserNotificationPlainAlertLevel, &error, (CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:
DialogTitle, kCFUserNotificationAlertHeaderKey,
[NSString stringWithFormat:DialogFormat, client_->host], kCFUserNotificationAlertMessageKey,
DialogAccept, kCFUserNotificationAlternateButtonTitleKey,
DialogReject, kCFUserNotificationDefaultButtonTitleKey,
nil]));
if (error != 0) {
CFRelease(notification);
notification = NULL;
}
if (notification == NULL) {
[condition_ lock];
action_ = RFB_CLIENT_REFUSE;
[condition_ signal];
[condition_ unlock];
return;
}
CFRunLoopSourceRef source(CFUserNotificationCreateRunLoopSource(kCFAllocatorDefault, notification, &OnUserNotification, 0));
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
}
+ (void) removeStatusBarItem {
AshikaseSetEnabled(false, false);
if (SBA_available())
SBA_removeStatusBarImage(const_cast<char *>("Veency"));
else if ($SBStatusBarController != nil)
[[$SBStatusBarController sharedStatusBarController] removeStatusBarItem:@"Veency"];
else if (UIApplication *app = [UIApplication sharedApplication])
[app removeStatusBarImageNamed:@"Veency"];
}
+ (void) registerClient {
// XXX: this could find a better home
if (ratio_ == 0) {
UIScreen *screen([UIScreen mainScreen]);
if ([screen respondsToSelector:@selector(scale)])
ratio_ = [screen scale];
else
ratio_ = 1;
}
++clients_;
AshikaseSetEnabled(true, false);
if (SBA_available())
SBA_addStatusBarImage(const_cast<char *>("Veency"));
else if ($SBStatusBarController != nil)
[[$SBStatusBarController sharedStatusBarController] addStatusBarItem:@"Veency"];
else if (UIApplication *app = [UIApplication sharedApplication])
[app addStatusBarImageNamed:@"Veency"];
}
+ (void) performSetup:(NSThread *)thread {
NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
[thread autorelease];
VNCSetup();
VNCEnabled();
[pool release];
}
@end
MSInstanceMessage2(void, VNCAlertItem, alertSheet,buttonClicked, id, sheet, int, button) {
[condition_ lock];
switch (button) {
case 1:
action_ = RFB_CLIENT_ACCEPT;
@synchronized (condition_) {
[VNCBridge registerClient];
}
break;
case 2:
action_ = RFB_CLIENT_REFUSE;
break;
}
[condition_ signal];
[condition_ unlock];
[self dismiss];
}
MSInstanceMessage2(void, VNCAlertItem, configure,requirePasscodeForActions, BOOL, configure, BOOL, require) {
UIModalView *sheet([self alertSheet]);
[sheet setDelegate:self];
[sheet setTitle:DialogTitle];
[sheet setBodyText:[NSString stringWithFormat:DialogFormat, client_->host]];
[sheet addButtonWithTitle:DialogAccept];
[sheet addButtonWithTitle:DialogReject];
}
MSInstanceMessage0(void, VNCAlertItem, performUnlockAction) {
[[$SBAlertItemsController sharedInstance] activateAlertItem:self];
}
static mach_port_t (*GSTakePurpleSystemEventPort)(void);
static bool PurpleAllocated;
static int Level_;
static void FixRecord(GSEventRecord *record) {
if (Level_ < 1)
memmove(&record->windowContextId, &record->windowContextId + 1, sizeof(*record) - (reinterpret_cast<uint8_t *>(&record->windowContextId + 1) - reinterpret_cast<uint8_t *>(record)) + record->size);
}
static void VNCSettings() {
NSDictionary *settings([NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Veency.plist", NSHomeDirectory()]]);
@synchronized (lock_) {
for (NSValue *handler in handlers_)
rfbUnregisterSecurityHandler(reinterpret_cast<rfbSecurityHandler *>([handler pointerValue]));
[handlers_ removeAllObjects];
}
@synchronized (condition_) {
if (screen_ == NULL)
return;
[reinterpret_cast<NSString *>(screen_->authPasswdData) release];
screen_->authPasswdData = NULL;
if (settings != nil)
if (NSString *password = [settings objectForKey:@"Password"])
if ([password length] != 0)
screen_->authPasswdData = [password retain];
NSNumber *cursor = [settings objectForKey:@"ShowCursor"];
cursor_ = cursor == nil ? true : [cursor boolValue];
if (clients_ != 0)
AshikaseSetEnabled(cursor_, true);
}
}
static void VNCNotifySettings(
CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef info
) {
VNCSettings();
}
static rfbBool VNCCheck(rfbClientPtr client, const char *data, int size) {
@synchronized (condition_) {
if (NSString *password = reinterpret_cast<NSString *>(screen_->authPasswdData)) {
NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
rfbEncryptBytes(client->authChallenge, const_cast<char *>([password UTF8String]));
bool good(memcmp(client->authChallenge, data, size) == 0);
[pool release];
return good;
} return TRUE;
}
}
static bool iPad1_;
struct VeencyEvent {
struct GSEventRecord record;
struct {
struct GSEventRecordInfo info;
struct GSPathInfo path;
} data;
};
static void VNCPointer(int buttons, int x, int y, rfbClientPtr client) {
if (ratio_ == 0)
return;
CGPoint location = {x, y};
if (width_ > height_) {
int t(x);
x = height_ - 1 - y;
y = t;
if (!iPad1_) {
x = height_ - 1 - x;
y = width_ - 1 - y;
}
}
x /= ratio_;
y /= ratio_;
x_ = x; y_ = y;
int diff = buttons_ ^ buttons;
bool twas((buttons_ & 0x1) != 0);
bool tis((buttons & 0x1) != 0);
buttons_ = buttons;
rfbDefaultPtrAddEvent(buttons, x, y, client);
if (Ashikase(false)) {
AshikaseSendEvent(x, y, buttons);
return;
}
mach_port_t purple(0);
if ((diff & 0x10) != 0) {
struct GSEventRecord record;
memset(&record, 0, sizeof(record));
record.type = (buttons & 0x10) != 0 ?
GSEventTypeHeadsetButtonDown :
GSEventTypeHeadsetButtonUp;
record.timestamp = GSCurrentEventTimestamp();
FixRecord(&record);
GSSendSystemEvent(&record);
}
if ((diff & 0x04) != 0) {
struct GSEventRecord record;
memset(&record, 0, sizeof(record));
record.type = (buttons & 0x04) != 0 ?
GSEventTypeMenuButtonDown :
GSEventTypeMenuButtonUp;
record.timestamp = GSCurrentEventTimestamp();
FixRecord(&record);
GSSendSystemEvent(&record);
}
if ((diff & 0x02) != 0) {
struct GSEventRecord record;
memset(&record, 0, sizeof(record));
record.type = (buttons & 0x02) != 0 ?
GSEventTypeLockButtonDown :
GSEventTypeLockButtonUp;
record.timestamp = GSCurrentEventTimestamp();
FixRecord(&record);
GSSendSystemEvent(&record);
}
if (twas != tis || tis) {
struct VeencyEvent event;
memset(&event, 0, sizeof(event));
event.record.type = GSEventTypeMouse;
event.record.locationInWindow.x = x;
event.record.locationInWindow.y = y;
event.record.timestamp = GSCurrentEventTimestamp();
event.record.size = sizeof(event.data);
event.data.info.handInfo.type = twas == tis ?
GSMouseEventTypeDragged :
tis ?
GSMouseEventTypeDown :
GSMouseEventTypeUp;
event.data.info.handInfo.x34 = 0x1;
event.data.info.handInfo.x38 = tis ? 0x1 : 0x0;
if (Level_ < 3)
event.data.info.pathPositions = 1;
else
event.data.info.x52 = 1;
event.data.path.x00 = 0x01;
event.data.path.x01 = 0x02;
event.data.path.x02 = tis ? 0x03 : 0x00;
event.data.path.position = event.record.locationInWindow;
mach_port_t port(0);
if (CAWindowServer *server = [CAWindowServer serverIfRunning]) {
NSArray *displays([server displays]);
if (displays != nil && [displays count] != 0)
if (CAWindowServerDisplay *display = [displays objectAtIndex:0])
port = [display clientPortAtPosition:location];
}
if (port == 0) {
if (purple == 0)
purple = (*GSTakePurpleSystemEventPort)();
port = purple;
}
FixRecord(&event.record);
GSSendEvent(&event.record, port);
}
if (purple != 0 && PurpleAllocated)
mach_port_deallocate(mach_task_self(), purple);
}
GSEventRef (*$GSEventCreateKeyEvent)(int, CGPoint, CFStringRef, CFStringRef, id, UniChar, short, short);
GSEventRef (*$GSCreateSyntheticKeyEvent)(UniChar, BOOL, BOOL);
static void VNCKeyboard(rfbBool down, rfbKeySym key, rfbClientPtr client) {
if (!down)
return;
switch (key) {
case XK_Return: key = '\r'; break;
case XK_BackSpace: key = 0x7f; break;
}
if (key > 0xfff)
return;
CGPoint point(CGPointMake(x_, y_));
UniChar unicode(key);
CFStringRef string(NULL);
GSEventRef event0, event1(NULL);
if ($GSEventCreateKeyEvent != NULL) {
string = CFStringCreateWithCharacters(kCFAllocatorDefault, &unicode, 1);
event0 = (*$GSEventCreateKeyEvent)(10, point, string, string, nil, 0, 0, 1);
event1 = (*$GSEventCreateKeyEvent)(11, point, string, string, nil, 0, 0, 1);
} else if ($GSCreateSyntheticKeyEvent != NULL) {
event0 = (*$GSCreateSyntheticKeyEvent)(unicode, YES, YES);
GSEventRecord *record(_GSEventGetGSEventRecord(event0));
record->type = GSEventTypeKeyDown;
} else return;
mach_port_t port(0);
if (CAWindowServer *server = [CAWindowServer serverIfRunning]) {
NSArray *displays([server displays]);
if (displays != nil && [displays count] != 0)
if (CAWindowServerDisplay *display = [displays objectAtIndex:0])
port = [display clientPortAtPosition:point];
}
mach_port_t purple(0);
if (port == 0) {
if (purple == 0)
purple = (*GSTakePurpleSystemEventPort)();
port = purple;
}
if (port != 0) {
GSSendEvent(_GSEventGetGSEventRecord(event0), port);
if (event1 != NULL)
GSSendEvent(_GSEventGetGSEventRecord(event1), port);
}
if (purple != 0 && PurpleAllocated)
mach_port_deallocate(mach_task_self(), purple);
CFRelease(event0);
if (event1 != NULL)
CFRelease(event1);
if (string != NULL)
CFRelease(string);
}
static void VNCDisconnect(rfbClientPtr client) {
@synchronized (condition_) {
if (--clients_ == 0)
[VNCBridge performSelectorOnMainThread:@selector(removeStatusBarItem) withObject:nil waitUntilDone:YES];
}
}
static rfbNewClientAction VNCClient(rfbClientPtr client) {
@synchronized (condition_) {
if (screen_->authPasswdData != NULL) {
[VNCBridge performSelectorOnMainThread:@selector(registerClient) withObject:nil waitUntilDone:YES];
client->clientGoneHook = &VNCDisconnect;
return RFB_CLIENT_ACCEPT;
}
}
[condition_ lock];
client_ = client;
[VNCBridge performSelectorOnMainThread:@selector(askForConnection) withObject:nil waitUntilDone:NO];
while (action_ == RFB_CLIENT_ON_HOLD)
[condition_ wait];
rfbNewClientAction action(action_);
action_ = RFB_CLIENT_ON_HOLD;
[condition_ unlock];
if (action == RFB_CLIENT_ACCEPT)
client->clientGoneHook = &VNCDisconnect;
return action;
}
//extern "C" bool GSSystemHasCapability(NSString *);
static CFTypeRef (*$GSSystemCopyCapability)(CFStringRef);
static CFTypeRef (*$GSSystemGetCapability)(CFStringRef);
static void VNCSetup() {
rfbLogEnable(false);
@synchronized (condition_) {
int argc(1);
char *arg0(strdup("VNCServer"));
char *argv[] = {arg0, NULL};
screen_ = rfbGetScreen(&argc, argv, width_, height_, BitsPerSample, 3, BytesPerPixel);
free(arg0);
VNCSettings();
}
screen_->desktopName = strdup([[[NSProcessInfo processInfo] hostName] UTF8String]);
screen_->alwaysShared = TRUE;
screen_->handleEventsEagerly = TRUE;
screen_->deferUpdateTime = 1000 / 25;
screen_->serverFormat.redShift = BitsPerSample * 2;
screen_->serverFormat.greenShift = BitsPerSample * 1;
screen_->serverFormat.blueShift = BitsPerSample * 0;
$GSSystemCopyCapability = reinterpret_cast<CFTypeRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemCopyCapability"));
$GSSystemGetCapability = reinterpret_cast<CFTypeRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemGetCapability"));
CFTypeRef opengles2;
if ($GSSystemCopyCapability != NULL) {
opengles2 = (*$GSSystemCopyCapability)(CFSTR("opengles-2"));
} else if ($GSSystemGetCapability != NULL) {
opengles2 = (*$GSSystemGetCapability)(CFSTR("opengles-2"));
if (opengles2 != NULL)
CFRetain(opengles2);
} else
opengles2 = NULL;
bool accelerated(opengles2 != NULL && [(NSNumber *)opengles2 boolValue]);
if (accelerated)
CoreSurfaceAcceleratorCreate(NULL, NULL, &accelerator_);
if (opengles2 != NULL)
CFRelease(opengles2);
if (accelerator_ != NULL)
buffer_ = CoreSurfaceBufferCreate((CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:
@"PurpleEDRAM", kCoreSurfaceBufferMemoryRegion,
[NSNumber numberWithBool:YES], kCoreSurfaceBufferGlobal,
[NSNumber numberWithInt:(width_ * BytesPerPixel)], kCoreSurfaceBufferPitch,
[NSNumber numberWithInt:width_], kCoreSurfaceBufferWidth,
[NSNumber numberWithInt:height_], kCoreSurfaceBufferHeight,
[NSNumber numberWithInt:'BGRA'], kCoreSurfaceBufferPixelFormat,
[NSNumber numberWithInt:(width_ * height_ * BytesPerPixel)], kCoreSurfaceBufferAllocSize,
nil]);
else
VNCBlack();
//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));
CoreSurfaceBufferLock(buffer_, 3);
screen_->frameBuffer = reinterpret_cast<char *>(CoreSurfaceBufferGetBaseAddress(buffer_));
CoreSurfaceBufferUnlock(buffer_);
screen_->kbdAddEvent = &VNCKeyboard;
screen_->ptrAddEvent = &VNCPointer;
screen_->newClientHook = &VNCClient;
screen_->passwordCheck = &VNCCheck;
screen_->cursor = NULL;
}
static void VNCEnabled() {
[lock_ lock];
bool enabled(true);
if (NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Veency.plist", NSHomeDirectory()]])
if (NSNumber *number = [settings objectForKey:@"Enabled"])
enabled = [number boolValue];
if (enabled != running_)
if (enabled) {
running_ = true;
screen_->socketState = RFB_SOCKET_INIT;
rfbInitServer(screen_);
rfbRunEventLoop(screen_, -1, true);
} else {
rfbShutdownServer(screen_, true);
running_ = false;
}
[lock_ unlock];
}
static void VNCNotifyEnabled(
CFNotificationCenterRef center,
void *observer,
CFStringRef name,
const void *object,
CFDictionaryRef info
) {
VNCEnabled();
}
void (*$IOMobileFramebufferIsMainDisplay)(IOMobileFramebufferRef, int *);
static IOMobileFramebufferRef main_;
static CoreSurfaceBufferRef layer_;
static void OnLayer(IOMobileFramebufferRef fb, CoreSurfaceBufferRef layer) {
if (_unlikely(width_ == 0 || height_ == 0)) {
CGSize size;
IOMobileFramebufferGetDisplaySize(fb, &size);
width_ = size.width;
height_ = size.height;
if (width_ == 0 || height_ == 0)
return;
NSThread *thread([NSThread alloc]);
[thread
initWithTarget:[VNCBridge class]
selector:@selector(performSetup:)
object:thread
];
[thread start];
} else if (_unlikely(clients_ != 0)) {
if (layer == NULL) {
if (accelerator_ != NULL)
memset(screen_->frameBuffer, 0, sizeof(rfbPixel) * width_ * height_);
else
VNCBlack();
} else {
if (accelerator_ != NULL)
CoreSurfaceAcceleratorTransferSurface(accelerator_, layer, buffer_, options_);
else {
CoreSurfaceBufferLock(layer, 2);
rfbPixel *data(reinterpret_cast<rfbPixel *>(CoreSurfaceBufferGetBaseAddress(layer)));
CoreSurfaceBufferFlushProcessorCaches(layer);
/*rfbPixel corner(data[0]);
data[0] = 0;
data[0] = corner;*/
screen_->frameBuffer = const_cast<char *>(reinterpret_cast<volatile char *>(data));
CoreSurfaceBufferUnlock(layer);
}
}
rfbMarkRectAsModified(screen_, 0, 0, width_, height_);
}
}
static bool wait_ = false;
MSHook(kern_return_t, IOMobileFramebufferSwapSetLayer,
IOMobileFramebufferRef fb,
int layer,
CoreSurfaceBufferRef buffer,
CGRect bounds,
CGRect frame,
int flags
) {
int main(false);
if (_unlikely(buffer == NULL))
main = fb == main_;
else if (_unlikely(fb == NULL))
main = false;
else if ($IOMobileFramebufferIsMainDisplay == NULL)
main = true;
else
(*$IOMobileFramebufferIsMainDisplay)(fb, &main);
if (_likely(main)) {
main_ = fb;
if (wait_)
layer_ = buffer;
else
OnLayer(fb, buffer);
}
return _IOMobileFramebufferSwapSetLayer(fb, layer, buffer, bounds, frame, flags);
}
// XXX: beg rpetrich for the type of this function
extern "C" void *IOMobileFramebufferSwapWait(IOMobileFramebufferRef, void *, unsigned);
MSHook(void *, IOMobileFramebufferSwapWait, IOMobileFramebufferRef fb, void *arg1, unsigned flags) {
void *value(_IOMobileFramebufferSwapWait(fb, arg1, flags));
if (fb == main_)
OnLayer(fb, layer_);
return value;
}
MSHook(void, rfbRegisterSecurityHandler, rfbSecurityHandler *handler) {
NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
@synchronized (lock_) {
[handlers_ addObject:[NSValue valueWithPointer:handler]];
_rfbRegisterSecurityHandler(handler);
}
[pool release];
}
template <typename Type_>
static void dlset(Type_ &function, const char *name) {
function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
}
MSInitialize {
NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
MSHookSymbol(GSTakePurpleSystemEventPort, "_GSGetPurpleSystemEventPort");
if (GSTakePurpleSystemEventPort == NULL) {
MSHookSymbol(GSTakePurpleSystemEventPort, "_GSCopyPurpleSystemEventPort");
PurpleAllocated = true;
}
if (dlsym(RTLD_DEFAULT, "GSLibraryCopyGenerationInfoValueForKey") != NULL)
Level_ = 3;
else if (dlsym(RTLD_DEFAULT, "GSKeyboardCreate") != NULL)
Level_ = 2;
else if (dlsym(RTLD_DEFAULT, "GSEventGetWindowContextId") != NULL)
Level_ = 1;
else
Level_ = 0;
size_t size;
sysctlbyname("hw.machine", NULL, &size, NULL, 0);
char machine[size];
sysctlbyname("hw.machine", machine, &size, NULL, 0);
iPad1_ = strcmp(machine, "iPad1,1") == 0;
dlset($GSEventCreateKeyEvent, "GSEventCreateKeyEvent");
dlset($GSCreateSyntheticKeyEvent, "_GSCreateSyntheticKeyEvent");
dlset($IOMobileFramebufferIsMainDisplay, "IOMobileFramebufferIsMainDisplay");
MSHookFunction(&IOMobileFramebufferSwapSetLayer, MSHake(IOMobileFramebufferSwapSetLayer));
MSHookFunction(&rfbRegisterSecurityHandler, MSHake(rfbRegisterSecurityHandler));
if (wait_)
MSHookFunction(&IOMobileFramebufferSwapWait, MSHake(IOMobileFramebufferSwapWait));
if ($SBAlertItem != nil) {
$VNCAlertItem = objc_allocateClassPair($SBAlertItem, "VNCAlertItem", 0);
MSAddMessage2(VNCAlertItem, "v@:@i", alertSheet,buttonClicked);
MSAddMessage2(VNCAlertItem, "v@:cc", configure,requirePasscodeForActions);
MSAddMessage0(VNCAlertItem, "v@:", performUnlockAction);
objc_registerClassPair($VNCAlertItem);
}
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
NULL, &VNCNotifyEnabled, CFSTR("com.saurik.Veency-Enabled"), NULL, 0
);
CFNotificationCenterAddObserver(
CFNotificationCenterGetDarwinNotifyCenter(),
NULL, &VNCNotifySettings, CFSTR("com.saurik.Veency-Settings"), NULL, 0
);
condition_ = [[NSCondition alloc] init];
lock_ = [[NSLock alloc] init];
handlers_ = [[NSMutableSet alloc] init];
bool value;
value = true;
cfTrue_ = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&value), sizeof(value));
value = false;
cfFalse_ = CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&value), sizeof(value));
cfEvent_ = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&event_), sizeof(event_), kCFAllocatorNull);
options_ = (CFDictionaryRef) [[NSDictionary dictionaryWithObjectsAndKeys:
nil] retain];
[pool release];
}