1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gmain.c GLib main loop integration
4 * Copyright (C) 2002 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"
30 DBusConnection *connection;
36 watch_callback (GIOChannel *source,
37 GIOCondition condition,
40 WatchCallback *cb = data;
41 unsigned int flags = 0;
43 if (condition & G_IO_IN)
44 flags |= DBUS_WATCH_READABLE;
45 if (condition & G_IO_OUT)
46 flags |= DBUS_WATCH_WRITABLE;
47 if (condition & G_IO_ERR)
48 flags |= DBUS_WATCH_ERROR;
49 if (condition & G_IO_HUP)
50 flags |= DBUS_WATCH_HANGUP;
52 dbus_connection_handle_watch (cb->connection,
56 /* Dispatch messages */
57 while (dbus_connection_dispatch_message (cb->connection));
63 free_callback_data (WatchCallback *cb)
65 dbus_connection_unref (cb->connection);
70 add_watch (DBusWatch *watch,
74 DBusConnection *connection = data;
75 GIOCondition condition = 0;
80 flags = dbus_watch_get_flags (watch);
83 if (flags & DBUS_WATCH_READABLE)
85 if (flags & DBUS_WATCH_WRITABLE)
86 condition |= G_IO_OUT;
87 if (flags & DBUS_WATCH_ERROR)
88 condition |= G_IO_ERR;
89 if (flags & DBUS_WATCH_HANGUP)
90 condition |= G_IO_HUP;
92 channel = g_io_channel_unix_new (dbus_watch_get_fd (watch));
93 g_io_channel_set_encoding (channel, NULL, NULL);
94 g_io_channel_set_buffered (channel, FALSE);
96 cb = g_new0 (WatchCallback, 1);
98 cb->connection = connection;
99 dbus_connection_ref (connection);
101 dbus_watch_set_data (watch, cb, (DBusFreeFunction)free_callback_data);
103 tag = g_io_add_watch (channel, condition, watch_callback, cb);
108 remove_watch (DBusWatch *watch,
113 cb = dbus_watch_get_data (watch);
115 g_source_remove (cb->tag);
117 dbus_watch_set_data (watch, NULL, NULL);
121 add_timeout (DBusTimeout *timeout,
127 remove_timeout (DBusTimeout *timeout,
133 dbus_connection_hookup_with_g_main (DBusConnection *connection)
136 dbus_connection_set_watch_functions (connection,
140 dbus_connection_set_timeout_functions (connection,