]> code.delx.au - pulseaudio/blob - src/modules/module-detect.c
Detect support for Windows' waveout.
[pulseaudio] / src / modules / module-detect.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <stdlib.h>
33
34 #include <polypcore/module.h>
35 #include <polypcore/modargs.h>
36 #include <polypcore/xmalloc.h>
37 #include <polypcore/log.h>
38
39 #include "module-detect-symdef.h"
40
41 PA_MODULE_AUTHOR("Lennart Poettering")
42 PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers")
43 PA_MODULE_VERSION(PACKAGE_VERSION)
44 PA_MODULE_USAGE("just-one=<boolean>")
45
46 static const char *endswith(const char *haystack, const char *needle) {
47 size_t l, m;
48 const char *p;
49
50 if ((l = strlen(haystack)) < (m = strlen(needle)))
51 return NULL;
52
53 if (strcmp(p = haystack + l - m, needle))
54 return NULL;
55
56 return p;
57 }
58
59 #ifdef HAVE_ALSA
60 static int detect_alsa(pa_core *c, int just_one) {
61 FILE *f;
62 int n = 0, n_sink = 0, n_source = 0;
63
64 if (!(f = fopen("/proc/asound/devices", "r"))) {
65
66 if (errno != ENOENT)
67 pa_log_error(__FILE__": open(\"/proc/asound/devices\") failed: %s\n", strerror(errno));
68
69 return -1;
70 }
71
72 while (!feof(f)) {
73 char line[64], args[64];
74 unsigned device, subdevice;
75 int is_sink;
76
77 if (!fgets(line, sizeof(line), f))
78 break;
79
80 line[strcspn(line, "\r\n")] = 0;
81
82 if (endswith(line, "digital audio playback"))
83 is_sink = 1;
84 else if (endswith(line, "digital audio capture"))
85 is_sink = 0;
86 else
87 continue;
88
89 if (just_one && is_sink && n_sink >= 1)
90 continue;
91
92 if (just_one && !is_sink && n_source >= 1)
93 continue;
94
95 if (sscanf(line, " %*i: [%u- %u]: ", &device, &subdevice) != 2)
96 continue;
97
98 /* Only one sink per device */
99 if (subdevice != 0)
100 continue;
101
102 snprintf(args, sizeof(args), "device=hw:%u,0", device);
103 if (!pa_module_load(c, is_sink ? "module-alsa-sink" : "module-alsa-source", args))
104 continue;
105
106 n++;
107
108 if (is_sink)
109 n_sink++;
110 else
111 n_source++;
112 }
113
114 fclose(f);
115
116 return n;
117 }
118 #endif
119
120 #ifdef HAVE_OSS
121 static int detect_oss(pa_core *c, int just_one) {
122 FILE *f;
123 int n = 0, b = 0;
124
125 if (!(f = fopen("/dev/sndstat", "r")) &&
126 !(f = fopen("/proc/sndstat", "r")) &&
127 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
128
129 if (errno != ENOENT)
130 pa_log_error(__FILE__": failed to open OSS sndstat device: %s\n", strerror(errno));
131
132 return -1;
133 }
134
135 while (!feof(f)) {
136 char line[64], args[64];
137 unsigned device;
138
139 if (!fgets(line, sizeof(line), f))
140 break;
141
142 line[strcspn(line, "\r\n")] = 0;
143
144 if (!b) {
145 b = strcmp(line, "Audio devices:") == 0;
146 continue;
147 }
148
149 if (line[0] == 0)
150 break;
151
152 if (sscanf(line, "%u: ", &device) != 1)
153 continue;
154
155 if (device == 0)
156 snprintf(args, sizeof(args), "device=/dev/dsp");
157 else
158 snprintf(args, sizeof(args), "device=/dev/dsp%u", device);
159
160 if (!pa_module_load(c, "module-oss", args))
161 continue;
162
163 n++;
164
165 if (just_one)
166 break;
167 }
168
169 fclose(f);
170 return n;
171 }
172 #endif
173
174 #ifdef HAVE_SOLARIS
175 static int detect_solaris(pa_core *c, int just_one) {
176 struct stat s;
177 const char *dev;
178 char args[64];
179
180 dev = getenv("AUDIODEV");
181 if (!dev)
182 dev = "/dev/audio";
183
184 if (stat(dev, &s) < 0) {
185 if (errno != ENOENT)
186 pa_log_error(__FILE__": failed to open device %s: %s\n", dev, strerror(errno));
187 return -1;
188 }
189
190 if (!S_ISCHR(s))
191 return 0;
192
193 snprintf(args, sizeof(args), "device=%s", dev);
194
195 if (!pa_module_load(c, "module-solaris", args))
196 return 0;
197
198 return 1;
199 }
200 #endif
201
202 #ifdef OS_IS_WIN32
203 static int detect_waveout(pa_core *c, int just_one) {
204 /*
205 * FIXME: No point in enumerating devices until the plugin supports
206 * selecting anything but the first.
207 */
208 if (!pa_module_load(c, "module-waveout", ""))
209 return 0;
210
211 return 1;
212 }
213 #endif
214
215 int pa__init(pa_core *c, pa_module*m) {
216 int just_one = 0, n = 0;
217 pa_modargs *ma;
218
219 static const char* const valid_modargs[] = {
220 "just-one",
221 NULL
222 };
223
224 assert(c);
225 assert(m);
226
227 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
228 pa_log(__FILE__": Failed to parse module arguments\n");
229 goto fail;
230 }
231
232 if (pa_modargs_get_value_boolean(ma, "just-one", &just_one) < 0) {
233 pa_log(__FILE__": just_one= expects a boolean argument.\n");
234 goto fail;
235 }
236
237 #if HAVE_ALSA
238 if ((n = detect_alsa(c, just_one)) <= 0)
239 #endif
240 #if HAVE_OSS
241 if ((n = detect_oss(c, just_one)) <= 0)
242 #endif
243 #if HAVE_SOLARIS
244 if ((n = detect_solaris(c, just_one)) <= 0)
245 #endif
246 #if OS_IS_WIN32
247 if ((n = detect_waveout(c, just_one)) <= 0)
248 #endif
249 {
250 pa_log_warn(__FILE__": failed to detect any sound hardware.\n");
251 goto fail;
252 }
253
254 pa_log_info(__FILE__": loaded %i modules.\n", n);
255
256 /* We were successful and can unload ourselves now. */
257 pa_module_unload_request(m);
258
259 pa_modargs_free(ma);
260
261 return 0;
262
263 fail:
264 if (ma)
265 pa_modargs_free(ma);
266
267 return -1;
268 }
269
270
271 void pa__done(pa_core *c, pa_module*m) {
272 /* NOP */
273 }
274