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"
49 #ifdef HAVE_GDK_PIXBUF
52 # include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
53 # else /* !HAVE_GTK2 */
54 # include <gdk-pixbuf/gdk-pixbuf-xlib.h>
55 # endif /* !HAVE_GTK2 */
56 #endif /* HAVE_GDK_PIXBUF */
59 # undef HAVE_GDK_PIXBUF
64 static char *defaults[] = {
65 #include "../driver/XScreenSaver_ad.h"
72 char *progclass = "XScreenSaver";
76 extern void grabscreen_verbose (void);
79 #define GETIMAGE_VIDEO_PROGRAM "xscreensaver-getimage-video"
80 #define GETIMAGE_FILE_PROGRAM "xscreensaver-getimage-file"
90 x_ehandler (Display *dpy, XErrorEvent *error)
92 fprintf (stderr, "\nX error in %s:\n", progname);
93 XmuPrintDefaultErrorMessage (dpy, error, stderr);
99 static Bool error_handler_hit_p = False;
102 ignore_all_errors_ehandler (Display *dpy, XErrorEvent *error)
104 error_handler_hit_p = True;
109 /* Returns True if the given Drawable is a Window; False if it's a Pixmap.
112 drawable_window_p (Display *dpy, Drawable d)
114 XErrorHandler old_handler;
115 XWindowAttributes xgwa;
118 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
119 error_handler_hit_p = False;
120 XGetWindowAttributes (dpy, d, &xgwa);
122 XSetErrorHandler (old_handler);
125 if (!error_handler_hit_p)
126 return True; /* It's a Window. */
128 return False; /* It's a Pixmap, or an invalid ID. */
132 /* Clear the window or pixmap to black, or its background color.
135 clear_drawable (Screen *screen, Drawable drawable)
137 Display *dpy = DisplayOfScreen (screen);
142 unsigned int w, h, bw, d;
143 XGetGeometry (dpy, drawable, &root, &x, &y, &w, &h, &bw, &d);
145 /* The window might have no-op background of None, so to clear it,
146 draw a black rectangle first, then do XClearWindow (in case the
147 actual background color is non-black...) */
149 /* #### really we should allocate "black" instead, but I'm lazy... */
150 gcv.foreground = BlackPixelOfScreen (screen);
151 gc = XCreateGC (dpy, drawable, GCForeground, &gcv);
152 XFillRectangle (dpy, drawable, gc, 0, 0, w, h);
154 if (drawable_window_p (dpy, drawable))
155 XClearWindow (dpy, (Window) drawable);
160 /* Figure out what kind of scaling/positioning we ought to do to display
161 a src-sized image in a dest-sized window/pixmap. Returns the width
162 and height to which the image should be scaled, and the position where
163 it should be displayed to center it.
166 compute_image_scaling (int src_w, int src_h,
167 int dest_w, int dest_h,
169 int *scaled_from_x_ret, int *scaled_from_y_ret,
170 int *scaled_to_x_ret, int *scaled_to_y_ret,
171 int *scaled_w_ret, int *scaled_h_ret)
173 int srcx, srcy, destx, desty;
175 Bool exact_fit_p = ((src_w == dest_w && src_h <= dest_h) ||
176 (src_h == dest_h && src_w <= dest_w));
178 if (!exact_fit_p) /* scale the image up or down */
180 float rw = (float) dest_w / src_w;
181 float rh = (float) dest_h / src_h;
182 float r = (rw < rh ? rw : rh);
187 if (pct < 95 || pct > 105) /* don't scale if it's close */
190 fprintf (stderr, "%s: scaling image by %d%% (%dx%d -> %dx%d)\n",
191 progname, pct, src_w, src_h, tw, th);
197 /* Center the image on the window/pixmap. */
200 destx = (dest_w - src_w) / 2;
201 desty = (dest_h - src_h) / 2;
202 if (destx < 0) srcx = -destx, destx = 0;
203 if (desty < 0) srcy = -desty, desty = 0;
205 if (dest_w < src_w) src_w = dest_w;
206 if (dest_h < src_h) src_h = dest_h;
208 *scaled_w_ret = src_w;
209 *scaled_h_ret = src_h;
210 *scaled_from_x_ret = srcx;
211 *scaled_from_y_ret = srcy;
212 *scaled_to_x_ret = destx;
213 *scaled_to_y_ret = desty;
216 fprintf (stderr, "%s: displaying %dx%d image at %d,%d.\n",
217 progname, src_w, src_h, destx, desty);
221 #ifdef HAVE_GDK_PIXBUF
223 /* Reads the given image file and renders it on the Drawable, using GDK.
224 Returns False if it fails.
227 read_file_gdk (Screen *screen, Window window, Drawable drawable,
228 const char *filename, Bool verbose_p)
231 Display *dpy = DisplayOfScreen (screen);
232 unsigned int win_width, win_height;
235 # endif /* HAVE_GTK2 */
241 XWindowAttributes xgwa;
242 XGetWindowAttributes (dpy, window, &xgwa);
243 screen = xgwa.screen;
244 XGetGeometry (dpy, drawable,
245 &root, &x, &y, &win_width, &win_height, &bw, &d);
248 gdk_pixbuf_xlib_init (dpy, screen_number (screen));
251 # else /* !HAVE_GTK2 */
252 xlib_rgb_init (dpy, screen);
253 # endif /* !HAVE_GTK2 */
255 pb = gdk_pixbuf_new_from_file (filename
258 # endif /* HAVE_GTK2 */
263 fprintf (stderr, "%s: unable to load \"%s\"\n", progname, filename);
265 if (gerr && gerr->message && *gerr->message)
266 fprintf (stderr, "%s: reason: %s\n", progname, gerr->message);
267 # endif /* HAVE_GTK2 */
272 int w = gdk_pixbuf_get_width (pb);
273 int h = gdk_pixbuf_get_height (pb);
274 int srcx, srcy, destx, desty, w2, h2;
276 compute_image_scaling (w, h, win_width, win_height, verbose_p,
277 &srcx, &srcy, &destx, &desty, &w2, &h2);
278 if (w != w2 || h != h2)
280 GdkPixbuf *pb2 = gdk_pixbuf_scale_simple (pb, w2, h2,
281 GDK_INTERP_BILINEAR);
284 gdk_pixbuf_unref (pb);
290 fprintf (stderr, "%s: out of memory when scaling?\n", progname);
293 clear_drawable (screen, drawable);
295 /* #### Note that this always uses the default colormap! Morons!
296 Owen says that in Gnome 2.0, I should try using
297 gdk_pixbuf_render_pixmap_and_mask_for_colormap() instead.
300 gdk_pixbuf_xlib_render_to_drawable_alpha (pb, drawable,
301 srcx, srcy, destx, desty,
303 GDK_PIXBUF_ALPHA_FULL, 127,
304 XLIB_RGB_DITHER_NORMAL,
312 #endif /* HAVE_GDK_PIXBUF */
318 /* Allocates a colormap that makes a PseudoColor or DirectColor
319 visual behave like a TrueColor visual of the same depth.
322 allocate_cubic_colormap (Screen *screen, Visual *visual, Colormap cmap,
325 Display *dpy = DisplayOfScreen (screen);
326 int nr, ng, nb, cells;
332 depth = visual_depth (screen, visual);
336 case 8: nr = 3; ng = 3; nb = 2; cells = 256; break;
337 case 12: nr = 4; ng = 4; nb = 4; cells = 4096; break;
338 default: abort(); break;
341 memset(colors, 0, sizeof(colors));
342 for (r = 0; r < (1 << nr); r++)
343 for (g = 0; g < (1 << ng); g++)
344 for (b = 0; b < (1 << nb); b++)
346 i = (r | (g << nr) | (b << (nr + ng)));
348 colors[i].flags = DoRed|DoGreen|DoBlue;
351 colors[i].red = ((r << 13) | (r << 10) | (r << 7) |
352 (r << 4) | (r << 1));
353 colors[i].green = ((g << 13) | (g << 10) | (g << 7) |
354 (g << 4) | (g << 1));
355 colors[i].blue = ((b << 14) | (b << 12) | (b << 10) |
356 (b << 8) | (b << 6) | (b << 4) |
361 colors[i].red = (r << 12) | (r << 8) | (r << 4) | r;
362 colors[i].green = (g << 12) | (g << 8) | (g << 4) | g;
363 colors[i].blue = (b << 12) | (b << 8) | (b << 4) | b;
370 int interleave = cells / 8; /* skip around, rather than allocating in
371 order, so that we get better coverage if
372 we can't allocated all of them. */
373 for (j = 0; j < interleave; j++)
374 for (i = 0; i < cells; i += interleave)
375 if (XAllocColor (dpy, cmap, &colors[i + j]))
379 fprintf (stderr, "%s: allocated %d of %d colors for cubic map\n",
380 progname, allocated, cells);
384 /* Find the pixel index that is closest to the given color
385 (using linear distance in RGB space -- which is far from the best way.)
388 find_closest_pixel (XColor *colors, int ncolors,
389 unsigned long r, unsigned long g, unsigned long b)
391 unsigned long distance = ~0;
396 for (i = 0; i < ncolors; i++)
401 rd = r - colors[i].red;
402 gd = g - colors[i].green;
403 bd = b - colors[i].blue;
404 if (rd < 0) rd = -rd;
405 if (gd < 0) gd = -gd;
406 if (bd < 0) bd = -bd;
407 d = (rd << 1) + (gd << 2) + bd;
422 /* Given an XImage with 8-bit or 12-bit RGB data, convert it to be
423 displayable with the given X colormap. The farther from a perfect
424 color cube the contents of the colormap are, the lossier the
425 transformation will be. No dithering is done.
428 remap_image (Screen *screen, Colormap cmap, XImage *image, Bool verbose_p)
430 Display *dpy = DisplayOfScreen (screen);
431 unsigned long map[4097];
436 if (image->depth == 8)
438 else if (image->depth == 12)
443 memset(map, -1, sizeof(*map));
444 memset(colors, -1, sizeof(*colors));
446 for (i = 0; i < cells; i++)
448 XQueryColors (dpy, cmap, colors, cells);
451 fprintf(stderr, "%s: building color cube for %d bit image\n",
452 progname, image->depth);
454 for (i = 0; i < cells; i++)
456 unsigned short r, g, b;
460 /* "RRR GGG BB" In an 8 bit map. Convert that to
461 "RRR RRR RR" "GGG GGG GG" "BB BB BB BB" to give
467 r = ((r << 13) | (r << 10) | (r << 7) | (r << 4) | (r << 1));
468 g = ((g << 13) | (g << 10) | (g << 7) | (g << 4) | (g << 1));
469 b = ((b << 14) | (b << 12) | (b << 10) | (b << 8) |
470 (b << 6) | (b << 4) | (b << 2) | b);
474 /* "RRRR GGGG BBBB" In a 12 bit map. Convert that to
475 "RRRR RRRR" "GGGG GGGG" "BBBB BBBB" to give an even
478 g = (i & 0x0F0) >> 4;
479 b = (i & 0xF00) >> 8;
481 r = (r << 12) | (r << 8) | (r << 4) | r;
482 g = (g << 12) | (g << 8) | (g << 4) | g;
483 b = (b << 12) | (b << 8) | (b << 4) | b;
486 map[i] = find_closest_pixel (colors, cells, r, g, b);
490 fprintf(stderr, "%s: remapping colors in %d bit image\n",
491 progname, image->depth);
493 for (y = 0; y < image->height; y++)
494 for (x = 0; x < image->width; x++)
496 unsigned long pixel = XGetPixel(image, x, y);
497 if (pixel >= cells) abort();
498 XPutPixel(image, x, y, map[pixel]);
503 /* If the file has a PPM (P6) on it, read it and return an XImage.
504 Otherwise, rewind the fd back to the beginning, and return 0.
507 maybe_read_ppm (Screen *screen, Visual *visual,
508 const char *filename, FILE *in, Bool verbose_p)
510 Display *dpy = DisplayOfScreen (screen);
511 int depth = visual_depth (screen, visual);
517 int x, y, w, h, maxval;
520 if (fstat (fileno (in), &st))
524 buf = (char *) malloc (bufsiz + 1);
527 fprintf (stderr, "%s: out of memory loading %d byte PPM file %s\n",
528 progname, bufsiz, filename);
532 if (! (s = fgets (buf, bufsiz, in))) /* line 1 */
535 if (!strncmp (buf, "\107\111", 2))
537 fprintf (stderr, "%s: %s: sorry, GIF files not supported"
538 " when compiled with JPEGlib instead of GDK_Pixbuf.\n",
542 else if (!strncmp (buf, "\211\120", 2))
544 fprintf (stderr, "%s: %s: sorry, PNG files not supported"
545 " when compiled with JPEGlib instead of GDK_Pixbuf.\n",
550 if (strncmp (s, "P6", 2))
553 if (! (s = fgets (buf, bufsiz, in))) /* line 2 */
555 if (2 != sscanf (s, " %d %d %c", &w, &h, &dummy))
557 fprintf (stderr, "%s: %s: invalid PPM (line 2)\n", progname, filename);
561 if (! (s = fgets (buf, bufsiz, in))) /* line 3 */
563 if (1 != sscanf (s, " %d %c", &maxval, &dummy))
565 fprintf (stderr, "%s: %s: invalid PPM (line 3)\n", progname, filename);
570 fprintf (stderr, "%s: %s: unparsable PPM: maxval is %d\n",
571 progname, filename, maxval);
575 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, 0,
578 ximage->data = (unsigned char *)
579 calloc (ximage->height, ximage->bytes_per_line);
580 if (!ximage || !ximage->data)
582 fprintf (stderr, "%s: out of memory loading %dx%d PPM file %s\n",
583 progname, ximage->width, ximage->height, filename);
589 while ((i = fread (s, 1, j, in)) > 0)
593 for (y = 0; y < ximage->height; y++)
594 for (x = 0; x < ximage->width; x++)
596 unsigned char r = buf[i++];
597 unsigned char g = buf[i++];
598 unsigned char b = buf[i++];
602 pixel = (r << 16) | (g << 8) | b;
604 pixel = ((r >> 5) | ((g >> 5) << 3) | ((b >> 6) << 6));
605 else if (depth == 12)
606 pixel = ((r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8));
607 else if (depth == 16 || depth == 15)
608 pixel = (((r >> 3) << 10) | ((g >> 3) << 5) | ((b >> 3)));
612 XPutPixel (ximage, x, y, pixel);
620 if (ximage && ximage->data)
625 if (ximage) XDestroyImage (ximage);
626 fseek (in, 0, SEEK_SET);
632 struct jpeg_error_mgr pub; /* this is what passes for subclassing in C */
633 const char *filename;
638 } getimg_jpg_error_mgr;
642 jpg_output_message (j_common_ptr cinfo)
644 getimg_jpg_error_mgr *err = (getimg_jpg_error_mgr *) cinfo->err;
645 char buf[JMSG_LENGTH_MAX];
646 cinfo->err->format_message (cinfo, buf);
647 fprintf (stderr, "%s: %s: %s\n", progname, err->filename, buf);
652 jpg_error_exit (j_common_ptr cinfo)
654 getimg_jpg_error_mgr *err = (getimg_jpg_error_mgr *) cinfo->err;
655 cinfo->err->output_message (cinfo);
656 draw_colorbars (err->screen, err->visual, err->drawable, err->cmap,
658 XSync (DisplayOfScreen (err->screen), False);
663 /* Reads a JPEG file, returns an RGB XImage of it.
666 read_jpeg_ximage (Screen *screen, Visual *visual, Drawable drawable,
667 Colormap cmap, const char *filename, Bool verbose_p)
669 Display *dpy = DisplayOfScreen (screen);
670 int depth = visual_depth (screen, visual);
674 struct jpeg_decompress_struct cinfo;
675 getimg_jpg_error_mgr jerr;
676 JSAMPARRAY scanbuf = 0;
679 jerr.filename = filename;
680 jerr.screen = screen;
681 jerr.visual = visual;
682 jerr.drawable = drawable;
685 if (! (depth >= 15 || depth == 12 || depth == 8))
687 fprintf (stderr, "%s: unsupported depth: %d\n", progname, depth);
691 in = fopen (filename, "rb");
694 fprintf (stderr, "%s: %s: unreadable\n", progname, filename);
698 /* Check to see if it's a PPM, and if so, read that instead of using
699 the JPEG library. Yeah, this is all modular and stuff.
701 if ((ximage = maybe_read_ppm (screen, visual, filename, in, verbose_p)))
707 cinfo.err = jpeg_std_error (&jerr.pub);
708 jerr.pub.output_message = jpg_output_message;
709 jerr.pub.error_exit = jpg_error_exit;
711 jpeg_create_decompress (&cinfo);
712 jpeg_stdio_src (&cinfo, in);
713 jpeg_read_header (&cinfo, TRUE);
715 /* set some decode parameters */
716 cinfo.out_color_space = JCS_RGB;
717 cinfo.quantize_colors = FALSE;
719 jpeg_start_decompress (&cinfo);
721 ximage = XCreateImage (dpy, visual, depth, ZPixmap, 0, 0,
722 cinfo.output_width, cinfo.output_height,
725 ximage->data = (unsigned char *)
726 calloc (ximage->height, ximage->bytes_per_line);
728 if (ximage && ximage->data)
729 scanbuf = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE,
730 cinfo.rec_outbuf_height *
732 cinfo.output_components,
734 if (!ximage || !ximage->data || !scanbuf)
736 fprintf (stderr, "%s: out of memory loading %dx%d file %s\n",
737 progname, ximage->width, ximage->height, filename);
742 while (cinfo.output_scanline < cinfo.output_height)
744 int n = jpeg_read_scanlines (&cinfo, scanbuf, 1);
746 for (i = 0; i < n; i++)
749 for (x = 0; x < ximage->width; x++)
751 int j = x * cinfo.num_components;
752 unsigned char r = scanbuf[i][j];
753 unsigned char g = scanbuf[i][j+1];
754 unsigned char b = scanbuf[i][j+2];
758 pixel = (r << 16) | (g << 8) | b;
760 pixel = ((r >> 5) | ((g >> 5) << 3) | ((b >> 6) << 6));
761 else if (depth == 12)
762 pixel = ((r >> 4) | ((g >> 4) << 4) | ((b >> 4) << 8));
763 else if (depth == 16 || depth == 15)
764 /* Gah! I don't understand why these are in the other
766 pixel = (((r >> 3) << 10) | ((g >> 3) << 5) | ((b >> 3)));
770 XPutPixel (ximage, x, y, pixel);
776 if (cinfo.output_scanline < cinfo.output_height)
777 /* don't goto FAIL -- we might have viewable partial data. */
778 jpeg_abort_decompress (&cinfo);
780 jpeg_finish_decompress (&cinfo);
782 jpeg_destroy_decompress (&cinfo);
790 if (ximage && ximage->data)
795 if (ximage) XDestroyImage (ximage);
796 if (scanbuf) free (scanbuf);
801 /* Scales an XImage, modifying it in place.
802 If out of memory, returns False, and the XImage will have been
806 scale_ximage (Screen *screen, Visual *visual,
807 XImage *ximage, int new_width, int new_height)
809 Display *dpy = DisplayOfScreen (screen);
810 int depth = visual_depth (screen, visual);
812 double xscale, yscale;
814 XImage *ximage2 = XCreateImage (dpy, visual, depth,
816 new_width, new_height, 8, 0);
817 ximage2->data = (unsigned char *)
818 calloc (ximage2->height, ximage2->bytes_per_line);
822 fprintf (stderr, "%s: out of memory scaling %dx%d image to %dx%d\n",
824 ximage->width, ximage->height,
825 ximage2->width, ximage2->height);
826 if (ximage->data) free (ximage->data);
827 if (ximage2->data) free (ximage2->data);
830 XDestroyImage (ximage);
831 XDestroyImage (ximage2);
835 /* Brute force scaling... */
836 xscale = (double) ximage->width / ximage2->width;
837 yscale = (double) ximage->height / ximage2->height;
838 for (y = 0; y < ximage2->height; y++)
839 for (x = 0; x < ximage2->width; x++)
840 XPutPixel (ximage2, x, y,
841 XGetPixel (ximage, x * xscale, y * yscale));
846 (*ximage) = (*ximage2);
849 XDestroyImage (ximage2);
855 /* Reads the given image file and renders it on the Drawable, using JPEG lib.
856 Returns False if it fails.
859 read_file_jpeglib (Screen *screen, Window window, Drawable drawable,
860 const char *filename, Bool verbose_p)
862 Display *dpy = DisplayOfScreen (screen);
867 unsigned int win_width, win_height, win_depth;
868 int srcx, srcy, destx, desty, w2, h2;
874 XWindowAttributes xgwa;
876 XGetWindowAttributes (dpy, window, &xgwa);
877 screen = xgwa.screen;
878 visual = xgwa.visual;
879 cmap = xgwa.colormap;
881 XGetGeometry (dpy, drawable,
882 &root, &x, &y, &win_width, &win_height, &bw, &win_depth);
885 /* Make sure we're not on some weirdo visual...
887 class = visual_class (screen, visual);
888 depth = visual_depth (screen, visual);
889 if ((class == PseudoColor || class == DirectColor) &&
890 (depth != 8 && depth != 12))
892 fprintf (stderr, "%s: Pseudo/DirectColor depth %d unsupported\n",
899 ximage = read_jpeg_ximage (screen, visual, drawable, cmap,
900 filename, verbose_p);
901 if (!ximage) return False;
903 /* Scale it, if necessary...
905 compute_image_scaling (ximage->width, ximage->height,
906 win_width, win_height, verbose_p,
907 &srcx, &srcy, &destx, &desty, &w2, &h2);
908 if (ximage->width != w2 || ximage->height != h2)
909 if (! scale_ximage (screen, visual, ximage, w2, h2))
912 /* Allocate a colormap, if we need to...
914 if (class == PseudoColor || class == DirectColor)
916 allocate_cubic_colormap (screen, visual, cmap, verbose_p);
917 remap_image (screen, cmap, ximage, verbose_p);
920 /* Finally, put the resized image on the window.
922 clear_drawable (screen, drawable);
926 gc = XCreateGC (dpy, drawable, 0, &gcv);
927 XPutImage (dpy, drawable, gc, ximage,
928 srcx, srcy, destx, desty, ximage->width, ximage->height);
934 XDestroyImage (ximage);
939 #endif /* HAVE_JPEGLIB */
942 /* Reads the given image file and renders it on the Drawable.
943 Returns False if it fails.
946 display_file (Screen *screen, Window window, Drawable drawable,
947 const char *filename, Bool verbose_p)
950 fprintf (stderr, "%s: loading \"%s\"\n", progname, filename);
952 # if defined(HAVE_GDK_PIXBUF)
953 if (read_file_gdk (screen, window, drawable, filename, verbose_p))
955 # elif defined(HAVE_JPEGLIB)
956 if (read_file_jpeglib (screen, window, drawable, filename, verbose_p))
958 # else /* !(HAVE_GDK_PIXBUF || HAVE_JPEGLIB) */
959 /* shouldn't get here if we have no image-loading methods available. */
961 # endif /* !(HAVE_GDK_PIXBUF || HAVE_JPEGLIB) */
967 /* Invokes a sub-process and returns its output (presumably, a file to
968 load.) Free the string when done. video_p controls which program
972 get_filename_1 (Screen *screen, const char *directory, Bool video_p,
975 Display *dpy = DisplayOfScreen (screen);
985 av[ac++] = GETIMAGE_FILE_PROGRAM;
987 av[ac++] = "--verbose";
989 av[ac++] = (char *) directory;
993 av[ac++] = GETIMAGE_VIDEO_PROGRAM;
995 av[ac++] = "--verbose";
1003 fprintf (stderr, "%s: executing:", progname);
1004 for (i = 0; i < ac; i++)
1005 fprintf (stderr, " %s", av[i]);
1006 fprintf (stderr, "\n");
1011 sprintf (buf, "%s: error creating pipe", progname);
1019 switch ((int) (forked = fork ()))
1023 sprintf (buf, "%s: couldn't fork", progname);
1031 close (in); /* don't need this one */
1032 close (ConnectionNumber (dpy)); /* close display fd */
1034 if (dup2 (out, stdout_fd) < 0) /* pipe stdout */
1036 sprintf (buf, "%s: could not dup() a new stdout", progname);
1037 exit (-1); /* exits fork */
1040 execvp (av[0], av); /* shouldn't return. */
1041 exit (-1); /* exits fork */
1047 int wait_status = 0;
1048 FILE *f = fdopen (in, "r");
1051 close (out); /* don't need this one */
1053 fgets (buf, sizeof(buf)-1, f);
1056 /* Wait for the child to die. */
1057 waitpid (-1, &wait_status, 0);
1060 while (L && buf[L-1] == '\n')
1067 fprintf (stderr, "%s: file does not exist: \"%s\"\n",
1072 return strdup (buf);
1080 /* Returns a pathname to an image file. Free the string when you're done.
1083 get_filename (Screen *screen, const char *directory, Bool verbose_p)
1085 return get_filename_1 (screen, directory, False, verbose_p);
1089 /* Grabs a video frame to a file, and returns a pathname to that file.
1090 Delete that file when you are done with it (and free the string.)
1093 get_video_filename (Screen *screen, Bool verbose_p)
1095 return get_filename_1 (screen, 0, True, verbose_p);
1099 /* Grabs a video frame, and renders it on the Drawable.
1100 Returns False if it fails;
1103 display_video (Screen *screen, Window window, Drawable drawable,
1106 char *filename = get_video_filename (screen, verbose_p);
1112 fprintf (stderr, "%s: video grab failed.\n", progname);
1116 status = display_file (screen, window, drawable, filename, verbose_p);
1118 if (unlink (filename))
1121 sprintf (buf, "%s: rm %.100s", progname, filename);
1125 fprintf (stderr, "%s: rm %s\n", progname, filename);
1127 if (filename) free (filename);
1132 /* Grabs an image (from a file, video, or the desktop) and renders it on
1133 the Drawable. If `file' is specified, always use that file. Otherwise,
1134 select randomly, based on the other arguments.
1137 get_image (Screen *screen,
1138 Window window, Drawable drawable,
1146 Display *dpy = DisplayOfScreen (screen);
1147 enum { do_desk, do_video, do_image, do_bars } which = do_bars;
1151 if (! drawable_window_p (dpy, window))
1153 fprintf (stderr, "%s: 0x%lx is a pixmap, not a window!\n",
1154 progname, (unsigned long) window);
1158 if (file && stat (file, &st))
1160 fprintf (stderr, "%s: file \"%s\" does not exist\n", progname, file);
1166 fprintf (stderr, "%s: grabDesktopImages: %s\n",
1167 progname, desk_p ? "True" : "False");
1168 fprintf (stderr, "%s: grabVideoFrames: %s\n",
1169 progname, video_p ? "True" : "False");
1170 fprintf (stderr, "%s: chooseRandomImages: %s\n",
1171 progname, image_p ? "True" : "False");
1172 fprintf (stderr, "%s: imageDirectory: %s\n",
1173 progname, (file ? file : dir ? dir : ""));
1176 # if !(defined(HAVE_GDK_PIXBUF) || defined(HAVE_JPEGLIB))
1177 image_p = False; /* can't load images from files... */
1181 "%s: image file loading not available at compile-time\n",
1183 fprintf (stderr, "%s: can't load \"%s\"\n", progname, file);
1186 # endif /* !(HAVE_GDK_PIXBUF || HAVE_JPEGLIB) */
1197 if (verbose_p && image_p)
1199 "%s: no imageDirectory: turning off chooseRandomImages.\n",
1206 # error Error! This file definitely needs vroot.h!
1209 /* We can grab desktop images if:
1210 - the window is the real root window;
1211 - the window is the virtal root window;
1212 - the window is a toplevel window.
1213 We cannot grab desktop images if:
1214 - the window is a non-top-level window.
1218 if (!top_level_window_p (screen, window))
1223 "%s: 0x%x not top-level: turning off grabDesktopImages.\n",
1224 progname, (unsigned int) window);
1229 if (desk_p) count++;
1230 if (video_p) count++;
1231 if (image_p) count++;
1238 while (1) /* loop until we get one that's permitted */
1240 which = (random() % 3);
1241 if (which == do_desk && desk_p) break;
1242 if (which == do_video && video_p) break;
1243 if (which == do_image && image_p) break;
1244 if (++i > 200) abort();
1249 /* If we're to search a directory to find an image file, do so now.
1251 if (which == do_image && !file)
1253 file = get_filename (screen, dir, verbose_p);
1258 fprintf (stderr, "%s: no image files found.\n", progname);
1262 /* Now actually render something.
1264 if (which == do_bars)
1266 XWindowAttributes xgwa;
1269 fprintf (stderr, "%s: drawing colorbars.\n", progname);
1270 XGetWindowAttributes (dpy, window, &xgwa);
1271 draw_colorbars (screen, xgwa.visual, drawable, xgwa.colormap,
1275 else if (which == do_desk)
1279 XWindowAttributes xgwa;
1283 fprintf (stderr, "%s: grabbing desktop image\n", progname);
1284 grabscreen_verbose();
1286 gc = XCreateGC (dpy, drawable, 0, &gcv);
1287 XGetWindowAttributes (dpy, window, &xgwa);
1288 grab_screen_image (screen, window);
1289 XCopyArea (dpy, window, drawable, gc,
1290 0, 0, xgwa.width, xgwa.height, 0, 0);
1294 else if (which == do_image)
1296 if (! display_file (screen, window, drawable, file, verbose_p))
1299 else if (which == do_video)
1301 if (! display_video (screen, window, drawable, verbose_p))
1311 mapper (XrmDatabase *db, XrmBindingList bindings, XrmQuarkList quarks,
1312 XrmRepresentation *type, XrmValue *value, XPointer closure)
1315 for (i = 0; quarks[i]; i++)
1317 if (bindings[i] == XrmBindTightly)
1318 fprintf (stderr, (i == 0 ? "" : "."));
1319 else if (bindings[i] == XrmBindLoosely)
1320 fprintf (stderr, "*");
1322 fprintf (stderr, " ??? ");
1323 fprintf(stderr, "%s", XrmQuarkToString (quarks[i]));
1326 fprintf (stderr, ": %s\n", (char *) value->addr);
1333 #define USAGE "usage: %s [ -options... ] window-id [pixmap-id]\n" \
1337 " %s puts an image on the given window or pixmap.\n" \
1339 " It is used by those xscreensaver demos that operate on images.\n" \
1340 " The image may be a file loaded from disk, a frame grabbed from\n" \
1341 " the system's video camera, or a screenshot of the desktop,\n" \
1342 " depending on command-line options or the ~/.xscreensaver file.\n" \
1344 " Options include:\n" \
1346 " -display host:dpy.screen which display to use\n" \
1347 " -root draw to the root window\n" \
1348 " -verbose print diagnostics\n" \
1349 " -images / -no-images whether to allow image file loading\n" \
1350 " -video / -no-video whether to allow video grabs\n" \
1351 " -desktop / -no-desktop whether to allow desktop screen grabs\n"\
1352 " -directory <path> where to find image files to load\n" \
1353 " -file <filename> load this image file\n" \
1355 " The XScreenSaver Control Panel (xscreensaver-demo) lets you set the\n"\
1356 " defaults for these options in your ~/.xscreensaver file.\n" \
1360 main (int argc, char **argv)
1362 saver_preferences P;
1366 char *oprogname = progname;
1370 Window window = (Window) 0;
1371 Drawable drawable = (Drawable) 0;
1372 const char *window_str = 0;
1373 const char *drawable_str = 0;
1378 s = strrchr (progname, '/');
1379 if (s) progname = s+1;
1380 oprogname = progname;
1382 /* half-assed way of avoiding buffer-overrun attacks. */
1383 if (strlen (progname) >= 100) progname[100] = 0;
1386 # error Error! This file definitely needs vroot.h!
1389 /* Get the version number, for error messages. */
1391 char *v = (char *) strdup(strchr(screensaver_id, ' '));
1392 char *s1, *s2, *s3, *s4;
1393 s1 = (char *) strchr(v, ' '); s1++;
1394 s2 = (char *) strchr(s1, ' ');
1395 s3 = (char *) strchr(v, '('); s3++;
1396 s4 = (char *) strchr(s3, ')');
1399 sprintf (version, "Part of XScreenSaver %s -- %s.", s1, s3);
1403 /* We must read exactly the same resources as xscreensaver.
1404 That means we must have both the same progclass *and* progname,
1405 at least as far as the resource database is concerned. So,
1406 put "xscreensaver" in argv[0] while initializing Xt.
1408 progname = argv[0] = "xscreensaver";
1410 /* allow one dash or two. */
1411 for (i = 1; i < argc; i++)
1412 if (argv[i][0] == '-' && argv[i][1] == '-') argv[i]++;
1414 toplevel = XtAppInitialize (&app, progclass, 0, 0, &argc, argv,
1416 dpy = XtDisplay (toplevel);
1417 screen = XtScreen (toplevel);
1418 db = XtDatabase (dpy);
1419 XtGetApplicationNameAndClass (dpy, &s, &progclass);
1420 XSetErrorHandler (x_ehandler);
1423 /* Randomize -- only need to do this here because this program
1424 doesn't use the `screenhack.h' or `lockmore.h' APIs. */
1425 # undef ya_rand_init
1428 memset (&P, 0, sizeof(P));
1430 load_init_file (&P);
1432 progname = argv[0] = oprogname;
1434 for (i = 1; i < argc; i++)
1439 /* Have to re-process these, or else the .xscreensaver file
1440 has priority over the command line...
1442 if (!strcmp (argv[i], "-v") || !strcmp (argv[i], "-verbose"))
1444 else if (!strcmp (argv[i], "-desktop")) P.grab_desktop_p = True;
1445 else if (!strcmp (argv[i], "-no-desktop")) P.grab_desktop_p = False;
1446 else if (!strcmp (argv[i], "-video")) P.grab_video_p = True;
1447 else if (!strcmp (argv[i], "-no-video")) P.grab_video_p = False;
1448 else if (!strcmp (argv[i], "-images")) P.random_image_p = True;
1449 else if (!strcmp (argv[i], "-no-images")) P.random_image_p = False;
1450 else if (!strcmp (argv[i], "-file")) file = argv[++i];
1451 else if (!strcmp (argv[i], "-directory") || !strcmp (argv[i], "-dir"))
1452 P.image_directory = argv[++i];
1453 else if (!strcmp (argv[i], "-root") || !strcmp (argv[i], "root"))
1457 fprintf (stderr, "%s: both %s and %s specified?\n",
1458 progname, argv[i], window_str);
1461 window_str = argv[i];
1462 window = (Window) RootWindowOfScreen (screen);
1464 else if ((1 == sscanf (argv[i], " 0x%lx %c", &w, &dummy) ||
1465 1 == sscanf (argv[i], " %ld %c", &w, &dummy)) &&
1470 fprintf (stderr, "%s: both %s and %s specified?\n",
1471 progname, drawable_str, argv[i]);
1476 drawable_str = argv[i];
1477 drawable = (Drawable) w;
1481 window_str = argv[i];
1482 window = (Window) w;
1487 if (argv[i][0] == '-')
1488 fprintf (stderr, "\n%s: unknown option \"%s\"\n",
1491 fprintf (stderr, "\n%s: unparsable window/pixmap ID: \"%s\"\n",
1494 fprintf (stderr, USAGE, progname, version, progname);
1501 fprintf (stderr, "\n%s: no window ID specified!\n", progname);
1507 if (P.verbose_p) /* Print out all the resources we can see. */
1509 XrmName name = { 0 };
1510 XrmClass class = { 0 };
1512 XrmEnumerateDatabase (db, &name, &class, XrmEnumAllLevels, mapper,
1513 (XtPointer) &count);
1517 if (!window) abort();
1518 if (!drawable) drawable = window;
1520 get_image (screen, window, drawable, P.verbose_p,
1521 P.grab_desktop_p, P.grab_video_p, P.random_image_p,
1522 P.image_directory, file);