]> code.delx.au - pulseaudio/blob - src/modules/dbus-util.c
get rid of svn $ keywords
[pulseaudio] / src / modules / dbus-util.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5 Copyright 2006 Shams E. King
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <pulse/xmalloc.h>
28 #include <pulse/timeval.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/props.h>
31
32 #include "dbus-util.h"
33
34 struct pa_dbus_connection {
35 PA_REFCNT_DECLARE;
36
37 pa_core *core;
38 DBusConnection *connection;
39 const char *property_name;
40 pa_defer_event* dispatch_event;
41 };
42
43 static void dispatch_cb(pa_mainloop_api *ea, pa_defer_event *ev, void *userdata) {
44 DBusConnection *conn = userdata;
45
46 if (dbus_connection_dispatch(conn) == DBUS_DISPATCH_COMPLETE) {
47 /* no more data to process, disable the deferred */
48 ea->defer_enable(ev, 0);
49 }
50 }
51
52 /* DBusDispatchStatusFunction callback for the pa mainloop */
53 static void dispatch_status(DBusConnection *conn, DBusDispatchStatus status, void *userdata) {
54 pa_dbus_connection *c = userdata;
55
56 pa_assert(c);
57
58 switch(status) {
59
60 case DBUS_DISPATCH_COMPLETE:
61 c->core->mainloop->defer_enable(c->dispatch_event, 0);
62 break;
63
64 case DBUS_DISPATCH_DATA_REMAINS:
65 case DBUS_DISPATCH_NEED_MEMORY:
66 default:
67 c->core->mainloop->defer_enable(c->dispatch_event, 1);
68 break;
69 }
70 }
71
72 static pa_io_event_flags_t get_watch_flags(DBusWatch *watch) {
73 unsigned int flags;
74 pa_io_event_flags_t events = 0;
75
76 pa_assert(watch);
77
78 flags = dbus_watch_get_flags(watch);
79
80 /* no watch flags for disabled watches */
81 if (!dbus_watch_get_enabled(watch))
82 return PA_IO_EVENT_NULL;
83
84 if (flags & DBUS_WATCH_READABLE)
85 events |= PA_IO_EVENT_INPUT;
86 if (flags & DBUS_WATCH_WRITABLE)
87 events |= PA_IO_EVENT_OUTPUT;
88
89 return events | PA_IO_EVENT_HANGUP | PA_IO_EVENT_ERROR;
90 }
91
92 /* pa_io_event_cb_t IO event handler */
93 static void handle_io_event(PA_GCC_UNUSED pa_mainloop_api *ea, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata) {
94 unsigned int flags = 0;
95 DBusWatch *watch = userdata;
96
97 #if HAVE_DBUS_WATCH_GET_UNIX_FD
98 pa_assert(fd == dbus_watch_get_unix_fd(watch));
99 #else
100 pa_assert(fd == dbus_watch_get_fd(watch));
101 #endif
102
103 if (!dbus_watch_get_enabled(watch)) {
104 pa_log_warn("Asked to handle disabled watch: %p %i", (void*) watch, fd);
105 return;
106 }
107
108 if (events & PA_IO_EVENT_INPUT)
109 flags |= DBUS_WATCH_READABLE;
110 if (events & PA_IO_EVENT_OUTPUT)
111 flags |= DBUS_WATCH_WRITABLE;
112 if (events & PA_IO_EVENT_HANGUP)
113 flags |= DBUS_WATCH_HANGUP;
114 if (events & PA_IO_EVENT_ERROR)
115 flags |= DBUS_WATCH_ERROR;
116
117 dbus_watch_handle(watch, flags);
118 }
119
120 /* pa_time_event_cb_t timer event handler */
121 static void handle_time_event(pa_mainloop_api *ea, pa_time_event* e, const struct timeval *tv, void *userdata) {
122 DBusTimeout *timeout = userdata;
123
124 if (dbus_timeout_get_enabled(timeout)) {
125 struct timeval next = *tv;
126 dbus_timeout_handle(timeout);
127
128 /* restart it for the next scheduled time */
129 pa_timeval_add(&next, dbus_timeout_get_interval(timeout) * 1000);
130 ea->time_restart(e, &next);
131 }
132 }
133
134 /* DBusAddWatchFunction callback for pa mainloop */
135 static dbus_bool_t add_watch(DBusWatch *watch, void *data) {
136 pa_core *c = PA_CORE(data);
137 pa_io_event *ev;
138
139 pa_assert(watch);
140 pa_assert(c);
141
142 ev = c->mainloop->io_new(
143 c->mainloop,
144 #if HAVE_DBUS_WATCH_GET_UNIX_FD
145 dbus_watch_get_unix_fd(watch),
146 #else
147 dbus_watch_get_fd(watch),
148 #endif
149 get_watch_flags(watch), handle_io_event, watch);
150
151 dbus_watch_set_data(watch, ev, NULL);
152
153 return TRUE;
154 }
155
156 /* DBusRemoveWatchFunction callback for pa mainloop */
157 static void remove_watch(DBusWatch *watch, void *data) {
158 pa_core *c = PA_CORE(data);
159 pa_io_event *ev;
160
161 pa_assert(watch);
162 pa_assert(c);
163
164 if ((ev = dbus_watch_get_data(watch)))
165 c->mainloop->io_free(ev);
166 }
167
168 /* DBusWatchToggledFunction callback for pa mainloop */
169 static void toggle_watch(DBusWatch *watch, void *data) {
170 pa_core *c = PA_CORE(data);
171 pa_io_event *ev;
172
173 pa_assert(watch);
174 pa_core_assert_ref(c);
175
176 pa_assert_se(ev = dbus_watch_get_data(watch));
177
178 /* get_watch_flags() checks if the watch is enabled */
179 c->mainloop->io_enable(ev, get_watch_flags(watch));
180 }
181
182 /* DBusAddTimeoutFunction callback for pa mainloop */
183 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data) {
184 pa_core *c = PA_CORE(data);
185 pa_time_event *ev;
186 struct timeval tv;
187
188 pa_assert(timeout);
189 pa_assert(c);
190
191 if (!dbus_timeout_get_enabled(timeout))
192 return FALSE;
193
194 pa_gettimeofday(&tv);
195 pa_timeval_add(&tv, dbus_timeout_get_interval(timeout) * 1000);
196
197 ev = c->mainloop->time_new(c->mainloop, &tv, handle_time_event, timeout);
198
199 dbus_timeout_set_data(timeout, ev, NULL);
200
201 return TRUE;
202 }
203
204 /* DBusRemoveTimeoutFunction callback for pa mainloop */
205 static void remove_timeout(DBusTimeout *timeout, void *data) {
206 pa_core *c = PA_CORE(data);
207 pa_time_event *ev;
208
209 pa_assert(timeout);
210 pa_assert(c);
211
212 if ((ev = dbus_timeout_get_data(timeout)))
213 c->mainloop->time_free(ev);
214 }
215
216 /* DBusTimeoutToggledFunction callback for pa mainloop */
217 static void toggle_timeout(DBusTimeout *timeout, void *data) {
218 pa_core *c = PA_CORE(data);
219 pa_time_event *ev;
220
221 pa_assert(timeout);
222 pa_assert(c);
223
224 pa_assert_se(ev = dbus_timeout_get_data(timeout));
225
226 if (dbus_timeout_get_enabled(timeout)) {
227 struct timeval tv;
228
229 pa_gettimeofday(&tv);
230 pa_timeval_add(&tv, dbus_timeout_get_interval(timeout) * 1000);
231
232 c->mainloop->time_restart(ev, &tv);
233 } else
234 c->mainloop->time_restart(ev, NULL);
235 }
236
237 static void wakeup_main(void *userdata) {
238 pa_dbus_connection *c = userdata;
239
240 pa_assert(c);
241
242 /* this will wakeup the mainloop and dispatch events, although
243 * it may not be the cleanest way of accomplishing it */
244 c->core->mainloop->defer_enable(c->dispatch_event, 1);
245 }
246
247 static pa_dbus_connection* pa_dbus_connection_new(pa_core* c, DBusConnection *conn, const char* name) {
248 pa_dbus_connection *pconn;
249
250 pconn = pa_xnew(pa_dbus_connection, 1);
251 PA_REFCNT_INIT(pconn);
252 pconn->core = c;
253 pconn->property_name = name;
254 pconn->connection = conn;
255 pconn->dispatch_event = c->mainloop->defer_new(c->mainloop, dispatch_cb, conn);
256
257 pa_property_set(c, name, pconn);
258
259 return pconn;
260 }
261
262 DBusConnection* pa_dbus_connection_get(pa_dbus_connection *c){
263 pa_assert(c);
264 pa_assert(PA_REFCNT_VALUE(c) > 0);
265 pa_assert(c->connection);
266
267 return c->connection;
268 }
269
270 void pa_dbus_connection_unref(pa_dbus_connection *c) {
271 pa_assert(c);
272 pa_assert(PA_REFCNT_VALUE(c) > 0);
273
274 if (PA_REFCNT_DEC(c) > 0)
275 return;
276
277 if (dbus_connection_get_is_connected(c->connection)) {
278 dbus_connection_close(c->connection);
279 /* must process remaining messages, bit of a kludge to handle
280 * both unload and shutdown */
281 while (dbus_connection_read_write_dispatch(c->connection, -1));
282 }
283
284 /* already disconnected, just free */
285 pa_property_remove(c->core, c->property_name);
286 c->core->mainloop->defer_free(c->dispatch_event);
287 dbus_connection_unref(c->connection);
288 pa_xfree(c);
289 }
290
291 pa_dbus_connection* pa_dbus_connection_ref(pa_dbus_connection *c) {
292 pa_assert(c);
293 pa_assert(PA_REFCNT_VALUE(c) > 0);
294
295 PA_REFCNT_INC(c);
296
297 return c;
298 }
299
300 pa_dbus_connection* pa_dbus_bus_get(pa_core *c, DBusBusType type, DBusError *error) {
301
302 static const char *const prop_name[] = {
303 [DBUS_BUS_SESSION] = "dbus-connection-session",
304 [DBUS_BUS_SYSTEM] = "dbus-connection-system",
305 [DBUS_BUS_STARTER] = "dbus-connection-starter"
306 };
307 DBusConnection *conn;
308 pa_dbus_connection *pconn;
309
310 pa_assert(type == DBUS_BUS_SYSTEM || type == DBUS_BUS_SESSION || type == DBUS_BUS_STARTER);
311
312 if ((pconn = pa_property_get(c, prop_name[type])))
313 return pa_dbus_connection_ref(pconn);
314
315 if (!(conn = dbus_bus_get_private(type, error)))
316 return NULL;
317
318 pconn = pa_dbus_connection_new(c, conn, prop_name[type]);
319
320 dbus_connection_set_exit_on_disconnect(conn, FALSE);
321 dbus_connection_set_dispatch_status_function(conn, dispatch_status, pconn, NULL);
322 dbus_connection_set_watch_functions(conn, add_watch, remove_watch, toggle_watch, c, NULL);
323 dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout, toggle_timeout, c, NULL);
324 dbus_connection_set_wakeup_main_function(conn, wakeup_main, pconn, NULL);
325
326 return pconn;
327 }