]> git.meshlink.io Git - catta/blob - avahi-client/dbus-watch-glue.c
* add new priority parameter to avahi_glib_poll_new()
[catta] / avahi-client / 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
24 #include <avahi-common/malloc.h>
25
26 #include "dbus-watch-glue.h"
27
28 static AvahiWatchEvent translate_dbus_to_avahi(unsigned int f) {
29     AvahiWatchEvent e = 0;
30
31     if (f & DBUS_WATCH_READABLE)
32         e |= AVAHI_WATCH_IN;
33     if (f & DBUS_WATCH_WRITABLE)
34         e |= AVAHI_WATCH_OUT;
35     if (f & DBUS_WATCH_ERROR)
36         e |= AVAHI_WATCH_ERR;
37     if (f & DBUS_WATCH_HANGUP)
38         e |= AVAHI_WATCH_HUP;
39
40     return e;
41 }
42
43 static unsigned int translate_avahi_to_dbus(AvahiWatchEvent e) {
44     unsigned int f = 0;
45
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;
54
55     return f;
56 }
57
58 static void watch_callback(AvahiWatch *avahi_watch, int fd, AvahiWatchEvent events, void *userdata) {
59     DBusWatch *dbus_watch = userdata;
60     assert(avahi_watch);
61     assert(dbus_watch);
62
63     dbus_watch_handle(dbus_watch, translate_avahi_to_dbus(events));
64     /* Ignore the return value */
65 }
66
67 static dbus_bool_t update_watch(const AvahiPoll *poll_api, DBusWatch *dbus_watch) {
68     AvahiWatch *avahi_watch;
69     dbus_bool_t b;
70     
71     assert(dbus_watch);
72
73     avahi_watch = dbus_watch_get_data(dbus_watch);
74
75     b = dbus_watch_get_enabled(dbus_watch);
76     
77     if (b && !avahi_watch) {
78
79         if (!(avahi_watch = poll_api->watch_new(
80                   poll_api,
81                   dbus_watch_get_fd(dbus_watch),
82                   translate_dbus_to_avahi(dbus_watch_get_flags(dbus_watch)),
83                   watch_callback,
84                   dbus_watch)))
85             return FALSE;
86
87         dbus_watch_set_data(dbus_watch, avahi_watch, (DBusFreeFunction) poll_api->watch_free);
88         
89     } else if (!b && avahi_watch) {
90         
91         poll_api->watch_free(avahi_watch);
92         dbus_watch_set_data(dbus_watch, NULL, NULL);
93
94     } else if (avahi_watch) {
95         
96         /* Update flags */
97         poll_api->watch_update(avahi_watch, dbus_watch_get_flags(dbus_watch));
98     }
99
100     return TRUE;
101 }
102
103 static dbus_bool_t add_watch(DBusWatch *dbus_watch, void *userdata) {
104     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
105     
106     assert(dbus_watch);
107     assert(poll_api);
108
109     return update_watch(poll_api, dbus_watch);
110 }
111
112 static void remove_watch(DBusWatch *dbus_watch, void *userdata) {
113     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
114     AvahiWatch *avahi_watch;
115     
116     assert(dbus_watch);
117     assert(poll_api);
118
119     avahi_watch = dbus_watch_get_data(dbus_watch);
120     poll_api->watch_free(avahi_watch);
121     dbus_watch_set_data(dbus_watch, NULL, NULL);
122 }
123
124 static void watch_toggled(DBusWatch *dbus_watch, void *userdata) {
125     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
126     
127     assert(dbus_watch);
128     assert(poll_api);
129
130     update_watch(poll_api, dbus_watch);
131 }
132
133 typedef struct TimeoutData {
134     const AvahiPoll *poll_api;
135     AvahiTimeout *avahi_timeout;
136     DBusTimeout *dbus_timeout;
137 } TimeoutData;
138
139 static void update_timeout(TimeoutData *timeout) {
140     assert(timeout);
141     
142     if (dbus_timeout_get_enabled(timeout->dbus_timeout)) {
143         struct timeval tv;
144         avahi_elapse_time(&tv, dbus_timeout_get_interval(timeout->dbus_timeout), 0);
145         timeout->poll_api->timeout_update(timeout->
146                                       avahi_timeout, &tv);
147     } else
148         timeout->poll_api->timeout_update(timeout->avahi_timeout, NULL);
149
150 }
151
152 static void timeout_callback(AvahiTimeout *avahi_timeout, void *userdata) {
153     TimeoutData *timeout = userdata;
154     
155     assert(avahi_timeout);
156     assert(timeout);
157
158     dbus_timeout_handle(timeout->dbus_timeout);
159     /* Ignore the return value */
160     
161     update_timeout(timeout);
162 }
163
164 static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {
165     TimeoutData *timeout;
166     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
167     struct timeval tv;
168     dbus_bool_t b;
169
170     assert(dbus_timeout);
171     assert(poll_api);
172
173     if (!(timeout = avahi_new(TimeoutData, 1)))
174         return FALSE;
175
176     timeout->dbus_timeout = dbus_timeout;
177     timeout->poll_api = poll_api;
178
179     if ((b = dbus_timeout_get_enabled(dbus_timeout)))
180         avahi_elapse_time(&tv, dbus_timeout_get_interval(dbus_timeout), 0);
181     
182     if (!(timeout->avahi_timeout = poll_api->timeout_new(
183               poll_api,
184               b ? &tv : NULL,
185               timeout_callback,
186               dbus_timeout))) {
187         avahi_free(timeout);
188         return FALSE;
189     }
190
191     dbus_timeout_set_data(dbus_timeout, timeout, NULL);
192     return TRUE;
193 }
194
195 static void remove_timeout(DBusTimeout *dbus_timeout, void *userdata) {
196     TimeoutData *timeout;
197     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
198
199     assert(dbus_timeout);
200     assert(poll_api);
201
202     timeout = dbus_timeout_get_data(dbus_timeout);
203     assert(timeout);
204
205     poll_api->timeout_free(timeout->avahi_timeout);
206     avahi_free(timeout);
207     dbus_timeout_set_data(dbus_timeout, NULL, NULL);
208 }
209
210 static void timeout_toggled(DBusTimeout *dbus_timeout, void *userdata) {
211     TimeoutData *timeout;
212     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
213
214     assert(dbus_timeout);
215     assert(poll_api);
216
217     timeout = dbus_timeout_get_data(dbus_timeout);
218     assert(timeout);
219
220     update_timeout(timeout);
221 }
222
223 int avahi_dbus_connection_glue(DBusConnection *c, const AvahiPoll *poll_api) {
224     assert(c);
225     assert(poll_api);
226
227     if (!(dbus_connection_set_watch_functions(c, add_watch, remove_watch, watch_toggled, (void*) poll_api, NULL)))
228         return -1;
229
230     if (!(dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, timeout_toggled, (void*) poll_api, NULL)))
231         return -1;
232
233     return 0;
234 }