5 * Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include <dbus/dbus.h>
31 #ifdef NEED_DBUS_WATCH_GET_UNIX_FD
32 #define dbus_watch_get_unix_fd dbus_watch_get_fd
37 #define DISPATCH_TIMEOUT 0
43 struct timeout_handler {
54 struct disconnect_data {
55 GDBusWatchFunction function;
59 static gboolean disconnected_signal(DBusConnection *conn,
60 DBusMessage *msg, void *data)
62 struct disconnect_data *dc_data = data;
64 error("Got disconnected from the system message bus");
66 dc_data->function(conn, dc_data->user_data);
68 dbus_connection_unref(conn);
73 static gboolean message_dispatch(void *data)
75 DBusConnection *conn = data;
77 dbus_connection_ref(conn);
79 /* Dispatch messages */
80 while (dbus_connection_dispatch(conn) == DBUS_DISPATCH_DATA_REMAINS);
82 dbus_connection_unref(conn);
87 static inline void queue_dispatch(DBusConnection *conn,
88 DBusDispatchStatus status)
90 if (status == DBUS_DISPATCH_DATA_REMAINS)
91 g_timeout_add(DISPATCH_TIMEOUT, message_dispatch, conn);
94 static gboolean watch_func(GIOChannel *chan, GIOCondition cond, gpointer data)
96 struct watch_info *info = data;
97 unsigned int flags = 0;
98 DBusDispatchStatus status;
100 dbus_connection_ref(info->conn);
102 if (cond & G_IO_IN) flags |= DBUS_WATCH_READABLE;
103 if (cond & G_IO_OUT) flags |= DBUS_WATCH_WRITABLE;
104 if (cond & G_IO_HUP) flags |= DBUS_WATCH_HANGUP;
105 if (cond & G_IO_ERR) flags |= DBUS_WATCH_ERROR;
107 dbus_watch_handle(info->watch, flags);
109 status = dbus_connection_get_dispatch_status(info->conn);
110 queue_dispatch(info->conn, status);
112 dbus_connection_unref(info->conn);
117 static void watch_info_free(void *data)
119 struct watch_info *info = data;
122 g_source_remove(info->id);
126 dbus_connection_unref(info->conn);
131 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
133 DBusConnection *conn = data;
134 GIOCondition cond = G_IO_HUP | G_IO_ERR;
136 struct watch_info *info;
140 if (!dbus_watch_get_enabled(watch))
143 info = g_new0(struct watch_info, 1);
145 fd = dbus_watch_get_unix_fd(watch);
146 chan = g_io_channel_unix_new(fd);
149 info->conn = dbus_connection_ref(conn);
151 dbus_watch_set_data(watch, info, watch_info_free);
153 flags = dbus_watch_get_flags(watch);
155 if (flags & DBUS_WATCH_READABLE) cond |= G_IO_IN;
156 if (flags & DBUS_WATCH_WRITABLE) cond |= G_IO_OUT;
158 info->id = g_io_add_watch(chan, cond, watch_func, info);
160 g_io_channel_unref(chan);
165 static void remove_watch(DBusWatch *watch, void *data)
167 if (dbus_watch_get_enabled(watch))
170 /* will trigger watch_info_free() */
171 dbus_watch_set_data(watch, NULL, NULL);
174 static void watch_toggled(DBusWatch *watch, void *data)
176 /* Because we just exit on OOM, enable/disable is
177 * no different from add/remove */
178 if (dbus_watch_get_enabled(watch))
179 add_watch(watch, data);
181 remove_watch(watch, data);
184 static gboolean timeout_handler_dispatch(gpointer data)
186 struct timeout_handler *handler = data;
190 /* if not enabled should not be polled by the main loop */
191 if (!dbus_timeout_get_enabled(handler->timeout))
194 dbus_timeout_handle(handler->timeout);
199 static void timeout_handler_free(void *data)
201 struct timeout_handler *handler = data;
203 if (handler->id > 0) {
204 g_source_remove(handler->id);
211 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
213 int interval = dbus_timeout_get_interval(timeout);
214 struct timeout_handler *handler;
216 if (!dbus_timeout_get_enabled(timeout))
219 handler = g_new0(struct timeout_handler, 1);
221 handler->timeout = timeout;
223 dbus_timeout_set_data(timeout, handler, timeout_handler_free);
225 handler->id = g_timeout_add(interval, timeout_handler_dispatch,
231 static void remove_timeout(DBusTimeout *timeout, void *data)
233 /* will trigger timeout_handler_free() */
234 dbus_timeout_set_data(timeout, NULL, NULL);
237 static void timeout_toggled(DBusTimeout *timeout, void *data)
239 if (dbus_timeout_get_enabled(timeout))
240 add_timeout(timeout, data);
242 remove_timeout(timeout, data);
245 static void dispatch_status(DBusConnection *conn,
246 DBusDispatchStatus status, void *data)
248 if (!dbus_connection_get_is_connected(conn))
251 queue_dispatch(conn, status);
254 static inline void setup_dbus_with_main_loop(DBusConnection *conn)
256 dbus_connection_set_watch_functions(conn, add_watch, remove_watch,
257 watch_toggled, conn, NULL);
259 dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout,
260 timeout_toggled, NULL, NULL);
262 dbus_connection_set_dispatch_status_function(conn, dispatch_status,
266 static gboolean setup_bus(DBusConnection *conn, const char *name,
270 DBusDispatchStatus status;
273 result = g_dbus_request_name(conn, name, error);
276 if (dbus_error_is_set(error) == TRUE)
284 setup_dbus_with_main_loop(conn);
286 status = dbus_connection_get_dispatch_status(conn);
287 queue_dispatch(conn, status);
292 DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name,
295 DBusConnection *conn;
297 conn = dbus_bus_get(type, error);
300 if (dbus_error_is_set(error) == TRUE)
307 if (setup_bus(conn, name, error) == FALSE) {
308 dbus_connection_unref(conn);
315 DBusConnection *g_dbus_setup_private(DBusBusType type, const char *name,
318 DBusConnection *conn;
320 conn = dbus_bus_get_private(type, error);
323 if (dbus_error_is_set(error) == TRUE)
330 if (setup_bus(conn, name, error) == FALSE) {
331 dbus_connection_unref(conn);
338 gboolean g_dbus_request_name(DBusConnection *connection, const char *name,
343 result = dbus_bus_request_name(connection, name,
344 DBUS_NAME_FLAG_DO_NOT_QUEUE, error);
347 if (dbus_error_is_set(error) == TRUE)
351 if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
353 dbus_set_error(error, name, "Name already in use");
361 gboolean g_dbus_set_disconnect_function(DBusConnection *connection,
362 GDBusWatchFunction function,
363 void *user_data, DBusFreeFunction destroy)
365 struct disconnect_data *dc_data;
367 dc_data = g_new0(struct disconnect_data, 1);
369 dc_data->function = function;
370 dc_data->user_data = user_data;
372 dbus_connection_set_exit_on_disconnect(connection, FALSE);
374 if (g_dbus_add_signal_watch(connection, NULL, NULL,
375 DBUS_INTERFACE_LOCAL, "Disconnected",
376 disconnected_signal, dc_data, g_free) == 0) {
377 error("Failed to add watch for D-Bus Disconnected signal");