Permalink
Browse files

[Mac] Calculate joystick Guid for USB devices

  • Loading branch information...
1 parent b458b40 commit 5c5afb3ea32934caf7f2d462194f49ef4adfc23f @thefiddler thefiddler committed Jan 5, 2014
Showing with 29 additions and 2 deletions.
  1. +29 −2 Source/OpenTK/Platform/MacOS/HIDInput.cs
@@ -58,6 +58,7 @@ class HIDInput : IInputDriver2, IMouseDriver2, IKeyboardDriver2, IJoystickDriver
class JoystickDetails
{
public string Name;
+ public Guid Guid;
public JoystickState State;
public JoystickCapabilities Capabilities;
readonly public Dictionary<int, JoystickButton> ElementUsageToButton =
@@ -394,7 +395,30 @@ static KeyboardState UpdateKeyboard(KeyboardState state, IOHIDValueRef val)
#region Joystick
- MacJoystick CreateJoystick(CFAllocatorRef sender, CFAllocatorRef device)
+ Guid CreateJoystickGuid(IntPtr device)
+ {
+ // Create a device guid from the product and vendor id keys
+ List<byte> guid_bytes = new List<byte>();
+ long vendor_id = 0;
+ long product_id = 0;
+
+ IntPtr vendor_id_ref = NativeMethods.IOHIDDeviceGetProperty(device, NativeMethods.IOHIDVendorIDKey);
+ IntPtr product_id_ref = NativeMethods.IOHIDDeviceGetProperty(device, NativeMethods.IOHIDProductIDKey);
+ if (vendor_id_ref != IntPtr.Zero)
+ {
+ CF.CFNumberGetValue(vendor_id_ref, CF.CFNumberType.kCFNumberLongType, out vendor_id);
+ }
+ if (product_id_ref != IntPtr.Zero)
+ {
+ CF.CFNumberGetValue(product_id_ref, CF.CFNumberType.kCFNumberLongType, out product_id);
+ }
+
+ guid_bytes.AddRange(BitConverter.GetBytes(vendor_id));
+ guid_bytes.AddRange(BitConverter.GetBytes(product_id));
+ return new Guid(guid_bytes.ToArray());
+ }
+
+ MacJoystick CreateJoystick(IntPtr sender, IntPtr device)
{
MacJoystick joy = null;
@@ -406,6 +430,8 @@ MacJoystick CreateJoystick(CFAllocatorRef sender, CFAllocatorRef device)
int buttons = 0;
int dpads = 0;
+ Guid guid = CreateJoystickGuid(device);
+
CFStringRef name_ref = NativeMethods.IOHIDDeviceGetProperty(device, NativeMethods.IOHIDProductKey);
string name = CF.CFStringGetCString(name_ref);
@@ -459,6 +485,7 @@ MacJoystick CreateJoystick(CFAllocatorRef sender, CFAllocatorRef device)
joy = new MacJoystick(-1, axes, buttons);
joy.Details.Name = name;
+ joy.Details.Guid = guid;
joy.Details.State.SetIsConnected(true);
joy.Details.Capabilities = new JoystickCapabilities(axes, buttons, true);
@@ -783,7 +810,7 @@ Guid IJoystickDriver2.GetGuid(int index)
MacJoystick joystick = GetJoystick(index);
if (joystick != null)
{
- //return joystick.Details.Capabilities;
+ return joystick.Details.Guid;
}
return new Guid();
}

0 comments on commit 5c5afb3

Please sign in to comment.