Bug 1648865 - Convert Gamepad trigger axis values to button values. r=baku
authorDaosheng Mu <daoshengmu@gmail.com>
Thu, 23 Jul 2020 18:13:24 +0000
changeset 542069 b3a3c131a27916b7b4751ba935f1560b1aba8b0f
parent 542068 2bd75664acd68cf8bf2ab2309259cda3ddbd994a
child 542070 0a3bb5c4669879bbba273296d2e7cbea5e26fb75
push id122610
push userdmu@mozilla.com
push dateFri, 24 Jul 2020 16:19:51 +0000
treeherderautoland@b3a3c131a279 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1648865
milestone80.0a1
first release with
nightly linux32
b3a3c131a279 / 80.0a1 / 20200724213808 / files
nightly linux64
b3a3c131a279 / 80.0a1 / 20200724213808 / files
nightly mac
b3a3c131a279 / 80.0a1 / 20200724213808 / files
nightly win32
b3a3c131a279 / 80.0a1 / 20200724213808 / files
nightly win64
b3a3c131a279 / 80.0a1 / 20200724213808 / files
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1648865 - Convert Gamepad trigger axis values to button values. r=baku Differential Revision: https://phabricator.services.mozilla.com/D81428
dom/gamepad/GamepadRemapping.cpp
--- a/dom/gamepad/GamepadRemapping.cpp
+++ b/dom/gamepad/GamepadRemapping.cpp
@@ -42,28 +42,35 @@ enum CanonicalButtonIndex {
 enum CanonicalAxisIndex {
   AXIS_INDEX_LEFT_STICK_X,
   AXIS_INDEX_LEFT_STICK_Y,
   AXIS_INDEX_RIGHT_STICK_X,
   AXIS_INDEX_RIGHT_STICK_Y,
   AXIS_INDEX_COUNT
 };
 
+const float BUTTON_THRESHOLD_VALUE = 0.1f;
+
 float NormalizeTouch(long aValue, long aMin, long aMax) {
   return (2.f * (aValue - aMin) / static_cast<float>(aMax - aMin)) - 1.f;
 }
 
 bool AxisNegativeAsButton(float input) {
   const float value = (input < -0.5f) ? 1.f : 0.f;
-  return value > 0.1f;
+  return value > BUTTON_THRESHOLD_VALUE;
 }
 
 bool AxisPositiveAsButton(float input) {
   const float value = (input > 0.5f) ? 1.f : 0.f;
-  return value > 0.1f;
+  return value > BUTTON_THRESHOLD_VALUE;
+}
+
+double AxisToButtonValue(double aValue) {
+  // Mapping axis value range from (-1, +1) to (0, +1).
+  return (aValue + 1.0f) * 0.5f;
 }
 
 void FetchDpadFromAxis(uint32_t aIndex, double dir) {
   bool up = false;
   bool right = false;
   bool down = false;
   bool left = false;
 
@@ -168,24 +175,28 @@ class ADT1Remapper final : public Gamepa
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
       case 2:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
-      case 3:
+      case 3: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
-      case 4:
+      }
+      case 4: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 5:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
       case 9:
         FetchDpadFromAxis(aIndex, aValue);
         break;
       default:
         NS_WARNING(
@@ -324,22 +335,26 @@ class StadiaControllerRemapper final : p
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
       case 2:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
       case 3:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
-      case 4:
-        service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER, aValue);
+      case 4: {
+        const double value = AxisToButtonValue(aValue);
+        service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER, value > BUTTON_THRESHOLD_VALUE, value);
         break;
-      case 5:
-        service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER, aValue);
+      }
+      case 5: {
+        const double value = AxisToButtonValue(aValue);
+        service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER, value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       default:
         NS_WARNING(
             nsPrintfCString(
                 "Axis idx '%d' doesn't support in StadiaControllerRemapper().",
                 aAxis)
                 .get());
         break;
     }
@@ -400,17 +415,17 @@ class Playstation3Remapper final : publi
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
       case 5:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
       default:
         NS_WARNING(
             nsPrintfCString(
-                "Axis idx '%d' doesn't support in Dualshock4Remapper().", aAxis)
+                "Axis idx '%d' doesn't support in Playstation3Remapper().", aAxis)
                 .get());
         break;
     }
   }
 
   void RemapButtonEvent(uint32_t aIndex, uint32_t aButton,
                         bool aPressed) const override {
     RefPtr<GamepadPlatformService> service =
@@ -570,24 +585,28 @@ class Dualshock4Remapper final : public 
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
       case 2:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
-      case 3:
+      case 3: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
-        break;
-      case 4:
+                                value > BUTTON_THRESHOLD_VALUE, value);
+         break;
+      }
+      case 4: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
-        break;
+                                value > BUTTON_THRESHOLD_VALUE, value);
+         break;
+      }
       case 5:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
       case 9:
         FetchDpadFromAxis(aIndex, aValue);
         break;
       default:
         NS_WARNING(
@@ -827,24 +846,28 @@ class NvShieldRemapper final : public Ga
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
       case 2:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
-      case 3:
+      case 3: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
-      case 4:
+      }
+      case 4: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 5:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
       case 9:
         FetchDpadFromAxis(aIndex, aValue);
         break;
       default:
         NS_WARNING(
@@ -919,24 +942,28 @@ class NvShield2017Remapper final : publi
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
       case 2:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
-      case 3:
+      case 3: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
-      case 4:
+      }
+      case 4: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 5:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
       case 9:
         FetchDpadFromAxis(aIndex, aValue);
         break;
       default:
         NS_WARNING(
@@ -1084,24 +1111,28 @@ class XSkillsRemapper final : public Gam
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
       case 2:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
-      case 3:
+      case 3: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
-      case 4:
+      }
+      case 4: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 5:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
       default:
         NS_WARNING(
             nsPrintfCString(
                 "Axis idx '%d' doesn't support in XSkillsRemapper().", aAxis)
                 .get());
@@ -1225,17 +1256,17 @@ class BoomN64PsxRemapper final : public 
  private:
   enum GamecubeButtons {
     GAMECUBE_BUTTON_LEFT_TRIGGER_CLICK = BUTTON_INDEX_COUNT,
     GAMECUBE_BUTTON_RIGHT_TRIGGER_CLICK,
     GAMECUBE_BUTTON_COUNT
   };
 };
 
-class AnalogGamepadRemapper final : public GamepadRemapper {
+class StadiaControllerOldFirmwareRemapper final : public GamepadRemapper {
  public:
   virtual uint32_t GetAxisCount() const override { return AXIS_INDEX_COUNT; }
 
   virtual uint32_t GetButtonCount() const override {
     return ANALOG_GAMEPAD_BUTTON_COUNT;
   }
 
   virtual void RemapAxisMoveEvent(uint32_t aIndex, uint32_t aAxis,
@@ -1251,24 +1282,28 @@ class AnalogGamepadRemapper final : publ
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
       case 2:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
-      case 3:
+      case 3: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
-      case 4:
+      }
+      case 4: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 5:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
       case 9:
         FetchDpadFromAxis(aIndex, aValue);
         break;
       default:
         NS_WARNING(
@@ -1347,24 +1382,28 @@ class RazerServalRemapper final : public
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
       case 2:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
-      case 3:
+      case 3: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
-      case 4:
+      }
+      case 4: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 5:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
       case 9:
         FetchDpadFromAxis(aIndex, aValue);
         break;
       default:
         NS_WARNING(
@@ -1430,24 +1469,28 @@ class MogaProRemapper final : public Gam
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
       case 2:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
-      case 3:
+      case 3: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
-      case 4:
+      }
+      case 4: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 5:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
       case 9:
         FetchDpadFromAxis(aIndex, aValue);
         break;
       default:
         NS_WARNING(
@@ -1507,30 +1550,34 @@ class OnLiveWirelessRemapper final : pub
 
     switch (aAxis) {
       case 0:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
-      case 2:
+      case 2: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 3:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
       case 4:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
-      case 5:
+      case 5: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 9:
         FetchDpadFromAxis(aIndex, aValue);
         break;
       default:
         NS_WARNING(
             nsPrintfCString(
                 "Axis idx '%d' doesn't support in OnLiveWirelessRemapper().",
                 aAxis)
@@ -1592,30 +1639,34 @@ class OUYARemapper final : public Gamepa
 
     switch (aAxis) {
       case 0:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_X, aValue);
         break;
       case 1:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_LEFT_STICK_Y, aValue);
         break;
-      case 2:
+      case 2: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_LEFT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       case 3:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_X, aValue);
         break;
       case 4:
         service->NewAxisMoveEvent(aIndex, AXIS_INDEX_RIGHT_STICK_Y, aValue);
         break;
-      case 5:
+      case 5: {
+        const double value = AxisToButtonValue(aValue);
         service->NewButtonEvent(aIndex, BUTTON_INDEX_RIGHT_TRIGGER,
-                                aValue > 0.1f);
+                                value > BUTTON_THRESHOLD_VALUE, value);
         break;
+      }
       default:
         NS_WARNING(
             nsPrintfCString("Axis idx '%d' doesn't support in OUYARemapper().",
                             aAxis)
                 .get());
         break;
     }
   }
@@ -1667,17 +1718,17 @@ already_AddRefed<GamepadRemapper> GetGam
       {GamepadId::kNintendoProduct2007, new SwitchJoyConRemapper()},
       {GamepadId::kNintendoProduct2009, new SwitchProRemapper()},
       {GamepadId::kNintendoProduct200e, new SwitchProRemapper()},
       {GamepadId::kNvidiaProduct7210, new NvShieldRemapper()},
       {GamepadId::kNvidiaProduct7214, new NvShield2017Remapper()},
       {GamepadId::kPadixProduct2060, new IBuffaloRemapper()},
       {GamepadId::kPlayComProduct0005, new XSkillsRemapper()},
       {GamepadId::kPrototypeVendorProduct0667, new BoomN64PsxRemapper()},
-      {GamepadId::kPrototypeVendorProduct9401, new AnalogGamepadRemapper()},
+      {GamepadId::kPrototypeVendorProduct9401, new StadiaControllerOldFirmwareRemapper()},
       {GamepadId::kRazer1532Product0900, new RazerServalRemapper()},
       {GamepadId::kSonyProduct0268, new Playstation3Remapper()},
       {GamepadId::kSonyProduct05c4, new Dualshock4Remapper()},
       {GamepadId::kSonyProduct09cc, new Dualshock4Remapper()},
       {GamepadId::kSonyProduct0ba0, new Dualshock4Remapper()},
       {GamepadId::kVendor20d6Product6271, new MogaProRemapper()},
       {GamepadId::kVendor2378Product1008, new OnLiveWirelessRemapper()},
       {GamepadId::kVendor2378Product100a, new OnLiveWirelessRemapper()},