]> git.meshlink.io Git - catta/blob - avahi-common/dbus-watch-glue.c
* implement ini file parser
[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
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, NULL);
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     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);
122     }
123 }
124
125 static void watch_toggled(DBusWatch *dbus_watch, void *userdata) {
126     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
127     
128     assert(dbus_watch);
129     assert(poll_api);
130
131     update_watch(poll_api, dbus_watch);
132 }
133
134 typedef struct TimeoutData {
135     const AvahiPoll *poll_api;
136     AvahiTimeout *avahi_timeout;
137     DBusTimeout *dbus_timeout;
138 } TimeoutData;
139
140 static void update_timeout(TimeoutData *timeout) {
141     assert(timeout);
142     
143     if (dbus_timeout_get_enabled(timeout->dbus_timeout)) {
144         struct timeval tv;
145         avahi_elapse_time(&tv, dbus_timeout_get_interval(timeout->dbus_timeout), 0);
146         timeout->poll_api->timeout_update(timeout->
147                                       avahi_timeout, &tv);
148     } else
149         timeout->poll_api->timeout_update(timeout->avahi_timeout, NULL);
150
151 }
152
153 static void timeout_callback(AvahiTimeout *avahi_timeout, void *userdata) {
154     TimeoutData *timeout = userdata;
155     
156     assert(avahi_timeout);
157     assert(timeout);
158
159     dbus_timeout_handle(timeout->dbus_timeout);
160     /* Ignore the return value */
161     
162     update_timeout(timeout);
163 }
164
165 static dbus_bool_t add_timeout(DBusTimeout *dbus_timeout, void *userdata) {
166     TimeoutData *timeout;
167     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
168     struct timeval tv;
169     dbus_bool_t b;
170
171     assert(dbus_timeout);
172     assert(poll_api);
173
174     if (!(timeout = avahi_new(TimeoutData, 1)))
175         return FALSE;
176
177     timeout->dbus_timeout = dbus_timeout;
178     timeout->poll_api = poll_api;
179
180     if ((b = dbus_timeout_get_enabled(dbus_timeout)))
181         avahi_elapse_time(&tv, dbus_timeout_get_interval(dbus_timeout), 0);
182     
183     if (!(timeout->avahi_timeout = poll_api->timeout_new(
184               poll_api,
185               b ? &tv : NULL,
186               timeout_callback,
187               dbus_timeout))) {
188         avahi_free(timeout);
189         return FALSE;
190     }
191
192     dbus_timeout_set_data(dbus_timeout, timeout, NULL);
193     return TRUE;
194 }
195
196 static void remove_timeout(DBusTimeout *dbus_timeout, void *userdata) {
197     TimeoutData *timeout;
198     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
199
200     assert(dbus_timeout);
201     assert(poll_api);
202
203     timeout = dbus_timeout_get_data(dbus_timeout);
204     assert(timeout);
205
206     poll_api->timeout_free(timeout->avahi_timeout);
207     avahi_free(timeout);
208     dbus_timeout_set_data(dbus_timeout, NULL, NULL);
209 }
210
211 static void timeout_toggled(DBusTimeout *dbus_timeout, void *userdata) {
212     TimeoutData *timeout;
213     const AvahiPoll *poll_api = (const AvahiPoll*) userdata;
214
215     assert(dbus_timeout);
216     assert(poll_api);
217
218     timeout = dbus_timeout_get_data(dbus_timeout);
219     assert(timeout);
220
221     update_timeout(timeout);
222 }
223
224 int avahi_dbus_connection_glue(DBusConnection *c, const AvahiPoll *poll_api) {
225     assert(c);
226     assert(poll_api);
227
228     if (!(dbus_connection_set_watch_functions(c, add_watch, remove_watch, watch_toggled, (void*) poll_api, NULL)))
229         return -1;
230
231     if (!(dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, timeout_toggled, (void*) poll_api, NULL)))
232         return -1;
233
234     return 0;
235 }