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 DBusHandlerResult disconnect_filter(DBusConnection *conn,
68 DBusMessage *msg, void *data)
70 struct disconnect_data *dc_data = data;
72 if (dbus_message_is_signal(msg,
73 DBUS_INTERFACE_LOCAL, "Disconnected") == TRUE) {
74 error("Got disconnected from the system message bus");
75 dc_data->disconnect_cb(conn, dc_data->user_data);
76 dbus_connection_unref(conn);
79 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
82 static gboolean message_dispatch_cb(void *data)
84 DBusConnection *connection = data;
86 dbus_connection_ref(connection);
88 /* Dispatch messages */
89 while (dbus_connection_dispatch(connection) == DBUS_DISPATCH_DATA_REMAINS);
91 dbus_connection_unref(connection);
96 static gboolean watch_func(GIOChannel *chan, GIOCondition cond, gpointer data)
98 DBusWatch *watch = data;
99 struct watch_info *info = dbus_watch_get_data(watch);
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(watch, flags);
109 if (dbus_connection_get_dispatch_status(info->conn) == DBUS_DISPATCH_DATA_REMAINS)
110 g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, info->conn);
115 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
117 GIOCondition cond = G_IO_HUP | G_IO_ERR;
118 DBusConnection *conn = data;
119 struct watch_info *info;
122 if (!dbus_watch_get_enabled(watch))
125 info = g_new(struct watch_info, 1);
127 fd = dbus_watch_get_unix_fd(watch);
128 info->io = g_io_channel_unix_new(fd);
129 info->conn = dbus_connection_ref(conn);
131 dbus_watch_set_data(watch, info, NULL);
133 flags = dbus_watch_get_flags(watch);
135 if (flags & DBUS_WATCH_READABLE) cond |= G_IO_IN;
136 if (flags & DBUS_WATCH_WRITABLE) cond |= G_IO_OUT;
138 info->watch_id = g_io_add_watch(info->io, cond, watch_func, watch);
143 static void remove_watch(DBusWatch *watch, void *data)
145 struct watch_info *info = dbus_watch_get_data(watch);
147 dbus_watch_set_data(watch, NULL, NULL);
150 g_source_remove(info->watch_id);
151 g_io_channel_unref(info->io);
152 dbus_connection_unref(info->conn);
157 static void watch_toggled(DBusWatch *watch, void *data)
159 /* Because we just exit on OOM, enable/disable is
160 * no different from add/remove */
161 if (dbus_watch_get_enabled(watch))
162 add_watch(watch, data);
164 remove_watch(watch, data);
167 static gboolean timeout_handler_dispatch(gpointer data)
169 timeout_handler_t *handler = data;
171 /* if not enabled should not be polled by the main loop */
172 if (dbus_timeout_get_enabled(handler->timeout) != TRUE)
175 dbus_timeout_handle(handler->timeout);
180 static void timeout_handler_free(void *data)
182 timeout_handler_t *handler = data;
186 g_source_remove(handler->id);
190 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
192 timeout_handler_t *handler;
194 if (!dbus_timeout_get_enabled(timeout))
197 handler = g_new0(timeout_handler_t, 1);
199 handler->timeout = timeout;
200 handler->id = g_timeout_add(dbus_timeout_get_interval(timeout),
201 timeout_handler_dispatch, handler);
203 dbus_timeout_set_data(timeout, handler, timeout_handler_free);
208 static void remove_timeout(DBusTimeout *timeout, void *data)
212 static void timeout_toggled(DBusTimeout *timeout, void *data)
214 if (dbus_timeout_get_enabled(timeout))
215 add_timeout(timeout, data);
217 remove_timeout(timeout, data);
220 static void dispatch_status_cb(DBusConnection *conn,
221 DBusDispatchStatus new_status, void *data)
223 if (!dbus_connection_get_is_connected(conn))
226 if (new_status == DBUS_DISPATCH_DATA_REMAINS)
227 g_timeout_add(DISPATCH_TIMEOUT, message_dispatch_cb, data);
230 static void setup_dbus_with_main_loop(DBusConnection *conn)
232 dbus_connection_set_watch_functions(conn, add_watch, remove_watch,
233 watch_toggled, conn, NULL);
235 dbus_connection_set_timeout_functions(conn, add_timeout, remove_timeout,
236 timeout_toggled, conn, NULL);
238 dbus_connection_set_dispatch_status_function(conn, dispatch_status_cb,
242 DBusConnection *g_dbus_setup_bus(DBusBusType type, const char *name,
245 DBusConnection *conn;
247 conn = dbus_bus_get(type, error);
250 if (dbus_error_is_set(error) == TRUE)
258 if (dbus_bus_request_name(conn, name,
259 DBUS_NAME_FLAG_DO_NOT_QUEUE, error) !=
260 DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER ) {
261 dbus_connection_unref(conn);
266 if (dbus_error_is_set(error) == TRUE) {
267 dbus_connection_unref(conn);
273 setup_dbus_with_main_loop(conn);
278 gboolean g_dbus_request_name(DBusConnection *connection, const char *name,
284 gboolean g_dbus_check_service(DBusConnection *connection, const char *name)
286 DBusMessage *message, *reply;
289 gboolean result = FALSE;
291 message = dbus_message_new_method_call(DBUS_SERVICE_DBUS,
292 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "ListNames");
293 if (message == NULL) {
294 error("Can't allocate new message");
298 reply = dbus_connection_send_with_reply_and_block(connection,
301 dbus_message_unref(message);
304 error("Failed to execute method call");
308 if (dbus_message_get_args(reply, NULL,
309 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
310 &names, &count, DBUS_TYPE_INVALID) == FALSE) {
311 error("Failed to read name list");
315 for (i = 0; i < count; i++)
316 if (g_str_equal(names[i], name) == TRUE) {
322 dbus_message_unref(reply);
327 gboolean g_dbus_set_disconnect_function(DBusConnection *connection,
328 GDBusWatchFunction function,
329 void *user_data, DBusFreeFunction destroy)
331 struct disconnect_data *dc_data;
333 dc_data = g_new(struct disconnect_data, 1);
335 dc_data->disconnect_cb = function;
336 dc_data->user_data = user_data;
338 dbus_connection_set_exit_on_disconnect(connection, FALSE);
340 if (dbus_connection_add_filter(connection, disconnect_filter,
341 dc_data, g_free) == FALSE) {
342 error("Can't add D-Bus disconnect filter");