[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,26 +127,33 @@ 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;
} }
if (recognized)
{
// The device is not normally available in the InputValueCallback (HandleDeviceValueReceived), so we include // The device is not normally available in the InputValueCallback (HandleDeviceValueReceived), so we include
// the device identifier as the context variable, so we can identify it and figure out the device later. // 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 // Thanks to Jase: http://www.opentk.com/node/2800
@ -156,30 +163,39 @@ namespace OpenTK.Platform.MacOS
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;
} }
if (recognized)
{
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero); NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero);
NativeMethods.IOHIDDeviceUnscheduleWithRunLoop(device, RunLoop, InputLoopMode); 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)
{ {