1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
4 | |
5 | This software is provided 'as-is', without any express or implied |
6 | warranty. In no event will the authors be held liable for any damages |
7 | arising from the use of this software. |
8 | |
9 | Permission is granted to anyone to use this software for any purpose, |
10 | including commercial applications, and to alter it and redistribute it |
11 | freely, subject to the following restrictions: |
12 | |
13 | 1. The origin of this software must not be misrepresented; you must not |
14 | claim that you wrote the original software. If you use this software |
15 | in a product, an acknowledgment in the product documentation would be |
16 | appreciated but is not required. |
17 | 2. Altered source versions must be plainly marked as such, and must not be |
18 | misrepresented as being the original software. |
19 | 3. This notice may not be removed or altered from any source distribution. |
20 | */ |
21 | #include "SDL_internal.h" |
22 | |
23 | // Get the name of the audio device we use for output |
24 | |
25 | #if defined(SDL_AUDIO_DRIVER_NETBSD) || defined(SDL_AUDIO_DRIVER_OSS) |
26 | |
27 | #include <fcntl.h> |
28 | #include <sys/types.h> |
29 | #include <sys/stat.h> |
30 | #include <unistd.h> // For close() |
31 | |
32 | #include "SDL_audiodev_c.h" |
33 | |
34 | #ifndef SDL_PATH_DEV_DSP |
35 | #if defined(SDL_PLATFORM_NETBSD) || defined(SDL_PLATFORM_OPENBSD) |
36 | #define SDL_PATH_DEV_DSP "/dev/audio" |
37 | #else |
38 | #define SDL_PATH_DEV_DSP "/dev/dsp" |
39 | #endif |
40 | #endif |
41 | #ifndef SDL_PATH_DEV_DSP24 |
42 | #define SDL_PATH_DEV_DSP24 "/dev/sound/dsp" |
43 | #endif |
44 | #ifndef SDL_PATH_DEV_AUDIO |
45 | #define SDL_PATH_DEV_AUDIO "/dev/audio" |
46 | #endif |
47 | |
48 | static void test_device(const bool recording, const char *fname, int flags, bool (*test)(int fd)) |
49 | { |
50 | struct stat sb; |
51 | const int audio_fd = open(fname, flags | O_CLOEXEC, 0); |
52 | if (audio_fd >= 0) { |
53 | if ((fstat(audio_fd, &sb) == 0) && (S_ISCHR(sb.st_mode))) { |
54 | const bool okay = test(audio_fd); |
55 | close(audio_fd); |
56 | if (okay) { |
57 | static size_t dummyhandle = 0; |
58 | dummyhandle++; |
59 | SDL_assert(dummyhandle != 0); |
60 | |
61 | /* Note that spec is NULL; while we are opening the device |
62 | * endpoint here, the endpoint does not provide any mix format |
63 | * information, making this information inaccessible at |
64 | * enumeration time |
65 | */ |
66 | SDL_AddAudioDevice(recording, fname, NULL, (void *)(uintptr_t)dummyhandle); |
67 | } |
68 | } else { |
69 | close(audio_fd); |
70 | } |
71 | } |
72 | } |
73 | |
74 | static bool test_stub(int fd) |
75 | { |
76 | return true; |
77 | } |
78 | |
79 | static void SDL_EnumUnixAudioDevices_Internal(const bool recording, const bool classic, bool (*test)(int)) |
80 | { |
81 | const int flags = recording ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT; |
82 | const char *audiodev; |
83 | char audiopath[1024]; |
84 | |
85 | if (!test) { |
86 | test = test_stub; |
87 | } |
88 | |
89 | // Figure out what our audio device is |
90 | audiodev = SDL_getenv("AUDIODEV" ); |
91 | if (!audiodev) { |
92 | if (classic) { |
93 | audiodev = SDL_PATH_DEV_AUDIO; |
94 | } else { |
95 | struct stat sb; |
96 | |
97 | // Added support for /dev/sound/\* in Linux 2.4 |
98 | if (((stat("/dev/sound" , &sb) == 0) && S_ISDIR(sb.st_mode)) && ((stat(SDL_PATH_DEV_DSP24, &sb) == 0) && S_ISCHR(sb.st_mode))) { |
99 | audiodev = SDL_PATH_DEV_DSP24; |
100 | } else { |
101 | audiodev = SDL_PATH_DEV_DSP; |
102 | } |
103 | } |
104 | } |
105 | test_device(recording, audiodev, flags, test); |
106 | |
107 | if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) { |
108 | int instance = 0; |
109 | while (instance <= 64) { |
110 | (void)SDL_snprintf(audiopath, SDL_arraysize(audiopath), |
111 | "%s%d" , audiodev, instance); |
112 | instance++; |
113 | test_device(recording, audiopath, flags, test); |
114 | } |
115 | } |
116 | } |
117 | |
118 | void SDL_EnumUnixAudioDevices(const bool classic, bool (*test)(int)) |
119 | { |
120 | SDL_EnumUnixAudioDevices_Internal(true, classic, test); |
121 | SDL_EnumUnixAudioDevices_Internal(false, classic, test); |
122 | } |
123 | |
124 | #endif // Audio device selection |
125 | |