4 This file is part of avahi.
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.
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.
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
27 #include "dbus-watch-glue.h"
29 static AvahiWatchEvent translate_dbus_to_avahi(unsigned int f) {
30 AvahiWatchEvent e = 0;
32 if (f & DBUS_WATCH_READABLE)
34 if (f & DBUS_WATCH_WRITABLE)
36 if (f & DBUS_WATCH_ERROR)
38 if (f & DBUS_WATCH_HANGUP)
44 static unsigned int translate_avahi_to_dbus(AvahiWatchEvent e) {
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;
60 DBusConnection *connection;
61 const AvahiPoll *poll_api;
62 AvahiTimeout *dispatch_timeout;
66 static ConnectionData *connection_data_ref(ConnectionData *d) {
74 static void connection_data_unref(ConnectionData *d) {
79 d->poll_api->timeout_free(d->dispatch_timeout);
84 static void request_dispatch(ConnectionData *d, int enable) {
85 static const struct timeval tv = { 0, 0 };
89 assert(dbus_connection_get_dispatch_status(d->connection) == DBUS_DISPATCH_DATA_REMAINS);
90 d->poll_api->timeout_update(d->dispatch_timeout, &tv);
92 d->poll_api->timeout_update(d->dispatch_timeout, NULL);
95 static void dispatch_timeout_callback(AvahiTimeout *t, void *userdata) {
96 ConnectionData *d = userdata;
100 connection_data_ref(d);
101 dbus_connection_ref(d->connection);
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);
107 request_dispatch(d, 0);
109 dbus_connection_unref(d->connection);
110 connection_data_unref(d);
113 static void watch_callback(AvahiWatch *avahi_watch, AVAHI_GCC_UNUSED int fd, AvahiWatchEvent events, void *userdata) {
114 DBusWatch *dbus_watch = userdata;
119 dbus_watch_handle(dbus_watch, translate_avahi_to_dbus(events));
120 /* Ignore the return value */
123 static dbus_bool_t update_watch(const AvahiPoll *poll_api, DBusWatch *dbus_watch) {
124 AvahiWatch *avahi_watch;
129 avahi_watch = dbus_watch_get_data(dbus_watch);
131 b = dbus_watch_get_enabled(dbus_watch);
133 if (b && !avahi_watch) {
135 if (!(avahi_watch = poll_api->watch_new(
137 dbus_watch_get_fd(dbus_watch),
138 translate_dbus_to_avahi(dbus_watch_get_flags(dbus_watch)),
143 dbus_watch_set_data(dbus_watch, avahi_watch, NULL);
145 } else if (!b && avahi_watch) {
147 poll_api->watch_free(avahi_watch);
148 dbus_watch_set_data(dbus_watch, NULL, NULL);
150 } else if (avahi_watch) {
153 poll_api->watch_update(avahi_watch, dbus_watch_get_flags(dbus_watch));
159 static dbus_bool_t add_watch(DBusWatch *dbus_watch, void *userdata) {
160 ConnectionData *d = userdata;
165 return update_watch(d->poll_api, dbus_watch);
168 static void remove_watch(DBusWatch *dbus_watch, void *userdata) {
169 ConnectionData *d = userdata;
170 AvahiWatch *avahi_watch;
175 if ((avahi_watch = dbus_watch_get_data(dbus_watch))) {
176 d->poll_api->watch_free(avahi_watch);
177 dbus_watch_set_data(dbus_watch, NULL, NULL);
181 static void watch_toggled(DBusWatch *dbus_watch, void *userdata) {
182 ConnectionData *d = userdata;
187 update_watch(d->poll_api, dbus_watch);
190 typedef struct TimeoutData {
191 const AvahiPoll *poll_api;
192 AvahiTimeout *avahi_timeout;
193 DBusTimeout *dbus_timeout;
197 static TimeoutData* timeout_data_ref(TimeoutData *t) {
205 static void timeout_data_unref(TimeoutData *t) {
210 if (t->avahi_timeout)
211 t->poll_api->timeout_free(t->avahi_timeout);
217 static void update_timeout(TimeoutData *timeout) {
219 assert(timeout->ref >= 1);
221 if (dbus_timeout_get_enabled(timeout->dbus_timeout)) {
223 avahi_elapse_time(&tv, dbus_timeout_get_interval(timeout->dbus_timeout), 0);
224 timeout->poll_api->timeout_update(timeout->
227 timeout->poll_api->timeout_update(timeout->avahi_timeout, NULL);
231 static void timeout_callback(AvahiTimeout *avahi_timeout, void *userdata) {
232 TimeoutData *timeout = userdata;
234 assert(avahi_timeout);
237 timeout_data_ref(timeout);
239 dbus_timeout_handle(timeout->dbus_timeout);
240 /* Ignore the return value */
242 if (timeout->avahi_timeout)
243 update_timeout(timeout);
245 timeout_data_unref(timeout);
248 static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {
249 TimeoutData *timeout;
250 ConnectionData *d = userdata;
254 assert(dbus_timeout);
257 if (!(timeout = avahi_new(TimeoutData, 1)))
260 timeout->dbus_timeout = dbus_timeout;
261 timeout->poll_api = d->poll_api;
264 if ((b = dbus_timeout_get_enabled(dbus_timeout)))
265 avahi_elapse_time(&tv, dbus_timeout_get_interval(dbus_timeout), 0);
267 if (!(timeout->avahi_timeout = d->poll_api->timeout_new(
276 dbus_timeout_set_data(dbus_timeout, timeout, (DBusFreeFunction) timeout_data_unref);
280 static void remove_timeout(DBusTimeout *dbus_timeout, void *userdata) {
281 ConnectionData *d = userdata;
282 TimeoutData *timeout;
284 assert(dbus_timeout);
287 timeout = dbus_timeout_get_data(dbus_timeout);
290 d->poll_api->timeout_free(timeout->avahi_timeout);
291 timeout->avahi_timeout = NULL;
294 static void timeout_toggled(DBusTimeout *dbus_timeout, AVAHI_GCC_UNUSED void *userdata) {
295 TimeoutData *timeout;
297 assert(dbus_timeout);
298 timeout = dbus_timeout_get_data(dbus_timeout);
301 update_timeout(timeout);
304 static void dispatch_status(AVAHI_GCC_UNUSED DBusConnection *connection, DBusDispatchStatus new_status, void *userdata) {
305 ConnectionData *d = userdata;
307 if (new_status == DBUS_DISPATCH_DATA_REMAINS)
308 request_dispatch(d, 1);
311 int avahi_dbus_connection_glue(DBusConnection *c, const AvahiPoll *poll_api) {
312 ConnectionData *d = NULL;
317 if (!(d = avahi_new(ConnectionData, 1)))
320 d->poll_api = poll_api;
324 if (!(d->dispatch_timeout = poll_api->timeout_new(poll_api, NULL, dispatch_timeout_callback, d)))
327 if (!(dbus_connection_set_watch_functions(c, add_watch, remove_watch, watch_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
330 if (!(dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, timeout_toggled, connection_data_ref(d), (DBusFreeFunction)connection_data_unref)))
333 dbus_connection_set_dispatch_status_function(c, dispatch_status, connection_data_ref(d), (DBusFreeFunction)connection_data_unref);
335 if (dbus_connection_get_dispatch_status(c) == DBUS_DISPATCH_DATA_REMAINS)
336 request_dispatch(d, 1);
338 connection_data_unref(d);
345 d->poll_api->timeout_free(d->dispatch_timeout);