[Mac] Explicitly close removed devices

Devices are now explicitly closed when they a DeviceRemoved event is
received. Additionally, exceptions are no longer allowed to escape into
unmanaged code, where they might crash the runtime.
This commit is contained in:
thefiddler 2014-07-23 21:19:46 +02:00
parent d12643cf44
commit 57c2f89038

View file

@ -180,53 +180,61 @@ namespace OpenTK.Platform.MacOS
IntPtr @event, IntPtr @event,
IntPtr refcon) IntPtr refcon)
{ {
CursorState.SetIsConnected(true); try
switch (type)
{ {
case CGEventType.MouseMoved: CursorState.SetIsConnected(true);
case CGEventType.LeftMouseDragged:
case CGEventType.RightMouseDragged:
case CGEventType.OtherMouseDragged:
{
Carbon.HIPoint p = CG.EventGetLocation(@event);
CursorState.X = (int)Math.Round(p.X);
CursorState.Y = (int)Math.Round(p.Y);
}
break;
case CGEventType.ScrollWheel: switch (type)
{ {
// Note: OpenTK follows the win32 convention, where case CGEventType.MouseMoved:
// (+h, +v) = (right, up). MacOS reports (+h, +v) = (left, up) case CGEventType.LeftMouseDragged:
// so we need to flip the horizontal scroll direction. case CGEventType.RightMouseDragged:
double h = CG.EventGetDoubleValueField(@event, CGEventField.ScrollWheelEventPointDeltaAxis2) * MacOSFactory.ScrollFactor; case CGEventType.OtherMouseDragged:
double v = CG.EventGetDoubleValueField(@event, CGEventField.ScrollWheelEventPointDeltaAxis1) * MacOSFactory.ScrollFactor; {
CursorState.SetScrollRelative((float)(-h), (float)v); Carbon.HIPoint p = CG.EventGetLocation(@event);
} CursorState.X = (int)Math.Round(p.X);
break; CursorState.Y = (int)Math.Round(p.Y);
}
break;
case CGEventType.LeftMouseDown: case CGEventType.ScrollWheel:
case CGEventType.RightMouseDown: {
case CGEventType.OtherMouseDown: // Note: OpenTK follows the win32 convention, where
{ // (+h, +v) = (right, up). MacOS reports (+h, +v) = (left, up)
int n = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber); // so we need to flip the horizontal scroll direction.
n = n == 1 ? 2 : n == 2 ? 1 : n; // flip middle and right button numbers to match OpenTK double h = CG.EventGetDoubleValueField(@event, CGEventField.ScrollWheelEventPointDeltaAxis2) * MacOSFactory.ScrollFactor;
MouseButton b = MouseButton.Left + n; double v = CG.EventGetDoubleValueField(@event, CGEventField.ScrollWheelEventPointDeltaAxis1) * MacOSFactory.ScrollFactor;
CursorState[b] = true; CursorState.SetScrollRelative((float)(-h), (float)v);
} }
break; break;
case CGEventType.LeftMouseUp: case CGEventType.LeftMouseDown:
case CGEventType.RightMouseUp: case CGEventType.RightMouseDown:
case CGEventType.OtherMouseUp: case CGEventType.OtherMouseDown:
{ {
int n = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber); int n = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber);
n = n == 1 ? 2 : n == 2 ? 1 : n; // flip middle and right button numbers to match OpenTK n = n == 1 ? 2 : n == 2 ? 1 : n; // flip middle and right button numbers to match OpenTK
MouseButton b = MouseButton.Left + n; MouseButton b = MouseButton.Left + n;
CursorState[b] = false; CursorState[b] = true;
} }
break; break;
case CGEventType.LeftMouseUp:
case CGEventType.RightMouseUp:
case CGEventType.OtherMouseUp:
{
int n = CG.EventGetIntegerValueField(@event, CGEventField.MouseEventButtonNumber);
n = n == 1 ? 2 : n == 2 ? 1 : n; // flip middle and right button numbers to match OpenTK
MouseButton b = MouseButton.Left + n;
CursorState[b] = false;
}
break;
}
}
catch (Exception e)
{
// Do not let any exceptions escape into unmanaged code!
Debug.Print(e.ToString());
} }
return @event; return @event;
@ -336,6 +344,7 @@ namespace OpenTK.Platform.MacOS
{ {
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero); NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero);
NativeMethods.IOHIDDeviceUnscheduleFromRunLoop(device, RunLoop, InputLoopMode); NativeMethods.IOHIDDeviceUnscheduleFromRunLoop(device, RunLoop, InputLoopMode);
NativeMethods.IOHIDDeviceClose(device, IOOptionBits.Zero);
} }
} }
catch (Exception e) catch (Exception e)
@ -1093,7 +1102,7 @@ namespace OpenTK.Platform.MacOS
[DllImport(hid)] [DllImport(hid)]
public static extern void IOHIDManagerSetDeviceMatching( public static extern void IOHIDManagerSetDeviceMatching(
IOHIDManagerRef manager, IOHIDManagerRef manager,
CFDictionaryRef matching) ; CFDictionaryRef matching);
[DllImport(hid)] [DllImport(hid)]
public static extern IOReturn IOHIDManagerOpen( public static extern IOReturn IOHIDManagerOpen(
@ -1105,6 +1114,11 @@ namespace OpenTK.Platform.MacOS
IOHIDDeviceRef manager, IOHIDDeviceRef manager,
IOOptionBits opts); IOOptionBits opts);
[DllImport(hid)]
public static extern IOReturn IOHIDDeviceClose(
IOHIDDeviceRef device,
IOOptionBits options);
[DllImport(hid)] [DllImport(hid)]
public static extern CFTypeRef IOHIDDeviceGetProperty( public static extern CFTypeRef IOHIDDeviceGetProperty(
IOHIDDeviceRef device, IOHIDDeviceRef device,