SDL: Fixed audio device removed events for ALSA

From e57fef8f0b7270952aae8b204e998f6164be43cf Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 23 Oct 2023 18:42:48 -0700
Subject: [PATCH] Fixed audio device removed events for ALSA

---
 src/audio/SDL_audio.c           | 15 +++++++++++++++
 src/audio/SDL_sysaudio.h        |  3 +++
 src/audio/alsa/SDL_alsa_audio.c |  2 +-
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/audio/SDL_audio.c b/src/audio/SDL_audio.c
index 5afe415edb89..73d85c61fddc 100644
--- a/src/audio/SDL_audio.c
+++ b/src/audio/SDL_audio.c
@@ -1265,6 +1265,21 @@ SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle)
     return SDL_FindPhysicalAudioDeviceByCallback(TestDeviceHandleCallback, handle);
 }
 
+static SDL_bool TestDeviceHandleStringCallback(SDL_AudioDevice *device, void *handle)
+{
+    if (handle && device->handle) {
+        if (SDL_strcmp((char *)handle, (char *)device->handle) == 0) {
+            return SDL_TRUE;
+        }
+    }
+    return SDL_FALSE;
+}
+
+SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandleString(const char *handle)
+{
+    return SDL_FindPhysicalAudioDeviceByCallback(TestDeviceHandleStringCallback, (void *)handle);
+}
+
 char *SDL_GetAudioDeviceName(SDL_AudioDeviceID devid)
 {
     SDL_AudioDevice *device = ObtainPhysicalAudioDevice(devid);
diff --git a/src/audio/SDL_sysaudio.h b/src/audio/SDL_sysaudio.h
index 374616c2c456..2c0ac0e10795 100644
--- a/src/audio/SDL_sysaudio.h
+++ b/src/audio/SDL_sysaudio.h
@@ -97,6 +97,9 @@ extern int SDL_AudioDeviceFormatChangedAlreadyLocked(SDL_AudioDevice *device, co
 // Find the SDL_AudioDevice associated with the handle supplied to SDL_AddAudioDevice. NULL if not found. DOES NOT LOCK THE DEVICE.
 extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle);
 
+// Find the SDL_AudioDevice associated with the handle as a string supplied to SDL_AddAudioDevice. NULL if not found. DOES NOT LOCK THE DEVICE.
+extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandleString(const char *handle);
+
 // Find an SDL_AudioDevice, selected by a callback. NULL if not found. DOES NOT LOCK THE DEVICE.
 extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByCallback(SDL_bool (*callback)(SDL_AudioDevice *device, void *userdata), void *userdata);
 
diff --git a/src/audio/alsa/SDL_alsa_audio.c b/src/audio/alsa/SDL_alsa_audio.c
index 0cec9bf5e200..3d2042d55909 100644
--- a/src/audio/alsa/SDL_alsa_audio.c
+++ b/src/audio/alsa/SDL_alsa_audio.c
@@ -877,7 +877,7 @@ static void ALSA_HotplugIteration(SDL_bool *has_default_output, SDL_bool *has_de
         for (ALSA_Device *dev = unseen; dev; dev = next) {
             //SDL_LogInfo(SDL_LOG_CATEGORY_AUDIO, "ALSA: removing %s device '%s'", dev->iscapture ? "capture" : "output", dev->name);
             next = dev->next;
-            SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandle(dev->name));
+            SDL_AudioDeviceDisconnected(SDL_FindPhysicalAudioDeviceByHandleString(dev->name));
             SDL_free(dev->name);
             SDL_free(dev);
         }