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
24 #include "dbus-glib.h"
28 * @defgroup DBusGLib GLib bindings
30 * @brief API for using D-BUS with GLib
32 * Convenience functions are provided for using D-BUS
33 * with the GLib library (see http://www.gtk.org for GLib
39 * @defgroup DBusGLibInternals GLib bindings implementation details
40 * @ingroup DBusInternals
41 * @brief Implementation details of GLib bindings
46 /** @typedef DBusGSource
47 * A GSource representing a #DBusConnection or #DBusServer
49 typedef struct DBusGSource DBusGSource;
53 GSource source; /**< the parent GSource */
55 GList *poll_fds; /**< descriptors we're watching */
56 GHashTable *watches; /**< hash of DBusWatch objects */
58 GMainContext *context; /**< the GMainContext to use, NULL for default */
60 void *connection_or_server; /**< DBusConnection or DBusServer */
63 static GStaticMutex connection_slot_lock = G_STATIC_MUTEX_INIT;
64 static int connection_slot = -1;
65 static GStaticMutex server_slot_lock = G_STATIC_MUTEX_INIT;
66 static int server_slot = -1;
68 static gboolean gsource_connection_prepare (GSource *source,
70 static gboolean gsource_connection_check (GSource *source);
71 static gboolean gsource_connection_dispatch (GSource *source,
74 static gboolean gsource_server_prepare (GSource *source,
76 static gboolean gsource_server_check (GSource *source);
77 static gboolean gsource_server_dispatch (GSource *source,
81 static GSourceFuncs dbus_connection_funcs = {
82 gsource_connection_prepare,
83 gsource_connection_check,
84 gsource_connection_dispatch,
88 static GSourceFuncs dbus_server_funcs = {
89 gsource_server_prepare,
91 gsource_server_dispatch,
96 gsource_connection_prepare (GSource *source,
99 DBusConnection *connection = ((DBusGSource *)source)->connection_or_server;
103 return (dbus_connection_get_dispatch_status (connection) == DBUS_DISPATCH_DATA_REMAINS);
107 gsource_server_prepare (GSource *source,
116 dbus_gsource_check (GSource *source)
118 DBusGSource *dbus_source = (DBusGSource *)source;
121 list = dbus_source->poll_fds;
125 GPollFD *poll_fd = list->data;
127 if (poll_fd->revents != 0)
137 gsource_connection_check (GSource *source)
139 return dbus_gsource_check (source);
143 gsource_server_check (GSource *source)
145 return dbus_gsource_check (source);
149 dbus_gsource_dispatch (GSource *source,
150 GSourceFunc callback,
152 dbus_bool_t is_server)
154 DBusGSource *dbus_source = (DBusGSource *)source;
157 /* We need to traverse a copy of the list, since it can change in
158 dbus_watch_handle(). */
159 copy = g_list_copy (dbus_source->poll_fds);
164 GPollFD *poll_fd = list->data;
166 if (poll_fd->revents != 0)
168 DBusWatch *watch = g_hash_table_lookup (dbus_source->watches, poll_fd);
171 if (poll_fd->revents & G_IO_IN)
172 condition |= DBUS_WATCH_READABLE;
173 if (poll_fd->revents & G_IO_OUT)
174 condition |= DBUS_WATCH_WRITABLE;
175 if (poll_fd->revents & G_IO_ERR)
176 condition |= DBUS_WATCH_ERROR;
177 if (poll_fd->revents & G_IO_HUP)
178 condition |= DBUS_WATCH_HANGUP;
180 dbus_watch_handle (watch, condition);
192 gsource_connection_dispatch (GSource *source,
193 GSourceFunc callback,
196 DBusGSource *dbus_source = (DBusGSource *)source;
197 DBusConnection *connection = dbus_source->connection_or_server;
199 dbus_connection_ref (connection);
201 dbus_gsource_dispatch (source, callback, user_data,
204 /* Dispatch messages */
205 while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS)
208 dbus_connection_unref (connection);
214 gsource_server_dispatch (GSource *source,
215 GSourceFunc callback,
218 DBusGSource *dbus_source = (DBusGSource *)source;
219 DBusServer *server = dbus_source->connection_or_server;
221 dbus_server_ref (server);
223 dbus_gsource_dispatch (source, callback, user_data,
226 dbus_server_unref (server);
232 add_watch (DBusWatch *watch,
236 DBusGSource *dbus_source;
239 if (!dbus_watch_get_enabled (watch))
244 poll_fd = g_new (GPollFD, 1);
245 poll_fd->fd = dbus_watch_get_fd (watch);
247 flags = dbus_watch_get_flags (watch);
248 dbus_watch_set_data (watch, poll_fd, NULL);
250 if (flags & DBUS_WATCH_READABLE)
251 poll_fd->events |= G_IO_IN;
252 if (flags & DBUS_WATCH_WRITABLE)
253 poll_fd->events |= G_IO_OUT;
254 poll_fd->events |= G_IO_ERR | G_IO_HUP;
256 g_source_add_poll ((GSource *)dbus_source, poll_fd);
258 dbus_source->poll_fds = g_list_prepend (dbus_source->poll_fds, poll_fd);
259 g_hash_table_insert (dbus_source->watches, poll_fd, watch);
265 remove_watch (DBusWatch *watch,
268 DBusGSource *dbus_source = data;
271 poll_fd = dbus_watch_get_data (watch);
273 return; /* probably a not-enabled watch that was added */
275 dbus_source->poll_fds = g_list_remove (dbus_source->poll_fds, poll_fd);
276 g_hash_table_remove (dbus_source->watches, poll_fd);
277 g_source_remove_poll ((GSource *)dbus_source, poll_fd);
279 dbus_watch_set_data (watch, NULL, NULL); /* needed due to watch_toggled
280 * breaking add/remove symmetry
287 watch_toggled (DBusWatch *watch,
290 /* Because we just exit on OOM, enable/disable is
291 * no different from add/remove
293 if (dbus_watch_get_enabled (watch))
294 add_watch (watch, data);
296 remove_watch (watch, data);
300 timeout_handler (gpointer data)
302 DBusTimeout *timeout = data;
304 dbus_timeout_handle (timeout);
310 add_timeout (DBusTimeout *timeout,
313 DBusGSource *dbus_source = data;
316 if (!dbus_timeout_get_enabled (timeout))
319 source = g_timeout_source_new (dbus_timeout_get_interval (timeout));
320 g_source_set_callback (source, timeout_handler, timeout, NULL);
321 g_source_attach (source, dbus_source->context);
323 dbus_timeout_set_data (timeout, GUINT_TO_POINTER (g_source_get_id (source)),
330 remove_timeout (DBusTimeout *timeout,
335 timeout_tag = GPOINTER_TO_UINT (dbus_timeout_get_data (timeout));
337 if (timeout_tag != 0) /* if 0, probably timeout was disabled */
338 g_source_remove (timeout_tag);
342 timeout_toggled (DBusTimeout *timeout,
345 /* Because we just exit on OOM, enable/disable is
346 * no different from add/remove
348 if (dbus_timeout_get_enabled (timeout))
349 add_timeout (timeout, data);
351 remove_timeout (timeout, data);
356 free_source (GSource *source)
358 g_source_destroy (source);
362 wakeup_main (void *data)
364 DBusGSource *dbus_source = data;
366 g_main_context_wakeup (dbus_source->context);
370 /** @} */ /* End of GLib bindings internals */
372 /** @addtogroup DBusGLib
377 create_source (void *connection_or_server,
379 GMainContext *context)
382 DBusGSource *dbus_source;
384 source = g_source_new (funcs, sizeof (DBusGSource));
386 dbus_source = (DBusGSource *)source;
387 dbus_source->watches = g_hash_table_new (NULL, NULL);
388 dbus_source->connection_or_server = connection_or_server;
389 dbus_source->context = context;
395 * Sets the watch and timeout functions of a #DBusConnection
396 * to integrate the connection with the GLib main loop.
398 * @param connection the connection
401 dbus_connection_setup_with_g_main (DBusConnection *connection,
402 GMainContext *context)
406 source = create_source (connection, &dbus_connection_funcs, context);
408 if (!dbus_connection_set_watch_functions (connection,
415 if (!dbus_connection_set_timeout_functions (connection,
422 dbus_connection_set_wakeup_main_function (connection,
426 g_source_attach (source, context);
428 g_static_mutex_lock (&connection_slot_lock);
429 if (connection_slot == -1 )
430 connection_slot = dbus_connection_allocate_data_slot ();
431 g_static_mutex_unlock (&connection_slot_lock);
433 if (connection_slot < 0)
436 if (!dbus_connection_set_data (connection, connection_slot, source,
437 (DBusFreeFunction)free_source))
443 g_error ("Not enough memory to set up DBusConnection for use with GLib");
447 * Sets the watch and timeout functions of a #DBusServer
448 * to integrate the server with the GLib main loop.
450 * @param server the server
453 dbus_server_setup_with_g_main (DBusServer *server, GMainContext *context)
457 source = create_source (server, &dbus_server_funcs, context);
459 dbus_server_set_watch_functions (server,
465 dbus_server_set_timeout_functions (server,
471 g_source_attach (source, context);
473 g_static_mutex_lock (&server_slot_lock);
474 if (server_slot == -1 )
475 server_slot = dbus_server_allocate_data_slot ();
476 g_static_mutex_unlock (&server_slot_lock);
481 if (!dbus_server_set_data (server, server_slot, source,
482 (DBusFreeFunction)free_source))
488 g_error ("Not enough memory to set up DBusServer for use with GLib");
491 /** @} */ /* end of public API */