1 /* xscreensaver, Copyright (c) 2006-2016 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* This is the portable implementation of Xt timers and inputs, for libjwxyz.
17 #ifdef HAVE_JWXYZ /* whole file */
26 #include <sys/select.h>
28 #include "jwxyz-timers.h"
31 extern void Log(const char *format, ...);
35 # define Log(S, ...) fprintf(stderr, "xscreensaver: " S "\n", __VA_ARGS__)
39 # define LOGT(...) Log(__VA_ARGS__)
45 # define LOGI(...) Log(__VA_ARGS__)
50 #define ASSERT_RET(C,S) do { \
52 jwxyz_abort ("jwxyz-timers: %s",(S)); \
56 #define ASSERT_RET0(C,S) do { \
58 jwxyz_abort ("jwxyz-timers: %s",(S)); \
64 XtDisplayToApplicationContext (Display *dpy)
66 return (XtAppContext) dpy;
69 #define app_to_display(APP) ((Display *) (APP))
72 struct jwxyz_sources_data {
74 XtInputId ids[FD_SETSIZE];
75 XtIntervalId all_timers;
78 struct jwxyz_XtIntervalId {
83 XtTimerCallbackProc cb;
89 struct jwxyz_XtInputId {
93 XtInputCallbackProc cb;
103 # ifdef GETTIMEOFDAY_TWO_ARGS
105 gettimeofday(&now, &tzp);
110 return (now.tv_sec + ((double) now.tv_usec * 0.000001));
115 jwxyz_sources_init (XtAppContext app)
117 jwxyz_sources_data *td = (jwxyz_sources_data *) calloc (1, sizeof (*td));
122 XtAppAddTimeOut (XtAppContext app, unsigned long msecs,
123 XtTimerCallbackProc cb, XtPointer closure)
125 jwxyz_sources_data *td = display_sources_data (app_to_display (app));
126 XtIntervalId data = (XtIntervalId) calloc (1, sizeof(*data));
127 double now = double_time();
130 data->closure = closure;
132 data->run_at = now + (msecs / 1000.0);
134 data->next = td->all_timers;
135 td->all_timers = data;
137 LOGT("timer 0x%08lX: alloc %lu %.2f", (unsigned long) data, msecs,
144 /* This is called both by the user to manually kill a timer,
145 and by the run loop after a timer has fired.
148 XtRemoveTimeOut (XtIntervalId data)
150 jwxyz_sources_data *td = display_sources_data (app_to_display (data->app));
152 LOGT("timer 0x%08lX: remove", (unsigned long) data);
153 ASSERT_RET (data->refcount > 0, "already freed");
156 LOGT("timer 0x%08lX: release %d", (unsigned long) data, data->refcount);
157 ASSERT_RET (data->refcount >= 0, "double free");
159 if (data->refcount == 0) {
161 /* Remove it from the list of live timers. */
162 XtIntervalId prev, timer;
164 for (timer = td->all_timers, prev = 0;
166 prev = timer, timer = timer->next) {
168 ASSERT_RET (!hit, "circular timer list");
170 prev->next = timer->next;
172 td->all_timers = timer->next;
176 ASSERT_RET (timer->refcount > 0, "timer list corrupted");
186 XtAppAddInput (XtAppContext app, int fd, XtPointer flags,
187 XtInputCallbackProc cb, XtPointer closure)
189 jwxyz_sources_data *td = display_sources_data (app_to_display (app));
190 XtInputId data = (XtInputId) calloc (1, sizeof(*data));
193 data->closure = closure;
197 LOGI("source 0x%08lX %2d: alloc", (unsigned long) data, data->fd);
199 ASSERT_RET0 (fd > 0 && fd < FD_SETSIZE, "fd out of range");
200 ASSERT_RET0 (td->ids[fd] == 0, "sources corrupted");
209 XtRemoveInput (XtInputId id)
211 jwxyz_sources_data *td = display_sources_data (app_to_display (id->app));
213 LOGI("source 0x%08lX %2d: remove", (unsigned long) id, id->fd);
214 ASSERT_RET (id->refcount > 0, "sources corrupted");
215 ASSERT_RET (td->fd_count > 0, "sources corrupted");
216 ASSERT_RET (id->fd > 0 && id->fd < FD_SETSIZE, "fd out of range");
217 ASSERT_RET (td->ids[id->fd] == id, "sources corrupted");
223 LOGI("source 0x%08lX %2d: release %d", (unsigned long) id, id->fd,
225 ASSERT_RET (id->refcount >= 0, "double free");
226 if (id->refcount == 0) {
227 memset (id, 0xA1, sizeof(*id));
235 jwxyz_timers_run (jwxyz_sources_data *td)
237 /* Iterate the timer list, being careful because XtRemoveTimeOut removes
238 the current item from that list. */
239 if (td->all_timers) {
240 XtIntervalId timer, next;
241 double now = double_time();
244 for (timer = td->all_timers, next = timer->next;
246 timer = next, next = (timer ? timer->next : 0)) {
247 if (timer->run_at <= now) {
248 LOGT("timer 0x%08lX: fire %.02f", (unsigned long) timer,
249 now - timer->run_at);
250 timer->cb (timer->closure, &timer);
251 XtRemoveTimeOut (timer);
253 ASSERT_RET (count < 10000, "way too many timers to run");
261 jwxyz_sources_run (jwxyz_sources_data *td)
263 if (td->fd_count == 0) return;
265 struct timeval tv = { 0, };
271 for (i = 0; i < FD_SETSIZE; i++) {
278 ASSERT_RET (max > 0, "no fds");
280 if (0 < select (max+1, &fds, NULL, NULL, &tv)) {
281 for (i = 0; i < FD_SETSIZE; i++) {
282 if (FD_ISSET (i, &fds)) {
283 XtInputId id = td->ids[i];
284 ASSERT_RET (id && id->cb, "sources corrupted");
285 ASSERT_RET (id->fd == i, "sources corrupted");
286 id->cb (id->closure, &id->fd, &id);
294 jwxyz_XtRemoveInput_all (jwxyz_sources_data *td)
297 for (i = 0; i < FD_SETSIZE; i++) {
298 XtInputId id = td->ids[i];
299 if (id) XtRemoveInput (id);
305 jwxyz_XtRemoveTimeOut_all (jwxyz_sources_data *td)
307 XtIntervalId timer, next;
310 /* Iterate the timer list, being careful because XtRemoveTimeOut removes
311 the current item from that list. */
312 if (td->all_timers) {
313 for (timer = td->all_timers, next = timer->next;
315 timer = next, next = (timer ? timer->next : 0)) {
316 XtRemoveTimeOut (timer);
318 ASSERT_RET (count < 10000, "way too many timers to free");
320 ASSERT_RET (!td->all_timers, "timer list didn't empty");
326 jwxyz_sources_free (jwxyz_sources_data *td)
328 jwxyz_XtRemoveInput_all (td);
329 jwxyz_XtRemoveTimeOut_all (td);
330 memset (td, 0xA1, sizeof(*td));
336 XtAppPending (XtAppContext app)
338 return XtIMAlternateInput; /* just always say yes */
342 XtAppProcessEvent (XtAppContext app, XtInputMask mask)
344 jwxyz_sources_data *td = display_sources_data (app_to_display (app));
345 if (mask & XtIMAlternateInput)
346 jwxyz_sources_run (td);
347 if (mask & XtIMTimer)
348 jwxyz_timers_run (td);
351 #endif /* HAVE_JWXYZ */