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 9373 679eb3986e37
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
--- a/src/joystick/emscripten/SDL_sysjoystick.c	Sun Feb 22 21:00:35 2015 +0100
+++ b/src/joystick/emscripten/SDL_sysjoystick.c	Sun Feb 22 23:21:32 2015 +0100
@@ -112,7 +112,7 @@
     event.type = SDL_JOYDEVICEADDED;
 
     if (SDL_GetEventState(event.type) == SDL_ENABLE) {
-        event.jdevice.which = item->index;
+        event.jdevice.which = numjoysticks - 1;
         if ( (SDL_EventOK == NULL) ||
              (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
             SDL_PushEvent(&event);
@@ -235,6 +235,21 @@
     return 0;
 }
 
+/* Returns item matching given SDL device index. */
+static SDL_joylist_item *
+JoystickByDeviceIndex(int device_index)
+{
+    SDL_joylist_item *item = SDL_joylist;
+
+    while (0 < device_index) {
+        --device_index;
+        item = item->next;
+    }
+
+    return item;
+}
+
+/* Returns item matching given HTML gamepad index. */
 static SDL_joylist_item *
 JoystickByIndex(int index)
 {
@@ -265,27 +280,15 @@
 
 /* Function to get the device-dependent name of a joystick */
 const char *
-SDL_SYS_JoystickNameForDeviceIndex(int index)
+SDL_SYS_JoystickNameForDeviceIndex(int device_index)
 {
-    SDL_joylist_item *item = JoystickByIndex(index);
-    if (item == NULL) {
-        SDL_SetError("Joystick with index %d not found", index);
-        return NULL;
-    }
-
-    return item->name;
+    return JoystickByDeviceIndex(device_index)->name;
 }
 
 /* Function to perform the mapping from device index to the instance id for this index */
-SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int index)
+SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
 {
-    SDL_joylist_item *item = JoystickByIndex(index);
-    if (item == NULL) {
-        SDL_SetError("Joystick with index %d not found", index);
-        return 0;
-    }
-
-    return item->device_instance;
+    return JoystickByDeviceIndex(device_index)->device_instance;
 }
 
 /* Function to open a joystick for use.
@@ -294,9 +297,9 @@
    It returns 0, or -1 if there is an error.
  */
 int
-SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int index)
+SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
 {
-    SDL_joylist_item *item = JoystickByIndex(index);
+    SDL_joylist_item *item = JoystickByDeviceIndex(device_index);
 
     if (item == NULL ) {
         return SDL_SetError("No such device");
@@ -405,18 +408,19 @@
     emscripten_set_gamepaddisconnected_callback(NULL, 0, NULL);
 }
 
-SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int index)
+SDL_JoystickGUID
+SDL_SYS_JoystickGetDeviceGUID(int device_index)
 {
     SDL_JoystickGUID guid;
     /* the GUID is just the first 16 chars of the name for now */
-    const char *name = SDL_SYS_JoystickNameForDeviceIndex(index);
+    const char *name = SDL_SYS_JoystickNameForDeviceIndex(device_index);
     SDL_zero(guid);
     SDL_memcpy(&guid, name, SDL_min(sizeof(guid), SDL_strlen(name)));
     return guid;
 }
 
-
-SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
+SDL_JoystickGUID
+SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
 {
     SDL_JoystickGUID guid;
     /* the GUID is just the first 16 chars of the name for now */