http://ftp.x.org/contrib/applications/xscreensaver-2.23.tar.gz
[xscreensaver] / hacks / rorschach.c
1 /* xscreensaver, Copyright (c) 1992, 1996 Jamie Zawinski <jwz@netscape.com>
2  *
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 
9  * implied warranty.
10  *
11  * 19971004: Johannes Keukelaar <johannes@nada.kth.se>: Use helix screen
12  *           eraser.
13  */
14
15 #include "screenhack.h"
16 #include "erase.h"
17
18 static GC draw_gc, erase_gc;
19 static unsigned int default_fg_pixel;
20 static int iterations, offset;
21 static Bool xsym, ysym;
22 static int sleep_time;
23
24 static void
25 init_rorschach (Display *dpy, Window window)
26 {
27   XGCValues gcv;
28   Colormap cmap;
29   XWindowAttributes xgwa;
30   XGetWindowAttributes (dpy, window, &xgwa);
31   cmap = xgwa.colormap;
32   gcv.foreground = default_fg_pixel =
33     get_pixel_resource ("foreground", "Foreground", dpy, cmap);
34   draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
35   gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
36   erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
37   iterations = get_integer_resource ("iterations", "Integer");
38   offset = get_integer_resource ("offset", "Integer");
39   if (offset <= 0) offset = 3;
40   if (iterations < 10) iterations = 10;
41   xsym = get_boolean_resource ("xsymmetry", "Symmetry");
42   ysym = get_boolean_resource ("ysymmetry", "Symmetry");
43 }
44
45 static void
46 hurm (Display *dpy, Window window)
47 {
48   Colormap cmap;
49   XWindowAttributes xgwa;
50   int xlim, ylim, x, y, i, got_color = 0;
51   XPoint points [4];
52   XColor color;
53   XClearWindow (dpy, window);
54   XGetWindowAttributes (dpy, window, &xgwa);
55   xlim = xgwa.width;
56   ylim = xgwa.height;
57   cmap = xgwa.colormap;
58
59   if (! mono_p)
60     hsv_to_rgb (random()%360, 1.0, 1.0, &color.red, &color.green, &color.blue);
61   if ((!mono_p) && (got_color = XAllocColor (dpy, cmap, &color)))
62     XSetForeground (dpy, draw_gc, color.pixel);
63   else
64     XSetForeground (dpy, draw_gc, default_fg_pixel);
65
66   x = xlim/2;
67   y = ylim/2;
68   for (i = 0; i < iterations; i++)
69     {
70       int j = 0;
71       x += ((random () % (1 + (offset << 1))) - offset);
72       y += ((random () % (1 + (offset << 1))) - offset);
73       points [j].x = x;
74       points [j].y = y;
75       j++;
76       if (xsym)
77         {
78           points [j].x = xlim - x;
79           points [j].y = y;
80           j++;
81         }
82       if (ysym)
83         {
84           points [j].x = x;
85           points [j].y = ylim - y;
86           j++;
87         }
88       if (xsym && ysym)
89         {
90           points [j].x = xlim - x;
91           points [j].y = ylim - y;
92           j++;
93         }
94       XDrawPoints (dpy, window, draw_gc, points, j, CoordModeOrigin);
95       XSync (dpy, True);
96     }
97   sleep ( sleep_time );
98
99   erase_full_window(dpy, window);
100
101   XClearWindow (dpy, window);
102   if (got_color) XFreeColors (dpy, cmap, &color.pixel, 1, 0);
103   XSync (dpy, True);
104   sleep (1);
105 }
106
107 \f
108 char *progclass = "Rorschach";
109
110 char *defaults [] = {
111   ".background: black",
112   ".foreground: white",
113   "*xsymmetry:  true",
114   "*ysymmetry:  false",
115   "*iterations: 4000",
116   "*offset:     4",
117   "*delay:      5",
118   0
119 };
120
121 XrmOptionDescRec options [] = {
122   { "-iterations",      ".iterations",  XrmoptionSepArg, 0 },
123   { "-offset",          ".offset",      XrmoptionSepArg, 0 },
124   { "-xsymmetry",       ".xsymmetry",   XrmoptionNoArg, "true" },
125   { "-ysymmetry",       ".ysymmetry",   XrmoptionNoArg, "true" },
126   { "-erase-speed",     ".eraseSpeed",          XrmoptionSepArg, 0 },
127   { "-delay",           ".delay",               XrmoptionSepArg, 0 },
128   { 0, 0, 0, 0 }
129 };
130
131 void
132 screenhack (Display *dpy, Window window)
133 {
134   sleep_time = get_integer_resource("delay", "Integer");
135   init_rorschach (dpy, window);
136   while (1)
137     hurm (dpy, window);
138 }