]> code.delx.au - pulseaudio/blob - src/modules/module-detect.c
fix module-detect on FreeBSD (patch from Diego "Flameeyes" Pettenó)
[pulseaudio] / src / modules / module-detect.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 <pulse/xmalloc.h>
37
38 #include <pulsecore/core-error.h>
39 #include <pulsecore/module.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/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", 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 || strcmp(line, "Installed devices:") == 0;
139 continue;
140 }
141
142 if (line[0] == 0)
143 break;
144
145 if (sscanf(line, "%u: ", &device) == 1) {
146 if (device == 0)
147 snprintf(args, sizeof(args), "device=/dev/dsp");
148 else
149 snprintf(args, sizeof(args), "device=/dev/dsp%u", device);
150
151 if (!pa_module_load(c, "module-oss", args))
152 continue;
153
154 } else if (sscanf(line, "pcm%u: ", &device) == 1) {
155 /* FreeBSD support, the devices are named /dev/dsp0.0, dsp0.1 and so on */
156 snprintf(args, sizeof(args), "device=/dev/dsp%u.0", device);
157
158 if (!pa_module_load(c, "module-oss", args))
159 continue;
160 }
161
162 n++;
163
164 if (just_one)
165 break;
166 }
167
168 fclose(f);
169 return n;
170 }
171 #endif
172
173 #ifdef HAVE_SOLARIS
174 static int detect_solaris(pa_core *c, int just_one) {
175 struct stat s;
176 const char *dev;
177 char args[64];
178
179 dev = getenv("AUDIODEV");
180 if (!dev)
181 dev = "/dev/audio";
182
183 if (stat(dev, &s) < 0) {
184 if (errno != ENOENT)
185 pa_log_error(__FILE__": failed to open device %s: %s", dev, pa_cstrerror(errno));
186 return -1;
187 }
188
189 if (!S_ISCHR(s.st_mode))
190 return 0;
191
192 snprintf(args, sizeof(args), "device=%s", dev);
193
194 if (!pa_module_load(c, "module-solaris", args))
195 return 0;
196
197 return 1;
198 }
199 #endif
200
201 #ifdef OS_IS_WIN32
202 static int detect_waveout(pa_core *c, int just_one) {
203 /*
204 * FIXME: No point in enumerating devices until the plugin supports
205 * selecting anything but the first.
206 */
207 if (!pa_module_load(c, "module-waveout", ""))
208 return 0;
209
210 return 1;
211 }
212 #endif
213
214 int pa__init(pa_core *c, pa_module*m) {
215 int just_one = 0, n = 0;
216 pa_modargs *ma;
217
218 static const char* const valid_modargs[] = {
219 "just-one",
220 NULL
221 };
222
223 assert(c);
224 assert(m);
225
226 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
227 pa_log(__FILE__": Failed to parse module arguments");
228 goto fail;
229 }
230
231 if (pa_modargs_get_value_boolean(ma, "just-one", &just_one) < 0) {
232 pa_log(__FILE__": just_one= expects a boolean argument.");
233 goto fail;
234 }
235
236 #if HAVE_ALSA
237 if ((n = detect_alsa(c, just_one)) <= 0)
238 #endif
239 #if HAVE_OSS
240 if ((n = detect_oss(c, just_one)) <= 0)
241 #endif
242 #if HAVE_SOLARIS
243 if ((n = detect_solaris(c, just_one)) <= 0)
244 #endif
245 #if OS_IS_WIN32
246 if ((n = detect_waveout(c, just_one)) <= 0)
247 #endif
248 {
249 pa_log_warn(__FILE__": failed to detect any sound hardware.");
250 goto fail;
251 }
252
253 pa_log_info(__FILE__": loaded %i modules.", n);
254
255 /* We were successful and can unload ourselves now. */
256 pa_module_unload_request(m);
257
258 pa_modargs_free(ma);
259
260 return 0;
261
262 fail:
263 if (ma)
264 pa_modargs_free(ma);
265
266 return -1;
267 }
268
269
270 void pa__done(PA_GCC_UNUSED pa_core *c, PA_GCC_UNUSED pa_module*m) {
271 /* NOP */
272 }
273