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
36 #include "module-detect-symdef.h"
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>")
45 static const char *endswith(const char *haystack, const char *needle) {
49 if ((l = strlen(haystack)) < (m = strlen(needle)))
52 if (strcmp(p = haystack + l - m, needle))
59 static int detect_alsa(pa_core *c, int just_one) {
61 int n = 0, n_sink = 0, n_source = 0;
63 if (!(f = fopen("/proc/asound/devices", "r"))) {
66 pa_log_error(__FILE__": open(\"/proc/asound/devices\") failed: %s\n", strerror(errno));
72 char line[64], args[64];
73 unsigned device, subdevice;
76 if (!fgets(line, sizeof(line), f))
79 line[strcspn(line, "\r\n")] = 0;
81 if (endswith(line, "digital audio playback"))
83 else if (endswith(line, "digital audio capture"))
88 if (just_one && is_sink && n_sink >= 1)
91 if (just_one && !is_sink && n_source >= 1)
94 if (sscanf(line, " %*i: [%u- %u]: ", &device, &subdevice) != 2)
97 /* Only one sink per device */
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))
120 static int detect_oss(pa_core *c, int just_one) {
124 if (!(f = fopen("/dev/sndstat", "r")) &&
125 !(f = fopen("/proc/sndstat", "r")) &&
126 !(f = fopen("/proc/asound/oss/sndstat", "r"))) {
129 pa_log_error(__FILE__": failed to open OSS sndstat device: %s\n", strerror(errno));
135 char line[64], args[64];
138 if (!fgets(line, sizeof(line), f))
141 line[strcspn(line, "\r\n")] = 0;
144 b = strcmp(line, "Audio devices:") == 0;
151 if (sscanf(line, "%u: ", &device) != 1)
155 snprintf(args, sizeof(args), "device=/dev/dsp");
157 snprintf(args, sizeof(args), "device=/dev/dsp%u", device);
159 if (!pa_module_load(c, "module-oss", args))
173 int pa__init(pa_core *c, pa_module*m) {
174 int just_one = 0, n = 0;
177 static const char* const valid_modargs[] = {
185 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
186 pa_log(__FILE__": Failed to parse module arguments\n");
190 if (pa_modargs_get_value_boolean(ma, "just-one", &just_one) < 0) {
191 pa_log(__FILE__": just_one= expects a boolean argument.\n");
196 if ((n = detect_alsa(c, just_one)) <= 0)
199 if ((n = detect_oss(c, just_one)) <= 0)
202 pa_log_warn(__FILE__": failed to detect any sound hardware.\n");
206 pa_log_info(__FILE__": loaded %i modules.\n", n);
208 /* We were successful and can unload ourselves now. */
209 pa_module_unload_request(m);
223 void pa__done(pa_core *c, pa_module*m) {