src/joystick/SDL_joystick.c
changeset 10226 cb13d22b7f09
parent 9998 f67cf37e9cd4
child 10477 dc4bec0fc176
     1.1 --- a/src/joystick/SDL_joystick.c	Fri Aug 26 11:16:44 2016 -0700
     1.2 +++ b/src/joystick/SDL_joystick.c	Fri Aug 26 12:18:08 2016 -0700
     1.3 @@ -497,6 +497,71 @@
     1.4  
     1.5  /* These are global for SDL_sysjoystick.c and SDL_events.c */
     1.6  
     1.7 +void SDL_PrivateJoystickAdded(int device_index)
     1.8 +{
     1.9 +#if !SDL_EVENTS_DISABLED
    1.10 +    SDL_Event event;
    1.11 +
    1.12 +    event.type = SDL_JOYDEVICEADDED;
    1.13 +
    1.14 +    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
    1.15 +        event.jdevice.which = device_index;
    1.16 +        if ( (SDL_EventOK == NULL) ||
    1.17 +             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
    1.18 +            SDL_PushEvent(&event);
    1.19 +        }
    1.20 +    }
    1.21 +#endif /* !SDL_EVENTS_DISABLED */
    1.22 +}
    1.23 +
    1.24 +/*
    1.25 + * If there is an existing add event in the queue, it needs to be modified
    1.26 + * to have the right value for which, because the number of controllers in
    1.27 + * the system is now one less.
    1.28 + */
    1.29 +static void UpdateEventsForDeviceRemoval()
    1.30 +{
    1.31 +    int i, num_events;
    1.32 +    SDL_Event *events;
    1.33 +
    1.34 +    num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
    1.35 +    if (num_events <= 0) {
    1.36 +        return;
    1.37 +    }
    1.38 +
    1.39 +    events = SDL_stack_alloc(SDL_Event, num_events);
    1.40 +    if (!events) {
    1.41 +        return;
    1.42 +    }
    1.43 +
    1.44 +    num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
    1.45 +    for (i = 0; i < num_events; ++i) {
    1.46 +        --events[i].jdevice.which;
    1.47 +    }
    1.48 +    SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
    1.49 +
    1.50 +    SDL_stack_free(events);
    1.51 +}
    1.52 +
    1.53 +void SDL_PrivateJoystickRemoved(SDL_JoystickID device_instance)
    1.54 +{
    1.55 +#if !SDL_EVENTS_DISABLED
    1.56 +    SDL_Event event;
    1.57 +
    1.58 +    event.type = SDL_JOYDEVICEREMOVED;
    1.59 +
    1.60 +    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
    1.61 +        event.jdevice.which = device_instance;
    1.62 +        if ( (SDL_EventOK == NULL) ||
    1.63 +             (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
    1.64 +            SDL_PushEvent(&event);
    1.65 +        }
    1.66 +    }
    1.67 +
    1.68 +    UpdateEventsForDeviceRemoval();
    1.69 +#endif /* !SDL_EVENTS_DISABLED */
    1.70 +}
    1.71 +
    1.72  int
    1.73  SDL_PrivateJoystickAxis(SDL_Joystick * joystick, Uint8 axis, Sint16 value)
    1.74  {