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"
27 typedef struct _DBusGSource DBusGSource;
33 DBusConnection *connection;
39 static gboolean dbus_connection_prepare (GSource *source,
41 static gboolean dbus_connection_check (GSource *source);
42 static gboolean dbus_connection_dispatch (GSource *source,
47 static GSourceFuncs dbus_funcs = {
48 dbus_connection_prepare,
49 dbus_connection_check,
50 dbus_connection_dispatch,
55 dbus_connection_prepare (GSource *source,
58 DBusConnection *connection = ((DBusGSource *)source)->connection;
62 return (dbus_connection_peek_message (connection) != NULL);
66 dbus_connection_check (GSource *source)
68 DBusGSource *dbus_source = (DBusGSource *)source;
71 list = dbus_source->poll_fds;
75 GPollFD *poll_fd = list->data;
77 if (poll_fd->revents != 0)
87 dbus_connection_dispatch (GSource *source,
91 DBusGSource *dbus_source = (DBusGSource *)source;
94 /* We need to traverse a copy of the list, since it can change in
95 dbus_connect_handle_watch. */
96 copy = g_list_copy (dbus_source->poll_fds);
101 GPollFD *poll_fd = list->data;
103 if (poll_fd->revents != 0)
105 DBusWatch *watch = g_hash_table_lookup (dbus_source->watches, poll_fd);
108 if (poll_fd->revents & G_IO_IN)
109 condition |= DBUS_WATCH_READABLE;
110 if (poll_fd->revents & G_IO_OUT)
111 condition |= DBUS_WATCH_WRITABLE;
112 if (poll_fd->revents & G_IO_ERR)
113 condition |= DBUS_WATCH_ERROR;
114 if (poll_fd->revents & G_IO_HUP)
115 condition |= DBUS_WATCH_HANGUP;
117 dbus_connection_handle_watch (dbus_source->connection, watch, condition);
125 /* Dispatch messages */
126 while (dbus_connection_dispatch_message (dbus_source->connection));
132 add_watch (DBusWatch *watch,
136 DBusGSource *dbus_source;
141 poll_fd = g_new (GPollFD, 1);
142 poll_fd->fd = dbus_watch_get_fd (watch);
144 flags = dbus_watch_get_flags (watch);
145 dbus_watch_set_data (watch, poll_fd, NULL);
147 if (flags & DBUS_WATCH_READABLE)
148 poll_fd->events |= G_IO_IN;
149 if (flags & DBUS_WATCH_WRITABLE)
150 poll_fd->events |= G_IO_OUT;
151 if (flags & DBUS_WATCH_ERROR)
152 poll_fd->events |= G_IO_ERR;
153 if (flags & DBUS_WATCH_HANGUP)
154 poll_fd->events |= G_IO_HUP;
156 g_source_add_poll ((GSource *)dbus_source, poll_fd);
158 dbus_source->poll_fds = g_list_prepend (dbus_source->poll_fds, poll_fd);
159 g_hash_table_insert (dbus_source->watches, poll_fd, watch);
163 remove_watch (DBusWatch *watch,
166 DBusGSource *dbus_source = data;
169 poll_fd = dbus_watch_get_data (watch);
171 dbus_source->poll_fds = g_list_remove (dbus_source->poll_fds, poll_fd);
172 g_hash_table_remove (dbus_source->watches, poll_fd);
173 g_source_remove_poll ((GSource *)dbus_source, poll_fd);
179 add_timeout (DBusTimeout *timeout,
185 remove_timeout (DBusTimeout *timeout,
191 dbus_connection_hookup_with_g_main (DBusConnection *connection)
194 DBusGSource *dbus_source;
196 source = g_source_new (&dbus_funcs, sizeof (DBusGSource));
198 dbus_source = (DBusGSource *)source;
199 dbus_source->watches = g_hash_table_new (NULL, NULL);
200 dbus_source->connection = connection;
202 dbus_connection_set_watch_functions (connection,
206 dbus_connection_set_timeout_functions (connection,
211 g_source_attach (source, NULL);