ftp://ftp.smr.ru/pub/0/FreeBSD/releases/distfiles/xscreensaver-3.16.tar.gz
[xscreensaver] / hacks / xroger-hack.c
1 /* xscreensaver, Copyright (c) 1991-1994 Jamie Zawinski <jwz@jwz.org>
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   { 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, False);
77       screenhack_handle_events (dpy);
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.6) V = 0.7, delta = -delta;
91             hsv_to_rgb (H, S, V, &color2.red, &color2.green, &color2.blue);
92             color3 = color2;
93             if (XAllocColor (dpy, cmap, &color3))
94               {
95                 XSetForeground (dpy, draw_gc, color3.pixel);
96                 color2.pixel = color3.pixel;
97                 XFreeColors (dpy, cmap, &color.pixel, 1, 0);
98               }
99             color = color2;
100             usleep (20000);
101           }
102     }
103 }