]> code.delx.au - pulseaudio/blob - src/modules/reserve-wrap.c
reserve-device: allow building without D-Bus
[pulseaudio] / 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
35 #ifdef HAVE_DBUS
36 #include <pulsecore/dbus-shared.h>
37 #include "reserve.h"
38 #endif
39
40 #include "reserve-wrap.h"
41
42 struct pa_reserve_wrapper {
43 PA_REFCNT_DECLARE;
44 pa_core *core;
45 pa_hook hook;
46 char *shared_name;
47 #ifdef HAVE_DBUS
48 pa_dbus_connection *connection;
49 struct rd_device *device;
50 #endif
51 };
52
53 static void reserve_wrapper_free(pa_reserve_wrapper *r) {
54 pa_assert(r);
55
56 #ifdef HAVE_DBUS
57 if (r->device)
58 rd_release(r->device);
59
60 if (r->connection)
61 pa_dbus_connection_unref(r->connection);
62 #endif
63
64 pa_hook_done(&r->hook);
65
66 if (r->shared_name) {
67 pa_assert_se(pa_shared_remove(r->core, r->shared_name) >= 0);
68 pa_xfree(r->shared_name);
69 }
70
71 pa_xfree(r);
72 }
73
74 #ifdef HAVE_DBUS
75 static int request_cb(rd_device *d, int forced) {
76 pa_reserve_wrapper *r;
77 int k;
78
79 pa_assert(d);
80 pa_assert_se(r = rd_get_userdata(d));
81 pa_assert(PA_REFCNT_VALUE(r) >= 1);
82
83 PA_REFCNT_INC(r);
84
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");
87
88 pa_reserve_wrapper_unref(r);
89
90 return k < 0 ? -1 : 1;
91 }
92 #endif
93
94 pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) {
95 pa_reserve_wrapper *r;
96 int k;
97 char *t;
98 #ifdef HAVE_DBUS
99 DBusError error;
100
101 dbus_error_init(&error);
102 #endif
103
104 pa_assert(c);
105 pa_assert(device_name);
106
107 t = pa_sprintf_malloc("reserve-wrapper@%s", device_name);
108
109 if ((r = pa_shared_get(c, t))) {
110 pa_xfree(t);
111
112 pa_assert(PA_REFCNT_VALUE(r) >= 1);
113 PA_REFCNT_INC(r);
114
115 return r;
116 }
117
118 r = pa_xnew0(pa_reserve_wrapper, 1);
119 PA_REFCNT_INIT(r);
120 r->core = c;
121 pa_hook_init(&r->hook, r);
122 r->shared_name = t;
123
124 pa_assert_se(pa_shared_set(c, r->shared_name, r) >= 0);
125
126 #ifdef HAVE_DBUS
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);
129
130 /* We don't treat this as error here because we want allow PA
131 * to run even when no session bus is available. */
132 return r;
133 }
134
135 if ((k = rd_acquire(
136 &r->device,
137 pa_dbus_connection_get(r->connection),
138 device_name,
139 _("PulseAudio Sound Server"),
140 0,
141 request_cb,
142 NULL)) < 0) {
143
144 if (k == -EBUSY) {
145 pa_log_error("Device '%s' already locked.", device_name);
146 goto fail;
147 } else {
148 pa_log_warn("Failed to acquire reservation lock on device '%s': %s", device_name, pa_cstrerror(-k));
149 return r;
150 }
151 }
152
153 pa_log_debug("Successfully acquired reservation lock on device '%s'", device_name);
154
155 rd_set_userdata(r->device, r);
156
157 return r;
158 fail:
159 dbus_error_free(&error);
160
161 reserve_wrapper_free(r);
162
163 return NULL;
164 #else
165 return r;
166 #endif
167 }
168
169 void pa_reserve_wrapper_unref(pa_reserve_wrapper *r) {
170 pa_assert(r);
171 pa_assert(PA_REFCNT_VALUE(r) >= 1);
172
173 if (PA_REFCNT_DEC(r) > 0)
174 return;
175
176 reserve_wrapper_free(r);
177 }
178
179 pa_hook* pa_reserve_wrapper_hook(pa_reserve_wrapper *r) {
180 pa_assert(r);
181 pa_assert(PA_REFCNT_VALUE(r) >= 1);
182
183 return &r->hook;
184 }
185
186 void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const char *name) {
187 pa_assert(r);
188 pa_assert(PA_REFCNT_VALUE(r) >= 1);
189
190 #ifdef HAVE_DBUS
191 rd_set_application_device_name(r->device, name);
192 #endif
193 }