http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.23.tar.gz
[xscreensaver] / driver / timers.c
1 /* timers.c --- detecting when the user is idle, and other timer-related tasks.
2  * xscreensaver, Copyright (c) 1991-2004 Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <stdio.h>
18 #include <X11/Xlib.h>
19 #include <X11/Intrinsic.h>
20 #include <X11/Xos.h>
21 #include <time.h>
22 #include <sys/time.h>
23 #ifdef HAVE_XMU
24 # ifndef VMS
25 #  include <X11/Xmu/Error.h>
26 # else /* VMS */
27 #  include <Xmu/Error.h>
28 # endif /* VMS */
29 # else /* !HAVE_XMU */
30 # include "xmu.h"
31 #endif /* !HAVE_XMU */
32
33 #ifdef HAVE_XIDLE_EXTENSION
34 #include <X11/extensions/xidle.h>
35 #endif /* HAVE_XIDLE_EXTENSION */
36
37 #ifdef HAVE_MIT_SAVER_EXTENSION
38 #include <X11/extensions/scrnsaver.h>
39 #endif /* HAVE_MIT_SAVER_EXTENSION */
40
41 #ifdef HAVE_SGI_SAVER_EXTENSION
42 #include <X11/extensions/XScreenSaver.h>
43 #endif /* HAVE_SGI_SAVER_EXTENSION */
44
45 #ifdef HAVE_RANDR
46 #include <X11/extensions/Xrandr.h>
47 #endif /* HAVE_RANDR */
48
49 #include "xscreensaver.h"
50
51 #undef ABS
52 #define ABS(x)((x)<0?-(x):(x))
53
54 #undef MAX
55 #define MAX(x,y)((x)>(y)?(x):(y))
56
57
58 #ifdef HAVE_PROC_INTERRUPTS
59 static Bool proc_interrupts_activity_p (saver_info *si);
60 #endif /* HAVE_PROC_INTERRUPTS */
61
62 static void check_for_clock_skew (saver_info *si);
63
64
65 void
66 idle_timer (XtPointer closure, XtIntervalId *id)
67 {
68   saver_info *si = (saver_info *) closure;
69
70   /* What an amazingly shitty design.  Not only does Xt execute timeout
71      events from XtAppNextEvent() instead of from XtDispatchEvent(), but
72      there is no way to tell Xt to block until there is an X event OR a
73      timeout happens.  Once your timeout proc is called, XtAppNextEvent()
74      still won't return until a "real" X event comes in.
75
76      So this function pushes a stupid, gratuitous, unnecessary event back
77      on the event queue to force XtAppNextEvent to return Right Fucking Now.
78      When the code in sleep_until_idle() sees an event of type XAnyEvent,
79      which the server never generates, it knows that a timeout has occurred.
80    */
81   XEvent fake_event;
82   fake_event.type = 0;  /* XAnyEvent type, ignored. */
83   fake_event.xany.display = si->dpy;
84   fake_event.xany.window  = 0;
85   XPutBackEvent (si->dpy, &fake_event);
86
87   /* If we are the timer that just went off, clear the pointer to the id. */
88   if (id)
89     {
90       if (si->timer_id && *id != si->timer_id)
91         abort();  /* oops, scheduled timer twice?? */
92       si->timer_id = 0;
93     }
94 }
95
96
97 void
98 schedule_wakeup_event (saver_info *si, Time when, Bool verbose_p)
99 {
100   if (si->timer_id)
101     {
102       if (verbose_p)
103         fprintf (stderr, "%s: idle_timer already running\n", blurb());
104       return;
105     }
106
107   /* Wake up periodically to ask the server if we are idle. */
108   si->timer_id = XtAppAddTimeOut (si->app, when, idle_timer,
109                                   (XtPointer) si);
110
111   if (verbose_p)
112     fprintf (stderr, "%s: starting idle_timer (%ld, %ld)\n",
113              blurb(), when, si->timer_id);
114 }
115
116
117 static void
118 notice_events (saver_info *si, Window window, Bool top_p)
119 {
120   saver_preferences *p = &si->prefs;
121   XWindowAttributes attrs;
122   unsigned long events;
123   Window root, parent, *kids;
124   unsigned int nkids;
125   int screen_no;
126
127   if (XtWindowToWidget (si->dpy, window))
128     /* If it's one of ours, don't mess up its event mask. */
129     return;
130
131   if (!XQueryTree (si->dpy, window, &root, &parent, &kids, &nkids))
132     return;
133   if (window == root)
134     top_p = False;
135
136   /* Figure out which screen this window is on, for the diagnostics. */
137   for (screen_no = 0; screen_no < si->nscreens; screen_no++)
138     if (root == RootWindowOfScreen (si->screens[screen_no].screen))
139       break;
140
141   XGetWindowAttributes (si->dpy, window, &attrs);
142   events = ((attrs.all_event_masks | attrs.do_not_propagate_mask)
143             & KeyPressMask);
144
145   /* Select for SubstructureNotify on all windows.
146      Select for KeyPress on all windows that already have it selected.
147
148      Note that we can't select for ButtonPress, because of X braindamage:
149      only one client at a time may select for ButtonPress on a given
150      window, though any number can select for KeyPress.  Someone explain
151      *that* to me.
152
153      So, if the user spends a while clicking the mouse without ever moving
154      the mouse or touching the keyboard, we won't know that they've been
155      active, and the screensaver will come on.  That sucks, but I don't
156      know how to get around it.
157
158      Since X presents mouse wheels as clicks, this applies to those, too:
159      scrolling through a document using only the mouse wheel doesn't
160      count as activity...  Fortunately, /proc/interrupts helps, on
161      systems that have it.  Oh, if it's a PS/2 mouse, not serial or USB.
162      This sucks!
163    */
164   XSelectInput (si->dpy, window, SubstructureNotifyMask | events);
165
166   if (top_p && p->debug_p && (events & KeyPressMask))
167     {
168       /* Only mention one window per tree (hack hack). */
169       fprintf (stderr, "%s: %d: selected KeyPress on 0x%lX\n",
170                blurb(), screen_no, (unsigned long) window);
171       top_p = False;
172     }
173
174   if (kids)
175     {
176       while (nkids)
177         notice_events (si, kids [--nkids], top_p);
178       XFree ((char *) kids);
179     }
180 }
181
182
183 int
184 BadWindow_ehandler (Display *dpy, XErrorEvent *error)
185 {
186   /* When we notice a window being created, we spawn a timer that waits
187      30 seconds or so, and then selects events on that window.  This error
188      handler is used so that we can cope with the fact that the window
189      may have been destroyed <30 seconds after it was created.
190    */
191   if (error->error_code == BadWindow ||
192       error->error_code == BadMatch ||
193       error->error_code == BadDrawable)
194     return 0;
195   else
196     return saver_ehandler (dpy, error);
197 }
198
199
200 struct notice_events_timer_arg {
201   saver_info *si;
202   Window w;
203 };
204
205 static void
206 notice_events_timer (XtPointer closure, XtIntervalId *id)
207 {
208   struct notice_events_timer_arg *arg =
209     (struct notice_events_timer_arg *) closure;
210
211   XErrorHandler old_handler = XSetErrorHandler (BadWindow_ehandler);
212
213   saver_info *si = arg->si;
214   Window window = arg->w;
215
216   free(arg);
217   notice_events (si, window, True);
218   XSync (si->dpy, False);
219   XSetErrorHandler (old_handler);
220 }
221
222 void
223 start_notice_events_timer (saver_info *si, Window w, Bool verbose_p)
224 {
225   saver_preferences *p = &si->prefs;
226   struct notice_events_timer_arg *arg =
227     (struct notice_events_timer_arg *) malloc(sizeof(*arg));
228   arg->si = si;
229   arg->w = w;
230   XtAppAddTimeOut (si->app, p->notice_events_timeout, notice_events_timer,
231                    (XtPointer) arg);
232
233   if (verbose_p)
234     fprintf (stderr, "%s: starting notice_events_timer for 0x%X (%lu)\n",
235              blurb(), (unsigned int) w, p->notice_events_timeout);
236 }
237
238
239 /* When the screensaver is active, this timer will periodically change
240    the running program.
241  */
242 void
243 cycle_timer (XtPointer closure, XtIntervalId *id)
244 {
245   saver_info *si = (saver_info *) closure;
246   saver_preferences *p = &si->prefs;
247   Time how_long = p->cycle;
248
249   if (si->selection_mode > 0 &&
250       screenhack_running_p (si))
251     /* If we're in "SELECT n" mode, the cycle timer going off will just
252        restart this same hack again.  There's not much point in doing this
253        every 5 or 10 minutes, but on the other hand, leaving one hack running
254        for days is probably not a great idea, since they tend to leak and/or
255        crash.  So, restart the thing once an hour. */
256     how_long = 1000 * 60 * 60;
257
258   if (si->dbox_up_p)
259     {
260       if (p->verbose_p)
261         fprintf (stderr, "%s: dialog box up; delaying hack change.\n",
262                  blurb());
263       how_long = 30000; /* 30 secs */
264     }
265   else
266     {
267       maybe_reload_init_file (si);
268       kill_screenhack (si);
269
270       if (!si->throttled_p)
271         spawn_screenhack (si, False);
272       else
273         {
274           raise_window (si, True, True, False);
275           if (p->verbose_p)
276             fprintf (stderr, "%s: not launching new hack (throttled.)\n",
277                      blurb());
278         }
279     }
280
281   if (how_long > 0)
282     {
283       si->cycle_id = XtAppAddTimeOut (si->app, how_long, cycle_timer,
284                                       (XtPointer) si);
285
286       if (p->debug_p)
287         fprintf (stderr, "%s: starting cycle_timer (%ld, %ld)\n",
288                  blurb(), how_long, si->cycle_id);
289     }
290   else
291     {
292       if (p->debug_p)
293         fprintf (stderr, "%s: not starting cycle_timer: how_long == %ld\n",
294                  blurb(), (unsigned long) how_long);
295     }
296 }
297
298
299 void
300 activate_lock_timer (XtPointer closure, XtIntervalId *id)
301 {
302   saver_info *si = (saver_info *) closure;
303   saver_preferences *p = &si->prefs;
304
305   if (p->verbose_p)
306     fprintf (stderr, "%s: timed out; activating lock.\n", blurb());
307   set_locked_p (si, True);
308 }
309
310
311 /* Call this when user activity (or "simulated" activity) has been noticed.
312  */
313 void
314 reset_timers (saver_info *si)
315 {
316   saver_preferences *p = &si->prefs;
317   if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
318     return;
319
320   if (si->timer_id)
321     {
322       if (p->debug_p)
323         fprintf (stderr, "%s: killing idle_timer  (%ld, %ld)\n",
324                  blurb(), p->timeout, si->timer_id);
325       XtRemoveTimeOut (si->timer_id);
326       si->timer_id = 0;
327     }
328
329   schedule_wakeup_event (si, p->timeout, p->debug_p); /* sets si->timer_id */
330
331   if (si->cycle_id) abort ();   /* no cycle timer when inactive */
332
333   si->last_activity_time = time ((time_t *) 0);
334
335   /* This will (hopefully, supposedly) tell the server to re-set its
336      DPMS timer.  Without this, the -deactivate clientmessage would
337      prevent xscreensaver from blanking, but would not prevent the
338      monitor from powering down. */
339   XForceScreenSaver (si->dpy, ScreenSaverReset);
340
341   /* And if the monitor is already powered off, turn it on.
342      You'd think the above would do that, but apparently not? */
343   monitor_power_on (si);
344 }
345
346
347 /* Returns true if the mouse has moved since the last time we checked.
348    Small motions (of less than "hysteresis" pixels/second) are ignored.
349  */
350 static Bool
351 pointer_moved_p (saver_screen_info *ssi, Bool mods_p)
352 {
353   saver_info *si = ssi->global;
354   saver_preferences *p = &si->prefs;
355
356   Window root, child;
357   int root_x, root_y, x, y;
358   unsigned int mask;
359   time_t now = time ((time_t *) 0);
360   unsigned int distance, dps;
361   unsigned long seconds = 0;
362   Bool moved_p = False;
363
364   /* don't check xinerama pseudo-screens. */
365   if (!ssi->real_screen_p) return False;
366
367   if (!XQueryPointer (si->dpy, ssi->screensaver_window, &root, &child,
368                       &root_x, &root_y, &x, &y, &mask))
369     {
370       /* If XQueryPointer() returns false, the mouse is not on this screen.
371        */
372       x = root_x = -1;
373       y = root_y = -1;
374       root = child = 0;
375       mask = 0;
376     }
377
378   distance = MAX (ABS (ssi->poll_mouse_last_root_x - root_x),
379                   ABS (ssi->poll_mouse_last_root_y - root_y));
380   seconds = (now - ssi->poll_mouse_last_time);
381
382
383   /* When the screen is blanked, we get MotionNotify events, but when not
384      blanked, we poll only every 5 seconds, and that's not enough resolution
385      to do hysteresis based on a 1 second interval.  So, assume that any
386      motion we've seen during the 5 seconds when our eyes were closed happened
387      in the last 1 second instead.
388    */
389   if (seconds > 1) seconds = 1;
390
391   dps = (seconds <= 0 ? distance : (distance / seconds));
392
393   /* Motion only counts if the rate is more than N pixels per second.
394    */
395   if (dps >= p->pointer_hysteresis &&
396       distance > 0)
397     moved_p = True;
398
399   /* If the mouse is not on this screen but used to be, that's motion.
400      If the mouse was not on this screen, but is now, that's motion.
401    */
402   {
403     Bool on_screen_p  = (root_x != -1 && root_y != -1);
404     Bool was_on_screen_p = (ssi->poll_mouse_last_root_x != -1 &&
405                             ssi->poll_mouse_last_root_y != -1);
406
407     if (on_screen_p != was_on_screen_p)
408       moved_p = True;
409   }
410
411   if (p->debug_p && (distance != 0 || moved_p))
412     {
413       fprintf (stderr, "%s: %d: pointer %s", blurb(), ssi->number,
414                (moved_p ? "moved:  " : "ignored:"));
415       if (ssi->poll_mouse_last_root_x == -1)
416         fprintf (stderr, "off screen");
417       else
418         fprintf (stderr, "%d,%d",
419                  ssi->poll_mouse_last_root_x,
420                  ssi->poll_mouse_last_root_y);
421       fprintf (stderr, " -> ");
422       if (root_x == -1)
423         fprintf (stderr, "off screen");
424       else
425         fprintf (stderr, "%d,%d", root_x, root_y);
426       if (ssi->poll_mouse_last_root_x != -1 && root_x != -1)
427         fprintf (stderr, " (%d,%d; %d/%lu=%d)",
428                  ABS(ssi->poll_mouse_last_root_x - root_x),
429                  ABS(ssi->poll_mouse_last_root_y - root_y),
430                  distance, seconds, dps);
431
432       fprintf (stderr, ".\n");
433     }
434
435   if (!moved_p &&
436       mods_p &&
437       mask != ssi->poll_mouse_last_mask)
438     {
439       moved_p = True;
440
441       if (p->debug_p)
442         fprintf (stderr, "%s: %d: modifiers changed: 0x%04x -> 0x%04x.\n",
443                  blurb(), ssi->number, ssi->poll_mouse_last_mask, mask);
444     }
445
446   si->last_activity_screen   = ssi;
447   ssi->poll_mouse_last_child = child;
448   ssi->poll_mouse_last_mask  = mask;
449
450   if (moved_p || seconds > 0)
451     {
452       ssi->poll_mouse_last_time   = now;
453       ssi->poll_mouse_last_root_x = root_x;
454       ssi->poll_mouse_last_root_y = root_y;
455     }
456
457   return moved_p;
458 }
459
460
461 /* When we aren't using a server extension, this timer is used to periodically
462    wake up and poll the mouse position, which is possibly more reliable than
463    selecting motion events on every window.
464  */
465 static void
466 check_pointer_timer (XtPointer closure, XtIntervalId *id)
467 {
468   int i;
469   saver_info *si = (saver_info *) closure;
470   saver_preferences *p = &si->prefs;
471   Bool active_p = False;
472
473   if (!si->using_proc_interrupts &&
474       (si->using_xidle_extension ||
475        si->using_mit_saver_extension ||
476        si->using_sgi_saver_extension))
477     /* If an extension is in use, we should not be polling the mouse.
478        Unless we're also checking /proc/interrupts, in which case, we should.
479      */
480     abort ();
481
482   if (id && *id == si->check_pointer_timer_id)  /* this is us - it's expired */
483     si->check_pointer_timer_id = 0;
484
485   if (si->check_pointer_timer_id)               /* only queue one at a time */
486     XtRemoveTimeOut (si->check_pointer_timer_id);
487
488   si->check_pointer_timer_id =                  /* now re-queue */
489     XtAppAddTimeOut (si->app, p->pointer_timeout, check_pointer_timer,
490                      (XtPointer) si);
491
492   for (i = 0; i < si->nscreens; i++)
493     {
494       saver_screen_info *ssi = &si->screens[i];
495       if (pointer_moved_p (ssi, True))
496         active_p = True;
497     }
498
499 #ifdef HAVE_PROC_INTERRUPTS
500   if (!active_p &&
501       si->using_proc_interrupts &&
502       proc_interrupts_activity_p (si))
503     {
504       active_p = True;
505     }
506 #endif /* HAVE_PROC_INTERRUPTS */
507
508   if (active_p)
509     reset_timers (si);
510
511   check_for_clock_skew (si);
512 }
513
514
515 /* An unfortunate situation is this: the saver is not active, because the
516    user has been typing.  The machine is a laptop.  The user closes the lid
517    and suspends it.  The CPU halts.  Some hours later, the user opens the
518    lid.  At this point, Xt's timers will fire, and xscreensaver will blank
519    the screen.
520
521    So far so good -- well, not really, but it's the best that we can do,
522    since the OS doesn't send us a signal *before* shutdown -- but if the
523    user had delayed locking (lockTimeout > 0) then we should start off
524    in the locked state, rather than only locking N minutes from when the
525    lid was opened.  Also, eschewing fading is probably a good idea, to
526    clamp down as soon as possible.
527
528    We only do this when we'd be polling the mouse position anyway.
529    This amounts to an assumption that machines with APM support also
530    have /proc/interrupts.
531  */
532 static void
533 check_for_clock_skew (saver_info *si)
534 {
535   saver_preferences *p = &si->prefs;
536   time_t now = time ((time_t *) 0);
537   long shift = now - si->last_wall_clock_time;
538
539   if (p->debug_p)
540     {
541       int i = (si->last_wall_clock_time == 0 ? 0 : shift);
542       fprintf (stderr,
543                "%s: checking wall clock for hibernation (%d:%02d:%02d).\n",
544                blurb(),
545                (i / (60 * 60)), ((i / 60) % 60), (i % 60));
546     }
547
548   if (si->last_wall_clock_time != 0 &&
549       shift > (p->timeout / 1000))
550     {
551       if (p->verbose_p)
552         fprintf (stderr, "%s: wall clock has jumped by %ld:%02ld:%02ld!\n",
553                  blurb(),
554                  (shift / (60 * 60)), ((shift / 60) % 60), (shift % 60));
555
556       si->emergency_lock_p = True;
557       idle_timer ((XtPointer) si, 0);
558     }
559
560   si->last_wall_clock_time = now;
561 }
562
563
564
565 static void
566 dispatch_event (saver_info *si, XEvent *event)
567 {
568   /* If this is for the splash dialog, pass it along.
569      Note that the password dialog is handled with its own event loop,
570      so events for that window will never come through here.
571    */
572   if (si->splash_dialog && event->xany.window == si->splash_dialog)
573     handle_splash_event (si, event);
574
575   XtDispatchEvent (event);
576 }
577
578
579 static void
580 swallow_unlock_typeahead_events (saver_info *si, XEvent *e)
581 {
582   XEvent event;
583   char buf [100];
584   int i = 0;
585
586   memset (buf, 0, sizeof(buf));
587
588   event = *e;
589
590   do
591     {
592       if (event.xany.type == KeyPress)
593         {
594           char s[2];
595           int size = XLookupString ((XKeyEvent *) &event, s, 1, 0, 0);
596           if (size != 1) continue;
597           switch (*s)
598             {
599             case '\010': case '\177':                   /* Backspace */
600               if (i > 0) i--;
601               break;
602             case '\025': case '\030':                   /* Erase line */
603             case '\012': case '\015':                   /* Enter */
604               i = 0;
605               break;
606             case '\040':                                /* Space */
607               if (i == 0)
608                 break;  /* ignore space at beginning of line */
609               /* else, fall through */
610             default:
611               buf [i++] = *s;
612               break;
613             }
614         }
615
616     } while (i < sizeof(buf)-1 &&
617              XCheckMaskEvent (si->dpy, KeyPressMask, &event));
618
619   buf[i] = 0;
620
621   if (si->unlock_typeahead)
622     {
623       memset (si->unlock_typeahead, 0, strlen(si->unlock_typeahead));
624       free (si->unlock_typeahead);
625     }
626
627   if (i > 0)
628     si->unlock_typeahead = strdup (buf);
629   else
630     si->unlock_typeahead = 0;
631
632   memset (buf, 0, sizeof(buf));
633 }
634
635
636 /* methods of detecting idleness:
637
638       explicitly informed by SGI SCREEN_SAVER server event;
639       explicitly informed by MIT-SCREEN-SAVER server event;
640       poll server idle time with XIDLE extension;
641       select events on all windows, and note absence of recent events;
642       note that /proc/interrupts has not changed in a while;
643       activated by clientmessage.
644
645    methods of detecting non-idleness:
646
647       read events on the xscreensaver window;
648       explicitly informed by SGI SCREEN_SAVER server event;
649       explicitly informed by MIT-SCREEN-SAVER server event;
650       select events on all windows, and note events on any of them;
651       note that /proc/interrupts has changed;
652       deactivated by clientmessage.
653
654    I trust that explains why this function is a big hairy mess.
655  */
656 void
657 sleep_until_idle (saver_info *si, Bool until_idle_p)
658 {
659   saver_preferences *p = &si->prefs;
660   XEvent event;
661
662   /* We need to select events on all windows if we're not using any extensions.
663      Otherwise, we don't need to. */
664   Bool scanning_all_windows = !(si->using_xidle_extension ||
665                                 si->using_mit_saver_extension ||
666                                 si->using_sgi_saver_extension);
667
668   /* We need to periodically wake up and check for idleness if we're not using
669      any extensions, or if we're using the XIDLE extension.  The other two
670      extensions explicitly deliver events when we go idle/non-idle, so we
671      don't need to poll. */
672   Bool polling_for_idleness = !(si->using_mit_saver_extension ||
673                                 si->using_sgi_saver_extension);
674
675   /* Whether we need to periodically wake up and check to see if the mouse has
676      moved.  We only need to do this when not using any extensions.  The reason
677      this isn't the same as `polling_for_idleness' is that the "idleness" poll
678      can happen (for example) 5 minutes from now, whereas the mouse-position
679      poll should happen with low periodicity.  We don't need to poll the mouse
680      position with the XIDLE extension, but we do need to periodically wake up
681      and query the server with that extension.  For our purposes, polling
682      /proc/interrupts is just like polling the mouse position.  It has to
683      happen on the same kind of schedule. */
684   Bool polling_mouse_position = (si->using_proc_interrupts ||
685                                  !(si->using_xidle_extension ||
686                                    si->using_mit_saver_extension ||
687                                    si->using_sgi_saver_extension));
688
689   if (until_idle_p)
690     {
691       if (polling_for_idleness)
692         /* This causes a no-op event to be delivered to us in a while, so that
693            we come back around through the event loop again.  */
694         schedule_wakeup_event (si, p->timeout, p->debug_p);
695
696       if (polling_mouse_position)
697         /* Check to see if the mouse has moved, and set up a repeating timer
698            to do so periodically (typically, every 5 seconds.) */
699         check_pointer_timer ((XtPointer) si, 0);
700     }
701
702   while (1)
703     {
704       XtAppNextEvent (si->app, &event);
705
706       switch (event.xany.type) {
707       case 0:           /* our synthetic "timeout" event has been signalled */
708         if (until_idle_p)
709           {
710             Time idle;
711
712             /* We may be idle; check one last time to see if the mouse has
713                moved, just in case the idle-timer went off within the 5 second
714                window between mouse polling.  If the mouse has moved, then
715                check_pointer_timer() will reset last_activity_time.
716              */
717             if (polling_mouse_position)
718               check_pointer_timer ((XtPointer) si, 0);
719
720 #ifdef HAVE_XIDLE_EXTENSION
721             if (si->using_xidle_extension)
722               {
723                 /* The XIDLE extension uses the synthetic event to prod us into
724                    re-asking the server how long the user has been idle. */
725                 if (! XGetIdleTime (si->dpy, &idle))
726                   {
727                     fprintf (stderr, "%s: XGetIdleTime() failed.\n", blurb());
728                     saver_exit (si, 1, 0);
729                   }
730               }
731             else
732 #endif /* HAVE_XIDLE_EXTENSION */
733 #ifdef HAVE_MIT_SAVER_EXTENSION
734               if (si->using_mit_saver_extension)
735                 {
736                   /* We don't need to do anything in this case - the synthetic
737                      event isn't necessary, as we get sent specific events
738                      to wake us up.  In fact, this event generally shouldn't
739                      be being delivered when the MIT extension is in use. */
740                   idle = 0;
741                 }
742             else
743 #endif /* HAVE_MIT_SAVER_EXTENSION */
744 #ifdef HAVE_SGI_SAVER_EXTENSION
745               if (si->using_sgi_saver_extension)
746                 {
747                   /* We don't need to do anything in this case - the synthetic
748                      event isn't necessary, as we get sent specific events
749                      to wake us up.  In fact, this event generally shouldn't
750                      be being delivered when the SGI extension is in use. */
751                   idle = 0;
752                 }
753             else
754 #endif /* HAVE_SGI_SAVER_EXTENSION */
755               {
756                 /* Otherwise, no server extension is in use.  The synthetic
757                    event was to tell us to wake up and see if the user is now
758                    idle.  Compute the amount of idle time by comparing the
759                    `last_activity_time' to the wall clock.  The l_a_t was set
760                    by calling `reset_timers()', which is called only in only
761                    two situations: when polling the mouse position has revealed
762                    the the mouse has moved (user activity) or when we have read
763                    an event (again, user activity.)
764                  */
765                 idle = 1000 * (si->last_activity_time - time ((time_t *) 0));
766               }
767
768             if (idle >= p->timeout)
769               {
770                 /* Look, we've been idle long enough.  We're done. */
771                 goto DONE;
772               }
773             else if (si->emergency_lock_p)
774               {
775                 /* Oops, the wall clock has jumped far into the future, so
776                    we need to lock down in a hurry! */
777                 goto DONE;
778               }
779             else
780               {
781                 /* The event went off, but it turns out that the user has not
782                    yet been idle for long enough.  So re-signal the event.
783                    Be economical: if we should blank after 5 minutes, and the
784                    user has been idle for 2 minutes, then set this timer to
785                    go off in 3 minutes.
786                  */
787                 if (polling_for_idleness)
788                   schedule_wakeup_event (si, p->timeout - idle, p->debug_p);
789               }
790           }
791         break;
792
793       case ClientMessage:
794         if (handle_clientmessage (si, &event, until_idle_p))
795           goto DONE;
796         break;
797
798       case CreateNotify:
799         /* A window has been created on the screen somewhere.  If we're
800            supposed to scan all windows for events, prepare this window. */
801         if (scanning_all_windows)
802           {
803             Window w = event.xcreatewindow.window;
804             start_notice_events_timer (si, w, p->debug_p);
805           }
806         break;
807
808       case KeyPress:
809       case KeyRelease:
810       case ButtonPress:
811       case ButtonRelease:
812       case MotionNotify:
813
814         if (p->debug_p)
815           {
816             Window root=0, window=0;
817             int x=-1, y=-1;
818             const char *type = 0;
819             if (event.xany.type == MotionNotify)
820               {
821                 /*type = "MotionNotify";*/
822                 root = event.xmotion.root;
823                 window = event.xmotion.window;
824                 x = event.xmotion.x_root;
825                 y = event.xmotion.y_root;
826               }
827             else if (event.xany.type == KeyPress)
828               {
829                 type = "KeyPress";
830                 root = event.xkey.root;
831                 window = event.xkey.window;
832                 x = y = -1;
833               }
834             else if (event.xany.type == ButtonPress)
835               {
836                 type = "ButtonPress";
837                 root = event.xkey.root;
838                 window = event.xkey.window;
839                 x = event.xmotion.x_root;
840                 y = event.xmotion.y_root;
841               }
842
843             if (type)
844               {
845                 int i;
846                 for (i = 0; i < si->nscreens; i++)
847                   if (root == RootWindowOfScreen (si->screens[i].screen))
848                     break;
849                 fprintf (stderr,"%s: %d: %s on 0x%lx",
850                          blurb(), i, type, (unsigned long) window);
851
852                 /* Be careful never to do this unless in -debug mode, as
853                    this could expose characters from the unlock password. */
854                 if (p->debug_p && event.xany.type == KeyPress)
855                   {
856                     KeySym keysym;
857                     char c = 0;
858                     XLookupString (&event.xkey, &c, 1, &keysym, 0);
859                     fprintf (stderr, " (%s%s)",
860                              (event.xkey.send_event ? "synthetic " : ""),
861                              XKeysymToString (keysym));
862                   }
863
864                 if (x == -1)
865                   fprintf (stderr, "\n");
866                 else
867                   fprintf (stderr, " at %d,%d.\n", x, y);
868               }
869           }
870
871         /* If any widgets want to handle this event, let them. */
872         dispatch_event (si, &event);
873
874         
875         /* If we got a MotionNotify event, figure out what screen it
876            was on and poll the mouse there: if the mouse hasn't moved
877            far enough to count as "real" motion, then ignore this
878            event.
879          */
880         if (event.xany.type == MotionNotify)
881           {
882             int i;
883             for (i = 0; i < si->nscreens; i++)
884               if (event.xmotion.root ==
885                   RootWindowOfScreen (si->screens[i].screen))
886                 break;
887             if (i < si->nscreens)
888               {
889                 if (!pointer_moved_p (&si->screens[i], False))
890                   continue;
891               }
892           }
893
894
895         /* We got a user event.
896            If we're waiting for the user to become active, this is it.
897            If we're waiting until the user becomes idle, reset the timers
898            (since now we have longer to wait.)
899          */
900         if (!until_idle_p)
901           {
902             if (si->demoing_p &&
903                 (event.xany.type == MotionNotify ||
904                  event.xany.type == KeyRelease))
905               /* When we're demoing a single hack, mouse motion doesn't
906                  cause deactivation.  Only clicks and keypresses do. */
907               ;
908             else
909               /* If we're not demoing, then any activity causes deactivation.
910                */
911               goto DONE;
912           }
913         else
914           reset_timers (si);
915
916         break;
917
918       default:
919
920 #ifdef HAVE_MIT_SAVER_EXTENSION
921         if (event.type == si->mit_saver_ext_event_number)
922           {
923             /* This event's number is that of the MIT-SCREEN-SAVER server
924                extension.  This extension has one event number, and the event
925                itself contains sub-codes that say what kind of event it was
926                (an "idle" or "not-idle" event.)
927              */
928             XScreenSaverNotifyEvent *sevent =
929               (XScreenSaverNotifyEvent *) &event;
930             if (sevent->state == ScreenSaverOn)
931               {
932                 int i = 0;
933                 if (p->verbose_p)
934                   fprintf (stderr, "%s: MIT ScreenSaverOn event received.\n",
935                            blurb());
936
937                 /* Get the "real" server window(s) out of the way as soon
938                    as possible. */
939                 for (i = 0; i < si->nscreens; i++)
940                   {
941                     saver_screen_info *ssi = &si->screens[i];
942                     if (ssi->server_mit_saver_window &&
943                         window_exists_p (si->dpy,
944                                          ssi->server_mit_saver_window))
945                       XUnmapWindow (si->dpy, ssi->server_mit_saver_window);
946                   }
947
948                 if (sevent->kind != ScreenSaverExternal)
949                   {
950                     fprintf (stderr,
951                          "%s: ScreenSaverOn event wasn't of type External!\n",
952                              blurb());
953                   }
954
955                 if (until_idle_p)
956                   goto DONE;
957               }
958             else if (sevent->state == ScreenSaverOff)
959               {
960                 if (p->verbose_p)
961                   fprintf (stderr, "%s: MIT ScreenSaverOff event received.\n",
962                            blurb());
963                 if (!until_idle_p)
964                   goto DONE;
965               }
966             else
967               fprintf (stderr,
968                        "%s: unknown MIT-SCREEN-SAVER event %d received!\n",
969                        blurb(), sevent->state);
970           }
971         else
972
973 #endif /* HAVE_MIT_SAVER_EXTENSION */
974
975
976 #ifdef HAVE_SGI_SAVER_EXTENSION
977         if (event.type == (si->sgi_saver_ext_event_number + ScreenSaverStart))
978           {
979             /* The SGI SCREEN_SAVER server extension has two event numbers,
980                and this event matches the "idle" event. */
981             if (p->verbose_p)
982               fprintf (stderr, "%s: SGI ScreenSaverStart event received.\n",
983                        blurb());
984
985             if (until_idle_p)
986               goto DONE;
987           }
988         else if (event.type == (si->sgi_saver_ext_event_number +
989                                 ScreenSaverEnd))
990           {
991             /* The SGI SCREEN_SAVER server extension has two event numbers,
992                and this event matches the "idle" event. */
993             if (p->verbose_p)
994               fprintf (stderr, "%s: SGI ScreenSaverEnd event received.\n",
995                        blurb());
996             if (!until_idle_p)
997               goto DONE;
998           }
999         else
1000 #endif /* HAVE_SGI_SAVER_EXTENSION */
1001
1002 #ifdef HAVE_RANDR
1003         if (event.type == (si->randr_event_number + RRScreenChangeNotify))
1004           {
1005             /* The Resize and Rotate extension sends an event when the
1006                size, rotation, or refresh rate of the screen has changed. */
1007
1008             XRRScreenChangeNotifyEvent *xrr_event =
1009               (XRRScreenChangeNotifyEvent *) &event;
1010             /* XRRRootToScreen is in Xrandr.h 1.4, 2001/06/07 */
1011             int screen = XRRRootToScreen (si->dpy, xrr_event->window);
1012
1013             if (p->verbose_p)
1014               {
1015                 if (si->screens[screen].width  == xrr_event->width &&
1016                     si->screens[screen].height == xrr_event->height)
1017                   fprintf (stderr,
1018                           "%s: %d: no-op screen size change event (%dx%d)\n",
1019                            blurb(), screen,
1020                            xrr_event->width, xrr_event->height);
1021                 else
1022                   fprintf (stderr,
1023                        "%s: %d: screen size changed from %dx%d to %dx%d\n",
1024                            blurb(), screen,
1025                            si->screens[screen].width,
1026                            si->screens[screen].height,
1027                            xrr_event->width, xrr_event->height);
1028               }
1029
1030 # ifdef RRScreenChangeNotifyMask
1031             /* Inform Xlib that it's ok to update its data structures. */
1032             XRRUpdateConfiguration (&event); /* Xrandr.h 1.9, 2002/09/29 */
1033 # endif /* RRScreenChangeNotifyMask */
1034
1035             /* Resize the existing xscreensaver windows and cached ssi data. */
1036             resize_screensaver_window (si);
1037           }
1038         else
1039 #endif /* HAVE_RANDR */
1040
1041           /* Just some random event.  Let the Widgets handle it, if desired. */
1042           dispatch_event (si, &event);
1043       }
1044     }
1045  DONE:
1046
1047
1048   /* If there's a user event on the queue, swallow it.
1049      If we're using a server extension, and the user becomes active, we
1050      get the extension event before the user event -- so the keypress or
1051      motion or whatever is still on the queue.  This makes "unfade" not
1052      work, because it sees that event, and bugs out.  (This problem
1053      doesn't exhibit itself without an extension, because in that case,
1054      there's only one event generated by user activity, not two.)
1055    */
1056   if (!until_idle_p && si->locked_p)
1057     swallow_unlock_typeahead_events (si, &event);
1058   else
1059     while (XCheckMaskEvent (si->dpy,
1060                             (KeyPressMask|ButtonPressMask|PointerMotionMask),
1061                      &event))
1062       ;
1063
1064
1065   if (si->check_pointer_timer_id)
1066     {
1067       XtRemoveTimeOut (si->check_pointer_timer_id);
1068       si->check_pointer_timer_id = 0;
1069     }
1070   if (si->timer_id)
1071     {
1072       XtRemoveTimeOut (si->timer_id);
1073       si->timer_id = 0;
1074     }
1075
1076   if (until_idle_p && si->cycle_id)     /* no cycle timer when inactive */
1077     abort ();
1078
1079   return;
1080 }
1081
1082
1083 \f
1084 /* Some crap for dealing with /proc/interrupts.
1085
1086    On Linux systems, it's possible to see the hardware interrupt count
1087    associated with the keyboard.  We can therefore use that as another method
1088    of detecting idleness.
1089
1090    Why is it a good idea to do this?  Because it lets us detect keyboard
1091    activity that is not associated with X events.  For example, if the user
1092    has switched to another virtual console, it's good for xscreensaver to not
1093    be running graphics hacks on the (non-visible) X display.  The common
1094    complaint that checking /proc/interrupts addresses is that the user is
1095    playing Quake on a non-X console, and the GL hacks are perceptibly slowing
1096    the game...
1097
1098    This is tricky for a number of reasons.
1099
1100      * First, we must be sure to only do this when running on an X server that
1101        is on the local machine (because otherwise, we'd be reacting to the
1102        wrong keyboard.)  The way we do this is by noting that the $DISPLAY is
1103        pointing to display 0 on the local machine.  It *could* be that display
1104        1 is also on the local machine (e.g., two X servers, each on a different
1105        virtual-terminal) but it's also possible that screen 1 is an X terminal,
1106        using this machine as the host.  So we can't take that chance.
1107
1108      * Second, one can only access these interrupt numbers in a completely
1109        and utterly brain-damaged way.  You would think that one would use an
1110        ioctl for this.  But no.  The ONLY way to get this information is to
1111        open the pseudo-file /proc/interrupts AS A FILE, and read the numbers
1112        out of it TEXTUALLY.  Because this is Unix, and all the world's a file,
1113        and the only real data type is the short-line sequence of ASCII bytes.
1114
1115        Now it's all well and good that the /proc/interrupts pseudo-file
1116        exists; that's a clever idea, and a useful API for things that are
1117        already textually oriented, like shell scripts, and users doing
1118        interactive debugging sessions.  But to make a *C PROGRAM* open a file
1119        and parse the textual representation of integers out of it is just
1120        insane.
1121
1122      * Third, you can't just hold the file open, and fseek() back to the
1123        beginning to get updated data!  If you do that, the data never changes.
1124        And I don't want to call open() every five seconds, because I don't want
1125        to risk going to disk for any inodes.  It turns out that if you dup()
1126        it early, then each copy gets fresh data, so we can get around that in
1127        this way (but for how many releases, one might wonder?)
1128
1129      * Fourth, the format of the output of the /proc/interrupts file is
1130        undocumented, and has changed several times already!  In Linux 2.0.33,
1131        even on a multiprocessor machine, it looks like this:
1132
1133           0:  309453991   timer
1134           1:    4771729   keyboard
1135    
1136        but in Linux 2.2 and 2.4 kernels with MP machines, it looks like this:
1137
1138                    CPU0       CPU1
1139           0:    1671450    1672618    IO-APIC-edge  timer
1140           1:      13037      13495    IO-APIC-edge  keyboard
1141
1142        and in Linux 2.6, it's gotten even goofier: now there are two lines
1143        labelled "i8042".  One of them is the keyboard, and one of them is
1144        the PS/2 mouse -- and of course, you can't tell them apart, except
1145        by wiggling the mouse and noting which one changes:
1146
1147                    CPU0       CPU1
1148           1:      32051      30864    IO-APIC-edge  i8042
1149          12:     476577     479913    IO-APIC-edge  i8042
1150
1151        Joy!  So how are we expected to parse that?  Well, this code doesn't
1152        parse it: it saves the first line with the string "keyboard" (or
1153        "i8042") in it, and does a string-comparison to note when it has
1154        changed.  If there are two "i8042" lines, we assume the first is
1155        the keyboard and the second is the mouse (doesn't matter which is
1156        which, really, as long as we don't compare them against each other.)
1157
1158    Thanks to Nat Friedman <nat@nat.org> for figuring out most of this crap.
1159
1160    Note that if you have a serial or USB mouse, or a USB keyboard, it won't
1161    detect it.  That's because there's no way to tell the difference between a
1162    serial mouse and a general serial port, and all USB devices look the same
1163    from here.  It would be somewhat unfortunate to have the screensaver turn
1164    off when the modem on COM1 burped, or when a USB disk was accessed.
1165  */
1166
1167
1168 #ifdef HAVE_PROC_INTERRUPTS
1169
1170 #define PROC_INTERRUPTS "/proc/interrupts"
1171
1172 Bool
1173 query_proc_interrupts_available (saver_info *si, const char **why)
1174 {
1175   /* We can use /proc/interrupts if $DISPLAY points to :0, and if the
1176      "/proc/interrupts" file exists and is readable.
1177    */
1178   FILE *f;
1179   if (why) *why = 0;
1180
1181   if (!display_is_on_console_p (si))
1182     {
1183       if (why) *why = "not on primary console";
1184       return False;
1185     }
1186
1187   f = fopen (PROC_INTERRUPTS, "r");
1188   if (!f)
1189     return False;
1190
1191   fclose (f);
1192   return True;
1193 }
1194
1195
1196 static Bool
1197 proc_interrupts_activity_p (saver_info *si)
1198 {
1199   static FILE *f0 = 0;
1200   FILE *f1 = 0;
1201   int fd;
1202   static char last_kbd_line[255] = { 0, };
1203   static char last_ptr_line[255] = { 0, };
1204   char new_line[sizeof(last_kbd_line)];
1205   Bool checked_kbd = False, kbd_changed = False;
1206   Bool checked_ptr = False, ptr_changed = False;
1207   int i8042_count = 0;
1208
1209   if (!f0)
1210     {
1211       /* First time -- open the file. */
1212       f0 = fopen (PROC_INTERRUPTS, "r");
1213       if (!f0)
1214         {
1215           char buf[255];
1216           sprintf(buf, "%s: error opening %s", blurb(), PROC_INTERRUPTS);
1217           perror (buf);
1218           goto FAIL;
1219         }
1220     }
1221
1222   if (f0 == (FILE *) -1)            /* means we got an error initializing. */
1223     return False;
1224
1225   fd = dup (fileno (f0));
1226   if (fd < 0)
1227     {
1228       char buf[255];
1229       sprintf(buf, "%s: could not dup() the %s fd", blurb(), PROC_INTERRUPTS);
1230       perror (buf);
1231       goto FAIL;
1232     }
1233
1234   f1 = fdopen (fd, "r");
1235   if (!f1)
1236     {
1237       char buf[255];
1238       sprintf(buf, "%s: could not fdopen() the %s fd", blurb(),
1239               PROC_INTERRUPTS);
1240       perror (buf);
1241       goto FAIL;
1242     }
1243
1244   /* Actually, I'm unclear on why this fseek() is necessary, given the timing
1245      of the dup() above, but it is. */
1246   if (fseek (f1, 0, SEEK_SET) != 0)
1247     {
1248       char buf[255];
1249       sprintf(buf, "%s: error rewinding %s", blurb(), PROC_INTERRUPTS);
1250       perror (buf);
1251       goto FAIL;
1252     }
1253
1254   /* Now read through the pseudo-file until we find the "keyboard",
1255      "PS/2 mouse", or "i8042" lines. */
1256
1257   while (fgets (new_line, sizeof(new_line)-1, f1))
1258     {
1259       Bool i8042_p = !!strstr (new_line, "i8042");
1260       if (i8042_p) i8042_count++;
1261
1262       if (strchr (new_line, ','))
1263         {
1264           /* Ignore any line that has a comma on it: this is because
1265              a setup like this:
1266
1267                  12:     930935          XT-PIC  usb-uhci, PS/2 Mouse
1268
1269              is really bad news.  It *looks* like we can note mouse
1270              activity from that line, but really, that interrupt gets
1271              fired any time any USB device has activity!  So we have
1272              to ignore any shared IRQs.
1273            */
1274         }
1275       else if (!checked_kbd &&
1276                (strstr (new_line, "keyboard") ||
1277                 (i8042_p && i8042_count == 1)))
1278         {
1279           /* Assume the keyboard interrupt is the line that says "keyboard",
1280              or the *first* line that says "i8042".
1281            */
1282           kbd_changed = (*last_kbd_line && !!strcmp (new_line, last_kbd_line));
1283           strcpy (last_kbd_line, new_line);
1284           checked_kbd = True;
1285         }
1286       else if (!checked_ptr &&
1287                (strstr (new_line, "PS/2 Mouse") ||
1288                 (i8042_p && i8042_count == 2)))
1289         {
1290           /* Assume the mouse interrupt is the line that says "PS/2 mouse",
1291              or the *second* line that says "i8042".
1292            */
1293           ptr_changed = (*last_ptr_line && !!strcmp (new_line, last_ptr_line));
1294           strcpy (last_ptr_line, new_line);
1295           checked_ptr = True;
1296         }
1297
1298       if (checked_kbd && checked_ptr)
1299         break;
1300     }
1301
1302   if (checked_kbd || checked_ptr)
1303     {
1304       fclose (f1);
1305
1306       if (si->prefs.debug_p && (kbd_changed || ptr_changed))
1307         fprintf (stderr, "%s: /proc/interrupts activity: %s\n",
1308                  blurb(),
1309                  ((kbd_changed && ptr_changed) ? "mouse and kbd" :
1310                   kbd_changed ? "kbd" :
1311                   ptr_changed ? "mouse" : "ERR"));
1312
1313       return (kbd_changed || ptr_changed);
1314     }
1315
1316
1317   /* If we got here, we didn't find either a "keyboard" or a "PS/2 Mouse"
1318      line in the file at all. */
1319   fprintf (stderr, "%s: no keyboard or mouse data in %s?\n",
1320            blurb(), PROC_INTERRUPTS);
1321
1322  FAIL:
1323   if (f1)
1324     fclose (f1);
1325
1326   if (f0 && f0 != (FILE *) -1)
1327     fclose (f0);
1328
1329   f0 = (FILE *) -1;
1330   return False;
1331 }
1332
1333 #endif /* HAVE_PROC_INTERRUPTS */
1334
1335 \f
1336 /* This timer goes off every few minutes, whether the user is idle or not,
1337    to try and clean up anything that has gone wrong.
1338
1339    It calls disable_builtin_screensaver() so that if xset has been used,
1340    or some other program (like xlock) has messed with the XSetScreenSaver()
1341    settings, they will be set back to sensible values (if a server extension
1342    is in use, messing with xlock can cause xscreensaver to never get a wakeup
1343    event, and could cause monitor power-saving to occur, and all manner of
1344    heinousness.)
1345
1346    If the screen is currently blanked, it raises the window, in case some
1347    other window has been mapped on top of it.
1348
1349    If the screen is currently blanked, and there is no hack running, it
1350    clears the window, in case there is an error message printed on it (we
1351    don't want the error message to burn in.)
1352  */
1353
1354 static void
1355 watchdog_timer (XtPointer closure, XtIntervalId *id)
1356 {
1357   saver_info *si = (saver_info *) closure;
1358   saver_preferences *p = &si->prefs;
1359
1360   disable_builtin_screensaver (si, False);
1361
1362   /* If the DPMS settings on the server have changed, change them back to
1363      what ~/.xscreensaver says they should be. */
1364   sync_server_dpms_settings (si->dpy,
1365                              (p->dpms_enabled_p  &&
1366                               p->mode != DONT_BLANK),
1367                              p->dpms_standby / 1000,
1368                              p->dpms_suspend / 1000,
1369                              p->dpms_off / 1000,
1370                              False);
1371
1372   if (si->screen_blanked_p)
1373     {
1374       Bool running_p = screenhack_running_p (si);
1375
1376       if (si->dbox_up_p)
1377         {
1378           if (si->prefs.debug_p)
1379             fprintf (stderr, "%s: dialog box is up: not raising screen.\n",
1380                      blurb());
1381         }
1382       else
1383         {
1384           if (si->prefs.debug_p)
1385             fprintf (stderr, "%s: watchdog timer raising %sscreen.\n",
1386                      blurb(), (running_p ? "" : "and clearing "));
1387
1388           raise_window (si, True, True, running_p);
1389         }
1390
1391       if (screenhack_running_p (si) &&
1392           !monitor_powered_on_p (si))
1393         {
1394           if (si->prefs.verbose_p)
1395             fprintf (stderr,
1396                      "%s: X says monitor has powered down; "
1397                      "killing running hacks.\n", blurb());
1398           kill_screenhack (si);
1399         }
1400
1401       /* Re-schedule this timer.  The watchdog timer defaults to a bit less
1402          than the hack cycle period, but is never longer than one hour.
1403        */
1404       si->watchdog_id = 0;
1405       reset_watchdog_timer (si, True);
1406     }
1407 }
1408
1409
1410 void
1411 reset_watchdog_timer (saver_info *si, Bool on_p)
1412 {
1413   saver_preferences *p = &si->prefs;
1414
1415   if (si->watchdog_id)
1416     {
1417       XtRemoveTimeOut (si->watchdog_id);
1418       si->watchdog_id = 0;
1419     }
1420
1421   if (on_p && p->watchdog_timeout)
1422     {
1423       si->watchdog_id = XtAppAddTimeOut (si->app, p->watchdog_timeout,
1424                                          watchdog_timer, (XtPointer) si);
1425
1426       if (p->debug_p)
1427         fprintf (stderr, "%s: restarting watchdog_timer (%ld, %ld)\n",
1428                  blurb(), p->watchdog_timeout, si->watchdog_id);
1429     }
1430 }
1431
1432
1433 /* It's possible that a race condition could have led to the saver
1434    window being unexpectedly still mapped.  This can happen like so:
1435
1436     - screen is blanked
1437     - hack is launched
1438     - that hack tries to grab a screen image (it does this by
1439       first unmapping the saver window, then remapping it.)
1440     - hack unmaps window
1441     - hack waits
1442     - user becomes active
1443     - hack re-maps window (*)
1444     - driver kills subprocess
1445     - driver unmaps window (**)
1446
1447    The race is that (*) might have been sent to the server before
1448    the client process was killed, but, due to scheduling randomness,
1449    might not have been received by the server until after (**).
1450    In other words, (*) and (**) might happen out of order, meaning
1451    the driver will unmap the window, and then after that, the
1452    recently-dead client will re-map it.  This leaves the user
1453    locked out (it looks like a desktop, but it's not!)
1454
1455    To avoid this: after un-blanking the screen, we launch a timer
1456    that wakes up once a second for ten seconds, and makes damned
1457    sure that the window is still unmapped.
1458  */
1459
1460 void
1461 de_race_timer (XtPointer closure, XtIntervalId *id)
1462 {
1463   saver_info *si = (saver_info *) closure;
1464   saver_preferences *p = &si->prefs;
1465   int secs = 1;
1466
1467   if (id == 0)  /* if id is 0, this is the initialization call. */
1468     {
1469       si->de_race_ticks = 10;
1470       if (p->verbose_p)
1471         fprintf (stderr, "%s: starting de-race timer (%d seconds.)\n",
1472                  blurb(), si->de_race_ticks);
1473     }
1474   else
1475     {
1476       int i;
1477       XSync (si->dpy, False);
1478       for (i = 0; i < si->nscreens; i++)
1479         {
1480           saver_screen_info *ssi = &si->screens[i];
1481           Window w = ssi->screensaver_window;
1482           XWindowAttributes xgwa;
1483           XGetWindowAttributes (si->dpy, w, &xgwa);
1484           if (xgwa.map_state != IsUnmapped)
1485             {
1486               if (p->verbose_p)
1487                 fprintf (stderr,
1488                          "%s: %d: client race! emergency unmap 0x%lx.\n",
1489                          blurb(), i, (unsigned long) w);
1490               XUnmapWindow (si->dpy, w);
1491             }
1492           else if (p->debug_p)
1493             fprintf (stderr, "%s: %d: (de-race of 0x%lx is cool.)\n",
1494                      blurb(), i, (unsigned long) w);
1495         }
1496       XSync (si->dpy, False);
1497
1498       si->de_race_ticks--;
1499     }
1500
1501   if (id && *id == si->de_race_id)
1502     si->de_race_id = 0;
1503
1504   if (si->de_race_id) abort();
1505
1506   if (si->de_race_ticks <= 0)
1507     {
1508       si->de_race_id = 0;
1509       if (p->verbose_p)
1510         fprintf (stderr, "%s: de-race completed.\n", blurb());
1511     }
1512   else
1513     {
1514       si->de_race_id = XtAppAddTimeOut (si->app, secs * 1000,
1515                                         de_race_timer, closure);
1516     }
1517 }