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 */
29 #include <sys/select.h>
31 #include "jwxyz-timers.h"
34 extern void Log(const char *format, ...);
38 # define Log(S, ...) fprintf(stderr, "xscreensaver: " S "\n", __VA_ARGS__)
42 # define LOGT(...) Log(__VA_ARGS__)
48 # define LOGI(...) Log(__VA_ARGS__)
53 #define ASSERT_RET(C,S) do { \
55 jwxyz_abort ("jwxyz-timers: %s",(S)); \
59 #define ASSERT_RET0(C,S) do { \
61 jwxyz_abort ("jwxyz-timers: %s",(S)); \
67 XtDisplayToApplicationContext (Display *dpy)
69 return (XtAppContext) dpy;
72 #define app_to_display(APP) ((Display *) (APP))
74 #define DISPLAY_SOURCES_DATA(APP) \
75 JWXYZ_VTBL(app_to_display (APP))->display_sources_data (app_to_display (APP))
78 struct jwxyz_sources_data {
80 XtInputId ids[FD_SETSIZE];
81 XtIntervalId all_timers;
84 struct jwxyz_XtIntervalId {
89 XtTimerCallbackProc cb;
95 struct jwxyz_XtInputId {
99 XtInputCallbackProc cb;
109 # ifdef GETTIMEOFDAY_TWO_ARGS
111 gettimeofday(&now, &tzp);
116 return (now.tv_sec + ((double) now.tv_usec * 0.000001));
121 jwxyz_sources_init (XtAppContext app)
123 jwxyz_sources_data *td = (jwxyz_sources_data *) calloc (1, sizeof (*td));
128 XtAppAddTimeOut (XtAppContext app, unsigned long msecs,
129 XtTimerCallbackProc cb, XtPointer closure)
131 jwxyz_sources_data *td = DISPLAY_SOURCES_DATA (app);
132 XtIntervalId data = (XtIntervalId) calloc (1, sizeof(*data));
133 double now = double_time();
136 data->closure = closure;
138 data->run_at = now + (msecs / 1000.0);
140 data->next = td->all_timers;
141 td->all_timers = data;
143 LOGT("timer 0x%08lX: alloc %lu %.2f", (unsigned long) data, msecs,
150 /* This is called both by the user to manually kill a timer,
151 and by the run loop after a timer has fired.
154 XtRemoveTimeOut (XtIntervalId data)
156 jwxyz_sources_data *td = DISPLAY_SOURCES_DATA (data->app);
158 LOGT("timer 0x%08lX: remove", (unsigned long) data);
159 ASSERT_RET (data->refcount > 0, "already freed");
162 LOGT("timer 0x%08lX: release %d", (unsigned long) data, data->refcount);
163 ASSERT_RET (data->refcount >= 0, "double free");
165 if (data->refcount == 0) {
167 /* Remove it from the list of live timers. */
168 XtIntervalId prev, timer;
170 for (timer = td->all_timers, prev = 0;
172 prev = timer, timer = timer->next) {
174 ASSERT_RET (!hit, "circular timer list");
176 prev->next = timer->next;
178 td->all_timers = timer->next;
182 ASSERT_RET (timer->refcount > 0, "timer list corrupted");
192 XtAppAddInput (XtAppContext app, int fd, XtPointer flags,
193 XtInputCallbackProc cb, XtPointer closure)
195 jwxyz_sources_data *td = DISPLAY_SOURCES_DATA (app);
196 XtInputId data = (XtInputId) calloc (1, sizeof(*data));
199 data->closure = closure;
203 LOGI("source 0x%08lX %2d: alloc", (unsigned long) data, data->fd);
205 ASSERT_RET0 (fd > 0 && fd < FD_SETSIZE, "fd out of range");
206 ASSERT_RET0 (td->ids[fd] == 0, "sources corrupted");
215 XtRemoveInput (XtInputId id)
217 jwxyz_sources_data *td = DISPLAY_SOURCES_DATA (id->app);
219 LOGI("source 0x%08lX %2d: remove", (unsigned long) id, id->fd);
220 ASSERT_RET (id->refcount > 0, "sources corrupted");
221 ASSERT_RET (td->fd_count > 0, "sources corrupted");
222 ASSERT_RET (id->fd > 0 && id->fd < FD_SETSIZE, "fd out of range");
223 ASSERT_RET (td->ids[id->fd] == id, "sources corrupted");
229 LOGI("source 0x%08lX %2d: release %d", (unsigned long) id, id->fd,
231 ASSERT_RET (id->refcount >= 0, "double free");
232 if (id->refcount == 0) {
233 memset (id, 0xA1, sizeof(*id));
241 jwxyz_timers_run (jwxyz_sources_data *td)
243 /* Iterate the timer list, being careful because XtRemoveTimeOut removes
244 the current item from that list. */
245 if (td->all_timers) {
246 XtIntervalId timer, next;
247 double now = double_time();
250 for (timer = td->all_timers, next = timer->next;
252 timer = next, next = (timer ? timer->next : 0)) {
253 if (timer->run_at <= now) {
254 LOGT("timer 0x%08lX: fire %.02f", (unsigned long) timer,
255 now - timer->run_at);
256 timer->cb (timer->closure, &timer);
257 XtRemoveTimeOut (timer);
259 ASSERT_RET (count < 10000, "way too many timers to run");
267 jwxyz_sources_run (jwxyz_sources_data *td)
269 if (td->fd_count == 0) return;
271 struct timeval tv = { 0, };
277 for (i = 0; i < FD_SETSIZE; i++) {
284 ASSERT_RET (max > 0, "no fds");
286 if (0 < select (max+1, &fds, NULL, NULL, &tv)) {
287 for (i = 0; i < FD_SETSIZE; i++) {
288 if (FD_ISSET (i, &fds)) {
289 XtInputId id = td->ids[i];
290 ASSERT_RET (id && id->cb, "sources corrupted");
291 ASSERT_RET (id->fd == i, "sources corrupted");
292 id->cb (id->closure, &id->fd, &id);
300 jwxyz_XtRemoveInput_all (jwxyz_sources_data *td)
303 for (i = 0; i < FD_SETSIZE; i++) {
304 XtInputId id = td->ids[i];
305 if (id) XtRemoveInput (id);
311 jwxyz_XtRemoveTimeOut_all (jwxyz_sources_data *td)
313 XtIntervalId timer, next;
316 /* Iterate the timer list, being careful because XtRemoveTimeOut removes
317 the current item from that list. */
318 if (td->all_timers) {
319 for (timer = td->all_timers, next = timer->next;
321 timer = next, next = (timer ? timer->next : 0)) {
322 XtRemoveTimeOut (timer);
324 ASSERT_RET (count < 10000, "way too many timers to free");
326 ASSERT_RET (!td->all_timers, "timer list didn't empty");
332 jwxyz_sources_free (jwxyz_sources_data *td)
334 jwxyz_XtRemoveInput_all (td);
335 jwxyz_XtRemoveTimeOut_all (td);
336 memset (td, 0xA1, sizeof(*td));
342 XtAppPending (XtAppContext app)
344 return XtIMAlternateInput; /* just always say yes */
348 XtAppProcessEvent (XtAppContext app, XtInputMask mask)
350 jwxyz_sources_data *td = DISPLAY_SOURCES_DATA (app);
351 if (mask & XtIMAlternateInput)
352 jwxyz_sources_run (td);
353 if (mask & XtIMTimer)
354 jwxyz_timers_run (td);
357 #endif /* HAVE_JWXYZ */