forked from julioverne/screendump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.mm
608 lines (508 loc) · 23 KB
/
main.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
#include <errno.h>
#include <substrate.h>
#include <rfb/rfb.h>
#define kSettingsPath @"//var/mobile/Library/Preferences/com.torrekie.screendump.plist"
static bool CCSisEnabled = true;
static NSString *CCSPassword = nil;
static rfbScreenInfoPtr screen;
static bool isVNCRunning;
// static NSCondition *condition_;
static NSLock *lock;
static size_t width;
static size_t height;
static size_t byte_per_pixel;
static size_t bytesPerRow;
static size_t pixelF;
static size_t bits_per_sample = 8;
static size_t size_image;
static size_t prefferH;
static size_t prefferW;
static CFTypeRef (*$GSSystemCopyCapability)(CFStringRef);
static CFTypeRef (*$GSSystemGetCapability)(CFStringRef);
static BOOL (*$MGGetBoolAnswer)(CFStringRef);
typedef void *IOMobileFramebufferRef;
typedef void *IOSurfaceAcceleratorRef;
typedef struct __IOMobileFramebuffer *IOMobileFramebufferConnection;
extern CFStringRef kIOSurfaceMemoryRegion;
extern const CFStringRef kIOSurfaceIsGlobal;
extern "C" void IOMobileFramebufferGetDisplaySize(IOMobileFramebufferRef connect, CGSize *size);
extern "C" int IOSurfaceAcceleratorCreate(CFAllocatorRef allocator, int type, IOSurfaceAcceleratorRef *accel);
extern "C" unsigned int IOSurfaceAcceleratorTransferSurface(IOSurfaceAcceleratorRef accelerator, IOSurfaceRef dest, IOSurfaceRef src, void *, void *, void *, void *);
extern "C" kern_return_t IOMobileFramebufferSwapSetLayer(
IOMobileFramebufferRef fb,
int layer,
IOSurfaceRef buffer,
CGRect bounds,
CGRect frame,
int flags
);
typedef mach_port_t io_service_t;
typedef kern_return_t IOReturn;
typedef IOReturn IOMobileFramebufferReturn;
typedef io_service_t IOMobileFramebufferService;
extern "C" void IOSurfaceFlushProcessorCaches(IOSurfaceRef buffer);
extern "C" int IOSurfaceLock(IOSurfaceRef surface, uint32_t options, uint32_t *seed);
extern "C" int IOSurfaceUnlock(IOSurfaceRef surface, uint32_t options, uint32_t *seed);
extern "C" Boolean IOSurfaceIsInUse(IOSurfaceRef buffer);
extern "C" CFMutableDictionaryRef IOServiceMatching(const char *name);
extern "C" const mach_port_t kIOMasterPortDefault;
extern "C" io_service_t IOServiceGetMatchingService(mach_port_t masterPort, CFDictionaryRef matching);
extern "C" IOMobileFramebufferReturn IOMobileFramebufferGetLayerDefaultSurface(IOMobileFramebufferRef pointer, int surface, IOSurfaceRef *buffer);
extern "C" IOMobileFramebufferReturn IOMobileFramebufferOpen(IOMobileFramebufferService service, mach_port_t owningTask, unsigned int type, IOMobileFramebufferRef *pointer);
extern "C" IOMobileFramebufferReturn IOMobileFramebufferGetMainDisplay(IOMobileFramebufferRef *pointer);
static IOSurfaceAcceleratorRef accelerator;
static IOSurfaceRef static_buffer;
static void VNCSettings(bool shouldStart, NSString *password);
static void VNCUpdateRunState(bool shouldStart);
static void handleVNCKeyboard(rfbBool down, rfbKeySym key, rfbClientPtr client);
static void handleVNCPointer(int buttons, int x, int y, rfbClientPtr client);
static BOOL isLoopFrame;
static rfbBool VNCCheck(rfbClientPtr client, const char *data, int size)
{
NSString *password = reinterpret_cast<NSString *>(screen->authPasswdData);
if(!password) {
return TRUE;
}
if ([password length] == 0) {
return TRUE;
}
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;
}
static IOSurfaceRef screenSurface = NULL;
static IOMobileFramebufferRef framebufferConnection = NULL;
@interface FrameUpdater : NSObject
@property (nonatomic, retain) NSTimer* myTimer;
- (void)startFrameLoop;
- (void)stopFrameLoop;
@end
static void VNCSetup()
{
if(!screenSurface) {
IOSurfaceAcceleratorCreate(kCFAllocatorDefault, 0, &accelerator);
IOMobileFramebufferGetMainDisplay(&framebufferConnection);
IOMobileFramebufferGetLayerDefaultSurface(framebufferConnection, 0, &screenSurface);
//CGSize size;
//IOMobileFramebufferGetDisplaySize(framebufferConnection, &size);
//width = size.width/2;
//height = size.height/2;
width = prefferW==0?IOSurfaceGetWidth(screenSurface):prefferW;
height = prefferW==0?IOSurfaceGetHeight(screenSurface):prefferH;
size_image = IOSurfaceGetAllocSize(screenSurface);
bytesPerRow = IOSurfaceGetBytesPerRow(screenSurface);
pixelF = IOSurfaceGetPixelFormat(screenSurface);
byte_per_pixel = 4;//IOSurfaceGetBytesPerElement(screenSurface);
bits_per_sample = 8;
static_buffer = IOSurfaceCreate((CFDictionaryRef) [NSDictionary dictionaryWithObjectsAndKeys:
@"PurpleEDRAM", kIOSurfaceMemoryRegion,
//[NSNumber numberWithBool:YES], kIOSurfaceIsGlobal,
[NSNumber numberWithInt:byte_per_pixel*width], kIOSurfaceBytesPerRow,
[NSNumber numberWithInt:byte_per_pixel], kIOSurfaceBytesPerElement,
[NSNumber numberWithInt:width], kIOSurfaceWidth,
[NSNumber numberWithInt:height], kIOSurfaceHeight,
[NSNumber numberWithInt:'BGRA'], kIOSurfacePixelFormat,
[NSNumber numberWithInt:(width*height*byte_per_pixel)], kIOSurfaceAllocSize,
nil]);
}
int argc(1);
char *arg0(strdup("ScreenDumpVNC"));
char *argv[] = {arg0, NULL};
screen = rfbGetScreen(&argc, argv, width, height, bits_per_sample, 3, byte_per_pixel);
screen->frameBuffer = reinterpret_cast<char *>(IOSurfaceGetBaseAddress((IOSurfaceRef)CFRetain(static_buffer)));
screen->serverFormat.redShift = bits_per_sample * 2;
screen->serverFormat.greenShift = bits_per_sample * 1;
screen->serverFormat.blueShift = bits_per_sample * 0;
screen->kbdAddEvent = &handleVNCKeyboard;
screen->ptrAddEvent = &handleVNCPointer;
screen->passwordCheck = &VNCCheck;
free(arg0);
VNCUpdateRunState(CCSisEnabled);
}
static void VNCSettings(bool shouldStart, NSString* password)
{
CCSisEnabled = shouldStart;
if(password) {
CCSPassword = password;
}
NSString *sEnabled = CCSisEnabled ? @"YES": @"NO";
VNCUpdateRunState(CCSisEnabled);
}
static void VNCUpdateRunState(bool shouldStart)
{
if(screen == NULL) {
return;
}
if(CCSPassword && CCSPassword.length) {
screen->authPasswdData = (void *) CCSPassword;
} else {
screen->authPasswdData = NULL;
}
if(shouldStart == isVNCRunning) {
return;
}
if(shouldStart) {
rfbInitServer(screen);
rfbRunEventLoop(screen, -1, true);
isLoopFrame = YES;
[[FrameUpdater shared] startFrameLoop];
} else {
isLoopFrame = NO;
[[FrameUpdater shared] stopFrameLoop];
rfbShutdownServer(screen, true);
}
isVNCRunning = shouldStart;
}
static void loadPrefs(void)
{
@autoreleasepool {
NSDictionary* defaults = nil;
CFStringRef appID = CFSTR("com.torrekie.screendump");
CFArrayRef keyList = CFPreferencesCopyKeyList(appID, CFSTR("mobile"), kCFPreferencesAnyHost);
if(keyList) {
defaults = (NSDictionary *)CFPreferencesCopyMultiple(keyList, appID, CFSTR("mobile"), kCFPreferencesAnyHost)?:@{};
CFRelease(keyList);
}
prefferH = [[defaults objectForKey:@"height"]?:@(0) intValue];
prefferW = [[defaults objectForKey:@"width"]?:@(0) intValue];
BOOL isEnabled = [[defaults objectForKey:@"CCSisEnabled"]?:@NO boolValue];
NSString *password = [defaults objectForKey:@"CCSPassword"];
VNCSettings(isEnabled, password);
}
}
static uint32_t oldSeed;
@implementation FrameUpdater
{
NSOperationQueue *q;
}
@synthesize myTimer;
+ (id)shared
{
static __strong FrameUpdater* initFrame;
if(!initFrame) {
initFrame = [[[self class] alloc] init];
}
return initFrame;
}
- (id)init
{
self = [super init];
q = [[NSOperationQueue alloc] init];
return self;
}
- (void)_upFrameLoop
{
if(isLoopFrame && CCSisEnabled) {
//check if screen changed
uint32_t newSeed = IOSurfaceGetSeed(screenSurface);
if(oldSeed != newSeed && rfbIsActive(screen)) {
oldSeed = newSeed;
[q addOperationWithBlock: ^{
IOSurfaceAcceleratorTransferSurface(accelerator, screenSurface, static_buffer, NULL, NULL, NULL, NULL);
rfbMarkRectAsModified(screen, 0, 0, width, height);
}];
}
} else {
[self stopFrameLoop];
}
}
- (void)stopFrameLoop
{
if(myTimer && [myTimer isValid]) {
dispatch_async(dispatch_get_main_queue(), ^(void){
[myTimer invalidate];
});
}
}
- (void)startFrameLoop
{
if(size_image == 0) {
VNCSetup();
}
[self stopFrameLoop];
dispatch_async(dispatch_get_main_queue(), ^(void){
myTimer = [NSTimer scheduledTimerWithTimeInterval:1/400 target:self selector:@selector(_upFrameLoop) userInfo:nil repeats:YES];
});
}
-(void)dealloc
{
[self stopFrameLoop];
}
@end
static void restartServer()
{
exit(0);
}
int main(int argc, const char *argv[])
{
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)loadPrefs, CFSTR("com.torrekie.screendump/preferences.changed"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), NULL, (CFNotificationCallback)restartServer, CFSTR("com.torrekie.screendump/restart"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
loadPrefs();
VNCSetup();
//VNCBlack();
[[NSRunLoop currentRunLoop] run];
return EXIT_SUCCESS;
}
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <rfb/rfb.h>
#include <rfb/keysym.h>
#include "./include/IOKit/hid/IOHIDEventTypes.h"
#include "./include/IOKit/hidsystem/IOHIDUsageTables.h"
typedef uint32_t IOHIDDigitizerTransducerType;
#ifdef __LP64__
typedef double IOHIDFloat;
#else
typedef float IOHIDFloat;
#endif
typedef UInt32 IOOptionBits;
typedef uint32_t IOHIDEventField;
extern "C" {
typedef uint32_t IOHIDEventOptionBits;
typedef struct __IOHIDEvent *IOHIDEventRef;
typedef struct CF_BRIDGED_TYPE(id) __IOHIDEventSystemClient * IOHIDEventSystemClientRef;
IOHIDEventRef IOHIDEventCreateKeyboardEvent(
CFAllocatorRef allocator,
uint64_t time, uint16_t page, uint16_t usage,
Boolean down, IOHIDEventOptionBits flags
);
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);
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);
IOHIDEventSystemClientRef IOHIDEventSystemClientCreate(CFAllocatorRef allocator);
void IOHIDEventAppendEvent(IOHIDEventRef parent, IOHIDEventRef child);
void IOHIDEventSetIntegerValue(IOHIDEventRef event, IOHIDEventField field, int value);
void IOHIDEventSetSenderID(IOHIDEventRef event, uint64_t sender);
void IOHIDEventSystemClientDispatchEvent(IOHIDEventSystemClientRef client, IOHIDEventRef event);
// void IOHIDEventSystemConnectionDispatchEvent(IOHIDEventSystemConnectionRef connection, IOHIDEventRef event);
}
static void VNCPointerNew(int buttons, int x, int y, CGPoint location, int diff, bool twas, bool tis);
static void SendHIDEvent(IOHIDEventRef event)
{
static IOHIDEventSystemClientRef client_(NULL);
if (client_ == NULL) {
client_ = IOHIDEventSystemClientCreate(kCFAllocatorDefault);
}
IOHIDEventSetSenderID(event, 0x8000000817319372);
IOHIDEventSystemClientDispatchEvent(client_, event);
CFRelease(event);
}
static void VNCKeyboard(rfbBool down, rfbKeySym key, rfbClientPtr client)
{
uint16_t usage;
switch (key) {
/* TTY Functions */
case XK_BackSpace:
case XK_Delete: usage = kHIDUsage_KeyboardDeleteOrBackspace; break;
case XK_Tab: usage = kHIDUsage_KeyboardTab; break;
case XK_Linefeed: usage = kHIDUsage_KeyboardReturnOrEnter; break;
case XK_Clear: usage = kHIDUsage_KeyboardClear; break;
case XK_Return: usage = kHIDUsage_KeyboardReturnOrEnter; break;
case XK_Pause: usage = kHIDUsage_KeyboardPause; break;
case XK_Scroll_Lock: usage = kHIDUsage_KeyboardScrollLock; break;
case XK_Sys_Req: usage = kHIDUsage_KeyboardSysReqOrAttention; break;
case XK_Escape: usage = kHIDUsage_KeyboardEscape; break;
/* Cursor control & motion */
case XK_Home: case XK_Begin: usage = kHIDUsage_KeyboardHome; break;
case XK_Left: usage = kHIDUsage_KeyboardLeftArrow; break;
case XK_Up: usage = kHIDUsage_KeyboardUpArrow; break;
case XK_Right: usage = kHIDUsage_KeyboardRightArrow; break;
case XK_Down: usage = kHIDUsage_KeyboardDownArrow; break;
// case XK_Prior: usage = kHIDUsage_KeyboardPrior; break;
case XK_Page_Up: usage = kHIDUsage_KeyboardPageUp; break;
// case XK_Next:
case XK_Page_Down: usage = kHIDUsage_KeyboardPageDown; break;
case XK_End: usage = kHIDUsage_KeyboardEnd; break;
/* Misc Functions */
case XK_Select: usage = kHIDUsage_KeyboardSelect; break;
case XK_Print: usage = kHIDUsage_KeyboardPrintScreen; break;
case XK_Execute: usage = kHIDUsage_KeyboardExecute; break;
case XK_Insert: usage = kHIDUsage_KeyboardInsert; break;
case XK_Undo: usage = kHIDUsage_KeyboardUndo; break;
case XK_Redo: usage = kHIDUsage_KeyboardAgain; break;
case XK_Menu: usage = kHIDUsage_KeyboardMenu; break;
case XK_Find: usage = kHIDUsage_KeyboardFind; break;
case XK_Cancel: usage = kHIDUsage_KeyboardCancel; break;
case XK_Help: usage = kHIDUsage_KeyboardHelp; break;
case XK_Break: usage = kHIDUsage_KeyboardF15; break;
// case XK_Mode_switch:
case XK_script_switch: usage = kHIDUsage_KeyboardF19; break;
case XK_Num_Lock: usage = kHIDUsage_KeyboardLockingNumLock; break;
/* Keypad Functions, keypad numbers cleverly chosen to map to ascii */
case XK_KP_Enter: usage = kHIDUsage_KeypadEnter; break;
case XK_KP_Equal: usage = kHIDUsage_KeypadEqualSign; break;
case XK_KP_Multiply: usage = kHIDUsage_KeypadAsterisk; break;
case XK_KP_Add: usage = kHIDUsage_KeypadPlus; break;
case XK_KP_Separator: usage = kHIDUsage_KeypadComma; break;
case XK_KP_Subtract: usage = kHIDUsage_KeypadHyphen; break;
case XK_KP_Decimal: usage = kHIDUsage_KeypadPeriod; break;
case XK_KP_Divide: usage = kHIDUsage_KeypadSlash; break;
case XK_KP_0: usage = kHIDUsage_Keypad0;
case XK_KP_1: usage = kHIDUsage_Keypad1;
case XK_KP_2: usage = kHIDUsage_Keypad2;
case XK_KP_3: usage = kHIDUsage_Keypad3;
case XK_KP_4: usage = kHIDUsage_Keypad4;
case XK_KP_5: usage = kHIDUsage_Keypad5;
case XK_KP_6: usage = kHIDUsage_Keypad6;
case XK_KP_7: usage = kHIDUsage_Keypad7;
case XK_KP_8: usage = kHIDUsage_Keypad8;
case XK_KP_9: usage = kHIDUsage_Keypad9;
/* Auxiliary Functions */
case XK_F1: usage = kHIDUsage_KeyboardF1; break;
case XK_F2: usage = kHIDUsage_KeyboardF2; break;
case XK_F3: usage = kHIDUsage_KeyboardF3; break;
case XK_F4: usage = kHIDUsage_KeyboardF4; break;
case XK_F5: usage = kHIDUsage_KeyboardF5; break;
case XK_F6: usage = kHIDUsage_KeyboardF6; break;
case XK_F7: usage = kHIDUsage_KeyboardF7; break;
case XK_F8: usage = kHIDUsage_KeyboardF8; break;
case XK_F9: usage = kHIDUsage_KeyboardF9; break;
case XK_F10: usage = kHIDUsage_KeyboardF10; break;
case XK_F11: usage = kHIDUsage_KeyboardF11; break;
case XK_F12: usage = kHIDUsage_KeyboardF12; break;
case XK_F13: usage = kHIDUsage_KeyboardF13; break;
case XK_F14: usage = kHIDUsage_KeyboardF14; break;
case XK_F15: usage = kHIDUsage_KeyboardF15; break;
case XK_F16: usage = kHIDUsage_KeyboardF16; break;
case XK_F17: usage = kHIDUsage_KeyboardF17; break;
case XK_F18: usage = kHIDUsage_KeyboardF18; break;
case XK_F19: usage = kHIDUsage_KeyboardF19; break;
case XK_F20: usage = kHIDUsage_KeyboardF20; break;
case XK_F21: usage = kHIDUsage_KeyboardF21; break;
case XK_F22: usage = kHIDUsage_KeyboardF22; break;
case XK_F23: usage = kHIDUsage_KeyboardF23; break;
case XK_F24: usage = kHIDUsage_KeyboardF24; break;
/* Modifiers */
case XK_Shift_L: usage = kHIDUsage_KeyboardLeftShift; break;
case XK_Shift_R: usage = kHIDUsage_KeyboardRightShift; break;
case XK_Control_L: usage = kHIDUsage_KeyboardLeftControl; break;
case XK_Control_R: usage = kHIDUsage_KeyboardRightControl; break;
case XK_Caps_Lock:
case XK_Shift_Lock: usage = kHIDUsage_KeyboardLockingCapsLock; break;
case XK_Meta_L: usage = kHIDUsage_KeyboardLeftAlt; break;
case XK_Meta_R: usage = kHIDUsage_KeyboardRightAlt; break;
case XK_Alt_L: usage = kHIDUsage_KeyboardLeftGUI; break;
case XK_Alt_R: usage = kHIDUsage_KeyboardRightGUI; break;
/* Latin */
case XK_space: usage = kHIDUsage_KeyboardSpacebar; break;
case XK_exclam: case XK_1: usage = kHIDUsage_Keyboard1; break;
case XK_at: case XK_2: usage = kHIDUsage_Keyboard2; break;
case XK_numbersign: case XK_3: usage = kHIDUsage_Keyboard3; break;
case XK_dollar: case XK_4: usage = kHIDUsage_Keyboard4; break;
case XK_percent: case XK_5: usage = kHIDUsage_Keyboard5; break;
case XK_asciicircum: case XK_6: usage = kHIDUsage_Keyboard6; break;
case XK_ampersand: case XK_7: usage = kHIDUsage_Keyboard7; break;
case XK_asterisk: case XK_8: usage = kHIDUsage_Keyboard8; break;
case XK_parenleft: case XK_9: usage = kHIDUsage_Keyboard9; break;
case XK_parenright: case XK_0: usage = kHIDUsage_Keyboard0; break;
case XK_underscore: case XK_minus: usage = kHIDUsage_KeyboardHyphen; break;
case XK_plus: case XK_equal: usage = kHIDUsage_KeyboardEqualSign; break;
case XK_greater: case XK_period: usage = kHIDUsage_KeyboardPeriod; break;
case XK_question: case XK_slash: usage = kHIDUsage_KeyboardSlash; break;
case XK_colon: case XK_semicolon: usage = kHIDUsage_KeyboardSemicolon; break;
case XK_less: case XK_comma: usage = kHIDUsage_KeyboardComma; break;
case XK_A: case XK_a: usage = kHIDUsage_KeyboardA; break;
case XK_B: case XK_b: usage = kHIDUsage_KeyboardB; break;
case XK_C: case XK_c: usage = kHIDUsage_KeyboardC; break;
case XK_D: case XK_d: usage = kHIDUsage_KeyboardD; break;
case XK_E: case XK_e: usage = kHIDUsage_KeyboardE; break;
case XK_F: case XK_f: usage = kHIDUsage_KeyboardF; break;
case XK_G: case XK_g: usage = kHIDUsage_KeyboardG; break;
case XK_H: case XK_h: usage = kHIDUsage_KeyboardH; break;
case XK_I: case XK_i: usage = kHIDUsage_KeyboardI; break;
case XK_J: case XK_j: usage = kHIDUsage_KeyboardJ; break;
case XK_K: case XK_k: usage = kHIDUsage_KeyboardK; break;
case XK_L: case XK_l: usage = kHIDUsage_KeyboardL; break;
case XK_M: case XK_m: usage = kHIDUsage_KeyboardM; break;
case XK_N: case XK_n: usage = kHIDUsage_KeyboardN; break;
case XK_O: case XK_o: usage = kHIDUsage_KeyboardO; break;
case XK_P: case XK_p: usage = kHIDUsage_KeyboardP; break;
case XK_Q: case XK_q: usage = kHIDUsage_KeyboardQ; break;
case XK_R: case XK_r: usage = kHIDUsage_KeyboardR; break;
case XK_S: case XK_s: usage = kHIDUsage_KeyboardS; break;
case XK_T: case XK_t: usage = kHIDUsage_KeyboardT; break;
case XK_U: case XK_u: usage = kHIDUsage_KeyboardU; break;
case XK_V: case XK_v: usage = kHIDUsage_KeyboardV; break;
case XK_W: case XK_w: usage = kHIDUsage_KeyboardW; break;
case XK_X: case XK_x: usage = kHIDUsage_KeyboardX; break;
case XK_Y: case XK_y: usage = kHIDUsage_KeyboardY; break;
case XK_Z: case XK_z: usage = kHIDUsage_KeyboardZ; break;
case XK_braceleft: case XK_bracketleft: usage = kHIDUsage_KeyboardOpenBracket; break;
case XK_braceright: case XK_bracketright: usage = kHIDUsage_KeyboardCloseBracket; break;
case XK_bar: case XK_backslash: usage = kHIDUsage_KeyboardBackslash; break;
case XK_quotedbl: case XK_apostrophe: usage = kHIDUsage_KeyboardQuote; break;
case XK_asciitilde: case XK_grave: usage = kHIDUsage_KeyboardGraveAccentAndTilde; break;
default: return;
}
SendHIDEvent(IOHIDEventCreateKeyboardEvent(kCFAllocatorDefault, mach_absolute_time(), kHIDPage_KeyboardOrKeypad, usage, down, 0));
}
static int buttons_;
static int x_, y_;
static void VNCPointer(int buttons, int x, int y, rfbClientPtr client) {
// if (ratio_ == 0)
// return;
CGPoint location = {static_cast<CGFloat>(x), static_cast<CGFloat>(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;
// }
return VNCPointerNew(buttons, x, y, location, diff, twas, tis);
}
static void VNCPointerNew(int buttons, int x, int y, CGPoint location, int diff, bool twas, bool tis) {
if ((diff & 0x10) != 0)
SendHIDEvent(IOHIDEventCreateKeyboardEvent(kCFAllocatorDefault, mach_absolute_time(), kHIDPage_Telephony, kHIDUsage_Tfon_Flash, (buttons & 0x10) != 0, 0));
if ((diff & 0x04) != 0)
SendHIDEvent(IOHIDEventCreateKeyboardEvent(kCFAllocatorDefault, mach_absolute_time(), kHIDPage_Consumer, kHIDUsage_Csmr_Menu, (buttons & 0x04) != 0, 0));
if ((diff & 0x02) != 0)
SendHIDEvent(IOHIDEventCreateKeyboardEvent(kCFAllocatorDefault, mach_absolute_time(), kHIDPage_Consumer, kHIDUsage_Csmr_Power, (buttons & 0x02) != 0, 0));
uint32_t handm;
uint32_t fingerm;
if (twas == 0 && tis == 1) {
handm = kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch | kIOHIDDigitizerEventIdentity;
fingerm = kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch;
} else if (twas == 1 && tis == 1) {
handm = kIOHIDDigitizerEventPosition;
fingerm = kIOHIDDigitizerEventPosition;
} else if (twas == 1 && tis == 0) {
handm = kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch | kIOHIDDigitizerEventIdentity | kIOHIDDigitizerEventPosition;
fingerm = kIOHIDDigitizerEventRange | kIOHIDDigitizerEventTouch;
} else return;
// XXX: avoid division in VNCPointer()
// x *= ratio_;
// y *= ratio_;
IOHIDFloat xf(x);
IOHIDFloat yf(y);
xf /= width;
yf /= height;
IOHIDEventRef hand(IOHIDEventCreateDigitizerEvent(kCFAllocatorDefault, mach_absolute_time(), kIOHIDDigitizerTransducerTypeHand, 1<<22, 1, handm, 0, xf, yf, 0, 0, 0, 0, 0, 0));
IOHIDEventSetIntegerValue(hand, kIOHIDEventFieldIsBuiltIn, true);
IOHIDEventSetIntegerValue(hand, kIOHIDEventFieldDigitizerIsDisplayIntegrated, true);
IOHIDEventRef finger(IOHIDEventCreateDigitizerFingerEvent(kCFAllocatorDefault, mach_absolute_time(), 3, 2, fingerm, xf, yf, 0, 0, 0, tis, tis, 0));
IOHIDEventAppendEvent(hand, finger);
CFRelease(finger);
SendHIDEvent(hand);
}
static void handleVNCKeyboard(rfbBool down, rfbKeySym key, rfbClientPtr client) {
VNCKeyboard(down, key, client);
}
static void handleVNCPointer(int buttons, int x, int y, rfbClientPtr client) {
VNCPointer(buttons, x, y, client);
}