1 /* xscreensaver, Copyright (c) 1997, 1998, 2001 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
11 * Concept snarfed from Michael D. Bayne in
12 * http://www.go2net.com/internet/deep/1997/04/16/body.html
15 #include "screenhack.h"
16 #include <X11/Xutil.h>
19 #ifdef HAVE_XSHM_EXTENSION
22 static XShmSegmentInfo shm_info;
23 #endif /* HAVE_XSHM_EXTENSION */
25 static int offset = 0;
26 static XColor *colors = 0;
27 static int ncolors = 0;
29 static unsigned long fg_pixel = 0;
30 static unsigned long bg_pixel = 0;
33 init_moire (Display *dpy, Window window)
38 double fgs, fgv, bgs, bgv;
39 XWindowAttributes xgwa;
42 XGetWindowAttributes (dpy, window, &xgwa);
44 offset = get_integer_resource ("offset", "Integer");
45 if (offset < 2) offset = 2;
47 #ifdef HAVE_XSHM_EXTENSION
48 use_shm = get_boolean_resource("useSHM", "Boolean");
49 #endif /* HAVE_XSHM_EXTENSION */
54 for (i = 0; i < ncolors; i++)
55 XFreeColors (dpy, xgwa.colormap, &colors[i].pixel, 1, 0);
62 fg_pixel = WhitePixelOfScreen (DefaultScreenOfDisplay(dpy));
63 bg_pixel = BlackPixelOfScreen (DefaultScreenOfDisplay(dpy));
67 fg_pixel = get_pixel_resource ("foreground", "Foreground", dpy,
69 bg_pixel = get_pixel_resource ("background", "Background", dpy,
75 offset *= 20; /* compensate for lack of shading */
76 gcv.foreground = fg_pixel;
80 ncolors = get_integer_resource ("ncolors", "Integer");
81 if (ncolors < 2) ncolors = 2;
84 fgc.flags = bgc.flags = DoRed|DoGreen|DoBlue;
85 if (get_boolean_resource("random","Boolean"))
87 fgc.red = random() & 0xFFFF;
88 fgc.green = random() & 0xFFFF;
89 fgc.blue = random() & 0xFFFF;
90 bgc.red = random() & 0xFFFF;
91 bgc.green = random() & 0xFFFF;
92 bgc.blue = random() & 0xFFFF;
98 XQueryColor (dpy, xgwa.colormap, &fgc);
99 XQueryColor (dpy, xgwa.colormap, &bgc);
101 rgb_to_hsv (fgc.red, fgc.green, fgc.blue, &fgh, &fgs, &fgv);
102 rgb_to_hsv (bgc.red, bgc.green, bgc.blue, &bgh, &bgs, &bgv);
104 colors = (XColor *) malloc (sizeof (XColor) * (ncolors+2));
105 memset(colors, 0, (sizeof (XColor) * (ncolors+2)));
106 make_color_ramp (dpy, xgwa.colormap,
107 fgh, fgs, fgv, bgh, bgs, bgv,
110 if (ncolors != oncolors)
111 fprintf(stderr, "%s: got %d of %d requested colors.\n",
112 progname, ncolors, oncolors);
120 gcv.foreground = colors[0].pixel;
122 gc = XCreateGC (dpy, window, GCForeground, &gcv);
127 moire (Display *dpy, Window window, int offset, XColor *colors, int ncolors)
130 int factor = (random() % offset) + 1;
132 XWindowAttributes xgwa;
135 XGetWindowAttributes (dpy, window, &xgwa);
137 xo = (random() % xgwa.width) - xgwa.width/2;
138 yo = (random() % xgwa.height) - xgwa.height/2;
140 depth = visual_depth(DefaultScreenOfDisplay(dpy), xgwa.visual);
143 # ifdef HAVE_XSHM_EXTENSION
146 image = create_xshm_image(dpy, xgwa.visual, depth, ZPixmap, 0,
147 &shm_info, xgwa.width, 1);
151 # endif /* HAVE_XSHM_EXTENSION */
155 image = XCreateImage (dpy, xgwa.visual,
156 depth, ZPixmap, 0, /* depth, format, offset */
157 0, xgwa.width, 1, 8, 0); /* data, w, h, pad, bpl */
158 image->data = (char *) calloc(image->height, image->bytes_per_line);
161 for (y = 0; y < xgwa.height; y++)
163 for (x = 0; x < xgwa.width; x++)
167 double i = ((xx * xx) + (yy * yy)) / (double) factor;
169 gcv.foreground = ((((long) i) & 1) ? fg_pixel : bg_pixel);
171 gcv.foreground = colors[((long) i) % ncolors].pixel;
172 XPutPixel (image, x, 0, gcv.foreground);
175 # ifdef HAVE_XSHM_EXTENSION
177 XShmPutImage(dpy, window, gc, image, 0, 0, 0, y, xgwa.width, 1, False);
179 # endif /* HAVE_XSHM_EXTENSION */
180 XPutImage (dpy, window, gc, image, 0, 0, 0, y, xgwa.width, 1);
185 # ifdef HAVE_XSHM_EXTENSION
187 # endif /* HAVE_XSHM_EXTENSION */
194 # ifdef HAVE_XSHM_EXTENSION
196 destroy_xshm_image (dpy, image, &shm_info);
198 # endif /* HAVE_XSHM_EXTENSION */
199 XDestroyImage (image);
203 char *progclass = "Moire";
205 char *defaults [] = {
212 #ifdef HAVE_XSHM_EXTENSION
214 #endif /* HAVE_XSHM_EXTENSION */
218 XrmOptionDescRec options [] = {
219 { "-random", ".random", XrmoptionNoArg, "True" },
220 { "-no-random", ".random", XrmoptionNoArg, "False" },
221 { "-delay", ".delay", XrmoptionSepArg, 0 },
222 { "-ncolors", ".ncolors", XrmoptionSepArg, 0 },
223 { "-offset", ".offset", XrmoptionSepArg, 0 },
224 #ifdef HAVE_XSHM_EXTENSION
225 { "-shm", ".useSHM", XrmoptionNoArg, "True" },
226 { "-no-shm", ".useSHM", XrmoptionNoArg, "False" },
227 #endif /* HAVE_XSHM_EXTENSION */
232 screenhack (Display *dpy, Window window)
234 int delay = get_integer_resource ("delay", "Integer");
237 init_moire (dpy, window);
238 moire (dpy, window, offset, colors, ncolors);
240 screenhack_handle_events (dpy);