summaryrefslogtreecommitdiffstats
path: root/glib/dbus-gmain.c
blob: f8355a3f0911ff4f4a38fbdff3836372cd886bd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-gmain.c GLib main loop integration
 *
 * Copyright (C) 2002, 2003  CodeFactory AB
 *
 * Licensed under the Academic Free License version 1.2
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "dbus-glib.h"
#include <glib.h>

typedef struct _DBusGSource DBusGSource;

struct _DBusGSource
{
  GSource source;

  DBusConnection *connection;

  GList *poll_fds;
  GHashTable *watches;
};

static int connection_slot = -1;

static gboolean dbus_connection_prepare  (GSource     *source,
					  gint        *timeout);
static gboolean dbus_connection_check    (GSource     *source);
static gboolean dbus_connection_dispatch (GSource     *source,
					  GSourceFunc  callback,
					  gpointer     user_data);


static GSourceFuncs dbus_funcs = {
  dbus_connection_prepare,
  dbus_connection_check,
  dbus_connection_dispatch,
  NULL
};

static gboolean
dbus_connection_prepare (GSource *source,
			 gint    *timeout)
{
  DBusConnection *connection = ((DBusGSource *)source)->connection;
  
  *timeout = -1;

  return (dbus_connection_get_n_messages (connection) > 0);  
}

static gboolean
dbus_connection_check (GSource *source)
{
  DBusGSource *dbus_source = (DBusGSource *)source;
  GList *list;

  list = dbus_source->poll_fds;

  while (list)
    {
      GPollFD *poll_fd = list->data;

      if (poll_fd->revents != 0)
	return TRUE;

      list = list->next;
    }

  return FALSE;  
}

static gboolean
dbus_connection_dispatch (GSource     *source,
			  GSourceFunc  callback,
			  gpointer     user_data)
{
   DBusGSource *dbus_source = (DBusGSource *)source;
   GList *copy, *list;

   /* We need to traverse a copy of the list, since it can change in
      dbus_connect_handle_watch. */
   copy = g_list_copy (dbus_source->poll_fds);

   list = copy;
   while (list)
     {
       GPollFD *poll_fd = list->data;

       if (poll_fd->revents != 0)
	 {
	   DBusWatch *watch = g_hash_table_lookup (dbus_source->watches, poll_fd);
	   guint condition = 0;
	   
	   if (poll_fd->revents & G_IO_IN)
	     condition |= DBUS_WATCH_READABLE;
	   if (poll_fd->revents & G_IO_OUT)
	     condition |= DBUS_WATCH_WRITABLE;
	   if (poll_fd->revents & G_IO_ERR)
	     condition |= DBUS_WATCH_ERROR;
	   if (poll_fd->revents & G_IO_HUP)
	     condition |= DBUS_WATCH_HANGUP;
	   
	   dbus_connection_handle_watch (dbus_source->connection, watch, condition);
	 }

       list = list->next;
     }

   g_list_free (copy);
   
   /* Dispatch messages */
   while (dbus_connection_dispatch_message (dbus_source->connection));

   return TRUE;
}

static void
add_watch (DBusWatch *watch,
	   gpointer   data)
{
  GPollFD *poll_fd;
  DBusGSource *dbus_source;
  guint flags;
  
  dbus_source = data;
  
  poll_fd = g_new (GPollFD, 1);
  poll_fd->fd = dbus_watch_get_fd (watch);
  poll_fd->events = 0;
  flags = dbus_watch_get_flags (watch);
  dbus_watch_set_data (watch, poll_fd, NULL);

  if (flags & DBUS_WATCH_READABLE)
    poll_fd->events |= G_IO_IN;
  if (flags & DBUS_WATCH_WRITABLE)
    poll_fd->events |= G_IO_OUT;
  poll_fd->events |= G_IO_ERR | G_IO_HUP;

  g_source_add_poll ((GSource *)dbus_source, poll_fd);

  dbus_source->poll_fds = g_list_prepend (dbus_source->poll_fds, poll_fd);
  g_hash_table_insert (dbus_source->watches, poll_fd, watch);
}

static void
remove_watch (DBusWatch *watch,
	      gpointer   data)
{
  DBusGSource *dbus_source = data;
  GPollFD *poll_fd;
  
  poll_fd = dbus_watch_get_data (watch);
  
  dbus_source->poll_fds = g_list_remove (dbus_source->poll_fds, poll_fd);
  g_hash_table_remove (dbus_source->watches, poll_fd);
  g_source_remove_poll ((GSource *)dbus_source, poll_fd);
  
  g_free (poll_fd);
}

static gboolean
timeout_handler (gpointer data)
{
  DBusTimeout *timeout = data;

  dbus_timeout_handle (timeout);

  return FALSE;
}


static void
add_timeout (DBusTimeout *timeout,
	     void        *data)
{
  guint timeout_tag;

  timeout_tag = g_timeout_add (dbus_timeout_get_interval (timeout),
			       timeout_handler, timeout);
  
  dbus_timeout_set_data (timeout, GUINT_TO_POINTER (timeout_tag), NULL);
}

static void
remove_timeout (DBusTimeout *timeout,
		void        *data)
{
  guint timeout_tag;
  
  timeout_tag = GPOINTER_TO_UINT (dbus_timeout_get_data (timeout));

  g_source_remove (timeout_tag);
}

static void
free_source (GSource *source)
{
  g_source_destroy (source);
}

void
dbus_connection_hookup_with_g_main (DBusConnection *connection)
{
  GSource *source;
  DBusGSource *dbus_source;

  source = g_source_new (&dbus_funcs, sizeof (DBusGSource));
  
  dbus_source = (DBusGSource *)source;  
  dbus_source->watches = g_hash_table_new (NULL, NULL);
  dbus_source->connection = connection;

  dbus_connection_set_watch_functions (connection,
				       add_watch,
				       remove_watch,
				       source, NULL);
  dbus_connection_set_timeout_functions (connection,
					 add_timeout,
					 remove_timeout,
					 NULL, NULL);

  g_source_attach (source, NULL);

  if (connection_slot == -1 )
    connection_slot = dbus_connection_allocate_data_slot ();

  dbus_connection_set_data (connection, connection_slot, source,
			    (DBusFreeFunction)free_source);
  
}