4 * Avahi poll implementation for the CUPS scheduler.
6 * Copyright (C) 2010 Red Hat, Inc.
8 * Tim Waugh <twaugh@redhat.com>
10 * Distribution and use rights are outlined in the file "LICENSE.txt"
11 * "LICENSE" which should have been included with this file. If this
12 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * watch_read_cb - Read callback for file descriptor
17 * watch_write_cb - Write callback for file descriptor
18 * watched_fd_add_select() - Call cupsdAddSelect() as needed
19 * watch_new() - Create a new file descriptor watch
20 * watch_free() - Free a file descriptor watch
21 * watch_update() - Update watched events for a file descriptor
22 * watch_get_events() - Get events that happened for a file descriptor
23 * timeout_cb() - Run a timed Avahi callback
24 * timeout_new() - Set a wakeup time
25 * timeout_update() - Update the expiration time for a timeout
26 * timeout_free() - Free a timeout
27 * compare_watched_fds() - Compare watched file descriptors for array sorting
28 * compare_timeouts() - Compare timeouts for array sorting
29 * avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
30 * avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
31 * avahi_cups_poll_get() - Get the abstract poll API structure
36 #ifdef HAVE_AVAHI /* Applies to entire file... */
39 * Include necessary headers...
44 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
46 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
49 # include <avahi-common/timeval.h>
50 #endif /* HAVE_AVAHI */
55 AvahiCupsPoll *cups_poll;
58 AvahiWatchEvent occurred;
59 cups_array_t *watches;
64 cupsd_watched_fd_t *watched_fd;
66 AvahiWatchEvent events;
67 AvahiWatchCallback callback;
73 AvahiCupsPoll *cups_poll;
74 AvahiTimeoutCallback callback;
76 cupsd_timeout_t *cupsd_timeout;
83 static AvahiWatch * watch_new(const AvahiPoll *api,
85 AvahiWatchEvent events,
86 AvahiWatchCallback callback,
88 static void watch_free(AvahiWatch *watch);
89 static void watch_update(AvahiWatch *watch,
90 AvahiWatchEvent events);
91 static AvahiWatchEvent watch_get_events(AvahiWatch *watch);
92 static int compare_watches(AvahiWatch *p0,
97 * 'watch_read_cb' - Read callback for file descriptor
101 watch_read_cb (void *userdata)
104 cupsd_watched_fd_t *watched_fd = userdata;
105 watched_fd->occurred |= AVAHI_WATCH_IN;
106 for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
108 watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
109 if (watch->events & watched_fd->occurred) {
110 (watch->callback) (watch, watched_fd->fd,
111 AVAHI_WATCH_IN, watch->userdata);
112 watched_fd->occurred &= ~AVAHI_WATCH_IN;
120 * 'watch_write_cb' - Write callback for file descriptor
124 watch_write_cb (void *userdata)
127 cupsd_watched_fd_t *watched_fd = userdata;
128 watched_fd->occurred |= AVAHI_WATCH_OUT;
129 for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
131 watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
132 if (watch->events & watched_fd->occurred) {
133 (watch->callback) (watch, watched_fd->fd,
134 AVAHI_WATCH_OUT, watch->userdata);
135 watched_fd->occurred &= ~AVAHI_WATCH_OUT;
143 * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
146 static int /* O - Watches? */
147 watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
150 cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
152 for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
154 watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
155 if (watch->events & (AVAHI_WATCH_IN |
158 read_cb = (cupsd_selfunc_t)watch_read_cb;
159 if (write_cb != NULL)
163 if (watch->events & AVAHI_WATCH_OUT) {
164 write_cb = (cupsd_selfunc_t)watch_write_cb;
170 if (read_cb || write_cb)
171 cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
173 cupsdRemoveSelect (watched_fd->fd);
175 return (read_cb || write_cb);
179 * 'watch_new' - Create a new file descriptor watch
183 watch_new (const AvahiPoll *api,
185 AvahiWatchEvent events,
186 AvahiWatchCallback callback,
189 cupsd_watched_fd_t key, *watched_fd;
190 AvahiCupsPoll *cups_poll = api->userdata;
191 AvahiWatch *watch = malloc(sizeof(AvahiWatch));
195 watch->events = events;
196 watch->callback = callback;
197 watch->userdata = userdata;
200 watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
201 if (watched_fd == NULL) {
202 watched_fd = malloc(sizeof(cupsd_watched_fd_t));
203 if (watched_fd == NULL) {
209 watched_fd->occurred = 0;
210 watched_fd->cups_poll = cups_poll;
211 watched_fd->watches = cupsArrayNew ((cups_array_func_t)compare_watches,
215 watch->watched_fd = watched_fd;
216 cupsArrayAdd(watched_fd->watches, watch);
217 watched_fd_add_select (watched_fd);
223 * 'watch_free' - Free a file descriptor watch
227 watch_free (AvahiWatch *watch)
229 cupsd_watched_fd_t *watched_fd = watch->watched_fd;
230 AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
232 cupsArrayRemove (watched_fd->watches, watch);
235 if (!watched_fd_add_select (watched_fd)) {
236 /* No more watches */
237 cupsArrayRemove (cups_poll->watched_fds, watched_fd);
244 * 'watch_update' - Update watched events for a file descriptor
248 watch_update (AvahiWatch *watch,
249 AvahiWatchEvent events)
251 watch->events = events;
252 watched_fd_add_select (watch->watched_fd);
257 * 'watch_get_events' - Get events that happened for a file descriptor
260 static AvahiWatchEvent
261 watch_get_events (AvahiWatch *watch)
263 return (watch->watched_fd->occurred);
268 * 'compare_watches' - Compare watches for array sorting
272 compare_watches (AvahiWatch *p0,
275 if (p0->watched_fd->fd < p1->watched_fd->fd)
278 return ((p0->watched_fd->fd == p1->watched_fd->fd) ? 0 : 1);
283 * 'timeout_cb()' - Run a timed Avahi callback
287 timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
289 AvahiTimeout *timeout = userdata;
290 (timeout->callback) (timeout, timeout->userdata);
295 * 'timeout_new' - Set a wakeup time
298 static AvahiTimeout *
299 timeout_new (const AvahiPoll *api,
300 const struct timeval *tv,
301 AvahiTimeoutCallback callback,
304 AvahiTimeout *timeout;
305 AvahiCupsPoll *cups_poll = api->userdata;
307 timeout = malloc(sizeof(AvahiTimeout));
311 timeout->cups_poll = cups_poll;
312 timeout->callback = callback;
313 timeout->userdata = userdata;
314 timeout->cupsd_timeout = cupsdAddTimeout (tv,
315 (cupsd_timeoutfunc_t)timeout_cb,
317 cupsArrayAdd (cups_poll->timeouts, timeout);
323 * 'timeout_update' - Update the expiration time for a timeout
327 timeout_update (AvahiTimeout *timeout,
328 const struct timeval *tv)
330 cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
335 * ' timeout_free' - Free a timeout
339 timeout_free (AvahiTimeout *timeout)
341 cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
342 cupsdRemoveTimeout (timeout->cupsd_timeout);
348 * 'compare_watched_fds' - Compare watched file descriptors for array sorting
351 compare_watched_fds(cupsd_watched_fd_t *p0,
352 cupsd_watched_fd_t *p1)
354 if (p0->fd != p1->fd)
355 return (p0->fd < p1->fd ? -1 : 1);
360 return (p0 < p1 ? -1 : 1);
365 * 'compare_timeouts' - Compare timeouts for array sorting
368 compare_timeouts(AvahiTimeout *p0,
372 * Just compare pointers to make it a stable sort.
375 if (p0->cupsd_timeout < p1->cupsd_timeout)
377 return ((p0->cupsd_timeout == p1->cupsd_timeout) ? 0 : 1);
382 * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
386 avahi_cups_poll_new (void)
388 AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
389 if (cups_poll == NULL)
392 cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
394 cups_poll->timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts,
397 cups_poll->api.userdata = cups_poll;
398 cups_poll->api.watch_new = watch_new;
399 cups_poll->api.watch_free = watch_free;
400 cups_poll->api.watch_update = watch_update;
401 cups_poll->api.watch_get_events = watch_get_events;
403 cups_poll->api.timeout_new = timeout_new;
404 cups_poll->api.timeout_update = timeout_update;
405 cups_poll->api.timeout_free = timeout_free;
412 * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
415 avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
417 cupsd_watched_fd_t *watched_fd;
419 for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
421 watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds)){
422 cupsArrayClear (watched_fd->watches);
425 cupsArrayClear (cups_poll->watched_fds);
426 cupsArrayClear (cups_poll->timeouts);
431 * 'avahi_cups_poll_get' - Get the abstract poll API structure
435 avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
437 return (&cups_poll->api);
441 #endif /* HAVE_AVAHI ... from top of file */