1 /* xscreensaver, Copyright (c) 2001, 2002, 2003 by Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* xscreensaver-getimage -- helper program that puts a random image
13 onto the given window or pixmap. That image is either a screen-grab,
14 a file loaded from disk, or a frame grabbed from the system's video
20 #include <X11/Intrinsic.h>
26 #ifdef HAVE_SYS_WAIT_H
27 # include <sys/wait.h> /* for waitpid() and associated macros */
32 # include <X11/Xmu/Error.h>
34 # include <Xmu/Error.h>
41 #include "grabscreen.h"
42 #include "resources.h"
43 #include "colorbars.h"
48 #ifdef HAVE_GDK_PIXBUF
51 # include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
52 # else /* !HAVE_GTK2 */
53 # include <gdk-pixbuf/gdk-pixbuf-xlib.h>
54 # endif /* !HAVE_GTK2 */
55 #endif /* HAVE_GDK_PIXBUF */
58 # undef HAVE_GDK_PIXBUF
63 static char *defaults[] = {
64 #include "../driver/XScreenSaver_ad.h"
71 char *progclass = "XScreenSaver";
75 extern void grabscreen_verbose (void);
78 #define GETIMAGE_VIDEO_PROGRAM "xscreensaver-getimage-video"
79 #define GETIMAGE_FILE_PROGRAM "xscreensaver-getimage-file"
89 x_ehandler (Display *dpy, XErrorEvent *error)
91 fprintf (stderr, "\nX error in %s:\n", progname);
92 XmuPrintDefaultErrorMessage (dpy, error, stderr);
98 static Bool error_handler_hit_p = False;
101 ignore_all_errors_ehandler (Display *dpy, XErrorEvent *error)
103 error_handler_hit_p = True;
108 /* Returns True if the given Drawable is a Window; False if it's a Pixmap.
111 drawable_window_p (Display *dpy, Drawable d)
113 XErrorHandler old_handler;
114 XWindowAttributes xgwa;
117 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
118 error_handler_hit_p = False;
119 XGetWindowAttributes (dpy, d, &xgwa);
121 XSetErrorHandler (old_handler);
124 if (!error_handler_hit_p)
125 return True; /* It's a Window. */
127 return False; /* It's a Pixmap, or an invalid ID. */
131 /* Clear the window or pixmap to black, or its background color.
134 clear_drawable (Screen *screen, Drawable drawable)
136 Display *dpy = DisplayOfScreen (screen);
141 unsigned int w, h, bw, d;
142 XGetGeometry (dpy, drawable, &root, &x, &y, &w, &h, &bw, &d);
144 /* The window might have no-op background of None, so to clear it,
145 draw a black rectangle first, then do XClearWindow (in case the
146 actual background color is non-black...) */
148 /* #### really we should allocate "black" instead, but I'm lazy... */
149 gcv.foreground = BlackPixelOfScreen (screen);
150 gc = XCreateGC (dpy, drawable, GCForeground, &gcv);
151 XFillRectangle (dpy, drawable, gc, 0, 0, w, h);
153 if (drawable_window_p (dpy, drawable))
154 XClearWindow (dpy, (Window) drawable);
159 /* Figure out what kind of scaling/positioning we ought to do to display
160 a src-sized image in a dest-sized window/pixmap. Returns the width
161 and height to which the image should be scaled, and the position where
162 it should be displayed to center it.
165 compute_image_scaling (int src_w, int src_h,
166 int dest_w, int dest_h,
168 int *scaled_from_x_ret, int *scaled_from_y_ret,
169 int *scaled_to_x_ret, int *scaled_to_y_ret,
170 int *scaled_w_ret, int *scaled_h_ret)
172 int srcx, srcy, destx, desty;
174 Bool exact_fit_p = ((src_w == dest_w && src_h <= dest_h) ||
175 (src_h == dest_h && src_w <= dest_w));
177 if (!exact_fit_p) /* scale the image up or down */
179 float rw = (float) dest_w / src_w;
180 float rh = (float) dest_h / src_h;
181 float r = (rw < rh ? rw : rh);
186 if (pct < 95 || pct > 105) /* don't scale if it's close */
189 fprintf (stderr, "%s: scaling image by %d%% (%dx%d -> %dx%d)\n",
190 progname, pct, src_w, src_h, tw, th);
196 /* Center the image on the window/pixmap. */
199 destx = (dest_w - src_w) / 2;
200 desty = (dest_h - src_h) / 2;
201 if (destx < 0) srcx = -destx, destx = 0;
202 if (desty < 0) srcy = -desty, desty = 0;
204 if (dest_w < src_w) src_w = dest_w;
205 if (dest_h < src_h) src_h = dest_h;
207 *scaled_w_ret = src_w;
208 *scaled_h_ret = src_h;
209 *scaled_from_x_ret = srcx;
210 *scaled_from_y_ret = srcy;
211 *scaled_to_x_ret = destx;
212 *scaled_to_y_ret = desty;
215 fprintf (stderr, "%s: displaying %dx%d image at %d,%d.\n",
216 progname, src_w, src_h, destx, desty);
220 #ifdef HAVE_GDK_PIXBUF
222 /* Reads the given image file and renders it on the Drawable, using GDK.
223 Returns False if it fails.
226 read_file_gdk (Screen *screen, Window window, Drawable drawable,
227 const char *filename, Bool verbose_p)
230 Display *dpy = DisplayOfScreen (screen);
231 unsigned int win_width, win_height;
234 # endif /* HAVE_GTK2 */
240 XWindowAttributes xgwa;
241 XGetWindowAttributes (dpy, window, &xgwa);
242 screen = xgwa.screen;
243 XGetGeometry (dpy, drawable,
244 &root, &x, &y, &win_width, &win_height, &bw, &d);
247 gdk_pixbuf_xlib_init (dpy, screen_number (screen));
250 # else /* !HAVE_GTK2 */
251 xlib_rgb_init (dpy, screen);
252 # endif /* !HAVE_GTK2 */
254 pb = gdk_pixbuf_new_from_file (filename
257 # endif /* HAVE_GTK2 */
262 fprintf (stderr, "%s: unable to load \"%s\"\n", progname, filename);
264 if (gerr && gerr->message && *gerr->message)
265 fprintf (stderr, "%s: reason: %s\n", progname, gerr->message);
266 # endif /* HAVE_GTK2 */
271 int w = gdk_pixbuf_get_width (pb);
272 int h = gdk_pixbuf_get_height (pb);
273 int srcx, srcy, destx, desty, w2, h2;
275 compute_image_scaling (w, h, win_width, win_height, verbose_p,
276 &srcx, &srcy, &destx, &desty, &w2, &h2);
277 if (w != w2 || h != h2)
279 GdkPixbuf *pb2 = gdk_pixbuf_scale_simple (pb, w2, h2,
280 GDK_INTERP_BILINEAR);
283 gdk_pixbuf_unref (pb);
289 fprintf (stderr, "%s: out of memory when scaling?\n", progname);
292 clear_drawable (screen, drawable);
294 /* #### Note that this always uses the default colormap! Morons!
295 Owen says that in Gnome 2.0, I should try using
296 gdk_pixbuf_render_pixmap_and_mask_for_colormap() instead.
299 gdk_pixbuf_xlib_render_to_drawable_alpha (pb, drawable,
300 srcx, srcy, destx, desty,
302 GDK_PIXBUF_ALPHA_FULL, 127,
303 XLIB_RGB_DITHER_NORMAL,
311 #endif /* HAVE_GDK_PIXBUF */
317 /* Allocates a colormap that makes a PseudoColor or DirectColor
318 visual behave like a TrueColor visual of the same depth.
321 allocate_cubic_colormap (Screen *screen, Visual *visual, Colormap cmap,
324 Display *dpy = DisplayOfScreen (screen);
325 int nr, ng, nb, cells;
331 depth = visual_depth (screen, visual);
335 case 8: nr = 3; ng = 3; nb = 2; cells = 256; break;
336 case 12: nr = 4; ng = 4; nb = 4; cells = 4096; break;
337 default: abort(); break;
340 memset(colors, 0, sizeof(colors));
341 for (r = 0; r < (1 << nr); r++)
342 for (g = 0; g < (1 << ng); g++)
343 for (b = 0; b < (1 << nb); b++)
345 i = (r | (g << nr) | (b << (nr + ng)));
347 colors[i].flags = DoRed|DoGreen|DoBlue;
350 colors[i].red = ((r << 13) | (r << 10) | (r << 7) |
351 (r << 4) | (r << 1));
352 colors[i].green = ((g << 13) | (g << 10) | (g << 7) |
353 (g << 4) | (g << 1));
354 colors[i].blue = ((b << 14) | (b << 12) | (b << 10) |
355 (b << 8) | (b << 6) | (b << 4) |
360 colors[i].red = (r << 12) | (r << 8) | (r << 4) | r;
361 colors[i].green = (g << 12) | (g << 8) | (g << 4) | g;
362 colors[i].blue = (b << 12) | (b << 8) | (b << 4) | b;
369 int interleave = cells / 8; /* skip around, rather than allocating in
370 order, so that we get better coverage if
371 we can't allocated all of them. */
372 for (j = 0; j < interleave; j++)
373 for (i = 0; i < cells; i += interleave)
374 if (XAllocColor (dpy, cmap, &colors[i + j]))
378 fprintf (stderr, "%s: allocated %d of %d colors for cubic map\n",
379 progname, allocated, cells);
383 /* Find the pixel index that is closest to the given color
384 (using linear distance in RGB space -- which is far from the best way.)
387 find_closest_pixel (XColor *colors, int ncolors,
388 unsigned long r, unsigned long g, unsigned long b)
390 unsigned long distance = ~0;
395 for (i = 0; i < ncolors; i++)
400 rd = r - colors[i].red;
401 gd = g - colors[i].green;
402 bd = b - colors[i].blue;
403 if (rd < 0) rd = -rd;
404 if (gd < 0) gd = -gd;
405 if (bd < 0) bd = -bd;
406 d = (rd << 1) + (gd << 2) + bd;
421 /* Given an XImage with 8-bit or 12-bit RGB data, convert it to be
422 displayable with the given X colormap. The farther from a perfect
423 color cube the contents of the colormap are, the lossier the
424 transformation will be. No dithering is done.
427 remap_image (Screen *screen, Colormap cmap, XImage *image, Bool verbose_p)
429 Display *dpy = DisplayOfScreen (screen);
430 unsigned long map[4097];
435 if (image->depth == 8)
437 else if (image->depth == 12)
442 memset(map, -1, sizeof(*map));
443 memset(colors, -1, sizeof(*colors));
445 for (i = 0; i < cells; i++)
447 XQueryColors (dpy, cmap, colors, cells);
450 fprintf(stderr, "%s: building color cube for %d bit image\n",
451 progname, image->depth);
453 for (i = 0; i < cells; i++)
455 unsigned short r, g, b;
459 /* "RRR GGG BB" In an 8 bit map. Convert that to
460 "RRR RRR RR" "GGG GGG GG" "BB BB BB BB" to give
466 r = ((r << 13) | (r << 10) | (r << 7) | (r << 4) | (r << 1));
467 g = ((g << 13) | (g << 10) | (g << 7) | (g << 4) | (g << 1));
468 b = ((b << 14) | (b << 12) | (b << 10) | (b << 8) |
469 (b << 6) | (b << 4) | (b << 2) | b);
473 /* "RRRR GGGG BBBB" In a 12 bit map. Convert that to
474 "RRRR RRRR" "GGGG GGGG" "BBBB BBBB" to give an even
477 g = (i & 0x0F0) >> 4;
478 b = (i & 0xF00) >> 8;
480 r = (r << 12) | (r << 8) | (r << 4) | r;
481 g = (g << 12) | (g << 8) | (g << 4) | g;
482 b = (b << 12) | (b << 8) | (b << 4) | b;
485 map[i] = find_closest_pixel (colors, cells, r, g, b);
489 fprintf(stderr, "%s: remapping colors in %d bit image\n",
490 progname, image->depth);
492 for (y = 0; y < image->height; y++)
493 for (x = 0; x < image->width; x++)
495 unsigned long pixel = XGetPixel(image, x, y);
496 if (pixel >= cells) abort();
497 XPutPixel(image, x, y, map[pixel]);
502 /* If the file has a PPM (P6) on it, read it and return an XImage.
503 Otherwise, rewind the fd back to the beginning, and return 0.
506 maybe_read_ppm (Screen *screen, Visual *visual,
507 const char *filename, FILE *in, Bool verbose_p)
509 Display *dpy = DisplayOfScreen (screen);
510 int depth = visual_depth (screen, visual);
516 int x, y, w, h, maxval;
519 if (fstat (fileno (in), &st))
523 buf = (char *) malloc (bufsiz + 1);
526 fprintf (stderr, "%s: out of memory loading %d byte PPM file %s\n",
527 progname, bufsiz, filename);
531 if (! (s = fgets (buf, bufsiz, in))) /* line 1 */
534 if (!strncmp (buf, "\107\111", 2))
536 fprintf (stderr, "%s: %s: sorry, GIF files not supported"
537 " when compiled with JPEGlib instead of GDK_Pixbuf.\n",
541 else if (!strncmp (buf, "\211\120", 2))
543 fprintf (stderr, "%s: %s: sorry, PNG files not supported"
544 " when compiled with JPEGlib instead of GDK_Pixbuf.\n",
549 if (strncmp (s, "P6", 2))
552 if (! (s = fgets (buf, bufsiz, in))) /* line 2 */
554 if (2 != sscanf (s, " %d %d %c", &w, &h, &dummy))
556 fprintf (stderr, "%s: %s: invalid PPM (line 2)\n", progname, filename);
560 if (! (s = fgets (buf, bufsiz, in))) /* line 3 */
562 if (1 != sscanf (s, " %d %c", &maxval, &dummy))
564 fprintf (stderr, "%s: %s: invalid PPM (line 3)\n", progname, filename);
569 fprintf (stderr, "%s: %s: unparsable PPM: maxval is %d\n",
570 progname, filename, maxval);
574 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, 0,
577 ximage->data = (unsigned char *)
578 calloc (ximage->height, ximage->bytes_per_line);
579 if (!ximage || !ximage->data)
581 fprintf (stderr, "%s: out of memory loading %dx%d PPM file %s\n",
582 progname, ximage->width, ximage->height, filename);
588 while ((i = fread (s, 1, j, in)) > 0)
592 for (y = 0; y < ximage->height; y++)
593 for (x = 0; x < ximage->width; x++)
595 unsigned char r = buf[i++];
596 unsigned char g = buf[i++];
597 unsigned char b = buf[i++];
601 pixel = (r << 16) | (g << 8) | b;
603 pixel = ((r >> 5) | ((g >> 5) << 3) | ((b >> 6) << 6));
604 else if (depth == 12)
605 pixel = ((r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8));
606 else if (depth == 16 || depth == 15)
607 pixel = (((r >> 3) << 10) | ((g >> 3) << 5) | ((b >> 3)));
611 XPutPixel (ximage, x, y, pixel);
619 if (ximage && ximage->data)
624 if (ximage) XDestroyImage (ximage);
625 fseek (in, 0, SEEK_SET);
631 struct jpeg_error_mgr pub; /* this is what passes for subclassing in C */
632 const char *filename;
637 } getimg_jpg_error_mgr;
641 jpg_output_message (j_common_ptr cinfo)
643 getimg_jpg_error_mgr *err = (getimg_jpg_error_mgr *) cinfo->err;
644 char buf[JMSG_LENGTH_MAX];
645 cinfo->err->format_message (cinfo, buf);
646 fprintf (stderr, "%s: %s: %s\n", progname, err->filename, buf);
651 jpg_error_exit (j_common_ptr cinfo)
653 getimg_jpg_error_mgr *err = (getimg_jpg_error_mgr *) cinfo->err;
654 cinfo->err->output_message (cinfo);
655 draw_colorbars (err->screen, err->visual, err->drawable, err->cmap,
657 XSync (DisplayOfScreen (err->screen), False);
662 /* Reads a JPEG file, returns an RGB XImage of it.
665 read_jpeg_ximage (Screen *screen, Visual *visual, Drawable drawable,
666 Colormap cmap, const char *filename, Bool verbose_p)
668 Display *dpy = DisplayOfScreen (screen);
669 int depth = visual_depth (screen, visual);
673 struct jpeg_decompress_struct cinfo;
674 getimg_jpg_error_mgr jerr;
675 JSAMPARRAY scanbuf = 0;
678 jerr.filename = filename;
679 jerr.screen = screen;
680 jerr.visual = visual;
681 jerr.drawable = drawable;
684 if (! (depth >= 15 || depth == 12 || depth == 8))
686 fprintf (stderr, "%s: unsupported depth: %d\n", progname, depth);
690 in = fopen (filename, "rb");
693 fprintf (stderr, "%s: %s: unreadable\n", progname, filename);
697 /* Check to see if it's a PPM, and if so, read that instead of using
698 the JPEG library. Yeah, this is all modular and stuff.
700 if ((ximage = maybe_read_ppm (screen, visual, filename, in, verbose_p)))
706 cinfo.err = jpeg_std_error (&jerr.pub);
707 jerr.pub.output_message = jpg_output_message;
708 jerr.pub.error_exit = jpg_error_exit;
710 jpeg_create_decompress (&cinfo);
711 jpeg_stdio_src (&cinfo, in);
712 jpeg_read_header (&cinfo, TRUE);
714 /* set some decode parameters */
715 cinfo.out_color_space = JCS_RGB;
716 cinfo.quantize_colors = FALSE;
718 jpeg_start_decompress (&cinfo);
720 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, 0,
721 cinfo.output_width, cinfo.output_height,
724 ximage->data = (unsigned char *)
725 calloc (ximage->height, ximage->bytes_per_line);
727 if (ximage && ximage->data)
728 scanbuf = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE,
729 cinfo.rec_outbuf_height *
731 cinfo.output_components,
733 if (!ximage || !ximage->data || !scanbuf)
735 fprintf (stderr, "%s: out of memory loading %dx%d file %s\n",
736 progname, ximage->width, ximage->height, filename);
741 while (cinfo.output_scanline < cinfo.output_height)
743 int n = jpeg_read_scanlines (&cinfo, scanbuf, 1);
745 for (i = 0; i < n; i++)
748 for (x = 0; x < ximage->width; x++)
750 int j = x * cinfo.num_components;
751 unsigned char r = scanbuf[i][j];
752 unsigned char g = scanbuf[i][j+1];
753 unsigned char b = scanbuf[i][j+2];
757 pixel = (r << 16) | (g << 8) | b;
759 pixel = ((r >> 5) | ((g >> 5) << 3) | ((b >> 6) << 6));
760 else if (depth == 12)
761 pixel = ((r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8));
762 else if (depth == 16 || depth == 15)
763 /* Gah! I don't understand why these are in the other
765 pixel = (((r >> 3) << 10) | ((g >> 3) << 5) | ((b >> 3)));
769 XPutPixel (ximage, x, y, pixel);
775 if (cinfo.output_scanline < cinfo.output_height)
776 /* don't goto FAIL -- we might have viewable partial data. */
777 jpeg_abort_decompress (&cinfo);
779 jpeg_finish_decompress (&cinfo);
781 jpeg_destroy_decompress (&cinfo);
789 if (ximage && ximage->data)
794 if (ximage) XDestroyImage (ximage);
795 if (scanbuf) free (scanbuf);
800 /* Scales an XImage, modifying it in place.
801 If out of memory, returns False, and the XImage will have been
805 scale_ximage (Screen *screen, Visual *visual,
806 XImage *ximage, int new_width, int new_height)
808 Display *dpy = DisplayOfScreen (screen);
809 int depth = visual_depth (screen, visual);
811 double xscale, yscale;
813 XImage *ximage2 = XCreateImage (dpy, visual, depth,
815 new_width, new_height, 8, 0);
816 ximage2->data = (unsigned char *)
817 calloc (ximage2->height, ximage2->bytes_per_line);
821 fprintf (stderr, "%s: out of memory scaling %dx%d image to %dx%d\n",
823 ximage->width, ximage->height,
824 ximage2->width, ximage2->height);
825 if (ximage->data) free (ximage->data);
826 if (ximage2->data) free (ximage2->data);
829 XDestroyImage (ximage);
830 XDestroyImage (ximage2);
834 /* Brute force scaling... */
835 xscale = (double) ximage->width / ximage2->width;
836 yscale = (double) ximage->height / ximage2->height;
837 for (y = 0; y < ximage2->height; y++)
838 for (x = 0; x < ximage2->width; x++)
839 XPutPixel (ximage2, x, y,
840 XGetPixel (ximage, x * xscale, y * yscale));
845 (*ximage) = (*ximage2);
848 XDestroyImage (ximage2);
854 /* Reads the given image file and renders it on the Drawable, using JPEG lib.
855 Returns False if it fails.
858 read_file_jpeglib (Screen *screen, Window window, Drawable drawable,
859 const char *filename, Bool verbose_p)
861 Display *dpy = DisplayOfScreen (screen);
866 unsigned int win_width, win_height, win_depth;
867 int srcx, srcy, destx, desty, w2, h2;
873 XWindowAttributes xgwa;
875 XGetWindowAttributes (dpy, window, &xgwa);
876 screen = xgwa.screen;
877 visual = xgwa.visual;
878 cmap = xgwa.colormap;
880 XGetGeometry (dpy, drawable,
881 &root, &x, &y, &win_width, &win_height, &bw, &win_depth);
884 /* Make sure we're not on some weirdo visual...
886 class = visual_class (screen, visual);
887 depth = visual_depth (screen, visual);
888 if ((class == PseudoColor || class == DirectColor) &&
889 (depth != 8 && depth != 12))
891 fprintf (stderr, "%s: Pseudo/DirectColor depth %d unsupported\n",
898 ximage = read_jpeg_ximage (screen, visual, drawable, cmap,
899 filename, verbose_p);
900 if (!ximage) return False;
902 /* Scale it, if necessary...
904 compute_image_scaling (ximage->width, ximage->height,
905 win_width, win_height, verbose_p,
906 &srcx, &srcy, &destx, &desty, &w2, &h2);
907 if (ximage->width != w2 || ximage->height != h2)
908 if (! scale_ximage (screen, visual, ximage, w2, h2))
911 /* Allocate a colormap, if we need to...
913 if (class == PseudoColor || class == DirectColor)
915 allocate_cubic_colormap (screen, visual, cmap, verbose_p);
916 remap_image (screen, cmap, ximage, verbose_p);
919 /* Finally, put the resized image on the window.
921 clear_drawable (screen, drawable);
925 gc = XCreateGC (dpy, drawable, 0, &gcv);
926 XPutImage (dpy, drawable, gc, ximage,
927 srcx, srcy, destx, desty, ximage->width, ximage->height);
933 XDestroyImage (ximage);
938 #endif /* HAVE_JPEGLIB */
941 /* Reads the given image file and renders it on the Drawable.
942 Returns False if it fails.
945 display_file (Screen *screen, Window window, Drawable drawable,
946 const char *filename, Bool verbose_p)
949 fprintf (stderr, "%s: loading \"%s\"\n", progname, filename);
951 # if defined(HAVE_GDK_PIXBUF)
952 if (read_file_gdk (screen, window, drawable, filename, verbose_p))
954 # elif defined(HAVE_JPEGLIB)
955 if (read_file_jpeglib (screen, window, drawable, filename, verbose_p))
957 # else /* !(HAVE_GDK_PIXBUF || HAVE_JPEGLIB) */
958 /* shouldn't get here if we have no image-loading methods available. */
960 # endif /* !(HAVE_GDK_PIXBUF || HAVE_JPEGLIB) */
966 /* Invokes a sub-process and returns its output (presumably, a file to
967 load.) Free the string when done. video_p controls which program
971 get_filename_1 (Screen *screen, const char *directory, Bool video_p,
974 Display *dpy = DisplayOfScreen (screen);
984 av[ac++] = GETIMAGE_FILE_PROGRAM;
986 av[ac++] = "--verbose";
988 av[ac++] = (char *) directory;
992 av[ac++] = GETIMAGE_VIDEO_PROGRAM;
994 av[ac++] = "--verbose";
1002 fprintf (stderr, "%s: executing:", progname);
1003 for (i = 0; i < ac; i++)
1004 fprintf (stderr, " %s", av[i]);
1005 fprintf (stderr, "\n");
1010 sprintf (buf, "%s: error creating pipe", progname);
1018 switch ((int) (forked = fork ()))
1022 sprintf (buf, "%s: couldn't fork", progname);
1030 close (in); /* don't need this one */
1031 close (ConnectionNumber (dpy)); /* close display fd */
1033 if (dup2 (out, stdout_fd) < 0) /* pipe stdout */
1035 sprintf (buf, "%s: could not dup() a new stdout", progname);
1036 exit (-1); /* exits fork */
1039 execvp (av[0], av); /* shouldn't return. */
1040 exit (-1); /* exits fork */
1046 int wait_status = 0;
1047 FILE *f = fdopen (in, "r");
1050 close (out); /* don't need this one */
1052 fgets (buf, sizeof(buf)-1, f);
1055 /* Wait for the child to die. */
1056 waitpid (-1, &wait_status, 0);
1059 while (L && buf[L-1] == '\n')
1066 fprintf (stderr, "%s: file does not exist: \"%s\"\n",
1071 return strdup (buf);
1079 /* Returns a pathname to an image file. Free the string when you're done.
1082 get_filename (Screen *screen, const char *directory, Bool verbose_p)
1084 return get_filename_1 (screen, directory, False, verbose_p);
1088 /* Grabs a video frame to a file, and returns a pathname to that file.
1089 Delete that file when you are done with it (and free the string.)
1092 get_video_filename (Screen *screen, Bool verbose_p)
1094 return get_filename_1 (screen, 0, True, verbose_p);
1098 /* Grabs a video frame, and renders it on the Drawable.
1099 Returns False if it fails;
1102 display_video (Screen *screen, Window window, Drawable drawable,
1105 char *filename = get_video_filename (screen, verbose_p);
1111 fprintf (stderr, "%s: video grab failed.\n", progname);
1115 status = display_file (screen, window, drawable, filename, verbose_p);
1117 if (unlink (filename))
1120 sprintf (buf, "%s: rm %.100s", progname, filename);
1124 fprintf (stderr, "%s: rm %s\n", progname, filename);
1126 if (filename) free (filename);
1131 /* Grabs an image (from a file, video, or the desktop) and renders it on
1132 the Drawable. If `file' is specified, always use that file. Otherwise,
1133 select randomly, based on the other arguments.
1136 get_image (Screen *screen,
1137 Window window, Drawable drawable,
1145 Display *dpy = DisplayOfScreen (screen);
1146 enum { do_desk, do_video, do_image, do_bars } which = do_bars;
1150 if (! drawable_window_p (dpy, window))
1152 fprintf (stderr, "%s: 0x%lx is a pixmap, not a window!\n",
1153 progname, (unsigned long) window);
1157 if (file && stat (file, &st))
1159 fprintf (stderr, "%s: file \"%s\" does not exist\n", progname, file);
1165 fprintf (stderr, "%s: grabDesktopImages: %s\n",
1166 progname, desk_p ? "True" : "False");
1167 fprintf (stderr, "%s: grabVideoFrames: %s\n",
1168 progname, video_p ? "True" : "False");
1169 fprintf (stderr, "%s: chooseRandomImages: %s\n",
1170 progname, image_p ? "True" : "False");
1171 fprintf (stderr, "%s: imageDirectory: %s\n",
1172 progname, (file ? file : dir ? dir : ""));
1175 # if !(defined(HAVE_GDK_PIXBUF) || defined(HAVE_JPEGLIB))
1176 image_p = False; /* can't load images from files... */
1180 "%s: image file loading not available at compile-time\n",
1182 fprintf (stderr, "%s: can't load \"%s\"\n", progname, file);
1185 # endif /* !(HAVE_GDK_PIXBUF || HAVE_JPEGLIB) */
1196 if (verbose_p && image_p)
1198 "%s: no imageDirectory: turning off chooseRandomImages.\n",
1205 # error Error! This file definitely needs vroot.h!
1208 /* We can grab desktop images if:
1209 - the window is the real root window;
1210 - the window is the virtal root window;
1211 - the window is a toplevel window.
1212 We cannot grab desktop images if:
1213 - the window is a non-top-level window.
1217 if (!top_level_window_p (screen, window))
1222 "%s: 0x%x not top-level: turning off grabDesktopImages.\n",
1223 progname, (unsigned int) window);
1228 if (desk_p) count++;
1229 if (video_p) count++;
1230 if (image_p) count++;
1237 while (1) /* loop until we get one that's permitted */
1239 which = (random() % 3);
1240 if (which == do_desk && desk_p) break;
1241 if (which == do_video && video_p) break;
1242 if (which == do_image && image_p) break;
1243 if (++i > 200) abort();
1248 /* If we're to search a directory to find an image file, do so now.
1250 if (which == do_image && !file)
1252 file = get_filename (screen, dir, verbose_p);
1257 fprintf (stderr, "%s: no image files found.\n", progname);
1261 /* Now actually render something.
1263 if (which == do_bars)
1265 XWindowAttributes xgwa;
1268 fprintf (stderr, "%s: drawing colorbars.\n", progname);
1269 XGetWindowAttributes (dpy, window, &xgwa);
1270 draw_colorbars (screen, xgwa.visual, drawable, xgwa.colormap,
1274 else if (which == do_desk)
1278 XWindowAttributes xgwa;
1282 fprintf (stderr, "%s: grabbing desktop image\n", progname);
1283 grabscreen_verbose();
1285 gc = XCreateGC (dpy, drawable, 0, &gcv);
1286 XGetWindowAttributes (dpy, window, &xgwa);
1287 grab_screen_image (screen, window);
1288 XCopyArea (dpy, window, drawable, gc,
1289 0, 0, xgwa.width, xgwa.height, 0, 0);
1293 else if (which == do_image)
1295 if (! display_file (screen, window, drawable, file, verbose_p))
1298 else if (which == do_video)
1300 if (! display_video (screen, window, drawable, verbose_p))
1310 mapper (XrmDatabase *db, XrmBindingList bindings, XrmQuarkList quarks,
1311 XrmRepresentation *type, XrmValue *value, XPointer closure)
1314 for (i = 0; quarks[i]; i++)
1316 if (bindings[i] == XrmBindTightly)
1317 fprintf (stderr, (i == 0 ? "" : "."));
1318 else if (bindings[i] == XrmBindLoosely)
1319 fprintf (stderr, "*");
1321 fprintf (stderr, " ??? ");
1322 fprintf(stderr, "%s", XrmQuarkToString (quarks[i]));
1325 fprintf (stderr, ": %s\n", (char *) value->addr);
1332 #define USAGE "usage: %s [ -options... ] window-id [pixmap-id]\n" \
1334 " This program puts an image on the given window or pixmap.\n" \
1336 " It is used by those xscreensaver demos that operate on images.\n" \
1337 " The image may be a file loaded from disk, a frame grabbed from\n" \
1338 " the system's video camera, or a screenshot of the desktop,\n" \
1339 " depending on command-line options or the ~/.xscreensaver file.\n" \
1341 " Options include:\n" \
1343 " -display host:dpy.screen which display to use\n" \
1344 " -root draw to the root window\n" \
1345 " -verbose print diagnostics\n" \
1346 " -images / -no-images whether to allow image file loading\n" \
1347 " -video / -no-video whether to allow video grabs\n" \
1348 " -desktop / -no-desktop whether to allow desktop screen grabs\n"\
1349 " -directory <path> where to find image files to load\n" \
1350 " -file <filename> load this image file\n" \
1352 " The XScreenSaver Control Panel (xscreensaver-demo) lets you set the\n"\
1353 " defaults for these options in your ~/.xscreensaver file.\n" \
1357 main (int argc, char **argv)
1359 saver_preferences P;
1363 char *oprogname = progname;
1366 Window window = (Window) 0;
1367 Drawable drawable = (Drawable) 0;
1368 const char *window_str = 0;
1369 const char *drawable_str = 0;
1374 s = strrchr (progname, '/');
1375 if (s) progname = s+1;
1376 oprogname = progname;
1378 /* half-assed way of avoiding buffer-overrun attacks. */
1379 if (strlen (progname) >= 100) progname[100] = 0;
1382 # error Error! This file definitely needs vroot.h!
1385 /* We must read exactly the same resources as xscreensaver.
1386 That means we must have both the same progclass *and* progname,
1387 at least as far as the resource database is concerned. So,
1388 put "xscreensaver" in argv[0] while initializing Xt.
1390 progname = argv[0] = "xscreensaver";
1392 /* allow one dash or two. */
1393 for (i = 1; i < argc; i++)
1394 if (argv[i][0] == '-' && argv[i][1] == '-') argv[i]++;
1396 toplevel = XtAppInitialize (&app, progclass, 0, 0, &argc, argv,
1398 dpy = XtDisplay (toplevel);
1399 screen = XtScreen (toplevel);
1400 db = XtDatabase (dpy);
1401 XtGetApplicationNameAndClass (dpy, &s, &progclass);
1402 XSetErrorHandler (x_ehandler);
1405 /* Randomize -- only need to do this here because this program
1406 doesn't use the `screenhack.h' or `lockmore.h' APIs. */
1407 # undef ya_rand_init
1410 memset (&P, 0, sizeof(P));
1412 load_init_file (&P);
1414 progname = argv[0] = oprogname;
1416 for (i = 1; i < argc; i++)
1421 /* Have to re-process these, or else the .xscreensaver file
1422 has priority over the command line...
1424 if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "-verbose"))
1426 else if (!strcmp (argv[i], "-desktop")) P.grab_desktop_p = True;
1427 else if (!strcmp (argv[i], "-no-desktop")) P.grab_desktop_p = False;
1428 else if (!strcmp (argv[i], "-video")) P.grab_video_p = True;
1429 else if (!strcmp (argv[i], "-no-video")) P.grab_video_p = False;
1430 else if (!strcmp (argv[i], "-images")) P.random_image_p = True;
1431 else if (!strcmp (argv[i], "-no-images")) P.random_image_p = False;
1432 else if (!strcmp (argv[i], "-file")) file = argv[++i];
1433 else if (!strcmp (argv[i], "-directory") || !strcmp (argv[i], "-dir"))
1434 P.image_directory = argv[++i];
1435 else if (!strcmp (argv[i], "-root") || !strcmp (argv[i], "root"))
1439 fprintf (stderr, "%s: both %s and %s specified?\n",
1440 progname, argv[i], window_str);
1443 window_str = argv[i];
1444 window = (Window) RootWindowOfScreen (screen);
1446 else if ((1 == sscanf (argv[i], " 0x%lx %c", &w, &dummy) ||
1447 1 == sscanf (argv[i], " %ld %c", &w, &dummy)) &&
1452 fprintf (stderr, "%s: both %s and %s specified?\n",
1453 progname, drawable_str, argv[i]);
1458 drawable_str = argv[i];
1459 drawable = (Drawable) w;
1463 window_str = argv[i];
1464 window = (Window) w;
1469 if (argv[i][0] == '-')
1470 fprintf (stderr, "\n%s: unknown option \"%s\"\n",
1473 fprintf (stderr, "\n%s: unparsable window/pixmap ID: \"%s\"\n",
1476 fprintf (stderr, USAGE, progname);
1483 fprintf (stderr, "\n%s: no window ID specified!\n", progname);
1489 if (P.verbose_p) /* Print out all the resources we can see. */
1491 XrmName name = { 0 };
1492 XrmClass class = { 0 };
1494 XrmEnumerateDatabase (db, &name, &class, XrmEnumAllLevels, mapper,
1495 (XtPointer) &count);
1499 if (!window) abort();
1500 if (!drawable) drawable = window;
1502 get_image (screen, window, drawable, P.verbose_p,
1503 P.grab_desktop_p, P.grab_video_p, P.random_image_p,
1504 P.image_directory, file);