build-system: move x11 and jack modules into subdirectories
[profile/ivi/pulseaudio-panda.git] / src / modules / hal-util.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2009 Lennart Poettering
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pulsecore/log.h>
27 #include <pulsecore/dbus-shared.h>
28
29 #include <hal/libhal.h>
30
31 #include "hal-util.h"
32
33 int pa_hal_get_info(pa_core *core, pa_proplist *p, int card) {
34     pa_dbus_connection *c = NULL;
35     LibHalContext *hal = NULL;
36     DBusError error;
37     int r = -1;
38     char **udis = NULL, *t;
39     int n, i;
40
41     pa_assert(core);
42     pa_assert(p);
43     pa_assert(card >= 0);
44
45     dbus_error_init(&error);
46
47     if (!(c = pa_dbus_bus_get(core, DBUS_BUS_SYSTEM, &error)) || dbus_error_is_set(&error)) {
48         pa_log_error("Unable to contact DBUS system bus: %s: %s", error.name, error.message);
49         goto finish;
50     }
51
52
53     if (!(hal = libhal_ctx_new())) {
54         pa_log_error("libhal_ctx_new() finished");
55         goto finish;
56     }
57
58     if (!libhal_ctx_set_dbus_connection(hal, pa_dbus_connection_get(c))) {
59         pa_log_error("Error establishing DBUS connection: %s: %s", error.name, error.message);
60         goto finish;
61     }
62
63     if (!libhal_ctx_init(hal, &error)) {
64         pa_log_error("Couldn't connect to hald: %s: %s", error.name, error.message);
65         goto finish;
66     }
67
68     if (!(udis = libhal_find_device_by_capability(hal, "sound", &n, &error)) < 0) {
69         pa_log_error("Couldn't find devices: %s: %s", error.name, error.message);
70         goto finish;
71     }
72
73     for (i = 0; i < n; i++) {
74         dbus_int32_t this_card;
75
76         this_card = libhal_device_get_property_int(hal, udis[i], "sound.card", &error);
77         if (dbus_error_is_set(&error)) {
78             dbus_error_free(&error);
79             continue;
80         }
81
82         if (this_card == card)
83             break;
84
85     }
86
87     if (i >= n)
88         goto finish;
89
90     pa_proplist_sets(p, "hal.udi", udis[i]);
91
92     /* The data HAL stores in info.product is not actually a product
93      * string but simply the ALSA card name. We will hence not write
94      * it to PA_PROP_DEVICE_PRODUCT_NAME */
95     t = libhal_device_get_property_string(hal, udis[i], "info.product", &error);
96     if (dbus_error_is_set(&error))
97         dbus_error_free(&error);
98     if (t) {
99         pa_proplist_sets(p, "hal.product", t);
100         libhal_free_string(t);
101     }
102
103     t = libhal_device_get_property_string(hal, udis[i], "sound.card_id", &error);
104     if (dbus_error_is_set(&error))
105         dbus_error_free(&error);
106     if (t) {
107         pa_proplist_sets(p, "hal.card_id", t);
108         libhal_free_string(t);
109     }
110
111     r = 0;
112
113 finish:
114
115     if (udis)
116         libhal_free_string_array(udis);
117
118     dbus_error_free(&error);
119
120     if (hal) {
121         libhal_ctx_shutdown(hal, &error);
122         libhal_ctx_free(hal);
123         dbus_error_free(&error);
124     }
125
126     if (c)
127         pa_dbus_connection_unref(c);
128
129     return r;
130 }