From http://www.jwz.org/xscreensaver/xscreensaver-5.27.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-2013 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       int i;
268       maybe_reload_init_file (si);
269       for (i = 0; i < si->nscreens; i++)
270         kill_screenhack (&si->screens[i]);
271
272       raise_window (si, True, True, False);
273
274       if (!si->throttled_p)
275         for (i = 0; i < si->nscreens; i++)
276           spawn_screenhack (&si->screens[i]);
277       else
278         {
279           if (p->verbose_p)
280             fprintf (stderr, "%s: not launching new hack (throttled.)\n",
281                      blurb());
282         }
283     }
284
285   if (how_long > 0)
286     {
287       si->cycle_id = XtAppAddTimeOut (si->app, how_long, cycle_timer,
288                                       (XtPointer) si);
289
290       if (p->debug_p)
291         fprintf (stderr, "%s: starting cycle_timer (%ld, %ld)\n",
292                  blurb(), how_long, si->cycle_id);
293     }
294   else
295     {
296       if (p->debug_p)
297         fprintf (stderr, "%s: not starting cycle_timer: how_long == %ld\n",
298                  blurb(), (unsigned long) how_long);
299     }
300 }
301
302
303 void
304 activate_lock_timer (XtPointer closure, XtIntervalId *id)
305 {
306   saver_info *si = (saver_info *) closure;
307   saver_preferences *p = &si->prefs;
308
309   if (p->verbose_p)
310     fprintf (stderr, "%s: timed out; activating lock.\n", blurb());
311   set_locked_p (si, True);
312 }
313
314
315 /* Call this when user activity (or "simulated" activity) has been noticed.
316  */
317 void
318 reset_timers (saver_info *si)
319 {
320   saver_preferences *p = &si->prefs;
321   if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
322     return;
323
324   if (si->timer_id)
325     {
326       if (p->debug_p)
327         fprintf (stderr, "%s: killing idle_timer  (%ld, %ld)\n",
328                  blurb(), p->timeout, si->timer_id);
329       XtRemoveTimeOut (si->timer_id);
330       si->timer_id = 0;
331     }
332
333   schedule_wakeup_event (si, p->timeout, p->debug_p); /* sets si->timer_id */
334
335   if (si->cycle_id) abort ();   /* no cycle timer when inactive */
336
337   si->last_activity_time = time ((time_t *) 0);
338
339   /* This will (hopefully, supposedly) tell the server to re-set its
340      DPMS timer.  Without this, the -deactivate clientmessage would
341      prevent xscreensaver from blanking, but would not prevent the
342      monitor from powering down. */
343 #if 0
344   /* #### With some servers, this causes the screen to flicker every
345      time a key is pressed!  Ok, I surrender.  I give up on ever
346      having DPMS work properly.
347    */
348   XForceScreenSaver (si->dpy, ScreenSaverReset);
349
350   /* And if the monitor is already powered off, turn it on.
351      You'd think the above would do that, but apparently not? */
352   monitor_power_on (si, True);
353 #endif
354
355 }
356
357
358 /* Returns true if a mouse has moved since the last time we checked.
359    Small motions (of less than "hysteresis" pixels/second) are ignored.
360  */
361 static Bool
362 device_pointer_moved_p (saver_info *si, poll_mouse_data *last_poll_mouse,
363                         poll_mouse_data *this_poll_mouse, Bool mods_p,
364                         const char *debug_type, int debug_id)
365 {
366   saver_preferences *p = &si->prefs;
367
368   unsigned int distance, dps;
369   unsigned long seconds = 0;
370   Bool moved_p = False;
371
372   distance = MAX (ABS (last_poll_mouse->root_x - this_poll_mouse->root_x),
373                   ABS (last_poll_mouse->root_y - this_poll_mouse->root_y));
374   seconds = (this_poll_mouse->time - last_poll_mouse->time);
375
376
377   /* When the screen is blanked, we get MotionNotify events, but when not
378      blanked, we poll only every 5 seconds, and that's not enough resolution
379      to do hysteresis based on a 1 second interval.  So, assume that any
380      motion we've seen during the 5 seconds when our eyes were closed happened
381      in the last 1 second instead.
382    */
383   if (seconds > 1) seconds = 1;
384
385   dps = (seconds <= 0 ? distance : (distance / seconds));
386
387   /* Motion only counts if the rate is more than N pixels per second.
388    */
389   if (dps >= p->pointer_hysteresis &&
390       distance > 0)
391     moved_p = True;
392
393   /* If the mouse is not on this screen but used to be, that's motion.
394      If the mouse was not on this screen, but is now, that's motion.
395    */
396   {
397     Bool on_screen_p  = (this_poll_mouse->root_x != -1 &&
398                          this_poll_mouse->root_y != -1);
399     Bool was_on_screen_p = (last_poll_mouse->root_x != -1 &&
400                             last_poll_mouse->root_y != -1);
401
402     if (on_screen_p != was_on_screen_p)
403       moved_p = True;
404   }
405
406   if (p->debug_p && (distance != 0 || moved_p))
407     {
408       fprintf (stderr, "%s: %s %d: pointer %s", blurb(), debug_type, debug_id,
409                (moved_p ? "moved:  " : "ignored:"));
410       if (last_poll_mouse->root_x == -1)
411         fprintf (stderr, "off screen");
412       else
413         fprintf (stderr, "%d,%d",
414                  last_poll_mouse->root_x,
415                  last_poll_mouse->root_y);
416       fprintf (stderr, " -> ");
417       if (this_poll_mouse->root_x == -1)
418         fprintf (stderr, "off screen");
419       else
420         fprintf (stderr, "%d,%d", this_poll_mouse->root_x,
421                  this_poll_mouse->root_y);
422       if (last_poll_mouse->root_x != -1 && this_poll_mouse->root_x != -1)
423         fprintf (stderr, " (%d,%d; %d/%lu=%d)",
424                  ABS(last_poll_mouse->root_x - this_poll_mouse->root_x),
425                  ABS(last_poll_mouse->root_y - this_poll_mouse->root_y),
426                  distance, seconds, dps);
427
428       fprintf (stderr, ".\n");
429     }
430
431   if (!moved_p &&
432       mods_p &&
433       this_poll_mouse->mask != last_poll_mouse->mask)
434     {
435       moved_p = True;
436
437       if (p->debug_p)
438         fprintf (stderr, "%s: %s %d: modifiers changed: 0x%04x -> 0x%04x.\n",
439                  blurb(), debug_type, debug_id,
440                  last_poll_mouse->mask, this_poll_mouse->mask);
441     }
442
443   last_poll_mouse->child = this_poll_mouse->child;
444   last_poll_mouse->mask  = this_poll_mouse->mask;
445
446   if (moved_p || seconds > 0)
447     {
448       last_poll_mouse->time   = this_poll_mouse->time;
449       last_poll_mouse->root_x = this_poll_mouse->root_x;
450       last_poll_mouse->root_y = this_poll_mouse->root_y;
451     }
452
453   return moved_p;
454 }
455
456 /* Returns true if core mouse pointer has moved since the last time we checked.
457  */
458 static Bool
459 pointer_moved_p (saver_screen_info *ssi, Bool mods_p)
460 {
461   saver_info *si = ssi->global;
462
463   Window root;
464   poll_mouse_data this_poll_mouse;
465   int x, y;
466
467   /* don't check xinerama pseudo-screens. */
468   if (!ssi->real_screen_p) return False;
469
470   this_poll_mouse.time = time ((time_t *) 0);
471   
472   if (!XQueryPointer (si->dpy, ssi->screensaver_window, &root,
473                       &this_poll_mouse.child,
474                       &this_poll_mouse.root_x, &this_poll_mouse.root_y,
475                       &x, &y, &this_poll_mouse.mask))
476     {
477       /* If XQueryPointer() returns false, the mouse is not on this screen.
478        */
479       this_poll_mouse.root_x = -1;
480       this_poll_mouse.root_y = -1;
481       this_poll_mouse.child = 0;
482       this_poll_mouse.mask = 0;
483     }
484   else
485     si->last_activity_screen = ssi;
486
487   return device_pointer_moved_p(si, &(ssi->last_poll_mouse), &this_poll_mouse,
488                                 mods_p, "screen", ssi->number);
489 }
490
491
492 /* When we aren't using a server extension, this timer is used to periodically
493    wake up and poll the mouse position, which is possibly more reliable than
494    selecting motion events on every window.
495  */
496 static void
497 check_pointer_timer (XtPointer closure, XtIntervalId *id)
498 {
499   int i;
500   saver_info *si = (saver_info *) closure;
501   saver_preferences *p = &si->prefs;
502   Bool active_p = False;
503
504   if (!si->using_proc_interrupts &&
505       (si->using_xidle_extension ||
506        si->using_mit_saver_extension ||
507        si->using_sgi_saver_extension))
508     /* If an extension is in use, we should not be polling the mouse.
509        Unless we're also checking /proc/interrupts, in which case, we should.
510      */
511     abort ();
512
513   if (id && *id == si->check_pointer_timer_id)  /* this is us - it's expired */
514     si->check_pointer_timer_id = 0;
515
516   if (si->check_pointer_timer_id)               /* only queue one at a time */
517     XtRemoveTimeOut (si->check_pointer_timer_id);
518
519   si->check_pointer_timer_id =                  /* now re-queue */
520     XtAppAddTimeOut (si->app, p->pointer_timeout, check_pointer_timer,
521                      (XtPointer) si);
522
523   for (i = 0; i < si->nscreens; i++)
524     {
525       saver_screen_info *ssi = &si->screens[i];
526       if (pointer_moved_p (ssi, True))
527         active_p = True;
528     }
529
530 #ifdef HAVE_PROC_INTERRUPTS
531   if (!active_p &&
532       si->using_proc_interrupts &&
533       proc_interrupts_activity_p (si))
534     {
535       active_p = True;
536     }
537 #endif /* HAVE_PROC_INTERRUPTS */
538
539   if (active_p)
540     reset_timers (si);
541
542   check_for_clock_skew (si);
543 }
544
545
546 /* An unfortunate situation is this: the saver is not active, because the
547    user has been typing.  The machine is a laptop.  The user closes the lid
548    and suspends it.  The CPU halts.  Some hours later, the user opens the
549    lid.  At this point, Xt's timers will fire, and xscreensaver will blank
550    the screen.
551
552    So far so good -- well, not really, but it's the best that we can do,
553    since the OS doesn't send us a signal *before* shutdown -- but if the
554    user had delayed locking (lockTimeout > 0) then we should start off
555    in the locked state, rather than only locking N minutes from when the
556    lid was opened.  Also, eschewing fading is probably a good idea, to
557    clamp down as soon as possible.
558
559    We only do this when we'd be polling the mouse position anyway.
560    This amounts to an assumption that machines with APM support also
561    have /proc/interrupts.
562  */
563 static void
564 check_for_clock_skew (saver_info *si)
565 {
566   saver_preferences *p = &si->prefs;
567   time_t now = time ((time_t *) 0);
568   long shift = now - si->last_wall_clock_time;
569
570   if (p->debug_p)
571     {
572       int i = (si->last_wall_clock_time == 0 ? 0 : shift);
573       fprintf (stderr,
574                "%s: checking wall clock for hibernation (%d:%02d:%02d).\n",
575                blurb(),
576                (i / (60 * 60)), ((i / 60) % 60), (i % 60));
577     }
578
579   if (si->last_wall_clock_time != 0 &&
580       shift > (p->timeout / 1000))
581     {
582       if (p->verbose_p)
583         fprintf (stderr, "%s: wall clock has jumped by %ld:%02ld:%02ld%s\n",
584                  blurb(),
585                  (shift / (60 * 60)), ((shift / 60) % 60), (shift % 60),
586                  (p->mode == DONT_BLANK ? " while saver disabled" : ""));
587
588       /* If the saver is entirely disabled, there's no need to do the
589          emergency-blank-and-lock thing.
590        */
591       if (p->mode != DONT_BLANK)
592         {
593           si->emergency_lock_p = True;
594           idle_timer ((XtPointer) si, 0);
595         }
596     }
597
598   si->last_wall_clock_time = now;
599 }
600
601
602
603 static void
604 dispatch_event (saver_info *si, XEvent *event)
605 {
606   /* If this is for the splash dialog, pass it along.
607      Note that the password dialog is handled with its own event loop,
608      so events for that window will never come through here.
609    */
610   if (si->splash_dialog && event->xany.window == si->splash_dialog)
611     handle_splash_event (si, event);
612
613   XtDispatchEvent (event);
614 }
615
616
617 static void
618 swallow_unlock_typeahead_events (saver_info *si, XEvent *e)
619 {
620   XEvent event;
621   char buf [100];
622   int i = 0;
623
624   memset (buf, 0, sizeof(buf));
625
626   event = *e;
627
628   do
629     {
630       if (event.xany.type == KeyPress)
631         {
632           char s[2];
633           int size = XLookupString ((XKeyEvent *) &event, s, 1, 0, 0);
634           if (size != 1) continue;
635           switch (*s)
636             {
637             case '\010': case '\177':                   /* Backspace */
638               if (i > 0) i--;
639               break;
640             case '\025': case '\030':                   /* Erase line */
641             case '\012': case '\015':                   /* Enter */
642             case '\033':                                /* ESC */
643               i = 0;
644               break;
645             case '\040':                                /* Space */
646               if (i == 0)
647                 break;  /* ignore space at beginning of line */
648               /* else, fall through */
649             default:
650               buf [i++] = *s;
651               break;
652             }
653         }
654
655     } while (i < sizeof(buf)-1 &&
656              XCheckMaskEvent (si->dpy, KeyPressMask, &event));
657
658   buf[i] = 0;
659
660   if (si->unlock_typeahead)
661     {
662       memset (si->unlock_typeahead, 0, strlen(si->unlock_typeahead));
663       free (si->unlock_typeahead);
664     }
665
666   if (i > 0)
667     si->unlock_typeahead = strdup (buf);
668   else
669     si->unlock_typeahead = 0;
670
671   memset (buf, 0, sizeof(buf));
672 }
673
674
675 /* methods of detecting idleness:
676
677       explicitly informed by SGI SCREEN_SAVER server event;
678       explicitly informed by MIT-SCREEN-SAVER server event;
679       poll server idle time with XIDLE extension;
680       select events on all windows, and note absence of recent events;
681       note that /proc/interrupts has not changed in a while;
682       activated by clientmessage.
683
684    methods of detecting non-idleness:
685
686       read events on the xscreensaver window;
687       explicitly informed by SGI SCREEN_SAVER server event;
688       explicitly informed by MIT-SCREEN-SAVER server event;
689       select events on all windows, and note events on any of them;
690       note that /proc/interrupts has changed;
691       deactivated by clientmessage.
692
693    I trust that explains why this function is a big hairy mess.
694  */
695 void
696 sleep_until_idle (saver_info *si, Bool until_idle_p)
697 {
698   saver_preferences *p = &si->prefs;
699
700   /* We have to go through this union bullshit because gcc-4.4.0 has
701      stricter struct-aliasing rules.  Without this, the optimizer
702      can fuck things up.
703    */
704   union {
705     XEvent x_event;
706 # ifdef HAVE_RANDR
707     XRRScreenChangeNotifyEvent xrr_event;
708 # endif /* HAVE_RANDR */
709 # ifdef HAVE_MIT_SAVER_EXTENSION
710     XScreenSaverNotifyEvent sevent;
711 # endif /* HAVE_MIT_SAVER_EXTENSION */
712   } event;
713
714   /* We need to select events on all windows if we're not using any extensions.
715      Otherwise, we don't need to. */
716   Bool scanning_all_windows = !(si->using_xidle_extension ||
717                                 si->using_mit_saver_extension ||
718                                 si->using_sgi_saver_extension);
719
720   /* We need to periodically wake up and check for idleness if we're not using
721      any extensions, or if we're using the XIDLE extension.  The other two
722      extensions explicitly deliver events when we go idle/non-idle, so we
723      don't need to poll. */
724   Bool polling_for_idleness = !(si->using_mit_saver_extension ||
725                                 si->using_sgi_saver_extension);
726
727   /* Whether we need to periodically wake up and check to see if the mouse has
728      moved.  We only need to do this when not using any extensions.  The reason
729      this isn't the same as `polling_for_idleness' is that the "idleness" poll
730      can happen (for example) 5 minutes from now, whereas the mouse-position
731      poll should happen with low periodicity.  We don't need to poll the mouse
732      position with the XIDLE extension, but we do need to periodically wake up
733      and query the server with that extension.  For our purposes, polling
734      /proc/interrupts is just like polling the mouse position.  It has to
735      happen on the same kind of schedule. */
736   Bool polling_mouse_position = (si->using_proc_interrupts ||
737                                  !(si->using_xidle_extension ||
738                                    si->using_mit_saver_extension ||
739                                    si->using_sgi_saver_extension) ||
740                                    si->using_xinput_extension);
741
742   const char *why = 0;  /* What caused the idle-state to change? */
743
744   if (until_idle_p)
745     {
746       if (polling_for_idleness)
747         /* This causes a no-op event to be delivered to us in a while, so that
748            we come back around through the event loop again.  */
749         schedule_wakeup_event (si, p->timeout, p->debug_p);
750
751       if (polling_mouse_position)
752         /* Check to see if the mouse has moved, and set up a repeating timer
753            to do so periodically (typically, every 5 seconds.) */
754         check_pointer_timer ((XtPointer) si, 0);
755     }
756
757   while (1)
758     {
759       XtAppNextEvent (si->app, &event.x_event);
760
761       switch (event.x_event.xany.type) {
762       case 0:           /* our synthetic "timeout" event has been signalled */
763         if (until_idle_p)
764           {
765             Time idle;
766
767             /* We may be idle; check one last time to see if the mouse has
768                moved, just in case the idle-timer went off within the 5 second
769                window between mouse polling.  If the mouse has moved, then
770                check_pointer_timer() will reset last_activity_time.
771              */
772             if (polling_mouse_position)
773               check_pointer_timer ((XtPointer) si, 0);
774
775 #ifdef HAVE_XIDLE_EXTENSION
776             if (si->using_xidle_extension)
777               {
778                 /* The XIDLE extension uses the synthetic event to prod us into
779                    re-asking the server how long the user has been idle. */
780                 if (! XGetIdleTime (si->dpy, &idle))
781                   {
782                     fprintf (stderr, "%s: XGetIdleTime() failed.\n", blurb());
783                     saver_exit (si, 1, 0);
784                   }
785               }
786             else
787 #endif /* HAVE_XIDLE_EXTENSION */
788 #ifdef HAVE_MIT_SAVER_EXTENSION
789               if (si->using_mit_saver_extension)
790                 {
791                   /* We don't need to do anything in this case - the synthetic
792                      event isn't necessary, as we get sent specific events
793                      to wake us up.  In fact, this event generally shouldn't
794                      be being delivered when the MIT extension is in use. */
795                   idle = 0;
796                 }
797             else
798 #endif /* HAVE_MIT_SAVER_EXTENSION */
799 #ifdef HAVE_SGI_SAVER_EXTENSION
800               if (si->using_sgi_saver_extension)
801                 {
802                   /* We don't need to do anything in this case - the synthetic
803                      event isn't necessary, as we get sent specific events
804                      to wake us up.  In fact, this event generally shouldn't
805                      be being delivered when the SGI extension is in use. */
806                   idle = 0;
807                 }
808             else
809 #endif /* HAVE_SGI_SAVER_EXTENSION */
810               {
811                 /* Otherwise, no server extension is in use.  The synthetic
812                    event was to tell us to wake up and see if the user is now
813                    idle.  Compute the amount of idle time by comparing the
814                    `last_activity_time' to the wall clock.  The l_a_t was set
815                    by calling `reset_timers()', which is called only in only
816                    two situations: when polling the mouse position has revealed
817                    the the mouse has moved (user activity) or when we have read
818                    an event (again, user activity.)
819                  */
820                 idle = 1000 * (si->last_activity_time - time ((time_t *) 0));
821               }
822
823             if (idle >= p->timeout)
824               {
825                 /* Look, we've been idle long enough.  We're done. */
826                 why = "timeout";
827                 goto DONE;
828               }
829             else if (si->emergency_lock_p)
830               {
831                 /* Oops, the wall clock has jumped far into the future, so
832                    we need to lock down in a hurry! */
833                 why = "large wall clock change";
834                 goto DONE;
835               }
836             else
837               {
838                 /* The event went off, but it turns out that the user has not
839                    yet been idle for long enough.  So re-signal the event.
840                    Be economical: if we should blank after 5 minutes, and the
841                    user has been idle for 2 minutes, then set this timer to
842                    go off in 3 minutes.
843                  */
844                 if (polling_for_idleness)
845                   schedule_wakeup_event (si, p->timeout - idle, p->debug_p);
846               }
847           }
848         break;
849
850       case ClientMessage:
851         if (handle_clientmessage (si, &event.x_event, until_idle_p))
852           {
853             why = "ClientMessage";
854             goto DONE;
855           }
856         break;
857
858       case CreateNotify:
859         /* A window has been created on the screen somewhere.  If we're
860            supposed to scan all windows for events, prepare this window. */
861         if (scanning_all_windows)
862           {
863             Window w = event.x_event.xcreatewindow.window;
864             start_notice_events_timer (si, w, p->debug_p);
865           }
866         break;
867
868       case KeyPress:
869       case ButtonPress:
870       /* Ignore release events so that hitting ESC at the password dialog
871          doesn't result in the password dialog coming right back again when
872          the fucking release key is seen! */
873       /* case KeyRelease:*/
874       /* case ButtonRelease:*/
875       case MotionNotify:
876
877         if (p->debug_p)
878           {
879             Window root=0, window=0;
880             int x=-1, y=-1;
881             const char *type = 0;
882             if (event.x_event.xany.type == MotionNotify)
883               {
884                 /*type = "MotionNotify";*/
885                 root = event.x_event.xmotion.root;
886                 window = event.x_event.xmotion.window;
887                 x = event.x_event.xmotion.x_root;
888                 y = event.x_event.xmotion.y_root;
889               }
890             else if (event.x_event.xany.type == KeyPress)
891               {
892                 type = "KeyPress";
893                 root = event.x_event.xkey.root;
894                 window = event.x_event.xkey.window;
895                 x = y = -1;
896               }
897             else if (event.x_event.xany.type == ButtonPress)
898               {
899                 type = "ButtonPress";
900                 root = event.x_event.xkey.root;
901                 window = event.x_event.xkey.window;
902                 x = event.x_event.xmotion.x_root;
903                 y = event.x_event.xmotion.y_root;
904               }
905
906             if (type)
907               {
908                 int i;
909                 for (i = 0; i < si->nscreens; i++)
910                   if (root == RootWindowOfScreen (si->screens[i].screen))
911                     break;
912                 fprintf (stderr,"%s: %d: %s on 0x%lx",
913                          blurb(), i, type, (unsigned long) window);
914
915                 /* Be careful never to do this unless in -debug mode, as
916                    this could expose characters from the unlock password. */
917                 if (p->debug_p && event.x_event.xany.type == KeyPress)
918                   {
919                     KeySym keysym;
920                     char c = 0;
921                     XLookupString (&event.x_event.xkey, &c, 1, &keysym, 0);
922                     fprintf (stderr, " (%s%s)",
923                              (event.x_event.xkey.send_event ? "synthetic " : ""),
924                              XKeysymToString (keysym));
925                   }
926
927                 if (x == -1)
928                   fprintf (stderr, "\n");
929                 else
930                   fprintf (stderr, " at %d,%d.\n", x, y);
931               }
932           }
933
934         /* If any widgets want to handle this event, let them. */
935         dispatch_event (si, &event.x_event);
936
937         
938         /* If we got a MotionNotify event, figure out what screen it
939            was on and poll the mouse there: if the mouse hasn't moved
940            far enough to count as "real" motion, then ignore this
941            event.
942          */
943         if (event.x_event.xany.type == MotionNotify)
944           {
945             int i;
946             for (i = 0; i < si->nscreens; i++)
947               if (event.x_event.xmotion.root ==
948                   RootWindowOfScreen (si->screens[i].screen))
949                 break;
950             if (i < si->nscreens)
951               {
952                 if (!pointer_moved_p (&si->screens[i], False))
953                   continue;
954               }
955           }
956
957
958         /* We got a user event.
959            If we're waiting for the user to become active, this is it.
960            If we're waiting until the user becomes idle, reset the timers
961            (since now we have longer to wait.)
962          */
963         if (!until_idle_p)
964           {
965             if (si->demoing_p &&
966                 (event.x_event.xany.type == MotionNotify ||
967                  event.x_event.xany.type == KeyRelease))
968               /* When we're demoing a single hack, mouse motion doesn't
969                  cause deactivation.  Only clicks and keypresses do. */
970               ;
971             else
972               {
973                 /* If we're not demoing, then any activity causes deactivation.
974                  */
975                 why = (event.x_event.xany.type == MotionNotify ?"mouse motion":
976                        event.x_event.xany.type == KeyPress?"keyboard activity":
977                        event.x_event.xany.type == ButtonPress ? "mouse click" :
978                        "unknown user activity");
979                 goto DONE;
980               }
981           }
982         else
983           reset_timers (si);
984
985         break;
986
987       default:
988
989 #ifdef HAVE_MIT_SAVER_EXTENSION
990         if (event.x_event.type == si->mit_saver_ext_event_number)
991           {
992             /* This event's number is that of the MIT-SCREEN-SAVER server
993                extension.  This extension has one event number, and the event
994                itself contains sub-codes that say what kind of event it was
995                (an "idle" or "not-idle" event.)
996              */
997             if (event.sevent.state == ScreenSaverOn)
998               {
999                 int i = 0;
1000                 if (p->verbose_p)
1001                   fprintf (stderr, "%s: MIT ScreenSaverOn event received.\n",
1002                            blurb());
1003
1004                 /* Get the "real" server window(s) out of the way as soon
1005                    as possible. */
1006                 for (i = 0; i < si->nscreens; i++)
1007                   {
1008                     saver_screen_info *ssi = &si->screens[i];
1009                     if (ssi->server_mit_saver_window &&
1010                         window_exists_p (si->dpy,
1011                                          ssi->server_mit_saver_window))
1012                       XUnmapWindow (si->dpy, ssi->server_mit_saver_window);
1013                   }
1014
1015                 if (event.sevent.kind != ScreenSaverExternal)
1016                   {
1017                     fprintf (stderr,
1018                          "%s: ScreenSaverOn event wasn't of type External!\n",
1019                              blurb());
1020                   }
1021
1022                 if (until_idle_p)
1023                   {
1024                     why = "MIT ScreenSaverOn";
1025                     goto DONE;
1026                   }
1027               }
1028             else if (event.sevent.state == ScreenSaverOff)
1029               {
1030                 if (p->verbose_p)
1031                   fprintf (stderr, "%s: MIT ScreenSaverOff event received.\n",
1032                            blurb());
1033                 if (!until_idle_p)
1034                   {
1035                     why = "MIT ScreenSaverOff";
1036                     goto DONE;
1037                   }
1038               }
1039             else
1040               fprintf (stderr,
1041                        "%s: unknown MIT-SCREEN-SAVER event %d received!\n",
1042                        blurb(), event.sevent.state);
1043           }
1044         else
1045
1046 #endif /* HAVE_MIT_SAVER_EXTENSION */
1047
1048
1049 #ifdef HAVE_SGI_SAVER_EXTENSION
1050         if (event.x_event.type == (si->sgi_saver_ext_event_number + ScreenSaverStart))
1051           {
1052             /* The SGI SCREEN_SAVER server extension has two event numbers,
1053                and this event matches the "idle" event. */
1054             if (p->verbose_p)
1055               fprintf (stderr, "%s: SGI ScreenSaverStart event received.\n",
1056                        blurb());
1057
1058             if (until_idle_p)
1059               {
1060                 why = "SGI ScreenSaverStart";
1061                 goto DONE;
1062               }
1063           }
1064         else if (event.x_event.type == (si->sgi_saver_ext_event_number +
1065                                 ScreenSaverEnd))
1066           {
1067             /* The SGI SCREEN_SAVER server extension has two event numbers,
1068                and this event matches the "idle" event. */
1069             if (p->verbose_p)
1070               fprintf (stderr, "%s: SGI ScreenSaverEnd event received.\n",
1071                        blurb());
1072             if (!until_idle_p)
1073               {
1074                 why = "SGI ScreenSaverEnd";
1075                 goto DONE;
1076               }
1077           }
1078         else
1079 #endif /* HAVE_SGI_SAVER_EXTENSION */
1080
1081 #ifdef HAVE_XINPUT
1082         /* If we got a MotionNotify event, check to see if the mouse has
1083            moved far enough to count as "real" motion, if not, then ignore
1084            this event.
1085          */
1086         if ((si->num_xinput_devices > 0) &&
1087             (event.x_event.type == si->xinput_DeviceMotionNotify))
1088           {
1089             XDeviceMotionEvent *dme = (XDeviceMotionEvent *) &event;
1090             poll_mouse_data *last_poll_mouse = NULL;
1091             int d;
1092
1093             for (d = 0; d < si->num_xinput_devices; d++)
1094               {
1095                 if (si->xinput_devices[d].device->device_id == dme->deviceid)
1096                   {
1097                     last_poll_mouse = &(si->xinput_devices[d].last_poll_mouse);
1098                     break;
1099                   }
1100               }
1101
1102             if (last_poll_mouse)
1103               {
1104                 poll_mouse_data this_poll_mouse;
1105                 this_poll_mouse.root_x = dme->x_root;
1106                 this_poll_mouse.root_y = dme->y_root;
1107                 this_poll_mouse.child = dme->subwindow;
1108                 this_poll_mouse.mask = dme->device_state;
1109                 this_poll_mouse.time = dme->time / 1000; /* milliseconds */
1110
1111                 if (!device_pointer_moved_p (si, last_poll_mouse,
1112                                              &this_poll_mouse, False,
1113                                              "device", dme->deviceid))
1114                   continue;
1115               }
1116             else if (p->debug_p)
1117               fprintf (stderr,
1118                        "%s: received MotionNotify from unknown device %d\n",
1119                        blurb(), (int) dme->deviceid);
1120           }
1121         
1122         if ((!until_idle_p) &&
1123             (si->num_xinput_devices > 0) &&
1124             (event.x_event.type == si->xinput_DeviceMotionNotify ||
1125              event.x_event.type == si->xinput_DeviceButtonPress))
1126           /* Ignore DeviceButtonRelease, see ButtonRelease comment above. */
1127           {
1128
1129             dispatch_event (si, &event.x_event);
1130             if (si->demoing_p &&
1131                 event.x_event.type == si->xinput_DeviceMotionNotify)
1132               /* When we're demoing a single hack, mouse motion doesn't
1133                  cause deactivation.  Only clicks and keypresses do. */
1134               ;
1135             else
1136               /* If we're not demoing, then any activity causes deactivation.
1137                */
1138               {
1139                 why = (event.x_event.type == si->xinput_DeviceMotionNotify
1140                        ? "XI mouse motion" :
1141                        event.x_event.type == si->xinput_DeviceButtonPress
1142                        ? "XI mouse click" : "unknown XINPUT event");
1143                 goto DONE;
1144               }
1145           }
1146         else
1147 #endif /* HAVE_XINPUT */
1148
1149 #ifdef HAVE_RANDR
1150         if (si->using_randr_extension &&
1151             (event.x_event.type == 
1152              (si->randr_event_number + RRScreenChangeNotify)))
1153           {
1154             /* The Resize and Rotate extension sends an event when the
1155                size, rotation, or refresh rate of any screen has changed.
1156              */
1157             if (p->verbose_p)
1158               {
1159                 /* XRRRootToScreen is in Xrandr.h 1.4, 2001/06/07 */
1160                 int screen = XRRRootToScreen (si->dpy, event.xrr_event.window);
1161                 fprintf (stderr, "%s: %d: screen change event received\n",
1162                          blurb(), screen);
1163               }
1164
1165 # ifdef RRScreenChangeNotifyMask
1166             /* Inform Xlib that it's ok to update its data structures. */
1167             XRRUpdateConfiguration (&event.x_event); /* Xrandr.h 1.9, 2002/09/29 */
1168 # endif /* RRScreenChangeNotifyMask */
1169
1170             /* Resize the existing xscreensaver windows and cached ssi data. */
1171             if (update_screen_layout (si))
1172               {
1173                 if (p->verbose_p)
1174                   {
1175                     fprintf (stderr, "%s: new layout:\n", blurb());
1176                     describe_monitor_layout (si);
1177                   }
1178                 resize_screensaver_window (si);
1179               }
1180           }
1181         else
1182 #endif /* HAVE_RANDR */
1183
1184           /* Just some random event.  Let the Widgets handle it, if desired. */
1185           dispatch_event (si, &event.x_event);
1186       }
1187     }
1188  DONE:
1189
1190   if (p->verbose_p)
1191     {
1192       if (! why) why = "unknown reason";
1193       fprintf (stderr, "%s: %s (%s)\n", blurb(),
1194                (until_idle_p ? "user is idle" : "user is active"),
1195                why);
1196     }
1197
1198   /* If there's a user event on the queue, swallow it.
1199      If we're using a server extension, and the user becomes active, we
1200      get the extension event before the user event -- so the keypress or
1201      motion or whatever is still on the queue.  This makes "unfade" not
1202      work, because it sees that event, and bugs out.  (This problem
1203      doesn't exhibit itself without an extension, because in that case,
1204      there's only one event generated by user activity, not two.)
1205    */
1206   if (!until_idle_p && si->locked_p)
1207     swallow_unlock_typeahead_events (si, &event.x_event);
1208   else
1209     while (XCheckMaskEvent (si->dpy,
1210                             (KeyPressMask|ButtonPressMask|PointerMotionMask),
1211                      &event.x_event))
1212       ;
1213
1214
1215   if (si->check_pointer_timer_id)
1216     {
1217       XtRemoveTimeOut (si->check_pointer_timer_id);
1218       si->check_pointer_timer_id = 0;
1219     }
1220   if (si->timer_id)
1221     {
1222       XtRemoveTimeOut (si->timer_id);
1223       si->timer_id = 0;
1224     }
1225
1226   if (until_idle_p && si->cycle_id)     /* no cycle timer when inactive */
1227     abort ();
1228 }
1229
1230
1231 \f
1232 /* Some crap for dealing with /proc/interrupts.
1233
1234    On Linux systems, it's possible to see the hardware interrupt count
1235    associated with the keyboard.  We can therefore use that as another method
1236    of detecting idleness.
1237
1238    Why is it a good idea to do this?  Because it lets us detect keyboard
1239    activity that is not associated with X events.  For example, if the user
1240    has switched to another virtual console, it's good for xscreensaver to not
1241    be running graphics hacks on the (non-visible) X display.  The common
1242    complaint that checking /proc/interrupts addresses is that the user is
1243    playing Quake on a non-X console, and the GL hacks are perceptibly slowing
1244    the game...
1245
1246    This is tricky for a number of reasons.
1247
1248      * First, we must be sure to only do this when running on an X server that
1249        is on the local machine (because otherwise, we'd be reacting to the
1250        wrong keyboard.)  The way we do this is by noting that the $DISPLAY is
1251        pointing to display 0 on the local machine.  It *could* be that display
1252        1 is also on the local machine (e.g., two X servers, each on a different
1253        virtual-terminal) but it's also possible that screen 1 is an X terminal,
1254        using this machine as the host.  So we can't take that chance.
1255
1256      * Second, one can only access these interrupt numbers in a completely
1257        and utterly brain-damaged way.  You would think that one would use an
1258        ioctl for this.  But no.  The ONLY way to get this information is to
1259        open the pseudo-file /proc/interrupts AS A FILE, and read the numbers
1260        out of it TEXTUALLY.  Because this is Unix, and all the world's a file,
1261        and the only real data type is the short-line sequence of ASCII bytes.
1262
1263        Now it's all well and good that the /proc/interrupts pseudo-file
1264        exists; that's a clever idea, and a useful API for things that are
1265        already textually oriented, like shell scripts, and users doing
1266        interactive debugging sessions.  But to make a *C PROGRAM* open a file
1267        and parse the textual representation of integers out of it is just
1268        insane.
1269
1270      * Third, you can't just hold the file open, and fseek() back to the
1271        beginning to get updated data!  If you do that, the data never changes.
1272        And I don't want to call open() every five seconds, because I don't want
1273        to risk going to disk for any inodes.  It turns out that if you dup()
1274        it early, then each copy gets fresh data, so we can get around that in
1275        this way (but for how many releases, one might wonder?)
1276
1277      * Fourth, the format of the output of the /proc/interrupts file is
1278        undocumented, and has changed several times already!  In Linux 2.0.33,
1279        even on a multiprocessor machine, it looks like this:
1280
1281           0:  309453991   timer
1282           1:    4771729   keyboard
1283    
1284        but in Linux 2.2 and 2.4 kernels with MP machines, it looks like this:
1285
1286                    CPU0       CPU1
1287           0:    1671450    1672618    IO-APIC-edge  timer
1288           1:      13037      13495    IO-APIC-edge  keyboard
1289
1290        and in Linux 2.6, it's gotten even goofier: now there are two lines
1291        labelled "i8042".  One of them is the keyboard, and one of them is
1292        the PS/2 mouse -- and of course, you can't tell them apart, except
1293        by wiggling the mouse and noting which one changes:
1294
1295                    CPU0       CPU1
1296           1:      32051      30864    IO-APIC-edge  i8042
1297          12:     476577     479913    IO-APIC-edge  i8042
1298
1299        Joy!  So how are we expected to parse that?  Well, this code doesn't
1300        parse it: it saves the first line with the string "keyboard" (or
1301        "i8042") in it, and does a string-comparison to note when it has
1302        changed.  If there are two "i8042" lines, we assume the first is
1303        the keyboard and the second is the mouse (doesn't matter which is
1304        which, really, as long as we don't compare them against each other.)
1305
1306    Thanks to Nat Friedman <nat@nat.org> for figuring out most of this crap.
1307
1308    Note that if you have a serial or USB mouse, or a USB keyboard, it won't
1309    detect it.  That's because there's no way to tell the difference between a
1310    serial mouse and a general serial port, and all USB devices look the same
1311    from here.  It would be somewhat unfortunate to have the screensaver turn
1312    off when the modem on COM1 burped, or when a USB disk was accessed.
1313  */
1314
1315
1316 #ifdef HAVE_PROC_INTERRUPTS
1317
1318 #define PROC_INTERRUPTS "/proc/interrupts"
1319
1320 Bool
1321 query_proc_interrupts_available (saver_info *si, const char **why)
1322 {
1323   /* We can use /proc/interrupts if $DISPLAY points to :0, and if the
1324      "/proc/interrupts" file exists and is readable.
1325    */
1326   FILE *f;
1327   if (why) *why = 0;
1328
1329   if (!display_is_on_console_p (si))
1330     {
1331       if (why) *why = "not on primary console";
1332       return False;
1333     }
1334
1335   f = fopen (PROC_INTERRUPTS, "r");
1336   if (!f)
1337     {
1338       if (why) *why = "does not exist";
1339       return False;
1340     }
1341
1342   fclose (f);
1343   return True;
1344 }
1345
1346
1347 static Bool
1348 proc_interrupts_activity_p (saver_info *si)
1349 {
1350   static FILE *f0 = 0;
1351   FILE *f1 = 0;
1352   int fd;
1353   static char last_kbd_line[255] = { 0, };
1354   static char last_ptr_line[255] = { 0, };
1355   char new_line[sizeof(last_kbd_line)];
1356   Bool checked_kbd = False, kbd_changed = False;
1357   Bool checked_ptr = False, ptr_changed = False;
1358   int i8042_count = 0;
1359
1360   if (!f0)
1361     {
1362       /* First time -- open the file. */
1363       f0 = fopen (PROC_INTERRUPTS, "r");
1364       if (!f0)
1365         {
1366           char buf[255];
1367           sprintf(buf, "%s: error opening %s", blurb(), PROC_INTERRUPTS);
1368           perror (buf);
1369           goto FAIL;
1370         }
1371
1372 # if defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
1373       /* Close this fd upon exec instead of inheriting / leaking it. */
1374       if (fcntl (fileno (f0), F_SETFD, FD_CLOEXEC) != 0)
1375         perror ("fcntl: CLOEXEC:");
1376 # endif
1377     }
1378
1379   if (f0 == (FILE *) -1)            /* means we got an error initializing. */
1380     return False;
1381
1382   fd = dup (fileno (f0));
1383   if (fd < 0)
1384     {
1385       char buf[255];
1386       sprintf(buf, "%s: could not dup() the %s fd", blurb(), PROC_INTERRUPTS);
1387       perror (buf);
1388       goto FAIL;
1389     }
1390
1391   f1 = fdopen (fd, "r");
1392   if (!f1)
1393     {
1394       char buf[255];
1395       sprintf(buf, "%s: could not fdopen() the %s fd", blurb(),
1396               PROC_INTERRUPTS);
1397       perror (buf);
1398       goto FAIL;
1399     }
1400
1401   /* Actually, I'm unclear on why this fseek() is necessary, given the timing
1402      of the dup() above, but it is. */
1403   if (fseek (f1, 0, SEEK_SET) != 0)
1404     {
1405       char buf[255];
1406       sprintf(buf, "%s: error rewinding %s", blurb(), PROC_INTERRUPTS);
1407       perror (buf);
1408       goto FAIL;
1409     }
1410
1411   /* Now read through the pseudo-file until we find the "keyboard",
1412      "PS/2 mouse", or "i8042" lines. */
1413
1414   while (fgets (new_line, sizeof(new_line)-1, f1))
1415     {
1416       Bool i8042_p = !!strstr (new_line, "i8042");
1417       if (i8042_p) i8042_count++;
1418
1419       if (strchr (new_line, ','))
1420         {
1421           /* Ignore any line that has a comma on it: this is because
1422              a setup like this:
1423
1424                  12:     930935          XT-PIC  usb-uhci, PS/2 Mouse
1425
1426              is really bad news.  It *looks* like we can note mouse
1427              activity from that line, but really, that interrupt gets
1428              fired any time any USB device has activity!  So we have
1429              to ignore any shared IRQs.
1430            */
1431         }
1432       else if (!checked_kbd &&
1433                (strstr (new_line, "keyboard") ||
1434                 (i8042_p && i8042_count == 1)))
1435         {
1436           /* Assume the keyboard interrupt is the line that says "keyboard",
1437              or the *first* line that says "i8042".
1438            */
1439           kbd_changed = (*last_kbd_line && !!strcmp (new_line, last_kbd_line));
1440           strcpy (last_kbd_line, new_line);
1441           checked_kbd = True;
1442         }
1443       else if (!checked_ptr &&
1444                (strstr (new_line, "PS/2 Mouse") ||
1445                 (i8042_p && i8042_count == 2)))
1446         {
1447           /* Assume the mouse interrupt is the line that says "PS/2 mouse",
1448              or the *second* line that says "i8042".
1449            */
1450           ptr_changed = (*last_ptr_line && !!strcmp (new_line, last_ptr_line));
1451           strcpy (last_ptr_line, new_line);
1452           checked_ptr = True;
1453         }
1454
1455       if (checked_kbd && checked_ptr)
1456         break;
1457     }
1458
1459   if (checked_kbd || checked_ptr)
1460     {
1461       fclose (f1);
1462
1463       if (si->prefs.debug_p && (kbd_changed || ptr_changed))
1464         fprintf (stderr, "%s: /proc/interrupts activity: %s\n",
1465                  blurb(),
1466                  ((kbd_changed && ptr_changed) ? "mouse and kbd" :
1467                   kbd_changed ? "kbd" :
1468                   ptr_changed ? "mouse" : "ERR"));
1469
1470       return (kbd_changed || ptr_changed);
1471     }
1472
1473
1474   /* If we got here, we didn't find either a "keyboard" or a "PS/2 Mouse"
1475      line in the file at all. */
1476   fprintf (stderr, "%s: no keyboard or mouse data in %s?\n",
1477            blurb(), PROC_INTERRUPTS);
1478
1479  FAIL:
1480   if (f1)
1481     fclose (f1);
1482
1483   if (f0 && f0 != (FILE *) -1)
1484     fclose (f0);
1485
1486   f0 = (FILE *) -1;
1487   return False;
1488 }
1489
1490 #endif /* HAVE_PROC_INTERRUPTS */
1491
1492 \f
1493 /* This timer goes off every few minutes, whether the user is idle or not,
1494    to try and clean up anything that has gone wrong.
1495
1496    It calls disable_builtin_screensaver() so that if xset has been used,
1497    or some other program (like xlock) has messed with the XSetScreenSaver()
1498    settings, they will be set back to sensible values (if a server extension
1499    is in use, messing with xlock can cause xscreensaver to never get a wakeup
1500    event, and could cause monitor power-saving to occur, and all manner of
1501    heinousness.)
1502
1503    If the screen is currently blanked, it raises the window, in case some
1504    other window has been mapped on top of it.
1505
1506    If the screen is currently blanked, and there is no hack running, it
1507    clears the window, in case there is an error message printed on it (we
1508    don't want the error message to burn in.)
1509  */
1510
1511 static void
1512 watchdog_timer (XtPointer closure, XtIntervalId *id)
1513 {
1514   saver_info *si = (saver_info *) closure;
1515   saver_preferences *p = &si->prefs;
1516
1517   disable_builtin_screensaver (si, False);
1518
1519   /* If the DPMS settings on the server have changed, change them back to
1520      what ~/.xscreensaver says they should be. */
1521   sync_server_dpms_settings (si->dpy,
1522                              (p->dpms_enabled_p  &&
1523                               p->mode != DONT_BLANK),
1524                              p->dpms_quickoff_p,
1525                              p->dpms_standby / 1000,
1526                              p->dpms_suspend / 1000,
1527                              p->dpms_off / 1000,
1528                              False);
1529
1530   if (si->screen_blanked_p)
1531     {
1532       Bool running_p = screenhack_running_p (si);
1533
1534       if (si->dbox_up_p)
1535         {
1536           if (si->prefs.debug_p)
1537             fprintf (stderr, "%s: dialog box is up: not raising screen.\n",
1538                      blurb());
1539         }
1540       else
1541         {
1542           if (si->prefs.debug_p)
1543             fprintf (stderr, "%s: watchdog timer raising %sscreen.\n",
1544                      blurb(), (running_p ? "" : "and clearing "));
1545
1546           raise_window (si, True, True, running_p);
1547         }
1548
1549       if (screenhack_running_p (si) &&
1550           !monitor_powered_on_p (si))
1551         {
1552           int i;
1553           if (si->prefs.verbose_p)
1554             fprintf (stderr,
1555                      "%s: X says monitor has powered down; "
1556                      "killing running hacks.\n", blurb());
1557           for (i = 0; i < si->nscreens; i++)
1558             kill_screenhack (&si->screens[i]);
1559         }
1560
1561       /* Re-schedule this timer.  The watchdog timer defaults to a bit less
1562          than the hack cycle period, but is never longer than one hour.
1563        */
1564       si->watchdog_id = 0;
1565       reset_watchdog_timer (si, True);
1566     }
1567 }
1568
1569
1570 void
1571 reset_watchdog_timer (saver_info *si, Bool on_p)
1572 {
1573   saver_preferences *p = &si->prefs;
1574
1575   if (si->watchdog_id)
1576     {
1577       XtRemoveTimeOut (si->watchdog_id);
1578       si->watchdog_id = 0;
1579     }
1580
1581   if (on_p && p->watchdog_timeout)
1582     {
1583       si->watchdog_id = XtAppAddTimeOut (si->app, p->watchdog_timeout,
1584                                          watchdog_timer, (XtPointer) si);
1585
1586       if (p->debug_p)
1587         fprintf (stderr, "%s: restarting watchdog_timer (%ld, %ld)\n",
1588                  blurb(), p->watchdog_timeout, si->watchdog_id);
1589     }
1590 }
1591
1592
1593 /* It's possible that a race condition could have led to the saver
1594    window being unexpectedly still mapped.  This can happen like so:
1595
1596     - screen is blanked
1597     - hack is launched
1598     - that hack tries to grab a screen image (it does this by
1599       first unmapping the saver window, then remapping it.)
1600     - hack unmaps window
1601     - hack waits
1602     - user becomes active
1603     - hack re-maps window (*)
1604     - driver kills subprocess
1605     - driver unmaps window (**)
1606
1607    The race is that (*) might have been sent to the server before
1608    the client process was killed, but, due to scheduling randomness,
1609    might not have been received by the server until after (**).
1610    In other words, (*) and (**) might happen out of order, meaning
1611    the driver will unmap the window, and then after that, the
1612    recently-dead client will re-map it.  This leaves the user
1613    locked out (it looks like a desktop, but it's not!)
1614
1615    To avoid this: after un-blanking the screen, we launch a timer
1616    that wakes up once a second for ten seconds, and makes damned
1617    sure that the window is still unmapped.
1618  */
1619
1620 void
1621 de_race_timer (XtPointer closure, XtIntervalId *id)
1622 {
1623   saver_info *si = (saver_info *) closure;
1624   saver_preferences *p = &si->prefs;
1625   int secs = 1;
1626
1627   if (id == 0)  /* if id is 0, this is the initialization call. */
1628     {
1629       si->de_race_ticks = 10;
1630       if (p->verbose_p)
1631         fprintf (stderr, "%s: starting de-race timer (%d seconds.)\n",
1632                  blurb(), si->de_race_ticks);
1633     }
1634   else
1635     {
1636       int i;
1637       XSync (si->dpy, False);
1638       for (i = 0; i < si->nscreens; i++)
1639         {
1640           saver_screen_info *ssi = &si->screens[i];
1641           Window w = ssi->screensaver_window;
1642           XWindowAttributes xgwa;
1643           XGetWindowAttributes (si->dpy, w, &xgwa);
1644           if (xgwa.map_state != IsUnmapped)
1645             {
1646               if (p->verbose_p)
1647                 fprintf (stderr,
1648                          "%s: %d: client race! emergency unmap 0x%lx.\n",
1649                          blurb(), i, (unsigned long) w);
1650               XUnmapWindow (si->dpy, w);
1651             }
1652           else if (p->debug_p)
1653             fprintf (stderr, "%s: %d: (de-race of 0x%lx is cool.)\n",
1654                      blurb(), i, (unsigned long) w);
1655         }
1656       XSync (si->dpy, False);
1657
1658       si->de_race_ticks--;
1659     }
1660
1661   if (id && *id == si->de_race_id)
1662     si->de_race_id = 0;
1663
1664   if (si->de_race_id) abort();
1665
1666   if (si->de_race_ticks <= 0)
1667     {
1668       si->de_race_id = 0;
1669       if (p->verbose_p)
1670         fprintf (stderr, "%s: de-race completed.\n", blurb());
1671     }
1672   else
1673     {
1674       si->de_race_id = XtAppAddTimeOut (si->app, secs * 1000,
1675                                         de_race_timer, closure);
1676     }
1677 }