diff --git a/Source/OpenTK/Platform/MacOS/HIDInput.cs b/Source/OpenTK/Platform/MacOS/HIDInput.cs index 9090e703..5322e30e 100755 --- a/Source/OpenTK/Platform/MacOS/HIDInput.cs +++ b/Source/OpenTK/Platform/MacOS/HIDInput.cs @@ -60,11 +60,17 @@ namespace OpenTK.Platform.MacOS new Dictionary(new IntPtrEqualityComparer()); readonly Dictionary MouseIndexToDevice = new Dictionary(); + readonly Dictionary KeyboardDevices = new Dictionary(new IntPtrEqualityComparer()); readonly Dictionary KeyboardIndexToDevice = new Dictionary(); + readonly Dictionary JoystickDevices = + new Dictionary(new IntPtrEqualityComparer()); + readonly Dictionary JoystickIndexToDevice = + new Dictionary(); + readonly CFRunLoop RunLoop = CF.CFRunLoopGetMain(); readonly CFString InputLoopMode = CF.RunLoopModeDefault; readonly CFDictionary DeviceTypes = new CFDictionary(); @@ -126,41 +132,19 @@ namespace OpenTK.Platform.MacOS if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse)) { - if (!MouseDevices.ContainsKey(device)) - { - Debug.Print("Mouse device {0:x} discovered, sender is {1:x}", device, sender); - MouseState state = new MouseState(); - state.IsConnected = true; - MouseIndexToDevice.Add(MouseDevices.Count, device); - MouseDevices.Add(device, state); - } - else - { - Debug.Print("Mouse device {0:x} reconnected, sender is {1:x}", device, sender); - MouseState state = MouseDevices[device]; - state.IsConnected = true; - MouseDevices[device] = state; - } + AddMouse(sender, device); } if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard)) { - if (!KeyboardDevices.ContainsKey(device)) - { - Debug.Print("Keyboard device {0:x} discovered, sender is {1:x}", device, sender); - KeyboardState state = new KeyboardState(); - state.IsConnected = true; - KeyboardIndexToDevice.Add(KeyboardDevices.Count, device); - KeyboardDevices.Add(device, state); - } - else - { - Debug.Print("Keyboard device {0:x} reconnected, sender is {1:x}", device, sender); - KeyboardState state = KeyboardDevices[device]; - state.IsConnected = true; - KeyboardDevices[device] = state; - } + AddKeyboard(sender, device); + } + + if (NativeMethods.IOHIDDeviceConformsTo(device, + HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick)) + { + AddJoystick(sender, device); } // The device is not normally available in the InputValueCallback (HandleDeviceValueReceived), so we include @@ -178,23 +162,19 @@ namespace OpenTK.Platform.MacOS if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Mouse) && MouseDevices.ContainsKey(device)) { - Debug.Print("Mouse device {0:x} disconnected, sender is {1:x}", device, sender); - - // Keep the device in case it comes back later on - MouseState state = MouseDevices[device]; - state.IsConnected = false; - MouseDevices[device] = state; + RemoveMouse(sender, device); } if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Keyboard) && KeyboardDevices.ContainsKey(device)) { - Debug.Print("Keyboard device {0:x} disconnected, sender is {1:x}", device, sender); + RemoveKeyboard(sender, device); + } - // Keep the device in case it comes back later on - KeyboardState state = KeyboardDevices[device]; - state.IsConnected = false; - KeyboardDevices[device] = state; + if (NativeMethods.IOHIDDeviceConformsTo(device, HIDPage.GenericDesktop, (int)HIDUsageGD.Joystick) && + JoystickDevices.ContainsKey(device)) + { + RemoveJoystick(sender, device); } NativeMethods.IOHIDDeviceRegisterInputValueCallback(device, IntPtr.Zero, IntPtr.Zero); @@ -224,6 +204,36 @@ namespace OpenTK.Platform.MacOS } } + #region Mouse + + void AddMouse(CFAllocatorRef sender, CFAllocatorRef device) + { + if (!MouseDevices.ContainsKey(device)) + { + Debug.Print("Mouse device {0:x} discovered, sender is {1:x}", device, sender); + MouseState state = new MouseState(); + state.IsConnected = true; + MouseIndexToDevice.Add(MouseDevices.Count, device); + MouseDevices.Add(device, state); + } + else + { + Debug.Print("Mouse device {0:x} reconnected, sender is {1:x}", device, sender); + MouseState state = MouseDevices[device]; + state.IsConnected = true; + MouseDevices[device] = state; + } + } + + void RemoveMouse(CFAllocatorRef sender, CFAllocatorRef device) + { + Debug.Print("Mouse device {0:x} disconnected, sender is {1:x}", device, sender); + // Keep the device in case it comes back later on + MouseState state = MouseDevices[device]; + state.IsConnected = false; + MouseDevices[device] = state; + } + static MouseState UpdateMouse(MouseState state, IOHIDValueRef val) { IOHIDElementRef elem = NativeMethods.IOHIDValueGetElement(val); @@ -260,6 +270,38 @@ namespace OpenTK.Platform.MacOS return state; } + #endregion + + #region Keyboard + + void AddKeyboard(CFAllocatorRef sender, CFAllocatorRef device) + { + if (!KeyboardDevices.ContainsKey(device)) + { + Debug.Print("Keyboard device {0:x} discovered, sender is {1:x}", device, sender); + KeyboardState state = new KeyboardState(); + state.IsConnected = true; + KeyboardIndexToDevice.Add(KeyboardDevices.Count, device); + KeyboardDevices.Add(device, state); + } + else + { + Debug.Print("Keyboard device {0:x} reconnected, sender is {1:x}", device, sender); + KeyboardState state = KeyboardDevices[device]; + state.IsConnected = true; + KeyboardDevices[device] = state; + } + } + + void RemoveKeyboard(CFAllocatorRef sender, CFAllocatorRef device) + { + Debug.Print("Keyboard device {0:x} disconnected, sender is {1:x}", device, sender); + // Keep the device in case it comes back later on + KeyboardState state = KeyboardDevices[device]; + state.IsConnected = false; + KeyboardDevices[device] = state; + } + static KeyboardState UpdateKeyboard(KeyboardState state, IOHIDValueRef val) { IOHIDElementRef elem = NativeMethods.IOHIDValueGetElement(val); @@ -289,6 +331,22 @@ namespace OpenTK.Platform.MacOS #endregion + #region Joystick + + void AddJoystick(CFAllocatorRef sender, CFAllocatorRef device) + { + throw new NotImplementedException(); + } + + void RemoveJoystick(CFAllocatorRef sender, CFAllocatorRef device) + { + throw new NotImplementedException(); + } + + #endregion + + #endregion + #region IInputDriver2 Members public IMouseDriver2 MouseDriver { get { return this; } }