From 471dd5bf1861443c046eecf6290607bcdcef54c0 Mon Sep 17 00:00:00 2001 From: Christopher Lees Date: Tue, 11 Jul 2017 11:46:54 +0100 Subject: [PATCH 1/3] Fix: Some joystick hats not returning centered correctly on Windows HID --- src/OpenTK/Platform/Windows/WinRawJoystick.cs | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/OpenTK/Platform/Windows/WinRawJoystick.cs b/src/OpenTK/Platform/Windows/WinRawJoystick.cs index 970e98c1..9bbe6b90 100644 --- a/src/OpenTK/Platform/Windows/WinRawJoystick.cs +++ b/src/OpenTK/Platform/Windows/WinRawJoystick.cs @@ -342,15 +342,33 @@ namespace OpenTK.Platform.Windows HatPosition GetHatPosition(uint value, HidProtocolValueCaps caps) { if (caps.LogicalMax == 8) - return (HatPosition)value; - else if (caps.LogicalMax == 7) { + //Hat states are represented as a plain number from 0-8 + //with centered being zero + //Padding should have already been stripped out, so just cast + if (value > 8) + { + //Value out of bounds, so return centered + return HatPosition.Centered; + } + return (HatPosition)value; + } + if (caps.LogicalMax == 7) + { + //Hat states are represented as a plain number from 0-7 + //with centered being 8 + if (value > 8) + { + //Return zero if our value is out of bounds ==> e.g. + //Thrustmaster T-Flight Hotas X returns 15 for the centered position + return HatPosition.Centered; + } value++; value %= 9; return (HatPosition)value; } - else - return HatPosition.Centered; + //The HID report length is unsupported + return HatPosition.Centered; } unsafe void UpdateAxes(RawInput* rin, Device stick) From ac9a1fb697d4b8a69e7619bd19e563a6a6391edb Mon Sep 17 00:00:00 2001 From: Christopher Lees Date: Tue, 11 Jul 2017 11:58:04 +0100 Subject: [PATCH 2/3] New: Add 4-position hat switches to Windows HID driver --- src/OpenTK/Platform/Windows/WinRawJoystick.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/OpenTK/Platform/Windows/WinRawJoystick.cs b/src/OpenTK/Platform/Windows/WinRawJoystick.cs index 9bbe6b90..5003af44 100644 --- a/src/OpenTK/Platform/Windows/WinRawJoystick.cs +++ b/src/OpenTK/Platform/Windows/WinRawJoystick.cs @@ -341,6 +341,24 @@ namespace OpenTK.Platform.Windows HatPosition GetHatPosition(uint value, HidProtocolValueCaps caps) { + if (caps.LogicalMax == 3) + { + //4-way hat switch as per the example in Appendix C + //http://www.usb.org/developers/hidpage/Hut1_12v2.pdf + switch (value) + { + case 0: + return HatPosition.Left; + case 1: + return HatPosition.Up; + case 2: + return HatPosition.Right; + case 3: + return HatPosition.Down; + default: + return HatPosition.Centered; + } + } if (caps.LogicalMax == 8) { //Hat states are represented as a plain number from 0-8 From b1450bc2f29af446bbeaa619e811da88fb99270b Mon Sep 17 00:00:00 2001 From: Christopher Lees Date: Tue, 11 Jul 2017 12:38:05 +0100 Subject: [PATCH 3/3] Change: Simplify bounds checking --- src/OpenTK/Platform/Windows/WinRawJoystick.cs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/OpenTK/Platform/Windows/WinRawJoystick.cs b/src/OpenTK/Platform/Windows/WinRawJoystick.cs index 5003af44..afe79473 100644 --- a/src/OpenTK/Platform/Windows/WinRawJoystick.cs +++ b/src/OpenTK/Platform/Windows/WinRawJoystick.cs @@ -341,6 +341,12 @@ namespace OpenTK.Platform.Windows HatPosition GetHatPosition(uint value, HidProtocolValueCaps caps) { + if (value > caps.LogicalMax) + { + //Return zero if our value is out of bounds ==> e.g. + //Thrustmaster T-Flight Hotas X returns 15 for the centered position + return HatPosition.Centered; + } if (caps.LogicalMax == 3) { //4-way hat switch as per the example in Appendix C @@ -355,8 +361,6 @@ namespace OpenTK.Platform.Windows return HatPosition.Right; case 3: return HatPosition.Down; - default: - return HatPosition.Centered; } } if (caps.LogicalMax == 8) @@ -364,23 +368,12 @@ namespace OpenTK.Platform.Windows //Hat states are represented as a plain number from 0-8 //with centered being zero //Padding should have already been stripped out, so just cast - if (value > 8) - { - //Value out of bounds, so return centered - return HatPosition.Centered; - } return (HatPosition)value; } if (caps.LogicalMax == 7) { //Hat states are represented as a plain number from 0-7 //with centered being 8 - if (value > 8) - { - //Return zero if our value is out of bounds ==> e.g. - //Thrustmaster T-Flight Hotas X returns 15 for the centered position - return HatPosition.Centered; - } value++; value %= 9; return (HatPosition)value;