Use main run loop for input callbacks. This way we don't have to run the loop ourselves.

Unregister callbacks for removed devices.
This commit is contained in:
the_fiddler 2010-11-24 13:13:14 +00:00
parent 6a4b41d975
commit 898315a58f
2 changed files with 14 additions and 14 deletions

View file

@ -128,6 +128,9 @@ namespace OpenTK.Platform.MacOS.Carbon
[DllImport(appServices)] [DllImport(appServices)]
internal static extern CFRunLoop CFRunLoopGetCurrent(); internal static extern CFRunLoop CFRunLoopGetCurrent();
[DllImport(appServices)]
internal static extern CFRunLoop CFRunLoopGetMain();
[DllImport(appServices)] [DllImport(appServices)]
internal static extern CFRunLoopExitReason CFRunLoopRunInMode( internal static extern CFRunLoopExitReason CFRunLoopRunInMode(
IntPtr cfstrMode, double interval, bool returnAfterSourceHandled); IntPtr cfstrMode, double interval, bool returnAfterSourceHandled);

View file

@ -49,7 +49,11 @@ namespace OpenTK.Platform.MacOS
#region Fields #region Fields
readonly IOHIDManagerRef hidmanager; readonly IOHIDManagerRef hidmanager;
readonly static CFString InputLoopMode = CF.CFSTR("opentkRunLoopCheckDevicesMode"); //readonly static CFRunLoop RunLoop = CF.CFRunLoopGetCurrent();
//readonly static CFString InputLoopMode = CF.CFSTR("opentkInputMode");
readonly static CFRunLoop RunLoop = CF.CFRunLoopGetMain();
readonly static CFString InputLoopMode = CF.RunLoopModeDefault;
readonly static CFDictionary DeviceTypes = new CFDictionary(); readonly static CFDictionary DeviceTypes = new CFDictionary();
readonly static NativeMethods.IOHIDDeviceCallback HandleDeviceAdded = delegate( readonly static NativeMethods.IOHIDDeviceCallback HandleDeviceAdded = delegate(
@ -63,7 +67,8 @@ namespace OpenTK.Platform.MacOS
Debug.Print("Device {0} connected", device); Debug.Print("Device {0} connected", device);
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, HandleValue, IntPtr.Zero); NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, HandleValue, IntPtr.Zero);
NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, CF.CFRunLoopGetCurrent(), InputLoopMode); NativeMethods.IOHIDDeviceScheduleWithRunLoop(device,
RunLoop, InputLoopMode);
} }
}; };
readonly static NativeMethods.IOHIDDeviceCallback HandleDeviceRemoved = delegate( readonly static NativeMethods.IOHIDDeviceCallback HandleDeviceRemoved = delegate(
@ -71,12 +76,12 @@ namespace OpenTK.Platform.MacOS
{ {
Debug.Print("Device {0} disconnected", device); Debug.Print("Device {0} disconnected", device);
NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, null, IntPtr.Zero); NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, null, IntPtr.Zero);
NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, IntPtr.Zero, IntPtr.Zero); //NativeMethods.IOHIDDeviceScheduleWithRunLoop(device, IntPtr.Zero, IntPtr.Zero);
}; };
readonly static NativeMethods.IOHIDValueCallback HandleValue = delegate( readonly static NativeMethods.IOHIDValueCallback HandleValue = delegate(
IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef device) IntPtr context, IOReturn res, IntPtr sender, IOHIDValueRef val)
{ {
Debug.Print("Value {0}:{1} received", sender, val);
}; };
#endregion #endregion
@ -104,23 +109,15 @@ namespace OpenTK.Platform.MacOS
// are called when we run the loop in CheckDevicesMode // are called when we run the loop in CheckDevicesMode
static void RegisterHIDCallbacks(IOHIDManagerRef hidmanager) static void RegisterHIDCallbacks(IOHIDManagerRef hidmanager)
{ {
CFRunLoop runloop = CF.CFRunLoopGetCurrent();
NativeMethods.IOHIDManagerRegisterDeviceMatchingCallback( NativeMethods.IOHIDManagerRegisterDeviceMatchingCallback(
hidmanager, HandleDeviceAdded, IntPtr.Zero); hidmanager, HandleDeviceAdded, IntPtr.Zero);
NativeMethods.IOHIDManagerRegisterDeviceRemovalCallback( NativeMethods.IOHIDManagerRegisterDeviceRemovalCallback(
hidmanager, HandleDeviceRemoved, IntPtr.Zero); hidmanager, HandleDeviceRemoved, IntPtr.Zero);
NativeMethods.IOHIDManagerScheduleWithRunLoop(hidmanager, NativeMethods.IOHIDManagerScheduleWithRunLoop(hidmanager,
runloop, InputLoopMode); RunLoop, InputLoopMode);
NativeMethods.IOHIDManagerSetDeviceMatching(hidmanager, DeviceTypes.Ref); NativeMethods.IOHIDManagerSetDeviceMatching(hidmanager, DeviceTypes.Ref);
NativeMethods.IOHIDManagerOpen(hidmanager, IOOptionBits.Zero); NativeMethods.IOHIDManagerOpen(hidmanager, IOOptionBits.Zero);
while (CF.CFRunLoopRunInMode(InputLoopMode, 0, true) ==
CF.CFRunLoopExitReason.HandledSource)
{
// Do nothing. The real work is done in the Handle* callbacks above.
}
} }
#endregion #endregion