summaryrefslogtreecommitdiffstats
path: root/glib/dbus-gmain.c
blob: 6e267cdb8f5a27aeb6f4a211b6c5370f6d3e3d95 (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
/* -*- mode: C; c-file-style: "gnu" -*- */
/* dbus-gmain.c GLib main loop integration
 *
 * Copyright (C) 2002  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
{
  DBusWatch *watch;
  DBusConnection *connection;

  guint tag;
} WatchCallback;

static gboolean
watch_callback (GIOChannel *source,
		GIOCondition condition,
		gpointer data)
{
  WatchCallback *cb = data;
  unsigned int flags = 0;

  if (condition & G_IO_IN)
    flags |= DBUS_WATCH_READABLE;
  if (condition & G_IO_OUT)
    flags |= DBUS_WATCH_WRITABLE;
  if (condition & G_IO_ERR)
    flags |= DBUS_WATCH_ERROR;
  if (condition & G_IO_HUP)
    flags |= DBUS_WATCH_HANGUP;

  dbus_connection_handle_watch (cb->connection,
				cb->watch,
				flags);

  /* Dispatch messages */
  while (dbus_connection_dispatch_message (cb->connection));
  
  return TRUE;
}

static void
free_callback_data (WatchCallback *cb)
{
  dbus_connection_unref (cb->connection);
  g_free (cb);
}

static void
add_watch (DBusWatch *watch,
	   gpointer   data)
{
  GIOChannel *channel;
  DBusConnection *connection = data;
  GIOCondition condition = 0;
  WatchCallback *cb;
  guint tag;
  gint flags;

  flags = dbus_watch_get_flags (watch);
  condition = 0;
  
  if (flags & DBUS_WATCH_READABLE)
    condition |= G_IO_IN;
  if (flags & DBUS_WATCH_WRITABLE)
    condition |= G_IO_OUT;
  if (flags & DBUS_WATCH_ERROR)
    condition |= G_IO_ERR;
  if (flags & DBUS_WATCH_HANGUP)
    condition |= G_IO_HUP;
  
  channel = g_io_channel_unix_new (dbus_watch_get_fd (watch));
  g_io_channel_set_encoding (channel, NULL, NULL);
  g_io_channel_set_buffered (channel, FALSE);

  cb = g_new0 (WatchCallback, 1);
  cb->watch = watch;
  cb->connection = connection;
  dbus_connection_ref (connection);
  
  dbus_watch_set_data (watch, cb, (DBusFreeFunction)free_callback_data);
  
  tag = g_io_add_watch (channel, condition, watch_callback, cb);
  cb->tag = tag;
}

static void
remove_watch (DBusWatch *watch,
	      gpointer   data)
{
  WatchCallback *cb;

  cb = dbus_watch_get_data (watch);

  g_source_remove (cb->tag);
  
  dbus_watch_set_data (watch, NULL, NULL);
}

static void
add_timeout (DBusTimeout *timeout,
	     void        *data)
{
}

static void
remove_timeout (DBusTimeout *timeout,
		void        *data)
{
}

void
dbus_connection_hookup_with_g_main (DBusConnection *connection)
{

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