ftp://ftp.smr.ru/pub/0/FreeBSD/releases/distfiles/xscreensaver-3.16.tar.gz
[xscreensaver] / hacks / xlock_23.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 **  Modified for xlockmore 3.0 by Anthony Thyssen <anthony@cit.gu.edu.au>
8 **  on August 1995.
9 **
10 **  Tweaked by jwz to work with both ANSI and K&R compilers, 10-May-97.
11 **
12 **    Note: this file no longer works as of (at least) xlockmore 4.03a10:
13 **    see jwz's new xlockmore.h file for a similar hack that works with
14 **    code written for that version.
15 **
16 **  To use, just copy the appropriate file from xlock, add a target
17 **  for it in the Imakefile, and do the following:
18 **
19 **  1) If you include math.h, make sure it is before xlock.h.
20 **  2) Make sure the first thing you do in initfoo() is to call
21 **     XGetWindowAttributes.  This is what actually sets up the
22 **     colormap and whatnot.
23 **  3) Add an appropriate PROGRAM() line at the end of the .c file.
24 **     The information you need for this comes from xlock's file
25 **     resource.c.
26 **
27 **  That's about all there is to it.
28 **
29 **  As an added bonus, if you put an empty definition of PROGRAM() in
30 **  xlock's xlock.h, you can now use the code with either xlock or
31 **  xscreensaver.
32 **
33 **
34 **  If you make any improvements to this code, please send them to me!
35 **  It could certainly use some more work.
36 */
37
38 #include "screenhack.h"
39
40 #define MAXSCREENS 1
41
42 static GC gc;
43 static unsigned long *pixels = 0, fg_pixel, bg_pixel;
44 static int npixels;
45 static Colormap cmap;
46
47 static int batchcount;
48 static unsigned int delay;
49 static unsigned int cycles;
50 static double saturation;
51
52 #ifndef min
53 #define min(a,b) ((a)<(b)?(a):(b))
54 #endif
55
56 typedef struct {
57   GC gc;
58   int npixels;
59   u_long *pixels;
60 } perscreen;
61
62 static perscreen Scr[MAXSCREENS];
63 static Display *dsp;
64
65 static int screen = 0;
66
67 static void
68 #ifdef __STDC__
69 My_XGetWindowAttributes (Display *dpy, Window win, XWindowAttributes *xgwa)
70 #else /* !__STDC__ */
71 My_XGetWindowAttributes (dpy, win, xgwa)
72   Display *dpy;
73   Window win;
74   XWindowAttributes *xgwa;
75 #endif /* !__STDC__ */
76 {
77   XGetWindowAttributes (dpy, win, xgwa);
78
79   if (! pixels) {
80     XGCValues gcv;
81     XColor color;
82     int n;
83     int i, shift;
84
85     cmap = xgwa->colormap;
86
87     i = get_integer_resource ("ncolors", "Integer");
88     if (i <= 2) i = 2, mono_p = True;
89     shift = 360 / i;
90     pixels = (unsigned long *) calloc (i, sizeof (unsigned long));
91     fg_pixel = get_pixel_resource ("foreground", "Foreground", dpy, cmap);
92     bg_pixel = get_pixel_resource ("background", "Background", dpy, cmap);
93     if (! mono_p) {
94       for (npixels = 0; npixels < i; npixels++) {
95         hsv_to_rgb ((360*npixels)/i, saturation, 1.0,
96                     &color.red, &color.green, &color.blue);
97         if (! XAllocColor (dpy, cmap, &color))
98           break;
99         pixels[npixels] = color.pixel;
100       }
101     }
102     n = get_integer_resource ("delay", "Usecs");
103     if (n >= 0) delay = n;
104     n = get_integer_resource ("count", "Integer");
105     if (n > 0) batchcount = n;
106
107     n = get_integer_resource ("cycles", "Integer");
108     if (n >= 0) cycles = n;
109
110     gcv.foreground = fg_pixel;
111     gcv.background = bg_pixel;
112     gc = XCreateGC (dpy, win, GCForeground|GCBackground, &gcv);
113
114     XClearWindow (dpy, win);
115
116     Scr[screen].gc = gc;
117     Scr[screen].npixels = npixels;
118     Scr[screen].pixels = pixels;
119   }
120 }
121
122 #define XGetWindowAttributes(a,b,c) My_XGetWindowAttributes(a,b,c)
123
124 #undef BlackPixel
125 #define BlackPixel(a,b) bg_pixel
126 #undef WhitePixel
127 #define WhitePixel(a,b) fg_pixel
128 #define mono mono_p
129
130 #define seconds() time((time_t*)0)
131
132 char *defaults[] = {
133   "*background: black",
134   "*foreground: white",
135   "*ncolors:    64",
136   "*delay:      -1",
137   "*count:      -1",
138   "*cycles:     -1",
139   0
140 };
141
142 XrmOptionDescRec options[] = {
143   {"-count",    ".count",       XrmoptionSepArg, 0},
144   {"-ncolors",  ".ncolors",     XrmoptionSepArg, 0},
145   {"-delay",    ".delay",       XrmoptionSepArg, 0},
146   {"-cycles",   ".cycles",      XrmoptionSepArg, 0},
147   { 0, 0, 0, 0 }
148 };
149
150 #if defined(__STDC__) || defined(__ANSI_CPP__)
151 # define XLOCK_INIT(Z) init##Z
152 # define XLOCK_DRAW(Z) draw##Z
153 #else  /* K&R CPP */
154 # define XLOCK_INIT(Z) init/**/Z
155 # define XLOCK_DRAW(Z) draw/**/Z
156 #endif /* K&R CPP */
157
158 #ifdef __STDC__
159 # define XLOCK_SCREENHACK_PROTOTYPE()   \
160   screenhack(Display *dpy, Window window)
161 # define XLOCK_PROTOS(Z) /* */
162 #else  /* K&R C */
163 # define XLOCK_SCREENHACK_PROTOTYPE()   \
164   screenhack(dpy, window)               \
165         Display *dpy;                   \
166         Window window;
167 # define XLOCK_PROTOS(Z)                \
168   void init##Z(Window);                 \
169   void draw##Z(Window);                 \
170
171 #endif /* K&R C */
172
173 #define PROGRAM(Y,Z,D,B,C,S)            \
174   char *progclass = Y;                  \
175   XLOCK_PROTOS(Z)                       \
176                                         \
177   void XLOCK_SCREENHACK_PROTOTYPE()     \
178   {                                     \
179     batchcount = B;                     \
180     delay = D;                          \
181     cycles = C;                         \
182     saturation = S;                     \
183     dsp = dpy;                          \
184                                         \
185     XLOCK_INIT(Z) (window);             \
186     while (1)                           \
187       {                                 \
188         XLOCK_DRAW(Z) (window);         \
189         XSync (dpy, True);              \
190         if (delay) usleep (delay);      \
191       }                                 \
192   }