]> git.meshlink.io Git - catta/blob - avahi-common/dbus-watch-glue.c
* avahi-utils: replace python avahi-browse with a version written in C.
[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) {
85     static const struct timeval tv = { 0, 0 };
86     assert(d);
87
88     assert(dbus_connection_get_dispatch_status(d->connection) == DBUS_DISPATCH_DATA_REMAINS);
89
90     d->poll_api->timeout_update(d->dispatch_timeout, &tv);
91 }
92
93 static void dispatch_timeout_callback(AvahiTimeout *t, void *userdata) {
94     ConnectionData *d = userdata;
95     assert(t);
96     assert(d);
97
98     connection_data_ref(d);
99     dbus_connection_ref(d->connection);
100
101     if (dbus_connection_dispatch(d->connection) == DBUS_DISPATCH_DATA_REMAINS)
102         /* If there's still data, request that this handler is called again */
103         request_dispatch(d);
104
105     dbus_connection_unref(d->connection);
106     connection_data_unref(d);
107 }
108
109 static void watch_callback(AvahiWatch *avahi_watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
110     DBusWatch *dbus_watch = userdata;
111     
112     assert(avahi_watch);
113     assert(dbus_watch);
114
115     dbus_watch_handle(dbus_watch, translate_avahi_to_dbus(events));
116     /* Ignore the return value */
117 }
118
119 static dbus_bool_t update_watch(const AvahiPoll *poll_api, DBusWatch *dbus_watch) {
120     AvahiWatch *avahi_watch;
121     dbus_bool_t b;
122     
123     assert(dbus_watch);
124
125     avahi_watch = dbus_watch_get_data(dbus_watch);
126
127     b = dbus_watch_get_enabled(dbus_watch);
128     
129     if (b && !avahi_watch) {
130
131         if (!(avahi_watch = poll_api->watch_new(
132                   poll_api,
133                   dbus_watch_get_fd(dbus_watch),
134                   translate_dbus_to_avahi(dbus_watch_get_flags(dbus_watch)),
135                   watch_callback,
136                   dbus_watch)))
137             return FALSE;
138
139         dbus_watch_set_data(dbus_watch, avahi_watch, NULL);
140         
141     } else if (!b && avahi_watch) {
142         
143         poll_api->watch_free(avahi_watch);
144         dbus_watch_set_data(dbus_watch, NULL, NULL);
145
146     } else if (avahi_watch) {
147         
148         /* Update flags */
149         poll_api->watch_update(avahi_watch, dbus_watch_get_flags(dbus_watch));
150     }
151
152     return TRUE;
153 }
154
155 static dbus_bool_t add_watch(DBusWatch *dbus_watch, void *userdata) {
156     ConnectionData *d = userdata;
157     
158     assert(dbus_watch);
159     assert(d);
160
161     return update_watch(d->poll_api, dbus_watch);
162 }
163
164 static void remove_watch(DBusWatch *dbus_watch, void *userdata) {
165     ConnectionData *d = userdata;
166     AvahiWatch *avahi_watch;
167     
168     assert(dbus_watch);
169     assert(d);
170
171     if ((avahi_watch = dbus_watch_get_data(dbus_watch))) {
172         d->poll_api->watch_free(avahi_watch);
173         dbus_watch_set_data(dbus_watch, NULL, NULL);
174     }
175 }
176
177 static void watch_toggled(DBusWatch *dbus_watch, void *userdata) {
178     ConnectionData *d = userdata;
179     
180     assert(dbus_watch);
181     assert(d);
182
183     update_watch(d->poll_api, dbus_watch);
184 }
185
186 typedef struct TimeoutData {
187     const AvahiPoll *poll_api;
188     AvahiTimeout *avahi_timeout;
189     DBusTimeout *dbus_timeout;
190     int ref;
191 } TimeoutData;
192
193 static TimeoutData* timeout_data_ref(TimeoutData *t) {
194     assert(t);
195     assert(t->ref >= 1);
196
197     t->ref++;
198     return t;
199 }
200
201 static void timeout_data_unref(TimeoutData *t) {
202     assert(t);
203     assert(t->ref >= 1);
204
205     if (--t->ref <= 0) {
206         if (t->avahi_timeout)
207             t->poll_api->timeout_free(t->avahi_timeout);
208         
209         avahi_free(t);
210     }
211 }
212
213 static void update_timeout(TimeoutData *timeout) {
214     assert(timeout);
215     assert(timeout->ref >= 1);
216     
217     if (dbus_timeout_get_enabled(timeout->dbus_timeout)) {
218         struct timeval tv;
219         avahi_elapse_time(&tv, dbus_timeout_get_interval(timeout->dbus_timeout), 0);
220         timeout->poll_api->timeout_update(timeout->
221                                       avahi_timeout, &tv);
222     } else
223         timeout->poll_api->timeout_update(timeout->avahi_timeout, NULL);
224
225 }
226
227 static void timeout_callback(AvahiTimeout *avahi_timeout, void *userdata) {
228     TimeoutData *timeout = userdata;
229     
230     assert(avahi_timeout);
231     assert(timeout);
232
233     timeout_data_ref(timeout);
234
235     dbus_timeout_handle(timeout->dbus_timeout);
236     /* Ignore the return value */
237
238     if (timeout->avahi_timeout)
239         update_timeout(timeout);
240     
241     timeout_data_unref(timeout);
242 }
243
244 static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {
245     TimeoutData *timeout;
246     ConnectionData *d = userdata;
247     struct timeval tv;
248     dbus_bool_t b;
249
250     assert(dbus_timeout);
251     assert(d);
252
253     if (!(timeout = avahi_new(TimeoutData, 1)))
254         return FALSE;
255
256     timeout->dbus_timeout = dbus_timeout;
257     timeout->poll_api = d->poll_api;
258     timeout->ref = 1;
259
260     if ((b = dbus_timeout_get_enabled(dbus_timeout)))
261         avahi_elapse_time(&tv, dbus_timeout_get_interval(dbus_timeout), 0);
262     
263     if (!(timeout->avahi_timeout = d->poll_api->timeout_new(
264               d->poll_api,
265               b ? &tv : NULL,
266               timeout_callback,
267               timeout))) {
268         avahi_free(timeout);
269         return FALSE;
270     }
271
272     dbus_timeout_set_data(dbus_timeout, timeout, (DBusFreeFunction) timeout_data_unref);
273     return TRUE;
274 }
275
276 static void remove_timeout(DBusTimeout *dbus_timeout, void *userdata) {
277     ConnectionData *d = userdata;
278     TimeoutData *timeout;
279
280     assert(dbus_timeout);
281     assert(d);
282
283     timeout = dbus_timeout_get_data(dbus_timeout);
284     assert(timeout);
285
286     d->poll_api->timeout_free(timeout->avahi_timeout);
287     timeout->avahi_timeout = NULL;
288 }
289
290 static void timeout_toggled(DBusTimeout *dbus_timeout, AVAHI_GCC_UNUSED void *userdata) {
291     TimeoutData *timeout;
292
293     assert(dbus_timeout);
294     timeout = dbus_timeout_get_data(dbus_timeout);
295     assert(timeout);
296
297     update_timeout(timeout);
298 }
299
300 static void dispatch_status(AVAHI_GCC_UNUSED DBusConnection *connection, DBusDispatchStatus new_status, void *userdata) {
301     ConnectionData *d = userdata;
302     
303     if (new_status == DBUS_DISPATCH_DATA_REMAINS)
304         request_dispatch(d);
305  }
306
307 int avahi_dbus_connection_glue(DBusConnection *c, const AvahiPoll *poll_api) {
308     ConnectionData *d = NULL;
309     
310     assert(c);
311     assert(poll_api);
312
313     if (!(d = avahi_new(ConnectionData, 1)))
314         goto fail;;
315
316     d->poll_api = poll_api;
317     d->connection = c;
318     d->ref = 1;
319     
320     if (!(d->dispatch_timeout = poll_api->timeout_new(poll_api, NULL, dispatch_timeout_callback, d)))
321         goto fail;
322     
323     if (!(dbus_connection_set_watch_functions(c, add_watch, remove_watch, watch_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
324         goto fail;
325
326     if (!(dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, timeout_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
327         goto fail;
328
329     dbus_connection_set_dispatch_status_function(c, dispatch_status, connection_data_ref(d), (DBusFreeFunction)connection_data_unref);
330
331     if (dbus_connection_get_dispatch_status(c) == DBUS_DISPATCH_DATA_REMAINS)
332         request_dispatch(d);
333
334     connection_data_unref(d);
335     
336     return 0;
337
338 fail:
339
340     if (d) {
341         d->poll_api->timeout_free(d->dispatch_timeout);
342
343         avahi_free(d);
344     }
345
346     return -1;
347 }