]> code.delx.au - pulseaudio/blob - src/modules/module-detect.c
* split pa_cstrerror() into its own file polypcore/core-error.[ch]
[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 #include <sys/types.h>
34 #include <sys/stat.h>
35
36 #include <polyp/xmalloc.h>
37
38 #include <polypcore/core-error.h>
39 #include <polypcore/module.h>
40 #include <polypcore/modargs.h>
41 #include <polypcore/log.h>
42 #include <polypcore/core-util.h>
43
44 #include "module-detect-symdef.h"
45
46 PA_MODULE_AUTHOR("Lennart Poettering")
47 PA_MODULE_DESCRIPTION("Detect available audio hardware and load matching drivers")
48 PA_MODULE_VERSION(PACKAGE_VERSION)
49 PA_MODULE_USAGE("just-one=<boolean>")
50
51 #ifdef HAVE_ALSA
52
53 static int detect_alsa(pa_core *c, int just_one) {
54 FILE *f;
55 int n = 0, n_sink = 0, n_source = 0;
56
57 if (!(f = fopen("/proc/asound/devices", "r"))) {
58
59 if (errno != ENOENT)
60 pa_log_error(__FILE__": open(\"/proc/asound/devices\") failed: %s", pa_cstrerror(errno));
61
62 return -1;
63 }
64
65 while (!feof(f)) {
66 char line[64], args[64];
67 unsigned device, subdevice;
68 int is_sink;
69
70 if (!fgets(line, sizeof(line), f))
71 break;
72
73 line[strcspn(line, "\r\n")] = 0;
74
75 if (pa_endswith(line, "digital audio playback"))
76 is_sink = 1;
77 else if (pa_endswith(line, "digital audio capture"))
78 is_sink = 0;
79 else
80 continue;
81
82 if (just_one && is_sink && n_sink >= 1)
83 continue;
84
85 if (just_one && !is_sink && n_source >= 1)
86 continue;
87
88 if (sscanf(line, " %*i: [%u- %u]: ", &device, &subdevice) != 2)
89 continue;
90
91 /* Only one sink per device */
92 if (subdevice != 0)
93 continue;
94
95 snprintf(args, sizeof(args), "device=hw:%u,0", device);
96 if (!pa_module_load(c, is_sink ? "module-alsa-sink" : "module-alsa-source", args))
97 continue;
98
99 n++;
100
101 if (is_sink)
102 n_sink++;
103 else
104 n_source++;
105 }
106
107 fclose(f);
108
109 return n;
110 }
111 #endif
112
113 #ifdef HAVE_OSS
114 static int detect_oss(pa_core *c, int just_one) {
115 FILE *f;
116 int n = 0, b = 0;
117
118 if (!(f = fopen("/dev/sndstat", "r")) &&
119 !(f = fopen("/proc/sndstat", "r")) &&
120 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
121
122 if (errno != ENOENT)
123 pa_log_error(__FILE__": failed to open OSS sndstat device: %s", pa_cstrerror(errno));
124
125 return -1;
126 }
127
128 while (!feof(f)) {
129 char line[64], args[64];
130 unsigned device;
131
132 if (!fgets(line, sizeof(line), f))
133 break;
134
135 line[strcspn(line, "\r\n")] = 0;
136
137 if (!b) {
138 b = strcmp(line, "Audio devices:") == 0;
139 continue;
140 }
141
142 if (line[0] == 0)
143 break;
144
145 if (sscanf(line, "%u: ", &device) != 1)
146 continue;
147
148 if (device == 0)
149 snprintf(args, sizeof(args), "device=/dev/dsp");
150 else
151 snprintf(args, sizeof(args), "device=/dev/dsp%u", device);
152
153 if (!pa_module_load(c, "module-oss", args))
154 continue;
155
156 n++;
157
158 if (just_one)
159 break;
160 }
161
162 fclose(f);
163 return n;
164 }
165 #endif
166
167 #ifdef HAVE_SOLARIS
168 static int detect_solaris(pa_core *c, int just_one) {
169 struct stat s;
170 const char *dev;
171 char args[64];
172
173 dev = getenv("AUDIODEV");
174 if (!dev)
175 dev = "/dev/audio";
176
177 if (stat(dev, &s) < 0) {
178 if (errno != ENOENT)
179 pa_log_error(__FILE__": failed to open device %s: %s", dev, pa_cstrerror(errno));
180 return -1;
181 }
182
183 if (!S_ISCHR(s.st_mode))
184 return 0;
185
186 snprintf(args, sizeof(args), "device=%s", dev);
187
188 if (!pa_module_load(c, "module-solaris", args))
189 return 0;
190
191 return 1;
192 }
193 #endif
194
195 #ifdef OS_IS_WIN32
196 static int detect_waveout(pa_core *c, int just_one) {
197 /*
198 * FIXME: No point in enumerating devices until the plugin supports
199 * selecting anything but the first.
200 */
201 if (!pa_module_load(c, "module-waveout", ""))
202 return 0;
203
204 return 1;
205 }
206 #endif
207
208 int pa__init(pa_core *c, pa_module*m) {
209 int just_one = 0, n = 0;
210 pa_modargs *ma;
211
212 static const char* const valid_modargs[] = {
213 "just-one",
214 NULL
215 };
216
217 assert(c);
218 assert(m);
219
220 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
221 pa_log(__FILE__": Failed to parse module arguments");
222 goto fail;
223 }
224
225 if (pa_modargs_get_value_boolean(ma, "just-one", &just_one) < 0) {
226 pa_log(__FILE__": just_one= expects a boolean argument.");
227 goto fail;
228 }
229
230 #if HAVE_ALSA
231 if ((n = detect_alsa(c, just_one)) <= 0)
232 #endif
233 #if HAVE_OSS
234 if ((n = detect_oss(c, just_one)) <= 0)
235 #endif
236 #if HAVE_SOLARIS
237 if ((n = detect_solaris(c, just_one)) <= 0)
238 #endif
239 #if OS_IS_WIN32
240 if ((n = detect_waveout(c, just_one)) <= 0)
241 #endif
242 {
243 pa_log_warn(__FILE__": failed to detect any sound hardware.");
244 goto fail;
245 }
246
247 pa_log_info(__FILE__": loaded %i modules.", n);
248
249 /* We were successful and can unload ourselves now. */
250 pa_module_unload_request(m);
251
252 pa_modargs_free(ma);
253
254 return 0;
255
256 fail:
257 if (ma)
258 pa_modargs_free(ma);
259
260 return -1;
261 }
262
263
264 void pa__done(PA_GCC_UNUSED pa_core *c, PA_GCC_UNUSED pa_module*m) {
265 /* NOP */
266 }
267