|
| 1 | +#import <Foundation/Foundation.h> |
| 2 | +#import <mach/mach_time.h> |
| 3 | + |
| 4 | +// Private simulator HID entry points; the host verifies the resulting hinge independently. |
| 5 | +extern CFDataRef IOCFSerialize(CFTypeRef object, CFOptionFlags options); |
| 6 | +extern CFTypeRef IOHIDEventCreateVendorDefinedEvent(CFAllocatorRef allocator, uint64_t timestamp, |
| 7 | + uint32_t usagePage, uint32_t usage, uint32_t version, const uint8_t *data, |
| 8 | + CFIndex length, uint32_t options); |
| 9 | +extern CFTypeRef IOHIDEventSystemClientCreate(CFAllocatorRef allocator); |
| 10 | +extern void IOHIDEventSystemClientDispatchEvent(CFTypeRef client, CFTypeRef event); |
| 11 | + |
| 12 | +int main(int argc, const char *argv[]) { |
| 13 | + @autoreleasepool { |
| 14 | + if (argc != 2) return 2; |
| 15 | + NSDictionary *angles = @{@"closed": @0, @"half-open": @130, @"open": @180}; |
| 16 | + NSNumber *angle = angles[@(argv[1])]; |
| 17 | + if (!angle) { |
| 18 | + fprintf(stderr, "Expected closed, half-open, or open\n"); |
| 19 | + return 2; |
| 20 | + } |
| 21 | + NSDictionary *payload = @{ |
| 22 | + @"provider": @"com.apple.Virtualization.VirtualMachines", |
| 23 | + @"source": @"hinge-slider-control", |
| 24 | + @"type": @"range", |
| 25 | + @"value": angle, |
| 26 | + }; |
| 27 | + CFDataRef data = IOCFSerialize((__bridge CFTypeRef)payload, 1); |
| 28 | + if (!data) { |
| 29 | + fprintf(stderr, "Unable to serialize simulator hinge payload\n"); |
| 30 | + return 1; |
| 31 | + } |
| 32 | + CFTypeRef client = IOHIDEventSystemClientCreate(kCFAllocatorDefault); |
| 33 | + CFTypeRef event = IOHIDEventCreateVendorDefinedEvent(kCFAllocatorDefault, |
| 34 | + mach_absolute_time(), 0xff61, 0x5b, 0, CFDataGetBytePtr(data), CFDataGetLength(data), 0); |
| 35 | + if (client && event) IOHIDEventSystemClientDispatchEvent(client, event); |
| 36 | + BOOL dispatched = client && event; |
| 37 | + if (event) CFRelease(event); |
| 38 | + if (client) CFRelease(client); |
| 39 | + CFRelease(data); |
| 40 | + if (!dispatched) fprintf(stderr, "Unable to create simulator HID client or event\n"); |
| 41 | + return dispatched ? 0 : 1; |
| 42 | + } |
| 43 | +} |
0 commit comments