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>
29 #include "glib-watch.h"
32 AvahiGLibPoll *glib_poll;
38 AvahiWatchCallback callback;
41 AVAHI_LLIST_FIELDS(AvahiWatch, watches);
45 AvahiGLibPoll *glib_poll;
49 struct timeval expiry;
51 AvahiTimeoutCallback callback;
54 AVAHI_LLIST_FIELDS(AvahiTimeout, timeouts);
57 struct AvahiGLibPoll {
60 GMainContext *context;
62 gboolean timeout_req_cleanup;
63 gboolean watch_req_cleanup;
65 AVAHI_LLIST_HEAD(AvahiWatch, watches);
66 AVAHI_LLIST_HEAD(AvahiTimeout, timeouts);
69 static void destroy_watch(AvahiWatch *w) {
73 g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
75 AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->glib_poll->watches, w);
80 static void cleanup_watches(AvahiGLibPoll *g, int all) {
84 for (w = g->watches; w; w = next) {
85 next = w->watches_next;
91 g->watch_req_cleanup = 0;
94 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
105 if (!(w = avahi_new(AvahiWatch, 1)))
111 (event & AVAHI_WATCH_IN ? G_IO_IN : 0) |
112 (event & AVAHI_WATCH_OUT ? G_IO_OUT : 0) |
113 (event & AVAHI_WATCH_ERR ? G_IO_ERR : 0) |
114 (event & AVAHI_WATCH_HUP ? G_IO_HUP : 0);
115 w->pollfd.revents = 0;
116 w->callback = callback;
117 w->userdata = userdata;
120 g_source_add_poll(&g->source, &w->pollfd);
121 w->pollfd_added = TRUE;
123 AVAHI_LLIST_PREPEND(AvahiWatch, watches, g->watches, w);
128 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
132 w->pollfd.events = events;
135 static AvahiWatchEvent watch_get_events(AvahiWatch *w) {
139 return w->pollfd.revents;
142 static void watch_free(AvahiWatch *w) {
146 if (w->pollfd_added) {
147 g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
148 w->pollfd_added = TRUE;
152 w->glib_poll->timeout_req_cleanup = TRUE;
155 static AvahiTimeout* timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallback callback, void *userdata) {
165 if (!(t = avahi_new(AvahiTimeout, 1)))
171 if ((t->enabled = !!tv))
174 t->callback = callback;
175 t->userdata = userdata;
177 AVAHI_LLIST_PREPEND(AvahiTimeout, timeouts, g->timeouts, t);
182 static void timeout_update(AvahiTimeout *t, const struct timeval *tv) {
186 if ((t->enabled = !!tv))
190 static void timeout_free(AvahiTimeout *t) {
195 t->glib_poll->timeout_req_cleanup = TRUE;
198 static void destroy_timeout(AvahiTimeout *t) {
201 AVAHI_LLIST_REMOVE(AvahiTimeout, timeouts, t->glib_poll->timeouts, t);
205 static void cleanup_timeouts(AvahiGLibPoll *g, int all) {
206 AvahiTimeout *t, *next;
209 for (t = g->timeouts; t; t = next) {
210 next = t->timeouts_next;
216 g->timeout_req_cleanup = FALSE;
219 static AvahiTimeout* find_next_timeout(AvahiGLibPoll *g) {
220 AvahiTimeout *t, *n = NULL;
223 for (t = g->timeouts; t; t = t->timeouts_next) {
225 if (t->dead || !t->enabled)
228 if (!n || avahi_timeval_compare(&t->expiry, &n->expiry) < 0)
235 static void start_timeout_callback(AvahiTimeout *t) {
241 t->callback(t, t->userdata);
244 static gboolean prepare_func(GSource *source, gint *timeout) {
245 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
246 AvahiTimeout *next_timeout;
251 if (g->watch_req_cleanup)
252 cleanup_watches(g, 0);
254 if (g->timeout_req_cleanup)
255 cleanup_timeouts(g, 0);
257 if ((next_timeout = find_next_timeout(g))) {
259 struct timeval tvnow;
262 g_source_get_current_time(source, &now);
263 tvnow.tv_sec = now.tv_sec;
264 tvnow.tv_usec = now.tv_usec;
266 usec = avahi_timeval_diff(&next_timeout->expiry, &tvnow);
271 *timeout = (gint) (usec / 1000);
277 static gboolean check_func(GSource *source) {
278 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
280 AvahiTimeout *next_timeout;
284 if ((next_timeout = find_next_timeout(g))) {
286 struct timeval tvnow;
287 g_source_get_current_time(source, &now);
288 tvnow.tv_sec = now.tv_sec;
289 tvnow.tv_usec = now.tv_usec;
291 if (avahi_timeval_compare(&next_timeout->expiry, &tvnow) < 0)
295 for (w = g->watches; w; w = w->watches_next)
296 if (w->pollfd.revents > 0)
302 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer userdata) {
303 AvahiGLibPoll* g = (AvahiGLibPoll*) source;
305 AvahiTimeout *next_timeout;
309 if ((next_timeout = find_next_timeout(g))) {
311 struct timeval tvnow;
312 g_source_get_current_time(source, &now);
313 tvnow.tv_sec = now.tv_sec;
314 tvnow.tv_usec = now.tv_usec;
316 if (avahi_timeval_compare(&next_timeout->expiry, &tvnow) < 0) {
317 start_timeout_callback(next_timeout);
322 for (w = g->watches; w; w = w->watches_next)
323 if (w->pollfd.revents > 0) {
325 w->callback(w, w->pollfd.fd, w->pollfd.revents, w->userdata);
326 w->pollfd.revents = 0;
333 AvahiGLibPoll *avahi_glib_poll_new(GMainContext *context, gint priority) {
336 static GSourceFuncs source_funcs = {
345 g = (AvahiGLibPoll*) g_source_new(&source_funcs, sizeof(AvahiGLibPoll));
346 g_main_context_ref(g->context = context ? context : g_main_context_default());
350 g->api.watch_new = watch_new;
351 g->api.watch_free = watch_free;
352 g->api.watch_update = watch_update;
353 g->api.watch_get_events = watch_get_events;
355 g->api.timeout_new = timeout_new;
356 g->api.timeout_free = timeout_free;
357 g->api.timeout_update = timeout_update;
359 g->watch_req_cleanup = FALSE;
360 g->timeout_req_cleanup = FALSE;
362 AVAHI_LLIST_HEAD_INIT(AvahiWatch, g->watches);
363 AVAHI_LLIST_HEAD_INIT(AvahiTimeout, g->timeouts);
365 g_source_attach(&g->source, g->context);
366 g_source_set_priority(&g->source, priority);
371 void avahi_glib_poll_free(AvahiGLibPoll *g) {
372 GSource *s = &g->source;
375 cleanup_watches(g, 1);
376 cleanup_timeouts(g, 1);
378 g_main_context_unref(g->context);
383 const AvahiPoll* avahi_glib_poll_get(AvahiGLibPoll *g) {