eab1388743c82d4885cc38ed18309aa648ba7bdf
[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
12 #include "screenhack.h"
13
14 static GC draw_gc, erase_gc;
15 static unsigned int default_fg_pixel;
16 static int iterations, offset;
17 static Bool xsym, ysym;
18
19 static void
20 init_rorschach (Display *dpy, Window window)
21 {
22   XGCValues gcv;
23   Colormap cmap;
24   XWindowAttributes xgwa;
25   XGetWindowAttributes (dpy, window, &xgwa);
26   cmap = xgwa.colormap;
27   gcv.foreground = default_fg_pixel =
28     get_pixel_resource ("foreground", "Foreground", dpy, cmap);
29   draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
30   gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
31   erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
32   iterations = get_integer_resource ("iterations", "Integer");
33   offset = get_integer_resource ("offset", "Integer");
34   if (offset <= 0) offset = 3;
35   if (iterations < 10) iterations = 10;
36   xsym = get_boolean_resource ("xsymmetry", "Symmetry");
37   ysym = get_boolean_resource ("ysymmetry", "Symmetry");
38 }
39
40 static void
41 hurm (Display *dpy, Window window)
42 {
43   Colormap cmap;
44   XWindowAttributes xgwa;
45   int xlim, ylim, x, y, i, got_color = 0;
46   XPoint points [4];
47   XColor color;
48   XClearWindow (dpy, window);
49   XGetWindowAttributes (dpy, window, &xgwa);
50   xlim = xgwa.width;
51   ylim = xgwa.height;
52   cmap = xgwa.colormap;
53
54   if (! mono_p)
55     hsv_to_rgb (random()%360, 1.0, 1.0, &color.red, &color.green, &color.blue);
56   if ((!mono_p) && (got_color = XAllocColor (dpy, cmap, &color)))
57     XSetForeground (dpy, draw_gc, color.pixel);
58   else
59     XSetForeground (dpy, draw_gc, default_fg_pixel);
60
61   x = xlim/2;
62   y = ylim/2;
63   for (i = 0; i < iterations; i++)
64     {
65       int j = 0;
66       x += ((random () % (1 + (offset << 1))) - offset);
67       y += ((random () % (1 + (offset << 1))) - offset);
68       points [j].x = x;
69       points [j].y = y;
70       j++;
71       if (xsym)
72         {
73           points [j].x = xlim - x;
74           points [j].y = y;
75           j++;
76         }
77       if (ysym)
78         {
79           points [j].x = x;
80           points [j].y = ylim - y;
81           j++;
82         }
83       if (xsym && ysym)
84         {
85           points [j].x = xlim - x;
86           points [j].y = ylim - y;
87           j++;
88         }
89       XDrawPoints (dpy, window, draw_gc, points, j, CoordModeOrigin);
90       XSync (dpy, True);
91     }
92   sleep (5);
93   for (i = 0; i < (ylim >> 1); i++)
94     {
95       y = (random () % ylim);
96       XDrawLine (dpy, window, erase_gc, 0, y, xlim, y);
97       XFlush (dpy);
98       if ((i % 50) == 0)
99         usleep (10000);
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   "Rorschach.background:        black",         /* to placate SGI */
112   "Rorschach.foreground:        white",
113   "*xsymmetry:  true",
114   "*ysymmetry:  false",
115   "*iterations: 4000",
116   "*offset:     4",
117   0
118 };
119
120 XrmOptionDescRec options [] = {
121   { "-iterations",      ".iterations",  XrmoptionSepArg, 0 },
122   { "-offset",          ".offset",      XrmoptionSepArg, 0 },
123   { "-xsymmetry",       ".xsymmetry",   XrmoptionNoArg, "true" },
124   { "-ysymmetry",       ".ysymmetry",   XrmoptionNoArg, "true" },
125   { 0, 0, 0, 0 }
126 };
127
128 void
129 screenhack (Display *dpy, Window window)
130 {
131   init_rorschach (dpy, window);
132   while (1)
133     hurm (dpy, window);
134 }