summaryrefslogtreecommitdiffstats
path: root/src/modules/reserve-wrap.c
blob: 202311ae9593f25f1683b395d9732aef1a970147 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/***
  This file is part of PulseAudio.

  Copyright 2009 Lennart Poettering

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>

#include <pulse/xmalloc.h>

#include <pulsecore/core-error.h>
#include <pulsecore/core-util.h>
#include <pulsecore/i18n.h>
#include <pulsecore/shared.h>

#ifdef HAVE_DBUS
#include <pulsecore/dbus-shared.h>
#include "reserve.h"
#include "reserve-monitor.h"
#endif

#include "reserve-wrap.h"

struct pa_reserve_wrapper {
    PA_REFCNT_DECLARE;
    pa_core *core;
    pa_hook hook;
    char *shared_name;
#ifdef HAVE_DBUS
    pa_dbus_connection *connection;
    struct rd_device *device;
#endif
};

struct pa_reserve_monitor_wrapper {
    PA_REFCNT_DECLARE;
    pa_core *core;
    pa_hook hook;
    char *shared_name;
#ifdef HAVE_DBUS
    pa_dbus_connection *connection;
    struct rm_monitor *monitor;
#endif
};

static void reserve_wrapper_free(pa_reserve_wrapper *r) {
    pa_assert(r);

#ifdef HAVE_DBUS
    if (r->device)
        rd_release(r->device);

    if (r->connection)
        pa_dbus_connection_unref(r->connection);
#endif

    pa_hook_done(&r->hook);

    if (r->shared_name) {
        pa_assert_se(pa_shared_remove(r->core, r->shared_name) >= 0);
        pa_xfree(r->shared_name);
    }

    pa_xfree(r);
}

#ifdef HAVE_DBUS
static int request_cb(rd_device *d, int forced) {
    pa_reserve_wrapper *r;
    int k;

    pa_assert(d);
    pa_assert_se(r = rd_get_userdata(d));
    pa_assert(PA_REFCNT_VALUE(r) >= 1);

    PA_REFCNT_INC(r);

    k = pa_hook_fire(&r->hook, PA_INT_TO_PTR(forced));
    pa_log_debug("Device unlock of %s has been requested and %s.", r->shared_name, k < 0 ? "failed" : "succeeded");

    pa_reserve_wrapper_unref(r);

    return k < 0 ? -1 : 1;
}
#endif

pa_reserve_wrapper* pa_reserve_wrapper_get(pa_core *c, const char *device_name) {
    pa_reserve_wrapper *r;
    char *t;
#ifdef HAVE_DBUS
    int k;
    DBusError error;

    dbus_error_init(&error);
#endif

    pa_assert(c);
    pa_assert(device_name);

    t = pa_sprintf_malloc("reserve-wrapper@%s", device_name);

    if ((r = pa_shared_get(c, t))) {
        pa_xfree(t);

        pa_assert(PA_REFCNT_VALUE(r) >= 1);
        PA_REFCNT_INC(r);

        return r;
    }

    r = pa_xnew0(pa_reserve_wrapper, 1);
    PA_REFCNT_INIT(r);
    r->core = c;
    pa_hook_init(&r->hook, r);
    r->shared_name = t;

    pa_assert_se(pa_shared_set(c, r->shared_name, r) >= 0);

#ifdef HAVE_DBUS
    if (!(r->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
        pa_log_debug("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);

        /* We don't treat this as error here because we want allow PA
         * to run even when no session bus is available. */
        return r;
    }

    if ((k = rd_acquire(
                 &r->device,
                 pa_dbus_connection_get(r->connection),
                 device_name,
                 _("PulseAudio Sound Server"),
                 0,
                 request_cb,
                 NULL)) < 0) {

        if (k == -EBUSY) {
            pa_log_debug("Device '%s' already locked.", device_name);
            goto fail;
        } else {
            pa_log_debug("Failed to acquire reservation lock on device '%s': %s", device_name, pa_cstrerror(-k));
            return r;
        }
    }

    pa_log_debug("Successfully acquired reservation lock on device '%s'", device_name);

    rd_set_userdata(r->device, r);

    return r;
fail:
    dbus_error_free(&error);

    reserve_wrapper_free(r);

    return NULL;
#else
    return r;
#endif
}

void pa_reserve_wrapper_unref(pa_reserve_wrapper *r) {
    pa_assert(r);
    pa_assert(PA_REFCNT_VALUE(r) >= 1);

    if (PA_REFCNT_DEC(r) > 0)
        return;

    reserve_wrapper_free(r);
}

pa_hook* pa_reserve_wrapper_hook(pa_reserve_wrapper *r) {
    pa_assert(r);
    pa_assert(PA_REFCNT_VALUE(r) >= 1);

    return &r->hook;
}

void pa_reserve_wrapper_set_application_device_name(pa_reserve_wrapper *r, const char *name) {
    pa_assert(r);
    pa_assert(PA_REFCNT_VALUE(r) >= 1);

#ifdef HAVE_DBUS
    rd_set_application_device_name(r->device, name);
#endif
}

static void reserve_monitor_wrapper_free(pa_reserve_monitor_wrapper *w) {
    pa_assert(w);

#ifdef HAVE_DBUS
    if (w->monitor)
        rm_release(w->monitor);

    if (w->connection)
        pa_dbus_connection_unref(w->connection);
#endif

    pa_hook_done(&w->hook);

    if (w->shared_name) {
        pa_assert_se(pa_shared_remove(w->core, w->shared_name) >= 0);
        pa_xfree(w->shared_name);
    }

    pa_xfree(w);
}

#ifdef HAVE_DBUS
static void change_cb(rm_monitor *m) {
    pa_reserve_monitor_wrapper *w;
    int k;

    pa_assert(m);
    pa_assert_se(w = rm_get_userdata(m));
    pa_assert(PA_REFCNT_VALUE(w) >= 1);

    PA_REFCNT_INC(w);

    if ((k = rm_busy(w->monitor)) < 0)
        return;

    pa_hook_fire(&w->hook, PA_INT_TO_PTR(!!k));
    pa_log_debug("Device lock status of %s changed: %s", w->shared_name, k ? "busy" : "not busy");

    pa_reserve_monitor_wrapper_unref(w);
}
#endif

pa_reserve_monitor_wrapper* pa_reserve_monitor_wrapper_get(pa_core *c, const char *device_name) {
    pa_reserve_monitor_wrapper *w;
    char *t;
#ifdef HAVE_DBUS
    int k;
    DBusError error;

    dbus_error_init(&error);
#endif

    pa_assert(c);
    pa_assert(device_name);

    t = pa_sprintf_malloc("reserve-monitor-wrapper@%s", device_name);

    if ((w = pa_shared_get(c, t))) {
        pa_xfree(t);

        pa_assert(PA_REFCNT_VALUE(w) >= 1);
        PA_REFCNT_INC(w);

        return w;
    }

    w = pa_xnew0(pa_reserve_monitor_wrapper, 1);
    PA_REFCNT_INIT(w);
    w->core = c;
    pa_hook_init(&w->hook, w);
    w->shared_name = t;

    pa_assert_se(pa_shared_set(c, w->shared_name, w) >= 0);

#ifdef HAVE_DBUS
    if (!(w->connection = pa_dbus_bus_get(c, DBUS_BUS_SESSION, &error)) || dbus_error_is_set(&error)) {
        pa_log_debug("Unable to contact D-Bus session bus: %s: %s", error.name, error.message);

        /* We don't treat this as error here because we want allow PA
         * to run even when no session bus is available. */
        return w;
    }

    if ((k = rm_watch(
                 &w->monitor,
                 pa_dbus_connection_get(w->connection),
                 device_name,
                 change_cb,
                 NULL)) < 0) {

        pa_log_debug("Failed to create watch on device '%s': %s", device_name, pa_cstrerror(-k));
        goto fail;
    }

    pa_log_debug("Successfully create reservation lock monitor for device '%s'", device_name);

    rm_set_userdata(w->monitor, w);
    return w;

fail:
    dbus_error_free(&error);

    reserve_monitor_wrapper_free(w);

    return NULL;
#else
    return w;
#endif
}

void pa_reserve_monitor_wrapper_unref(pa_reserve_monitor_wrapper *w) {
    pa_assert(w);
    pa_assert(PA_REFCNT_VALUE(w) >= 1);

    if (PA_REFCNT_DEC(w) > 0)
        return;

    reserve_monitor_wrapper_free(w);
}

pa_hook* pa_reserve_monitor_wrapper_hook(pa_reserve_monitor_wrapper *w) {
    pa_assert(w);
    pa_assert(PA_REFCNT_VALUE(w) >= 1);

    return &w->hook;
}

bool pa_reserve_monitor_wrapper_busy(pa_reserve_monitor_wrapper *w) {
    pa_assert(w);

    pa_assert(PA_REFCNT_VALUE(w) >= 1);

#ifdef HAVE_DBUS
    return rm_busy(w->monitor) > 0;
#else
    return false;
#endif
}