982b073291687780549080a7bfe161b4bfee158a
[xscreensaver] / driver / timers.c
1 /* timers.c --- detecting when the user is idle, and other timer-related tasks.
2  * xscreensaver, Copyright (c) 1991-1997, 1998
3  *  Jamie Zawinski <jwz@jwz.org>
4  *
5  * Permission to use, copy, modify, distribute, and sell this software and its
6  * documentation for any purpose is hereby granted without fee, provided that
7  * the above copyright notice appear in all copies and that both that
8  * copyright notice and this permission notice appear in supporting
9  * documentation.  No representations are made about the suitability of this
10  * software for any purpose.  It is provided "as is" without express or 
11  * implied warranty.
12  */
13
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
17
18 /* #define DEBUG_TIMERS */
19
20 #include <stdio.h>
21 #include <X11/Xlib.h>
22 #include <X11/Intrinsic.h>
23 #include <X11/Xos.h>
24 #ifdef HAVE_XMU
25 # ifndef VMS
26 #  include <X11/Xmu/Error.h>
27 # else /* VMS */
28 #  include <Xmu/Error.h>
29 # endif /* VMS */
30 # else /* !HAVE_XMU */
31 # include "xmu.h"
32 #endif /* !HAVE_XMU */
33
34 #ifdef HAVE_XIDLE_EXTENSION
35 #include <X11/extensions/xidle.h>
36 #endif /* HAVE_XIDLE_EXTENSION */
37
38 #ifdef HAVE_MIT_SAVER_EXTENSION
39 #include <X11/extensions/scrnsaver.h>
40 #endif /* HAVE_MIT_SAVER_EXTENSION */
41
42 #ifdef HAVE_SGI_SAVER_EXTENSION
43 #include <X11/extensions/XScreenSaver.h>
44 #endif /* HAVE_SGI_SAVER_EXTENSION */
45
46 #ifdef HAVE_XHPDISABLERESET
47 # include <X11/XHPlib.h>
48   extern Bool hp_locked_p;      /* from windows.c */
49 #endif /* HAVE_XHPDISABLERESET */
50
51 #include "xscreensaver.h"
52
53 #ifdef HAVE_PROC_INTERRUPTS
54 static Bool proc_interrupts_activity_p (saver_info *si);
55 #endif /* HAVE_PROC_INTERRUPTS */
56
57
58 void
59 idle_timer (XtPointer closure, XtIntervalId *id)
60 {
61   saver_info *si = (saver_info *) closure;
62
63   /* What an amazingly shitty design.  Not only does Xt execute timeout
64      events from XtAppNextEvent() instead of from XtDispatchEvent(), but
65      there is no way to tell Xt to block until there is an X event OR a
66      timeout happens.  Once your timeout proc is called, XtAppNextEvent()
67      still won't return until a "real" X event comes in.
68
69      So this function pushes a stupid, gratuitous, unnecessary event back
70      on the event queue to force XtAppNextEvent to return Right Fucking Now.
71      When the code in sleep_until_idle() sees an event of type XAnyEvent,
72      which the server never generates, it knows that a timeout has occurred.
73    */
74   XEvent fake_event;
75   fake_event.type = 0;  /* XAnyEvent type, ignored. */
76   fake_event.xany.display = si->dpy;
77   fake_event.xany.window  = 0;
78   XPutBackEvent (si->dpy, &fake_event);
79 }
80
81
82 static void
83 schedule_wakeup_event (saver_info *si, Time when, Bool verbose_p)
84 {
85   /* Wake up periodically to ask the server if we are idle. */
86   si->timer_id = XtAppAddTimeOut (si->app, when, idle_timer,
87                                   (XtPointer) si);
88
89 #ifdef DEBUG_TIMERS
90   if (verbose_p)
91     fprintf (stderr, "%s: starting idle_timer (%ld, %ld)\n",
92              blurb(), when, si->timer_id);
93 #endif /* DEBUG_TIMERS */
94 }
95
96
97 static void
98 notice_events (saver_info *si, Window window, Bool top_p)
99 {
100   saver_preferences *p = &si->prefs;
101   XWindowAttributes attrs;
102   unsigned long events;
103   Window root, parent, *kids;
104   unsigned int nkids;
105
106   if (XtWindowToWidget (si->dpy, window))
107     /* If it's one of ours, don't mess up its event mask. */
108     return;
109
110   if (!XQueryTree (si->dpy, window, &root, &parent, &kids, &nkids))
111     return;
112   if (window == root)
113     top_p = False;
114
115   XGetWindowAttributes (si->dpy, window, &attrs);
116   events = ((attrs.all_event_masks | attrs.do_not_propagate_mask)
117             & KeyPressMask);
118
119   /* Select for SubstructureNotify on all windows.
120      Select for KeyPress on all windows that already have it selected.
121      Do we need to select for ButtonRelease?  I don't think so.
122    */
123   XSelectInput (si->dpy, window, SubstructureNotifyMask | events);
124
125   if (top_p && p->verbose_p && (events & KeyPressMask))
126     {
127       /* Only mention one window per tree (hack hack). */
128       fprintf (stderr, "%s: selected KeyPress on 0x%lX\n", blurb(),
129                (unsigned long) window);
130       top_p = False;
131     }
132
133   if (kids)
134     {
135       while (nkids)
136         notice_events (si, kids [--nkids], top_p);
137       XFree ((char *) kids);
138     }
139 }
140
141
142 int
143 BadWindow_ehandler (Display *dpy, XErrorEvent *error)
144 {
145   /* When we notice a window being created, we spawn a timer that waits
146      30 seconds or so, and then selects events on that window.  This error
147      handler is used so that we can cope with the fact that the window
148      may have been destroyed <30 seconds after it was created.
149    */
150   if (error->error_code == BadWindow ||
151       error->error_code == BadMatch ||
152       error->error_code == BadDrawable)
153     return 0;
154   else
155     return saver_ehandler (dpy, error);
156 }
157
158
159 struct notice_events_timer_arg {
160   saver_info *si;
161   Window w;
162 };
163
164 static void
165 notice_events_timer (XtPointer closure, XtIntervalId *id)
166 {
167   struct notice_events_timer_arg *arg =
168     (struct notice_events_timer_arg *) closure;
169
170   XErrorHandler old_handler = XSetErrorHandler (BadWindow_ehandler);
171
172   saver_info *si = arg->si;
173   Window window = arg->w;
174
175   free(arg);
176   notice_events (si, window, True);
177   XSync (si->dpy, False);
178   XSetErrorHandler (old_handler);
179 }
180
181 void
182 start_notice_events_timer (saver_info *si, Window w, Bool verbose_p)
183 {
184   saver_preferences *p = &si->prefs;
185   struct notice_events_timer_arg *arg =
186     (struct notice_events_timer_arg *) malloc(sizeof(*arg));
187   arg->si = si;
188   arg->w = w;
189   XtAppAddTimeOut (si->app, p->notice_events_timeout, notice_events_timer,
190                    (XtPointer) arg);
191
192   if (verbose_p)
193     fprintf (stderr, "%s: starting notice_events_timer for 0x%X (%lu)\n",
194              blurb(), (unsigned int) w, p->notice_events_timeout);
195 }
196
197
198 /* When the screensaver is active, this timer will periodically change
199    the running program.
200  */
201 void
202 cycle_timer (XtPointer closure, XtIntervalId *id)
203 {
204   saver_info *si = (saver_info *) closure;
205   saver_preferences *p = &si->prefs;
206   Time how_long = p->cycle;
207   if (si->dbox_up_p)
208     {
209       if (p->verbose_p)
210         fprintf (stderr, "%s: dialog box up; delaying hack change.\n",
211                  blurb());
212       how_long = 30000; /* 30 secs */
213     }
214   else
215     {
216       maybe_reload_init_file (si);
217       if (p->verbose_p)
218         fprintf (stderr, "%s: changing graphics hacks.\n", blurb());
219       kill_screenhack (si);
220       spawn_screenhack (si, False);
221     }
222   si->cycle_id = XtAppAddTimeOut (si->app, how_long, cycle_timer,
223                                   (XtPointer) si);
224
225 #ifdef DEBUG_TIMERS
226   if (p->verbose_p)
227     fprintf (stderr, "%s: starting cycle_timer (%ld, %ld)\n",
228             blurb(), how_long, si->cycle_id);
229 #endif /* DEBUG_TIMERS */
230 }
231
232
233 void
234 activate_lock_timer (XtPointer closure, XtIntervalId *id)
235 {
236   saver_info *si = (saver_info *) closure;
237   saver_preferences *p = &si->prefs;
238
239   if (p->verbose_p)
240     fprintf (stderr, "%s: timed out; activating lock\n", blurb());
241   si->locked_p = True;
242
243 #ifdef HAVE_XHPDISABLERESET
244   if (!hp_locked_p)
245     {
246       XHPDisableReset (si->dpy);        /* turn off C-Sh-Reset */
247       hp_locked_p = True;
248     }
249 #endif
250 }
251
252
253 /* Call this when user activity (or "simulated" activity) has been noticed.
254  */
255 static void
256 reset_timers (saver_info *si)
257 {
258   saver_preferences *p = &si->prefs;
259   if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
260     return;
261
262   if (si->timer_id)
263     {
264 #ifdef DEBUG_TIMERS
265       if (p->verbose_p)
266         fprintf (stderr, "%s: killing idle_timer  (%ld, %ld)\n",
267                  blurb(), p->timeout, si->timer_id);
268 #endif /* DEBUG_TIMERS */
269       XtRemoveTimeOut (si->timer_id);
270     }
271
272   schedule_wakeup_event (si, p->timeout, p->verbose_p); /* sets si->timer_id */
273
274   if (si->cycle_id) abort ();   /* no cycle timer when inactive */
275
276   si->last_activity_time = time ((time_t *) 0);
277 }
278
279
280 /* When we aren't using a server extension, this timer is used to periodically
281    wake up and poll the mouse position, which is possibly more reliable than
282    selecting motion events on every window.
283  */
284 static void
285 check_pointer_timer (XtPointer closure, XtIntervalId *id)
286 {
287   int i;
288   saver_info *si = (saver_info *) closure;
289   saver_preferences *p = &si->prefs;
290   Bool active_p = False;
291
292   if (!si->using_proc_interrupts &&
293       (si->using_xidle_extension ||
294        si->using_mit_saver_extension ||
295        si->using_sgi_saver_extension))
296     /* If an extension is in use, we should not be polling the mouse.
297        Unless we're also checking /proc/interrupts, in which case, we should.
298      */
299     abort ();
300
301   si->check_pointer_timer_id =
302     XtAppAddTimeOut (si->app, p->pointer_timeout, check_pointer_timer,
303                      (XtPointer) si);
304
305   for (i = 0; i < si->nscreens; i++)
306     {
307       saver_screen_info *ssi = &si->screens[i];
308       Window root, child;
309       int root_x, root_y, x, y;
310       unsigned int mask;
311
312       XQueryPointer (si->dpy, ssi->screensaver_window, &root, &child,
313                      &root_x, &root_y, &x, &y, &mask);
314
315       if (root_x == ssi->poll_mouse_last_root_x &&
316           root_y == ssi->poll_mouse_last_root_y &&
317           child  == ssi->poll_mouse_last_child &&
318           mask   == ssi->poll_mouse_last_mask)
319         continue;
320
321       active_p = True;
322
323 #ifdef DEBUG_TIMERS
324       if (p->verbose_p)
325         if (root_x == ssi->poll_mouse_last_root_x &&
326             root_y == ssi->poll_mouse_last_root_y &&
327             child  == ssi->poll_mouse_last_child)
328           fprintf (stderr, "%s: modifiers changed at %s on screen %d.\n",
329                    blurb(), timestring(), i);
330         else
331           fprintf (stderr, "%s: pointer moved at %s on screen %d.\n",
332                    blurb(), timestring(), i);
333 #endif /* DEBUG_TIMERS */
334
335       si->last_activity_screen    = ssi;
336       ssi->poll_mouse_last_root_x = root_x;
337       ssi->poll_mouse_last_root_y = root_y;
338       ssi->poll_mouse_last_child  = child;
339       ssi->poll_mouse_last_mask   = mask;
340     }
341
342 #ifdef HAVE_PROC_INTERRUPTS
343   if (!active_p &&
344       si->using_proc_interrupts &&
345       proc_interrupts_activity_p (si))
346     {
347 # ifdef DEBUG_TIMERS
348       if (p->verbose_p)
349         fprintf (stderr, "%s: /proc/interrupts activity at %s.\n",
350                  blurb(), timestring());
351 # endif /* DEBUG_TIMERS */
352       active_p = True;
353     }
354 #endif /* HAVE_PROC_INTERRUPTS */
355
356   if (active_p)
357     reset_timers (si);
358 }
359
360
361 static void
362 dispatch_event (saver_info *si, XEvent *event)
363 {
364   /* If this is for the splash dialog, pass it along.
365      Note that the password dialog is handled with its own event loop,
366      so events for that window will never come through here.
367    */
368   if (si->splash_dialog && event->xany.window == si->splash_dialog)
369     handle_splash_event (si, event);
370
371   XtDispatchEvent (event);
372 }
373
374
375 /* methods of detecting idleness:
376
377       explicitly informed by SGI SCREEN_SAVER server event;
378       explicitly informed by MIT-SCREEN-SAVER server event;
379       poll server idle time with XIDLE extension;
380       select events on all windows, and note absence of recent events;
381       note that /proc/interrupts has not changed in a while;
382       activated by clientmessage.
383
384    methods of detecting non-idleness:
385
386       read events on the xscreensaver window;
387       explicitly informed by SGI SCREEN_SAVER server event;
388       explicitly informed by MIT-SCREEN-SAVER server event;
389       select events on all windows, and note events on any of them;
390       note that /proc/interrupts has changed;
391       deactivated by clientmessage.
392
393    I trust that explains why this function is a big hairy mess.
394  */
395 void
396 sleep_until_idle (saver_info *si, Bool until_idle_p)
397 {
398   saver_preferences *p = &si->prefs;
399   XEvent event;
400
401   /* We need to select events on all windows if we're not using any extensions.
402      Otherwise, we don't need to. */
403   Bool scanning_all_windows = !(si->using_xidle_extension ||
404                                 si->using_mit_saver_extension ||
405                                 si->using_sgi_saver_extension);
406
407   /* We need to periodically wake up and check for idleness if we're not using
408      any extensions, or if we're using the XIDLE extension.  The other two
409      extensions explicitly deliver events when we go idle/non-idle, so we
410      don't need to poll. */
411   Bool polling_for_idleness = !(si->using_mit_saver_extension ||
412                                 si->using_sgi_saver_extension);
413
414   /* Whether we need to periodically wake up and check to see if the mouse has
415      moved.  We only need to do this when not using any extensions.  The reason
416      this isn't the same as `polling_for_idleness' is that the "idleness" poll
417      can happen (for example) 5 minutes from now, whereas the mouse-position
418      poll should happen with low periodicity.  We don't need to poll the mouse
419      position with the XIDLE extension, but we do need to periodically wake up
420      and query the server with that extension.  For our purposes, polling
421      /proc/interrupts is just like polling the mouse position.  It has to
422      happen on the same kind of schedule. */
423   Bool polling_mouse_position = (si->using_proc_interrupts ||
424                                  !(si->using_xidle_extension ||
425                                    si->using_mit_saver_extension ||
426                                    si->using_sgi_saver_extension));
427
428   if (until_idle_p)
429     {
430       if (polling_for_idleness)
431         /* This causes a no-op event to be delivered to us in a while, so that
432            we come back around through the event loop again.  Use of this timer
433            is economical: for example, if the screensaver should come on in 5
434            minutes, and the user has been idle for 2 minutes, then this
435            timeout will go off no sooner than 3 minutes from now.  */
436         schedule_wakeup_event (si, p->timeout, p->verbose_p);
437
438       if (polling_mouse_position)
439         /* Check to see if the mouse has moved, and set up a repeating timer
440            to do so periodically (typically, every 5 seconds.) */
441         check_pointer_timer ((XtPointer) si, 0);
442     }
443
444   while (1)
445     {
446       XtAppNextEvent (si->app, &event);
447
448       switch (event.xany.type) {
449       case 0:           /* our synthetic "timeout" event has been signalled */
450         if (until_idle_p)
451           {
452             Time idle;
453 #ifdef HAVE_XIDLE_EXTENSION
454             if (si->using_xidle_extension)
455               {
456                 /* The XIDLE extension uses the synthetic event to prod us into
457                    re-asking the server how long the user has been idle. */
458                 if (! XGetIdleTime (si->dpy, &idle))
459                   {
460                     fprintf (stderr, "%s: XGetIdleTime() failed.\n", blurb());
461                     saver_exit (si, 1, 0);
462                   }
463               }
464             else
465 #endif /* HAVE_XIDLE_EXTENSION */
466 #ifdef HAVE_MIT_SAVER_EXTENSION
467               if (si->using_mit_saver_extension)
468                 {
469                   /* We don't need to do anything in this case - the synthetic
470                      event isn't necessary, as we get sent specific events
471                      to wake us up.  In fact, this event generally shouldn't
472                      be being delivered when the MIT extension is in use. */
473                   idle = 0;
474                 }
475             else
476 #endif /* HAVE_MIT_SAVER_EXTENSION */
477 #ifdef HAVE_SGI_SAVER_EXTENSION
478               if (si->using_sgi_saver_extension)
479                 {
480                   /* We don't need to do anything in this case - the synthetic
481                      event isn't necessary, as we get sent specific events
482                      to wake us up.  In fact, this event generally shouldn't
483                      be being delivered when the SGI extension is in use. */
484                   idle = 0;
485                 }
486             else
487 #endif /* HAVE_SGI_SAVER_EXTENSION */
488               {
489                 /* Otherwise, no server extension is in use.  The synthetic
490                    event was to tell us to wake up and see if the user is now
491                    idle.  Compute the amount of idle time by comparing the
492                    `last_activity_time' to the wall clock.  The l_a_t was set
493                    by calling `reset_timers()', which is called only in only
494                    two situations: when polling the mouse position has revealed
495                    the the mouse has moved (user activity) or when we have read
496                    an event (again, user activity.)
497                  */
498                 idle = 1000 * (si->last_activity_time - time ((time_t *) 0));
499               }
500
501             if (idle >= p->timeout)
502               {
503                 /* Look, we've been idle long enough.  We're done. */
504                 goto DONE;
505               }
506             else
507               {
508                 /* The event went off, but it turns out that the user has not
509                    yet been idle for long enough.  So re-signal the event.
510                    */
511                 if (polling_for_idleness)
512                   schedule_wakeup_event (si, p->timeout - idle, p->verbose_p);
513               }
514           }
515         break;
516
517       case ClientMessage:
518         if (handle_clientmessage (si, &event, until_idle_p))
519           goto DONE;
520         break;
521
522       case CreateNotify:
523         /* A window has been created on the screen somewhere.  If we're
524            supposed to scan all windows for events, prepare this window. */
525         if (scanning_all_windows)
526           {
527             Window w = event.xcreatewindow.window;
528 #ifdef DEBUG_TIMERS
529             start_notice_events_timer (si, w, p->verbose_p);
530 #else  /* !DEBUG_TIMERS */
531             start_notice_events_timer (si, w, False);
532 #endif /* !DEBUG_TIMERS */
533           }
534         break;
535
536       case KeyPress:
537       case KeyRelease:
538       case ButtonPress:
539       case ButtonRelease:
540       case MotionNotify:
541
542 #ifdef DEBUG_TIMERS
543         if (p->verbose_p)
544           {
545             if (event.xany.type == MotionNotify)
546               fprintf (stderr,"%s: MotionNotify at %s\n",blurb(),timestring());
547             else if (event.xany.type == KeyPress)
548               fprintf (stderr, "%s: KeyPress seen on 0x%X at %s\n", blurb(),
549                        (unsigned int) event.xkey.window, timestring ());
550             else if (event.xany.type == ButtonPress)
551               fprintf (stderr, "%s: ButtonPress seen on 0x%X at %s\n", blurb(),
552                        (unsigned int) event.xbutton.window, timestring ());
553           }
554 #endif /* DEBUG_TIMERS */
555
556         /* If any widgets want to handle this event, let them. */
557         dispatch_event (si, &event);
558
559         /* We got a user event.
560            If we're waiting for the user to become active, this is it.
561            If we're waiting until the user becomes idle, reset the timers
562            (since now we have longer to wait.)
563          */
564         if (!until_idle_p)
565           {
566             if (si->demoing_p &&
567                 (event.xany.type == MotionNotify ||
568                  event.xany.type == KeyRelease))
569               /* When we're demoing a single hack, mouse motion doesn't
570                  cause deactivation.  Only clicks and keypresses do. */
571               ;
572             else
573               /* If we're not demoing, then any activity causes deactivation.
574                */
575               goto DONE;
576           }
577         else
578           reset_timers (si);
579
580         break;
581
582       default:
583
584 #ifdef HAVE_MIT_SAVER_EXTENSION
585         if (event.type == si->mit_saver_ext_event_number)
586           {
587             /* This event's number is that of the MIT-SCREEN-SAVER server
588                extension.  This extension has one event number, and the event
589                itself contains sub-codes that say what kind of event it was
590                (an "idle" or "not-idle" event.)
591              */
592             XScreenSaverNotifyEvent *sevent =
593               (XScreenSaverNotifyEvent *) &event;
594             if (sevent->state == ScreenSaverOn)
595               {
596                 int i = 0;
597                 if (p->verbose_p)
598                   fprintf (stderr, "%s: MIT ScreenSaverOn event received.\n",
599                            blurb());
600
601                 /* Get the "real" server window(s) out of the way as soon
602                    as possible. */
603                 for (i = 0; i < si->nscreens; i++)
604                   {
605                     saver_screen_info *ssi = &si->screens[i];
606                     if (ssi->server_mit_saver_window &&
607                         window_exists_p (si->dpy,
608                                          ssi->server_mit_saver_window))
609                       XUnmapWindow (si->dpy, ssi->server_mit_saver_window);
610                   }
611
612                 if (sevent->kind != ScreenSaverExternal)
613                   {
614                     fprintf (stderr,
615                          "%s: ScreenSaverOn event wasn't of type External!\n",
616                              blurb());
617                   }
618
619                 if (until_idle_p)
620                   goto DONE;
621               }
622             else if (sevent->state == ScreenSaverOff)
623               {
624                 if (p->verbose_p)
625                   fprintf (stderr, "%s: MIT ScreenSaverOff event received.\n",
626                            blurb());
627                 if (!until_idle_p)
628                   goto DONE;
629               }
630             else
631               fprintf (stderr,
632                        "%s: unknown MIT-SCREEN-SAVER event %d received!\n",
633                        blurb(), sevent->state);
634           }
635         else
636
637 #endif /* HAVE_MIT_SAVER_EXTENSION */
638
639
640 #ifdef HAVE_SGI_SAVER_EXTENSION
641         if (event.type == (si->sgi_saver_ext_event_number + ScreenSaverStart))
642           {
643             /* The SGI SCREEN_SAVER server extension has two event numbers,
644                and this event matches the "idle" event. */
645             if (p->verbose_p)
646               fprintf (stderr, "%s: SGI ScreenSaverStart event received.\n",
647                        blurb());
648
649             if (until_idle_p)
650               goto DONE;
651           }
652         else if (event.type == (si->sgi_saver_ext_event_number +
653                                 ScreenSaverEnd))
654           {
655             /* The SGI SCREEN_SAVER server extension has two event numbers,
656                and this event matches the "idle" event. */
657             if (p->verbose_p)
658               fprintf (stderr, "%s: SGI ScreenSaverEnd event received.\n",
659                        blurb());
660             if (!until_idle_p)
661               goto DONE;
662           }
663         else
664 #endif /* HAVE_SGI_SAVER_EXTENSION */
665
666           /* Just some random event.  Let the Widgets handle it, if desired. */
667           dispatch_event (si, &event);
668       }
669     }
670  DONE:
671
672
673   /* If there's a user event on the queue, swallow it.
674      If we're using a server extension, and the user becomes active, we
675      get the extension event before the user event -- so the keypress or
676      motion or whatever is still on the queue.  This makes "unfade" not
677      work, because it sees that event, and bugs out.  (This problem
678      doesn't exhibit itself without an extension, because in that case,
679      there's only one event generated by user activity, not two.)
680    */
681   XCheckMaskEvent (si->dpy, (KeyPressMask|ButtonPressMask|PointerMotionMask),
682                    &event);
683
684
685   if (si->check_pointer_timer_id)
686     {
687       XtRemoveTimeOut (si->check_pointer_timer_id);
688       si->check_pointer_timer_id = 0;
689     }
690   if (si->timer_id)
691     {
692       XtRemoveTimeOut (si->timer_id);
693       si->timer_id = 0;
694     }
695
696   if (until_idle_p && si->cycle_id)     /* no cycle timer when inactive */
697     abort ();
698
699   return;
700 }
701
702
703 \f
704 /* Some crap for dealing with /proc/interrupts.
705
706    On Linux systems, it's possible to see the hardware interrupt count
707    associated with the keyboard.  We can therefore use that as another method
708    of detecting idleness.
709
710    Why is it a good idea to do this?  Because it lets us detect keyboard
711    activity that is not associated with X events.  For example, if the user
712    has switched to another virtual console, it's good for xscreensaver to not
713    be running graphics hacks on the (non-visible) X display.  The common
714    complaint that checking /proc/interrupts addresses is that the user is
715    playing Quake on a non-X console, and the GL hacks are perceptibly slowing
716    the game...
717
718    This is tricky for a number of reasons.
719
720      * First, we must be sure to only do this when running on an X server that
721        is on the local machine (because otherwise, we'd be reacting to the
722        wrong keyboard.)  The way we do this is by noting that the $DISPLAY is
723        pointing to display 0 on the local machine.  It *could* be that display
724        1 is also on the local machine (e.g., two X servers, each on a different
725        virtual-terminal) but it's also possible that screen 1 is an X terminal,
726        using this machine as the host.  So we can't take that chance.
727
728      * Second, one can only access these interrupt numbers in a completely
729        and utterly brain-damaged way.  You would think that one would use an
730        ioctl for this.  But no.  The ONLY way to get this information is to
731        open the pseudo-file /proc/interrupts AS A FILE, and read the numbers
732        out of it TEXTUALLY.  Because this is Unix, and all the world's a file,
733        and the only real data type is the short-line sequence of ASCII bytes.
734
735        Now it's all well and good that the /proc/interrupts pseudo-file
736        exists; that's a clever idea, and a useful API for things that are
737        already textually oriented, like shell scripts, and users doing
738        interactive debugging sessions.  But to make a *C PROGRAM* open a file
739        and parse the textual representation of integers out of it is just
740        insane.
741
742      * Third, you can't just hold the file open, and fseek() back to the
743        beginning to get updated data!  If you do that, the data never changes.
744        And I don't want to call open() every five seconds, because I don't want
745        to risk going to disk for any inodes.  It turns out that if you dup()
746        it early, then each copy gets fresh data, so we can get around that in
747        this way (but for how many releases, one might wonder?)
748
749      * Fourth, the format of the output of the /proc/interrupts file is
750        undocumented, and has changed several times already!  In Linux 2.0.33,
751        even on a multiprocessor machine, it looks like this:
752
753           0:  309453991   timer
754           1:    4771729   keyboard
755    
756        but on later kernels with MP machines, it looks like this:
757
758                    CPU0       CPU1
759           0:    1671450    1672618    IO-APIC-edge  timer
760           1:      13037      13495    IO-APIC-edge  keyboard
761
762        Joy!  So how are we expected to parse that?  Well, this code doesn't
763        parse it: it saves the last line with the string "keyboard" in it, and
764        does a string-comparison to note when it has changed.
765
766    Thanks to Nat Friedman <nat@nat.org> for figuring out all of this crap.
767
768    Note that this only checks for lines with "keyboard" in them.  Perhaps we
769    should also be checking for lines with "PS/2 Mouse" in them.  But that
770    would obviously fail to work for regular serial mice, and obviously just
771    using COM1 would be bad news (turn off the screensaver because the modem
772    is active, yum.)
773  */
774
775
776 #ifdef HAVE_PROC_INTERRUPTS
777
778 #define PROC_INTERRUPTS "/proc/interrupts"
779
780 Bool
781 query_proc_interrupts_available (saver_info *si, const char **why)
782 {
783   /* We can use /proc/interrupts if $DISPLAY points to :0, and if the
784      "/proc/interrupts" file exists and is readable.
785    */
786   FILE *f;
787   if (why) *why = 0;
788
789   if (!display_is_on_console_p (si))
790     {
791       if (why) *why = "not on primary console";
792       return False;
793     }
794
795   f = fopen (PROC_INTERRUPTS, "r");
796   if (!f)
797     return False;
798
799   fclose (f);
800   return True;
801 }
802
803
804 static Bool
805 proc_interrupts_activity_p (saver_info *si)
806 {
807   static FILE *f0 = 0;
808   FILE *f1 = 0;
809   int fd;
810   static char last_line[255] = { 0, };
811   char new_line[sizeof(last_line)];
812
813   if (!f0)
814     {
815       /* First time -- open the file. */
816       f0 = fopen (PROC_INTERRUPTS, "r");
817       if (!f0)
818         {
819           char buf[255];
820           sprintf(buf, "%s: error opening %s", blurb(), PROC_INTERRUPTS);
821           perror (buf);
822           goto FAIL;
823         }
824     }
825
826   if (f0 == (FILE *) -1)            /* means we got an error initializing. */
827     return False;
828
829   fd = dup (fileno (f0));
830   if (fd < 0)
831     {
832       char buf[255];
833       sprintf(buf, "%s: could not dup() the %s fd", blurb(), PROC_INTERRUPTS);
834       perror (buf);
835       goto FAIL;
836     }
837
838   f1 = fdopen (fd, "r");
839   if (!f1)
840     {
841       char buf[255];
842       sprintf(buf, "%s: could not fdopen() the %s fd", blurb(),
843               PROC_INTERRUPTS);
844       perror (buf);
845       goto FAIL;
846     }
847
848   /* Actually, I'm unclear on why this fseek() is necessary, given the timing
849      of the dup() above, but it is. */
850   if (fseek (f1, 0, SEEK_SET) != 0)
851     {
852       char buf[255];
853       sprintf(buf, "%s: error rewinding %s", blurb(), PROC_INTERRUPTS);
854       perror (buf);
855       goto FAIL;
856     }
857
858   /* Now read through the pseudo-file until we find the "keyboard" line. */
859
860   while (fgets (new_line, sizeof(new_line)-1, f1))
861     if (strstr (new_line, "keyboard"))
862       {
863         Bool diff = (*last_line &&
864                      !!strcmp (new_line, last_line));
865         strcpy (last_line, new_line);   /* save this line for next time */
866         fclose (f1);
867         return diff;
868       }
869
870   /* If we got here, we didn't find a "keyboard" line in the file at all. */
871   fprintf (stderr, "%s: no keyboard data in %s?\n", blurb(), PROC_INTERRUPTS);
872
873  FAIL:
874   if (f1)
875     fclose (f1);
876
877   if (f0 && f0 != (FILE *) -1)
878     fclose (f0);
879
880   f0 = (FILE *) -1;
881   return False;
882 }
883
884 #endif /* HAVE_PROC_INTERRUPTS */
885
886 \f
887 /* This timer goes off every few minutes, whether the user is idle or not,
888    to try and clean up anything that has gone wrong.
889
890    It calls disable_builtin_screensaver() so that if xset has been used,
891    or some other program (like xlock) has messed with the XSetScreenSaver()
892    settings, they will be set back to sensible values (if a server extension
893    is in use, messing with xlock can cause xscreensaver to never get a wakeup
894    event, and could cause monitor power-saving to occur, and all manner of
895    heinousness.)
896
897    If the screen is currently blanked, it raises the window, in case some
898    other window has been mapped on top of it.
899
900    If the screen is currently blanked, and there is no hack running, it
901    clears the window, in case there is an error message printed on it (we
902    don't want the error message to burn in.)
903  */
904
905 static void
906 watchdog_timer (XtPointer closure, XtIntervalId *id)
907 {
908   saver_info *si = (saver_info *) closure;
909
910   disable_builtin_screensaver (si, False);
911
912   if (si->screen_blanked_p)
913     {
914       Bool running_p = screenhack_running_p(si);
915
916 #ifdef DEBUG_TIMERS
917       if (si->prefs.verbose_p)
918         fprintf (stderr, "%s: watchdog timer raising %sscreen.\n",
919                  blurb(), (running_p ? "" : "and clearing "));
920 #endif /* DEBUG_TIMERS */
921
922       raise_window (si, True, True, running_p);
923
924       if (!monitor_powered_on_p (si))
925         {
926           if (si->prefs.verbose_p)
927             fprintf (stderr,
928                      "%s: server reports that monitor has powered down; "
929                      "killing running hacks.\n", blurb());
930           kill_screenhack (si);
931         }
932     }
933 }
934
935
936 void
937 reset_watchdog_timer (saver_info *si, Bool on_p)
938 {
939   saver_preferences *p = &si->prefs;
940
941   if (si->watchdog_id)
942     {
943       XtRemoveTimeOut (si->watchdog_id);
944       si->watchdog_id = 0;
945     }
946
947   if (on_p && p->watchdog_timeout)
948     {
949       si->watchdog_id = XtAppAddTimeOut (si->app, p->watchdog_timeout,
950                                          watchdog_timer, (XtPointer) si);
951
952 #ifdef DEBUG_TIMERS
953       if (p->verbose_p)
954         fprintf (stderr, "%s: restarting watchdog_timer (%ld, %ld)\n",
955                  blurb(), p->watchdog_timeout, si->watchdog_id);
956 #endif /* DEBUG_TIMERS */
957
958     }
959 }