summaryrefslogtreecommitdiff
path: root/src/lib/ecore_avahi/ecore_avahi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/ecore_avahi/ecore_avahi.c')
-rw-r--r--src/lib/ecore_avahi/ecore_avahi.c247
1 files changed, 247 insertions, 0 deletions
diff --git a/src/lib/ecore_avahi/ecore_avahi.c b/src/lib/ecore_avahi/ecore_avahi.c
new file mode 100644
index 0000000..93c5301
--- /dev/null
+++ b/src/lib/ecore_avahi/ecore_avahi.c
@@ -0,0 +1,247 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include <Eina.h>
6#include <Ecore.h>
7
8#include "Ecore_Avahi.h"
9
10#ifdef HAVE_AVAHI
11#include <avahi-common/watch.h>
12
13typedef struct _Ecore_Avahi_Watch Ecore_Avahi_Watch;
14typedef struct _Ecore_Avahi_Timeout Ecore_Avahi_Timeout;
15
16struct _Ecore_Avahi_Watch
17{
18 Ecore_Fd_Handler *handler;
19 Ecore_Avahi *parent;
20
21 AvahiWatchCallback callback;
22 void *callback_data;
23};
24
25struct _Ecore_Avahi_Timeout
26{
27 Ecore_Timer *timer;
28 Ecore_Avahi *parent;
29
30 AvahiTimeoutCallback callback;
31 void *callback_data;
32};
33
34struct _Ecore_Avahi
35{
36 AvahiPoll api;
37
38 Eina_List *watches;
39 Eina_List *timeouts;
40};
41
42static Ecore_Fd_Handler_Flags
43_ecore_avahi_events2ecore(AvahiWatchEvent events)
44{
45 return (events & AVAHI_WATCH_IN ? ECORE_FD_READ : 0) |
46 (events & AVAHI_WATCH_OUT ? ECORE_FD_WRITE : 0) |
47 (events & AVAHI_WATCH_ERR ? ECORE_FD_ERROR : 0);
48}
49
50static Eina_Bool
51_ecore_avahi_watch_cb(void *data, Ecore_Fd_Handler *fd_handler)
52{
53 Ecore_Avahi_Watch *watch = data;
54 AvahiWatchEvent flags = 0;
55
56 flags = ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ) ? AVAHI_WATCH_IN : 0;
57 flags |= ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_WRITE) ? AVAHI_WATCH_OUT : 0;
58 flags |= ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR) ? AVAHI_WATCH_ERR : 0;
59
60 watch->callback((AvahiWatch*) watch, ecore_main_fd_handler_fd_get(fd_handler), flags, watch->callback_data);
61
62 return ECORE_CALLBACK_RENEW;
63}
64
65static AvahiWatch *
66_ecore_avahi_watch_new(const AvahiPoll *api,
67 int fd, AvahiWatchEvent events,
68 AvahiWatchCallback callback, void *userdata)
69{
70 Ecore_Avahi_Watch *watch;
71 Ecore_Avahi *ea;
72
73 ea = api->userdata;
74 watch = calloc(1, sizeof (Ecore_Avahi_Watch));
75 if (!watch) return NULL;
76
77 watch->handler = ecore_main_fd_handler_add(fd, _ecore_avahi_events2ecore(events),
78 _ecore_avahi_watch_cb, watch, NULL, NULL);
79 watch->callback = callback;
80 watch->callback_data = userdata;
81 watch->parent = ea;
82
83 ea->watches = eina_list_append(ea->watches, watch);
84
85 return (AvahiWatch*) watch;
86}
87
88static void
89_ecore_avahi_watch_update(AvahiWatch *w, AvahiWatchEvent events)
90{
91 Ecore_Avahi_Watch *watch = (Ecore_Avahi_Watch *) w;
92
93 ecore_main_fd_handler_active_set(watch->handler, _ecore_avahi_events2ecore(events));
94}
95
96static void
97_ecore_avahi_watch_free(AvahiWatch *w)
98{
99 Ecore_Avahi_Watch *watch = (Ecore_Avahi_Watch *) w;
100
101 ecore_main_fd_handler_del(watch->handler);
102 watch->parent->watches = eina_list_remove(watch->parent->watches, watch);
103 free(watch);
104}
105
106static AvahiWatchEvent
107_ecore_avahi_watch_get_events(AvahiWatch *w)
108{
109 Ecore_Avahi_Watch *watch = (Ecore_Avahi_Watch *) w;
110 AvahiWatchEvent flags = 0;
111
112 flags = ecore_main_fd_handler_active_get(watch->handler, ECORE_FD_READ) ? AVAHI_WATCH_IN : 0;
113 flags |= ecore_main_fd_handler_active_get(watch->handler, ECORE_FD_WRITE) ? AVAHI_WATCH_OUT : 0;
114 flags |= ecore_main_fd_handler_active_get(watch->handler, ECORE_FD_ERROR) ? AVAHI_WATCH_ERR : 0;
115
116 return flags;
117}
118
119static double
120_ecore_avahi_timeval2double(const struct timeval *tv)
121{
122 if (!tv) return 3600;
123 return tv->tv_sec + (double) tv->tv_usec / 1000000;
124}
125
126static Eina_Bool
127_ecore_avahi_timeout_cb(void *data)
128{
129 Ecore_Avahi_Timeout *timeout = data;
130
131 ecore_timer_freeze(timeout->timer);
132 timeout->callback((AvahiTimeout*) timeout, timeout->callback_data);
133
134 return ECORE_CALLBACK_RENEW;
135}
136
137static AvahiTimeout *
138_ecore_avahi_timeout_new(const AvahiPoll *api, const struct timeval *tv,
139 AvahiTimeoutCallback callback, void *userdata)
140{
141 Ecore_Avahi_Timeout *timeout;
142 Ecore_Avahi *ea;
143
144 ea = api->userdata;
145 timeout = calloc(1, sizeof (Ecore_Avahi_Timeout));
146 if (!timeout) return NULL;
147
148 timeout->timer = ecore_timer_add(_ecore_avahi_timeval2double(tv), _ecore_avahi_timeout_cb, timeout);
149 if (!tv) ecore_timer_freeze(timeout->timer);
150 timeout->callback = callback;
151 timeout->callback_data = userdata;
152 timeout->parent = ea;
153
154 ea->timeouts = eina_list_append(ea->timeouts, timeout);
155
156 return (AvahiTimeout*) timeout;
157}
158
159static void
160_ecore_avahi_timeout_update(AvahiTimeout *t, const struct timeval *tv)
161{
162 Ecore_Avahi_Timeout *timeout = (Ecore_Avahi_Timeout *) t;
163
164 if (tv)
165 {
166 ecore_timer_interval_set(timeout->timer, _ecore_avahi_timeval2double(tv));
167 if (ecore_timer_freeze_get(timeout->timer))
168 ecore_timer_thaw(timeout->timer);
169 }
170 else
171 {
172 ecore_timer_freeze(timeout->timer);
173 }
174}
175
176static void
177_ecore_avahi_timeout_free(AvahiTimeout *t)
178{
179 Ecore_Avahi_Timeout *timeout = (Ecore_Avahi_Timeout *) t;
180
181 ecore_timer_del(timeout->timer);
182 timeout->parent->timeouts = eina_list_remove(timeout->parent->timeouts, timeout);
183 free(timeout);
184}
185#endif
186
187EAPI Ecore_Avahi *
188ecore_avahi_add(void)
189{
190#ifdef HAVE_AVAHI
191 Ecore_Avahi *handler;
192
193 handler = calloc(1, sizeof (Ecore_Avahi));
194 if (!handler) return NULL;
195
196 handler->api.userdata = handler;
197 handler->api.watch_new = _ecore_avahi_watch_new;
198 handler->api.watch_free = _ecore_avahi_watch_free;
199 handler->api.watch_update = _ecore_avahi_watch_update;
200 handler->api.watch_get_events = _ecore_avahi_watch_get_events;
201
202 handler->api.timeout_new = _ecore_avahi_timeout_new;
203 handler->api.timeout_free = _ecore_avahi_timeout_free;
204 handler->api.timeout_update = _ecore_avahi_timeout_update;
205
206 return handler;
207#else
208 return NULL;
209#endif
210}
211
212EAPI void
213ecore_avahi_del(Ecore_Avahi *handler)
214{
215#ifdef HAVE_AVAHI
216 Ecore_Avahi_Timeout *timeout;
217 Ecore_Avahi_Watch *watch;
218
219 EINA_LIST_FREE(handler->watches, watch)
220 {
221 ecore_main_fd_handler_del(watch->handler);
222 free(watch);
223 }
224
225 EINA_LIST_FREE(handler->timeouts, timeout)
226 {
227 ecore_timer_del(timeout->timer);
228 free(timeout);
229 }
230
231 free(handler);
232#else
233 (void) handler;
234#endif
235}
236
237EAPI const void *
238ecore_avahi_poll_get(Ecore_Avahi *handler)
239{
240#ifdef HAVE_AVAHI
241 if (!handler) return NULL;
242 return &handler->api;
243#else
244 return NULL;
245#endif
246}
247