1 /* xscreensaver, Copyright (c) 2006-2017 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.
19 #ifdef HAVE_JWXYZ /* whole file */
28 #include <sys/select.h>
30 #include "jwxyz-timers.h"
33 extern void Log(const char *format, ...);
37 # define Log(S, ...) fprintf(stderr, "xscreensaver: " S "\n", __VA_ARGS__)
41 # define LOGT(...) Log(__VA_ARGS__)
47 # define LOGI(...) Log(__VA_ARGS__)
52 #define ASSERT_RET(C,S) do { \
54 jwxyz_abort ("jwxyz-timers: %s",(S)); \
58 #define ASSERT_RET0(C,S) do { \
60 jwxyz_abort ("jwxyz-timers: %s",(S)); \
66 XtDisplayToApplicationContext (Display *dpy)
68 return (XtAppContext) dpy;
71 #define app_to_display(APP) ((Display *) (APP))
74 struct jwxyz_sources_data {
76 XtInputId ids[FD_SETSIZE];
77 XtIntervalId all_timers;
80 struct jwxyz_XtIntervalId {
85 XtTimerCallbackProc cb;
91 struct jwxyz_XtInputId {
95 XtInputCallbackProc cb;
105 # ifdef GETTIMEOFDAY_TWO_ARGS
107 gettimeofday(&now, &tzp);
112 return (now.tv_sec + ((double) now.tv_usec * 0.000001));
117 jwxyz_sources_init (XtAppContext app)
119 jwxyz_sources_data *td = (jwxyz_sources_data *) calloc (1, sizeof (*td));
124 XtAppAddTimeOut (XtAppContext app, unsigned long msecs,
125 XtTimerCallbackProc cb, XtPointer closure)
127 jwxyz_sources_data *td = display_sources_data (app_to_display (app));
128 XtIntervalId data = (XtIntervalId) calloc (1, sizeof(*data));
129 double now = double_time();
132 data->closure = closure;
134 data->run_at = now + (msecs / 1000.0);
136 data->next = td->all_timers;
137 td->all_timers = data;
139 LOGT("timer 0x%08lX: alloc %lu %.2f", (unsigned long) data, msecs,
146 /* This is called both by the user to manually kill a timer,
147 and by the run loop after a timer has fired.
150 XtRemoveTimeOut (XtIntervalId data)
152 jwxyz_sources_data *td = display_sources_data (app_to_display (data->app));
154 LOGT("timer 0x%08lX: remove", (unsigned long) data);
155 ASSERT_RET (data->refcount > 0, "already freed");
158 LOGT("timer 0x%08lX: release %d", (unsigned long) data, data->refcount);
159 ASSERT_RET (data->refcount >= 0, "double free");
161 if (data->refcount == 0) {
163 /* Remove it from the list of live timers. */
164 XtIntervalId prev, timer;
166 for (timer = td->all_timers, prev = 0;
168 prev = timer, timer = timer->next) {
170 ASSERT_RET (!hit, "circular timer list");
172 prev->next = timer->next;
174 td->all_timers = timer->next;
178 ASSERT_RET (timer->refcount > 0, "timer list corrupted");
188 XtAppAddInput (XtAppContext app, int fd, XtPointer flags,
189 XtInputCallbackProc cb, XtPointer closure)
191 jwxyz_sources_data *td = display_sources_data (app_to_display (app));
192 XtInputId data = (XtInputId) calloc (1, sizeof(*data));
195 data->closure = closure;
199 LOGI("source 0x%08lX %2d: alloc", (unsigned long) data, data->fd);
201 ASSERT_RET0 (fd > 0 && fd < FD_SETSIZE, "fd out of range");
202 ASSERT_RET0 (td->ids[fd] == 0, "sources corrupted");
211 XtRemoveInput (XtInputId id)
213 jwxyz_sources_data *td = display_sources_data (app_to_display (id->app));
215 LOGI("source 0x%08lX %2d: remove", (unsigned long) id, id->fd);
216 ASSERT_RET (id->refcount > 0, "sources corrupted");
217 ASSERT_RET (td->fd_count > 0, "sources corrupted");
218 ASSERT_RET (id->fd > 0 && id->fd < FD_SETSIZE, "fd out of range");
219 ASSERT_RET (td->ids[id->fd] == id, "sources corrupted");
225 LOGI("source 0x%08lX %2d: release %d", (unsigned long) id, id->fd,
227 ASSERT_RET (id->refcount >= 0, "double free");
228 if (id->refcount == 0) {
229 memset (id, 0xA1, sizeof(*id));
237 jwxyz_timers_run (jwxyz_sources_data *td)
239 /* Iterate the timer list, being careful because XtRemoveTimeOut removes
240 the current item from that list. */
241 if (td->all_timers) {
242 XtIntervalId timer, next;
243 double now = double_time();
246 for (timer = td->all_timers, next = timer->next;
248 timer = next, next = (timer ? timer->next : 0)) {
249 if (timer->run_at <= now) {
250 LOGT("timer 0x%08lX: fire %.02f", (unsigned long) timer,
251 now - timer->run_at);
252 timer->cb (timer->closure, &timer);
253 XtRemoveTimeOut (timer);
255 ASSERT_RET (count < 10000, "way too many timers to run");
263 jwxyz_sources_run (jwxyz_sources_data *td)
265 if (td->fd_count == 0) return;
267 struct timeval tv = { 0, };
273 for (i = 0; i < FD_SETSIZE; i++) {
280 ASSERT_RET (max > 0, "no fds");
282 if (0 < select (max+1, &fds, NULL, NULL, &tv)) {
283 for (i = 0; i < FD_SETSIZE; i++) {
284 if (FD_ISSET (i, &fds)) {
285 XtInputId id = td->ids[i];
286 ASSERT_RET (id && id->cb, "sources corrupted");
287 ASSERT_RET (id->fd == i, "sources corrupted");
288 id->cb (id->closure, &id->fd, &id);
296 jwxyz_XtRemoveInput_all (jwxyz_sources_data *td)
299 for (i = 0; i < FD_SETSIZE; i++) {
300 XtInputId id = td->ids[i];
301 if (id) XtRemoveInput (id);
307 jwxyz_XtRemoveTimeOut_all (jwxyz_sources_data *td)
309 XtIntervalId timer, next;
312 /* Iterate the timer list, being careful because XtRemoveTimeOut removes
313 the current item from that list. */
314 if (td->all_timers) {
315 for (timer = td->all_timers, next = timer->next;
317 timer = next, next = (timer ? timer->next : 0)) {
318 XtRemoveTimeOut (timer);
320 ASSERT_RET (count < 10000, "way too many timers to free");
322 ASSERT_RET (!td->all_timers, "timer list didn't empty");
328 jwxyz_sources_free (jwxyz_sources_data *td)
330 jwxyz_XtRemoveInput_all (td);
331 jwxyz_XtRemoveTimeOut_all (td);
332 memset (td, 0xA1, sizeof(*td));
338 XtAppPending (XtAppContext app)
340 return XtIMAlternateInput; /* just always say yes */
344 XtAppProcessEvent (XtAppContext app, XtInputMask mask)
346 jwxyz_sources_data *td = display_sources_data (app_to_display (app));
347 if (mask & XtIMAlternateInput)
348 jwxyz_sources_run (td);
349 if (mask & XtIMTimer)
350 jwxyz_timers_run (td);
353 #endif /* HAVE_JWXYZ */