Skip to content

Commit

Permalink
Fixed incorrect WGI controller state when the application loses focus
Browse files Browse the repository at this point in the history
Recenter the controller elements when WGI stops reporting valid state

Fixes libsdl-org#5261
  • Loading branch information
slouken committed Nov 11, 2022
1 parent 875e9b3 commit b7e65a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/joystick/SDL_joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ static void UpdateEventsForDeviceRemoval(int device_index, Uint32 type)
SDL_small_free(events, isstack);
}

static void
void
SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
{
int i, j;
Expand Down
1 change: 1 addition & 0 deletions src/joystick/SDL_joystick_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ extern void SDL_PrivateJoystickAddTouchpad(SDL_Joystick *joystick, int nfingers)
extern void SDL_PrivateJoystickAddSensor(SDL_Joystick *joystick, SDL_SensorType type, float rate);
extern void SDL_PrivateJoystickAdded(SDL_JoystickID device_instance);
extern void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance);
extern void SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick);
extern int SDL_PrivateJoystickAxis(SDL_Joystick *joystick,
Uint8 axis, Sint16 value);
extern int SDL_PrivateJoystickBall(SDL_Joystick *joystick,
Expand Down
26 changes: 19 additions & 7 deletions src/joystick/windows/SDL_windows_gaming_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -852,15 +852,27 @@ WGI_JoystickUpdate(SDL_Joystick *joystick)
hr = __x_ABI_CWindows_CGaming_CInput_CIRawGameController_GetCurrentReading(hwdata->controller, nbuttons, buttons, nhats, hats, naxes, axes, &timestamp);
if (SUCCEEDED(hr) && timestamp != hwdata->timestamp) {
UINT32 i;
SDL_bool all_zero = SDL_TRUE;

for (i = 0; i < nbuttons; ++i) {
SDL_PrivateJoystickButton(joystick, (Uint8)i, buttons[i]);
}
for (i = 0; i < nhats; ++i) {
SDL_PrivateJoystickHat(joystick, (Uint8)i, ConvertHatValue(hats[i]));
}
/* The axes are all zero when the application loses focus */
for (i = 0; i < naxes; ++i) {
SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)((int) (axes[i] * 65535) - 32768));
if (axes[i] != 0.0f) {
all_zero = SDL_FALSE;
break;
}
}
if (all_zero) {
SDL_PrivateJoystickForceRecentering(joystick);
} else {
for (i = 0; i < nbuttons; ++i) {
SDL_PrivateJoystickButton(joystick, (Uint8) i, buttons[i]);
}
for (i = 0; i < nhats; ++i) {
SDL_PrivateJoystickHat(joystick, (Uint8) i, ConvertHatValue(hats[i]));
}
for (i = 0; i < naxes; ++i) {
SDL_PrivateJoystickAxis(joystick, (Uint8)i, (Sint16)((int) (axes[i] * 65535) - 32768));
}
}
hwdata->timestamp = timestamp;
}
Expand Down

0 comments on commit b7e65a8

Please sign in to comment.