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 published
8 by the Free Software Foundation; either version 2.1 of the License,
9 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 License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/core-error.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/i18n.h>
33 #include <pulsecore/shared.h>
36 #include <pulsecore/dbus-shared.h>
38 #include "reserve-monitor.h"
41 #include "reserve-wrap.h"
43 struct pa_reserve_wrapper {
49 pa_dbus_connection *connection;
50 struct rd_device *device;
54 struct pa_reserve_monitor_wrapper {
60 pa_dbus_connection *connection;
61 struct rm_monitor *monitor;
65 static void reserve_wrapper_free(pa_reserve_wrapper *r) {
70 rd_release(r->device);
73 pa_dbus_connection_unref(r->connection);
76 pa_hook_done(&r->hook);
79 pa_assert_se(pa_shared_remove(r->core, r->shared_name) >= 0);
80 pa_xfree(r->shared_name);
87 static int request_cb(rd_device *d, int forced) {
88 pa_reserve_wrapper *r;
92 pa_assert_se(r = rd_get_userdata(d));
93 pa_assert(PA_REFCNT_VALUE(r) >= 1);
97 k = pa_hook_fire(&r->hook, PA_INT_TO_PTR(forced));
98 pa_log_debug("Device unlock of %s has been requested and %s.", r->shared_name, k < 0 ? "failed" : "succeeded");
100 pa_reserve_wrapper_unref(r);
102 return k < 0 ? -1 : 1;
106 pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) {
107 pa_reserve_wrapper *r;
113 dbus_error_init(&error);
117 pa_assert(device_name);
119 t = pa_sprintf_malloc("reserve-wrapper@%s", device_name);
121 if ((r = pa_shared_get(c, t))) {
124 pa_assert(PA_REFCNT_VALUE(r) >= 1);
130 r = pa_xnew0(pa_reserve_wrapper, 1);
133 pa_hook_init(&r->hook, r);
136 pa_assert_se(pa_shared_set(c, r->shared_name, r) >= 0);
139 if (!(r->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
140 pa_log_debug("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);
142 /* We don't treat this as error here because we want allow PA
143 * to run even when no session bus is available. */
149 pa_dbus_connection_get(r->connection),
151 _("PulseAudio Sound Server"),
157 pa_log_debug("Device '%s' already locked.", device_name);
160 pa_log_debug("Failed to acquire reservation lock on device '%s': %s", device_name, pa_cstrerror(-k));
165 pa_log_debug("Successfully acquired reservation lock on device '%s'", device_name);
167 rd_set_userdata(r->device, r);
171 dbus_error_free(&error);
173 reserve_wrapper_free(r);
181 void pa_reserve_wrapper_unref(pa_reserve_wrapper *r) {
183 pa_assert(PA_REFCNT_VALUE(r) >= 1);
185 if (PA_REFCNT_DEC(r) > 0)
188 reserve_wrapper_free(r);
191 pa_hook* pa_reserve_wrapper_hook(pa_reserve_wrapper *r) {
193 pa_assert(PA_REFCNT_VALUE(r) >= 1);
198 void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const char *name) {
200 pa_assert(PA_REFCNT_VALUE(r) >= 1);
203 rd_set_application_device_name(r->device, name);
207 static void reserve_monitor_wrapper_free(pa_reserve_monitor_wrapper *w) {
212 rm_release(w->monitor);
215 pa_dbus_connection_unref(w->connection);
218 pa_hook_done(&w->hook);
220 if (w->shared_name) {
221 pa_assert_se(pa_shared_remove(w->core, w->shared_name) >= 0);
222 pa_xfree(w->shared_name);
229 static void change_cb(rm_monitor *m) {
230 pa_reserve_monitor_wrapper *w;
234 pa_assert_se(w = rm_get_userdata(m));
235 pa_assert(PA_REFCNT_VALUE(w) >= 1);
239 if ((k = rm_busy(w->monitor)) < 0)
242 pa_hook_fire(&w->hook, PA_INT_TO_PTR(!!k));
243 pa_log_debug("Device lock status of %s changed: %s", w->shared_name, k ? "busy" : "not busy");
245 pa_reserve_monitor_wrapper_unref(w);
249 pa_reserve_monitor_wrapper* pa_reserve_monitor_wrapper_get(pa_core *c, const char *device_name) {
250 pa_reserve_monitor_wrapper *w;
256 dbus_error_init(&error);
260 pa_assert(device_name);
262 t = pa_sprintf_malloc("reserve-monitor-wrapper@%s", device_name);
264 if ((w = pa_shared_get(c, t))) {
267 pa_assert(PA_REFCNT_VALUE(w) >= 1);
273 w = pa_xnew0(pa_reserve_monitor_wrapper, 1);
276 pa_hook_init(&w->hook, w);
279 pa_assert_se(pa_shared_set(c, w->shared_name, w) >= 0);
282 if (!(w->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
283 pa_log_debug("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);
285 /* We don't treat this as error here because we want allow PA
286 * to run even when no session bus is available. */
292 pa_dbus_connection_get(w->connection),
297 pa_log_debug("Failed to create watch on device '%s': %s", device_name, pa_cstrerror(-k));
301 pa_log_debug("Successfully create reservation lock monitor for device '%s'", device_name);
303 rm_set_userdata(w->monitor, w);
307 dbus_error_free(&error);
309 reserve_monitor_wrapper_free(w);
317 void pa_reserve_monitor_wrapper_unref(pa_reserve_monitor_wrapper *w) {
319 pa_assert(PA_REFCNT_VALUE(w) >= 1);
321 if (PA_REFCNT_DEC(w) > 0)
324 reserve_monitor_wrapper_free(w);
327 pa_hook* pa_reserve_monitor_wrapper_hook(pa_reserve_monitor_wrapper *w) {
329 pa_assert(PA_REFCNT_VALUE(w) >= 1);
334 pa_bool_t pa_reserve_monitor_wrapper_busy(pa_reserve_monitor_wrapper *w) {
337 pa_assert(PA_REFCNT_VALUE(w) >= 1);
340 return rm_busy(w->monitor) > 0;