| 1 | |
| 2 | #import <Cocoa/Cocoa.h> |
| 3 | #import <IOKit/hidsystem/IOLLEvent.h> |
| 4 | #import "bridge.h" |
| 5 | #import "../../scripts/guest-os/macos-setup-view.m" |
| 6 | |
| 7 | static VZVirtualMachine *machine; |
| 8 | static CageGuestSetupView *loginView; |
| 9 | |
| 10 | |
| 11 | static const CGFloat scanWidth = 1920, scanHeight = 1200; |
| 12 | |
| 13 | void cageLoginLoop(void *ref) { |
| 14 | @autoreleasepool { |
| 15 | machine = (__bridge VZVirtualMachine *)ref; |
| 16 | NSApplication *app = [NSApplication sharedApplication]; |
| 17 | [app setActivationPolicy:NSApplicationActivationPolicyProhibited]; |
| 18 | [app run]; |
| 19 | [loginView close]; loginView = nil; machine = nil; |
| 20 | } |
| 21 | } |
| 22 | void cageLoginStop(void) { |
| 23 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 24 | [NSApp stop:nil]; |
| 25 | [NSApp postEvent:[NSEvent otherEventWithType:NSEventTypeApplicationDefined location:NSZeroPoint |
| 26 | modifierFlags:0 timestamp:0 windowNumber:0 context:nil subtype:0 data1:0 data2:0] atStart:YES]; |
| 27 | }); |
| 28 | } |
| 29 | void cageLoginEnable(bool enabled) { |
| 30 | dispatch_sync(dispatch_get_main_queue(), ^{ |
| 31 | [loginView close]; loginView = nil; |
| 32 | if (enabled && machine) loginView = [[CageGuestSetupView alloc] initWithMachine:machine size:NSMakeSize(scanWidth,scanHeight) |
| 33 | setupAuthorized:^BOOL { return machine != nil; }]; |
| 34 | }); |
| 35 | } |
| 36 | void *cageLoginImage(int *size) { |
| 37 | __block void *copy = NULL; |
| 38 | dispatch_sync(dispatch_get_main_queue(), ^{ |
| 39 | @autoreleasepool { |
| 40 | NSData *image = [loginView snapshotJPEG]; |
| 41 | if (image.length && image.length <= (4<<20)) { |
| 42 | copy = malloc(image.length); |
| 43 | if (copy) { memcpy(copy, image.bytes, image.length); *size = (int)image.length; } |
| 44 | } |
| 45 | } |
| 46 | }); |
| 47 | return copy; |
| 48 | } |
| 49 | void *cageLoginImageRegion(int x, int y, int width, int height, int *size) { |
| 50 | if (x < 0 || y < 0 || width <= 0 || height <= 0 || x + width > 1024 || y + height > 640) return NULL; |
| 51 | __block void *copy = NULL; |
| 52 | dispatch_sync(dispatch_get_main_queue(), ^{ |
| 53 | @autoreleasepool { |
| 54 | NSData *frame = [loginView snapshotJPEG]; |
| 55 | if (!frame.length) return; |
| 56 | NSBitmapImageRep *source = [NSBitmapImageRep imageRepWithData:frame]; |
| 57 | if (!source.CGImage) return; |
| 58 | CGRect region = CGRectMake(x * source.pixelsWide / 1024.0, y * source.pixelsHigh / 640.0, |
| 59 | width * source.pixelsWide / 1024.0, height * source.pixelsHigh / 640.0); |
| 60 | CGImageRef cropped = CGImageCreateWithImageInRect(source.CGImage, region); |
| 61 | if (!cropped) return; |
| 62 | NSBitmapImageRep *output = [[NSBitmapImageRep alloc] initWithCGImage:cropped]; |
| 63 | CGImageRelease(cropped); |
| 64 | NSData *image = [output representationUsingType:NSBitmapImageFileTypeJPEG properties:@{NSImageCompressionFactor:@0.92}]; |
| 65 | if (image.length && image.length <= (4<<20)) { |
| 66 | copy = malloc(image.length); |
| 67 | if (copy) { memcpy(copy, image.bytes, image.length); *size = (int)image.length; } |
| 68 | } |
| 69 | } |
| 70 | }); |
| 71 | return copy; |
| 72 | } |
| 73 | bool cageLoginClick(int x, int y) { |
| 74 | __block BOOL ok = NO; |
| 75 | dispatch_sync(dispatch_get_main_queue(), ^{ ok = [loginView clickAtX:x * scanWidth / 1024.0 y:y * scanHeight / 640.0]; }); |
| 76 | return ok; |
| 77 | } |
| 78 | bool cageLoginKey(int code, bool shift) { |
| 79 | __block BOOL ok = NO; |
| 80 | dispatch_sync(dispatch_get_main_queue(), ^{ |
| 81 | if (!loginView) return; |
| 82 | |
| 83 | |
| 84 | NSEventModifierFlags flags = shift ? NSEventModifierFlagShift | NX_DEVICELSHIFTKEYMASK : 0; |
| 85 | NSEvent *(^event)(NSEventType, int, NSEventModifierFlags) = ^NSEvent *(NSEventType kind, int key, NSEventModifierFlags mods) { |
| 86 | return [NSEvent keyEventWithType:kind location:NSZeroPoint modifierFlags:mods timestamp:NSProcessInfo.processInfo.systemUptime |
| 87 | windowNumber:0 context:nil characters:@"" charactersIgnoringModifiers:@"" isARepeat:NO keyCode:(unsigned short)key]; |
| 88 | }; |
| 89 | if (shift) { |
| 90 | [loginView sendKey:event(NSEventTypeFlagsChanged,56,flags)]; |
| 91 | [loginView sendKey:event(NSEventTypeKeyDown,56,flags)]; |
| 92 | } |
| 93 | ok = [loginView sendKey:event(NSEventTypeKeyDown,code,flags)] && [loginView sendKey:event(NSEventTypeKeyUp,code,flags)]; |
| 94 | if (shift) { |
| 95 | [loginView sendKey:event(NSEventTypeKeyUp,56,0)]; |
| 96 | [loginView sendKey:event(NSEventTypeFlagsChanged,56,0)]; |
| 97 | } |
| 98 | }); |
| 99 | return ok; |
| 100 | } |
| 101 | bool cageLoginSelectAll(void) { |
| 102 | __block BOOL ok = NO; |
| 103 | dispatch_sync(dispatch_get_main_queue(), ^{ |
| 104 | if (!loginView) return; |
| 105 | NSEventModifierFlags command = NSEventModifierFlagCommand | NX_DEVICELCMDKEYMASK; |
| 106 | NSEvent *(^event)(NSEventType, int, NSEventModifierFlags) = ^NSEvent *(NSEventType kind, int key, NSEventModifierFlags flags) { |
| 107 | return [NSEvent keyEventWithType:kind location:NSZeroPoint modifierFlags:flags timestamp:NSProcessInfo.processInfo.systemUptime |
| 108 | windowNumber:0 context:nil characters:@"" charactersIgnoringModifiers:@"" isARepeat:NO keyCode:(unsigned short)key]; |
| 109 | }; |
| 110 | [loginView sendKey:event(NSEventTypeFlagsChanged,55,command)]; |
| 111 | [loginView sendKey:event(NSEventTypeKeyDown,55,command)]; |
| 112 | ok = [loginView sendKey:event(NSEventTypeKeyDown,0,command)] && [loginView sendKey:event(NSEventTypeKeyUp,0,command)]; |
| 113 | [loginView sendKey:event(NSEventTypeKeyUp,55,0)]; |
| 114 | [loginView sendKey:event(NSEventTypeFlagsChanged,55,0)]; |
| 115 | }); |
| 116 | return ok; |
| 117 | } |
| 118 | bool cageLoginScroll(int x, int y, int deltaX, int deltaY) { |
| 119 | __block BOOL ok = NO; |
| 120 | dispatch_sync(dispatch_get_main_queue(), ^{ |
| 121 | ok = [loginView scrollAtX:x * scanWidth / 1024.0 y:y * scanHeight / 640.0 |
| 122 | deltaX:deltaX * scanWidth / 1024.0 deltaY:deltaY * scanHeight / 640.0]; |
| 123 | }); |
| 124 | return ok; |
| 125 | } |