]>
code.delx.au - pulseaudio/blob - src/modules/reserve-wrap.c
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>
29 #include <pulse/i18n.h>
31 #include <pulsecore/core-error.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/shared.h>
36 #include <pulsecore/dbus-shared.h>
40 #include "reserve-wrap.h"
42 struct pa_reserve_wrapper
{
48 pa_dbus_connection
*connection
;
49 struct rd_device
*device
;
53 static void reserve_wrapper_free(pa_reserve_wrapper
*r
) {
58 rd_release(r
->device
);
61 pa_dbus_connection_unref(r
->connection
);
64 pa_hook_done(&r
->hook
);
67 pa_assert_se(pa_shared_remove(r
->core
, r
->shared_name
) >= 0);
68 pa_xfree(r
->shared_name
);
75 static int request_cb(rd_device
*d
, int forced
) {
76 pa_reserve_wrapper
*r
;
80 pa_assert_se(r
= rd_get_userdata(d
));
81 pa_assert(PA_REFCNT_VALUE(r
) >= 1);
85 k
= pa_hook_fire(&r
->hook
, PA_INT_TO_PTR(forced
));
86 pa_log_debug("Device unlock has been requested and %s.", k
< 0 ? "failed" : "succeeded");
88 pa_reserve_wrapper_unref(r
);
90 return k
< 0 ? -1 : 1;
94 pa_reserve_wrapper
* pa_reserve_wrapper_get(pa_core
*c
, const char *device_name
) {
95 pa_reserve_wrapper
*r
;
101 dbus_error_init(&error
);
105 pa_assert(device_name
);
107 t
= pa_sprintf_malloc("reserve-wrapper@%s", device_name
);
109 if ((r
= pa_shared_get(c
, t
))) {
112 pa_assert(PA_REFCNT_VALUE(r
) >= 1);
118 r
= pa_xnew0(pa_reserve_wrapper
, 1);
121 pa_hook_init(&r
->hook
, r
);
124 pa_assert_se(pa_shared_set(c
, r
->shared_name
, r
) >= 0);
127 if (!(r
->connection
= pa_dbus_bus_get(c
, DBUS_BUS_SESSION
, &error
)) || dbus_error_is_set(&error
)) {
128 pa_log_warn("Unable to contact D-Bus session bus: %s: %s", error
.name
, error
.message
);
130 /* We don't treat this as error here because we want allow PA
131 * to run even when no session bus is available. */
137 pa_dbus_connection_get(r
->connection
),
139 _("PulseAudio Sound Server"),
145 pa_log_error("Device '%s' already locked.", device_name
);
148 pa_log_warn("Failed to acquire reservation lock on device '%s': %s", device_name
, pa_cstrerror(-k
));
153 pa_log_debug("Successfully acquired reservation lock on device '%s'", device_name
);
155 rd_set_userdata(r
->device
, r
);
159 dbus_error_free(&error
);
161 reserve_wrapper_free(r
);
169 void pa_reserve_wrapper_unref(pa_reserve_wrapper
*r
) {
171 pa_assert(PA_REFCNT_VALUE(r
) >= 1);
173 if (PA_REFCNT_DEC(r
) > 0)
176 reserve_wrapper_free(r
);
179 pa_hook
* pa_reserve_wrapper_hook(pa_reserve_wrapper
*r
) {
181 pa_assert(PA_REFCNT_VALUE(r
) >= 1);
186 void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper
*r
, const char *name
) {
188 pa_assert(PA_REFCNT_VALUE(r
) >= 1);
191 rd_set_application_device_name(r
->device
, name
);