summaryrefslogtreecommitdiff
path: root/src/lib/ecore_avahi
diff options
context:
space:
mode:
authorCedric BAIL <cedric@efl.so>2013-11-10 08:51:42 +0100
committerCedric BAIL <cedric.bail@samsung.com>2013-12-17 19:37:05 +0900
commit0a1cc79a917b770ef66afe8b99731f6d5754ca44 (patch)
tree99d52907af9d893cb1fc08d75cd1b9f2ab2bfd0c /src/lib/ecore_avahi
parent99e3ce489adbdc711571fa7d71acbd03a413ec00 (diff)
Ecore_Avahi: initial commit.
TODO: - Add tests, how ? - Integrate with Eo, needed ?
Diffstat (limited to 'src/lib/ecore_avahi')
-rw-r--r--src/lib/ecore_avahi/Ecore_Avahi.h88
-rw-r--r--src/lib/ecore_avahi/ecore_avahi.c247
2 files changed, 335 insertions, 0 deletions
diff --git a/src/lib/ecore_avahi/Ecore_Avahi.h b/src/lib/ecore_avahi/Ecore_Avahi.h
new file mode 100644
index 0000000..a085e07
--- /dev/null
+++ b/src/lib/ecore_avahi/Ecore_Avahi.h
@@ -0,0 +1,88 @@
1/**
2 @brief Ecore Avahi integration Library Public API Calls
3
4 These routines are used for integrating Avahi with Ecore main loop
5 */
6
7#ifndef _ECORE_AVAHI_H
8# define _ECORE_AVAHI_H
9
10#ifdef EAPI
11# undef EAPI
12#endif
13
14#ifdef _WIN32
15# ifdef EFL_ECORE_BUILD
16# ifdef DLL_EXPORT
17# define EAPI __declspec(dllexport)
18# else
19# define EAPI
20# endif /* ! DLL_EXPORT */
21# else
22# define EAPI __declspec(dllimport)
23# endif /* ! EFL_ECORE_BUILD */
24#else
25# ifdef __GNUC__
26# if __GNUC__ >= 4
27# define EAPI __attribute__ ((visibility("default")))
28# else
29# define EAPI
30# endif
31# else
32# define EAPI
33# endif
34#endif /* ! _WIN32 */
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/**
41 * @defgroup Ecore_Avahi Ecore main loop integration function.
42 * @ingroup Ecore
43 *
44 * @{
45 */
46
47/**
48 * @since 1.9
49 */
50typedef struct _Ecore_Avahi Ecore_Avahi; /**< A handle for an Avahi instance. */
51
52/**
53 * @brief Create an AvahiPoll context and integrate it within Ecore main loop.
54 *
55 * @return A handler that reference the AvahiPoll context
56 * @since 1.9
57 */
58EAPI Ecore_Avahi *ecore_avahi_add(void);
59
60/**
61 * @brief Delete the specified handler of an AvahiPoll.
62 *
63 * @param handler The actual handler to destroy.
64 * @since 1.9
65 *
66 * Be aware there should not be any reference still using that handler before
67 * destroying it.
68 */
69EAPI void ecore_avahi_del(Ecore_Avahi *handler);
70
71/**
72 * @brief Get the AvahiPoll structure to integrate with Ecore main loop.
73 *
74 * @param handler The handler to get the AvahiPoll structure from.
75 * @return return the actual AvahiPoll structure to use with Avahi.
76 * @since 1.9
77 */
78EAPI const void *ecore_avahi_poll_get(Ecore_Avahi *handler); // return AvahiPoll
79
80/**
81 * @}
82 */
83
84#ifdef __cplusplus
85}
86#endif
87
88#endif
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