| 1 | #import "macos-setup-bridge.h" |
| 2 | #import "macos-setup-view.h" |
| 3 | |
| 4 | |
| 5 | |
| 6 | void CageRunSetupBridge(void *machine, void *queue, double width, double height, |
| 7 | const char *title, bool controller) { |
| 8 | (void)width; (void)height; (void)title; (void)controller; |
| 9 | @autoreleasepool { |
| 10 | if (!NSThread.isMainThread) { |
| 11 | fprintf(stderr, "Cage setup probe requires the AppKit main thread\n"); |
| 12 | return; |
| 13 | } |
| 14 | NSApplication *app = [NSApplication sharedApplication]; |
| 15 | [app setActivationPolicy:NSApplicationActivationPolicyProhibited]; |
| 16 | VZVirtualMachine *vm = (__bridge VZVirtualMachine *)machine; |
| 17 | __block BOOL allowed = YES; |
| 18 | CageGuestSetupView *setup = [[CageGuestSetupView alloc] initWithMachine:vm |
| 19 | size:NSMakeSize(1024, 640) setupAuthorized:^BOOL { return allowed; }]; |
| 20 | allowed = NO; |
| 21 | BOOL revoked = setup && ![setup snapshotPNG] && ![setup clickAtX:1 y:1] && ![setup scrollAtX:1 y:1 deltaX:0 deltaY:10]; |
| 22 | fprintf(stderr, "Cage setup probe: main-thread binding and revocation %s\n", revoked ? "passed" : "failed"); |
| 23 | [setup close]; |
| 24 | |
| 25 | |
| 26 | dispatch_queue_t vmQueue = (__bridge dispatch_queue_t)queue; |
| 27 | __block BOOL seenRunning = NO; |
| 28 | NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.25 repeats:YES block:^(NSTimer *tick) { |
| 29 | (void)tick; |
| 30 | dispatch_async(vmQueue, ^{ |
| 31 | VZVirtualMachineState state = vm.state; |
| 32 | if (state == VZVirtualMachineStateRunning) seenRunning = YES; |
| 33 | if (seenRunning && (state == VZVirtualMachineStateStopped || state == VZVirtualMachineStateError)) { |
| 34 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 35 | [app stop:nil]; |
| 36 | NSEvent *wake = [NSEvent otherEventWithType:NSEventTypeApplicationDefined |
| 37 | location:NSZeroPoint modifierFlags:0 timestamp:0 windowNumber:0 |
| 38 | context:nil subtype:0 data1:0 data2:0]; |
| 39 | [app postEvent:wake atStart:YES]; |
| 40 | }); |
| 41 | } |
| 42 | }); |
| 43 | }]; |
| 44 | [app run]; |
| 45 | [timer invalidate]; |
| 46 | } |
| 47 | } |