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