Fixed bug #1121 (More than one device through SDL_JOYSTICK_DEVICE) SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Wed, 16 Feb 2011 04:49:07 -0800
branchSDL-1.2
changeset 53166bb01861c4c0
parent 5170 2fdf3e37f4d1
child 5334 d898ee5431f5
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	Fri Feb 04 09:07:39 2011 -0800
     1.2 +++ b/src/joystick/linux/SDL_sysjoystick.c	Wed Feb 16 04:49:07 2011 -0800
     1.3 @@ -417,21 +417,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 ) {