5 * Copyright (C) 2004-2009 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
31 #include <dbus/dbus.h>
33 #ifdef NEED_DBUS_WATCH_GET_UNIX_FD
34 #define dbus_watch_get_unix_fd dbus_watch_get_fd
39 #define DISPATCH_TIMEOUT 0
62 struct disconnect_data {
63 GDBusWatchFunction disconnect_cb;
67 static gboolean disconnected_signal(DBusConnection *conn,
68 DBusMessage *msg, void *data)
70 struct disconnect_data *dc_data = data;
72 error("Got disconnected from the system message bus");
73 dc_data->disconnect_cb(conn, dc_data->user_data);
74 dbus_connection_unref(conn);
79 static gboolean message_dispatch_cb(void *data)
81 DBusConnection *connection = data;
83 dbus_connection_ref(connection);
85 /* Dispatch messages */
86 while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS);
88 dbus_connection_unref(connection);
93 static gboolean watch_func(GIOChannel *chan, GIOCondition cond, gpointer data)
95 DBusWatch *watch = data;
96 struct watch_info *info = dbus_watch_get_data(watch);
99 if (cond & G_IO_IN) flags |= DBUS_WATCH_READABLE;
100 if (cond & G_IO_OUT) flags |= DBUS_WATCH_WRITABLE;
101 if (cond & G_IO_HUP) flags |= DBUS_WATCH_HANGUP;
102 if (cond & G_IO_ERR) flags |= DBUS_WATCH_ERROR;
104 dbus_watch_handle(watch, flags);
106 if (dbus_connection_get_dispatch_status(info->conn) == DBUS_DISPATCH_DATA_REMAINS)
107 g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, info->conn);
112 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
114 GIOCondition cond = G_IO_HUP | G_IO_ERR;
115 DBusConnection *conn = data;
116 struct watch_info *info;
119 if (!dbus_watch_get_enabled(watch))
122 info = g_new(struct watch_info, 1);
124 fd = dbus_watch_get_unix_fd(watch);
125 info->io = g_io_channel_unix_new(fd);
126 info->conn = dbus_connection_ref(conn);
128 dbus_watch_set_data(watch, info, NULL);
130 flags = dbus_watch_get_flags(watch);
132 if (flags & DBUS_WATCH_READABLE) cond |= G_IO_IN;
133 if (flags & DBUS_WATCH_WRITABLE) cond |= G_IO_OUT;
135 info->watch_id = g_io_add_watch(info->io, cond, watch_func, watch);
140 static void remove_watch(DBusWatch *watch, void *data)
142 struct watch_info *info = dbus_watch_get_data(watch);
144 dbus_watch_set_data(watch, NULL, NULL);
147 g_source_remove(info->watch_id);
148 g_io_channel_unref(info->io);
149 dbus_connection_unref(info->conn);
154 static void watch_toggled(DBusWatch *watch, void *data)
156 /* Because we just exit on OOM, enable/disable is
157 * no different from add/remove */
158 if (dbus_watch_get_enabled(watch))
159 add_watch(watch, data);
161 remove_watch(watch, data);
164 static gboolean timeout_handler_dispatch(gpointer data)
166 timeout_handler_t *handler = data;
168 /* if not enabled should not be polled by the main loop */
169 if (dbus_timeout_get_enabled(handler->timeout) != TRUE)
172 dbus_timeout_handle(handler->timeout);
177 static void timeout_handler_free(void *data)
179 timeout_handler_t *handler = data;
183 if (handler->id > 0) {
184 g_source_remove(handler->id);
191 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
193 timeout_handler_t *handler;
195 if (!dbus_timeout_get_enabled(timeout))
198 handler = g_new0(timeout_handler_t, 1);
200 handler->timeout = timeout;
201 handler->id = g_timeout_add(dbus_timeout_get_interval(timeout),
202 timeout_handler_dispatch, handler);
204 dbus_timeout_set_data(timeout, handler, timeout_handler_free);
209 static void remove_timeout(DBusTimeout *timeout, void *data)
211 timeout_handler_t *handler;
213 handler = dbus_timeout_get_data(timeout);
218 if (handler->id > 0) {
219 g_source_remove(handler->id);
224 static void timeout_toggled(DBusTimeout *timeout, void *data)
226 if (dbus_timeout_get_enabled(timeout))
227 add_timeout(timeout, data);
229 remove_timeout(timeout, data);
232 static void dispatch_status_cb(DBusConnection *conn,
233 DBusDispatchStatus new_status, void *data)
235 if (!dbus_connection_get_is_connected(conn))
238 if (new_status == DBUS_DISPATCH_DATA_REMAINS)
239 g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, data);
242 static void setup_dbus_with_main_loop(DBusConnection *conn)
244 dbus_connection_set_watch_functions(conn, add_watch, remove_watch,
245 watch_toggled, conn, NULL);
247 dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout,
248 timeout_toggled, conn, NULL);
250 dbus_connection_set_dispatch_status_function(conn, dispatch_status_cb,
254 DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name,
257 DBusConnection *conn;
259 conn = dbus_bus_get(type, error);
262 if (dbus_error_is_set(error) == TRUE)
270 if (dbus_bus_request_name(conn, name,
271 DBUS_NAME_FLAG_DO_NOT_QUEUE, error) !=
272 DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER ) {
273 dbus_connection_unref(conn);
278 if (dbus_error_is_set(error) == TRUE) {
279 dbus_connection_unref(conn);
285 setup_dbus_with_main_loop(conn);
287 if (dbus_connection_get_dispatch_status(conn) == DBUS_DISPATCH_DATA_REMAINS)
288 g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, conn);
293 gboolean g_dbus_request_name(DBusConnection *connection, const char *name,
299 gboolean g_dbus_set_disconnect_function(DBusConnection *connection,
300 GDBusWatchFunction function,
301 void *user_data, DBusFreeFunction destroy)
303 struct disconnect_data *dc_data;
305 dc_data = g_new(struct disconnect_data, 1);
307 dc_data->disconnect_cb = function;
308 dc_data->user_data = user_data;
310 dbus_connection_set_exit_on_disconnect(connection, FALSE);
312 if (g_dbus_add_signal_watch(connection, NULL, NULL,
313 DBUS_INTERFACE_LOCAL, "Disconnected",
314 disconnected_signal, dc_data,
316 error("Can't add watch for D-Bus Disconnected signal\n");