+
+static Bool
+proc_interrupts_activity_p (saver_info *si)
+{
+ static FILE *f0 = 0;
+ FILE *f1 = 0;
+ int fd;
+ static char last_kbd_line[255] = { 0, };
+ static char last_ptr_line[255] = { 0, };
+ char new_line[sizeof(last_kbd_line)];
+ Bool checked_kbd = False, kbd_changed = False;
+ Bool checked_ptr = False, ptr_changed = False;
+ int i8042_count = 0;
+
+ if (!f0)
+ {
+ /* First time -- open the file. */
+ f0 = fopen (PROC_INTERRUPTS, "r");
+ if (!f0)
+ {
+ char buf[255];
+ sprintf(buf, "%s: error opening %s", blurb(), PROC_INTERRUPTS);
+ perror (buf);
+ goto FAIL;
+ }
+
+# if defined(HAVE_FCNTL) && defined(FD_CLOEXEC)
+ /* Close this fd upon exec instead of inheriting / leaking it. */
+ if (fcntl (fileno (f0), F_SETFD, FD_CLOEXEC) != 0)
+ perror ("fcntl: CLOEXEC:");
+# endif
+ }
+
+ if (f0 == (FILE *) -1) /* means we got an error initializing. */
+ return False;
+
+ fd = dup (fileno (f0));
+ if (fd < 0)
+ {
+ char buf[255];
+ sprintf(buf, "%s: could not dup() the %s fd", blurb(), PROC_INTERRUPTS);
+ perror (buf);
+ goto FAIL;
+ }
+
+ f1 = fdopen (fd, "r");
+ if (!f1)
+ {
+ char buf[255];
+ sprintf(buf, "%s: could not fdopen() the %s fd", blurb(),
+ PROC_INTERRUPTS);
+ perror (buf);
+ goto FAIL;
+ }
+
+ /* Actually, I'm unclear on why this fseek() is necessary, given the timing
+ of the dup() above, but it is. */
+ if (fseek (f1, 0, SEEK_SET) != 0)
+ {
+ char buf[255];
+ sprintf(buf, "%s: error rewinding %s", blurb(), PROC_INTERRUPTS);
+ perror (buf);
+ goto FAIL;
+ }
+
+ /* Now read through the pseudo-file until we find the "keyboard",
+ "PS/2 mouse", or "i8042" lines. */
+
+ while (fgets (new_line, sizeof(new_line)-1, f1))
+ {
+ Bool i8042_p = !!strstr (new_line, "i8042");
+ if (i8042_p) i8042_count++;
+
+ if (strchr (new_line, ','))
+ {
+ /* Ignore any line that has a comma on it: this is because
+ a setup like this:
+
+ 12: 930935 XT-PIC usb-uhci, PS/2 Mouse
+
+ is really bad news. It *looks* like we can note mouse
+ activity from that line, but really, that interrupt gets
+ fired any time any USB device has activity! So we have
+ to ignore any shared IRQs.
+ */
+ }
+ else if (!checked_kbd &&
+ (strstr (new_line, "keyboard") ||
+ (i8042_p && i8042_count == 1)))
+ {
+ /* Assume the keyboard interrupt is the line that says "keyboard",
+ or the *first* line that says "i8042".
+ */
+ kbd_changed = (*last_kbd_line && !!strcmp (new_line, last_kbd_line));
+ strcpy (last_kbd_line, new_line);
+ checked_kbd = True;
+ }
+ else if (!checked_ptr &&
+ (strstr (new_line, "PS/2 Mouse") ||
+ (i8042_p && i8042_count == 2)))
+ {
+ /* Assume the mouse interrupt is the line that says "PS/2 mouse",
+ or the *second* line that says "i8042".
+ */
+ ptr_changed = (*last_ptr_line && !!strcmp (new_line, last_ptr_line));
+ strcpy (last_ptr_line, new_line);
+ checked_ptr = True;
+ }
+
+ if (checked_kbd && checked_ptr)
+ break;
+ }
+
+ if (checked_kbd || checked_ptr)
+ {
+ fclose (f1);
+
+ if (si->prefs.debug_p && (kbd_changed || ptr_changed))
+ fprintf (stderr, "%s: /proc/interrupts activity: %s\n",
+ blurb(),
+ ((kbd_changed && ptr_changed) ? "mouse and kbd" :
+ kbd_changed ? "kbd" :
+ ptr_changed ? "mouse" : "ERR"));
+
+ return (kbd_changed || ptr_changed);
+ }
+
+
+ /* If we got here, we didn't find either a "keyboard" or a "PS/2 Mouse"
+ line in the file at all. */
+ fprintf (stderr, "%s: no keyboard or mouse data in %s?\n",
+ blurb(), PROC_INTERRUPTS);
+
+ FAIL:
+ if (f1)
+ fclose (f1);
+
+ if (f0 && f0 != (FILE *) -1)
+ fclose (f0);
+
+ f0 = (FILE *) -1;
+ return False;
+}
+
+#endif /* HAVE_PROC_INTERRUPTS */
+
+\f
+/* This timer goes off every few minutes, whether the user is idle or not,
+ to try and clean up anything that has gone wrong.
+
+ It calls disable_builtin_screensaver() so that if xset has been used,
+ or some other program (like xlock) has messed with the XSetScreenSaver()
+ settings, they will be set back to sensible values (if a server extension
+ is in use, messing with xlock can cause xscreensaver to never get a wakeup
+ event, and could cause monitor power-saving to occur, and all manner of
+ heinousness.)
+
+ If the screen is currently blanked, it raises the window, in case some
+ other window has been mapped on top of it.
+
+ If the screen is currently blanked, and there is no hack running, it
+ clears the window, in case there is an error message printed on it (we
+ don't want the error message to burn in.)
+ */
+
+static void
+watchdog_timer (XtPointer closure, XtIntervalId *id)
+{
+ saver_info *si = (saver_info *) closure;
+ saver_preferences *p = &si->prefs;
+
+ disable_builtin_screensaver (si, False);
+
+ /* If the DPMS settings on the server have changed, change them back to
+ what ~/.xscreensaver says they should be. */
+ sync_server_dpms_settings (si->dpy,
+ (p->dpms_enabled_p &&
+ p->mode != DONT_BLANK),
+ p->dpms_quickoff_p,
+ p->dpms_standby / 1000,
+ p->dpms_suspend / 1000,
+ p->dpms_off / 1000,
+ False);
+
+ if (si->screen_blanked_p)
+ {
+ Bool running_p = screenhack_running_p (si);
+
+ if (si->dbox_up_p)
+ {
+ if (si->prefs.debug_p)
+ fprintf (stderr, "%s: dialog box is up: not raising screen.\n",
+ blurb());
+ }
+ else
+ {
+ if (si->prefs.debug_p)
+ fprintf (stderr, "%s: watchdog timer raising %sscreen.\n",
+ blurb(), (running_p ? "" : "and clearing "));
+
+ raise_window (si, True, True, running_p);
+ }
+
+ if (screenhack_running_p (si) &&
+ !monitor_powered_on_p (si))
+ {
+ int i;
+ if (si->prefs.verbose_p)
+ fprintf (stderr,
+ "%s: X says monitor has powered down; "
+ "killing running hacks.\n", blurb());
+ for (i = 0; i < si->nscreens; i++)
+ kill_screenhack (&si->screens[i]);
+ }
+
+ /* Re-schedule this timer. The watchdog timer defaults to a bit less
+ than the hack cycle period, but is never longer than one hour.
+ */
+ si->watchdog_id = 0;
+ reset_watchdog_timer (si, True);
+ }
+}
+
+
+void
+reset_watchdog_timer (saver_info *si, Bool on_p)
+{
+ saver_preferences *p = &si->prefs;
+
+ if (si->watchdog_id)
+ {
+ XtRemoveTimeOut (si->watchdog_id);
+ si->watchdog_id = 0;
+ }
+
+ if (on_p && p->watchdog_timeout)
+ {
+ si->watchdog_id = XtAppAddTimeOut (si->app, p->watchdog_timeout,
+ watchdog_timer, (XtPointer) si);
+
+ if (p->debug_p)
+ fprintf (stderr, "%s: restarting watchdog_timer (%ld, %ld)\n",
+ blurb(), p->watchdog_timeout, si->watchdog_id);
+ }
+}
+
+
+/* It's possible that a race condition could have led to the saver
+ window being unexpectedly still mapped. This can happen like so:
+
+ - screen is blanked
+ - hack is launched
+ - that hack tries to grab a screen image (it does this by
+ first unmapping the saver window, then remapping it.)
+ - hack unmaps window
+ - hack waits
+ - user becomes active
+ - hack re-maps window (*)
+ - driver kills subprocess
+ - driver unmaps window (**)
+
+ The race is that (*) might have been sent to the server before
+ the client process was killed, but, due to scheduling randomness,
+ might not have been received by the server until after (**).
+ In other words, (*) and (**) might happen out of order, meaning
+ the driver will unmap the window, and then after that, the
+ recently-dead client will re-map it. This leaves the user
+ locked out (it looks like a desktop, but it's not!)
+
+ To avoid this: after un-blanking the screen, we launch a timer
+ that wakes up once a second for ten seconds, and makes damned
+ sure that the window is still unmapped.
+ */
+
+void
+de_race_timer (XtPointer closure, XtIntervalId *id)
+{
+ saver_info *si = (saver_info *) closure;
+ saver_preferences *p = &si->prefs;
+ int secs = 1;
+
+ if (id == 0) /* if id is 0, this is the initialization call. */
+ {
+ si->de_race_ticks = 10;
+ if (p->verbose_p)
+ fprintf (stderr, "%s: starting de-race timer (%d seconds.)\n",
+ blurb(), si->de_race_ticks);
+ }
+ else
+ {
+ int i;
+ XSync (si->dpy, False);
+ for (i = 0; i < si->nscreens; i++)
+ {
+ saver_screen_info *ssi = &si->screens[i];
+ Window w = ssi->screensaver_window;
+ XWindowAttributes xgwa;
+ XGetWindowAttributes (si->dpy, w, &xgwa);
+ if (xgwa.map_state != IsUnmapped)
+ {
+ if (p->verbose_p)
+ fprintf (stderr,
+ "%s: %d: client race! emergency unmap 0x%lx.\n",
+ blurb(), i, (unsigned long) w);
+ XUnmapWindow (si->dpy, w);
+ }
+ else if (p->debug_p)
+ fprintf (stderr, "%s: %d: (de-race of 0x%lx is cool.)\n",
+ blurb(), i, (unsigned long) w);
+ }
+ XSync (si->dpy, False);
+
+ si->de_race_ticks--;
+ }
+
+ if (id && *id == si->de_race_id)
+ si->de_race_id = 0;
+
+ if (si->de_race_id) abort();
+
+ if (si->de_race_ticks <= 0)
+ {
+ si->de_race_id = 0;
+ if (p->verbose_p)
+ fprintf (stderr, "%s: de-race completed.\n", blurb());
+ }
+ else
+ {
+ si->de_race_id = XtAppAddTimeOut (si->app, secs * 1000,
+ de_race_timer, closure);
+ }