Skip to content

Commit 1f6b5c6

Browse files
committed
Set the initial axis values for HIDAPI and XInput controllers
Fixes #13020
1 parent fdc4f8f commit 1f6b5c6

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/joystick/SDL_joystick.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,9 +1361,35 @@ SDL_Joystick *SDL_OpenJoystick(SDL_JoystickID instance_id)
13611361

13621362
// If this joystick is known to have all zero centered axes, skip the auto-centering code
13631363
if (SDL_JoystickAxesCenteredAtZero(joystick)) {
1364-
int i;
1364+
for (int i = 0; i < joystick->naxes; ++i) {
1365+
joystick->axes[i].has_initial_value = true;
1366+
}
1367+
}
13651368

1366-
for (i = 0; i < joystick->naxes; ++i) {
1369+
// We know the initial values for HIDAPI and XInput joysticks
1370+
if ((SDL_IsJoystickHIDAPI(joystick->guid) ||
1371+
SDL_IsJoystickXInput(joystick->guid) ||
1372+
SDL_IsJoystickRAWINPUT(joystick->guid) ||
1373+
SDL_IsJoystickWGI(joystick->guid)) &&
1374+
joystick->naxes >= SDL_GAMEPAD_AXIS_COUNT) {
1375+
int left_trigger, right_trigger;
1376+
if (SDL_IsJoystickXInput(joystick->guid)) {
1377+
left_trigger = 2;
1378+
right_trigger = 5;
1379+
} else {
1380+
left_trigger = SDL_GAMEPAD_AXIS_LEFT_TRIGGER;
1381+
right_trigger = SDL_GAMEPAD_AXIS_RIGHT_TRIGGER;
1382+
}
1383+
for (int i = 0; i < SDL_GAMEPAD_AXIS_COUNT; ++i) {
1384+
int initial_value;
1385+
if (i == left_trigger || i == right_trigger) {
1386+
initial_value = SDL_MIN_SINT16;
1387+
} else {
1388+
initial_value = 0;
1389+
}
1390+
joystick->axes[i].value = initial_value;
1391+
joystick->axes[i].zero = initial_value;
1392+
joystick->axes[i].initial_value = initial_value;
13671393
joystick->axes[i].has_initial_value = true;
13681394
}
13691395
}

0 commit comments

Comments
 (0)