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