joystick: Make sure recentering events happen before disconnect events. Fixes Bugzilla #5063.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index aeedd29..bc6c720 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -1074,14 +1074,46 @@ static void UpdateEventsForDeviceRemoval()
SDL_small_free(events, isstack);
}
+static void
+SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
+{
+ int i;
+
+ /* Tell the app that everything is centered/unpressed... */
+ for (i = 0; i < joystick->naxes; i++) {
+ if (joystick->axes[i].has_initial_value) {
+ SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
+ }
+ }
+
+ for (i = 0; i < joystick->nbuttons; i++) {
+ SDL_PrivateJoystickButton(joystick, i, 0);
+ }
+
+ for (i = 0; i < joystick->nhats; i++) {
+ SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
+ }
+}
+
void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
{
- SDL_Joystick *joystick;
+ SDL_Joystick *joystick = NULL;
int player_index;
-
#if !SDL_EVENTS_DISABLED
SDL_Event event;
+#endif
+
+ /* Find this joystick... */
+ for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
+ if (joystick->instance_id == device_instance) {
+ SDL_PrivateJoystickForceRecentering(joystick);
+ joystick->attached = SDL_FALSE;
+ break;
+ }
+ }
+#if !SDL_EVENTS_DISABLED
+ SDL_zero(event);
event.type = SDL_JOYDEVICEREMOVED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
@@ -1092,15 +1124,6 @@ void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
UpdateEventsForDeviceRemoval();
#endif /* !SDL_EVENTS_DISABLED */
- /* Mark this joystick as no longer attached */
- for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
- if (joystick->instance_id == device_instance) {
- joystick->attached = SDL_FALSE;
- joystick->force_recentering = SDL_TRUE;
- break;
- }
- }
-
SDL_LockJoysticks();
player_index = SDL_GetPlayerIndexForJoystickID(device_instance);
if (player_index >= 0) {
@@ -1349,22 +1372,9 @@ SDL_JoystickUpdate(void)
SDL_UnlockJoysticks();
}
+ /* !!! FIXME: only one thing sets force_recentering now (the Darwin code), see if that can be removed. */
if (joystick->force_recentering) {
- /* Tell the app that everything is centered/unpressed... */
- for (i = 0; i < joystick->naxes; i++) {
- if (joystick->axes[i].has_initial_value) {
- SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
- }
- }
-
- for (i = 0; i < joystick->nbuttons; i++) {
- SDL_PrivateJoystickButton(joystick, i, 0);
- }
-
- for (i = 0; i < joystick->nhats; i++) {
- SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
- }
-
+ SDL_PrivateJoystickForceRecentering(joystick);
joystick->force_recentering = SDL_FALSE;
}
}