Haptic/Linux: Keep track of device numbers properly to track duplicates.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 16 Jun 2015 00:57:45 -0400
changeset 97435f2ac7cec7e9
parent 9742 4189503733ac
child 9744 59a012991992
Haptic/Linux: Keep track of device numbers properly to track duplicates.

Fixes Bugzilla #3014.
src/haptic/linux/SDL_syshaptic.c
     1.1 --- a/src/haptic/linux/SDL_syshaptic.c	Sun Jun 14 19:26:20 2015 -0700
     1.2 +++ b/src/haptic/linux/SDL_syshaptic.c	Tue Jun 16 00:57:45 2015 -0400
     1.3 @@ -59,6 +59,7 @@
     1.4  {
     1.5      char *fname;                /* Dev path name (like /dev/input/event1) */
     1.6      SDL_Haptic *haptic;         /* Associated haptic. */
     1.7 +    dev_t dev_num;
     1.8      struct SDL_hapticlist_item *next;
     1.9  } SDL_hapticlist_item;
    1.10  
    1.11 @@ -236,15 +237,11 @@
    1.12  static int
    1.13  MaybeAddDevice(const char *path)
    1.14  {
    1.15 -    dev_t dev_nums[MAX_HAPTICS];
    1.16      struct stat sb;
    1.17      int fd;
    1.18 -    int k;
    1.19 -    int duplicate;
    1.20      int success;
    1.21      SDL_hapticlist_item *item;
    1.22  
    1.23 -
    1.24      if (path == NULL) {
    1.25          return -1;
    1.26      }
    1.27 @@ -255,15 +252,11 @@
    1.28      }
    1.29  
    1.30      /* check for duplicates */
    1.31 -    duplicate = 0;
    1.32 -    for (k = 0; (k < numhaptics) && !duplicate; ++k) {
    1.33 -        if (sb.st_rdev == dev_nums[k]) {
    1.34 -            duplicate = 1;
    1.35 +    for (item = SDL_hapticlist; item != NULL; item = item->next) {
    1.36 +        if (item->dev_num == sb.st_rdev) {
    1.37 +            return -1;  /* duplicate. */
    1.38          }
    1.39      }
    1.40 -    if (duplicate) {
    1.41 -        return -1;
    1.42 -    }
    1.43  
    1.44      /* try to open */
    1.45      fd = open(path, O_RDWR, 0);
    1.46 @@ -293,6 +286,8 @@
    1.47          return -1;
    1.48      }
    1.49  
    1.50 +    item->dev_num = sb.st_rdev;
    1.51 +
    1.52      /* TODO: should we add instance IDs? */
    1.53      if (SDL_hapticlist_tail == NULL) {
    1.54          SDL_hapticlist = SDL_hapticlist_tail = item;
    1.55 @@ -301,8 +296,6 @@
    1.56          SDL_hapticlist_tail = item;
    1.57      }
    1.58  
    1.59 -    dev_nums[numhaptics] = sb.st_rdev;
    1.60 -
    1.61      ++numhaptics;
    1.62  
    1.63      /* !!! TODO: Send a haptic add event? */
    1.64 @@ -546,7 +539,6 @@
    1.65      int ret;
    1.66      SDL_hapticlist_item *item;
    1.67  
    1.68 -
    1.69      /* Find the joystick in the haptic list. */
    1.70      for (item = SDL_hapticlist; item; item = item->next) {
    1.71          if (SDL_strcmp(item->fname, joystick->hwdata->fname) == 0) {