Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)
authorSam Lantinga <slouken@libsdl.org>
Wed, 16 Feb 2011 04:51:13 -0800
changeset 5317901a9e9ff0b7
parent 5315 192f2d605339
child 5318 214da1e9fd7c
Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE)

Chusslove Illich 2011-02-13 04:30:28 PST

Currently SDL_JOYSTICK_DEVICE environment variable can be used to add
exactly one topmost device. I think it would be nice if one could also set
several devices (e.g. a stick and a throttle) in this way, as a colon-
separated list. The attached patch implements this
src/joystick/linux/SDL_sysjoystick.c
     1.1 --- a/src/joystick/linux/SDL_sysjoystick.c	Wed Feb 16 04:08:28 2011 -0800
     1.2 +++ b/src/joystick/linux/SDL_sysjoystick.c	Wed Feb 16 04:51:13 2011 -0800
     1.3 @@ -413,21 +413,31 @@
     1.4  
     1.5      numjoysticks = 0;
     1.6  
     1.7 -    /* First see if the user specified a joystick to use */
     1.8 +    /* First see if the user specified one or more joysticks to use */
     1.9      if (SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL) {
    1.10 -        SDL_strlcpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
    1.11 -        if (stat(path, &sb) == 0) {
    1.12 -            fd = open(path, O_RDONLY, 0);
    1.13 -            if (fd >= 0) {
    1.14 -                /* Assume the user knows what they're doing. */
    1.15 -                SDL_joylist[numjoysticks].fname = SDL_strdup(path);
    1.16 -                if (SDL_joylist[numjoysticks].fname) {
    1.17 -                    dev_nums[numjoysticks] = sb.st_rdev;
    1.18 -                    ++numjoysticks;
    1.19 +        char *envcopy, *envpath, *delim;
    1.20 +        envcopy = SDL_strdup(SDL_getenv("SDL_JOYSTICK_DEVICE"));
    1.21 +        envpath = envcopy;
    1.22 +        while (envpath != NULL) {
    1.23 +            delim = SDL_strchr(envpath, ':');
    1.24 +            if (delim != NULL) {
    1.25 +                *delim++ = '\0';
    1.26 +            }
    1.27 +            if (stat(envpath, &sb) == 0) {
    1.28 +                fd = open(envpath, O_RDONLY, 0);
    1.29 +                if (fd >= 0) {
    1.30 +                    /* Assume the user knows what they're doing. */
    1.31 +                    SDL_joylist[numjoysticks].fname = SDL_strdup(envpath);
    1.32 +                    if (SDL_joylist[numjoysticks].fname) {
    1.33 +                        dev_nums[numjoysticks] = sb.st_rdev;
    1.34 +                        ++numjoysticks;
    1.35 +                    }
    1.36 +                    close(fd);
    1.37                  }
    1.38 -                close(fd);
    1.39              }
    1.40 +            envpath = delim;
    1.41          }
    1.42 +        SDL_free(envcopy);
    1.43      }
    1.44  
    1.45      for (i = 0; i < SDL_arraysize(joydev_pattern); ++i) {