1 diff -up cups-1.5.0/scheduler/avahi.c.avahi-4-poll cups-1.5.0/scheduler/avahi.c
2 --- cups-1.5.0/scheduler/avahi.c.avahi-4-poll 2011-08-05 15:07:09.570033486 +0100
3 +++ cups-1.5.0/scheduler/avahi.c 2011-08-05 15:07:09.570033486 +0100
8 + * Avahi poll implementation for the CUPS scheduler.
10 + * Copyright (C) 2010 Red Hat, Inc.
12 + * Tim Waugh <twaugh@redhat.com>
14 + * Distribution and use rights are outlined in the file "LICENSE.txt"
15 + * "LICENSE" which should have been included with this file. If this
16 + * file is missing or damaged, see the license at "http://www.cups.org/".
20 + * watch_read_cb - Read callback for file descriptor
21 + * watch_write_cb - Write callback for file descriptor
22 + * watched_fd_add_select() - Call cupsdAddSelect() as needed
23 + * watch_new() - Create a new file descriptor watch
24 + * watch_free() - Free a file descriptor watch
25 + * watch_update() - Update watched events for a file descriptor
26 + * watch_get_events() - Get events that happened for a file descriptor
27 + * timeout_cb() - Run a timed Avahi callback
28 + * timeout_new() - Set a wakeup time
29 + * timeout_update() - Update the expiration time for a timeout
30 + * timeout_free() - Free a timeout
31 + * compare_watched_fds() - Compare watched file descriptors for array sorting
32 + * compare_timeouts() - Compare timeouts for array sorting
33 + * avahi_cups_poll_new() - Create a new Avahi main loop object for CUPS
34 + * avahi_cups_poll_free() - Free an Avahi main loop object for CUPS
35 + * avahi_cups_poll_get() - Get the abstract poll API structure
40 +#ifdef HAVE_AVAHI /* Applies to entire file... */
43 + * Include necessary headers...
48 +#if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
50 +#endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
53 +# include <avahi-common/timeval.h>
54 +#endif /* HAVE_AVAHI */
59 + AvahiCupsPoll *cups_poll;
62 + AvahiWatchEvent occurred;
63 + cups_array_t *watches;
64 +} cupsd_watched_fd_t;
68 + cupsd_watched_fd_t *watched_fd;
70 + AvahiWatchEvent events;
71 + AvahiWatchCallback callback;
77 + AvahiCupsPoll *cups_poll;
78 + AvahiTimeoutCallback callback;
80 + cupsd_timeout_t *cupsd_timeout;
84 + * Local functions...
87 +static AvahiWatch * watch_new(const AvahiPoll *api,
89 + AvahiWatchEvent events,
90 + AvahiWatchCallback callback,
92 +static void watch_free(AvahiWatch *watch);
93 +static void watch_update(AvahiWatch *watch,
94 + AvahiWatchEvent events);
95 +static AvahiWatchEvent watch_get_events(AvahiWatch *watch);
96 +static int compare_watches(AvahiWatch *p0,
101 + * 'watch_read_cb' - Read callback for file descriptor
105 +watch_read_cb (void *userdata)
108 + cupsd_watched_fd_t *watched_fd = userdata;
109 + watched_fd->occurred |= AVAHI_WATCH_IN;
110 + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
112 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
113 + if (watch->events & watched_fd->occurred) {
114 + (watch->callback) (watch, watched_fd->fd,
115 + AVAHI_WATCH_IN, watch->userdata);
116 + watched_fd->occurred &= ~AVAHI_WATCH_IN;
124 + * 'watch_write_cb' - Write callback for file descriptor
128 +watch_write_cb (void *userdata)
131 + cupsd_watched_fd_t *watched_fd = userdata;
132 + watched_fd->occurred |= AVAHI_WATCH_OUT;
133 + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
135 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
136 + if (watch->events & watched_fd->occurred) {
137 + (watch->callback) (watch, watched_fd->fd,
138 + AVAHI_WATCH_OUT, watch->userdata);
139 + watched_fd->occurred &= ~AVAHI_WATCH_OUT;
147 + * 'watched_fd_add_select' - Call cupsdAddSelect() as needed
150 +static int /* O - Watches? */
151 +watched_fd_add_select (cupsd_watched_fd_t *watched_fd)
154 + cupsd_selfunc_t read_cb = NULL, write_cb = NULL;
156 + for (watch = (AvahiWatch *)cupsArrayFirst(watched_fd->watches);
158 + watch = (AvahiWatch *)cupsArrayNext(watched_fd->watches)) {
159 + if (watch->events & (AVAHI_WATCH_IN |
161 + AVAHI_WATCH_HUP)) {
162 + read_cb = (cupsd_selfunc_t)watch_read_cb;
163 + if (write_cb != NULL)
167 + if (watch->events & AVAHI_WATCH_OUT) {
168 + write_cb = (cupsd_selfunc_t)watch_write_cb;
169 + if (read_cb != NULL)
174 + if (read_cb || write_cb)
175 + cupsdAddSelect (watched_fd->fd, read_cb, write_cb, watched_fd);
177 + cupsdRemoveSelect (watched_fd->fd);
179 + return (read_cb || write_cb);
183 + * 'watch_new' - Create a new file descriptor watch
187 +watch_new (const AvahiPoll *api,
189 + AvahiWatchEvent events,
190 + AvahiWatchCallback callback,
193 + cupsd_watched_fd_t key, *watched_fd;
194 + AvahiCupsPoll *cups_poll = api->userdata;
195 + AvahiWatch *watch = malloc(sizeof(AvahiWatch));
199 + watch->events = events;
200 + watch->callback = callback;
201 + watch->userdata = userdata;
204 + watched_fd = cupsArrayFind (cups_poll->watched_fds, &key);
205 + if (watched_fd == NULL) {
206 + watched_fd = malloc(sizeof(cupsd_watched_fd_t));
207 + if (watched_fd == NULL) {
212 + watched_fd->fd = fd;
213 + watched_fd->occurred = 0;
214 + watched_fd->cups_poll = cups_poll;
215 + watched_fd->watches = cupsArrayNew ((cups_array_func_t)compare_watches,
219 + watch->watched_fd = watched_fd;
220 + cupsArrayAdd(watched_fd->watches, watch);
221 + watched_fd_add_select (watched_fd);
227 + * 'watch_free' - Free a file descriptor watch
231 +watch_free (AvahiWatch *watch)
233 + cupsd_watched_fd_t *watched_fd = watch->watched_fd;
234 + AvahiCupsPoll *cups_poll = watched_fd->cups_poll;
236 + cupsArrayRemove (watched_fd->watches, watch);
239 + if (!watched_fd_add_select (watched_fd)) {
240 + /* No more watches */
241 + cupsArrayRemove (cups_poll->watched_fds, watched_fd);
248 + * 'watch_update' - Update watched events for a file descriptor
252 +watch_update (AvahiWatch *watch,
253 + AvahiWatchEvent events)
255 + watch->events = events;
256 + watched_fd_add_select (watch->watched_fd);
261 + * 'watch_get_events' - Get events that happened for a file descriptor
264 +static AvahiWatchEvent
265 +watch_get_events (AvahiWatch *watch)
267 + return (watch->watched_fd->occurred);
272 + * 'compare_watches' - Compare watches for array sorting
276 +compare_watches (AvahiWatch *p0,
279 + if (p0->watched_fd->fd < p1->watched_fd->fd)
282 + return ((p0->watched_fd->fd == p1->watched_fd->fd) ? 0 : 1);
287 + * 'timeout_cb()' - Run a timed Avahi callback
291 +timeout_cb (cupsd_timeout_t *cupsd_timeout, void *userdata)
293 + AvahiTimeout *timeout = userdata;
294 + (timeout->callback) (timeout, timeout->userdata);
299 + * 'timeout_new' - Set a wakeup time
302 +static AvahiTimeout *
303 +timeout_new (const AvahiPoll *api,
304 + const struct timeval *tv,
305 + AvahiTimeoutCallback callback,
308 + AvahiTimeout *timeout;
309 + AvahiCupsPoll *cups_poll = api->userdata;
311 + timeout = malloc(sizeof(AvahiTimeout));
312 + if (timeout == NULL)
315 + timeout->cups_poll = cups_poll;
316 + timeout->callback = callback;
317 + timeout->userdata = userdata;
318 + timeout->cupsd_timeout = cupsdAddTimeout (tv,
319 + (cupsd_timeoutfunc_t)timeout_cb,
321 + cupsArrayAdd (cups_poll->timeouts, timeout);
327 + * 'timeout_update' - Update the expiration time for a timeout
331 +timeout_update (AvahiTimeout *timeout,
332 + const struct timeval *tv)
334 + cupsdUpdateTimeout (timeout->cupsd_timeout, tv);
339 + * ' timeout_free' - Free a timeout
343 +timeout_free (AvahiTimeout *timeout)
345 + cupsArrayRemove (timeout->cups_poll->timeouts, timeout);
346 + cupsdRemoveTimeout (timeout->cupsd_timeout);
352 + * 'compare_watched_fds' - Compare watched file descriptors for array sorting
355 +compare_watched_fds(cupsd_watched_fd_t *p0,
356 + cupsd_watched_fd_t *p1)
358 + if (p0->fd != p1->fd)
359 + return (p0->fd < p1->fd ? -1 : 1);
364 + return (p0 < p1 ? -1 : 1);
369 + * 'compare_timeouts' - Compare timeouts for array sorting
372 +compare_timeouts(AvahiTimeout *p0,
376 + * Just compare pointers to make it a stable sort.
379 + if (p0->cupsd_timeout < p1->cupsd_timeout)
381 + return ((p0->cupsd_timeout == p1->cupsd_timeout) ? 0 : 1);
386 + * 'avahi_cups_poll_new' - Create a new Avahi main loop object for CUPS
390 +avahi_cups_poll_new (void)
392 + AvahiCupsPoll *cups_poll = malloc(sizeof(AvahiCupsPoll));
393 + if (cups_poll == NULL)
396 + cups_poll->watched_fds = cupsArrayNew ((cups_array_func_t)compare_watched_fds,
398 + cups_poll->timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts,
401 + cups_poll->api.userdata = cups_poll;
402 + cups_poll->api.watch_new = watch_new;
403 + cups_poll->api.watch_free = watch_free;
404 + cups_poll->api.watch_update = watch_update;
405 + cups_poll->api.watch_get_events = watch_get_events;
407 + cups_poll->api.timeout_new = timeout_new;
408 + cups_poll->api.timeout_update = timeout_update;
409 + cups_poll->api.timeout_free = timeout_free;
411 + return (cups_poll);
416 + * 'avahi_cups_poll_free' - Free an Avahi main loop object for CUPS
419 +avahi_cups_poll_free (AvahiCupsPoll *cups_poll)
421 + cupsd_watched_fd_t *watched_fd;
423 + for (watched_fd = (cupsd_watched_fd_t*)cupsArrayFirst(cups_poll->watched_fds);
425 + watched_fd = (cupsd_watched_fd_t*)cupsArrayNext(cups_poll->watched_fds)){
426 + cupsArrayClear (watched_fd->watches);
429 + cupsArrayClear (cups_poll->watched_fds);
430 + cupsArrayClear (cups_poll->timeouts);
435 + * 'avahi_cups_poll_get' - Get the abstract poll API structure
439 +avahi_cups_poll_get (AvahiCupsPoll *cups_poll)
441 + return (&cups_poll->api);
445 +#endif /* HAVE_AVAHI ... from top of file */
450 diff -up cups-1.5.0/scheduler/avahi.h.avahi-4-poll cups-1.5.0/scheduler/avahi.h
451 --- cups-1.5.0/scheduler/avahi.h.avahi-4-poll 2011-08-05 15:07:09.594032723 +0100
452 +++ cups-1.5.0/scheduler/avahi.h 2011-08-05 15:07:09.595032692 +0100
457 + * Avahi poll implementation for the CUPS scheduler.
459 + * Copyright (C) 2010 Red Hat, Inc.
461 + * Tim Waugh <twaugh@redhat.com>
463 + * Distribution and use rights are outlined in the file "LICENSE.txt"
464 + * which should have been included with this file. If this file is
465 + * file is missing or damaged, see the license at "http://www.cups.org/".
471 +# include <avahi-client/client.h>
472 +# include <avahi-client/publish.h>
473 +#endif /* HAVE_AVAHI */
475 +#ifdef HAVE_AUTHORIZATION_H
476 +# include <Security/Authorization.h>
477 +#endif /* HAVE_AUTHORIZATION_H */
484 + cups_array_t *watched_fds;
485 + cups_array_t *timeouts;
487 +#endif /* HAVE_AVAHI */
494 +extern AvahiCupsPoll * avahi_cups_poll_new(void);
495 +extern void avahi_cups_poll_free(AvahiCupsPoll *cups_poll);
496 +extern const AvahiPoll *avahi_cups_poll_get(AvahiCupsPoll *cups_poll);
497 +#endif /* HAVE_AVAHI */
503 diff -up cups-1.5.0/scheduler/Makefile.avahi-4-poll cups-1.5.0/scheduler/Makefile
504 --- cups-1.5.0/scheduler/Makefile.avahi-4-poll 2011-08-05 15:06:48.548700563 +0100
505 +++ cups-1.5.0/scheduler/Makefile 2011-08-05 15:07:09.570033486 +0100
506 @@ -17,6 +17,7 @@ include ../Makedefs