1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gmain.c GLib main loop integration
4 * Copyright (C) 2002, 2003 CodeFactory AB
6 * Licensed under the Academic Free License version 1.2
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-glib.h"
26 #include "dbus-gtest.h"
29 #define _(x) dgettext (GETTEXT_PACKAGE, x)
33 * @defgroup DBusGLib GLib bindings
35 * @brief API for using D-BUS with GLib
37 * Convenience functions are provided for using D-BUS
38 * with the GLib library (see http://www.gtk.org for GLib
44 * @defgroup DBusGLibInternals GLib bindings implementation details
45 * @ingroup DBusInternals
46 * @brief Implementation details of GLib bindings
51 /** @typedef DBusGSource
52 * A GSource representing a #DBusConnection or #DBusServer
54 typedef struct DBusGSource DBusGSource;
57 * A GSource subclass for a DBusConnection.
61 GSource source; /**< the parent GSource */
63 GList *poll_fds; /**< descriptors we're watching */
64 GHashTable *watches; /**< hash of DBusWatch objects */
66 GMainContext *context; /**< the GMainContext to use, NULL for default */
68 void *connection_or_server; /**< DBusConnection or DBusServer */
71 static dbus_int32_t connection_slot = -1;
72 static dbus_int32_t server_slot = -1;
74 static gboolean gsource_connection_prepare (GSource *source,
76 static gboolean gsource_connection_check (GSource *source);
77 static gboolean gsource_connection_dispatch (GSource *source,
80 static gboolean gsource_server_prepare (GSource *source,
82 static gboolean gsource_server_check (GSource *source);
83 static gboolean gsource_server_dispatch (GSource *source,
87 static GSourceFuncs dbus_connection_funcs = {
88 gsource_connection_prepare,
89 gsource_connection_check,
90 gsource_connection_dispatch,
94 static GSourceFuncs dbus_server_funcs = {
95 gsource_server_prepare,
97 gsource_server_dispatch,
102 gsource_connection_prepare (GSource *source,
105 DBusConnection *connection = ((DBusGSource *)source)->connection_or_server;
109 return (dbus_connection_get_dispatch_status (connection) == DBUS_DISPATCH_DATA_REMAINS);
113 gsource_server_prepare (GSource *source,
122 dbus_gsource_check (GSource *source)
124 DBusGSource *dbus_source = (DBusGSource *)source;
127 list = dbus_source->poll_fds;
131 GPollFD *poll_fd = list->data;
133 if (poll_fd->revents != 0)
143 gsource_connection_check (GSource *source)
145 return dbus_gsource_check (source);
149 gsource_server_check (GSource *source)
151 return dbus_gsource_check (source);
155 dbus_gsource_dispatch (GSource *source,
156 GSourceFunc callback,
158 dbus_bool_t is_server)
160 DBusGSource *dbus_source = (DBusGSource *)source;
163 /* We need to traverse a copy of the list, since it can change in
164 dbus_watch_handle(). */
165 copy = g_list_copy (dbus_source->poll_fds);
170 GPollFD *poll_fd = list->data;
172 if (poll_fd->revents != 0)
174 DBusWatch *watch = g_hash_table_lookup (dbus_source->watches, poll_fd);
177 if (poll_fd->revents & G_IO_IN)
178 condition |= DBUS_WATCH_READABLE;
179 if (poll_fd->revents & G_IO_OUT)
180 condition |= DBUS_WATCH_WRITABLE;
181 if (poll_fd->revents & G_IO_ERR)
182 condition |= DBUS_WATCH_ERROR;
183 if (poll_fd->revents & G_IO_HUP)
184 condition |= DBUS_WATCH_HANGUP;
186 dbus_watch_handle (watch, condition);
198 gsource_connection_dispatch (GSource *source,
199 GSourceFunc callback,
202 DBusGSource *dbus_source = (DBusGSource *)source;
203 DBusConnection *connection = dbus_source->connection_or_server;
205 dbus_connection_ref (connection);
207 dbus_gsource_dispatch (source, callback, user_data,
210 /* Dispatch messages */
211 while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS)
214 dbus_connection_unref (connection);
220 gsource_server_dispatch (GSource *source,
221 GSourceFunc callback,
224 DBusGSource *dbus_source = (DBusGSource *)source;
225 DBusServer *server = dbus_source->connection_or_server;
227 dbus_server_ref (server);
229 dbus_gsource_dispatch (source, callback, user_data,
232 dbus_server_unref (server);
238 add_watch (DBusWatch *watch,
242 DBusGSource *dbus_source;
245 if (!dbus_watch_get_enabled (watch))
250 poll_fd = g_new (GPollFD, 1);
251 poll_fd->fd = dbus_watch_get_fd (watch);
253 flags = dbus_watch_get_flags (watch);
254 dbus_watch_set_data (watch, poll_fd, NULL);
256 if (flags & DBUS_WATCH_READABLE)
257 poll_fd->events |= G_IO_IN;
258 if (flags & DBUS_WATCH_WRITABLE)
259 poll_fd->events |= G_IO_OUT;
260 poll_fd->events |= G_IO_ERR | G_IO_HUP;
262 g_source_add_poll ((GSource *)dbus_source, poll_fd);
264 dbus_source->poll_fds = g_list_prepend (dbus_source->poll_fds, poll_fd);
265 g_hash_table_insert (dbus_source->watches, poll_fd, watch);
271 remove_watch (DBusWatch *watch,
274 DBusGSource *dbus_source = data;
277 poll_fd = dbus_watch_get_data (watch);
279 return; /* probably a not-enabled watch that was added */
281 dbus_source->poll_fds = g_list_remove (dbus_source->poll_fds, poll_fd);
282 g_hash_table_remove (dbus_source->watches, poll_fd);
283 g_source_remove_poll ((GSource *)dbus_source, poll_fd);
285 dbus_watch_set_data (watch, NULL, NULL); /* needed due to watch_toggled
286 * breaking add/remove symmetry
293 watch_toggled (DBusWatch *watch,
296 /* Because we just exit on OOM, enable/disable is
297 * no different from add/remove
299 if (dbus_watch_get_enabled (watch))
300 add_watch (watch, data);
302 remove_watch (watch, data);
306 timeout_handler (gpointer data)
308 DBusTimeout *timeout = data;
310 dbus_timeout_handle (timeout);
316 add_timeout (DBusTimeout *timeout,
319 DBusGSource *dbus_source = data;
322 if (!dbus_timeout_get_enabled (timeout))
325 source = g_timeout_source_new (dbus_timeout_get_interval (timeout));
326 g_source_set_callback (source, timeout_handler, timeout, NULL);
327 g_source_attach (source, dbus_source->context);
329 dbus_timeout_set_data (timeout, GUINT_TO_POINTER (g_source_get_id (source)),
336 remove_timeout (DBusTimeout *timeout,
341 timeout_tag = GPOINTER_TO_UINT (dbus_timeout_get_data (timeout));
343 if (timeout_tag != 0) /* if 0, probably timeout was disabled */
344 g_source_remove (timeout_tag);
348 timeout_toggled (DBusTimeout *timeout,
351 /* Because we just exit on OOM, enable/disable is
352 * no different from add/remove
354 if (dbus_timeout_get_enabled (timeout))
355 add_timeout (timeout, data);
357 remove_timeout (timeout, data);
362 free_source (GSource *source)
364 g_source_destroy (source);
368 wakeup_main (void *data)
370 DBusGSource *dbus_source = data;
372 g_main_context_wakeup (dbus_source->context);
376 /** @} */ /* End of GLib bindings internals */
378 /** @addtogroup DBusGLib
383 create_source (void *connection_or_server,
385 GMainContext *context)
388 DBusGSource *dbus_source;
390 source = g_source_new (funcs, sizeof (DBusGSource));
392 dbus_source = (DBusGSource *)source;
393 dbus_source->watches = g_hash_table_new (NULL, NULL);
394 dbus_source->connection_or_server = connection_or_server;
395 dbus_source->context = context;
401 * Sets the watch and timeout functions of a #DBusConnection
402 * to integrate the connection with the GLib main loop.
403 * Pass in #NULL for the #GMainContext unless you're
404 * doing something specialized.
406 * @param connection the connection
407 * @param context the #GMainContext or #NULL for default context
410 dbus_connection_setup_with_g_main (DBusConnection *connection,
411 GMainContext *context)
415 source = create_source (connection, &dbus_connection_funcs, context);
417 if (!dbus_connection_set_watch_functions (connection,
424 if (!dbus_connection_set_timeout_functions (connection,
431 dbus_connection_set_wakeup_main_function (connection,
435 g_source_attach (source, context);
437 /* FIXME we never free the slot, so its refcount just keeps growing,
438 * which is kind of broken.
440 dbus_connection_allocate_data_slot (&connection_slot);
441 if (connection_slot < 0)
444 if (!dbus_connection_set_data (connection, connection_slot, source,
445 (DBusFreeFunction)free_source))
451 g_error ("Not enough memory to set up DBusConnection for use with GLib");
455 * Sets the watch and timeout functions of a #DBusServer
456 * to integrate the server with the GLib main loop.
457 * In most cases the context argument should be #NULL.
459 * @param server the server
460 * @param context the #GMainContext or #NULL for default
463 dbus_server_setup_with_g_main (DBusServer *server,
464 GMainContext *context)
468 source = create_source (server, &dbus_server_funcs, context);
470 dbus_server_set_watch_functions (server,
476 dbus_server_set_timeout_functions (server,
482 g_source_attach (source, context);
484 /* FIXME we never free the slot, so its refcount just keeps growing,
485 * which is kind of broken.
487 dbus_server_allocate_data_slot (&server_slot);
491 if (!dbus_server_set_data (server, server_slot, source,
492 (DBusFreeFunction)free_source))
498 g_error ("Not enough memory to set up DBusServer for use with GLib");
502 * The implementation of DBUS_GERROR error domain. See documentation
503 * for GError in GLib reference manual.
505 * @returns the error domain quark for use with GError
508 dbus_g_error_quark (void)
510 static GQuark quark = 0;
512 quark = g_quark_from_static_string ("g-exec-error-quark");
518 * Set a GError return location from a DBusError.
520 * @todo expand the DBUS_GERROR enum and take advantage of it here
522 * @param gerror location to store a GError, or #NULL
523 * @param derror the DBusError
526 dbus_set_g_error (GError **gerror,
529 g_return_if_fail (derror != NULL);
530 g_return_if_fail (dbus_error_is_set (derror));
532 g_set_error (gerror, DBUS_GERROR,
534 _("D-BUS error %s: %s"),
535 derror->name, derror->message);
538 /** @} */ /* end of public API */
540 #ifdef DBUS_BUILD_TESTS
543 * @ingroup DBusGLibInternals
544 * Unit test for GLib main loop integration
545 * @returns #TRUE on success.
548 _dbus_gmain_test (const char *test_data_dir)
554 #endif /* DBUS_BUILD_TESTS */