joystick: On disconnect, recenter all game controller inputs. The joystick layer can't necessarily give us perfect centering, but we know that the game controller level has logical absolute idle positions that have nothing to do with the physical device. So send game controller events to make it look like the device is completely untouched before sending the final removal event.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
diff --git a/src/joystick/SDL_gamecontroller.c b/src/joystick/SDL_gamecontroller.c
index 4dcbd5d..32eb9e2 100644
--- a/src/joystick/SDL_gamecontroller.c
+++ b/src/joystick/SDL_gamecontroller.c
@@ -344,6 +344,28 @@ static void HandleJoystickHat(SDL_GameController *gamecontroller, int hat, Uint8
gamecontroller->last_hat_mask[hat] = value;
}
+
+/* The joystick layer will _also_ send events to recenter before disconnect,
+ but it has to make (sometimes incorrect) guesses at what being "centered"
+ is. The game controller layer, however, can set a definite logical idle
+ position, so set them all here. If we happened to already be at the
+ center thanks to the joystick layer or idle hands, this won't generate
+ duplicate events. */
+static void RecenterGameController(SDL_GameController *gamecontroller)
+{
+ SDL_GameControllerButton button;
+ SDL_GameControllerAxis axis;
+
+ for (button = (SDL_GameControllerButton) 0; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
+ SDL_PrivateGameControllerButton(gamecontroller, button, SDL_RELEASED);
+ }
+
+ for (axis = (SDL_GameControllerAxis) 0; axis < SDL_CONTROLLER_AXIS_MAX; axis++) {
+ SDL_PrivateGameControllerAxis(gamecontroller, axis, 0);
+ }
+}
+
+
/*
* Event filter to fire controller events from joystick ones
*/
@@ -404,6 +426,8 @@ static int SDLCALL SDL_GameControllerEventWatcher(void *userdata, SDL_Event * ev
if (controllerlist->joystick->instance_id == event->jdevice.which) {
SDL_Event deviceevent;
+ RecenterGameController(controllerlist);
+
deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
deviceevent.cdevice.which = event->jdevice.which;
SDL_PushEvent(&deviceevent);