From 503948bfdade6e6524bfe262b6a69e152e165cec Mon Sep 17 00:00:00 2001 From: dkeruza Date: Fri, 1 Mar 2024 09:37:56 -0500 Subject: [PATCH] Soundcard pick --- Makefile | 2 +- src/lindbergh/hook.c | 15 ++++++++++ src/lindbergh/soundcard.c | 63 +++++++++++++++++++++++++++++++++++++++ src/lindbergh/soundcard.h | 1 + 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/lindbergh/soundcard.c create mode 100644 src/lindbergh/soundcard.h diff --git a/Makefile b/Makefile index 74aee06..1bb6c7e 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ CC=gcc -m32 CFLAGS = -g -O0 -fPIC -m32 -Wall -Werror -Wno-unused-variable -Wno-unused-function LD = g++ -m32 -LDFLAGS = -Wl,-z,defs -rdynamic -static-libstdc++ -static-libgcc -lc -ldl -lGL -lglut -lX11 -lm -lpthread -shared -nostdlib +LDFLAGS = -Wl,-z,defs -rdynamic -static-libstdc++ -static-libgcc -lc -ldl -lGL -lglut -lX11 -lm -lpthread -shared -nostdlib -lasound BUILD = build diff --git a/src/lindbergh/hook.c b/src/lindbergh/hook.c index 9d418e9..6fa42e9 100644 --- a/src/lindbergh/hook.c +++ b/src/lindbergh/hook.c @@ -30,6 +30,15 @@ #include "patch.h" #include "pcidata.h" #include "input.h" +#include "soundcard.h" + +#include "alsa/global.h" +#include "alsa/input.h" +#include "alsa/output.h" +#include "alsa/conf.h" +#include "alsa/pcm.h" +#include "alsa/control.h" +#include "alsa/error.h" #define HOOK_FILE_NAME "/dev/zero" @@ -743,3 +752,9 @@ int unsetenv(const char *name) return _unsetenv(name); } + +int snd_pcm_open(snd_pcm_t **pcmp, const char *name, snd_pcm_stream_t stream, int mode) +{ + int (*_snd_pcm_open)(snd_pcm_t **pcmp, const char *name, snd_pcm_stream_t stream, int mode) = dlsym(RTLD_NEXT, "snd_pcm_open"); + return _snd_pcm_open(pcmp, get_sndcard(), stream, mode); +} diff --git a/src/lindbergh/soundcard.c b/src/lindbergh/soundcard.c new file mode 100644 index 0000000..043eacd --- /dev/null +++ b/src/lindbergh/soundcard.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include "alsa/global.h" +#include "alsa/input.h" +#include "alsa/output.h" +#include "alsa/conf.h" +#include "alsa/pcm.h" +#include "alsa/control.h" +#include "alsa/error.h" + +const char* get_sndcard() +{ + int card = -1; + int err; + snd_ctl_t *handle; + snd_ctl_card_info_t *info; + static char hwdevice[15]; + + while (snd_card_next(&card) == 0 && card >= 0) + { + char cardname[32]; + sprintf(cardname, "hw:%d", card); + + if ((err = snd_ctl_open(&handle, cardname, 0)) < 0) + { + fprintf(stderr, "Cannot open control for card %d: %s\n", card, snd_strerror(err)); + continue; + } + + snd_ctl_card_info_alloca(&info); + if ((err = snd_ctl_card_info(handle, info)) < 0) + { + fprintf(stderr, "Cannot get card info for card %d: %s\n", card, snd_strerror(err)); + snd_ctl_close(handle); + continue; + } + + // List PCM devices + int dev = -1; + snd_pcm_info_t *pcm_info; + snd_pcm_info_alloca(&pcm_info); + while (snd_ctl_pcm_next_device(handle, &dev) == 0 && dev >= 0) + { + snd_pcm_info_set_device(pcm_info, dev); + snd_pcm_info_set_subdevice(pcm_info, 0); + snd_pcm_info_set_stream(pcm_info, SND_PCM_STREAM_PLAYBACK); + if (snd_ctl_pcm_info(handle, pcm_info) >= 0) + { + if(strstr(snd_pcm_info_get_name(pcm_info),"HDMI") == NULL) + { + snd_ctl_close(handle); + sprintf(hwdevice, "plughw:%d,0", card); + return hwdevice; + } + } + } + snd_ctl_close(handle); + } + return "plughw:0,0"; +} diff --git a/src/lindbergh/soundcard.h b/src/lindbergh/soundcard.h new file mode 100644 index 0000000..80c6f32 --- /dev/null +++ b/src/lindbergh/soundcard.h @@ -0,0 +1 @@ +const char* get_sndcard(); \ No newline at end of file