[Mac] Only add callbacks for recognized USB HID devices

This commit is contained in:
thefiddler 2014-01-05 00:00:49 +01:00
parent c44b477388
commit bf8efea121

View file

@ -127,58 +127,74 @@ namespace OpenTK.Platform.MacOS
void DeviceAdded(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) void DeviceAdded(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device)
{ {
bool recognized = false;
if (NativeMethods.IOHIDDeviceOpen(device, IOOptionBits.Zero) == IOReturn.Zero) if (NativeMethods.IOHIDDeviceOpen(device, IOOptionBits.Zero) == IOReturn.Zero)
{ {
if (NativeMethods.IOHIDDeviceConformsTo(device, if (NativeMethods.IOHIDDeviceConformsTo(device,
HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse)) HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse))
{ {
AddMouse(sender, device); AddMouse(sender, device);
recognized = true;
} }
if (NativeMethods.IOHIDDeviceConformsTo(device, if (NativeMethods.IOHIDDeviceConformsTo(device,
HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard)) HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard))
{ {
AddKeyboard(sender, device); AddKeyboard(sender, device);
recognized = true;
} }
if (NativeMethods.IOHIDDeviceConformsTo(device, if (NativeMethods.IOHIDDeviceConformsTo(device,
HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick)) HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick))
{ {
AddJoystick(sender, device); AddJoystick(sender, device);
recognized = true;
} }
// The device is not normally available in the InputValueCallback (HandleDeviceValueReceived), so we include if (recognized)
// the device identifier as the context variable, so we can identify it and figure out the device later. {
// Thanks to Jase: http://www.opentk.com/node/2800 // The device is not normally available in the InputValueCallback (HandleDeviceValueReceived), so we include
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, // the device identifier as the context variable, so we can identify it and figure out the device later.
HandleDeviceValueReceived, device); // Thanks to Jase: http://www.opentk.com/node/2800
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device,
HandleDeviceValueReceived, device);
NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, RunLoop, InputLoopMode); NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, RunLoop, InputLoopMode);
}
} }
} }
void DeviceRemoved(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) void DeviceRemoved(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device)
{ {
bool recognized = false;
if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse) && if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse) &&
MouseDevices.ContainsKey(device)) MouseDevices.ContainsKey(device))
{ {
RemoveMouse(sender, device); RemoveMouse(sender, device);
recognized = true;
} }
if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard) && if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard) &&
KeyboardDevices.ContainsKey(device)) KeyboardDevices.ContainsKey(device))
{ {
RemoveKeyboard(sender, device); RemoveKeyboard(sender, device);
recognized = true;
} }
if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick) && if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick) &&
JoystickDevices.ContainsKey(device)) JoystickDevices.ContainsKey(device))
{ {
RemoveJoystick(sender, device); RemoveJoystick(sender, device);
recognized = true;
} }
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero); if (recognized)
NativeMethods.IOHIDDeviceUnscheduleWithRunLoop(device, RunLoop, InputLoopMode); {
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero);
NativeMethods.IOHIDDeviceUnscheduleWithRunLoop(device, RunLoop, InputLoopMode);
}
} }
void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val) void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val)