diff --git a/Source/OpenTK/Platform/Common/Hid.cs b/Source/OpenTK/Platform/Common/Hid.cs
index 0d3c102d..fc0c4321 100644
--- a/Source/OpenTK/Platform/Common/Hid.cs
+++ b/Source/OpenTK/Platform/Common/Hid.cs
@@ -29,10 +29,83 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Text;
+using OpenTK.Input;
namespace OpenTK.Platform.Common
{
+ class HidHelper
+ {
+ ///
+ /// Scales the specified value linearly between min and max.
+ ///
+ /// The value to scale
+ /// The minimum expected value (inclusive)
+ /// The maximum expected value (inclusive)
+ /// The minimum output value (inclusive)
+ /// The maximum output value (inclusive)
+ /// The value, scaled linearly between min and max
+ public static int ScaleValue(int value, int value_min, int value_max,
+ int result_min, int result_max)
+ {
+ if (value_min >= value_max || result_min >= result_max)
+ throw new ArgumentOutOfRangeException();
+ MathHelper.Clamp(value, value_min, value_max);
+
+ int range = result_max - result_min;
+ int half_range = range >> 1;
+ long temp = (value - value_min) * range; // need long to avoid overflow
+ return (int)(temp / (value_max - value_min) + half_range);
+ }
+
+ public static JoystickAxis TranslateJoystickAxis(HIDPage page, int usage)
+ {
+ switch (page)
+ {
+ case HIDPage.GenericDesktop:
+ switch ((HIDUsageGD)usage)
+ {
+ case HIDUsageGD.X:
+ return JoystickAxis.Axis0;
+ case HIDUsageGD.Y:
+ return JoystickAxis.Axis1;
+
+ case HIDUsageGD.Z:
+ return JoystickAxis.Axis2;
+ case HIDUsageGD.Rz:
+ return JoystickAxis.Axis3;
+
+ case HIDUsageGD.Rx:
+ return JoystickAxis.Axis4;
+ case HIDUsageGD.Ry:
+ return JoystickAxis.Axis5;
+
+ case HIDUsageGD.Slider:
+ return JoystickAxis.Axis6;
+ case HIDUsageGD.Dial:
+ return JoystickAxis.Axis7;
+ case HIDUsageGD.Wheel:
+ return JoystickAxis.Axis8;
+ }
+ break;
+
+ case HIDPage.Simulation:
+ switch ((HIDUsageSim)usage)
+ {
+ case HIDUsageSim.Rudder:
+ return JoystickAxis.Axis9;
+ case HIDUsageSim.Throttle:
+ return JoystickAxis.Axis10;
+ }
+ break;
+ }
+
+ Debug.Print("[Input] Unknown axis with HID page/usage {0}/{1}", page, usage);
+ return 0;
+ }
+ }
+
enum HIDPage : ushort
{
Undefined = 0x00,
diff --git a/Source/OpenTK/Platform/MacOS/HIDInput.cs b/Source/OpenTK/Platform/MacOS/HIDInput.cs
index cccd9206..e4a4614d 100755
--- a/Source/OpenTK/Platform/MacOS/HIDInput.cs
+++ b/Source/OpenTK/Platform/MacOS/HIDInput.cs
@@ -756,7 +756,7 @@ namespace OpenTK.Platform.MacOS
case HIDUsageGD.Dial:
case HIDUsageGD.Wheel:
short offset = GetJoystickAxis(val, elem);
- JoystickAxis axis = TranslateJoystickAxis(usage);
+ JoystickAxis axis = HidHelper.TranslateJoystickAxis(page, usage);
if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last)
{
joy.State.SetAxis(axis, offset);
@@ -780,7 +780,7 @@ namespace OpenTK.Platform.MacOS
case HIDUsageSim.Rudder:
case HIDUsageSim.Throttle:
short offset = GetJoystickAxis(val, elem);
- JoystickAxis axis = TranslateJoystickAxis(usage);
+ JoystickAxis axis = HidHelper.TranslateJoystickAxis(page, usage);
if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last)
{
joy.State.SetAxis(axis, offset);
@@ -807,51 +807,7 @@ namespace OpenTK.Platform.MacOS
int max = NativeMethods.IOHIDElementGetLogicalMax(element).ToInt32();
int min = NativeMethods.IOHIDElementGetLogicalMin(element).ToInt32();
int offset = NativeMethods.IOHIDValueGetIntegerValue(val).ToInt32();
- if (offset < min)
- offset = min;
- if (offset > max)
- offset = max;
-
- const int range = short.MaxValue - short.MinValue + 1;
- const int half_range = short.MaxValue + 1;
- return (short)((offset - min) * range / (max - min) + half_range);
- }
-
- static JoystickAxis TranslateJoystickAxis(int usage)
- {
- switch (usage)
- {
- case (int)HIDUsageGD.X:
- return JoystickAxis.Axis0;
- case (int)HIDUsageGD.Y:
- return JoystickAxis.Axis1;
-
- case (int)HIDUsageGD.Z:
- return JoystickAxis.Axis2;
- case (int)HIDUsageGD.Rz:
- return JoystickAxis.Axis3;
-
- case (int)HIDUsageGD.Rx:
- return JoystickAxis.Axis4;
- case (int)HIDUsageGD.Ry:
- return JoystickAxis.Axis5;
-
- case (int)HIDUsageGD.Slider:
- return JoystickAxis.Axis6;
- case (int)HIDUsageGD.Dial:
- return JoystickAxis.Axis7;
- case (int)HIDUsageGD.Wheel:
- return JoystickAxis.Axis8;
-
- case (int)HIDUsageSim.Rudder:
- return JoystickAxis.Axis9;
- case (int)HIDUsageSim.Throttle:
- return JoystickAxis.Axis10;
-
- default:
- Debug.Print("[Mac] Unknown axis with HID usage {0}", usage);
- return 0;
- }
+ return (short)HidHelper.ScaleValue(offset, min, max, short.MinValue, short.MaxValue);
}
static bool GetJoystickButton(IOHIDValueRef val, IOHIDElementRef element)