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