2 * Copyright © 2013 David Herrmann <dh.herrmann@gmail.com>
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 * This file contains the dbus mainloop integration and several helpers to
26 * make lowlevel libdbus access easier.
31 #include <dbus/dbus.h>
37 #include <sys/epoll.h>
38 #include <sys/eventfd.h>
39 #include <sys/timerfd.h>
41 #include <wayland-server.h>
43 #include "compositor.h"
47 * DBus Mainloop Integration
48 * weston_dbus_bind() and weston_dbus_unbind() allow to bind an existing
49 * DBusConnection to an existing wl_event_loop object. All dbus dispatching
50 * is then nicely integrated into the wayland event loop.
51 * Note that this only provides basic watch and timeout dispatching. No
52 * remote thread wakeup, signal handling or other dbus insanity is supported.
53 * This is fine as long as you don't use any of the deprecated libdbus
54 * interfaces (like waking up remote threads..). There is really no rational
55 * reason to support these.
58 static int weston_dbus_dispatch_watch(int fd, uint32_t mask, void *data)
60 DBusWatch *watch = data;
63 if (dbus_watch_get_enabled(watch)) {
64 if (mask & WL_EVENT_READABLE)
65 flags |= DBUS_WATCH_READABLE;
66 if (mask & WL_EVENT_WRITABLE)
67 flags |= DBUS_WATCH_WRITABLE;
68 if (mask & WL_EVENT_HANGUP)
69 flags |= DBUS_WATCH_HANGUP;
70 if (mask & WL_EVENT_ERROR)
71 flags |= DBUS_WATCH_ERROR;
73 dbus_watch_handle(watch, flags);
79 static dbus_bool_t weston_dbus_add_watch(DBusWatch *watch, void *data)
81 struct wl_event_loop *loop = data;
82 struct wl_event_source *s;
84 uint32_t mask = 0, flags;
86 if (dbus_watch_get_enabled(watch)) {
87 flags = dbus_watch_get_flags(watch);
88 if (flags & DBUS_WATCH_READABLE)
89 mask |= WL_EVENT_READABLE;
90 if (flags & DBUS_WATCH_WRITABLE)
91 mask |= WL_EVENT_WRITABLE;
94 fd = dbus_watch_get_unix_fd(watch);
95 s = wl_event_loop_add_fd(loop, fd, mask, weston_dbus_dispatch_watch,
100 dbus_watch_set_data(watch, s, NULL);
104 static void weston_dbus_remove_watch(DBusWatch *watch, void *data)
106 struct wl_event_source *s;
108 s = dbus_watch_get_data(watch);
112 wl_event_source_remove(s);
115 static void weston_dbus_toggle_watch(DBusWatch *watch, void *data)
117 struct wl_event_source *s;
118 uint32_t mask = 0, flags;
120 s = dbus_watch_get_data(watch);
124 if (dbus_watch_get_enabled(watch)) {
125 flags = dbus_watch_get_flags(watch);
126 if (flags & DBUS_WATCH_READABLE)
127 mask |= WL_EVENT_READABLE;
128 if (flags & DBUS_WATCH_WRITABLE)
129 mask |= WL_EVENT_WRITABLE;
132 wl_event_source_fd_update(s, mask);
135 static int weston_dbus_dispatch_timeout(void *data)
137 DBusTimeout *timeout = data;
139 if (dbus_timeout_get_enabled(timeout))
140 dbus_timeout_handle(timeout);
145 static int weston_dbus_adjust_timeout(DBusTimeout *timeout,
146 struct wl_event_source *s)
150 if (dbus_timeout_get_enabled(timeout))
151 t = dbus_timeout_get_interval(timeout);
153 return wl_event_source_timer_update(s, t);
156 static dbus_bool_t weston_dbus_add_timeout(DBusTimeout *timeout, void *data)
158 struct wl_event_loop *loop = data;
159 struct wl_event_source *s;
162 s = wl_event_loop_add_timer(loop, weston_dbus_dispatch_timeout,
167 r = weston_dbus_adjust_timeout(timeout, s);
169 wl_event_source_remove(s);
173 dbus_timeout_set_data(timeout, s, NULL);
177 static void weston_dbus_remove_timeout(DBusTimeout *timeout, void *data)
179 struct wl_event_source *s;
181 s = dbus_timeout_get_data(timeout);
185 wl_event_source_remove(s);
188 static void weston_dbus_toggle_timeout(DBusTimeout *timeout, void *data)
190 struct wl_event_source *s;
192 s = dbus_timeout_get_data(timeout);
196 weston_dbus_adjust_timeout(timeout, s);
199 static int weston_dbus_dispatch(int fd, uint32_t mask, void *data)
201 DBusConnection *c = data;
205 r = dbus_connection_dispatch(c);
206 if (r == DBUS_DISPATCH_COMPLETE)
208 else if (r == DBUS_DISPATCH_DATA_REMAINS)
210 else if (r == DBUS_DISPATCH_NEED_MEMORY)
214 } while (r == -EAGAIN);
217 weston_log("cannot dispatch dbus events: %d\n", r);
222 static int weston_dbus_bind(struct wl_event_loop *loop, DBusConnection *c,
223 struct wl_event_source **ctx_out)
228 /* Idle events cannot reschedule themselves, therefore we use a dummy
229 * event-fd and mark it for post-dispatch. Hence, the dbus
230 * dispatcher is called after every dispatch-round.
231 * This is required as dbus doesn't allow dispatching events from
232 * within its own event sources. */
233 fd = eventfd(0, EFD_CLOEXEC);
237 *ctx_out = wl_event_loop_add_fd(loop, fd, 0, weston_dbus_dispatch, c);
243 wl_event_source_check(*ctx_out);
245 b = dbus_connection_set_watch_functions(c,
246 weston_dbus_add_watch,
247 weston_dbus_remove_watch,
248 weston_dbus_toggle_watch,
256 b = dbus_connection_set_timeout_functions(c,
257 weston_dbus_add_timeout,
258 weston_dbus_remove_timeout,
259 weston_dbus_toggle_timeout,
267 dbus_connection_ref(c);
271 dbus_connection_set_timeout_functions(c, NULL, NULL, NULL,
273 dbus_connection_set_watch_functions(c, NULL, NULL, NULL,
275 wl_event_source_remove(*ctx_out);
280 static void weston_dbus_unbind(DBusConnection *c, struct wl_event_source *ctx)
282 dbus_connection_set_timeout_functions(c, NULL, NULL, NULL,
284 dbus_connection_set_watch_functions(c, NULL, NULL, NULL,
286 dbus_connection_unref(c);
287 wl_event_source_remove(ctx);
291 * Convenience Helpers
292 * Several convenience helpers are provided to make using dbus in weston
293 * easier. We don't use any of the gdbus or qdbus helpers as they pull in
294 * huge dependencies and actually are quite awful to use. Instead, we only
295 * use the basic low-level libdbus library.
298 int weston_dbus_open(struct wl_event_loop *loop, DBusBusType bus,
299 DBusConnection **out, struct wl_event_source **ctx_out)
304 /* Ihhh, global state.. stupid dbus. */
305 dbus_connection_set_change_sigpipe(FALSE);
307 /* This is actually synchronous. It blocks for some authentication and
308 * setup. We just trust the dbus-server here and accept this blocking
309 * call. There is no real reason to complicate things further and make
310 * this asynchronous/non-blocking. A context should be created during
311 * thead/process/app setup, so blocking calls should be fine. */
312 c = dbus_bus_get_private(bus, NULL);
316 dbus_connection_set_exit_on_disconnect(c, FALSE);
318 r = weston_dbus_bind(loop, c, ctx_out);
326 dbus_connection_close(c);
327 dbus_connection_unref(c);
331 void weston_dbus_close(DBusConnection *c, struct wl_event_source *ctx)
333 weston_dbus_unbind(c, ctx);
334 dbus_connection_close(c);
335 dbus_connection_unref(c);
338 int weston_dbus_add_match(DBusConnection *c, const char *format, ...)
345 va_start(list, format);
346 r = vasprintf(&str, format, list);
352 dbus_error_init(&err);
353 dbus_bus_add_match(c, str, &err);
355 if (dbus_error_is_set(&err)) {
356 dbus_error_free(&err);
363 int weston_dbus_add_match_signal(DBusConnection *c, const char *sender,
364 const char *iface, const char *member,
367 return weston_dbus_add_match(c,
373 sender, iface, member, path);
376 void weston_dbus_remove_match(DBusConnection *c, const char *format, ...)
382 va_start(list, format);
383 r = vasprintf(&str, format, list);
389 dbus_bus_remove_match(c, str, NULL);
393 void weston_dbus_remove_match_signal(DBusConnection *c, const char *sender,
394 const char *iface, const char *member,
397 return weston_dbus_remove_match(c,
403 sender, iface, member, path);