17 #if defined(__APPLE__) && !defined(CPPPARSER)
24 IOKitInputDeviceManager::
25 IOKitInputDeviceManager() {
26 _hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
29 <<
"Failed to create an IOHIDManager.\n";
34 int page = kHIDPage_GenericDesktop;
35 int usages[] = {kHIDUsage_GD_GamePad,
36 kHIDUsage_GD_Joystick,
38 kHIDUsage_GD_Keyboard,
39 kHIDUsage_GD_MultiAxisController, 0};
44 CFMutableArrayRef match = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
47 CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
48 CFNumberRef page_ref = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
49 CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsagePageKey), page_ref);
51 CFNumberRef usage_ref = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, usage);
52 CFDictionarySetValue(dict, CFSTR(kIOHIDDeviceUsageKey), usage_ref);
54 CFArrayAppendValue(match, dict);
58 IOHIDManagerSetDeviceMatchingMultiple(_hid_manager, match);
61 IOHIDManagerRegisterDeviceMatchingCallback(_hid_manager, on_match_device,
this);
62 IOHIDManagerRegisterDeviceRemovalCallback(_hid_manager, on_remove_device,
this);
63 IOHIDManagerScheduleWithRunLoop(_hid_manager, CFRunLoopGetMain(), kCFRunLoopCommonModes);
64 IOHIDManagerOpen(_hid_manager, kIOHIDOptionsTypeNone);
70 IOKitInputDeviceManager::
71 ~IOKitInputDeviceManager() {
72 IOHIDManagerUnscheduleFromRunLoop(_hid_manager, CFRunLoopGetMain(), kCFRunLoopCommonModes);
73 IOHIDManagerClose(_hid_manager, kIOHIDOptionsTypeNone);
74 CFRelease(_hid_manager);
80 void IOKitInputDeviceManager::
81 on_match_device(
void *ctx, IOReturn result,
void *sender, IOHIDDeviceRef device) {
82 IOKitInputDeviceManager *mgr = (IOKitInputDeviceManager *)ctx;
83 nassertv(mgr !=
nullptr);
86 IOKitInputDevice *input_device =
new IOKitInputDevice(device);
87 if (device_cat.is_debug()) {
89 <<
"Discovered input device " << *input_device <<
"\n";
91 mgr->add_device(input_device);
92 mgr->_devices_by_hidref[device] = input_device;
98 void IOKitInputDeviceManager::
99 on_remove_device(
void *ctx, IOReturn result,
void *sender, IOHIDDeviceRef device) {
100 IOKitInputDeviceManager *mgr = (IOKitInputDeviceManager *)ctx;
101 nassertv(mgr !=
nullptr);
104 auto it = mgr->_devices_by_hidref.find(device);
105 nassertv(it != mgr->_devices_by_hidref.end());
106 IOKitInputDevice *input_device = it->second;
108 input_device->set_connected(
false);
110 mgr->_devices_by_hidref.erase(device);
112 IOHIDDeviceClose(device, kIOHIDOptionsTypeNone);
114 if (device_cat.is_debug()) {
116 <<
"Removed input device " << *input_device <<
"\n";
119 mgr->remove_device(input_device);