4 This file is part of polypaudio.
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.
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.
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
33 #include <sys/types.h>
36 #include <polyp/xmalloc.h>
38 #include <polypcore/module.h>
39 #include <polypcore/modargs.h>
40 #include <polypcore/log.h>
41 #include <polypcore/util.h>
43 #include "module-detect-symdef.h"
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>")
52 static int detect_alsa(pa_core *c, int just_one) {
54 int n = 0, n_sink = 0, n_source = 0;
56 if (!(f = fopen("/proc/asound/devices", "r"))) {
59 pa_log_error(__FILE__": open(\"/proc/asound/devices\") failed: %s", strerror(errno));
65 char line[64], args[64];
66 unsigned device, subdevice;
69 if (!fgets(line, sizeof(line), f))
72 line[strcspn(line, "\r\n")] = 0;
74 if (pa_endswith(line, "digital audio playback"))
76 else if (pa_endswith(line, "digital audio capture"))
81 if (just_one && is_sink && n_sink >= 1)
84 if (just_one && !is_sink && n_source >= 1)
87 if (sscanf(line, " %*i: [%u- %u]: ", &device, &subdevice) != 2)
90 /* Only one sink per device */
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))
113 static int detect_oss(pa_core *c, int just_one) {
117 if (!(f = fopen("/dev/sndstat", "r")) &&
118 !(f = fopen("/proc/sndstat", "r")) &&
119 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
122 pa_log_error(__FILE__": failed to open OSS sndstat device: %s", strerror(errno));
128 char line[64], args[64];
131 if (!fgets(line, sizeof(line), f))
134 line[strcspn(line, "\r\n")] = 0;
137 b = strcmp(line, "Audio devices:") == 0;
144 if (sscanf(line, "%u: ", &device) != 1)
148 snprintf(args, sizeof(args), "device=/dev/dsp");
150 snprintf(args, sizeof(args), "device=/dev/dsp%u", device);
152 if (!pa_module_load(c, "module-oss", args))
167 static int detect_solaris(pa_core *c, int just_one) {
172 dev = getenv("AUDIODEV");
176 if (stat(dev, &s) < 0) {
178 pa_log_error(__FILE__": failed to open device %s: %s", dev, strerror(errno));
182 if (!S_ISCHR(s.st_mode))
185 snprintf(args, sizeof(args), "device=%s", dev);
187 if (!pa_module_load(c, "module-solaris", args))
195 static int detect_waveout(pa_core *c, int just_one) {
197 * FIXME: No point in enumerating devices until the plugin supports
198 * selecting anything but the first.
200 if (!pa_module_load(c, "module-waveout", ""))
207 int pa__init(pa_core *c, pa_module*m) {
208 int just_one = 0, n = 0;
211 static const char* const valid_modargs[] = {
219 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
220 pa_log(__FILE__": Failed to parse module arguments");
224 if (pa_modargs_get_value_boolean(ma, "just-one", &just_one) < 0) {
225 pa_log(__FILE__": just_one= expects a boolean argument.");
230 if ((n = detect_alsa(c, just_one)) <= 0)
233 if ((n = detect_oss(c, just_one)) <= 0)
236 if ((n = detect_solaris(c, just_one)) <= 0)
239 if ((n = detect_waveout(c, just_one)) <= 0)
242 pa_log_warn(__FILE__": failed to detect any sound hardware.");
246 pa_log_info(__FILE__": loaded %i modules.", n);
248 /* We were successful and can unload ourselves now. */
249 pa_module_unload_request(m);
263 void pa__done(PA_GCC_UNUSED pa_core *c, PA_GCC_UNUSED pa_module*m) {