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
24 #include <avahi-common/malloc.h>
26 #include "dbus-watch-glue.h"
28 static AvahiWatchEvent translate_dbus_to_avahi(unsigned int f) {
29 AvahiWatchEvent e = 0;
31 if (f & DBUS_WATCH_READABLE)
33 if (f & DBUS_WATCH_WRITABLE)
35 if (f & DBUS_WATCH_ERROR)
37 if (f & DBUS_WATCH_HANGUP)
43 static unsigned int translate_avahi_to_dbus(AvahiWatchEvent e) {
46 if (e & AVAHI_WATCH_IN)
47 f |= DBUS_WATCH_READABLE;
48 if (e & AVAHI_WATCH_OUT)
49 f |= DBUS_WATCH_WRITABLE;
50 if (e & AVAHI_WATCH_ERR)
51 f |= DBUS_WATCH_ERROR;
52 if (e & AVAHI_WATCH_HUP)
53 f |= DBUS_WATCH_HANGUP;
58 static void watch_callback(AvahiWatch *avahi_watch, int fd, AvahiWatchEvent events, void *userdata) {
59 DBusWatch *dbus_watch = userdata;
63 dbus_watch_handle(dbus_watch, translate_avahi_to_dbus(events));
64 /* Ignore the return value */
67 static dbus_bool_t update_watch(const AvahiPoll *poll_api, DBusWatch *dbus_watch) {
68 AvahiWatch *avahi_watch;
73 avahi_watch = dbus_watch_get_data(dbus_watch);
75 b = dbus_watch_get_enabled(dbus_watch);
77 if (b && !avahi_watch) {
79 if (!(avahi_watch = poll_api->watch_new(
81 dbus_watch_get_fd(dbus_watch),
82 translate_dbus_to_avahi(dbus_watch_get_flags(dbus_watch)),
87 dbus_watch_set_data(dbus_watch, avahi_watch, NULL);
89 } else if (!b && avahi_watch) {
91 poll_api->watch_free(avahi_watch);
92 dbus_watch_set_data(dbus_watch, NULL, NULL);
94 } else if (avahi_watch) {
97 poll_api->watch_update(avahi_watch, dbus_watch_get_flags(dbus_watch));
103 static dbus_bool_t add_watch(DBusWatch *dbus_watch, void *userdata) {
104 const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
109 return update_watch(poll_api, dbus_watch);
112 static void remove_watch(DBusWatch *dbus_watch, void *userdata) {
113 const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
114 AvahiWatch *avahi_watch;
119 if ((avahi_watch = dbus_watch_get_data(dbus_watch))) {
120 poll_api->watch_free(avahi_watch);
121 dbus_watch_set_data(dbus_watch, NULL, NULL);
125 static void watch_toggled(DBusWatch *dbus_watch, void *userdata) {
126 const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
131 update_watch(poll_api, dbus_watch);
134 typedef struct TimeoutData {
135 const AvahiPoll *poll_api;
136 AvahiTimeout *avahi_timeout;
137 DBusTimeout *dbus_timeout;
140 static void update_timeout(TimeoutData *timeout) {
143 if (dbus_timeout_get_enabled(timeout->dbus_timeout)) {
145 avahi_elapse_time(&tv, dbus_timeout_get_interval(timeout->dbus_timeout), 0);
146 timeout->poll_api->timeout_update(timeout->
149 timeout->poll_api->timeout_update(timeout->avahi_timeout, NULL);
153 static void timeout_callback(AvahiTimeout *avahi_timeout, void *userdata) {
154 TimeoutData *timeout = userdata;
156 assert(avahi_timeout);
159 dbus_timeout_handle(timeout->dbus_timeout);
160 /* Ignore the return value */
162 update_timeout(timeout);
165 static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {
166 TimeoutData *timeout;
167 const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
171 assert(dbus_timeout);
174 if (!(timeout = avahi_new(TimeoutData, 1)))
177 timeout->dbus_timeout = dbus_timeout;
178 timeout->poll_api = poll_api;
180 if ((b = dbus_timeout_get_enabled(dbus_timeout)))
181 avahi_elapse_time(&tv, dbus_timeout_get_interval(dbus_timeout), 0);
183 if (!(timeout->avahi_timeout = poll_api->timeout_new(
192 dbus_timeout_set_data(dbus_timeout, timeout, NULL);
196 static void remove_timeout(DBusTimeout *dbus_timeout, void *userdata) {
197 TimeoutData *timeout;
198 const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
200 assert(dbus_timeout);
203 timeout = dbus_timeout_get_data(dbus_timeout);
206 poll_api->timeout_free(timeout->avahi_timeout);
208 dbus_timeout_set_data(dbus_timeout, NULL, NULL);
211 static void timeout_toggled(DBusTimeout *dbus_timeout, void *userdata) {
212 TimeoutData *timeout;
213 const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
215 assert(dbus_timeout);
218 timeout = dbus_timeout_get_data(dbus_timeout);
221 update_timeout(timeout);
224 int avahi_dbus_connection_glue(DBusConnection *c, const AvahiPoll *poll_api) {
228 if (!(dbus_connection_set_watch_functions(c, add_watch, remove_watch, watch_toggled, (void*) poll_api, NULL)))
231 if (!(dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, timeout_toggled, (void*) poll_api, NULL)))