http://se.aminet.net/pub/X11/ftp.x.org/contrib/vms/xscreensaver-124.zip
[xscreensaver] / hacks / greynetic.c
1 /* xscreensaver, Copyright (c) 1992 Jamie Zawinski <jwz@mcom.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 #define NBITS 12
15
16 #ifndef VMS
17 #include <X11/bitmaps/stipple>
18 #include <X11/bitmaps/cross_weave>
19 #include <X11/bitmaps/dimple1>
20 #include <X11/bitmaps/dimple3>
21 #include <X11/bitmaps/flipped_gray>
22 #include <X11/bitmaps/gray1>
23 #include <X11/bitmaps/gray3>
24 #include <X11/bitmaps/hlines2>
25 #include <X11/bitmaps/light_gray>
26 #include <X11/bitmaps/root_weave>
27 #include <X11/bitmaps/vlines2>
28 #include <X11/bitmaps/vlines3>
29 #else
30 #include "sys$common:[decw$include.bitmaps]stipple.xbm"
31 #include "sys$common:[decw$include.bitmaps]cross_weave.xbm"
32 #include "sys$common:[decw$include.bitmaps]dimple1.xbm"
33 #include "sys$common:[decw$include.bitmaps]dimple3.xbm"
34 #include "sys$common:[decw$include.bitmaps]flipped_gray.xbm"
35 #include "sys$common:[decw$include.bitmaps]gray1.xbm"
36 #include "sys$common:[decw$include.bitmaps]gray3.xbm"
37 #include "sys$common:[decw$include.bitmaps]hlines2.xbm"
38 #include "sys$common:[decw$include.bitmaps]light_gray.xbm"
39 #include "sys$common:[decw$include.bitmaps]root_weave.xbm"
40 #include "sys$common:[decw$include.bitmaps]vlines2.xbm"
41 #include "sys$common:[decw$include.bitmaps]vlines3.xbm"
42 #endif
43
44 static Pixmap pixmaps [NBITS];
45 static GC gc;
46 static int delay;
47 static unsigned long fg, bg, pixels [512];
48 static int npixels;
49
50 static void
51 init_greynetic (dpy, window)
52      Display *dpy;
53      Window window;
54 {
55   int i;
56   XGCValues gcv;
57   XWindowAttributes xgwa;
58   Colormap cmap;
59   XGetWindowAttributes (dpy, window, &xgwa);
60   cmap = xgwa.colormap;
61   npixels = 0;
62   gcv.foreground= fg= get_pixel_resource("foreground","Foreground", dpy, cmap);
63   gcv.background= bg= get_pixel_resource("background","Background", dpy, cmap);
64   gcv.fill_style= FillOpaqueStippled;
65   gc = XCreateGC (dpy, window, GCForeground|GCBackground|GCFillStyle, &gcv);
66
67   delay = get_integer_resource ("delay", "Integer");
68   if (delay < 0) delay = 0;
69
70   i = 0;
71 #define BITS(n,w,h) \
72   pixmaps [i++] = XCreatePixmapFromBitmapData (dpy, window, n, w, h, 1, 0, 1)
73
74   BITS (stipple_bits, stipple_width, stipple_height);
75   BITS (cross_weave_bits, cross_weave_width, cross_weave_height);
76   BITS (dimple1_bits, dimple1_width, dimple1_height);
77   BITS (dimple3_bits, dimple3_width, dimple3_height);
78   BITS (flipped_gray_bits, flipped_gray_width, flipped_gray_height);
79   BITS (gray1_bits, gray1_width, gray1_height);
80   BITS (gray3_bits, gray3_width, gray3_height);
81   BITS (hlines2_bits, hlines2_width, hlines2_height);
82   BITS (light_gray_bits, light_gray_width, light_gray_height);
83   BITS (root_weave_bits, root_weave_width, root_weave_height);
84   BITS (vlines2_bits, vlines2_width, vlines2_height);
85   BITS (vlines3_bits, vlines3_width, vlines3_height);
86 }
87
88 static void
89 greynetic (dpy, window)
90      Display *dpy;
91      Window window;
92 {
93   static int tick = 500, xlim, ylim;
94   static Colormap cmap;
95   int x, y, w, h, i;
96   XGCValues gcv;
97   if (tick++ == 500)
98     {
99       XWindowAttributes xgwa;
100       XGetWindowAttributes (dpy, window, &xgwa);
101       tick = 0;
102       xlim = xgwa.width;
103       ylim = xgwa.height;
104       cmap = xgwa.colormap;
105     }
106   for (i = 0; i < 10; i++) /* minimize area, but don't try too hard */
107     {
108       w = 50 + random () % (xlim - 50);
109       h = 50 + random () % (ylim - 50);
110       if (w + h < xlim && w + h < ylim)
111         break;
112     }
113   x = random () % (xlim - w);
114   y = random () % (ylim - h);
115   gcv.stipple = pixmaps [random () % NBITS];
116   if (mono_p)
117     {
118       if (random () % 1)
119         gcv.foreground = fg, gcv.background = bg;
120       else
121         gcv.foreground = bg, gcv.background = fg;
122     }
123   else
124     {
125       XColor fgc, bgc;
126       if (npixels == sizeof (pixels) / sizeof (unsigned long))
127         goto REUSE;
128       fgc.flags = bgc.flags = DoRed|DoGreen|DoBlue;
129       fgc.red = random ();
130       fgc.green = random ();
131       fgc.blue = random ();
132       bgc.red = random ();
133       bgc.green = random ();
134       bgc.blue = random ();
135       if (! XAllocColor (dpy, cmap, &fgc))
136         goto REUSE;
137       pixels [npixels++] = fgc.pixel;
138       gcv.foreground = fgc.pixel;
139       if (! XAllocColor (dpy, cmap, &bgc))
140         goto REUSE;
141       pixels [npixels++] = bgc.pixel;
142       gcv.background = bgc.pixel;
143       goto DONE;
144     REUSE:
145       gcv.foreground = pixels [random () % npixels];
146       gcv.background = pixels [random () % npixels];
147     DONE:
148       ;
149     }
150   XChangeGC (dpy, gc, GCStipple|GCForeground|GCBackground, &gcv);
151   XFillRectangle (dpy, window, gc, x, y, w, h);
152   XSync (dpy, True);
153 }
154
155 \f
156 char *progclass = "Greynetic";
157
158 char *defaults [] = {
159   "Greynetic.background:        black",         /* to placate SGI */
160   "Greynetic.foreground:        white",
161   "*delay:      0",
162   0
163 };
164
165 XrmOptionDescRec options [] = {
166   { "-delay",           ".delay",       XrmoptionSepArg, 0 }
167 };
168 int options_size = (sizeof (options) / sizeof (options[0]));
169
170 void
171 screenhack (dpy, window)
172      Display *dpy;
173      Window window;
174 {
175   init_greynetic (dpy, window);
176   while (1)
177     {
178       greynetic (dpy, window);
179       if (delay) usleep (delay);
180     }
181 }