SDL-1.2: Support comma-separated lists in SDL_AUDIODRIVER

From e23a8932d254bff936aca61bd40ad7e859e97404 Mon Sep 17 00:00:00 2001
From: Sebastian Krzyszkowiak <[EMAIL REDACTED]>
Date: Tue, 10 Aug 2021 13:39:02 +0200
Subject: [PATCH] Support comma-separated lists in SDL_AUDIODRIVER

Adapted from SDL2:
 https://github.com/libsdl-org/SDL/pull/4267
 https://github.com/libsdl-org/SDL/pull/4618
---
 src/audio/SDL_audio.c | 35 +++++++++++++++++++----------------
 1 file changed, 19 insertions(+), 16 deletions(-)

diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 450a53f46..ad512f242 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -278,13 +278,6 @@ int SDL_AudioInit(const char *driver_name)
 		SDL_AudioQuit();
 	}
 
-#if SDL_AUDIO_DRIVER_PULSE
-	/* SDL 2.0 uses the name "pulseaudio", so we'll support both */
-	if ( driver_name && SDL_strcasecmp(driver_name, "pulseaudio") == 0 ) {
-		driver_name = "pulse";
-	}
-#endif /* */
-
 	/* Select the proper audio driver */
 	audio = NULL;
 	idx = 0;
@@ -320,18 +313,28 @@ int SDL_AudioInit(const char *driver_name)
 #endif /* SDL_AUDIO_DRIVER_ESD */
 	if ( audio == NULL ) {
 		if ( driver_name != NULL ) {
-#if 0	/* This will be replaced with a better driver selection API */
-			if ( SDL_strrchr(driver_name, ':') != NULL ) {
-				idx = atoi(SDL_strrchr(driver_name, ':')+1);
-			}
+			const char *driver_attempt = driver_name;
+			while(driver_attempt != NULL && *driver_attempt != 0 && audio == NULL) {
+				const char* driver_attempt_end = SDL_strchr(driver_attempt, ',');
+				size_t driver_attempt_len = (driver_attempt_end != NULL) ? (driver_attempt_end - driver_attempt)
+				                                                         : SDL_strlen(driver_attempt);
+#if SDL_AUDIO_DRIVER_PULSE
+				/* SDL 2.0 uses the name "pulseaudio", so we'll support both */
+				if ( (driver_attempt_len == SDL_strlen("pulseaudio")) &&
+				     (SDL_strncasecmp(driver_attempt, "pulseaudio", driver_attempt_len) == 0 ) ) {
+					driver_attempt_len = SDL_strlen("pulse");
+				}
 #endif
-			for ( i=0; bootstrap[i]; ++i ) {
-				if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
-					if ( bootstrap[i]->available() ) {
-						audio=bootstrap[i]->create(idx);
-						break;
+				for ( i=0; bootstrap[i]; ++i ) {
+					if ((driver_attempt_len == SDL_strlen(bootstrap[i]->name)) &&
+					    (SDL_strncasecmp(bootstrap[i]->name, driver_attempt, driver_attempt_len) == 0)) {
+						if ( bootstrap[i]->available() ) {
+							audio=bootstrap[i]->create(idx);
+							break;
+						}
 					}
 				}
+				driver_attempt = (driver_attempt_end != NULL) ? (driver_attempt_end + 1) : NULL;
 			}
 		} else {
 			for ( i=0; bootstrap[i]; ++i ) {