joystick: Make sure recentering events happen before disconnect events.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 26 Mar 2020 18:47:04 -0400
changeset 136749adcb74f0be2
parent 13673 be892626e1aa
child 13675 e3844f61860a
joystick: Make sure recentering events happen before disconnect events.

Fixes Bugzilla #5063.
src/joystick/SDL_joystick.c
     1.1 --- a/src/joystick/SDL_joystick.c	Thu Mar 26 13:42:56 2020 -0400
     1.2 +++ b/src/joystick/SDL_joystick.c	Thu Mar 26 18:47:04 2020 -0400
     1.3 @@ -1074,14 +1074,46 @@
     1.4      SDL_small_free(events, isstack);
     1.5  }
     1.6  
     1.7 +static void
     1.8 +SDL_PrivateJoystickForceRecentering(SDL_Joystick *joystick)
     1.9 +{
    1.10 +    int i;
    1.11 +
    1.12 +    /* Tell the app that everything is centered/unpressed... */
    1.13 +    for (i = 0; i < joystick->naxes; i++) {
    1.14 +        if (joystick->axes[i].has_initial_value) {
    1.15 +            SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
    1.16 +        }
    1.17 +    }
    1.18 +
    1.19 +    for (i = 0; i < joystick->nbuttons; i++) {
    1.20 +        SDL_PrivateJoystickButton(joystick, i, 0);
    1.21 +    }
    1.22 +
    1.23 +    for (i = 0; i < joystick->nhats; i++) {
    1.24 +        SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
    1.25 +    }
    1.26 +}
    1.27 +
    1.28  void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
    1.29  {
    1.30 -    SDL_Joystick *joystick;
    1.31 +    SDL_Joystick *joystick = NULL;
    1.32      int player_index;
    1.33 +#if !SDL_EVENTS_DISABLED
    1.34 +    SDL_Event event;
    1.35 +#endif
    1.36 +
    1.37 +    /* Find this joystick... */
    1.38 +    for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
    1.39 +        if (joystick->instance_id == device_instance) {
    1.40 +            SDL_PrivateJoystickForceRecentering(joystick);
    1.41 +            joystick->attached = SDL_FALSE;
    1.42 +            break;
    1.43 +        }
    1.44 +    }
    1.45  
    1.46  #if !SDL_EVENTS_DISABLED
    1.47 -    SDL_Event event;
    1.48 -
    1.49 +    SDL_zero(event);
    1.50      event.type = SDL_JOYDEVICEREMOVED;
    1.51  
    1.52      if (SDL_GetEventState(event.type) == SDL_ENABLE) {
    1.53 @@ -1092,15 +1124,6 @@
    1.54      UpdateEventsForDeviceRemoval();
    1.55  #endif /* !SDL_EVENTS_DISABLED */
    1.56  
    1.57 -    /* Mark this joystick as no longer attached */
    1.58 -    for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
    1.59 -        if (joystick->instance_id == device_instance) {
    1.60 -            joystick->attached = SDL_FALSE;
    1.61 -            joystick->force_recentering = SDL_TRUE;
    1.62 -            break;
    1.63 -        }
    1.64 -    }
    1.65 -
    1.66      SDL_LockJoysticks();
    1.67      player_index = SDL_GetPlayerIndexForJoystickID(device_instance);
    1.68      if (player_index >= 0) {
    1.69 @@ -1349,22 +1372,9 @@
    1.70              SDL_UnlockJoysticks();
    1.71          }
    1.72  
    1.73 +        /* !!! FIXME: only one thing sets force_recentering now (the Darwin code), see if that can be removed. */
    1.74          if (joystick->force_recentering) {
    1.75 -            /* Tell the app that everything is centered/unpressed... */
    1.76 -            for (i = 0; i < joystick->naxes; i++) {
    1.77 -                if (joystick->axes[i].has_initial_value) {
    1.78 -                    SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
    1.79 -                }
    1.80 -            }
    1.81 -
    1.82 -            for (i = 0; i < joystick->nbuttons; i++) {
    1.83 -                SDL_PrivateJoystickButton(joystick, i, 0);
    1.84 -            }
    1.85 -
    1.86 -            for (i = 0; i < joystick->nhats; i++) {
    1.87 -                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
    1.88 -            }
    1.89 -
    1.90 +            SDL_PrivateJoystickForceRecentering(joystick);
    1.91              joystick->force_recentering = SDL_FALSE;
    1.92          }
    1.93      }