+}
+
+
+\f
+/* Some crap for dealing with /proc/interrupts.
+
+ On Linux systems, it's possible to see the hardware interrupt count
+ associated with the keyboard. We can therefore use that as another method
+ of detecting idleness.
+
+ Why is it a good idea to do this? Because it lets us detect keyboard
+ activity that is not associated with X events. For example, if the user
+ has switched to another virtual console, it's good for xscreensaver to not
+ be running graphics hacks on the (non-visible) X display. The common
+ complaint that checking /proc/interrupts addresses is that the user is
+ playing Quake on a non-X console, and the GL hacks are perceptibly slowing
+ the game...
+
+ This is tricky for a number of reasons.
+
+ * First, we must be sure to only do this when running on an X server that
+ is on the local machine (because otherwise, we'd be reacting to the
+ wrong keyboard.) The way we do this is by noting that the $DISPLAY is
+ pointing to display 0 on the local machine. It *could* be that display
+ 1 is also on the local machine (e.g., two X servers, each on a different
+ virtual-terminal) but it's also possible that screen 1 is an X terminal,
+ using this machine as the host. So we can't take that chance.
+
+ * Second, one can only access these interrupt numbers in a completely
+ and utterly brain-damaged way. You would think that one would use an
+ ioctl for this. But no. The ONLY way to get this information is to
+ open the pseudo-file /proc/interrupts AS A FILE, and read the numbers
+ out of it TEXTUALLY. Because this is Unix, and all the world's a file,
+ and the only real data type is the short-line sequence of ASCII bytes.
+
+ Now it's all well and good that the /proc/interrupts pseudo-file
+ exists; that's a clever idea, and a useful API for things that are
+ already textually oriented, like shell scripts, and users doing
+ interactive debugging sessions. But to make a *C PROGRAM* open a file
+ and parse the textual representation of integers out of it is just
+ insane.
+
+ * Third, you can't just hold the file open, and fseek() back to the
+ beginning to get updated data! If you do that, the data never changes.
+ And I don't want to call open() every five seconds, because I don't want
+ to risk going to disk for any inodes. It turns out that if you dup()
+ it early, then each copy gets fresh data, so we can get around that in
+ this way (but for how many releases, one might wonder?)
+
+ * Fourth, the format of the output of the /proc/interrupts file is
+ undocumented, and has changed several times already! In Linux 2.0.33,
+ even on a multiprocessor machine, it looks like this:
+
+ 0: 309453991 timer
+ 1: 4771729 keyboard
+
+ but in Linux 2.2 and 2.4 kernels with MP machines, it looks like this:
+
+ CPU0 CPU1
+ 0: 1671450 1672618 IO-APIC-edge timer
+ 1: 13037 13495 IO-APIC-edge keyboard
+
+ and in Linux 2.6, it's gotten even goofier: now there are two lines
+ labelled "i8042". One of them is the keyboard, and one of them is
+ the PS/2 mouse -- and of course, you can't tell them apart, except
+ by wiggling the mouse and noting which one changes:
+
+ CPU0 CPU1
+ 1: 32051 30864 IO-APIC-edge i8042
+ 12: 476577 479913 IO-APIC-edge i8042
+
+ Joy! So how are we expected to parse that? Well, this code doesn't
+ parse it: it saves the first line with the string "keyboard" (or
+ "i8042") in it, and does a string-comparison to note when it has
+ changed. If there are two "i8042" lines, we assume the first is
+ the keyboard and the second is the mouse (doesn't matter which is
+ which, really, as long as we don't compare them against each other.)
+
+ Thanks to Nat Friedman <nat@nat.org> for figuring out most of this crap.
+
+ Note that if you have a serial or USB mouse, or a USB keyboard, it won't
+ detect it. That's because there's no way to tell the difference between a
+ serial mouse and a general serial port, and all USB devices look the same
+ from here. It would be somewhat unfortunate to have the screensaver turn
+ off when the modem on COM1 burped, or when a USB disk was accessed.
+ */
+
+
+#ifdef HAVE_PROC_INTERRUPTS
+
+#define PROC_INTERRUPTS "/proc/interrupts"
+
+Bool
+query_proc_interrupts_available (saver_info *si, const char **why)
+{
+ /* We can use /proc/interrupts if $DISPLAY points to :0, and if the
+ "/proc/interrupts" file exists and is readable.
+ */
+ FILE *f;
+ if (why) *why = 0;
+
+ if (!display_is_on_console_p (si))
+ {
+ if (why) *why = "not on primary console";
+ return False;
+ }
+
+ f = fopen (PROC_INTERRUPTS, "r");
+ if (!f)
+ {
+ if (why) *why = "does not exist";
+ return False;
+ }
+
+ fclose (f);
+ return True;
+}
+
+
+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;