1 /* timers.c --- detecting when the user is idle, and other timer-related tasks.
2 * xscreensaver, Copyright (c) 1991-2002 Jamie Zawinski <jwz@jwz.org>
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
19 #include <X11/Intrinsic.h>
25 # include <X11/Xmu/Error.h>
27 # include <Xmu/Error.h>
29 # else /* !HAVE_XMU */
31 #endif /* !HAVE_XMU */
33 #ifdef HAVE_XIDLE_EXTENSION
34 #include <X11/extensions/xidle.h>
35 #endif /* HAVE_XIDLE_EXTENSION */
37 #ifdef HAVE_MIT_SAVER_EXTENSION
38 #include <X11/extensions/scrnsaver.h>
39 #endif /* HAVE_MIT_SAVER_EXTENSION */
41 #ifdef HAVE_SGI_SAVER_EXTENSION
42 #include <X11/extensions/XScreenSaver.h>
43 #endif /* HAVE_SGI_SAVER_EXTENSION */
45 #include "xscreensaver.h"
47 #ifdef HAVE_PROC_INTERRUPTS
48 static Bool proc_interrupts_activity_p (saver_info *si);
49 #endif /* HAVE_PROC_INTERRUPTS */
51 static void check_for_clock_skew (saver_info *si);
55 idle_timer (XtPointer closure, XtIntervalId *id)
57 saver_info *si = (saver_info *) closure;
59 /* What an amazingly shitty design. Not only does Xt execute timeout
60 events from XtAppNextEvent() instead of from XtDispatchEvent(), but
61 there is no way to tell Xt to block until there is an X event OR a
62 timeout happens. Once your timeout proc is called, XtAppNextEvent()
63 still won't return until a "real" X event comes in.
65 So this function pushes a stupid, gratuitous, unnecessary event back
66 on the event queue to force XtAppNextEvent to return Right Fucking Now.
67 When the code in sleep_until_idle() sees an event of type XAnyEvent,
68 which the server never generates, it knows that a timeout has occurred.
71 fake_event.type = 0; /* XAnyEvent type, ignored. */
72 fake_event.xany.display = si->dpy;
73 fake_event.xany.window = 0;
74 XPutBackEvent (si->dpy, &fake_event);
79 schedule_wakeup_event (saver_info *si, Time when, Bool verbose_p)
81 /* Wake up periodically to ask the server if we are idle. */
82 si->timer_id = XtAppAddTimeOut (si->app, when, idle_timer,
86 fprintf (stderr, "%s: starting idle_timer (%ld, %ld)\n",
87 blurb(), when, si->timer_id);
92 notice_events (saver_info *si, Window window, Bool top_p)
94 saver_preferences *p = &si->prefs;
95 XWindowAttributes attrs;
97 Window root, parent, *kids;
101 if (XtWindowToWidget (si->dpy, window))
102 /* If it's one of ours, don't mess up its event mask. */
105 if (!XQueryTree (si->dpy, window, &root, &parent, &kids, &nkids))
110 /* Figure out which screen this window is on, for the diagnostics. */
111 for (screen_no = 0; screen_no < si->nscreens; screen_no++)
112 if (root == RootWindowOfScreen (si->screens[screen_no].screen))
115 XGetWindowAttributes (si->dpy, window, &attrs);
116 events = ((attrs.all_event_masks | attrs.do_not_propagate_mask)
119 /* Select for SubstructureNotify on all windows.
120 Select for KeyPress on all windows that already have it selected.
122 Note that we can't select for ButtonPress, because of X braindamage:
123 only one client at a time may select for ButtonPress on a given
124 window, though any number can select for KeyPress. Someone explain
127 So, if the user spends a while clicking the mouse without ever moving
128 the mouse or touching the keyboard, we won't know that they've been
129 active, and the screensaver will come on. That sucks, but I don't
130 know how to get around it.
132 Since X presents mouse wheels as clicks, this applies to those, too:
133 scrolling through a document using only the mouse wheel doesn't
134 count as activity... Fortunately, /proc/interrupts helps, on
135 systems that have it. Oh, if it's a PS/2 mouse, not serial or USB.
138 XSelectInput (si->dpy, window, SubstructureNotifyMask | events);
140 if (top_p && p->debug_p && (events & KeyPressMask))
142 /* Only mention one window per tree (hack hack). */
143 fprintf (stderr, "%s: %d: selected KeyPress on 0x%lX\n",
144 blurb(), screen_no, (unsigned long) window);
151 notice_events (si, kids [--nkids], top_p);
152 XFree ((char *) kids);
158 BadWindow_ehandler (Display *dpy, XErrorEvent *error)
160 /* When we notice a window being created, we spawn a timer that waits
161 30 seconds or so, and then selects events on that window. This error
162 handler is used so that we can cope with the fact that the window
163 may have been destroyed <30 seconds after it was created.
165 if (error->error_code == BadWindow ||
166 error->error_code == BadMatch ||
167 error->error_code == BadDrawable)
170 return saver_ehandler (dpy, error);
174 struct notice_events_timer_arg {
180 notice_events_timer (XtPointer closure, XtIntervalId *id)
182 struct notice_events_timer_arg *arg =
183 (struct notice_events_timer_arg *) closure;
185 XErrorHandler old_handler = XSetErrorHandler (BadWindow_ehandler);
187 saver_info *si = arg->si;
188 Window window = arg->w;
191 notice_events (si, window, True);
192 XSync (si->dpy, False);
193 XSetErrorHandler (old_handler);
197 start_notice_events_timer (saver_info *si, Window w, Bool verbose_p)
199 saver_preferences *p = &si->prefs;
200 struct notice_events_timer_arg *arg =
201 (struct notice_events_timer_arg *) malloc(sizeof(*arg));
204 XtAppAddTimeOut (si->app, p->notice_events_timeout, notice_events_timer,
208 fprintf (stderr, "%s: starting notice_events_timer for 0x%X (%lu)\n",
209 blurb(), (unsigned int) w, p->notice_events_timeout);
213 /* When the screensaver is active, this timer will periodically change
217 cycle_timer (XtPointer closure, XtIntervalId *id)
219 saver_info *si = (saver_info *) closure;
220 saver_preferences *p = &si->prefs;
221 Time how_long = p->cycle;
223 if (si->selection_mode > 0 &&
224 screenhack_running_p (si))
225 /* If we're in "SELECT n" mode, the cycle timer going off will just
226 restart this same hack again. There's not much point in doing this
227 every 5 or 10 minutes, but on the other hand, leaving one hack running
228 for days is probably not a great idea, since they tend to leak and/or
229 crash. So, restart the thing once an hour. */
230 how_long = 1000 * 60 * 60;
235 fprintf (stderr, "%s: dialog box up; delaying hack change.\n",
237 how_long = 30000; /* 30 secs */
241 maybe_reload_init_file (si);
242 kill_screenhack (si);
244 if (!si->throttled_p)
245 spawn_screenhack (si, False);
248 raise_window (si, True, True, False);
250 fprintf (stderr, "%s: not launching new hack (throttled.)\n",
257 si->cycle_id = XtAppAddTimeOut (si->app, how_long, cycle_timer,
261 fprintf (stderr, "%s: starting cycle_timer (%ld, %ld)\n",
262 blurb(), how_long, si->cycle_id);
267 fprintf (stderr, "%s: not starting cycle_timer: how_long == %ld\n",
268 blurb(), (unsigned long) how_long);
274 activate_lock_timer (XtPointer closure, XtIntervalId *id)
276 saver_info *si = (saver_info *) closure;
277 saver_preferences *p = &si->prefs;
280 fprintf (stderr, "%s: timed out; activating lock.\n", blurb());
281 set_locked_p (si, True);
285 /* Call this when user activity (or "simulated" activity) has been noticed.
288 reset_timers (saver_info *si)
290 saver_preferences *p = &si->prefs;
291 if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
297 fprintf (stderr, "%s: killing idle_timer (%ld, %ld)\n",
298 blurb(), p->timeout, si->timer_id);
299 XtRemoveTimeOut (si->timer_id);
302 schedule_wakeup_event (si, p->timeout, p->debug_p); /* sets si->timer_id */
304 if (si->cycle_id) abort (); /* no cycle timer when inactive */
306 si->last_activity_time = time ((time_t *) 0);
310 /* When we aren't using a server extension, this timer is used to periodically
311 wake up and poll the mouse position, which is possibly more reliable than
312 selecting motion events on every window.
315 check_pointer_timer (XtPointer closure, XtIntervalId *id)
318 saver_info *si = (saver_info *) closure;
319 saver_preferences *p = &si->prefs;
320 Bool active_p = False;
322 if (!si->using_proc_interrupts &&
323 (si->using_xidle_extension ||
324 si->using_mit_saver_extension ||
325 si->using_sgi_saver_extension))
326 /* If an extension is in use, we should not be polling the mouse.
327 Unless we're also checking /proc/interrupts, in which case, we should.
331 si->check_pointer_timer_id =
332 XtAppAddTimeOut (si->app, p->pointer_timeout, check_pointer_timer,
335 for (i = 0; i < si->nscreens; i++)
337 saver_screen_info *ssi = &si->screens[i];
339 int root_x, root_y, x, y;
342 if (!XQueryPointer (si->dpy, ssi->screensaver_window, &root, &child,
343 &root_x, &root_y, &x, &y, &mask))
345 /* If XQueryPointer() returns false, the mouse is not on this screen.
351 if (root_x == ssi->poll_mouse_last_root_x &&
352 root_y == ssi->poll_mouse_last_root_y &&
353 child == ssi->poll_mouse_last_child &&
354 mask == ssi->poll_mouse_last_mask)
361 if (root_x == ssi->poll_mouse_last_root_x &&
362 root_y == ssi->poll_mouse_last_root_y &&
363 child == ssi->poll_mouse_last_child)
364 fprintf (stderr, "%s: %d: modifiers changed: 0x%04x -> 0x%04x.\n",
365 blurb(), i, ssi->poll_mouse_last_mask, mask);
368 fprintf (stderr, "%s: %d: pointer moved: ", blurb(), i);
369 if (ssi->poll_mouse_last_root_x == -1)
370 fprintf (stderr, "off screen");
372 fprintf (stderr, "%d,%d",
373 ssi->poll_mouse_last_root_x,
374 ssi->poll_mouse_last_root_y);
375 fprintf (stderr, " -> ");
377 fprintf (stderr, "off screen.");
379 fprintf (stderr, "%d,%d", root_x, root_y);
380 if (ssi->poll_mouse_last_root_x == -1 || root_x == -1)
381 fprintf (stderr, ".\n");
384 # define ABS(x)((x)<0?-(x):(x))
385 fprintf (stderr, " (%d,%d).\n",
386 ABS(ssi->poll_mouse_last_root_x - root_x),
387 ABS(ssi->poll_mouse_last_root_y - root_y));
392 si->last_activity_screen = ssi;
393 ssi->poll_mouse_last_root_x = root_x;
394 ssi->poll_mouse_last_root_y = root_y;
395 ssi->poll_mouse_last_child = child;
396 ssi->poll_mouse_last_mask = mask;
399 #ifdef HAVE_PROC_INTERRUPTS
401 si->using_proc_interrupts &&
402 proc_interrupts_activity_p (si))
406 #endif /* HAVE_PROC_INTERRUPTS */
412 check_for_clock_skew (si);
416 /* An unfortunate situation is this: the saver is not active, because the
417 user has been typing. The machine is a laptop. The user closes the lid
418 and suspends it. The CPU halts. Some hours later, the user opens the
419 lid. At this point, Xt's timers will fire, and xscreensaver will blank
422 So far so good -- well, not really, but it's the best that we can do,
423 since the OS doesn't send us a signal *before* shutdown -- but if the
424 user had delayed locking (lockTimeout > 0) then we should start off
425 in the locked state, rather than only locking N minutes from when the
426 lid was opened. Also, eschewing fading is probably a good idea, to
427 clamp down as soon as possible.
429 We only do this when we'd be polling the mouse position anyway.
430 This amounts to an assumption that machines with APM support also
431 have /proc/interrupts.
434 check_for_clock_skew (saver_info *si)
436 saver_preferences *p = &si->prefs;
437 time_t now = time ((time_t *) 0);
438 long shift = now - si->last_wall_clock_time;
442 int i = (si->last_wall_clock_time == 0 ? 0 : shift);
444 "%s: checking wall clock for hibernation (%d:%02d:%02d).\n",
446 (i / (60 * 60)), ((i / 60) % 60), (i % 60));
449 if (si->last_wall_clock_time != 0 &&
450 shift > (p->timeout / 1000))
453 fprintf (stderr, "%s: wall clock has jumped by %ld:%02ld:%02ld!\n",
455 (shift / (60 * 60)), ((shift / 60) % 60), (shift % 60));
457 si->emergency_lock_p = True;
458 idle_timer ((XtPointer) si, 0);
461 si->last_wall_clock_time = now;
467 dispatch_event (saver_info *si, XEvent *event)
469 /* If this is for the splash dialog, pass it along.
470 Note that the password dialog is handled with its own event loop,
471 so events for that window will never come through here.
473 if (si->splash_dialog && event->xany.window == si->splash_dialog)
474 handle_splash_event (si, event);
476 XtDispatchEvent (event);
481 swallow_unlock_typeahead_events (saver_info *si, XEvent *e)
487 memset (buf, 0, sizeof(buf));
493 if (event.xany.type == KeyPress)
496 int size = XLookupString ((XKeyEvent *) &event, s, 1, 0, 0);
497 if (size != 1) continue;
500 case '\010': case '\177': /* Backspace */
503 case '\025': case '\030': /* Erase line */
504 case '\012': case '\015': /* Enter */
507 case '\040': /* Space */
509 break; /* ignore space at beginning of line */
510 /* else, fall through */
517 } while (i < sizeof(buf)-1 &&
518 XCheckMaskEvent (si->dpy, KeyPressMask, &event));
522 if (si->unlock_typeahead)
524 memset (si->unlock_typeahead, 0, strlen(si->unlock_typeahead));
525 free (si->unlock_typeahead);
529 si->unlock_typeahead = strdup (buf);
531 si->unlock_typeahead = 0;
533 memset (buf, 0, sizeof(buf));
537 /* methods of detecting idleness:
539 explicitly informed by SGI SCREEN_SAVER server event;
540 explicitly informed by MIT-SCREEN-SAVER server event;
541 poll server idle time with XIDLE extension;
542 select events on all windows, and note absence of recent events;
543 note that /proc/interrupts has not changed in a while;
544 activated by clientmessage.
546 methods of detecting non-idleness:
548 read events on the xscreensaver window;
549 explicitly informed by SGI SCREEN_SAVER server event;
550 explicitly informed by MIT-SCREEN-SAVER server event;
551 select events on all windows, and note events on any of them;
552 note that /proc/interrupts has changed;
553 deactivated by clientmessage.
555 I trust that explains why this function is a big hairy mess.
558 sleep_until_idle (saver_info *si, Bool until_idle_p)
560 saver_preferences *p = &si->prefs;
563 /* We need to select events on all windows if we're not using any extensions.
564 Otherwise, we don't need to. */
565 Bool scanning_all_windows = !(si->using_xidle_extension ||
566 si->using_mit_saver_extension ||
567 si->using_sgi_saver_extension);
569 /* We need to periodically wake up and check for idleness if we're not using
570 any extensions, or if we're using the XIDLE extension. The other two
571 extensions explicitly deliver events when we go idle/non-idle, so we
572 don't need to poll. */
573 Bool polling_for_idleness = !(si->using_mit_saver_extension ||
574 si->using_sgi_saver_extension);
576 /* Whether we need to periodically wake up and check to see if the mouse has
577 moved. We only need to do this when not using any extensions. The reason
578 this isn't the same as `polling_for_idleness' is that the "idleness" poll
579 can happen (for example) 5 minutes from now, whereas the mouse-position
580 poll should happen with low periodicity. We don't need to poll the mouse
581 position with the XIDLE extension, but we do need to periodically wake up
582 and query the server with that extension. For our purposes, polling
583 /proc/interrupts is just like polling the mouse position. It has to
584 happen on the same kind of schedule. */
585 Bool polling_mouse_position = (si->using_proc_interrupts ||
586 !(si->using_xidle_extension ||
587 si->using_mit_saver_extension ||
588 si->using_sgi_saver_extension));
592 if (polling_for_idleness)
593 /* This causes a no-op event to be delivered to us in a while, so that
594 we come back around through the event loop again. Use of this timer
595 is economical: for example, if the screensaver should come on in 5
596 minutes, and the user has been idle for 2 minutes, then this
597 timeout will go off no sooner than 3 minutes from now. */
598 schedule_wakeup_event (si, p->timeout, p->debug_p);
600 if (polling_mouse_position)
601 /* Check to see if the mouse has moved, and set up a repeating timer
602 to do so periodically (typically, every 5 seconds.) */
603 check_pointer_timer ((XtPointer) si, 0);
608 XtAppNextEvent (si->app, &event);
610 switch (event.xany.type) {
611 case 0: /* our synthetic "timeout" event has been signalled */
615 #ifdef HAVE_XIDLE_EXTENSION
616 if (si->using_xidle_extension)
618 /* The XIDLE extension uses the synthetic event to prod us into
619 re-asking the server how long the user has been idle. */
620 if (! XGetIdleTime (si->dpy, &idle))
622 fprintf (stderr, "%s: XGetIdleTime() failed.\n", blurb());
623 saver_exit (si, 1, 0);
627 #endif /* HAVE_XIDLE_EXTENSION */
628 #ifdef HAVE_MIT_SAVER_EXTENSION
629 if (si->using_mit_saver_extension)
631 /* We don't need to do anything in this case - the synthetic
632 event isn't necessary, as we get sent specific events
633 to wake us up. In fact, this event generally shouldn't
634 be being delivered when the MIT extension is in use. */
638 #endif /* HAVE_MIT_SAVER_EXTENSION */
639 #ifdef HAVE_SGI_SAVER_EXTENSION
640 if (si->using_sgi_saver_extension)
642 /* We don't need to do anything in this case - the synthetic
643 event isn't necessary, as we get sent specific events
644 to wake us up. In fact, this event generally shouldn't
645 be being delivered when the SGI extension is in use. */
649 #endif /* HAVE_SGI_SAVER_EXTENSION */
651 /* Otherwise, no server extension is in use. The synthetic
652 event was to tell us to wake up and see if the user is now
653 idle. Compute the amount of idle time by comparing the
654 `last_activity_time' to the wall clock. The l_a_t was set
655 by calling `reset_timers()', which is called only in only
656 two situations: when polling the mouse position has revealed
657 the the mouse has moved (user activity) or when we have read
658 an event (again, user activity.)
660 idle = 1000 * (si->last_activity_time - time ((time_t *) 0));
663 if (idle >= p->timeout)
665 /* Look, we've been idle long enough. We're done. */
668 else if (si->emergency_lock_p)
670 /* Oops, the wall clock has jumped far into the future, so
671 we need to lock down in a hurry! */
676 /* The event went off, but it turns out that the user has not
677 yet been idle for long enough. So re-signal the event.
679 if (polling_for_idleness)
680 schedule_wakeup_event (si, p->timeout - idle, p->debug_p);
686 if (handle_clientmessage (si, &event, until_idle_p))
691 /* A window has been created on the screen somewhere. If we're
692 supposed to scan all windows for events, prepare this window. */
693 if (scanning_all_windows)
695 Window w = event.xcreatewindow.window;
696 start_notice_events_timer (si, w, p->debug_p);
708 Window root=0, window=0;
710 const char *type = 0;
711 if (event.xany.type == MotionNotify)
713 type = "MotionNotify";
714 root = event.xmotion.root;
715 window = event.xmotion.window;
716 x = event.xmotion.x_root;
717 y = event.xmotion.y_root;
719 else if (event.xany.type == KeyPress)
722 root = event.xkey.root;
723 window = event.xkey.window;
726 else if (event.xany.type == ButtonPress)
728 type = "ButtonPress";
729 root = event.xkey.root;
730 window = event.xkey.window;
731 x = event.xmotion.x_root;
732 y = event.xmotion.y_root;
738 for (i = 0; i < si->nscreens; i++)
739 if (root == RootWindowOfScreen (si->screens[i].screen))
741 fprintf (stderr,"%s: %d: %s on 0x%lx",
742 blurb(), i, type, (unsigned long) window);
744 fprintf (stderr, "\n");
746 fprintf (stderr, " at %d,%d.\n", x, y);
750 /* If any widgets want to handle this event, let them. */
751 dispatch_event (si, &event);
753 /* We got a user event.
754 If we're waiting for the user to become active, this is it.
755 If we're waiting until the user becomes idle, reset the timers
756 (since now we have longer to wait.)
761 (event.xany.type == MotionNotify ||
762 event.xany.type == KeyRelease))
763 /* When we're demoing a single hack, mouse motion doesn't
764 cause deactivation. Only clicks and keypresses do. */
767 /* If we're not demoing, then any activity causes deactivation.
778 #ifdef HAVE_MIT_SAVER_EXTENSION
779 if (event.type == si->mit_saver_ext_event_number)
781 /* This event's number is that of the MIT-SCREEN-SAVER server
782 extension. This extension has one event number, and the event
783 itself contains sub-codes that say what kind of event it was
784 (an "idle" or "not-idle" event.)
786 XScreenSaverNotifyEvent *sevent =
787 (XScreenSaverNotifyEvent *) &event;
788 if (sevent->state == ScreenSaverOn)
792 fprintf (stderr, "%s: MIT ScreenSaverOn event received.\n",
795 /* Get the "real" server window(s) out of the way as soon
797 for (i = 0; i < si->nscreens; i++)
799 saver_screen_info *ssi = &si->screens[i];
800 if (ssi->server_mit_saver_window &&
801 window_exists_p (si->dpy,
802 ssi->server_mit_saver_window))
803 XUnmapWindow (si->dpy, ssi->server_mit_saver_window);
806 if (sevent->kind != ScreenSaverExternal)
809 "%s: ScreenSaverOn event wasn't of type External!\n",
816 else if (sevent->state == ScreenSaverOff)
819 fprintf (stderr, "%s: MIT ScreenSaverOff event received.\n",
826 "%s: unknown MIT-SCREEN-SAVER event %d received!\n",
827 blurb(), sevent->state);
831 #endif /* HAVE_MIT_SAVER_EXTENSION */
834 #ifdef HAVE_SGI_SAVER_EXTENSION
835 if (event.type == (si->sgi_saver_ext_event_number + ScreenSaverStart))
837 /* The SGI SCREEN_SAVER server extension has two event numbers,
838 and this event matches the "idle" event. */
840 fprintf (stderr, "%s: SGI ScreenSaverStart event received.\n",
846 else if (event.type == (si->sgi_saver_ext_event_number +
849 /* The SGI SCREEN_SAVER server extension has two event numbers,
850 and this event matches the "idle" event. */
852 fprintf (stderr, "%s: SGI ScreenSaverEnd event received.\n",
858 #endif /* HAVE_SGI_SAVER_EXTENSION */
860 /* Just some random event. Let the Widgets handle it, if desired. */
861 dispatch_event (si, &event);
867 /* If there's a user event on the queue, swallow it.
868 If we're using a server extension, and the user becomes active, we
869 get the extension event before the user event -- so the keypress or
870 motion or whatever is still on the queue. This makes "unfade" not
871 work, because it sees that event, and bugs out. (This problem
872 doesn't exhibit itself without an extension, because in that case,
873 there's only one event generated by user activity, not two.)
875 if (!until_idle_p && si->locked_p)
876 swallow_unlock_typeahead_events (si, &event);
878 while (XCheckMaskEvent (si->dpy,
879 (KeyPressMask|ButtonPressMask|PointerMotionMask),
884 if (si->check_pointer_timer_id)
886 XtRemoveTimeOut (si->check_pointer_timer_id);
887 si->check_pointer_timer_id = 0;
891 XtRemoveTimeOut (si->timer_id);
895 if (until_idle_p && si->cycle_id) /* no cycle timer when inactive */
903 /* Some crap for dealing with /proc/interrupts.
905 On Linux systems, it's possible to see the hardware interrupt count
906 associated with the keyboard. We can therefore use that as another method
907 of detecting idleness.
909 Why is it a good idea to do this? Because it lets us detect keyboard
910 activity that is not associated with X events. For example, if the user
911 has switched to another virtual console, it's good for xscreensaver to not
912 be running graphics hacks on the (non-visible) X display. The common
913 complaint that checking /proc/interrupts addresses is that the user is
914 playing Quake on a non-X console, and the GL hacks are perceptibly slowing
917 This is tricky for a number of reasons.
919 * First, we must be sure to only do this when running on an X server that
920 is on the local machine (because otherwise, we'd be reacting to the
921 wrong keyboard.) The way we do this is by noting that the $DISPLAY is
922 pointing to display 0 on the local machine. It *could* be that display
923 1 is also on the local machine (e.g., two X servers, each on a different
924 virtual-terminal) but it's also possible that screen 1 is an X terminal,
925 using this machine as the host. So we can't take that chance.
927 * Second, one can only access these interrupt numbers in a completely
928 and utterly brain-damaged way. You would think that one would use an
929 ioctl for this. But no. The ONLY way to get this information is to
930 open the pseudo-file /proc/interrupts AS A FILE, and read the numbers
931 out of it TEXTUALLY. Because this is Unix, and all the world's a file,
932 and the only real data type is the short-line sequence of ASCII bytes.
934 Now it's all well and good that the /proc/interrupts pseudo-file
935 exists; that's a clever idea, and a useful API for things that are
936 already textually oriented, like shell scripts, and users doing
937 interactive debugging sessions. But to make a *C PROGRAM* open a file
938 and parse the textual representation of integers out of it is just
941 * Third, you can't just hold the file open, and fseek() back to the
942 beginning to get updated data! If you do that, the data never changes.
943 And I don't want to call open() every five seconds, because I don't want
944 to risk going to disk for any inodes. It turns out that if you dup()
945 it early, then each copy gets fresh data, so we can get around that in
946 this way (but for how many releases, one might wonder?)
948 * Fourth, the format of the output of the /proc/interrupts file is
949 undocumented, and has changed several times already! In Linux 2.0.33,
950 even on a multiprocessor machine, it looks like this:
955 but on later kernels with MP machines, it looks like this:
958 0: 1671450 1672618 IO-APIC-edge timer
959 1: 13037 13495 IO-APIC-edge keyboard
961 Joy! So how are we expected to parse that? Well, this code doesn't
962 parse it: it saves the last line with the string "keyboard" in it, and
963 does a string-comparison to note when it has changed.
965 Thanks to Nat Friedman <nat@nat.org> for figuring out all of this crap.
967 Note that this only checks for lines with "keyboard" or "PS/2 Mouse" in
968 them. If you have a serial mouse, it won't detect that, it will only detect
969 keyboard activity. That's because there's no way to tell the difference
970 between a serial mouse and a general serial port, and it would be somewhat
971 unfortunate to have the screensaver turn off when the modem on COM1 burped.
975 #ifdef HAVE_PROC_INTERRUPTS
977 #define PROC_INTERRUPTS "/proc/interrupts"
980 query_proc_interrupts_available (saver_info *si, const char **why)
982 /* We can use /proc/interrupts if $DISPLAY points to :0, and if the
983 "/proc/interrupts" file exists and is readable.
988 if (!display_is_on_console_p (si))
990 if (why) *why = "not on primary console";
994 f = fopen (PROC_INTERRUPTS, "r");
1004 proc_interrupts_activity_p (saver_info *si)
1006 static FILE *f0 = 0;
1009 static char last_kbd_line[255] = { 0, };
1010 static char last_ptr_line[255] = { 0, };
1011 char new_line[sizeof(last_kbd_line)];
1012 Bool checked_kbd = False, kbd_changed = False;
1013 Bool checked_ptr = False, ptr_changed = False;
1017 /* First time -- open the file. */
1018 f0 = fopen (PROC_INTERRUPTS, "r");
1022 sprintf(buf, "%s: error opening %s", blurb(), PROC_INTERRUPTS);
1028 if (f0 == (FILE *) -1) /* means we got an error initializing. */
1031 fd = dup (fileno (f0));
1035 sprintf(buf, "%s: could not dup() the %s fd", blurb(), PROC_INTERRUPTS);
1040 f1 = fdopen (fd, "r");
1044 sprintf(buf, "%s: could not fdopen() the %s fd", blurb(),
1050 /* Actually, I'm unclear on why this fseek() is necessary, given the timing
1051 of the dup() above, but it is. */
1052 if (fseek (f1, 0, SEEK_SET) != 0)
1055 sprintf(buf, "%s: error rewinding %s", blurb(), PROC_INTERRUPTS);
1060 /* Now read through the pseudo-file until we find the "keyboard" line. */
1062 while (fgets (new_line, sizeof(new_line)-1, f1))
1064 if (!checked_kbd && strstr (new_line, "keyboard"))
1066 kbd_changed = (*last_kbd_line && !!strcmp (new_line, last_kbd_line));
1067 strcpy (last_kbd_line, new_line);
1070 else if (!checked_ptr && strstr (new_line, "PS/2 Mouse"))
1072 ptr_changed = (*last_ptr_line && !!strcmp (new_line, last_ptr_line));
1073 strcpy (last_ptr_line, new_line);
1077 if (checked_kbd && checked_ptr)
1081 if (checked_kbd || checked_ptr)
1085 if (si->prefs.debug_p && (kbd_changed || ptr_changed))
1086 fprintf (stderr, "%s: /proc/interrupts activity: %s\n",
1088 ((kbd_changed && ptr_changed) ? "mouse and kbd" :
1089 kbd_changed ? "kbd" :
1090 ptr_changed ? "mouse" : "ERR"));
1092 return (kbd_changed || ptr_changed);
1096 /* If we got here, we didn't find either a "keyboard" or a "PS/2 Mouse"
1097 line in the file at all. */
1098 fprintf (stderr, "%s: no keyboard or mouse data in %s?\n",
1099 blurb(), PROC_INTERRUPTS);
1105 if (f0 && f0 != (FILE *) -1)
1112 #endif /* HAVE_PROC_INTERRUPTS */
1115 /* This timer goes off every few minutes, whether the user is idle or not,
1116 to try and clean up anything that has gone wrong.
1118 It calls disable_builtin_screensaver() so that if xset has been used,
1119 or some other program (like xlock) has messed with the XSetScreenSaver()
1120 settings, they will be set back to sensible values (if a server extension
1121 is in use, messing with xlock can cause xscreensaver to never get a wakeup
1122 event, and could cause monitor power-saving to occur, and all manner of
1125 If the screen is currently blanked, it raises the window, in case some
1126 other window has been mapped on top of it.
1128 If the screen is currently blanked, and there is no hack running, it
1129 clears the window, in case there is an error message printed on it (we
1130 don't want the error message to burn in.)
1134 watchdog_timer (XtPointer closure, XtIntervalId *id)
1136 saver_info *si = (saver_info *) closure;
1137 saver_preferences *p = &si->prefs;
1139 disable_builtin_screensaver (si, False);
1141 /* If the DPMS settings on the server have changed, change them back to
1142 what ~/.xscreensaver says they should be. */
1143 sync_server_dpms_settings (si->dpy,
1144 (p->dpms_enabled_p &&
1145 p->mode != DONT_BLANK),
1146 p->dpms_standby / 1000,
1147 p->dpms_suspend / 1000,
1151 if (si->screen_blanked_p)
1153 Bool running_p = screenhack_running_p (si);
1157 if (si->prefs.debug_p)
1158 fprintf (stderr, "%s: dialog box is up: not raising screen.\n",
1163 if (si->prefs.debug_p)
1164 fprintf (stderr, "%s: watchdog timer raising %sscreen.\n",
1165 blurb(), (running_p ? "" : "and clearing "));
1167 raise_window (si, True, True, running_p);
1170 if (screenhack_running_p (si) &&
1171 !monitor_powered_on_p (si))
1173 if (si->prefs.verbose_p)
1175 "%s: X says monitor has powered down; "
1176 "killing running hacks.\n", blurb());
1177 kill_screenhack (si);
1180 /* Re-schedule this timer. The watchdog timer defaults to a bit less
1181 than the hack cycle period, but is never longer than one hour.
1183 si->watchdog_id = 0;
1184 reset_watchdog_timer (si, True);
1190 reset_watchdog_timer (saver_info *si, Bool on_p)
1192 saver_preferences *p = &si->prefs;
1194 if (si->watchdog_id)
1196 XtRemoveTimeOut (si->watchdog_id);
1197 si->watchdog_id = 0;
1200 if (on_p && p->watchdog_timeout)
1202 si->watchdog_id = XtAppAddTimeOut (si->app, p->watchdog_timeout,
1203 watchdog_timer, (XtPointer) si);
1206 fprintf (stderr, "%s: restarting watchdog_timer (%ld, %ld)\n",
1207 blurb(), p->watchdog_timeout, si->watchdog_id);