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 AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
106 if (!(w = avahi_new(AvahiWatch, 1)))
112 (event & AVAHI_WATCH_IN ? G_IO_IN : 0) |
113 (event & AVAHI_WATCH_OUT ? G_IO_OUT : 0) |
114 (event & AVAHI_WATCH_ERR ? G_IO_ERR : 0) |
115 (event & AVAHI_WATCH_HUP ? G_IO_HUP : 0);
116 w->pollfd.revents = 0;
117 w->callback = callback;
118 w->userdata = userdata;
121 g_source_add_poll(&g->source, &w->pollfd);
122 w->pollfd_added = TRUE;
124 AVAHI_LLIST_PREPEND(AvahiWatch, watches, g->watches, w);
129 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
133 w->pollfd.events = events;
136 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
140 return w->pollfd.revents;
143 static void watch_free(AvahiWatch *w) {
147 if (w->pollfd_added) {
148 g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
149 w->pollfd_added = TRUE;
153 w->glib_poll->timeout_req_cleanup = TRUE;
156 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
166 if (!(t = avahi_new(AvahiTimeout, 1)))
172 if ((t->enabled = !!tv))
175 t->callback = callback;
176 t->userdata = userdata;
178 AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, g->timeouts, t);
183 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
187 if ((t->enabled = !!tv))
191 static void timeout_free(AvahiTimeout *t) {
196 t->glib_poll->timeout_req_cleanup = TRUE;
199 static void destroy_timeout(AvahiTimeout *t) {
202 AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->glib_poll->timeouts, t);
206 static void cleanup_timeouts(AvahiGLibPoll *g, int all) {
207 AvahiTimeout *t, *next;
210 for (t = g->timeouts; t; t = next) {
211 next = t->timeouts_next;
217 g->timeout_req_cleanup = FALSE;
220 static AvahiTimeout* find_next_timeout(AvahiGLibPoll *g) {
221 AvahiTimeout *t, *n = NULL;
224 for (t = g->timeouts; t; t = t->timeouts_next) {
226 if (t->dead || !t->enabled)
229 if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
236 static void start_timeout_callback(AvahiTimeout *t) {
242 t->callback(t, t->userdata);
245 static gboolean prepare_func(GSource *source, gint *timeout) {
246 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
247 AvahiTimeout *next_timeout;
252 if (g->watch_req_cleanup)
253 cleanup_watches(g, 0);
255 if (g->timeout_req_cleanup)
256 cleanup_timeouts(g, 0);
258 if ((next_timeout = find_next_timeout(g))) {
260 struct timeval tvnow;
263 g_source_get_current_time(source, &now);
264 tvnow.tv_sec = now.tv_sec;
265 tvnow.tv_usec = now.tv_usec;
267 usec = avahi_timeval_diff(&next_timeout->expiry, &tvnow);
272 *timeout = (gint) (usec / 1000);
278 static gboolean check_func(GSource *source) {
279 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
281 AvahiTimeout *next_timeout;
285 if ((next_timeout = find_next_timeout(g))) {
287 struct timeval tvnow;
288 g_source_get_current_time(source, &now);
289 tvnow.tv_sec = now.tv_sec;
290 tvnow.tv_usec = now.tv_usec;
292 if (avahi_timeval_compare(&next_timeout->expiry, &tvnow) < 0)
296 for (w = g->watches; w; w = w->watches_next)
297 if (w->pollfd.revents > 0)
303 static gboolean dispatch_func(GSource *source, AVAHI_GCC_UNUSED GSourceFunc callback, AVAHI_GCC_UNUSED gpointer userdata) {
304 AvahiGLibPoll* g = (AvahiGLibPoll*) source;
306 AvahiTimeout *next_timeout;
310 if ((next_timeout = find_next_timeout(g))) {
312 struct timeval tvnow;
313 g_source_get_current_time(source, &now);
314 tvnow.tv_sec = now.tv_sec;
315 tvnow.tv_usec = now.tv_usec;
317 if (avahi_timeval_compare(&next_timeout->expiry, &tvnow) < 0) {
318 start_timeout_callback(next_timeout);
323 for (w = g->watches; w; w = w->watches_next)
324 if (w->pollfd.revents > 0) {
326 w->callback(w, w->pollfd.fd, w->pollfd.revents, w->userdata);
327 w->pollfd.revents = 0;
334 AvahiGLibPoll *avahi_glib_poll_new(GMainContext *context, gint priority) {
337 static GSourceFuncs source_funcs = {
346 g = (AvahiGLibPoll*) g_source_new(&source_funcs, sizeof(AvahiGLibPoll));
347 g_main_context_ref(g->context = context ? context : g_main_context_default());
351 g->api.watch_new = watch_new;
352 g->api.watch_free = watch_free;
353 g->api.watch_update = watch_update;
354 g->api.watch_get_events = watch_get_events;
356 g->api.timeout_new = timeout_new;
357 g->api.timeout_free = timeout_free;
358 g->api.timeout_update = timeout_update;
360 g->watch_req_cleanup = FALSE;
361 g->timeout_req_cleanup = FALSE;
363 AVAHI_LLIST_HEAD_INIT(AvahiWatch, g->watches);
364 AVAHI_LLIST_HEAD_INIT(AvahiTimeout, g->timeouts);
366 g_source_attach(&g->source, g->context);
367 g_source_set_priority(&g->source, priority);
372 void avahi_glib_poll_free(AvahiGLibPoll *g) {
373 GSource *s = &g->source;
376 cleanup_watches(g, 1);
377 cleanup_timeouts(g, 1);
379 g_main_context_unref(g->context);
384 const AvahiPoll* avahi_glib_poll_get(AvahiGLibPoll *g) {