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