http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.20.tar.gz
[xscreensaver] / driver / xscreensaver-getimage.c
index e46bc5c97262a6e9d55ce566c0142ec8c4b25228..bf0feef95785248736844599af396396086a9e44 100644 (file)
@@ -1,4 +1,4 @@
-/* xscreensaver, Copyright (c) 2001, 2002, 2003 by Jamie Zawinski <jwz@jwz.org>
+/* xscreensaver, Copyright (c) 2001-2004 by Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -136,6 +136,15 @@ ignore_all_errors_ehandler (Display *dpy, XErrorEvent *error)
   return 0;
 }
 
+static int
+ignore_badmatch_ehandler (Display *dpy, XErrorEvent *error)
+{
+  if (error->error_code == BadMatch)
+    return ignore_all_errors_ehandler (dpy, error);
+  else
+    return x_ehandler (dpy, error);
+}
+
 
 /* Returns True if the given Drawable is a Window; False if it's a Pixmap.
  */
@@ -171,7 +180,7 @@ root_window_p (Screen *screen, Window window)
   Atom type;
   int format;
   unsigned long nitems, bytesafter;
-  char *version;
+  unsigned char *version;
 
   if (window != RootWindowOfScreen (screen))
     return False;
@@ -180,7 +189,7 @@ root_window_p (Screen *screen, Window window)
                          XInternAtom (dpy, "_SCREENSAVER_VERSION", False),
                          0, 1, False, XA_STRING,
                          &type, &format, &nitems, &bytesafter,
-                         (unsigned char **) &version)
+                         &version)
       == Success
       && type != None)
     return False;
@@ -276,8 +285,8 @@ compute_image_scaling (int src_w, int src_h,
   *scaled_to_y_ret = desty;
 
   if (verbose_p)
-    fprintf (stderr, "%s: displaying %dx%d image at %d,%d.\n",
-             progname, src_w, src_h, destx, desty);
+    fprintf (stderr, "%s: displaying %dx%d image at %d,%d in %dx%d.\n",
+             progname, src_w, src_h, destx, desty, dest_w, dest_h);
 }
 
 
@@ -342,7 +351,8 @@ scale_ximage (Screen *screen, Visual *visual,
  */
 static Bool
 read_file_gdk (Screen *screen, Window window, Drawable drawable,
-               const char *filename, Bool verbose_p)
+               const char *filename, Bool verbose_p,
+               XRectangle *geom_ret)
 {
   GdkPixbuf *pb;
   Display *dpy = DisplayOfScreen (screen);
@@ -442,6 +452,14 @@ read_file_gdk (Screen *screen, Window window, Drawable drawable,
           XSetWindowBackgroundPixmap (dpy, window, drawable);
           XClearWindow (dpy, window);
         }
+
+      if (geom_ret)
+        {
+          geom_ret->x = destx;
+          geom_ret->y = desty;
+          geom_ret->width  = w;
+          geom_ret->height = h;
+        }
     }
 
   XSync (dpy, False);
@@ -885,7 +903,7 @@ read_jpeg_ximage (Screen *screen, Visual *visual, Drawable drawable,
           int x;
           for (x = 0; x < ximage->width; x++)
             {
-              int j = x * cinfo.num_components;
+              int j = x * cinfo.output_components;
               unsigned char r = scanbuf[i][j];
               unsigned char g = scanbuf[i][j+1];
               unsigned char b = scanbuf[i][j+2];
@@ -897,10 +915,12 @@ read_jpeg_ximage (Screen *screen, Visual *visual, Drawable drawable,
                 pixel = ((r >> 5) | ((g >> 5) << 3) | ((b >> 6) << 6));
               else if (depth == 12)
                 pixel = ((r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8));
-              else if (depth == 16 || depth == 15)
+              else if (depth == 15)
                 /* Gah! I don't understand why these are in the other
                    order. */
                 pixel = (((r >> 3) << 10) | ((g >> 3) << 5) | ((b >> 3)));
+              else if (depth == 16)
+                pixel = (((r >> 3) << 11) | ((g >> 2) << 5) | ((b >> 3)));
               else
                 abort();
 
@@ -940,7 +960,8 @@ read_jpeg_ximage (Screen *screen, Visual *visual, Drawable drawable,
  */
 static Bool
 read_file_jpeglib (Screen *screen, Window window, Drawable drawable,
-                   const char *filename, Bool verbose_p)
+                   const char *filename, Bool verbose_p,
+                   XRectangle *geom_ret)
 {
   Display *dpy = DisplayOfScreen (screen);
   XImage *ximage;
@@ -1031,6 +1052,14 @@ read_file_jpeglib (Screen *screen, Window window, Drawable drawable,
     XFreeGC (dpy, gc);
   }
 
+  if (geom_ret)
+    {
+      geom_ret->x = destx;
+      geom_ret->y = desty;
+      geom_ret->width  = ximage->width;
+      geom_ret->height = ximage->height;
+    }
+
   free (ximage->data);
   ximage->data = 0;
   XDestroyImage (ximage);
@@ -1046,16 +1075,18 @@ read_file_jpeglib (Screen *screen, Window window, Drawable drawable,
  */
 static Bool
 display_file (Screen *screen, Window window, Drawable drawable,
-              const char *filename, Bool verbose_p)
+              const char *filename, Bool verbose_p,
+              XRectangle *geom_ret)
 {
   if (verbose_p)
     fprintf (stderr, "%s: loading \"%s\"\n", progname, filename);
 
 # if defined(HAVE_GDK_PIXBUF)
-  if (read_file_gdk (screen, window, drawable, filename, verbose_p))
+  if (read_file_gdk (screen, window, drawable, filename, verbose_p, geom_ret))
     return True;
 # elif defined(HAVE_JPEGLIB)
-  if (read_file_jpeglib (screen, window, drawable, filename, verbose_p))
+  if (read_file_jpeglib (screen, window, drawable, filename, verbose_p,
+                         geom_ret))
     return True;
 # else  /* !(HAVE_GDK_PIXBUF || HAVE_JPEGLIB) */
   /* shouldn't get here if we have no image-loading methods available. */
@@ -1067,7 +1098,7 @@ display_file (Screen *screen, Window window, Drawable drawable,
 
 
 /* Invokes a sub-process and returns its output (presumably, a file to
-   load.)  Free the string when done.  video_p controls which program
+   load.)  Free the string when done.  'grab_type' controls which program
    to run.
  */
 static char *
@@ -1228,7 +1259,7 @@ get_desktop_filename (Screen *screen, Bool verbose_p)
  */
 static Bool
 display_video (Screen *screen, Window window, Drawable drawable,
-               Bool verbose_p)
+               Bool verbose_p, XRectangle *geom_ret)
 {
   char *filename = get_video_filename (screen, verbose_p);
   Bool status;
@@ -1240,7 +1271,8 @@ display_video (Screen *screen, Window window, Drawable drawable,
       return False;
     }
 
-  status = display_file (screen, window, drawable, filename, verbose_p);
+  status = display_file (screen, window, drawable, filename, verbose_p,
+                         geom_ret);
 
   if (unlink (filename))
     {
@@ -1263,7 +1295,7 @@ display_video (Screen *screen, Window window, Drawable drawable,
  */
 static Bool
 display_desktop (Screen *screen, Window window, Drawable drawable,
-                 Bool verbose_p)
+                 Bool verbose_p, XRectangle *geom_ret)
 {
 # ifdef USE_EXTERNAL_SCREEN_GRABBER
 
@@ -1299,7 +1331,8 @@ display_desktop (Screen *screen, Window window, Drawable drawable,
       return False;
     }
 
-  status = display_file (screen, window, drawable, filename, verbose_p);
+  status = display_file (screen, window, drawable, filename, verbose_p,
+                         geom_ret);
 
   if (unlink (filename))
     {
@@ -1347,12 +1380,38 @@ display_desktop (Screen *screen, Window window, Drawable drawable,
     }
   else  /* size mismatch -- must scale client-side images to fit drawable */
     {
-      XImage *ximage = XGetImage (dpy, window, 0, 0, xgwa.width, xgwa.height,
-                                  ~0L, ZPixmap);
       GC gc;
+      XImage *ximage = 0;
+      XErrorHandler old_handler;
+
+      XSync (dpy, False);
+      old_handler = XSetErrorHandler (ignore_badmatch_ehandler);
+      error_handler_hit_p = False;
+
+      /* This can return BadMatch if the window is not fully on screen.
+         Trap that error and return color bars in that case.
+         (Note that this only happens with XGetImage, not with XCopyArea:
+         yet another totally gratuitous inconsistency in X, thanks.)
+       */
+      ximage = XGetImage (dpy, window, 0, 0, xgwa.width, xgwa.height,
+                          ~0L, ZPixmap);
+
+      XSync (dpy, False);
+      XSetErrorHandler (old_handler);
+      XSync (dpy, False);
+
+      if (error_handler_hit_p)
+        {
+          ximage = 0;
+          if (verbose_p)
+            fprintf (stderr, "%s: BadMatch reading window 0x%x contents!\n",
+                     progname, (unsigned int) window);
+        }
+
       if (!ximage ||
           !scale_ximage (xgwa.screen, xgwa.visual, ximage, w2, h2))
         return False;
+
       gc = XCreateGC (dpy, drawable, 0, &gcv);
       clear_drawable (screen, drawable);
       XPutImage (dpy, drawable, gc, ximage, 
@@ -1361,6 +1420,14 @@ display_desktop (Screen *screen, Window window, Drawable drawable,
       XFreeGC (dpy, gc);
     }
 
+  if (geom_ret)
+    {
+      geom_ret->x = destx;
+      geom_ret->y = desty;
+      geom_ret->width  = w2;
+      geom_ret->height = h2;
+    }
+
   XSync (dpy, False);
   return True;
 
@@ -1387,6 +1454,7 @@ get_image (Screen *screen,
   int count = 0;
   struct stat st;
   const char *file_prop = 0;
+  XRectangle geom = { 0, 0, 0, 0 };
 
   if (! drawable_window_p (dpy, window))
     {
@@ -1442,8 +1510,7 @@ get_image (Screen *screen,
       video_p = False;
       image_p = True;
     }
-
-  if (!dir || !*dir)
+  else if (!dir || !*dir)
     {
       if (verbose_p && image_p)
         fprintf (stderr,
@@ -1531,19 +1598,19 @@ get_image (Screen *screen,
       break;
 
     case GRAB_DESK:
-      if (! display_desktop (screen, window, drawable, verbose_p))
+      if (! display_desktop (screen, window, drawable, verbose_p, &geom))
         goto COLORBARS;
       file_prop = "desktop";
       break;
 
     case GRAB_FILE:
-      if (! display_file (screen, window, drawable, file, verbose_p))
+      if (! display_file (screen, window, drawable, file, verbose_p, &geom))
         goto COLORBARS;
       file_prop = file;
       break;
 
     case GRAB_VIDEO:
-      if (! display_video (screen, window, drawable, verbose_p))
+      if (! display_video (screen, window, drawable, verbose_p, &geom))
         goto COLORBARS;
       file_prop = "video";
       break;
@@ -1560,6 +1627,17 @@ get_image (Screen *screen,
                        (unsigned char *) file_prop, strlen(file_prop));
     else
       XDeleteProperty (dpy, window, a);
+
+    a = XInternAtom (dpy, XA_XSCREENSAVER_IMAGE_GEOMETRY, False);
+    if (geom.width > 0)
+      {
+        char gstr[30];
+        sprintf (gstr, "%dx%d+%d+%d", geom.width, geom.height, geom.x, geom.y);
+        XChangeProperty (dpy, window, a, XA_STRING, 8, PropModeReplace, 
+                         (unsigned char *) gstr, strlen (gstr));
+      }
+    else
+      XDeleteProperty (dpy, window, a);
   }
 
   XSync (dpy, False);