4 * Timeout functions for the Common UNIX Printing System (CUPS).
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 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
16 * cupsdInitTimeouts() - Initialise timeout structure.
17 * cupsdAddTimeout() - Add a timed callback.
18 * cupsdNextTimeout() - Find the next enabled timed callback.
19 * cupsdUpdateTimeout() - Adjust the time of a timed callback or disable it.
20 * cupsdRemoveTimeout() - Discard a timed callback.
21 * compare_timeouts() - Compare timed callbacks for array sorting.
26 #ifdef HAVE_AVAHI /* Applies to entire file... */
29 * Include necessary headers...
34 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLINFO)
36 #endif /* HAVE_MALLOC_H && HAVE_MALLINFO */
39 # include <avahi-common/timeval.h>
40 #endif /* HAVE_AVAHI */
43 struct _cupsd_timeout_s
47 cupsd_timeoutfunc_t callback;
56 * 'compare_timeouts()' - Compare timed callbacks for array sorting.
60 compare_addrs (void *p0, void *p1)
70 compare_timeouts (cupsd_timeout_t *p0, cupsd_timeout_t *p1)
72 int addrsdiff = compare_addrs (p0, p1);
78 if (!p0->enabled || !p1->enabled)
80 if (!p0->enabled && !p1->enabled)
83 return (p0->enabled ? -1 : 1);
86 tvdiff = avahi_timeval_compare (&p0->when, &p1->when);
95 * 'cupsdInitTimeouts()' - Initialise timeout structures.
99 cupsdInitTimeouts(void)
101 Timeouts = cupsArrayNew ((cups_array_func_t)compare_timeouts, NULL);
106 * 'cupsdAddTimeout()' - Add a timed callback.
109 cupsd_timeout_t * /* O - Timeout handle */
110 cupsdAddTimeout(const struct timeval *tv, /* I - Absolute time */
111 cupsd_timeoutfunc_t cb, /* I - Callback function */
112 void *data) /* I - User data */
114 cupsd_timeout_t *timeout;
116 timeout = malloc (sizeof(cupsd_timeout_t));
119 timeout->enabled = (tv != NULL);
122 timeout->when.tv_sec = tv->tv_sec;
123 timeout->when.tv_usec = tv->tv_usec;
126 timeout->callback = cb;
127 timeout->data = data;
128 cupsArrayAdd (Timeouts, timeout);
136 * 'cupsdNextTimeout()' - Find the next enabled timed callback.
139 cupsd_timeout_t * /* O - Next enabled timeout or NULL */
140 cupsdNextTimeout(long *delay) /* O - Seconds before scheduled */
142 cupsd_timeout_t *first = cupsArrayFirst (Timeouts);
143 struct timeval curtime;
145 if (first && !first->enabled)
150 gettimeofday (&curtime, NULL);
151 if (avahi_timeval_compare (&curtime, &first->when) > 0)
155 *delay = 1 + first->when.tv_sec - curtime.tv_sec;
156 if (first->when.tv_usec < curtime.tv_usec)
166 * 'cupsdRunTimeout()' - Run a timed callback.
170 cupsdRunTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
174 timeout->enabled = 0;
175 if (!timeout->callback)
177 timeout->callback (timeout, timeout->data);
181 * 'cupsdUpdateTimeout()' - Adjust the time of a timed callback or disable it.
185 cupsdUpdateTimeout(cupsd_timeout_t *timeout, /* I - Timeout */
186 const struct timeval *tv) /* I - Absolute time or NULL */
188 cupsArrayRemove (Timeouts, timeout);
189 timeout->enabled = (tv != NULL);
192 timeout->when.tv_sec = tv->tv_sec;
193 timeout->when.tv_usec = tv->tv_usec;
195 cupsArrayAdd (Timeouts, timeout);
200 * 'cupsdRemoveTimeout()' - Discard a timed callback.
204 cupsdRemoveTimeout(cupsd_timeout_t *timeout) /* I - Timeout */
206 cupsArrayRemove (Timeouts, timeout);
211 #endif /* HAVE_AVAHI ... from top of file */