1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2021 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 SDL_AUDIO_DRIVER_NETBSD || SDL_AUDIO_DRIVER_OSS || SDL_AUDIO_DRIVER_SUNAUDIO |
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_stdinc.h" |
33 | #include "SDL_audiodev_c.h" |
34 | |
35 | #ifndef _PATH_DEV_DSP |
36 | #if defined(__NETBSD__) || defined(__OPENBSD__) |
37 | #define _PATH_DEV_DSP "/dev/audio" |
38 | #else |
39 | #define _PATH_DEV_DSP "/dev/dsp" |
40 | #endif |
41 | #endif |
42 | #ifndef _PATH_DEV_DSP24 |
43 | #define _PATH_DEV_DSP24 "/dev/sound/dsp" |
44 | #endif |
45 | #ifndef _PATH_DEV_AUDIO |
46 | #define _PATH_DEV_AUDIO "/dev/audio" |
47 | #endif |
48 | |
49 | static void |
50 | test_device(const int iscapture, const char *fname, int flags, int (*test) (int fd)) |
51 | { |
52 | struct stat sb; |
53 | if ((stat(fname, &sb) == 0) && (S_ISCHR(sb.st_mode))) { |
54 | const int audio_fd = open(fname, flags, 0); |
55 | if (audio_fd >= 0) { |
56 | const int okay = test(audio_fd); |
57 | close(audio_fd); |
58 | if (okay) { |
59 | static size_t dummyhandle = 0; |
60 | dummyhandle++; |
61 | SDL_assert(dummyhandle != 0); |
62 | |
63 | /* Note that spec is NULL; while we are opening the device |
64 | * endpoint here, the endpoint does not provide any mix format |
65 | * information, making this information inaccessible at |
66 | * enumeration time |
67 | */ |
68 | SDL_AddAudioDevice(iscapture, fname, NULL, (void *) dummyhandle); |
69 | } |
70 | } |
71 | } |
72 | } |
73 | |
74 | static int |
75 | test_stub(int fd) |
76 | { |
77 | return 1; |
78 | } |
79 | |
80 | static void |
81 | SDL_EnumUnixAudioDevices_Internal(const int iscapture, const int classic, int (*test)(int)) |
82 | { |
83 | const int flags = iscapture ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT; |
84 | const char *audiodev; |
85 | char audiopath[1024]; |
86 | |
87 | if (test == NULL) |
88 | test = test_stub; |
89 | |
90 | /* Figure out what our audio device is */ |
91 | if (((audiodev = SDL_getenv("SDL_PATH_DSP" )) == NULL) && |
92 | ((audiodev = SDL_getenv("AUDIODEV" )) == NULL)) { |
93 | if (classic) { |
94 | audiodev = _PATH_DEV_AUDIO; |
95 | } else { |
96 | struct stat sb; |
97 | |
98 | /* Added support for /dev/sound/\* in Linux 2.4 */ |
99 | if (((stat("/dev/sound" , &sb) == 0) && S_ISDIR(sb.st_mode)) |
100 | && ((stat(_PATH_DEV_DSP24, &sb) == 0) |
101 | && S_ISCHR(sb.st_mode))) { |
102 | audiodev = _PATH_DEV_DSP24; |
103 | } else { |
104 | audiodev = _PATH_DEV_DSP; |
105 | } |
106 | } |
107 | } |
108 | test_device(iscapture, audiodev, flags, test); |
109 | |
110 | if (SDL_strlen(audiodev) < (sizeof(audiopath) - 3)) { |
111 | int instance = 0; |
112 | while (instance <= 64) { |
113 | SDL_snprintf(audiopath, SDL_arraysize(audiopath), |
114 | "%s%d" , audiodev, instance); |
115 | instance++; |
116 | test_device(iscapture, audiopath, flags, test); |
117 | } |
118 | } |
119 | } |
120 | |
121 | void |
122 | SDL_EnumUnixAudioDevices(const int classic, int (*test)(int)) |
123 | { |
124 | SDL_EnumUnixAudioDevices_Internal(SDL_TRUE, classic, test); |
125 | SDL_EnumUnixAudioDevices_Internal(SDL_FALSE, classic, test); |
126 | } |
127 | |
128 | #endif /* Audio driver selection */ |
129 | |
130 | /* vi: set ts=4 sw=4 expandtab: */ |
131 | |