http://ftp.ksu.edu.tw/FTP/FreeBSD/distfiles/xscreensaver-4.23.tar.gz
[xscreensaver] / driver / xscreensaver-getimage.c
index db8bc6202325549858319042693dbf007ec13e67..24c0246e262d2c1a1214f5e5d666121c5f2f8fd8 100644 (file)
@@ -136,6 +136,7 @@ ignore_all_errors_ehandler (Display *dpy, XErrorEvent *error)
   return 0;
 }
 
+#ifndef USE_EXTERNAL_SCREEN_GRABBER
 static int
 ignore_badmatch_ehandler (Display *dpy, XErrorEvent *error)
 {
@@ -144,6 +145,7 @@ ignore_badmatch_ehandler (Display *dpy, XErrorEvent *error)
   else
     return x_ehandler (dpy, error);
 }
+#endif /* ! USE_EXTERNAL_SCREEN_GRABBER */
 
 
 /* Returns True if the given Drawable is a Window; False if it's a Pixmap.
@@ -180,7 +182,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;
@@ -189,7 +191,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;
@@ -285,8 +287,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);
 }
 
 
@@ -295,6 +297,7 @@ compute_image_scaling (int src_w, int src_h,
    If out of memory, returns False, and the XImage will have been
    destroyed and freed.
  */
+#ifndef USE_EXTERNAL_SCREEN_GRABBER
 static Bool
 scale_ximage (Screen *screen, Visual *visual,
               XImage *ximage, int new_width, int new_height)
@@ -342,6 +345,7 @@ scale_ximage (Screen *screen, Visual *visual,
 
   return True;
 }
+#endif /* ! USE_EXTERNAL_SCREEN_GRABBER */
 
 
 #ifdef HAVE_GDK_PIXBUF
@@ -351,7 +355,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);
@@ -451,6 +456,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);
@@ -951,7 +964,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;
@@ -1042,6 +1056,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);
@@ -1057,16 +1079,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. */
@@ -1078,7 +1102,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 *
@@ -1177,7 +1201,8 @@ get_filename_1 (Screen *screen, const char *directory, grab_type type,
 
         close (out);  /* don't need this one */
         *buf = 0;
-        fgets (buf, sizeof(buf)-1, f);
+        if (! fgets (buf, sizeof(buf)-1, f))
+          *buf = 0;
         fclose (f);
 
         /* Wait for the child to die. */
@@ -1239,7 +1264,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;
@@ -1251,7 +1276,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))
     {
@@ -1274,7 +1300,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
 
@@ -1310,7 +1336,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))
     {
@@ -1398,6 +1425,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;
 
@@ -1421,9 +1456,9 @@ get_image (Screen *screen,
 {
   Display *dpy = DisplayOfScreen (screen);
   grab_type which = GRAB_BARS;
-  int count = 0;
   struct stat st;
   const char *file_prop = 0;
+  XRectangle geom = { 0, 0, 0, 0 };
 
   if (! drawable_window_p (dpy, window))
     {
@@ -1515,24 +1550,28 @@ get_image (Screen *screen,
     }
 # endif /* !USE_EXTERNAL_SCREEN_GRABBER */
 
-  count = 0;
-  if (desk_p)  count++;
-  if (video_p) count++;
-  if (image_p) count++;
-
-  if (count == 0)
+  if (! (desk_p || video_p || image_p))
     which = GRAB_BARS;
   else
     {
       int i = 0;
-      while (1)  /* loop until we get one that's permitted */
-        {
-          which = (random() % 3);
-          if (which == GRAB_DESK  && desk_p)  break;
-          if (which == GRAB_VIDEO && video_p) break;
-          if (which == GRAB_FILE  && image_p) break;
-          if (++i > 200) abort();
-        }
+      int n;
+      /* Loop until we get one that's permitted.
+         If files or video are permitted, do them more often
+         than desktop.
+
+             D+V+I: 10% + 45% + 45%.
+             V+I:   50% + 50%
+             D+V:   18% + 82%
+             D+I:   18% + 82%
+       */
+    AGAIN:
+      n = (random() % 100);
+      if (++i > 300) abort();
+      else if (desk_p  && n < 10) which = GRAB_DESK;   /* 10% */
+      else if (video_p && n < 55) which = GRAB_VIDEO;  /* 45% */
+      else if (image_p)           which = GRAB_FILE;   /* 45% */
+      else goto AGAIN;
     }
 
 
@@ -1567,19 +1606,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;
@@ -1596,6 +1635,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);