Skip to content

Commit e68f5ca

Browse files
sloukenicculus
authored andcommitted
alsa: use udev if available instead of a hotplug thread
This makes detecting audio device changes more responsive.
1 parent 051ce0f commit e68f5ca

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

src/audio/alsa/SDL_alsa_audio.c

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "../SDL_sysaudio.h"
4040
#include "SDL_alsa_audio.h"
41+
#include "../../core/linux/SDL_udev.h"
4142

4243
#if SDL_ALSA_DEBUG
4344
#define LOGDEBUG(...) SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "ALSA: " __VA_ARGS__)
@@ -1441,6 +1442,65 @@ static int SDLCALL ALSA_HotplugThread(void *arg)
14411442
}
14421443
#endif
14431444

1445+
#ifdef SDL_USE_LIBUDEV
1446+
1447+
static bool udev_initialized;
1448+
1449+
static void ALSA_udev_callback(SDL_UDEV_deviceevent udev_type, int udev_class, const char *devpath)
1450+
{
1451+
if (!devpath) {
1452+
return;
1453+
}
1454+
1455+
switch (udev_type) {
1456+
case SDL_UDEV_DEVICEADDED:
1457+
ALSA_HotplugIteration(NULL, NULL);
1458+
break;
1459+
1460+
case SDL_UDEV_DEVICEREMOVED:
1461+
ALSA_HotplugIteration(NULL, NULL);
1462+
break;
1463+
1464+
default:
1465+
break;
1466+
}
1467+
}
1468+
1469+
static bool ALSA_start_udev()
1470+
{
1471+
udev_initialized = SDL_UDEV_Init();
1472+
if (udev_initialized) {
1473+
// Set up the udev callback
1474+
if (!SDL_UDEV_AddCallback(ALSA_udev_callback)) {
1475+
SDL_UDEV_Quit();
1476+
udev_initialized = false;
1477+
}
1478+
}
1479+
return udev_initialized;
1480+
}
1481+
1482+
static void ALSA_stop_udev()
1483+
{
1484+
if (udev_initialized) {
1485+
SDL_UDEV_DelCallback(ALSA_udev_callback);
1486+
SDL_UDEV_Quit();
1487+
udev_initialized = false;
1488+
}
1489+
}
1490+
1491+
#else
1492+
1493+
static bool ALSA_start_udev()
1494+
{
1495+
return false;
1496+
}
1497+
1498+
static void ALSA_stop_udev()
1499+
{
1500+
}
1501+
1502+
#endif // SDL_USE_LIBUDEV
1503+
14441504
static void ALSA_DetectDevices(SDL_AudioDevice **default_playback, SDL_AudioDevice **default_recording)
14451505
{
14461506
ALSA_guess_device_prefix();
@@ -1456,11 +1516,13 @@ static void ALSA_DetectDevices(SDL_AudioDevice **default_playback, SDL_AudioDevi
14561516
*default_recording = SDL_AddAudioDevice(/*recording=*/true, "ALSA default recording device", NULL, (void *)&default_recording_handle);
14571517
}
14581518

1519+
if (!ALSA_start_udev()) {
14591520
#if SDL_ALSA_HOTPLUG_THREAD
1460-
SDL_SetAtomicInt(&ALSA_hotplug_shutdown, 0);
1461-
ALSA_hotplug_thread = SDL_CreateThread(ALSA_HotplugThread, "SDLHotplugALSA", NULL);
1462-
// if the thread doesn't spin, oh well, you just don't get further hotplug events.
1521+
SDL_SetAtomicInt(&ALSA_hotplug_shutdown, 0);
1522+
ALSA_hotplug_thread = SDL_CreateThread(ALSA_HotplugThread, "SDLHotplugALSA", NULL);
1523+
// if the thread doesn't spin, oh well, you just don't get further hotplug events.
14631524
#endif
1525+
}
14641526
}
14651527

14661528
static void ALSA_DeinitializeStart(void)
@@ -1475,6 +1537,7 @@ static void ALSA_DeinitializeStart(void)
14751537
ALSA_hotplug_thread = NULL;
14761538
}
14771539
#endif
1540+
ALSA_stop_udev();
14781541

14791542
// Shutting down! Clean up any data we've gathered.
14801543
for (dev = hotplug_devices; dev; dev = next) {

0 commit comments

Comments
 (0)