http://se.aminet.net/pub/X11/ftp.x.org/contrib/vms/xscreensaver-124.zip
[xscreensaver] / hacks / xlock.h
1 /*
2 **  Helpful definitions for porting xlock modes to xscreensaver.
3 **  by Charles Hannum, mycroft@ai.mit.edu
4 **
5 **  for xlock 2.3 and xscreensaver 1.2, 28AUG92
6 **
7 **
8 **  To use, just copy the appropriate file from xlock, add a target
9 **  for it in the Imakefile, and do the following:
10 **
11 **  1) If you include math.h, make sure it is before xlock.h.
12 **  2) Make sure the first thing you do in initfoo() is to call
13 **     XGetWindowAttributes.  This is what actually sets up the
14 **     colormap and whatnot.
15 **  3) Add an appropriate PROGRAM() line at the end of the .c file.
16 **     The information you need for this comes from xlock's file
17 **     resource.c.
18 **
19 **  That's about all there is to it.
20 **
21 **  As an added bonus, if you put an empty definition of PROGRAM() in
22 **  xlock's xlock.h, you can now use the code with either xlock or
23 **  xscreensaver.
24 **
25 **
26 **  If you make any improvements to this code, please send them to me!
27 **  It could certainly use some more work.
28 */
29
30 #include "screenhack.h"
31
32 #define MAXSCREENS 1
33
34 static GC gc;
35 static unsigned long *pixels = 0, fg_pixel, bg_pixel;
36 static int npixels;
37 static Colormap cmap;
38
39 static int batchcount;
40 static unsigned int delay;
41 static double saturation;
42
43 typedef struct {
44   GC gc;
45   int npixels;
46   u_long *pixels;
47 } perscreen;
48
49 static perscreen Scr[MAXSCREENS];
50 static Display *dsp;
51
52 static int screen = 0;
53
54 static void
55 My_XGetWindowAttributes (dpy, win, xgwa)
56   Display *dpy;
57   Window win;
58   XWindowAttributes *xgwa;
59 {
60   XGetWindowAttributes (dpy, win, xgwa);
61
62   if (! pixels) {
63     XGCValues gcv;
64     XColor color;
65     int n;
66     int i, shift;
67
68     cmap = xgwa->colormap;
69
70     i = get_integer_resource ("ncolors", "Integer");
71     if (i <= 2) i = 2, mono_p = True;
72     shift = 360 / i;
73     pixels = (unsigned long *) calloc (i, sizeof (unsigned long));
74     fg_pixel = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
75     bg_pixel = get_pixel_resource ("background", "Background", dpy, cmap);
76     if (! mono_p) {
77       for (npixels = 0; npixels < i; npixels++) {
78         hsv_to_rgb ((360*npixels)/i, saturation, 1.0, &color.red, &color.green, &color.blue);
79         if (! XAllocColor (dpy, cmap, &color))
80           break;
81         pixels[npixels] = color.pixel;
82       }
83     }
84     n = get_integer_resource ("delay", "Usecs");
85     if (n >= 0) delay = n;
86     n = get_integer_resource ("count", "Integer");
87     if (n > 0) batchcount = n;
88
89     gcv.foreground = fg_pixel;
90     gcv.background = bg_pixel;
91     gc = XCreateGC (dpy, win, GCForeground|GCBackground, &gcv);
92
93     XClearWindow (dpy, win);
94
95     Scr[screen].gc = gc;
96     Scr[screen].npixels = npixels;
97     Scr[screen].pixels = pixels;
98   }
99 }
100
101 #define XGetWindowAttributes(a,b,c) My_XGetWindowAttributes(a,b,c)
102
103 #undef BlackPixel
104 #define BlackPixel(a,b) bg_pixel
105 #undef WhitePixel
106 #define WhitePixel(a,b) fg_pixel
107 #define mono mono_p
108
109 #define seconds() time((time_t*)0)
110
111 char *defaults[] = {
112   "*background: black",
113   "*foreground: white",
114   "*ncolors:    64",
115   "*delay:      -1",
116   0
117 };
118
119 XrmOptionDescRec options[] = {
120   {"-count",    ".count",       XrmoptionSepArg, 0},
121   {"-ncolors",  ".ncolors",     XrmoptionSepArg, 0},
122   {"-delay",    ".delay",       XrmoptionSepArg, 0},
123 };
124 int options_size = (sizeof (options) / sizeof (options[0]));
125
126 #define PROGRAM(Y,Z,D,B,S) \
127 char *progclass = Y;                    \
128                                         \
129 void screenhack (dpy, window)           \
130   Display *dpy;                         \
131   Window window;                        \
132 {                                       \
133   batchcount = B;                       \
134   delay = D;                            \
135   saturation = S;                       \
136   dsp = dpy;                            \
137                                         \
138   while (1) {                           \
139     init##Z (window);                   \
140     for (;;) {                          \
141       draw##Z (window);                 \
142       XSync (dpy, True);                \
143       if (delay) usleep (delay);        \
144     }                                   \
145   }                                     \
146 }