Commit 0c3830a9a9d3cf071bb4dc9dee0403af867d66cd

Ryan C. Gordon 2015-06-16T00:57:45

Haptic/Linux: Keep track of device numbers properly to track duplicates. Fixes Bugzilla #3014.

diff --git a/src/haptic/linux/SDL_syshaptic.c b/src/haptic/linux/SDL_syshaptic.c
index f72edfc..ab0a570 100644
--- a/src/haptic/linux/SDL_syshaptic.c
+++ b/src/haptic/linux/SDL_syshaptic.c
@@ -59,6 +59,7 @@ typedef struct SDL_hapticlist_item
 {
     char *fname;                /* Dev path name (like /dev/input/event1) */
     SDL_Haptic *haptic;         /* Associated haptic. */
+    dev_t dev_num;
     struct SDL_hapticlist_item *next;
 } SDL_hapticlist_item;
 
@@ -236,15 +237,11 @@ void haptic_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const 
 static int
 MaybeAddDevice(const char *path)
 {
-    dev_t dev_nums[MAX_HAPTICS];
     struct stat sb;
     int fd;
-    int k;
-    int duplicate;
     int success;
     SDL_hapticlist_item *item;
 
-
     if (path == NULL) {
         return -1;
     }
@@ -255,15 +252,11 @@ MaybeAddDevice(const char *path)
     }
 
     /* check for duplicates */
-    duplicate = 0;
-    for (k = 0; (k < numhaptics) && !duplicate; ++k) {
-        if (sb.st_rdev == dev_nums[k]) {
-            duplicate = 1;
+    for (item = SDL_hapticlist; item != NULL; item = item->next) {
+        if (item->dev_num == sb.st_rdev) {
+            return -1;  /* duplicate. */
         }
     }
-    if (duplicate) {
-        return -1;
-    }
 
     /* try to open */
     fd = open(path, O_RDWR, 0);
@@ -293,6 +286,8 @@ MaybeAddDevice(const char *path)
         return -1;
     }
 
+    item->dev_num = sb.st_rdev;
+
     /* TODO: should we add instance IDs? */
     if (SDL_hapticlist_tail == NULL) {
         SDL_hapticlist = SDL_hapticlist_tail = item;
@@ -301,8 +296,6 @@ MaybeAddDevice(const char *path)
         SDL_hapticlist_tail = item;
     }
 
-    dev_nums[numhaptics] = sb.st_rdev;
-
     ++numhaptics;
 
     /* !!! TODO: Send a haptic add event? */
@@ -546,7 +539,6 @@ SDL_SYS_HapticOpenFromJoystick(SDL_Haptic * haptic, SDL_Joystick * joystick)
     int ret;
     SDL_hapticlist_item *item;
 
-
     /* Find the joystick in the haptic list. */
     for (item = SDL_hapticlist; item; item = item->next) {
         if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {