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
26 #include <avahi-common/llist.h>
27 #include <avahi-common/malloc.h>
28 #include <avahi-common/timeval.h>
30 #include "glib-watch.h"
33 AvahiGLibPoll *glib_poll;
39 AvahiWatchCallback callback;
42 AVAHI_LLIST_FIELDS(AvahiWatch, watches);
46 AvahiGLibPoll *glib_poll;
50 struct timeval expiry;
52 AvahiTimeoutCallback callback;
55 AVAHI_LLIST_FIELDS(AvahiTimeout, timeouts);
58 struct AvahiGLibPoll {
61 GMainContext *context;
63 gboolean timeout_req_cleanup;
64 gboolean watch_req_cleanup;
66 AVAHI_LLIST_HEAD(AvahiWatch, watches);
67 AVAHI_LLIST_HEAD(AvahiTimeout, timeouts);
70 static void destroy_watch(AvahiWatch *w) {
74 g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
76 AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->glib_poll->watches, w);
81 static void cleanup_watches(AvahiGLibPoll *g, int all) {
85 for (w = g->watches; w; w = next) {
86 next = w->watches_next;
92 g->watch_req_cleanup = 0;
95 static gushort map_events_to_glib(AvahiWatchEvent events) {
97 (events & AVAHI_WATCH_IN ? G_IO_IN : 0) |
98 (events & AVAHI_WATCH_OUT ? G_IO_OUT : 0) |
99 (events & AVAHI_WATCH_ERR ? G_IO_ERR : 0) |
100 (events & AVAHI_WATCH_HUP ? G_IO_HUP : 0);
103 static AvahiWatchEvent map_events_from_glib(gushort events) {
105 (events & G_IO_IN ? AVAHI_WATCH_IN : 0) |
106 (events & G_IO_OUT ? AVAHI_WATCH_OUT : 0) |
107 (events & G_IO_ERR ? AVAHI_WATCH_ERR : 0) |
108 (events & G_IO_HUP ? AVAHI_WATCH_HUP : 0);
111 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent events, AvahiWatchCallback callback, void *userdata) {
122 if (!(w = avahi_new(AvahiWatch, 1)))
127 w->pollfd.events = map_events_to_glib(events);
128 w->pollfd.revents = 0;
129 w->callback = callback;
130 w->userdata = userdata;
133 g_source_add_poll(&g->source, &w->pollfd);
134 w->pollfd_added = TRUE;
136 AVAHI_LLIST_PREPEND(AvahiWatch, watches, g->watches, w);
141 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
145 w->pollfd.events = map_events_to_glib(events);
148 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
152 return map_events_from_glib(w->pollfd.revents);
155 static void watch_free(AvahiWatch *w) {
159 if (w->pollfd_added) {
160 g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
161 w->pollfd_added = FALSE;
165 w->glib_poll->timeout_req_cleanup = TRUE;
168 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
178 if (!(t = avahi_new(AvahiTimeout, 1)))
184 if ((t->enabled = !!tv))
187 t->callback = callback;
188 t->userdata = userdata;
190 AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, g->timeouts, t);
195 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
199 if ((t->enabled = !!tv))
203 static void timeout_free(AvahiTimeout *t) {
208 t->glib_poll->timeout_req_cleanup = TRUE;
211 static void destroy_timeout(AvahiTimeout *t) {
214 AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->glib_poll->timeouts, t);
218 static void cleanup_timeouts(AvahiGLibPoll *g, int all) {
219 AvahiTimeout *t, *next;
222 for (t = g->timeouts; t; t = next) {
223 next = t->timeouts_next;
229 g->timeout_req_cleanup = FALSE;
232 static AvahiTimeout* find_next_timeout(AvahiGLibPoll *g) {
233 AvahiTimeout *t, *n = NULL;
236 for (t = g->timeouts; t; t = t->timeouts_next) {
238 if (t->dead || !t->enabled)
241 if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
248 static void start_timeout_callback(AvahiTimeout *t) {
254 t->callback(t, t->userdata);
257 static gboolean prepare_func(GSource *source, gint *timeout) {
258 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
259 AvahiTimeout *next_timeout;
264 if (g->watch_req_cleanup)
265 cleanup_watches(g, 0);
267 if (g->timeout_req_cleanup)
268 cleanup_timeouts(g, 0);
270 if ((next_timeout = find_next_timeout(g))) {
272 struct timeval tvnow;
275 g_source_get_current_time(source, &now);
276 tvnow.tv_sec = now.tv_sec;
277 tvnow.tv_usec = now.tv_usec;
279 usec = avahi_timeval_diff(&next_timeout->expiry, &tvnow);
286 *timeout = (gint) (usec / 1000);
293 static gboolean check_func(GSource *source) {
294 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
296 AvahiTimeout *next_timeout;
300 if ((next_timeout = find_next_timeout(g))) {
302 struct timeval tvnow;
303 g_source_get_current_time(source, &now);
304 tvnow.tv_sec = now.tv_sec;
305 tvnow.tv_usec = now.tv_usec;
307 if (avahi_timeval_compare(&next_timeout->expiry, &tvnow) <= 0)
311 for (w = g->watches; w; w = w->watches_next)
312 if (w->pollfd.revents > 0)
318 static gboolean dispatch_func(GSource *source, AVAHI_GCC_UNUSED GSourceFunc callback, AVAHI_GCC_UNUSED gpointer userdata) {
319 AvahiGLibPoll* g = (AvahiGLibPoll*) source;
321 AvahiTimeout *next_timeout;
325 if ((next_timeout = find_next_timeout(g))) {
327 struct timeval tvnow;
328 g_source_get_current_time(source, &now);
329 tvnow.tv_sec = now.tv_sec;
330 tvnow.tv_usec = now.tv_usec;
332 if (avahi_timeval_compare(&next_timeout->expiry, &tvnow) < 0) {
333 start_timeout_callback(next_timeout);
338 for (w = g->watches; w; w = w->watches_next)
339 if (w->pollfd.revents > 0) {
341 w->callback(w, w->pollfd.fd, map_events_from_glib(w->pollfd.revents), w->userdata);
342 w->pollfd.revents = 0;
349 AvahiGLibPoll *avahi_glib_poll_new(GMainContext *context, gint priority) {
352 static GSourceFuncs source_funcs = {
361 g = (AvahiGLibPoll*) g_source_new(&source_funcs, sizeof(AvahiGLibPoll));
362 g_main_context_ref(g->context = context ? context : g_main_context_default());
366 g->api.watch_new = watch_new;
367 g->api.watch_free = watch_free;
368 g->api.watch_update = watch_update;
369 g->api.watch_get_events = watch_get_events;
371 g->api.timeout_new = timeout_new;
372 g->api.timeout_free = timeout_free;
373 g->api.timeout_update = timeout_update;
375 g->watch_req_cleanup = FALSE;
376 g->timeout_req_cleanup = FALSE;
378 AVAHI_LLIST_HEAD_INIT(AvahiWatch, g->watches);
379 AVAHI_LLIST_HEAD_INIT(AvahiTimeout, g->timeouts);
381 g_source_attach(&g->source, g->context);
382 g_source_set_priority(&g->source, priority);
383 g_source_set_can_recurse(&g->source, FALSE);
388 void avahi_glib_poll_free(AvahiGLibPoll *g) {
389 GSource *s = &g->source;
392 cleanup_watches(g, 1);
393 cleanup_timeouts(g, 1);
395 g_main_context_unref(g->context);
400 const AvahiPoll* avahi_glib_poll_get(AvahiGLibPoll *g) {