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;
36 AvahiWatchCallback callback;
39 AVAHI_LLIST_FIELDS(AvahiWatch, watches);
42 struct AvahiGLibPoll {
45 GMainContext *context;
47 struct timeval wakeup;
51 AvahiGLibProcessCallback process_callback;
54 AVAHI_LLIST_HEAD(AvahiWatch, watches);
57 static void destroy_watch(AvahiWatch *w) {
61 g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
63 AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->glib_poll->watches, w);
68 static void cleanup(AvahiGLibPoll *g, int all) {
72 for (w = g->watches; w; w = next) {
73 next = w->watches_next;
82 static AvahiWatch* watch_new(AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
93 if (!(w = avahi_new(AvahiWatch, 1)))
99 (event & AVAHI_WATCH_IN ? G_IO_IN : 0) |
100 (event & AVAHI_WATCH_OUT ? G_IO_OUT : 0) |
101 (event & AVAHI_WATCH_ERR ? G_IO_ERR : 0) |
102 (event & AVAHI_WATCH_HUP ? G_IO_HUP : 0);
104 w->callback = callback;
105 w->userdata = userdata;
108 g_source_add_poll(&g->source, &w->pollfd);
111 AVAHI_LLIST_PREPEND(AvahiWatch, watches, g->watches, w);
116 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
120 w->pollfd.events = events;
123 static void watch_free(AvahiWatch *w) {
127 if (w->pollfd_added) {
128 g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
133 w->glib_poll->req_cleanup = 1;
136 static void set_wakeup_time(AvahiPoll *api, const struct timeval *tv) {
149 static gboolean prepare_func(GSource *source, gint *timeout) {
150 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
157 struct timeval tvnow;
160 g_source_get_current_time(source, &now);
161 tvnow.tv_sec = now.tv_sec;
162 tvnow.tv_usec = now.tv_usec;
164 usec = avahi_timeval_diff(&g->wakeup, &tvnow);
169 *timeout = (gint) (usec / 1000);
175 static gboolean check_func(GSource *source) {
176 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
181 for (w = g->watches; w; w = w->watches_next)
182 if (w->pollfd.revents > 0)
188 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer userdata) {
189 AvahiGLibPoll* g = (AvahiGLibPoll*) source;
197 if (g->process_callback)
198 g->process_callback(g, g->userdata);
200 for (w = g->watches; w; w = w->watches_next)
201 if (w->pollfd.revents > 0) {
203 w->callback(w, w->pollfd.fd, w->pollfd.revents, w->userdata);
204 w->pollfd.revents = 0;
213 AvahiGLibPoll *avahi_glib_poll_new(GMainContext *context, AvahiGLibProcessCallback callback, void *userdata) {
216 static GSourceFuncs source_funcs = {
225 g = (AvahiGLibPoll*) g_source_new(&source_funcs, sizeof(AvahiGLibPoll));
226 g_main_context_ref(g->context = context ? context : g_main_context_default());
229 g->api.watch_new = watch_new;
230 g->api.watch_free = watch_free;
231 g->api.watch_update = watch_update;
232 g->api.set_wakeup_time = set_wakeup_time;
235 g->process_callback = callback;
236 g->userdata = userdata;
239 AVAHI_LLIST_HEAD_INIT(AvahiWatch, g->watches);
241 g_source_attach(&g->source, g->context);
246 void avahi_glib_poll_free(AvahiGLibPoll *g) {
247 GSource *s = &g->source;
252 g_main_context_unref(g->context);
257 AvahiPoll* avahi_glib_poll_get(AvahiGLibPoll *g) {