Emscripten: Fixed out of range joystick device index after joystick disconnect.
authorPhilipp Wiesemann <philipp.wiesemann@arcor.de>
Sun, 22 Feb 2015 23:21:32 +0100
changeset 9373679eb3986e37
parent 9372 a9bf04588f17
child 9374 b577c4753421
Emscripten: Fixed out of range joystick device index after joystick disconnect.

After disconnecting a joystick the remaining kept their original device index.
This was not correct because the device index must be a number between 0 and
SDL_NumJoysticks(). It was fixed with ideas from SDL's joystick implementation
for Android. Some range checks were removed as the caller already checks them.
src/joystick/emscripten/SDL_sysjoystick.c
     1.1 --- a/src/joystick/emscripten/SDL_sysjoystick.c	Sun Feb 22 21:00:35 2015 +0100
     1.2 +++ b/src/joystick/emscripten/SDL_sysjoystick.c	Sun Feb 22 23:21:32 2015 +0100
     1.3 @@ -112,7 +112,7 @@
     1.4      event.type = SDL_JOYDEVICEADDED;
     1.5  
     1.6      if (SDL_GetEventState(event.type) == SDL_ENABLE) {
     1.7 -        event.jdevice.which = item->index;
     1.8 +        event.jdevice.which = numjoysticks - 1;
     1.9          if ( (SDL_EventOK == NULL) ||
    1.10               (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
    1.11              SDL_PushEvent(&event);
    1.12 @@ -235,6 +235,21 @@
    1.13      return 0;
    1.14  }
    1.15  
    1.16 +/* Returns item matching given SDL device index. */
    1.17 +static SDL_joylist_item *
    1.18 +JoystickByDeviceIndex(int device_index)
    1.19 +{
    1.20 +    SDL_joylist_item *item = SDL_joylist;
    1.21 +
    1.22 +    while (0 < device_index) {
    1.23 +        --device_index;
    1.24 +        item = item->next;
    1.25 +    }
    1.26 +
    1.27 +    return item;
    1.28 +}
    1.29 +
    1.30 +/* Returns item matching given HTML gamepad index. */
    1.31  static SDL_joylist_item *
    1.32  JoystickByIndex(int index)
    1.33  {
    1.34 @@ -265,27 +280,15 @@
    1.35  
    1.36  /* Function to get the device-dependent name of a joystick */
    1.37  const char *
    1.38 -SDL_SYS_JoystickNameForDeviceIndex(int index)
    1.39 +SDL_SYS_JoystickNameForDeviceIndex(int device_index)
    1.40  {
    1.41 -    SDL_joylist_item *item = JoystickByIndex(index);
    1.42 -    if (item == NULL) {
    1.43 -        SDL_SetError("Joystick with index %d not found", index);
    1.44 -        return NULL;
    1.45 -    }
    1.46 -
    1.47 -    return item->name;
    1.48 +    return JoystickByDeviceIndex(device_index)->name;
    1.49  }
    1.50  
    1.51  /* Function to perform the mapping from device index to the instance id for this index */
    1.52 -SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
    1.53 +SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
    1.54  {
    1.55 -    SDL_joylist_item *item = JoystickByIndex(index);
    1.56 -    if (item == NULL) {
    1.57 -        SDL_SetError("Joystick with index %d not found", index);
    1.58 -        return 0;
    1.59 -    }
    1.60 -
    1.61 -    return item->device_instance;
    1.62 +    return JoystickByDeviceIndex(device_index)->device_instance;
    1.63  }
    1.64  
    1.65  /* Function to open a joystick for use.
    1.66 @@ -294,9 +297,9 @@
    1.67     It returns 0, or -1 if there is an error.
    1.68   */
    1.69  int
    1.70 -SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int index)
    1.71 +SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
    1.72  {
    1.73 -    SDL_joylist_item *item = JoystickByIndex(index);
    1.74 +    SDL_joylist_item *item = JoystickByDeviceIndex(device_index);
    1.75  
    1.76      if (item == NULL ) {
    1.77          return SDL_SetError("No such device");
    1.78 @@ -405,18 +408,19 @@
    1.79      emscripten_set_gamepaddisconnected_callback(NULL, 0, NULL);
    1.80  }
    1.81  
    1.82 -SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int index)
    1.83 +SDL_JoystickGUID
    1.84 +SDL_SYS_JoystickGetDeviceGUID(int device_index)
    1.85  {
    1.86      SDL_JoystickGUID guid;
    1.87      /* the GUID is just the first 16 chars of the name for now */
    1.88 -    const char *name = SDL_SYS_JoystickNameForDeviceIndex(index);
    1.89 +    const char *name = SDL_SYS_JoystickNameForDeviceIndex(device_index);
    1.90      SDL_zero(guid);
    1.91      SDL_memcpy(&guid, name, SDL_min(sizeof(guid), SDL_strlen(name)));
    1.92      return guid;
    1.93  }
    1.94  
    1.95 -
    1.96 -SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
    1.97 +SDL_JoystickGUID
    1.98 +SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
    1.99  {
   1.100      SDL_JoystickGUID guid;
   1.101      /* the GUID is just the first 16 chars of the name for now */