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 * @defgroup DBusGLib GLib bindings
31 * @brief API for using D-BUS with GLib
33 * Convenience functions are provided for using D-BUS
34 * with the GLib library (see http://www.gtk.org for GLib
40 * @defgroup DBusGLibInternals GLib bindings implementation details
41 * @ingroup DBusInternals
42 * @brief Implementation details of GLib bindings
47 /** @typedef DBusGSource
48 * A GSource representing a #DBusConnection or #DBusServer
50 typedef struct DBusGSource DBusGSource;
54 GSource source; /**< the parent GSource */
56 GList *poll_fds; /**< descriptors we're watching */
57 GHashTable *watches; /**< hash of DBusWatch objects */
59 GMainContext *context; /**< the GMainContext to use, NULL for default */
61 void *connection_or_server; /**< DBusConnection or DBusServer */
64 static dbus_int32_t connection_slot = -1;
65 static dbus_int32_t server_slot = -1;
67 static gboolean gsource_connection_prepare (GSource *source,
69 static gboolean gsource_connection_check (GSource *source);
70 static gboolean gsource_connection_dispatch (GSource *source,
73 static gboolean gsource_server_prepare (GSource *source,
75 static gboolean gsource_server_check (GSource *source);
76 static gboolean gsource_server_dispatch (GSource *source,
80 static GSourceFuncs dbus_connection_funcs = {
81 gsource_connection_prepare,
82 gsource_connection_check,
83 gsource_connection_dispatch,
87 static GSourceFuncs dbus_server_funcs = {
88 gsource_server_prepare,
90 gsource_server_dispatch,
95 gsource_connection_prepare (GSource *source,
98 DBusConnection *connection = ((DBusGSource *)source)->connection_or_server;
102 return (dbus_connection_get_dispatch_status (connection) == DBUS_DISPATCH_DATA_REMAINS);
106 gsource_server_prepare (GSource *source,
115 dbus_gsource_check (GSource *source)
117 DBusGSource *dbus_source = (DBusGSource *)source;
120 list = dbus_source->poll_fds;
124 GPollFD *poll_fd = list->data;
126 if (poll_fd->revents != 0)
136 gsource_connection_check (GSource *source)
138 return dbus_gsource_check (source);
142 gsource_server_check (GSource *source)
144 return dbus_gsource_check (source);
148 dbus_gsource_dispatch (GSource *source,
149 GSourceFunc callback,
151 dbus_bool_t is_server)
153 DBusGSource *dbus_source = (DBusGSource *)source;
156 /* We need to traverse a copy of the list, since it can change in
157 dbus_watch_handle(). */
158 copy = g_list_copy (dbus_source->poll_fds);
163 GPollFD *poll_fd = list->data;
165 if (poll_fd->revents != 0)
167 DBusWatch *watch = g_hash_table_lookup (dbus_source->watches, poll_fd);
170 if (poll_fd->revents & G_IO_IN)
171 condition |= DBUS_WATCH_READABLE;
172 if (poll_fd->revents & G_IO_OUT)
173 condition |= DBUS_WATCH_WRITABLE;
174 if (poll_fd->revents & G_IO_ERR)
175 condition |= DBUS_WATCH_ERROR;
176 if (poll_fd->revents & G_IO_HUP)
177 condition |= DBUS_WATCH_HANGUP;
179 dbus_watch_handle (watch, condition);
191 gsource_connection_dispatch (GSource *source,
192 GSourceFunc callback,
195 DBusGSource *dbus_source = (DBusGSource *)source;
196 DBusConnection *connection = dbus_source->connection_or_server;
198 dbus_connection_ref (connection);
200 dbus_gsource_dispatch (source, callback, user_data,
203 /* Dispatch messages */
204 while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS)
207 dbus_connection_unref (connection);
213 gsource_server_dispatch (GSource *source,
214 GSourceFunc callback,
217 DBusGSource *dbus_source = (DBusGSource *)source;
218 DBusServer *server = dbus_source->connection_or_server;
220 dbus_server_ref (server);
222 dbus_gsource_dispatch (source, callback, user_data,
225 dbus_server_unref (server);
231 add_watch (DBusWatch *watch,
235 DBusGSource *dbus_source;
238 if (!dbus_watch_get_enabled (watch))
243 poll_fd = g_new (GPollFD, 1);
244 poll_fd->fd = dbus_watch_get_fd (watch);
246 flags = dbus_watch_get_flags (watch);
247 dbus_watch_set_data (watch, poll_fd, NULL);
249 if (flags & DBUS_WATCH_READABLE)
250 poll_fd->events |= G_IO_IN;
251 if (flags & DBUS_WATCH_WRITABLE)
252 poll_fd->events |= G_IO_OUT;
253 poll_fd->events |= G_IO_ERR | G_IO_HUP;
255 g_source_add_poll ((GSource *)dbus_source, poll_fd);
257 dbus_source->poll_fds = g_list_prepend (dbus_source->poll_fds, poll_fd);
258 g_hash_table_insert (dbus_source->watches, poll_fd, watch);
264 remove_watch (DBusWatch *watch,
267 DBusGSource *dbus_source = data;
270 poll_fd = dbus_watch_get_data (watch);
272 return; /* probably a not-enabled watch that was added */
274 dbus_source->poll_fds = g_list_remove (dbus_source->poll_fds, poll_fd);
275 g_hash_table_remove (dbus_source->watches, poll_fd);
276 g_source_remove_poll ((GSource *)dbus_source, poll_fd);
278 dbus_watch_set_data (watch, NULL, NULL); /* needed due to watch_toggled
279 * breaking add/remove symmetry
286 watch_toggled (DBusWatch *watch,
289 /* Because we just exit on OOM, enable/disable is
290 * no different from add/remove
292 if (dbus_watch_get_enabled (watch))
293 add_watch (watch, data);
295 remove_watch (watch, data);
299 timeout_handler (gpointer data)
301 DBusTimeout *timeout = data;
303 dbus_timeout_handle (timeout);
309 add_timeout (DBusTimeout *timeout,
312 DBusGSource *dbus_source = data;
315 if (!dbus_timeout_get_enabled (timeout))
318 source = g_timeout_source_new (dbus_timeout_get_interval (timeout));
319 g_source_set_callback (source, timeout_handler, timeout, NULL);
320 g_source_attach (source, dbus_source->context);
322 dbus_timeout_set_data (timeout, GUINT_TO_POINTER (g_source_get_id (source)),
329 remove_timeout (DBusTimeout *timeout,
334 timeout_tag = GPOINTER_TO_UINT (dbus_timeout_get_data (timeout));
336 if (timeout_tag != 0) /* if 0, probably timeout was disabled */
337 g_source_remove (timeout_tag);
341 timeout_toggled (DBusTimeout *timeout,
344 /* Because we just exit on OOM, enable/disable is
345 * no different from add/remove
347 if (dbus_timeout_get_enabled (timeout))
348 add_timeout (timeout, data);
350 remove_timeout (timeout, data);
355 free_source (GSource *source)
357 g_source_destroy (source);
361 wakeup_main (void *data)
363 DBusGSource *dbus_source = data;
365 g_main_context_wakeup (dbus_source->context);
369 /** @} */ /* End of GLib bindings internals */
371 /** @addtogroup DBusGLib
376 create_source (void *connection_or_server,
378 GMainContext *context)
381 DBusGSource *dbus_source;
383 source = g_source_new (funcs, sizeof (DBusGSource));
385 dbus_source = (DBusGSource *)source;
386 dbus_source->watches = g_hash_table_new (NULL, NULL);
387 dbus_source->connection_or_server = connection_or_server;
388 dbus_source->context = context;
394 * Sets the watch and timeout functions of a #DBusConnection
395 * to integrate the connection with the GLib main loop.
396 * Pass in #NULL for the #GMainContext unless you're
397 * doing something specialized.
399 * @param connection the connection
400 * @param context the #GMainContext or #NULL for default context
403 dbus_connection_setup_with_g_main (DBusConnection *connection,
404 GMainContext *context)
408 source = create_source (connection, &dbus_connection_funcs, context);
410 if (!dbus_connection_set_watch_functions (connection,
417 if (!dbus_connection_set_timeout_functions (connection,
424 dbus_connection_set_wakeup_main_function (connection,
428 g_source_attach (source, context);
430 /* FIXME we never free the slot, so its refcount just keeps growing,
431 * which is kind of broken.
433 dbus_connection_allocate_data_slot (&connection_slot);
434 if (connection_slot < 0)
437 if (!dbus_connection_set_data (connection, connection_slot, source,
438 (DBusFreeFunction)free_source))
444 g_error ("Not enough memory to set up DBusConnection for use with GLib");
448 * Sets the watch and timeout functions of a #DBusServer
449 * to integrate the server with the GLib main loop.
450 * In most cases the context argument should be #NULL.
452 * @param server the server
453 * @param context the #GMainContext or #NULL for default
456 dbus_server_setup_with_g_main (DBusServer *server,
457 GMainContext *context)
461 source = create_source (server, &dbus_server_funcs, context);
463 dbus_server_set_watch_functions (server,
469 dbus_server_set_timeout_functions (server,
475 g_source_attach (source, context);
477 /* FIXME we never free the slot, so its refcount just keeps growing,
478 * which is kind of broken.
480 dbus_server_allocate_data_slot (&server_slot);
484 if (!dbus_server_set_data (server, server_slot, source,
485 (DBusFreeFunction)free_source))
491 g_error ("Not enough memory to set up DBusServer for use with GLib");
494 /** @} */ /* end of public API */
496 #ifdef DBUS_BUILD_TESTS
499 * @ingroup DBusGLibInternals
500 * Unit test for GLib main loop integration
501 * @returns #TRUE on success.
504 _dbus_gmain_test (const char *test_data_dir)
510 #endif /* DBUS_BUILD_TESTS */