lirc: fix logic behind mute buttons
[profile/ivi/pulseaudio-panda.git] / src / modules / reserve-wrap.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 published
8   by the Free Software Foundation; either version 2.1 of the License,
9   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 License
17   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 <errno.h>
27
28 #include <pulse/xmalloc.h>
29 #include <pulse/i18n.h>
30
31 #include <pulsecore/core-error.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/shared.h>
34 #include <pulsecore/dbus-shared.h>
35
36 #include "reserve.h"
37 #include "reserve-wrap.h"
38
39 struct pa_reserve_wrapper {
40     PA_REFCNT_DECLARE;
41     pa_core *core;
42     pa_dbus_connection *connection;
43     pa_hook hook;
44     struct rd_device *device;
45     char *shared_name;
46 };
47
48 static void reserve_wrapper_free(pa_reserve_wrapper *r) {
49     pa_assert(r);
50
51     if (r->device)
52         rd_release(r->device);
53
54     pa_hook_done(&r->hook);
55
56     if (r->connection)
57         pa_dbus_connection_unref(r->connection);
58
59     if (r->shared_name) {
60         pa_assert_se(pa_shared_remove(r->core, r->shared_name) >= 0);
61         pa_xfree(r->shared_name);
62     }
63
64     pa_xfree(r);
65 }
66
67 static int request_cb(rd_device *d, int forced) {
68     pa_reserve_wrapper *r;
69     int k;
70
71     pa_assert(d);
72     pa_assert_se(r = rd_get_userdata(d));
73     pa_assert(PA_REFCNT_VALUE(r) >= 1);
74
75     PA_REFCNT_INC(r);
76
77     k = pa_hook_fire(&r->hook, PA_INT_TO_PTR(forced));
78     pa_log_debug("Device unlock has been requested and %s.", k < 0 ? "failed" : "succeeded");
79
80     pa_reserve_wrapper_unref(r);
81
82     return k < 0 ? -1 : 1;
83 }
84
85 pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) {
86     pa_reserve_wrapper *r;
87     DBusError error;
88     int k;
89     char *t;
90
91     dbus_error_init(&error);
92
93     pa_assert(c);
94     pa_assert(device_name);
95
96     t = pa_sprintf_malloc("reserve-wrapper@%s", device_name);
97
98     if ((r = pa_shared_get(c, t))) {
99         pa_xfree(t);
100
101         pa_assert(PA_REFCNT_VALUE(r) >= 1);
102         PA_REFCNT_INC(r);
103
104         return r;
105     }
106
107     r = pa_xnew0(pa_reserve_wrapper, 1);
108     PA_REFCNT_INIT(r);
109     r->core = c;
110     pa_hook_init(&r->hook, r);
111     r->shared_name = t;
112
113     pa_assert_se(pa_shared_set(c, r->shared_name, r) >= 0);
114
115     if (!(r->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
116         pa_log_warn("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);
117
118         /* We don't treat this as error here because we want allow PA
119          * to run even when no session bus is available. */
120         return r;
121     }
122
123     if ((k = rd_acquire(
124                  &r->device,
125                  pa_dbus_connection_get(r->connection),
126                  device_name,
127                  _("PulseAudio Sound Server"),
128                  0,
129                  request_cb,
130                  NULL)) < 0) {
131
132         if (k == -EBUSY) {
133             pa_log_error("Device '%s' already locked.", device_name);
134             goto fail;
135         } else {
136             pa_log_warn("Failed to acquire reservation lock on device '%s': %s", device_name, pa_cstrerror(-k));
137             return r;
138         }
139     }
140
141     pa_log_debug("Successfully acquired reservation lock on device '%s'", device_name);
142
143     rd_set_userdata(r->device, r);
144
145     return r;
146
147 fail:
148     dbus_error_free(&error);
149
150     reserve_wrapper_free(r);
151
152     return NULL;
153 }
154
155 void pa_reserve_wrapper_unref(pa_reserve_wrapper *r) {
156     pa_assert(r);
157     pa_assert(PA_REFCNT_VALUE(r) >= 1);
158
159     if (PA_REFCNT_DEC(r) > 0)
160         return;
161
162     reserve_wrapper_free(r);
163 }
164
165 pa_hook* pa_reserve_wrapper_hook(pa_reserve_wrapper *r) {
166     pa_assert(r);
167     pa_assert(PA_REFCNT_VALUE(r) >= 1);
168
169     return &r->hook;
170 }
171
172 void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const char *name) {
173     pa_assert(r);
174     pa_assert(PA_REFCNT_VALUE(r) >= 1);
175
176     rd_set_application_device_name(r->device, name);
177 }