src/joystick/SDL_gamecontroller.c
changeset 10226 cb13d22b7f09
parent 10225 3134026517cb
child 10423 c5b42350ccbb
     1.1 --- a/src/joystick/SDL_gamecontroller.c	Fri Aug 26 11:16:44 2016 -0700
     1.2 +++ b/src/joystick/SDL_gamecontroller.c	Fri Aug 26 12:18:08 2016 -0700
     1.3 @@ -106,6 +106,35 @@
     1.4  int SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state);
     1.5  
     1.6  /*
     1.7 + * If there is an existing add event in the queue, it needs to be modified
     1.8 + * to have the right value for which, because the number of controllers in
     1.9 + * the system is now one less.
    1.10 + */
    1.11 +static void UpdateEventsForDeviceRemoval()
    1.12 +{
    1.13 +    int i, num_events;
    1.14 +    SDL_Event *events;
    1.15 +
    1.16 +    num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
    1.17 +    if (num_events <= 0) {
    1.18 +        return;
    1.19 +    }
    1.20 +
    1.21 +    events = SDL_stack_alloc(SDL_Event, num_events);
    1.22 +    if (!events) {
    1.23 +        return;
    1.24 +    }
    1.25 +
    1.26 +    num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
    1.27 +    for (i = 0; i < num_events; ++i) {
    1.28 +        --events[i].cdevice.which;
    1.29 +    }
    1.30 +    SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
    1.31 +
    1.32 +    SDL_stack_free(events);
    1.33 +}
    1.34 +
    1.35 +/*
    1.36   * Event filter to fire controller events from joystick ones
    1.37   */
    1.38  int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
    1.39 @@ -222,22 +251,13 @@
    1.40              SDL_GameController *controllerlist = SDL_gamecontrollers;
    1.41              while (controllerlist) {
    1.42                  if (controllerlist->joystick->instance_id == event->jdevice.which) {
    1.43 -					SDL_Event peeped;
    1.44                      SDL_Event deviceevent;
    1.45  
    1.46 -					/* If there is an existing add event in the queue, it
    1.47 -					 * needs to be modified to have the right value for which,
    1.48 -					 * because the number of controllers in the system is now
    1.49 -					 * one less.
    1.50 -					 */
    1.51 -					if ( SDL_PeepEvents(&peeped, 1, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED) > 0) {
    1.52 -						peeped.jdevice.which--;
    1.53 -						SDL_PushEvent(&peeped);
    1.54 -					}
    1.55 -
    1.56                      deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
    1.57                      deviceevent.cdevice.which = event->jdevice.which;
    1.58                      SDL_PushEvent(&deviceevent);
    1.59 +
    1.60 +                    UpdateEventsForDeviceRemoval();
    1.61                      break;
    1.62                  }
    1.63                  controllerlist = controllerlist->next;