[Win] Detect xinput devices and use xinput driver

This commit is contained in:
thefiddler 2014-08-01 00:41:49 +02:00
parent be575d4475
commit 0bd6854a65

View file

@ -50,6 +50,8 @@ namespace OpenTK.Platform.Windows
new List<HidProtocolValueCaps>(); new List<HidProtocolValueCaps>();
internal readonly List<HidProtocolButtonCaps> ButtonCaps = internal readonly List<HidProtocolButtonCaps> ButtonCaps =
new List<HidProtocolButtonCaps>(); new List<HidProtocolButtonCaps>();
internal readonly bool IsXInput;
internal readonly int XInputIndex;
readonly Dictionary<int, JoystickAxis> axes = readonly Dictionary<int, JoystickAxis> axes =
new Dictionary<int,JoystickAxis>(); new Dictionary<int,JoystickAxis>();
@ -60,10 +62,12 @@ namespace OpenTK.Platform.Windows
#region Constructors #region Constructors
public Device(IntPtr handle, Guid guid) public Device(IntPtr handle, Guid guid, bool is_xinput, int xinput_index)
{ {
Handle = handle; Handle = handle;
Guid = guid; Guid = guid;
IsXInput = is_xinput;
XInputIndex = xinput_index;
} }
#endregion #endregion
@ -165,6 +169,8 @@ namespace OpenTK.Platform.Windows
#endregion #endregion
} }
XInputJoystick XInput = new XInputJoystick();
// Defines which types of HID devices we are interested in // Defines which types of HID devices we are interested in
readonly RawInputDevice[] DeviceTypes; readonly RawInputDevice[] DeviceTypes;
@ -218,6 +224,7 @@ namespace OpenTK.Platform.Windows
} }
// Discover joystick devices // Discover joystick devices
int xinput_device_count = 0;
foreach (RawInputDeviceList dev in WinRawInput.GetDeviceList()) foreach (RawInputDeviceList dev in WinRawInput.GetDeviceList())
{ {
// Skip non-joystick devices // Skip non-joystick devices
@ -232,7 +239,8 @@ namespace OpenTK.Platform.Windows
// because it is costly to query (and we need to query // because it is costly to query (and we need to query
// that every time we process a device event.) // that every time we process a device event.)
IntPtr handle = dev.Device; IntPtr handle = dev.Device;
Guid guid = GetDeviceGuid(handle); bool is_xinput;
Guid guid = GetDeviceGuid(handle, out is_xinput);
long hardware_id = handle.ToInt64(); long hardware_id = handle.ToInt64();
Device device = Devices.FromHardwareId(hardware_id); Device device = Devices.FromHardwareId(hardware_id);
@ -243,7 +251,8 @@ namespace OpenTK.Platform.Windows
} }
else else
{ {
device = new Device(handle, guid); device = new Device(handle, guid, is_xinput,
is_xinput ? xinput_device_count++ : 0);
// This is a new device, query its capabilities and add it // This is a new device, query its capabilities and add it
// to the device list // to the device list
@ -299,6 +308,11 @@ namespace OpenTK.Platform.Windows
return false; return false;
} }
if (stick.IsXInput)
{
return true;
}
if (!GetPreparsedData(handle, ref PreparsedData)) if (!GetPreparsedData(handle, ref PreparsedData))
{ {
return false; return false;
@ -589,8 +603,9 @@ namespace OpenTK.Platform.Windows
// Retrieves the GUID of a device, which is stored // Retrieves the GUID of a device, which is stored
// in the last part of the DEVICENAME string // in the last part of the DEVICENAME string
Guid GetDeviceGuid(IntPtr handle) Guid GetDeviceGuid(IntPtr handle, out bool is_xinput)
{ {
is_xinput = false;
Guid guid = new Guid(); Guid guid = new Guid();
unsafe unsafe
@ -628,6 +643,8 @@ namespace OpenTK.Platform.Windows
{ {
guid = new Guid(parts[3]); guid = new Guid(parts[3]);
} }
is_xinput = name.Contains("IG_");
} }
return guid; return guid;
@ -669,7 +686,15 @@ namespace OpenTK.Platform.Windows
{ {
if (IsValid(index)) if (IsValid(index))
{ {
return Devices.FromIndex(index).GetState(); Device dev = Devices.FromIndex(index);
if (dev.IsXInput)
{
return XInput.GetState(dev.XInputIndex);
}
else
{
return dev.GetState();
}
} }
return new JoystickState(); return new JoystickState();
} }
@ -681,7 +706,15 @@ namespace OpenTK.Platform.Windows
{ {
if (IsValid(index)) if (IsValid(index))
{ {
return Devices.FromIndex(index).GetCapabilities(); Device dev = Devices.FromIndex(index);
if (dev.IsXInput)
{
return XInput.GetCapabilities(dev.XInputIndex);
}
else
{
return dev.GetCapabilities();
}
} }
return new JoystickCapabilities(); return new JoystickCapabilities();
} }
@ -693,7 +726,15 @@ namespace OpenTK.Platform.Windows
{ {
if (IsValid(index)) if (IsValid(index))
{ {
return Devices.FromIndex(index).GetGuid(); Device dev = Devices.FromIndex(index);
if (dev.IsXInput)
{
return XInput.GetGuid(dev.XInputIndex);
}
else
{
return dev.GetGuid();
}
} }
return new Guid(); return new Guid();
} }