1 /* timers.c --- detecting when the user is idle, and other timer-related tasks.
2 * xscreensaver, Copyright (c) 1991-2008 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 */
46 #include <X11/extensions/Xrandr.h>
47 #endif /* HAVE_RANDR */
49 #include "xscreensaver.h"
52 #define ABS(x)((x)<0?-(x):(x))
55 #define MAX(x,y)((x)>(y)?(x):(y))
58 #ifdef HAVE_PROC_INTERRUPTS
59 static Bool proc_interrupts_activity_p (saver_info *si);
60 #endif /* HAVE_PROC_INTERRUPTS */
62 static void check_for_clock_skew (saver_info *si);
66 idle_timer (XtPointer closure, XtIntervalId *id)
68 saver_info *si = (saver_info *) closure;
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.
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.
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);
87 /* If we are the timer that just went off, clear the pointer to the id. */
90 if (si->timer_id && *id != si->timer_id)
91 abort(); /* oops, scheduled timer twice?? */
98 schedule_wakeup_event (saver_info *si, Time when, Bool verbose_p)
103 fprintf (stderr, "%s: idle_timer already running\n", blurb());
107 /* Wake up periodically to ask the server if we are idle. */
108 si->timer_id = XtAppAddTimeOut (si->app, when, idle_timer,
112 fprintf (stderr, "%s: starting idle_timer (%ld, %ld)\n",
113 blurb(), when, si->timer_id);
118 notice_events (saver_info *si, Window window, Bool top_p)
120 saver_preferences *p = &si->prefs;
121 XWindowAttributes attrs;
122 unsigned long events;
123 Window root, parent, *kids;
127 if (XtWindowToWidget (si->dpy, window))
128 /* If it's one of ours, don't mess up its event mask. */
131 if (!XQueryTree (si->dpy, window, &root, &parent, &kids, &nkids))
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))
141 XGetWindowAttributes (si->dpy, window, &attrs);
142 events = ((attrs.all_event_masks | attrs.do_not_propagate_mask)
145 /* Select for SubstructureNotify on all windows.
146 Select for KeyPress on all windows that already have it selected.
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
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.
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.
164 XSelectInput (si->dpy, window, SubstructureNotifyMask | events);
166 if (top_p && p->debug_p && (events & KeyPressMask))
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);
177 notice_events (si, kids [--nkids], top_p);
178 XFree ((char *) kids);
184 BadWindow_ehandler (Display *dpy, XErrorEvent *error)
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.
191 if (error->error_code == BadWindow ||
192 error->error_code == BadMatch ||
193 error->error_code == BadDrawable)
196 return saver_ehandler (dpy, error);
200 struct notice_events_timer_arg {
206 notice_events_timer (XtPointer closure, XtIntervalId *id)
208 struct notice_events_timer_arg *arg =
209 (struct notice_events_timer_arg *) closure;
211 XErrorHandler old_handler = XSetErrorHandler (BadWindow_ehandler);
213 saver_info *si = arg->si;
214 Window window = arg->w;
217 notice_events (si, window, True);
218 XSync (si->dpy, False);
219 XSetErrorHandler (old_handler);
223 start_notice_events_timer (saver_info *si, Window w, Bool verbose_p)
225 saver_preferences *p = &si->prefs;
226 struct notice_events_timer_arg *arg =
227 (struct notice_events_timer_arg *) malloc(sizeof(*arg));
230 XtAppAddTimeOut (si->app, p->notice_events_timeout, notice_events_timer,
234 fprintf (stderr, "%s: starting notice_events_timer for 0x%X (%lu)\n",
235 blurb(), (unsigned int) w, p->notice_events_timeout);
239 /* When the screensaver is active, this timer will periodically change
243 cycle_timer (XtPointer closure, XtIntervalId *id)
245 saver_info *si = (saver_info *) closure;
246 saver_preferences *p = &si->prefs;
247 Time how_long = p->cycle;
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;
261 fprintf (stderr, "%s: dialog box up; delaying hack change.\n",
263 how_long = 30000; /* 30 secs */
268 maybe_reload_init_file (si);
269 for (i = 0; i < si->nscreens; i++)
270 kill_screenhack (&si->screens[i]);
272 raise_window (si, True, True, False);
274 if (!si->throttled_p)
275 for (i = 0; i < si->nscreens; i++)
276 spawn_screenhack (&si->screens[i]);
280 fprintf (stderr, "%s: not launching new hack (throttled.)\n",
287 si->cycle_id = XtAppAddTimeOut (si->app, how_long, cycle_timer,
291 fprintf (stderr, "%s: starting cycle_timer (%ld, %ld)\n",
292 blurb(), how_long, si->cycle_id);
297 fprintf (stderr, "%s: not starting cycle_timer: how_long == %ld\n",
298 blurb(), (unsigned long) how_long);
304 activate_lock_timer (XtPointer closure, XtIntervalId *id)
306 saver_info *si = (saver_info *) closure;
307 saver_preferences *p = &si->prefs;
310 fprintf (stderr, "%s: timed out; activating lock.\n", blurb());
311 set_locked_p (si, True);
315 /* Call this when user activity (or "simulated" activity) has been noticed.
318 reset_timers (saver_info *si)
320 saver_preferences *p = &si->prefs;
321 if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
327 fprintf (stderr, "%s: killing idle_timer (%ld, %ld)\n",
328 blurb(), p->timeout, si->timer_id);
329 XtRemoveTimeOut (si->timer_id);
333 schedule_wakeup_event (si, p->timeout, p->debug_p); /* sets si->timer_id */
335 if (si->cycle_id) abort (); /* no cycle timer when inactive */
337 si->last_activity_time = time ((time_t *) 0);
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. */
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.
348 XForceScreenSaver (si->dpy, ScreenSaverReset);
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);
358 /* Returns true if the mouse has moved since the last time we checked.
359 Small motions (of less than "hysteresis" pixels/second) are ignored.
362 pointer_moved_p (saver_screen_info *ssi, Bool mods_p)
364 saver_info *si = ssi->global;
365 saver_preferences *p = &si->prefs;
368 int root_x, root_y, x, y;
370 time_t now = time ((time_t *) 0);
371 unsigned int distance, dps;
372 unsigned long seconds = 0;
373 Bool moved_p = False;
375 /* don't check xinerama pseudo-screens. */
376 if (!ssi->real_screen_p) return False;
378 if (!XQueryPointer (si->dpy, ssi->screensaver_window, &root, &child,
379 &root_x, &root_y, &x, &y, &mask))
381 /* If XQueryPointer() returns false, the mouse is not on this screen.
389 distance = MAX (ABS (ssi->poll_mouse_last_root_x - root_x),
390 ABS (ssi->poll_mouse_last_root_y - root_y));
391 seconds = (now - ssi->poll_mouse_last_time);
394 /* When the screen is blanked, we get MotionNotify events, but when not
395 blanked, we poll only every 5 seconds, and that's not enough resolution
396 to do hysteresis based on a 1 second interval. So, assume that any
397 motion we've seen during the 5 seconds when our eyes were closed happened
398 in the last 1 second instead.
400 if (seconds > 1) seconds = 1;
402 dps = (seconds <= 0 ? distance : (distance / seconds));
404 /* Motion only counts if the rate is more than N pixels per second.
406 if (dps >= p->pointer_hysteresis &&
410 /* If the mouse is not on this screen but used to be, that's motion.
411 If the mouse was not on this screen, but is now, that's motion.
414 Bool on_screen_p = (root_x != -1 && root_y != -1);
415 Bool was_on_screen_p = (ssi->poll_mouse_last_root_x != -1 &&
416 ssi->poll_mouse_last_root_y != -1);
418 if (on_screen_p != was_on_screen_p)
422 if (p->debug_p && (distance != 0 || moved_p))
424 fprintf (stderr, "%s: %d: pointer %s", blurb(), ssi->number,
425 (moved_p ? "moved: " : "ignored:"));
426 if (ssi->poll_mouse_last_root_x == -1)
427 fprintf (stderr, "off screen");
429 fprintf (stderr, "%d,%d",
430 ssi->poll_mouse_last_root_x,
431 ssi->poll_mouse_last_root_y);
432 fprintf (stderr, " -> ");
434 fprintf (stderr, "off screen");
436 fprintf (stderr, "%d,%d", root_x, root_y);
437 if (ssi->poll_mouse_last_root_x != -1 && root_x != -1)
438 fprintf (stderr, " (%d,%d; %d/%lu=%d)",
439 ABS(ssi->poll_mouse_last_root_x - root_x),
440 ABS(ssi->poll_mouse_last_root_y - root_y),
441 distance, seconds, dps);
443 fprintf (stderr, ".\n");
448 mask != ssi->poll_mouse_last_mask)
453 fprintf (stderr, "%s: %d: modifiers changed: 0x%04x -> 0x%04x.\n",
454 blurb(), ssi->number, ssi->poll_mouse_last_mask, mask);
457 si->last_activity_screen = ssi;
458 ssi->poll_mouse_last_child = child;
459 ssi->poll_mouse_last_mask = mask;
461 if (moved_p || seconds > 0)
463 ssi->poll_mouse_last_time = now;
464 ssi->poll_mouse_last_root_x = root_x;
465 ssi->poll_mouse_last_root_y = root_y;
472 /* When we aren't using a server extension, this timer is used to periodically
473 wake up and poll the mouse position, which is possibly more reliable than
474 selecting motion events on every window.
477 check_pointer_timer (XtPointer closure, XtIntervalId *id)
480 saver_info *si = (saver_info *) closure;
481 saver_preferences *p = &si->prefs;
482 Bool active_p = False;
484 if (!si->using_proc_interrupts &&
485 (si->using_xidle_extension ||
486 si->using_mit_saver_extension ||
487 si->using_sgi_saver_extension))
488 /* If an extension is in use, we should not be polling the mouse.
489 Unless we're also checking /proc/interrupts, in which case, we should.
493 if (id && *id == si->check_pointer_timer_id) /* this is us - it's expired */
494 si->check_pointer_timer_id = 0;
496 if (si->check_pointer_timer_id) /* only queue one at a time */
497 XtRemoveTimeOut (si->check_pointer_timer_id);
499 si->check_pointer_timer_id = /* now re-queue */
500 XtAppAddTimeOut (si->app, p->pointer_timeout, check_pointer_timer,
503 for (i = 0; i < si->nscreens; i++)
505 saver_screen_info *ssi = &si->screens[i];
506 if (pointer_moved_p (ssi, True))
510 #ifdef HAVE_PROC_INTERRUPTS
512 si->using_proc_interrupts &&
513 proc_interrupts_activity_p (si))
517 #endif /* HAVE_PROC_INTERRUPTS */
522 check_for_clock_skew (si);
526 /* An unfortunate situation is this: the saver is not active, because the
527 user has been typing. The machine is a laptop. The user closes the lid
528 and suspends it. The CPU halts. Some hours later, the user opens the
529 lid. At this point, Xt's timers will fire, and xscreensaver will blank
532 So far so good -- well, not really, but it's the best that we can do,
533 since the OS doesn't send us a signal *before* shutdown -- but if the
534 user had delayed locking (lockTimeout > 0) then we should start off
535 in the locked state, rather than only locking N minutes from when the
536 lid was opened. Also, eschewing fading is probably a good idea, to
537 clamp down as soon as possible.
539 We only do this when we'd be polling the mouse position anyway.
540 This amounts to an assumption that machines with APM support also
541 have /proc/interrupts.
544 check_for_clock_skew (saver_info *si)
546 saver_preferences *p = &si->prefs;
547 time_t now = time ((time_t *) 0);
548 long shift = now - si->last_wall_clock_time;
552 int i = (si->last_wall_clock_time == 0 ? 0 : shift);
554 "%s: checking wall clock for hibernation (%d:%02d:%02d).\n",
556 (i / (60 * 60)), ((i / 60) % 60), (i % 60));
559 if (si->last_wall_clock_time != 0 &&
560 shift > (p->timeout / 1000))
563 fprintf (stderr, "%s: wall clock has jumped by %ld:%02ld:%02ld!\n",
565 (shift / (60 * 60)), ((shift / 60) % 60), (shift % 60));
567 si->emergency_lock_p = True;
568 idle_timer ((XtPointer) si, 0);
571 si->last_wall_clock_time = now;
577 dispatch_event (saver_info *si, XEvent *event)
579 /* If this is for the splash dialog, pass it along.
580 Note that the password dialog is handled with its own event loop,
581 so events for that window will never come through here.
583 if (si->splash_dialog && event->xany.window == si->splash_dialog)
584 handle_splash_event (si, event);
586 XtDispatchEvent (event);
591 swallow_unlock_typeahead_events (saver_info *si, XEvent *e)
597 memset (buf, 0, sizeof(buf));
603 if (event.xany.type == KeyPress)
606 int size = XLookupString ((XKeyEvent *) &event, s, 1, 0, 0);
607 if (size != 1) continue;
610 case '\010': case '\177': /* Backspace */
613 case '\025': case '\030': /* Erase line */
614 case '\012': case '\015': /* Enter */
617 case '\040': /* Space */
619 break; /* ignore space at beginning of line */
620 /* else, fall through */
627 } while (i < sizeof(buf)-1 &&
628 XCheckMaskEvent (si->dpy, KeyPressMask, &event));
632 if (si->unlock_typeahead)
634 memset (si->unlock_typeahead, 0, strlen(si->unlock_typeahead));
635 free (si->unlock_typeahead);
639 si->unlock_typeahead = strdup (buf);
641 si->unlock_typeahead = 0;
643 memset (buf, 0, sizeof(buf));
647 /* methods of detecting idleness:
649 explicitly informed by SGI SCREEN_SAVER server event;
650 explicitly informed by MIT-SCREEN-SAVER server event;
651 poll server idle time with XIDLE extension;
652 select events on all windows, and note absence of recent events;
653 note that /proc/interrupts has not changed in a while;
654 activated by clientmessage.
656 methods of detecting non-idleness:
658 read events on the xscreensaver window;
659 explicitly informed by SGI SCREEN_SAVER server event;
660 explicitly informed by MIT-SCREEN-SAVER server event;
661 select events on all windows, and note events on any of them;
662 note that /proc/interrupts has changed;
663 deactivated by clientmessage.
665 I trust that explains why this function is a big hairy mess.
668 sleep_until_idle (saver_info *si, Bool until_idle_p)
670 saver_preferences *p = &si->prefs;
673 /* We need to select events on all windows if we're not using any extensions.
674 Otherwise, we don't need to. */
675 Bool scanning_all_windows = !(si->using_xidle_extension ||
676 si->using_mit_saver_extension ||
677 si->using_sgi_saver_extension);
679 /* We need to periodically wake up and check for idleness if we're not using
680 any extensions, or if we're using the XIDLE extension. The other two
681 extensions explicitly deliver events when we go idle/non-idle, so we
682 don't need to poll. */
683 Bool polling_for_idleness = !(si->using_mit_saver_extension ||
684 si->using_sgi_saver_extension);
686 /* Whether we need to periodically wake up and check to see if the mouse has
687 moved. We only need to do this when not using any extensions. The reason
688 this isn't the same as `polling_for_idleness' is that the "idleness" poll
689 can happen (for example) 5 minutes from now, whereas the mouse-position
690 poll should happen with low periodicity. We don't need to poll the mouse
691 position with the XIDLE extension, but we do need to periodically wake up
692 and query the server with that extension. For our purposes, polling
693 /proc/interrupts is just like polling the mouse position. It has to
694 happen on the same kind of schedule. */
695 Bool polling_mouse_position = (si->using_proc_interrupts ||
696 !(si->using_xidle_extension ||
697 si->using_mit_saver_extension ||
698 si->using_sgi_saver_extension));
702 if (polling_for_idleness)
703 /* This causes a no-op event to be delivered to us in a while, so that
704 we come back around through the event loop again. */
705 schedule_wakeup_event (si, p->timeout, p->debug_p);
707 if (polling_mouse_position)
708 /* Check to see if the mouse has moved, and set up a repeating timer
709 to do so periodically (typically, every 5 seconds.) */
710 check_pointer_timer ((XtPointer) si, 0);
715 XtAppNextEvent (si->app, &event);
717 switch (event.xany.type) {
718 case 0: /* our synthetic "timeout" event has been signalled */
723 /* We may be idle; check one last time to see if the mouse has
724 moved, just in case the idle-timer went off within the 5 second
725 window between mouse polling. If the mouse has moved, then
726 check_pointer_timer() will reset last_activity_time.
728 if (polling_mouse_position)
729 check_pointer_timer ((XtPointer) si, 0);
731 #ifdef HAVE_XIDLE_EXTENSION
732 if (si->using_xidle_extension)
734 /* The XIDLE extension uses the synthetic event to prod us into
735 re-asking the server how long the user has been idle. */
736 if (! XGetIdleTime (si->dpy, &idle))
738 fprintf (stderr, "%s: XGetIdleTime() failed.\n", blurb());
739 saver_exit (si, 1, 0);
743 #endif /* HAVE_XIDLE_EXTENSION */
744 #ifdef HAVE_MIT_SAVER_EXTENSION
745 if (si->using_mit_saver_extension)
747 /* We don't need to do anything in this case - the synthetic
748 event isn't necessary, as we get sent specific events
749 to wake us up. In fact, this event generally shouldn't
750 be being delivered when the MIT extension is in use. */
754 #endif /* HAVE_MIT_SAVER_EXTENSION */
755 #ifdef HAVE_SGI_SAVER_EXTENSION
756 if (si->using_sgi_saver_extension)
758 /* We don't need to do anything in this case - the synthetic
759 event isn't necessary, as we get sent specific events
760 to wake us up. In fact, this event generally shouldn't
761 be being delivered when the SGI extension is in use. */
765 #endif /* HAVE_SGI_SAVER_EXTENSION */
767 /* Otherwise, no server extension is in use. The synthetic
768 event was to tell us to wake up and see if the user is now
769 idle. Compute the amount of idle time by comparing the
770 `last_activity_time' to the wall clock. The l_a_t was set
771 by calling `reset_timers()', which is called only in only
772 two situations: when polling the mouse position has revealed
773 the the mouse has moved (user activity) or when we have read
774 an event (again, user activity.)
776 idle = 1000 * (si->last_activity_time - time ((time_t *) 0));
779 if (idle >= p->timeout)
781 /* Look, we've been idle long enough. We're done. */
784 else if (si->emergency_lock_p)
786 /* Oops, the wall clock has jumped far into the future, so
787 we need to lock down in a hurry! */
792 /* The event went off, but it turns out that the user has not
793 yet been idle for long enough. So re-signal the event.
794 Be economical: if we should blank after 5 minutes, and the
795 user has been idle for 2 minutes, then set this timer to
798 if (polling_for_idleness)
799 schedule_wakeup_event (si, p->timeout - idle, p->debug_p);
805 if (handle_clientmessage (si, &event, until_idle_p))
810 /* A window has been created on the screen somewhere. If we're
811 supposed to scan all windows for events, prepare this window. */
812 if (scanning_all_windows)
814 Window w = event.xcreatewindow.window;
815 start_notice_events_timer (si, w, p->debug_p);
827 Window root=0, window=0;
829 const char *type = 0;
830 if (event.xany.type == MotionNotify)
832 /*type = "MotionNotify";*/
833 root = event.xmotion.root;
834 window = event.xmotion.window;
835 x = event.xmotion.x_root;
836 y = event.xmotion.y_root;
838 else if (event.xany.type == KeyPress)
841 root = event.xkey.root;
842 window = event.xkey.window;
845 else if (event.xany.type == ButtonPress)
847 type = "ButtonPress";
848 root = event.xkey.root;
849 window = event.xkey.window;
850 x = event.xmotion.x_root;
851 y = event.xmotion.y_root;
857 for (i = 0; i < si->nscreens; i++)
858 if (root == RootWindowOfScreen (si->screens[i].screen))
860 fprintf (stderr,"%s: %d: %s on 0x%lx",
861 blurb(), i, type, (unsigned long) window);
863 /* Be careful never to do this unless in -debug mode, as
864 this could expose characters from the unlock password. */
865 if (p->debug_p && event.xany.type == KeyPress)
869 XLookupString (&event.xkey, &c, 1, &keysym, 0);
870 fprintf (stderr, " (%s%s)",
871 (event.xkey.send_event ? "synthetic " : ""),
872 XKeysymToString (keysym));
876 fprintf (stderr, "\n");
878 fprintf (stderr, " at %d,%d.\n", x, y);
882 /* If any widgets want to handle this event, let them. */
883 dispatch_event (si, &event);
886 /* If we got a MotionNotify event, figure out what screen it
887 was on and poll the mouse there: if the mouse hasn't moved
888 far enough to count as "real" motion, then ignore this
891 if (event.xany.type == MotionNotify)
894 for (i = 0; i < si->nscreens; i++)
895 if (event.xmotion.root ==
896 RootWindowOfScreen (si->screens[i].screen))
898 if (i < si->nscreens)
900 if (!pointer_moved_p (&si->screens[i], False))
906 /* We got a user event.
907 If we're waiting for the user to become active, this is it.
908 If we're waiting until the user becomes idle, reset the timers
909 (since now we have longer to wait.)
914 (event.xany.type == MotionNotify ||
915 event.xany.type == KeyRelease))
916 /* When we're demoing a single hack, mouse motion doesn't
917 cause deactivation. Only clicks and keypresses do. */
920 /* If we're not demoing, then any activity causes deactivation.
931 #ifdef HAVE_MIT_SAVER_EXTENSION
932 if (event.type == si->mit_saver_ext_event_number)
934 /* This event's number is that of the MIT-SCREEN-SAVER server
935 extension. This extension has one event number, and the event
936 itself contains sub-codes that say what kind of event it was
937 (an "idle" or "not-idle" event.)
939 XScreenSaverNotifyEvent *sevent =
940 (XScreenSaverNotifyEvent *) &event;
941 if (sevent->state == ScreenSaverOn)
945 fprintf (stderr, "%s: MIT ScreenSaverOn event received.\n",
948 /* Get the "real" server window(s) out of the way as soon
950 for (i = 0; i < si->nscreens; i++)
952 saver_screen_info *ssi = &si->screens[i];
953 if (ssi->server_mit_saver_window &&
954 window_exists_p (si->dpy,
955 ssi->server_mit_saver_window))
956 XUnmapWindow (si->dpy, ssi->server_mit_saver_window);
959 if (sevent->kind != ScreenSaverExternal)
962 "%s: ScreenSaverOn event wasn't of type External!\n",
969 else if (sevent->state == ScreenSaverOff)
972 fprintf (stderr, "%s: MIT ScreenSaverOff event received.\n",
979 "%s: unknown MIT-SCREEN-SAVER event %d received!\n",
980 blurb(), sevent->state);
984 #endif /* HAVE_MIT_SAVER_EXTENSION */
987 #ifdef HAVE_SGI_SAVER_EXTENSION
988 if (event.type == (si->sgi_saver_ext_event_number + ScreenSaverStart))
990 /* The SGI SCREEN_SAVER server extension has two event numbers,
991 and this event matches the "idle" event. */
993 fprintf (stderr, "%s: SGI ScreenSaverStart event received.\n",
999 else if (event.type == (si->sgi_saver_ext_event_number +
1002 /* The SGI SCREEN_SAVER server extension has two event numbers,
1003 and this event matches the "idle" event. */
1005 fprintf (stderr, "%s: SGI ScreenSaverEnd event received.\n",
1011 #endif /* HAVE_SGI_SAVER_EXTENSION */
1014 if (event.type == (si->randr_event_number + RRScreenChangeNotify))
1016 /* The Resize and Rotate extension sends an event when the
1017 size, rotation, or refresh rate of any screen has changed.
1019 XRRScreenChangeNotifyEvent *xrr_event =
1020 (XRRScreenChangeNotifyEvent *) &event;
1024 /* XRRRootToScreen is in Xrandr.h 1.4, 2001/06/07 */
1025 int screen = XRRRootToScreen (si->dpy, xrr_event->window);
1026 fprintf (stderr, "%s: %d: screen change event received\n",
1030 # ifdef RRScreenChangeNotifyMask
1031 /* Inform Xlib that it's ok to update its data structures. */
1032 XRRUpdateConfiguration (&event); /* Xrandr.h 1.9, 2002/09/29 */
1033 # endif /* RRScreenChangeNotifyMask */
1035 /* Resize the existing xscreensaver windows and cached ssi data. */
1036 if (update_screen_layout (si))
1040 fprintf (stderr, "%s: new layout:\n", blurb());
1041 describe_monitor_layout (si);
1043 resize_screensaver_window (si);
1047 #endif /* HAVE_RANDR */
1049 /* Just some random event. Let the Widgets handle it, if desired. */
1050 dispatch_event (si, &event);
1056 /* If there's a user event on the queue, swallow it.
1057 If we're using a server extension, and the user becomes active, we
1058 get the extension event before the user event -- so the keypress or
1059 motion or whatever is still on the queue. This makes "unfade" not
1060 work, because it sees that event, and bugs out. (This problem
1061 doesn't exhibit itself without an extension, because in that case,
1062 there's only one event generated by user activity, not two.)
1064 if (!until_idle_p && si->locked_p)
1065 swallow_unlock_typeahead_events (si, &event);
1067 while (XCheckMaskEvent (si->dpy,
1068 (KeyPressMask|ButtonPressMask|PointerMotionMask),
1073 if (si->check_pointer_timer_id)
1075 XtRemoveTimeOut (si->check_pointer_timer_id);
1076 si->check_pointer_timer_id = 0;
1080 XtRemoveTimeOut (si->timer_id);
1084 if (until_idle_p && si->cycle_id) /* no cycle timer when inactive */
1092 /* Some crap for dealing with /proc/interrupts.
1094 On Linux systems, it's possible to see the hardware interrupt count
1095 associated with the keyboard. We can therefore use that as another method
1096 of detecting idleness.
1098 Why is it a good idea to do this? Because it lets us detect keyboard
1099 activity that is not associated with X events. For example, if the user
1100 has switched to another virtual console, it's good for xscreensaver to not
1101 be running graphics hacks on the (non-visible) X display. The common
1102 complaint that checking /proc/interrupts addresses is that the user is
1103 playing Quake on a non-X console, and the GL hacks are perceptibly slowing
1106 This is tricky for a number of reasons.
1108 * First, we must be sure to only do this when running on an X server that
1109 is on the local machine (because otherwise, we'd be reacting to the
1110 wrong keyboard.) The way we do this is by noting that the $DISPLAY is
1111 pointing to display 0 on the local machine. It *could* be that display
1112 1 is also on the local machine (e.g., two X servers, each on a different
1113 virtual-terminal) but it's also possible that screen 1 is an X terminal,
1114 using this machine as the host. So we can't take that chance.
1116 * Second, one can only access these interrupt numbers in a completely
1117 and utterly brain-damaged way. You would think that one would use an
1118 ioctl for this. But no. The ONLY way to get this information is to
1119 open the pseudo-file /proc/interrupts AS A FILE, and read the numbers
1120 out of it TEXTUALLY. Because this is Unix, and all the world's a file,
1121 and the only real data type is the short-line sequence of ASCII bytes.
1123 Now it's all well and good that the /proc/interrupts pseudo-file
1124 exists; that's a clever idea, and a useful API for things that are
1125 already textually oriented, like shell scripts, and users doing
1126 interactive debugging sessions. But to make a *C PROGRAM* open a file
1127 and parse the textual representation of integers out of it is just
1130 * Third, you can't just hold the file open, and fseek() back to the
1131 beginning to get updated data! If you do that, the data never changes.
1132 And I don't want to call open() every five seconds, because I don't want
1133 to risk going to disk for any inodes. It turns out that if you dup()
1134 it early, then each copy gets fresh data, so we can get around that in
1135 this way (but for how many releases, one might wonder?)
1137 * Fourth, the format of the output of the /proc/interrupts file is
1138 undocumented, and has changed several times already! In Linux 2.0.33,
1139 even on a multiprocessor machine, it looks like this:
1144 but in Linux 2.2 and 2.4 kernels with MP machines, it looks like this:
1147 0: 1671450 1672618 IO-APIC-edge timer
1148 1: 13037 13495 IO-APIC-edge keyboard
1150 and in Linux 2.6, it's gotten even goofier: now there are two lines
1151 labelled "i8042". One of them is the keyboard, and one of them is
1152 the PS/2 mouse -- and of course, you can't tell them apart, except
1153 by wiggling the mouse and noting which one changes:
1156 1: 32051 30864 IO-APIC-edge i8042
1157 12: 476577 479913 IO-APIC-edge i8042
1159 Joy! So how are we expected to parse that? Well, this code doesn't
1160 parse it: it saves the first line with the string "keyboard" (or
1161 "i8042") in it, and does a string-comparison to note when it has
1162 changed. If there are two "i8042" lines, we assume the first is
1163 the keyboard and the second is the mouse (doesn't matter which is
1164 which, really, as long as we don't compare them against each other.)
1166 Thanks to Nat Friedman <nat@nat.org> for figuring out most of this crap.
1168 Note that if you have a serial or USB mouse, or a USB keyboard, it won't
1169 detect it. That's because there's no way to tell the difference between a
1170 serial mouse and a general serial port, and all USB devices look the same
1171 from here. It would be somewhat unfortunate to have the screensaver turn
1172 off when the modem on COM1 burped, or when a USB disk was accessed.
1176 #ifdef HAVE_PROC_INTERRUPTS
1178 #define PROC_INTERRUPTS "/proc/interrupts"
1181 query_proc_interrupts_available (saver_info *si, const char **why)
1183 /* We can use /proc/interrupts if $DISPLAY points to :0, and if the
1184 "/proc/interrupts" file exists and is readable.
1189 if (!display_is_on_console_p (si))
1191 if (why) *why = "not on primary console";
1195 f = fopen (PROC_INTERRUPTS, "r");
1198 if (why) *why = "does not exist";
1208 proc_interrupts_activity_p (saver_info *si)
1210 static FILE *f0 = 0;
1213 static char last_kbd_line[255] = { 0, };
1214 static char last_ptr_line[255] = { 0, };
1215 char new_line[sizeof(last_kbd_line)];
1216 Bool checked_kbd = False, kbd_changed = False;
1217 Bool checked_ptr = False, ptr_changed = False;
1218 int i8042_count = 0;
1222 /* First time -- open the file. */
1223 f0 = fopen (PROC_INTERRUPTS, "r");
1227 sprintf(buf, "%s: error opening %s", blurb(), PROC_INTERRUPTS);
1233 if (f0 == (FILE *) -1) /* means we got an error initializing. */
1236 fd = dup (fileno (f0));
1240 sprintf(buf, "%s: could not dup() the %s fd", blurb(), PROC_INTERRUPTS);
1245 f1 = fdopen (fd, "r");
1249 sprintf(buf, "%s: could not fdopen() the %s fd", blurb(),
1255 /* Actually, I'm unclear on why this fseek() is necessary, given the timing
1256 of the dup() above, but it is. */
1257 if (fseek (f1, 0, SEEK_SET) != 0)
1260 sprintf(buf, "%s: error rewinding %s", blurb(), PROC_INTERRUPTS);
1265 /* Now read through the pseudo-file until we find the "keyboard",
1266 "PS/2 mouse", or "i8042" lines. */
1268 while (fgets (new_line, sizeof(new_line)-1, f1))
1270 Bool i8042_p = !!strstr (new_line, "i8042");
1271 if (i8042_p) i8042_count++;
1273 if (strchr (new_line, ','))
1275 /* Ignore any line that has a comma on it: this is because
1278 12: 930935 XT-PIC usb-uhci, PS/2 Mouse
1280 is really bad news. It *looks* like we can note mouse
1281 activity from that line, but really, that interrupt gets
1282 fired any time any USB device has activity! So we have
1283 to ignore any shared IRQs.
1286 else if (!checked_kbd &&
1287 (strstr (new_line, "keyboard") ||
1288 (i8042_p && i8042_count == 1)))
1290 /* Assume the keyboard interrupt is the line that says "keyboard",
1291 or the *first* line that says "i8042".
1293 kbd_changed = (*last_kbd_line && !!strcmp (new_line, last_kbd_line));
1294 strcpy (last_kbd_line, new_line);
1297 else if (!checked_ptr &&
1298 (strstr (new_line, "PS/2 Mouse") ||
1299 (i8042_p && i8042_count == 2)))
1301 /* Assume the mouse interrupt is the line that says "PS/2 mouse",
1302 or the *second* line that says "i8042".
1304 ptr_changed = (*last_ptr_line && !!strcmp (new_line, last_ptr_line));
1305 strcpy (last_ptr_line, new_line);
1309 if (checked_kbd && checked_ptr)
1313 if (checked_kbd || checked_ptr)
1317 if (si->prefs.debug_p && (kbd_changed || ptr_changed))
1318 fprintf (stderr, "%s: /proc/interrupts activity: %s\n",
1320 ((kbd_changed && ptr_changed) ? "mouse and kbd" :
1321 kbd_changed ? "kbd" :
1322 ptr_changed ? "mouse" : "ERR"));
1324 return (kbd_changed || ptr_changed);
1328 /* If we got here, we didn't find either a "keyboard" or a "PS/2 Mouse"
1329 line in the file at all. */
1330 fprintf (stderr, "%s: no keyboard or mouse data in %s?\n",
1331 blurb(), PROC_INTERRUPTS);
1337 if (f0 && f0 != (FILE *) -1)
1344 #endif /* HAVE_PROC_INTERRUPTS */
1347 /* This timer goes off every few minutes, whether the user is idle or not,
1348 to try and clean up anything that has gone wrong.
1350 It calls disable_builtin_screensaver() so that if xset has been used,
1351 or some other program (like xlock) has messed with the XSetScreenSaver()
1352 settings, they will be set back to sensible values (if a server extension
1353 is in use, messing with xlock can cause xscreensaver to never get a wakeup
1354 event, and could cause monitor power-saving to occur, and all manner of
1357 If the screen is currently blanked, it raises the window, in case some
1358 other window has been mapped on top of it.
1360 If the screen is currently blanked, and there is no hack running, it
1361 clears the window, in case there is an error message printed on it (we
1362 don't want the error message to burn in.)
1366 watchdog_timer (XtPointer closure, XtIntervalId *id)
1368 saver_info *si = (saver_info *) closure;
1369 saver_preferences *p = &si->prefs;
1371 disable_builtin_screensaver (si, False);
1373 /* If the DPMS settings on the server have changed, change them back to
1374 what ~/.xscreensaver says they should be. */
1375 sync_server_dpms_settings (si->dpy,
1376 (p->dpms_enabled_p &&
1377 p->mode != DONT_BLANK),
1378 p->dpms_standby / 1000,
1379 p->dpms_suspend / 1000,
1383 if (si->screen_blanked_p)
1385 Bool running_p = screenhack_running_p (si);
1389 if (si->prefs.debug_p)
1390 fprintf (stderr, "%s: dialog box is up: not raising screen.\n",
1395 if (si->prefs.debug_p)
1396 fprintf (stderr, "%s: watchdog timer raising %sscreen.\n",
1397 blurb(), (running_p ? "" : "and clearing "));
1399 raise_window (si, True, True, running_p);
1402 if (screenhack_running_p (si) &&
1403 !monitor_powered_on_p (si))
1406 if (si->prefs.verbose_p)
1408 "%s: X says monitor has powered down; "
1409 "killing running hacks.\n", blurb());
1410 for (i = 0; i < si->nscreens; i++)
1411 kill_screenhack (&si->screens[i]);
1414 /* Re-schedule this timer. The watchdog timer defaults to a bit less
1415 than the hack cycle period, but is never longer than one hour.
1417 si->watchdog_id = 0;
1418 reset_watchdog_timer (si, True);
1424 reset_watchdog_timer (saver_info *si, Bool on_p)
1426 saver_preferences *p = &si->prefs;
1428 if (si->watchdog_id)
1430 XtRemoveTimeOut (si->watchdog_id);
1431 si->watchdog_id = 0;
1434 if (on_p && p->watchdog_timeout)
1436 si->watchdog_id = XtAppAddTimeOut (si->app, p->watchdog_timeout,
1437 watchdog_timer, (XtPointer) si);
1440 fprintf (stderr, "%s: restarting watchdog_timer (%ld, %ld)\n",
1441 blurb(), p->watchdog_timeout, si->watchdog_id);
1446 /* It's possible that a race condition could have led to the saver
1447 window being unexpectedly still mapped. This can happen like so:
1451 - that hack tries to grab a screen image (it does this by
1452 first unmapping the saver window, then remapping it.)
1453 - hack unmaps window
1455 - user becomes active
1456 - hack re-maps window (*)
1457 - driver kills subprocess
1458 - driver unmaps window (**)
1460 The race is that (*) might have been sent to the server before
1461 the client process was killed, but, due to scheduling randomness,
1462 might not have been received by the server until after (**).
1463 In other words, (*) and (**) might happen out of order, meaning
1464 the driver will unmap the window, and then after that, the
1465 recently-dead client will re-map it. This leaves the user
1466 locked out (it looks like a desktop, but it's not!)
1468 To avoid this: after un-blanking the screen, we launch a timer
1469 that wakes up once a second for ten seconds, and makes damned
1470 sure that the window is still unmapped.
1474 de_race_timer (XtPointer closure, XtIntervalId *id)
1476 saver_info *si = (saver_info *) closure;
1477 saver_preferences *p = &si->prefs;
1480 if (id == 0) /* if id is 0, this is the initialization call. */
1482 si->de_race_ticks = 10;
1484 fprintf (stderr, "%s: starting de-race timer (%d seconds.)\n",
1485 blurb(), si->de_race_ticks);
1490 XSync (si->dpy, False);
1491 for (i = 0; i < si->nscreens; i++)
1493 saver_screen_info *ssi = &si->screens[i];
1494 Window w = ssi->screensaver_window;
1495 XWindowAttributes xgwa;
1496 XGetWindowAttributes (si->dpy, w, &xgwa);
1497 if (xgwa.map_state != IsUnmapped)
1501 "%s: %d: client race! emergency unmap 0x%lx.\n",
1502 blurb(), i, (unsigned long) w);
1503 XUnmapWindow (si->dpy, w);
1505 else if (p->debug_p)
1506 fprintf (stderr, "%s: %d: (de-race of 0x%lx is cool.)\n",
1507 blurb(), i, (unsigned long) w);
1509 XSync (si->dpy, False);
1511 si->de_race_ticks--;
1514 if (id && *id == si->de_race_id)
1517 if (si->de_race_id) abort();
1519 if (si->de_race_ticks <= 0)
1523 fprintf (stderr, "%s: de-race completed.\n", blurb());
1527 si->de_race_id = XtAppAddTimeOut (si->app, secs * 1000,
1528 de_race_timer, closure);