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;
48 AvahiWakeupCallback wakeup_callback;
49 void *wakeup_userdata;
53 AVAHI_LLIST_HEAD(AvahiWatch, watches);
56 static void destroy_watch(AvahiWatch *w) {
60 g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
62 AVAHI_LLIST_REMOVE(AvahiWatch, watches, w->glib_poll->watches, w);
67 static void cleanup(AvahiGLibPoll *g, int all) {
71 for (w = g->watches; w; w = next) {
72 next = w->watches_next;
81 static AvahiWatch* watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent event, AvahiWatchCallback callback, void *userdata) {
92 if (!(w = avahi_new(AvahiWatch, 1)))
98 (event & AVAHI_WATCH_IN ? G_IO_IN : 0) |
99 (event & AVAHI_WATCH_OUT ? G_IO_OUT : 0) |
100 (event & AVAHI_WATCH_ERR ? G_IO_ERR : 0) |
101 (event & AVAHI_WATCH_HUP ? G_IO_HUP : 0);
103 w->callback = callback;
104 w->userdata = userdata;
107 g_source_add_poll(&g->source, &w->pollfd);
110 AVAHI_LLIST_PREPEND(AvahiWatch, watches, g->watches, w);
115 static void watch_update(AvahiWatch *w, AvahiWatchEvent events) {
119 w->pollfd.events = events;
122 static void watch_free(AvahiWatch *w) {
126 if (w->pollfd_added) {
127 g_source_remove_poll(&w->glib_poll->source, &w->pollfd);
132 w->glib_poll->req_cleanup = 1;
135 static void set_wakeup(const AvahiPoll *api, const struct timeval *tv, AvahiWakeupCallback callback, void *userdata) {
145 g->wakeup.tv_sec = 0;
146 g->wakeup.tv_usec = 0;
149 g->wakeup_callback = callback;
150 g->wakeup_userdata = userdata;
152 g->wakeup_callback = NULL;
155 static void start_wakeup_callback(AvahiGLibPoll *g) {
156 AvahiWakeupCallback callback;
161 /* Reset the wakeup functions, but allow changing of the two
162 values from the callback function */
164 callback = g->wakeup_callback;
165 userdata = g->wakeup_userdata;
166 g->wakeup_callback = NULL;
167 g->wakeup_userdata = NULL;
171 callback(&g->api, userdata);
174 static gboolean prepare_func(GSource *source, gint *timeout) {
175 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
183 if (g->wakeup_callback) {
185 struct timeval tvnow;
188 g_source_get_current_time(source, &now);
189 tvnow.tv_sec = now.tv_sec;
190 tvnow.tv_usec = now.tv_usec;
192 usec = avahi_timeval_diff(&g->wakeup, &tvnow);
197 *timeout = (gint) (usec / 1000);
203 static gboolean check_func(GSource *source) {
204 AvahiGLibPoll *g = (AvahiGLibPoll*) source;
209 if (g->wakeup_callback) {
211 struct timeval tvnow;
212 g_source_get_current_time(source, &now);
213 tvnow.tv_sec = now.tv_sec;
214 tvnow.tv_usec = now.tv_usec;
216 if (avahi_timeval_compare(&g->wakeup, &tvnow) < 0)
220 for (w = g->watches; w; w = w->watches_next)
221 if (w->pollfd.revents > 0)
227 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer userdata) {
228 AvahiGLibPoll* g = (AvahiGLibPoll*) source;
233 if (g->wakeup_callback) {
235 struct timeval tvnow;
236 g_source_get_current_time(source, &now);
237 tvnow.tv_sec = now.tv_sec;
238 tvnow.tv_usec = now.tv_usec;
240 if (avahi_timeval_compare(&g->wakeup, &tvnow) < 0) {
241 start_wakeup_callback(g);
246 for (w = g->watches; w; w = w->watches_next)
247 if (w->pollfd.revents > 0) {
249 w->callback(w, w->pollfd.fd, w->pollfd.revents, w->userdata);
250 w->pollfd.revents = 0;
257 AvahiGLibPoll *avahi_glib_poll_new(GMainContext *context) {
260 static GSourceFuncs source_funcs = {
269 g = (AvahiGLibPoll*) g_source_new(&source_funcs, sizeof(AvahiGLibPoll));
270 g_main_context_ref(g->context = context ? context : g_main_context_default());
273 g->api.watch_new = watch_new;
274 g->api.watch_free = watch_free;
275 g->api.watch_update = watch_update;
276 g->api.set_wakeup = set_wakeup;
278 g->wakeup_callback = NULL;
281 AVAHI_LLIST_HEAD_INIT(AvahiWatch, g->watches);
283 g_source_attach(&g->source, g->context);
288 void avahi_glib_poll_free(AvahiGLibPoll *g) {
289 GSource *s = &g->source;
292 /* g_message("BEFORE"); */
295 /* g_message("MIDDLE"); */
296 g_main_context_unref(g->context);
299 /* g_message("AFTER"); */
302 const AvahiPoll* avahi_glib_poll_get(AvahiGLibPoll *g) {