Browse Source

Set the initial axis values for HIDAPI and XInput controllers

Fixes https://github.com/libsdl-org/SDL/issues/13020

(cherry picked from commit 1f6b5c681d788d3881a40c456bed5ce3e1a83aaa)
Sam Lantinga 2 months ago
parent
commit
bc3eeecf7f
1 changed files with 28 additions and 2 deletions
  1. 28 2
      src/joystick/SDL_joystick.c

+ 28 - 2
src/joystick/SDL_joystick.c

@@ -1190,9 +1190,35 @@ SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)
 
     // If this joystick is known to have all zero centered axes, skip the auto-centering code
     if (SDL_JoystickAxesCenteredAtZero(joystick)) {
-        int i;
+        for (int i = 0; i < joystick->naxes; ++i) {
+            joystick->axes[i].has_initial_value = true;
+        }
+    }
 
-        for (i = 0; i < joystick->naxes; ++i) {
+    // We know the initial values for HIDAPI and XInput joysticks
+    if ((SDL_IsJoystickHIDAPI(joystick->guid) ||
+         SDL_IsJoystickXInput(joystick->guid) ||
+         SDL_IsJoystickRAWINPUT(joystick->guid) ||
+         SDL_IsJoystickWGI(joystick->guid)) &&
+        joystick->naxes >= SDL_GAMEPAD_AXIS_COUNT) {
+        int left_trigger, right_trigger;
+        if (SDL_IsJoystickXInput(joystick->guid)) {
+            left_trigger = 2;
+            right_trigger = 5;
+        } else {
+            left_trigger = SDL_GAMEPAD_AXIS_LEFT_TRIGGER;
+            right_trigger = SDL_GAMEPAD_AXIS_RIGHT_TRIGGER;
+        }
+        for (int i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) {
+            int initial_value;
+            if (i == left_trigger || i == right_trigger) {
+                initial_value = SDL_MIN_SINT16;
+            } else {
+                initial_value = 0;
+            }
+            joystick->axes[i].value = initial_value;
+            joystick->axes[i].zero = initial_value;
+            joystick->axes[i].initial_value = initial_value;
             joystick->axes[i].has_initial_value = true;
         }
     }