http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.01.tar.gz
[xscreensaver] / driver / timers.c
index 1dbdeff6706481a32ba9a99697a26cf44284bf06..9f3877e5b07e0a4a230c79d759f0e8ef9e1e5c52 100644 (file)
@@ -21,6 +21,8 @@
 #include <X11/Xlib.h>
 #include <X11/Intrinsic.h>
 #include <X11/Xos.h>
+#include <time.h>
+#include <sys/time.h>
 #ifdef HAVE_XMU
 # ifndef VMS
 #  include <X11/Xmu/Error.h>
@@ -99,6 +101,7 @@ notice_events (saver_info *si, Window window, Bool top_p)
   unsigned long events;
   Window root, parent, *kids;
   unsigned int nkids;
+  int screen_no;
 
   if (XtWindowToWidget (si->dpy, window))
     /* If it's one of ours, don't mess up its event mask. */
@@ -109,6 +112,11 @@ notice_events (saver_info *si, Window window, Bool top_p)
   if (window == root)
     top_p = False;
 
+  /* Figure out which screen this window is on, for the diagnostics. */
+  for (screen_no = 0; screen_no < si->nscreens; screen_no++)
+    if (root == RootWindowOfScreen (si->screens[screen_no].screen))
+      break;
+
   XGetWindowAttributes (si->dpy, window, &attrs);
   events = ((attrs.all_event_masks | attrs.do_not_propagate_mask)
            & KeyPressMask);
@@ -125,14 +133,20 @@ notice_events (saver_info *si, Window window, Bool top_p)
      the mouse or touching the keyboard, we won't know that they've been
      active, and the screensaver will come on.  That sucks, but I don't
      know how to get around it.
+
+     Since X presents mouse wheels as clicks, this applies to those, too:
+     scrolling through a document using only the mouse wheel doesn't
+     count as activity...  Fortunately, /proc/interrupts helps, on
+     systems that have it.  Oh, if it's a PS/2 mouse, not serial or USB.
+     This sucks!
    */
   XSelectInput (si->dpy, window, SubstructureNotifyMask | events);
 
   if (top_p && p->verbose_p && (events & KeyPressMask))
     {
       /* Only mention one window per tree (hack hack). */
-      fprintf (stderr, "%s: selected KeyPress on 0x%lX\n", blurb(),
-              (unsigned long) window);
+      fprintf (stderr, "%s: %d: selected KeyPress on 0x%lX\n",
+               blurb(), screen_no, (unsigned long) window);
       top_p = False;
     }
 
@@ -243,14 +257,25 @@ cycle_timer (XtPointer closure, XtIntervalId *id)
         }
     }
 
-  si->cycle_id = XtAppAddTimeOut (si->app, how_long, cycle_timer,
-                                 (XtPointer) si);
+  if (how_long > 0)
+    {
+      si->cycle_id = XtAppAddTimeOut (si->app, how_long, cycle_timer,
+                                      (XtPointer) si);
 
-#ifdef DEBUG_TIMERS
-  if (p->verbose_p)
-    fprintf (stderr, "%s: starting cycle_timer (%ld, %ld)\n",
-           blurb(), how_long, si->cycle_id);
-#endif /* DEBUG_TIMERS */
+# ifdef DEBUG_TIMERS
+      if (p->verbose_p)
+        fprintf (stderr, "%s: starting cycle_timer (%ld, %ld)\n",
+                 blurb(), how_long, si->cycle_id);
+# endif /* DEBUG_TIMERS */
+    }
+# ifdef DEBUG_TIMERS
+  else
+    {
+      if (p->verbose_p)
+        fprintf (stderr, "%s: not starting cycle_timer: how_long == %d\n",
+                 blurb(), how_long);
+    }
+# endif /* DEBUG_TIMERS */
 }
 
 
@@ -268,7 +293,7 @@ activate_lock_timer (XtPointer closure, XtIntervalId *id)
 
 /* Call this when user activity (or "simulated" activity) has been noticed.
  */
-static void
+void
 reset_timers (saver_info *si)
 {
   saver_preferences *p = &si->prefs;
@@ -325,8 +350,14 @@ check_pointer_timer (XtPointer closure, XtIntervalId *id)
       int root_x, root_y, x, y;
       unsigned int mask;
 
-      XQueryPointer (si->dpy, ssi->screensaver_window, &root, &child,
-                    &root_x, &root_y, &x, &y, &mask);
+      if (!XQueryPointer (si->dpy, ssi->screensaver_window, &root, &child,
+                          &root_x, &root_y, &x, &y, &mask))
+        {
+          /* If XQueryPointer() returns false, the mouse is not on this screen.
+           */
+          root_x = -1;
+          root_y = -1;
+        }
 
       if (root_x == ssi->poll_mouse_last_root_x &&
          root_y == ssi->poll_mouse_last_root_y &&
@@ -338,23 +369,37 @@ check_pointer_timer (XtPointer closure, XtIntervalId *id)
 
 #ifdef DEBUG_TIMERS
       if (p->verbose_p)
-       if (root_x == ssi->poll_mouse_last_root_x &&
-           root_y == ssi->poll_mouse_last_root_y &&
-           child  == ssi->poll_mouse_last_child)
-         fprintf (stderr, "%s: modifiers changed at %s on screen %d.\n",
-                  blurb(), timestring(), i);
-       else
-         fprintf (stderr, "%s: pointer moved at %s on screen %d.\n",
-                  blurb(), timestring(), i);
-
-# if 0
-      fprintf (stderr, "%s: old: %d %d 0x%x ; new: %d %d 0x%x\n",
-               blurb(), 
-               ssi->poll_mouse_last_root_x,
-               ssi->poll_mouse_last_root_y,
-               (unsigned int) ssi->poll_mouse_last_child,
-               root_x, root_y, (unsigned int) child);
-# endif /* 0 */
+        {
+          if (root_x == ssi->poll_mouse_last_root_x &&
+              root_y == ssi->poll_mouse_last_root_y &&
+              child  == ssi->poll_mouse_last_child)
+            fprintf (stderr, "%s: %d: modifiers changed: 0x%04x -> 0x%04x.\n",
+                     blurb(), i, ssi->poll_mouse_last_mask, mask);
+          else
+            {
+              fprintf (stderr, "%s: %d: pointer moved: ", blurb(), i);
+              if (ssi->poll_mouse_last_root_x == -1)
+                fprintf (stderr, "off screen");
+              else
+                fprintf (stderr, "%d,%d",
+                         ssi->poll_mouse_last_root_x,
+                         ssi->poll_mouse_last_root_y);
+              fprintf (stderr, " -> ");
+              if (root_x == -1)
+                fprintf (stderr, "off screen.");
+              else
+                fprintf (stderr, "%d,%d", root_x, root_y);
+              if (ssi->poll_mouse_last_root_x == -1 || root_x == -1)
+                fprintf (stderr, ".\n");
+              else
+#   undef ABS
+#   define ABS(x)((x)<0?-(x):(x))
+                fprintf (stderr, " (%d,%d).\n",
+                         ABS(ssi->poll_mouse_last_root_x - root_x),
+                         ABS(ssi->poll_mouse_last_root_y - root_y));
+# undef ABS
+            }
+        }
 
 #endif /* DEBUG_TIMERS */
 
@@ -413,8 +458,13 @@ check_for_clock_skew (saver_info *si)
 
 #ifdef DEBUG_TIMERS
   if (p->verbose_p)
-    fprintf (stderr, "%s: checking wall clock (%d).\n", blurb(),
-             (si->last_wall_clock_time == 0 ? 0 : shift));
+    {
+      int i = (si->last_wall_clock_time == 0 ? 0 : shift);
+      fprintf (stderr,
+               "%s: checking wall clock for hibernation (%d:%02d:%02d).\n",
+               blurb(),
+               (i / (60 * 60)), ((i / 60) % 60), (i % 60));
+    }
 #endif /* DEBUG_TIMERS */
 
   if (si->last_wall_clock_time != 0 &&
@@ -681,14 +731,46 @@ sleep_until_idle (saver_info *si, Bool until_idle_p)
 #ifdef DEBUG_TIMERS
        if (p->verbose_p)
          {
+            Window root, window;
+            int x, y;
+            const char *type = 0;
            if (event.xany.type == MotionNotify)
-             fprintf (stderr,"%s: MotionNotify at %s\n",blurb(),timestring());
+              {
+                type = "MotionNotify";
+                root = event.xmotion.root;
+                window = event.xmotion.window;
+                x = event.xmotion.x_root;
+                y = event.xmotion.y_root;
+              }
            else if (event.xany.type == KeyPress)
-             fprintf (stderr, "%s: KeyPress seen on 0x%X at %s\n", blurb(),
-                      (unsigned int) event.xkey.window, timestring ());
+              {
+                type = "KeyPress";
+                root = event.xkey.root;
+                window = event.xkey.window;
+                x = y = -1;
+              }
            else if (event.xany.type == ButtonPress)
-             fprintf (stderr, "%s: ButtonPress seen on 0x%X at %s\n", blurb(),
-                      (unsigned int) event.xbutton.window, timestring ());
+              {
+                type = "ButtonPress";
+                root = event.xkey.root;
+                window = event.xkey.window;
+                x = event.xmotion.x_root;
+                y = event.xmotion.y_root;
+              }
+
+            if (type)
+              {
+                int i;
+                for (i = 0; i < si->nscreens; i++)
+                  if (root == RootWindowOfScreen (si->screens[i].screen))
+                    break;
+                fprintf (stderr,"%s: %d: %s on 0x%x",
+                         blurb(), i, type, (unsigned long) window);
+                if (x == -1)
+                  fprintf (stderr, "\n");
+                else
+                  fprintf (stderr, " at %d,%d.\n", x, y);
+              }
          }
 #endif /* DEBUG_TIMERS */
 
@@ -1071,9 +1153,20 @@ 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_standby / 1000,
+                             p->dpms_suspend / 1000,
+                             p->dpms_off / 1000,
+                             False);
+
   if (si->screen_blanked_p)
     {
       Bool running_p = screenhack_running_p (si);