From a047c08d3d159e176a8e86b5fb9480d02e8bc2a4 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Tue, 22 Jul 2014 14:28:48 +0200 Subject: [PATCH] [Mac] Correctly shutdown HID input driver --- Source/OpenTK/Platform/MacOS/HIDInput.cs | 36 +++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/Source/OpenTK/Platform/MacOS/HIDInput.cs b/Source/OpenTK/Platform/MacOS/HIDInput.cs index 5bde1be..19c9d80 100755 --- a/Source/OpenTK/Platform/MacOS/HIDInput.cs +++ b/Source/OpenTK/Platform/MacOS/HIDInput.cs @@ -77,7 +77,7 @@ namespace OpenTK.Platform.MacOS new Dictionary(new IntPtrEqualityComparer()); } - readonly IOHIDManagerRef hidmanager; + IOHIDManagerRef hidmanager; readonly Dictionary MouseDevices = new Dictionary(new IntPtrEqualityComparer()); @@ -124,6 +124,12 @@ namespace OpenTK.Platform.MacOS // For retrieving input directly from the hardware hidmanager = CreateHIDManager(); + if (hidmanager == IntPtr.Zero) + { + Debug.Print("[Mac] Failed to create IO HID manager, HIDInput driver not supported."); + throw new NotSupportedException(); + } + RegisterHIDCallbacks(hidmanager); // For retrieving the global cursor position @@ -1032,7 +1038,11 @@ namespace OpenTK.Platform.MacOS [DllImport(hid)] public static extern IOHIDManagerRef IOHIDManagerCreate( - CFAllocatorRef allocator, IOOptionBits options) ; + CFAllocatorRef allocator, IOOptionBits options); + + [DllImport(hid)] + public static extern IOReturn IOHIDManagerClose( + IOHIDManagerRef allocator, IOOptionBits options); // This routine will be called when a new (matching) device is connected. [DllImport(hid)] @@ -1724,6 +1734,17 @@ namespace OpenTK.Platform.MacOS { if (manual) { + if (MouseEventTap != IntPtr.Zero) + { + CF.CFRelease(MouseEventTap); + MouseEventTap = IntPtr.Zero; + } + if (MouseEventTapSource != IntPtr.Zero) + { + CF.CFRelease(MouseEventTapSource); + MouseEventTapSource = IntPtr.Zero; + } + NativeMethods.IOHIDManagerRegisterDeviceMatchingCallback( hidmanager, IntPtr.Zero, IntPtr.Zero); NativeMethods.IOHIDManagerRegisterDeviceRemovalCallback( @@ -1750,15 +1771,10 @@ namespace OpenTK.Platform.MacOS HandleDeviceRemoved = null; HandleDeviceValueReceived = null; - if (MouseEventTap != IntPtr.Zero) - { - CF.CFRelease(MouseEventTap); - MouseEventTap = IntPtr.Zero; - } - if (MouseEventTapSource != IntPtr.Zero) + if (hidmanager != IntPtr.Zero) { - CF.CFRelease(MouseEventTapSource); - MouseEventTapSource = IntPtr.Zero; + NativeMethods.IOHIDManagerClose(hidmanager, IOOptionBits.Zero); + hidmanager = IntPtr.Zero; } } else -- 2.7.4