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 5316 6bb01861c4c0
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
--- a/src/joystick/linux/SDL_sysjoystick.c	Fri Feb 04 09:07:39 2011 -0800
+++ b/src/joystick/linux/SDL_sysjoystick.c	Wed Feb 16 04:49:07 2011 -0800
@@ -417,21 +417,31 @@
 
 	numjoysticks = 0;
 
-	/* First see if the user specified a joystick to use */
+	/* First see if the user specified one or more joysticks to use */
 	if ( SDL_getenv("SDL_JOYSTICK_DEVICE") != NULL ) {
-		SDL_strlcpy(path, SDL_getenv("SDL_JOYSTICK_DEVICE"), sizeof(path));
-		if ( stat(path, &sb) == 0 ) {
-			fd = open(path, O_RDONLY, 0);
-			if ( fd >= 0 ) {
-				/* Assume the user knows what they're doing. */
-				SDL_joylist[numjoysticks].fname = SDL_strdup(path);
-				if ( SDL_joylist[numjoysticks].fname ) {
-					dev_nums[numjoysticks] = sb.st_rdev;
-					++numjoysticks;
+		char *envcopy, *envpath, *delim;
+		envcopy = SDL_strdup(SDL_getenv("SDL_JOYSTICK_DEVICE"));
+		envpath = envcopy;
+		while ( envpath != NULL ) {
+			delim = SDL_strchr(envpath, ':');
+			if ( delim != NULL ) {
+				*delim++ = '\0';
+			}
+			if ( stat(envpath, &sb) == 0 ) {
+				fd = open(envpath, O_RDONLY, 0);
+				if ( fd >= 0 ) {
+					/* Assume the user knows what they're doing. */
+					SDL_joylist[numjoysticks].fname = SDL_strdup(envpath);
+					if ( SDL_joylist[numjoysticks].fname ) {
+						dev_nums[numjoysticks] = sb.st_rdev;
+						++numjoysticks;
+					}
+					close(fd);
 				}
-				close(fd);
 			}
+			envpath = delim;
 		}
+		SDL_free(envcopy);
 	}
 
 	for ( i=0; i<SDL_arraysize(joydev_pattern); ++i ) {