SDL: Support comma-separated lists in SDL_AUDIODRIVER

From 45de0a1d1339cabb1e96203c75c410b2d4cb34f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luis=20C=C3=A1ceres?= <[EMAIL REDACTED]>
Date: Fri, 2 Apr 2021 06:28:10 +0100
Subject: [PATCH] Support comma-separated lists in SDL_AUDIODRIVER

---
 src/audio/SDL_audio.c | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 5ce94d1fff..dff8d0817d 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -972,19 +972,38 @@ SDL_AudioInit(const char *driver_name)
         driver_name = SDL_getenv("SDL_AUDIODRIVER");
     }
 
-    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
-        /* make sure we should even try this driver before doing so... */
-        const AudioBootStrap *backend = bootstrap[i];
-        if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
-            (!driver_name && backend->demand_only)) {
-            continue;
+    if (driver_name != NULL) {
+        const char *driver_attempt = driver_name;
+        while (driver_attempt != NULL && *driver_attempt != 0 && !initialized) {
+            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);
+
+            for (i = 0; bootstrap[i]; ++i) {
+                if (SDL_strncasecmp(bootstrap[i]->name, driver_attempt, driver_attempt_len) == 0) {
+                    tried_to_init = 1;
+                    SDL_zero(current_audio);
+                    current_audio.name = bootstrap[i]->name;
+                    current_audio.desc = bootstrap[i]->desc;
+                    initialized = bootstrap[i]->init(&current_audio.impl);
+                    break;
+                }
+            }
+
+            driver_attempt = (driver_attempt_end != NULL) ? (driver_attempt_end + 1) : NULL;
         }
+    } else {
+        for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
+            if(bootstrap[i]->demand_only) {
+                continue;
+            }
 
-        tried_to_init = 1;
-        SDL_zero(current_audio);
-        current_audio.name = backend->name;
-        current_audio.desc = backend->desc;
-        initialized = backend->init(&current_audio.impl);
+            tried_to_init = 1;
+            SDL_zero(current_audio);
+            current_audio.name = bootstrap[i]->name;
+            current_audio.desc = bootstrap[i]->desc;
+            initialized = bootstrap[i]->init(&current_audio.impl);
+        }
     }
 
     if (!initialized) {