ftp://ftp.ntnu.no/old/pub/X11/R5contrib/xscreensaver-1.17.tar.gz
[xscreensaver] / hacks / xroger-hack.c
1 /* xscreensaver, Copyright (c) 1991-1993 Jamie Zawinski <jwz@lucid.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 char *progclass = "XRoger";
15
16 char *defaults [] = {
17   "*background: black",
18   "*foreground: red",
19   "*delay:      5",
20   0
21 };
22
23 XrmOptionDescRec options [] = {
24   { "-delay",           ".delay",       XrmoptionSepArg, 0 }
25 };
26 int options_size = (sizeof (options) / sizeof (options[0]));
27
28 #if __STDC__
29 extern void skull (Display *, Window, GC, GC, int, int, int, int);
30 #endif
31
32 void
33 screenhack (dpy, window)
34      Display *dpy;
35      Window window;
36 {
37   double delta = 0.005;
38   XGCValues gcv;
39   Colormap cmap;
40   GC draw_gc, erase_gc;
41   unsigned int fg;
42   XColor color, color2;
43   int delay = get_integer_resource ("delay", "Integer");
44   XWindowAttributes xgwa;
45   XGetWindowAttributes (dpy, window, &xgwa);
46   cmap = xgwa.colormap;
47   gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
48   erase_gc = XCreateGC (dpy, window, GCForeground, &gcv);
49   fg = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
50   if (fg == gcv.foreground)
51     fg = ((gcv.foreground == WhitePixel (dpy, DefaultScreen (dpy)))
52           ? BlackPixel (dpy, DefaultScreen (dpy))
53           : WhitePixel (dpy, DefaultScreen (dpy)));
54   gcv.foreground = fg;
55   draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
56   color.pixel = gcv.foreground;
57   XQueryColor (dpy, cmap, &color);
58   while (1)
59     {
60       int w, h, ww, hh, x, y;
61       time_t start_time;
62       XWindowAttributes xgwa;
63       XGetWindowAttributes (dpy, window, &xgwa);
64       w = xgwa.width;
65       h = xgwa.height;
66
67       ww = 100 + random () % (w - 100);
68       hh = 100 + random () % (h - 100);
69       if (ww < 10) ww = 50;
70       if (hh < 10) hh = 50;
71       if (ww < hh) hh = ww;
72       else ww = hh;
73       x = random () % (w - ww);
74       y = random () % (h - hh);
75       XClearWindow (dpy, window);
76       skull (dpy, window, draw_gc, erase_gc, x, y, ww, hh);
77       XSync (dpy, True);
78       start_time = time ((time_t *) 0);
79       if (mono_p)
80         sleep (delay);
81       else
82         while (start_time + delay > time ((time_t *) 0))
83           {
84             int h;
85             double s, v;
86             color2 = color;
87             rgb_to_hsv (color2.red, color2.green, color2.blue, &h, &s, &v);
88             v += delta;
89             if (v >= 1.0) v = 1.0, delta = -delta;
90             if (v <= 0.7) v = 0.7, delta = -delta;
91             hsv_to_rgb (h, s, v, &color2.red, &color2.green, &color2.blue);
92             if (XAllocColor (dpy, cmap, &color2))
93               {
94                 XSetForeground (dpy, draw_gc, color.pixel);
95                 XFreeColors (dpy, cmap, &color.pixel, 1, 0);
96               }
97             color = color2;
98             usleep (20000);
99           }
100     }
101 }