default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / modules / udev-util.c
blob356f7736320848cc360ee16509521516d80bdeae
1 /***
2 This file is part of PulseAudio.
4 Copyright 2009 Lennart Poettering
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
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License 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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <libudev.h>
28 #include <pulse/xmalloc.h>
29 #include <pulse/proplist.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/core-util.h>
34 #include "udev-util.h"
36 static int read_id(struct udev_device *d, const char *n) {
37 const char *v;
38 unsigned u;
40 pa_assert(d);
41 pa_assert(n);
43 if (!(v = udev_device_get_property_value(d, n)))
44 return -1;
46 if (pa_startswith(v, "0x"))
47 v += 2;
49 if (!*v)
50 return -1;
52 if (sscanf(v, "%04x", &u) != 1)
53 return -1;
55 if (u > 0xFFFFU)
56 return -1;
58 return u;
61 static int dehex(char x) {
62 if (x >= '0' && x <= '9')
63 return x - '0';
65 if (x >= 'A' && x <= 'F')
66 return x - 'A' + 10;
68 if (x >= 'a' && x <= 'f')
69 return x - 'a' + 10;
71 return -1;
74 static void proplist_sets_unescape(pa_proplist *p, const char *prop, const char *s) {
75 const char *f;
76 char *t, *r;
77 int c = 0;
79 enum {
80 TEXT,
81 BACKSLASH,
82 EX,
83 FIRST
84 } state = TEXT;
86 /* The resulting string is definitely shorter than the source string */
87 r = pa_xnew(char, strlen(s)+1);
89 for (f = s, t = r; *f; f++) {
91 switch (state) {
93 case TEXT:
94 if (*f == '\\')
95 state = BACKSLASH;
96 else
97 *(t++) = *f;
98 break;
100 case BACKSLASH:
101 if (*f == 'x')
102 state = EX;
103 else {
104 *(t++) = '\\';
105 *(t++) = *f;
106 state = TEXT;
108 break;
110 case EX:
111 c = dehex(*f);
113 if (c < 0) {
114 *(t++) = '\\';
115 *(t++) = 'x';
116 *(t++) = *f;
117 state = TEXT;
118 } else
119 state = FIRST;
121 break;
123 case FIRST: {
124 int d = dehex(*f);
126 if (d < 0) {
127 *(t++) = '\\';
128 *(t++) = 'x';
129 *(t++) = *(f-1);
130 *(t++) = *f;
131 } else
132 *(t++) = (char) (c << 4) | d;
134 state = TEXT;
135 break;
140 switch (state) {
142 case TEXT:
143 break;
145 case BACKSLASH:
146 *(t++) = '\\';
147 break;
149 case EX:
150 *(t++) = '\\';
151 *(t++) = 'x';
152 break;
154 case FIRST:
155 *(t++) = '\\';
156 *(t++) = 'x';
157 *(t++) = *(f-1);
158 break;
161 *t = 0;
163 pa_proplist_sets(p, prop, r);
164 pa_xfree(r);
167 int pa_udev_get_info(int card_idx, pa_proplist *p) {
168 int r = -1;
169 struct udev *udev;
170 struct udev_device *card = NULL;
171 char *t;
172 const char *v;
173 int id;
175 pa_assert(p);
176 pa_assert(card_idx >= 0);
178 if (!(udev = udev_new())) {
179 pa_log_error("Failed to allocate udev context.");
180 goto finish;
183 t = pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev), card_idx);
184 card = udev_device_new_from_syspath(udev, t);
185 pa_xfree(t);
187 if (!card) {
188 pa_log_error("Failed to get card object.");
189 goto finish;
192 if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS_PATH))
193 if (((v = udev_device_get_property_value(card, "ID_PATH")) && *v) ||
194 (v = udev_device_get_devpath(card)))
195 pa_proplist_sets(p, PA_PROP_DEVICE_BUS_PATH, v);
197 if (!pa_proplist_contains(p, "sysfs.path"))
198 if ((v = udev_device_get_devpath(card)))
199 pa_proplist_sets(p, "sysfs.path", v);
201 if (!pa_proplist_contains(p, "udev.id"))
202 if ((v = udev_device_get_property_value(card, "ID_ID")) && *v)
203 pa_proplist_sets(p, "udev.id", v);
205 if (!pa_proplist_contains(p, PA_PROP_DEVICE_BUS))
206 if ((v = udev_device_get_property_value(card, "ID_BUS")) && *v)
207 pa_proplist_sets(p, PA_PROP_DEVICE_BUS, v);
209 if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_ID))
210 if ((id = read_id(card, "ID_VENDOR_ID")) > 0)
211 pa_proplist_setf(p, PA_PROP_DEVICE_VENDOR_ID, "%04x", id);
213 if (!pa_proplist_contains(p, PA_PROP_DEVICE_VENDOR_NAME)) {
214 if ((v = udev_device_get_property_value(card, "ID_VENDOR_FROM_DATABASE")) && *v)
215 pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
216 else if ((v = udev_device_get_property_value(card, "ID_VENDOR_ENC")) && *v)
217 proplist_sets_unescape(p, PA_PROP_DEVICE_VENDOR_NAME, v);
218 else if ((v = udev_device_get_property_value(card, "ID_VENDOR")) && *v)
219 pa_proplist_sets(p, PA_PROP_DEVICE_VENDOR_NAME, v);
222 if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_ID))
223 if ((id = read_id(card, "ID_MODEL_ID")) >= 0)
224 pa_proplist_setf(p, PA_PROP_DEVICE_PRODUCT_ID, "%04x", id);
226 if (!pa_proplist_contains(p, PA_PROP_DEVICE_PRODUCT_NAME)) {
227 if ((v = udev_device_get_property_value(card, "ID_MODEL_FROM_DATABASE")) && *v)
228 pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
229 else if ((v = udev_device_get_property_value(card, "ID_MODEL_ENC")) && *v)
230 proplist_sets_unescape(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
231 else if ((v = udev_device_get_property_value(card, "ID_MODEL")) && *v)
232 pa_proplist_sets(p, PA_PROP_DEVICE_PRODUCT_NAME, v);
235 if (!pa_proplist_contains(p, PA_PROP_DEVICE_SERIAL))
236 if ((v = udev_device_get_property_value(card, "ID_SERIAL")) && *v)
237 pa_proplist_sets(p, PA_PROP_DEVICE_SERIAL, v);
239 if (!pa_proplist_contains(p, PA_PROP_DEVICE_CLASS))
240 if ((v = udev_device_get_property_value(card, "SOUND_CLASS")) && *v)
241 pa_proplist_sets(p, PA_PROP_DEVICE_CLASS, v);
243 if (!pa_proplist_contains(p, PA_PROP_DEVICE_FORM_FACTOR))
244 if ((v = udev_device_get_property_value(card, "SOUND_FORM_FACTOR")) && *v)
245 pa_proplist_sets(p, PA_PROP_DEVICE_FORM_FACTOR, v);
247 /* This is normaly not set by the udev rules but may be useful to
248 * allow administrators to overwrite the device description.*/
249 if (!pa_proplist_contains(p, PA_PROP_DEVICE_DESCRIPTION))
250 if ((v = udev_device_get_property_value(card, "SOUND_DESCRIPTION")) && *v)
251 pa_proplist_sets(p, PA_PROP_DEVICE_DESCRIPTION, v);
253 r = 0;
255 finish:
257 if (card)
258 udev_device_unref(card);
260 if (udev)
261 udev_unref(udev);
263 return r;
266 char* pa_udev_get_property(int card_idx, const char *name) {
267 struct udev *udev;
268 struct udev_device *card = NULL;
269 char *t, *r = NULL;
270 const char *v;
272 pa_assert(card_idx >= 0);
273 pa_assert(name);
275 if (!(udev = udev_new())) {
276 pa_log_error("Failed to allocate udev context.");
277 goto finish;
280 t = pa_sprintf_malloc("%s/class/sound/card%i", udev_get_sys_path(udev), card_idx);
281 card = udev_device_new_from_syspath(udev, t);
282 pa_xfree(t);
284 if (!card) {
285 pa_log_error("Failed to get card object.");
286 goto finish;
289 if ((v = udev_device_get_property_value(card, name)) && *v)
290 r = pa_xstrdup(v);
292 finish:
294 if (card)
295 udev_device_unref(card);
297 if (udev)
298 udev_unref(udev);
300 return r;