]> git.meshlink.io Git - catta/blob - avahi-common/dbus-watch-glue.c
use dbus_watch_get_unix_fd() only in dbus >= 1.1.1
[catta] / avahi-common / dbus-watch-glue.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #include <assert.h>
23 #include <stdio.h>
24
25 #include "malloc.h"
26 #include "timeval.h"
27 #include "dbus-watch-glue.h"
28
29 static AvahiWatchEvent translate_dbus_to_avahi(unsigned int f) {
30     AvahiWatchEvent e = 0;
31
32     if (f & DBUS_WATCH_READABLE)
33         e |= AVAHI_WATCH_IN;
34     if (f & DBUS_WATCH_WRITABLE)
35         e |= AVAHI_WATCH_OUT;
36     if (f & DBUS_WATCH_ERROR)
37         e |= AVAHI_WATCH_ERR;
38     if (f & DBUS_WATCH_HANGUP)
39         e |= AVAHI_WATCH_HUP;
40
41     return e;
42 }
43
44 static unsigned int translate_avahi_to_dbus(AvahiWatchEvent e) {
45     unsigned int f = 0;
46
47     if (e & AVAHI_WATCH_IN)
48         f |= DBUS_WATCH_READABLE;
49     if (e & AVAHI_WATCH_OUT)
50         f |= DBUS_WATCH_WRITABLE;
51     if (e & AVAHI_WATCH_ERR)
52         f |= DBUS_WATCH_ERROR;
53     if (e & AVAHI_WATCH_HUP)
54         f |= DBUS_WATCH_HANGUP;
55
56     return f;
57 }
58
59 typedef struct {
60     DBusConnection *connection;
61     const AvahiPoll *poll_api;
62     AvahiTimeout *dispatch_timeout;
63     int ref;
64 } ConnectionData;
65
66 static ConnectionData *connection_data_ref(ConnectionData *d) {
67     assert(d);
68     assert(d->ref >= 1);
69
70     d->ref++;
71     return d;
72 }
73
74 static void connection_data_unref(ConnectionData *d) {
75     assert(d);
76     assert(d->ref >= 1);
77
78     if (--d->ref <= 0) {
79         d->poll_api->timeout_free(d->dispatch_timeout);
80         avahi_free(d);
81     }
82 }
83
84 static void request_dispatch(ConnectionData *d, int enable) {
85     static const struct timeval tv = { 0, 0 };
86     assert(d);
87
88     if (enable) {
89         assert(dbus_connection_get_dispatch_status(d->connection) == DBUS_DISPATCH_DATA_REMAINS);
90         d->poll_api->timeout_update(d->dispatch_timeout, &tv);
91     } else
92         d->poll_api->timeout_update(d->dispatch_timeout, NULL);
93 }
94
95 static void dispatch_timeout_callback(AvahiTimeout *t, void *userdata) {
96     ConnectionData *d = userdata;
97     assert(t);
98     assert(d);
99
100     connection_data_ref(d);
101     dbus_connection_ref(d->connection);
102
103     if (dbus_connection_dispatch(d->connection) == DBUS_DISPATCH_DATA_REMAINS)
104         /* If there's still data, request that this handler is called again */
105         request_dispatch(d, 1);
106     else
107         request_dispatch(d, 0);
108
109     dbus_connection_unref(d->connection);
110     connection_data_unref(d);
111 }
112
113 static void watch_callback(AvahiWatch *avahi_watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
114     DBusWatch *dbus_watch = userdata;
115     
116     assert(avahi_watch);
117     assert(dbus_watch);
118
119     dbus_watch_handle(dbus_watch, translate_avahi_to_dbus(events));
120     /* Ignore the return value */
121 }
122
123 static dbus_bool_t update_watch(const AvahiPoll *poll_api, DBusWatch *dbus_watch) {
124     AvahiWatch *avahi_watch;
125     dbus_bool_t b;
126     
127     assert(dbus_watch);
128
129     avahi_watch = dbus_watch_get_data(dbus_watch);
130
131     b = dbus_watch_get_enabled(dbus_watch);
132     
133     if (b && !avahi_watch) {
134
135         if (!(avahi_watch = poll_api->watch_new(
136                   poll_api,
137 #if (DBUS_VERSION_MAJOR == 1 && DBUS_VERSION_MINOR == 1 && DBUS_VERSION_MICRO >= 1) || (DBUS_VERSION_MAJOR == 1 && DBUS_VERSION_MAJOR > 1) || (DBUS_VERSION_MAJOR > 1)
138                   dbus_watch_get_unix_fd(dbus_watch),
139 #else
140                   dbus_watch_get_fd(dbus_watch),
141 #endif
142                   translate_dbus_to_avahi(dbus_watch_get_flags(dbus_watch)),
143                   watch_callback,
144                   dbus_watch)))
145             return FALSE;
146
147         dbus_watch_set_data(dbus_watch, avahi_watch, NULL);
148         
149     } else if (!b && avahi_watch) {
150         
151         poll_api->watch_free(avahi_watch);
152         dbus_watch_set_data(dbus_watch, NULL, NULL);
153
154     } else if (avahi_watch) {
155         
156         /* Update flags */
157         poll_api->watch_update(avahi_watch, dbus_watch_get_flags(dbus_watch));
158     }
159
160     return TRUE;
161 }
162
163 static dbus_bool_t add_watch(DBusWatch *dbus_watch, void *userdata) {
164     ConnectionData *d = userdata;
165     
166     assert(dbus_watch);
167     assert(d);
168
169     return update_watch(d->poll_api, dbus_watch);
170 }
171
172 static void remove_watch(DBusWatch *dbus_watch, void *userdata) {
173     ConnectionData *d = userdata;
174     AvahiWatch *avahi_watch;
175     
176     assert(dbus_watch);
177     assert(d);
178
179     if ((avahi_watch = dbus_watch_get_data(dbus_watch))) {
180         d->poll_api->watch_free(avahi_watch);
181         dbus_watch_set_data(dbus_watch, NULL, NULL);
182     }
183 }
184
185 static void watch_toggled(DBusWatch *dbus_watch, void *userdata) {
186     ConnectionData *d = userdata;
187     
188     assert(dbus_watch);
189     assert(d);
190
191     update_watch(d->poll_api, dbus_watch);
192 }
193
194 typedef struct TimeoutData {
195     const AvahiPoll *poll_api;
196     AvahiTimeout *avahi_timeout;
197     DBusTimeout *dbus_timeout;
198     int ref;
199 } TimeoutData;
200
201 static TimeoutData* timeout_data_ref(TimeoutData *t) {
202     assert(t);
203     assert(t->ref >= 1);
204
205     t->ref++;
206     return t;
207 }
208
209 static void timeout_data_unref(TimeoutData *t) {
210     assert(t);
211     assert(t->ref >= 1);
212
213     if (--t->ref <= 0) {
214         if (t->avahi_timeout)
215             t->poll_api->timeout_free(t->avahi_timeout);
216         
217         avahi_free(t);
218     }
219 }
220
221 static void update_timeout(TimeoutData *timeout) {
222     assert(timeout);
223     assert(timeout->ref >= 1);
224     
225     if (dbus_timeout_get_enabled(timeout->dbus_timeout)) {
226         struct timeval tv;
227         avahi_elapse_time(&tv, dbus_timeout_get_interval(timeout->dbus_timeout), 0);
228         timeout->poll_api->timeout_update(timeout->
229                                       avahi_timeout, &tv);
230     } else
231         timeout->poll_api->timeout_update(timeout->avahi_timeout, NULL);
232
233 }
234
235 static void timeout_callback(AvahiTimeout *avahi_timeout, void *userdata) {
236     TimeoutData *timeout = userdata;
237     
238     assert(avahi_timeout);
239     assert(timeout);
240
241     timeout_data_ref(timeout);
242
243     dbus_timeout_handle(timeout->dbus_timeout);
244     /* Ignore the return value */
245
246     if (timeout->avahi_timeout)
247         update_timeout(timeout);
248     
249     timeout_data_unref(timeout);
250 }
251
252 static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {
253     TimeoutData *timeout;
254     ConnectionData *d = userdata;
255     struct timeval tv;
256     dbus_bool_t b;
257
258     assert(dbus_timeout);
259     assert(d);
260
261     if (!(timeout = avahi_new(TimeoutData, 1)))
262         return FALSE;
263
264     timeout->dbus_timeout = dbus_timeout;
265     timeout->poll_api = d->poll_api;
266     timeout->ref = 1;
267
268     if ((b = dbus_timeout_get_enabled(dbus_timeout)))
269         avahi_elapse_time(&tv, dbus_timeout_get_interval(dbus_timeout), 0);
270     
271     if (!(timeout->avahi_timeout = d->poll_api->timeout_new(
272               d->poll_api,
273               b ? &tv : NULL,
274               timeout_callback,
275               timeout))) {
276         avahi_free(timeout);
277         return FALSE;
278     }
279
280     dbus_timeout_set_data(dbus_timeout, timeout, (DBusFreeFunction) timeout_data_unref);
281     return TRUE;
282 }
283
284 static void remove_timeout(DBusTimeout *dbus_timeout, void *userdata) {
285     ConnectionData *d = userdata;
286     TimeoutData *timeout;
287
288     assert(dbus_timeout);
289     assert(d);
290
291     timeout = dbus_timeout_get_data(dbus_timeout);
292     assert(timeout);
293
294     d->poll_api->timeout_free(timeout->avahi_timeout);
295     timeout->avahi_timeout = NULL;
296 }
297
298 static void timeout_toggled(DBusTimeout *dbus_timeout, AVAHI_GCC_UNUSED void *userdata) {
299     TimeoutData *timeout;
300
301     assert(dbus_timeout);
302     timeout = dbus_timeout_get_data(dbus_timeout);
303     assert(timeout);
304
305     update_timeout(timeout);
306 }
307
308 static void dispatch_status(AVAHI_GCC_UNUSED DBusConnection *connection, DBusDispatchStatus new_status, void *userdata) {
309     ConnectionData *d = userdata;
310     
311     if (new_status == DBUS_DISPATCH_DATA_REMAINS)
312         request_dispatch(d, 1);
313  }
314
315 int avahi_dbus_connection_glue(DBusConnection *c, const AvahiPoll *poll_api) {
316     ConnectionData *d = NULL;
317     
318     assert(c);
319     assert(poll_api);
320
321     if (!(d = avahi_new(ConnectionData, 1)))
322         goto fail;;
323
324     d->poll_api = poll_api;
325     d->connection = c;
326     d->ref = 1;
327     
328     if (!(d->dispatch_timeout = poll_api->timeout_new(poll_api, NULL, dispatch_timeout_callback, d)))
329         goto fail;
330     
331     if (!(dbus_connection_set_watch_functions(c, add_watch, remove_watch, watch_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
332         goto fail;
333
334     if (!(dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, timeout_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
335         goto fail;
336
337     dbus_connection_set_dispatch_status_function(c, dispatch_status, connection_data_ref(d), (DBusFreeFunction)connection_data_unref);
338
339     if (dbus_connection_get_dispatch_status(c) == DBUS_DISPATCH_DATA_REMAINS)
340         request_dispatch(d, 1);
341
342     connection_data_unref(d);
343     
344     return 0;
345
346 fail:
347
348     if (d) {
349         d->poll_api->timeout_free(d->dispatch_timeout);
350
351         avahi_free(d);
352     }
353
354     return -1;
355 }