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