[Mac] Don't let exceptions escape to unmanaged code

This commit is contained in:
thefiddler 2014-01-05 04:20:51 +01:00
parent 892d129e54
commit 4c7f6a92a7

View file

@ -144,103 +144,124 @@ 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; try
if (NativeMethods.IOHIDDeviceOpen(device, IOOptionBits.Zero) == IOReturn.Zero)
{ {
if (NativeMethods.IOHIDDeviceConformsTo(device, bool recognized = false;
HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse))
{
AddMouse(sender, device);
recognized = true;
}
if (NativeMethods.IOHIDDeviceConformsTo(device, if (NativeMethods.IOHIDDeviceOpen(device, IOOptionBits.Zero) == IOReturn.Zero)
HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard))
{ {
AddKeyboard(sender, device); if (NativeMethods.IOHIDDeviceConformsTo(device,
recognized = true; HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse))
} {
AddMouse(sender, device);
recognized = true;
}
if (NativeMethods.IOHIDDeviceConformsTo(device, if (NativeMethods.IOHIDDeviceConformsTo(device,
HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick)) HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard))
{ {
AddJoystick(sender, device); AddKeyboard(sender, device);
recognized = true; recognized = true;
} }
if (recognized) if (NativeMethods.IOHIDDeviceConformsTo(device,
{ HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick))
// 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. AddJoystick(sender, device);
// Thanks to Jase: http://www.opentk.com/node/2800 recognized = true;
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, }
HandleDeviceValueReceived, device);
NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, RunLoop, InputLoopMode); if (recognized)
{
// 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.
// Thanks to Jase: http://www.opentk.com/node/2800
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device,
HandleDeviceValueReceived, device);
NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, RunLoop, InputLoopMode);
}
} }
} }
catch (Exception e)
{
Debug.Print("[Mac] Exception in managed callback: {0}", e);
}
} }
void DeviceRemoved(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device) void DeviceRemoved(IntPtr context, IOReturn res, IntPtr sender, IOHIDDeviceRef device)
{ {
bool recognized = false; try
if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse) &&
MouseDevices.ContainsKey(device))
{ {
RemoveMouse(sender, device); bool recognized = false;
recognized = true;
if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse) &&
MouseDevices.ContainsKey(device))
{
RemoveMouse(sender, device);
recognized = true;
}
if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard) &&
KeyboardDevices.ContainsKey(device))
{
RemoveKeyboard(sender, device);
recognized = true;
}
if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick) &&
JoystickDevices.ContainsKey(device))
{
RemoveJoystick(sender, device);
recognized = true;
}
if (recognized)
{
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero);
NativeMethods.IOHIDDeviceUnscheduleFromRunLoop(device, RunLoop, InputLoopMode);
}
} }
catch (Exception e)
if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard) &&
KeyboardDevices.ContainsKey(device))
{ {
RemoveKeyboard(sender, device); Debug.Print("[Mac] Exception in managed callback: {0}", e);
recognized = true;
}
if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick) &&
JoystickDevices.ContainsKey(device))
{
RemoveJoystick(sender, device);
recognized = true;
}
if (recognized)
{
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero);
NativeMethods.IOHIDDeviceUnscheduleFromRunLoop(device, RunLoop, InputLoopMode);
} }
} }
void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val) void DeviceValueReceived(IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val)
{ {
if (disposed) try
{ {
Debug.Print("DeviceValueReceived({0}, {1}, {2}, {3}) called on disposed {4}", if (disposed)
context, res, sender, val, GetType()); {
return; Debug.Print("DeviceValueReceived({0}, {1}, {2}, {3}) called on disposed {4}",
} context, res, sender, val, GetType());
return;
}
MouseState mouse; MouseState mouse;
KeyboardState keyboard; KeyboardState keyboard;
MacJoystick joystick; MacJoystick joystick;
if (MouseDevices.TryGetValue(context, out mouse)) if (MouseDevices.TryGetValue(context, out mouse))
{ {
MouseDevices[context] = UpdateMouse(mouse, val); MouseDevices[context] = UpdateMouse(mouse, val);
}
else if (KeyboardDevices.TryGetValue(context, out keyboard))
{
KeyboardDevices[context] = UpdateKeyboard(keyboard, val);
}
else if (JoystickDevices.TryGetValue(context, out joystick))
{
JoystickDevices[context] = UpdateJoystick(joystick, val);
}
else
{
//Debug.Print ("Device {0:x} not found in list of keyboards or mice", sender);
}
} }
else if (KeyboardDevices.TryGetValue(context, out keyboard)) catch (Exception e)
{ {
KeyboardDevices[context] = UpdateKeyboard(keyboard, val); Debug.Print("[Mac] Exception in managed callback: {0}", e);
}
else if (JoystickDevices.TryGetValue(context, out joystick))
{
JoystickDevices[context] = UpdateJoystick(joystick, val);
}
else
{
//Debug.Print ("Device {0:x} not found in list of keyboards or mice", sender);
} }
} }