+ /* Make sure the Screen and the Window correspond. */
+ {
+ XWindowAttributes xgwa;
+ XGetWindowAttributes (dpy, window, &xgwa);
+ screen = xgwa.screen;
+ }
+
+ if (file && stat (file, &st))
+ {
+ fprintf (stderr, "%s: file \"%s\" does not exist\n", progname, file);
+ file = 0;
+ }
+
+ if (verbose_p)
+ {
+ fprintf (stderr, "%s: grabDesktopImages: %s\n",
+ progname, desk_p ? "True" : "False");
+ fprintf (stderr, "%s: grabVideoFrames: %s\n",
+ progname, video_p ? "True" : "False");
+ fprintf (stderr, "%s: chooseRandomImages: %s\n",
+ progname, image_p ? "True" : "False");
+ fprintf (stderr, "%s: imageDirectory: %s\n",
+ progname, (file ? file : dir ? dir : ""));
+ }
+
+# if !(defined(HAVE_GDK_PIXBUF) || defined(HAVE_JPEGLIB))
+ image_p = False; /* can't load images from files... */
+# ifdef USE_EXTERNAL_SCREEN_GRABBER
+ desk_p = False; /* ...or from desktops grabbed to files. */
+# endif
+
+ if (file)
+ {
+ fprintf (stderr,
+ "%s: image file loading not available at compile-time\n",
+ progname);
+ fprintf (stderr, "%s: can't load \"%s\"\n", progname, file);
+ file = 0;
+ }
+# endif /* !(HAVE_GDK_PIXBUF || HAVE_JPEGLIB) */
+
+ if (file)
+ {
+ desk_p = False;
+ video_p = False;
+ image_p = True;
+ }
+
+ if (!dir || !*dir)
+ {
+ if (verbose_p && image_p)
+ fprintf (stderr,
+ "%s: no imageDirectory: turning off chooseRandomImages.\n",
+ progname);
+ image_p = False;
+ }
+
+
+# ifndef _VROOT_H_
+# error Error! This file definitely needs vroot.h!
+# endif
+
+ /* We can grab desktop images (using the normal X11 method) if:
+ - the window is the real root window;
+ - the window is a toplevel window.
+ We cannot grab desktop images that way if:
+ - the window is a non-top-level window.
+
+ Using the MacOS X way, desktops are just like loaded image files.
+ */
+# ifndef USE_EXTERNAL_SCREEN_GRABBER
+ if (desk_p)
+ {
+ if (!top_level_window_p (screen, window))
+ {
+ desk_p = False;
+ if (verbose_p)
+ fprintf (stderr,
+ "%s: 0x%x not top-level: turning off grabDesktopImages.\n",
+ progname, (unsigned int) window);
+ }
+ }
+# endif /* !USE_EXTERNAL_SCREEN_GRABBER */
+
+ count = 0;
+ if (desk_p) count++;
+ if (video_p) count++;
+ if (image_p) count++;
+
+ if (count == 0)
+ 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();
+ }
+ }
+
+
+ /* If we're to search a directory to find an image file, do so now.
+ */
+ if (which == GRAB_FILE && !file)
+ {
+ file = get_filename (screen, dir, verbose_p);
+ if (!file)
+ {
+ which = GRAB_BARS;
+ if (verbose_p)
+ fprintf (stderr, "%s: no image files found.\n", progname);
+ }
+ }
+
+ /* Now actually render something.
+ */
+ switch (which)
+ {
+ case GRAB_BARS:
+ {
+ XWindowAttributes xgwa;
+ COLORBARS:
+ if (verbose_p)
+ fprintf (stderr, "%s: drawing colorbars.\n", progname);
+ XGetWindowAttributes (dpy, window, &xgwa);
+ draw_colorbars (screen, xgwa.visual, drawable, xgwa.colormap,
+ 0, 0, 0, 0);
+ XSync (dpy, False);
+ }
+ break;
+
+ case GRAB_DESK:
+ if (! display_desktop (screen, window, drawable, verbose_p))
+ goto COLORBARS;
+ file_prop = "desktop";
+ break;
+
+ case GRAB_FILE:
+ if (! display_file (screen, window, drawable, file, verbose_p))
+ goto COLORBARS;
+ file_prop = file;
+ break;
+
+ case GRAB_VIDEO:
+ if (! display_video (screen, window, drawable, verbose_p))
+ goto COLORBARS;
+ file_prop = "video";
+ break;
+
+ default:
+ abort();
+ break;
+ }
+
+ {
+ Atom a = XInternAtom (dpy, XA_XSCREENSAVER_IMAGE_FILENAME, False);
+ if (file_prop && *file_prop)
+ XChangeProperty (dpy, window, a, XA_STRING, 8, PropModeReplace,
+ (unsigned char *) file_prop, strlen(file_prop));
+ else
+ XDeleteProperty (dpy, window, a);
+ }
+
+ XSync (dpy, False);
+}
+
+
+#ifdef DEBUG