6e267cdb8f5a27aeb6f4a211b6c5370f6d3e3d95
[platform/upstream/dbus.git] / glib / dbus-gmain.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gmain.c GLib main loop integration
3  *
4  * Copyright (C) 2002  CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include "dbus-glib.h"
25 #include <glib.h>
26
27 typedef struct
28 {
29   DBusWatch *watch;
30   DBusConnection *connection;
31
32   guint tag;
33 } WatchCallback;
34
35 static gboolean
36 watch_callback (GIOChannel *source,
37                 GIOCondition condition,
38                 gpointer data)
39 {
40   WatchCallback *cb = data;
41   unsigned int flags = 0;
42
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;
51
52   dbus_connection_handle_watch (cb->connection,
53                                 cb->watch,
54                                 flags);
55
56   /* Dispatch messages */
57   while (dbus_connection_dispatch_message (cb->connection));
58   
59   return TRUE;
60 }
61
62 static void
63 free_callback_data (WatchCallback *cb)
64 {
65   dbus_connection_unref (cb->connection);
66   g_free (cb);
67 }
68
69 static void
70 add_watch (DBusWatch *watch,
71            gpointer   data)
72 {
73   GIOChannel *channel;
74   DBusConnection *connection = data;
75   GIOCondition condition = 0;
76   WatchCallback *cb;
77   guint tag;
78   gint flags;
79
80   flags = dbus_watch_get_flags (watch);
81   condition = 0;
82   
83   if (flags & DBUS_WATCH_READABLE)
84     condition |= G_IO_IN;
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;
91   
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);
95
96   cb = g_new0 (WatchCallback, 1);
97   cb->watch = watch;
98   cb->connection = connection;
99   dbus_connection_ref (connection);
100   
101   dbus_watch_set_data (watch, cb, (DBusFreeFunction)free_callback_data);
102   
103   tag = g_io_add_watch (channel, condition, watch_callback, cb);
104   cb->tag = tag;
105 }
106
107 static void
108 remove_watch (DBusWatch *watch,
109               gpointer   data)
110 {
111   WatchCallback *cb;
112
113   cb = dbus_watch_get_data (watch);
114
115   g_source_remove (cb->tag);
116   
117   dbus_watch_set_data (watch, NULL, NULL);
118 }
119
120 static void
121 add_timeout (DBusTimeout *timeout,
122              void        *data)
123 {
124 }
125
126 static void
127 remove_timeout (DBusTimeout *timeout,
128                 void        *data)
129 {
130 }
131
132 void
133 dbus_connection_hookup_with_g_main (DBusConnection *connection)
134 {
135
136   dbus_connection_set_watch_functions (connection,
137                                        add_watch,
138                                        remove_watch,
139                                        connection, NULL);
140   dbus_connection_set_timeout_functions (connection,
141                                          add_timeout,
142                                          remove_timeout,
143                                          NULL, NULL);
144                                          
145 }