More narrow device matching to avoid monitoring prompt

This commit is contained in:
samhenrigold
2025-09-06 18:08:35 -04:00
parent 1ee1d846db
commit 82189fa0b3

View File

@@ -48,11 +48,13 @@
return NULL; return NULL;
} }
// Use Generic Desktop + Mouse criteria to find candidate devices // Match specifically for the lid angle sensor to avoid permission prompts
// This broader search allows us to find the lid sensor among other HID devices // Target: Sensor page (0x0020), Orientation usage (0x008A)
NSDictionary *matchingDict = @{ NSDictionary *matchingDict = @{
@"UsagePage": @(0x0001), // Generic Desktop @"VendorID": @(0x05AC), // Apple
@"Usage": @(0x0003), // Mouse @"ProductID": @(0x8104), // Specific product
@"UsagePage": @(0x0020), // Sensor page
@"Usage": @(0x008A), // Orientation usage
}; };
IOHIDManagerSetDeviceMatching(manager, (__bridge CFDictionaryRef)matchingDict); IOHIDManagerSetDeviceMatching(manager, (__bridge CFDictionaryRef)matchingDict);
@@ -60,33 +62,38 @@
IOHIDDeviceRef device = NULL; IOHIDDeviceRef device = NULL;
if (devices && CFSetGetCount(devices) > 0) { if (devices && CFSetGetCount(devices) > 0) {
NSLog(@"[LidAngleSensor] Found %ld devices, looking for sensor...", CFSetGetCount(devices)); NSLog(@"[LidAngleSensor] Found %ld matching lid angle sensor device(s)", CFSetGetCount(devices));
const void **deviceArray = malloc(sizeof(void*) * CFSetGetCount(devices)); const void **deviceArray = malloc(sizeof(void*) * CFSetGetCount(devices));
CFSetGetValues(devices, deviceArray); CFSetGetValues(devices, deviceArray);
// Search for the specific lid angle sensor device // Test each matching device to find the one that actually works
// Discovered through reverse engineering: Apple device with Sensor page
for (CFIndex i = 0; i < CFSetGetCount(devices); i++) { for (CFIndex i = 0; i < CFSetGetCount(devices); i++) {
IOHIDDeviceRef currentDevice = (IOHIDDeviceRef)deviceArray[i]; IOHIDDeviceRef testDevice = (IOHIDDeviceRef)deviceArray[i];
CFNumberRef vendorID = IOHIDDeviceGetProperty(currentDevice, CFSTR("VendorID")); // Try to open and read from this device
CFNumberRef productID = IOHIDDeviceGetProperty(currentDevice, CFSTR("ProductID")); if (IOHIDDeviceOpen(testDevice, kIOHIDOptionsTypeNone) == kIOReturnSuccess) {
CFNumberRef usagePage = IOHIDDeviceGetProperty(currentDevice, CFSTR("PrimaryUsagePage")); uint8_t testReport[8] = {0};
CFNumberRef usage = IOHIDDeviceGetProperty(currentDevice, CFSTR("PrimaryUsage")); CFIndex reportLength = sizeof(testReport);
int vid = 0, pid = 0, up = 0, u = 0; IOReturn result = IOHIDDeviceGetReport(testDevice,
if (vendorID) CFNumberGetValue(vendorID, kCFNumberIntType, &vid); kIOHIDReportTypeFeature,
if (productID) CFNumberGetValue(productID, kCFNumberIntType, &pid); 1,
if (usagePage) CFNumberGetValue(usagePage, kCFNumberIntType, &up); testReport,
if (usage) CFNumberGetValue(usage, kCFNumberIntType, &u); &reportLength);
// Target the specific lid angle sensor device if (result == kIOReturnSuccess && reportLength >= 3) {
// VID=0x05AC (Apple), PID=0x8104, UsagePage=0x0020 (Sensor), Usage=0x008A (Orientation) // This device works! Use it.
if (vid == 0x05AC && pid == 0x8104 && up == 0x0020 && u == 0x008A) { device = (IOHIDDeviceRef)CFRetain(testDevice);
device = (IOHIDDeviceRef)CFRetain(currentDevice); NSLog(@"[LidAngleSensor] Successfully found working lid angle sensor device (index %ld)", i);
NSLog(@"[LidAngleSensor] Found lid angle sensor device: VID=0x%04X, PID=0x%04X", vid, pid); IOHIDDeviceClose(testDevice, kIOHIDOptionsTypeNone); // Close for now, will reopen in init
break; break;
} else {
NSLog(@"[LidAngleSensor] Device %ld failed to read (result: %d, length: %ld)", i, result, reportLength);
IOHIDDeviceClose(testDevice, kIOHIDOptionsTypeNone);
}
} else {
NSLog(@"[LidAngleSensor] Failed to open device %ld", i);
} }
} }