14 #include <IOKit/hid/IOHIDLib.h>
26 "VuoList_VuoHidDevice"
36 typedef struct _VuoHid_internal
42 IOHIDManagerRef manager;
53 static void __attribute__((constructor)) VuoHidIo_init()
63 if (result != kIOReturnSuccess)
72 IOHIDElementRef element = IOHIDValueGetElement(value);
73 IOHIDDeviceRef device = IOHIDElementGetDevice(element);
85 control.
value = IOHIDValueGetIntegerValue(value);
87 si->triggerSet.fire(control);
104 si->device = realizedDevice;
107 si->manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
110 VUserLog(
"Error: Couldn't initialize IOHIDManager.");
115 CFMutableDictionaryRef matchesCF = CFDictionaryCreateMutable(NULL, 4, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
123 if (realizedDevice.vendorID)
125 CFNumberRef vendorCF = CFNumberCreate(NULL, kCFNumberSInt64Type, &realizedDevice.vendorID);
126 CFDictionarySetValue(matchesCF, CFSTR(kIOHIDVendorIDKey), vendorCF);
130 if (realizedDevice.productID)
132 CFNumberRef productCF = CFNumberCreate(NULL, kCFNumberSInt64Type, &realizedDevice.productID);
133 CFDictionarySetValue(matchesCF, CFSTR(kIOHIDProductIDKey), productCF);
134 CFRelease(productCF);
137 if (realizedDevice.usagePage)
139 CFNumberRef usagePageCF = CFNumberCreate(NULL, kCFNumberSInt64Type, &realizedDevice.usagePage);
140 CFDictionarySetValue(matchesCF, CFSTR(kIOHIDDeviceUsagePageKey), usagePageCF);
141 CFRelease(usagePageCF);
144 if (realizedDevice.usage)
146 CFNumberRef usageCF = CFNumberCreate(NULL, kCFNumberSInt64Type, &realizedDevice.usage);
147 CFDictionarySetValue(matchesCF, CFSTR(kIOHIDDeviceUsageKey), usageCF);
151 IOHIDManagerSetDeviceMatching(si->manager, matchesCF);
152 CFRelease(matchesCF);
155 IOReturn ret = IOHIDManagerOpen(si->manager, si->exclusive ? kIOHIDOptionsTypeSeizeDevice : kIOHIDOptionsTypeNone);
156 if (ret != kIOReturnSuccess)
159 VUserLog(
"Error: Couldn't open IOHIDManager: %s", text);
161 CFRelease(si->manager);
167 CFSetRef deviceCF = IOHIDManagerCopyDevices(si->manager);
170 VUserLog(
"Error: Couldn't copy device list.");
171 CFRelease(si->manager);
175 if (CFSetGetCount(deviceCF) == 0)
178 CFRelease(si->manager);
187 IOHIDManagerScheduleWithRunLoop(si->manager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
200 VuoHid_openDevice(*i);
224 dispatch_sync(dispatch_get_main_queue(), ^{
226 IOHIDManagerRegisterInputValueCallback(si->manager, NULL, NULL);
227 IOHIDManagerClose(si->manager, kIOHIDOptionsTypeNone);
228 CFRelease(si->manager);
250 si->exclusive = exclusive;
278 si->triggerSet.addTrigger(receivedControl);
293 si->triggerSet.removeTrigger(receivedControl);