include a few HAL properties in our card/sink/source properties for ALSA devices
[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 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
28 #include <hal/libhal.h>
29
30 #include "dbus-util.h"
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     t = libhal_device_get_property_string(hal, udis[i], "info.product", &error);
93     if (dbus_error_is_set(&error))
94         dbus_error_free(&error);
95     if (t) {
96         pa_proplist_sets(p, "hal.product", t);
97         libhal_free_string(t);
98     }
99
100     t = libhal_device_get_property_string(hal, udis[i], "sound.card_id", &error);
101     if (dbus_error_is_set(&error))
102         dbus_error_free(&error);
103     if (t) {
104         pa_proplist_sets(p, "hal.card_id", t);
105         libhal_free_string(t);
106     }
107
108     r = 0;
109
110 finish:
111
112     if (udis)
113         libhal_free_string_array(udis);
114
115     dbus_error_free(&error);
116
117     if (hal) {
118         libhal_ctx_shutdown(hal, &error);
119         libhal_ctx_free(hal);
120         dbus_error_free(&error);
121     }
122
123     if (c)
124         pa_dbus_connection_unref(c);
125
126     return r;
127 }