ftp://ftp.smr.ru/pub/0/FreeBSD/releases/distfiles/xscreensaver-3.16.tar.gz
[xscreensaver] / hacks / xlockmore.c
1 /* xlockmore.c --- xscreensaver compatibility layer for xlockmore modules.
2  * xscreensaver, Copyright (c) 1997, 1998 Jamie Zawinski <jwz@jwz.org>
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  *
12  * This file, along with xlockmore.h, make it possible to compile an xlockmore
13  * module into a standalone program, and thus use it with xscreensaver.
14  * By Jamie Zawinski <jwz@jwz.org> on 10-May-97; based on the ideas
15  * in the older xlock.h by Charles Hannum <mycroft@ai.mit.edu>.  (I had
16  * to redo it, since xlockmore has diverged so far from xlock...)
17  */
18
19 #include <stdio.h>
20 #include <math.h>
21 #include <string.h>
22 #include "screenhack.h"
23 #include "xlockmoreI.h"
24
25 #define countof(x) (sizeof((x))/sizeof(*(x)))
26
27 extern ModeSpecOpt xlockmore_opts[];
28 extern const char *app_defaults;
29
30 void
31 pre_merge_options (void)
32 {
33   int i, j;
34   char *s;
35
36   /* Translate the xlockmore `opts[]' argument to a form that
37      screenhack.c expects.
38    */
39   for (i = 0; i < xlockmore_opts->numopts; i++)
40     {
41       XrmOptionDescRec *old = &xlockmore_opts->opts[i];
42       XrmOptionDescRec *new = &options[i];
43
44       if (old->option[0] == '-')
45         new->option = old->option;
46       else
47         {
48           /* Convert "+foo" to "-no-foo". */
49           new->option = (char *) malloc (strlen(old->option) + 5);
50           strcpy (new->option, "-no-");
51           strcat (new->option, old->option + 1);
52         }
53
54       new->specifier = strrchr (old->specifier, '.');
55       if (!new->specifier) abort();
56
57       new->argKind = old->argKind;
58       new->value = old->value;
59     }
60
61   /* Add extra args, if they're mentioned in the defaults... */
62   {
63     char *args[] = { "-count", "-cycles", "-delay", "-ncolors",
64                      "-size", "-wireframe", "-use3d", "-useSHM" };
65     for (j = 0; j < countof(args); j++)
66       if (strstr(app_defaults, args[j]+1))
67         {
68           XrmOptionDescRec *new = &options[i++];
69           new->option = args[j];
70           new->specifier = strdup(args[j]);
71           new->specifier[0] = '.';
72           if (!strcmp(new->option, "-wireframe"))
73             {
74               new->argKind = XrmoptionNoArg;
75               new->value = "True";
76               new = &options[i++];
77               new->option = "-no-wireframe";
78               new->specifier = options[i-2].specifier;
79               new->argKind = XrmoptionNoArg;
80               new->value = "False";
81             }
82           else if (!strcmp(new->option, "-use3d"))
83             {
84               new->option = "-3d";
85               new->argKind = XrmoptionNoArg;
86               new->value = "True";
87               new = &options[i++];
88               new->option = "-no-3d";
89               new->specifier = options[i-2].specifier;
90               new->argKind = XrmoptionNoArg;
91               new->value = "False";
92             }
93           else if (!strcmp(new->option, "-useSHM"))
94             {
95               new->option = "-shm";
96               new->argKind = XrmoptionNoArg;
97               new->value = "True";
98               new = &options[i++];
99               new->option = "-no-shm";
100               new->specifier = options[i-2].specifier;
101               new->argKind = XrmoptionNoArg;
102               new->value = "False";
103             }
104           else
105             {
106               new->argKind = XrmoptionSepArg;
107               new->value = 0;
108             }
109         }
110   }
111
112
113   /* Construct the kind of `defaults' that screenhack.c expects from
114      the xlockmore `vars[]' argument.
115    */
116   i = 0;
117
118   /* Put on the PROGCLASS.background/foreground resources. */
119   s = (char *) malloc(50);
120   strcpy (s, progclass);
121   strcat (s, ".background: black");
122   defaults [i++] = s;
123
124   s = (char *) malloc(50);
125   strcpy (s, progclass);
126   strcat (s, ".foreground: white");
127   defaults [i++] = s;
128
129   /* Copy the lines out of the `app_defaults' var and into this array. */
130   s = strdup (app_defaults);
131   while (s && *s)
132     {
133       defaults [i++] = s;
134       s = strchr(s, '\n');
135       if (s)
136         *s++ = 0;
137     }
138
139   /* Copy the defaults out of the `xlockmore_opts->' variable. */
140   for (j = 0; j < xlockmore_opts->numvarsdesc; j++)
141     {
142       const char *def = xlockmore_opts->vars[j].def;
143
144       if (!def) abort();
145       if (!*def) abort();
146       if (strlen(def) > 1000) abort();
147
148       s = (char *) malloc (strlen (xlockmore_opts->vars[j].name) +
149                            strlen (def) + 10);
150       strcpy (s, "*");
151       strcat (s, xlockmore_opts->vars[j].name);
152       strcat (s, ": ");
153       strcat (s, def);
154       defaults [i++] = s;
155     }
156
157   defaults [i] = 0;
158 }
159
160
161 static void
162 xlockmore_read_resources (void)
163 {
164   int i;
165   for (i = 0; i < xlockmore_opts->numvarsdesc; i++)
166     {
167       void  *var   = xlockmore_opts->vars[i].var;
168       Bool  *var_b = (Bool *)  var;
169       char **var_c = (char **) var;
170       int   *var_i = (int *) var;
171       float *var_f = (float *) var;
172
173       switch (xlockmore_opts->vars[i].type)
174         {
175         case t_String:
176           *var_c = get_string_resource (xlockmore_opts->vars[i].name,
177                                         xlockmore_opts->vars[i].classname);
178           break;
179         case t_Float:
180           *var_f = get_float_resource (xlockmore_opts->vars[i].name,
181                                        xlockmore_opts->vars[i].classname);
182           break;
183         case t_Int:
184           *var_i = get_integer_resource (xlockmore_opts->vars[i].name,
185                                          xlockmore_opts->vars[i].classname);
186           break;
187         case t_Bool:
188           *var_b = get_boolean_resource (xlockmore_opts->vars[i].name,
189                                          xlockmore_opts->vars[i].classname);
190           break;
191         default:
192           abort ();
193         }
194     }
195 }
196
197
198
199 void 
200 xlockmore_screenhack (Display *dpy, Window window,
201                       Bool want_writable_colors,
202                       Bool want_uniform_colors,
203                       Bool want_smooth_colors,
204                       Bool want_bright_colors,
205                       void (*hack_init) (ModeInfo *),
206                       void (*hack_draw) (ModeInfo *),
207                       void (*hack_free) (ModeInfo *))
208 {
209   ModeInfo mi;
210   XGCValues gcv;
211   XColor color;
212   int i;
213   time_t start, now;
214   int orig_pause;
215
216   memset(&mi, 0, sizeof(mi));
217   mi.dpy = dpy;
218   mi.window = window;
219   XGetWindowAttributes (dpy, window, &mi.xgwa);
220
221   color.flags = DoRed|DoGreen|DoBlue;
222   color.red = color.green = color.blue = 0;
223   if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
224     abort();
225   mi.black = color.pixel;
226   color.red = color.green = color.blue = 0xFFFF;
227   if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
228     abort();
229   mi.white = color.pixel;
230
231   if (mono_p)
232     {
233       static unsigned long pixels[2];
234       static XColor colors[2];
235     MONO:
236       mi.npixels = 2;
237       mi.pixels = pixels;
238       mi.colors = colors;
239       pixels[0] = mi.black;
240       pixels[1] = mi.white;
241       colors[0].flags = DoRed|DoGreen|DoBlue;
242       colors[1].flags = DoRed|DoGreen|DoBlue;
243       colors[0].red = colors[0].green = colors[0].blue = 0;
244       colors[1].red = colors[1].green = colors[1].blue = 0xFFFF;
245       mi.writable_p = False;
246     }
247   else
248     {
249       mi.npixels = get_integer_resource ("ncolors", "Integer");
250       if (mi.npixels <= 0)
251         mi.npixels = 64;
252       else if (mi.npixels > 256)
253         mi.npixels = 256;
254
255       mi.colors = (XColor *) calloc (mi.npixels, sizeof (*mi.colors));
256
257       mi.writable_p = want_writable_colors;
258
259       if (want_uniform_colors)
260         make_uniform_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
261                                mi.colors, &mi.npixels,
262                                True, &mi.writable_p, True);
263       else if (want_smooth_colors)
264         make_smooth_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
265                               mi.colors, &mi.npixels,
266                               True, &mi.writable_p, True);
267       else
268         make_random_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
269                               mi.colors, &mi.npixels,
270                               want_bright_colors,
271                               True, &mi.writable_p, True);
272
273       if (mi.npixels <= 2)
274         goto MONO;
275       else
276         {
277           int i;
278           mi.pixels = (unsigned long *)
279             calloc (mi.npixels, sizeof (*mi.pixels));
280           for (i = 0; i < mi.npixels; i++)
281             mi.pixels[i] = mi.colors[i].pixel;
282         }
283     }
284
285   gcv.foreground = mi.white;
286   gcv.background = mi.black;
287   mi.gc = XCreateGC(dpy, window, GCForeground|GCBackground, &gcv);
288
289   mi.fullrandom = True;
290
291   mi.pause      = get_integer_resource ("delay", "Usecs");
292
293   mi.cycles     = get_integer_resource ("cycles", "Int");
294   mi.batchcount = get_integer_resource ("count", "Int");
295   mi.size       = get_integer_resource ("size", "Int");
296
297   mi.threed = get_boolean_resource ("use3d", "Boolean");
298   mi.threed_delta = get_float_resource ("delta3d", "Boolean");
299   mi.threed_right_color = get_pixel_resource ("right3d", "Color", dpy,
300                                               mi.xgwa.colormap);
301   mi.threed_left_color = get_pixel_resource ("left3d", "Color", dpy,
302                                              mi.xgwa.colormap);
303   mi.threed_both_color = get_pixel_resource ("both3d", "Color", dpy,
304                                              mi.xgwa.colormap);
305   mi.threed_none_color = get_pixel_resource ("none3d", "Color", dpy,
306                                              mi.xgwa.colormap);
307
308   mi.wireframe_p = get_boolean_resource ("wireframe", "Boolean");
309   mi.root_p = (window == RootWindowOfScreen (mi.xgwa.screen));
310 #ifdef HAVE_XSHM_EXTENSION
311   mi.use_shm = get_boolean_resource ("useSHM", "Boolean");
312 #endif /* !HAVE_XSHM_EXTENSION */
313
314   if (mi.pause < 0)
315     mi.pause = 0;
316   else if (mi.pause > 100000000)
317     mi.pause = 100000000;
318   orig_pause = mi.pause;
319
320   xlockmore_read_resources ();
321
322   XClearWindow (dpy, window);
323
324   i = 0;
325   start = time((time_t) 0);
326
327   hack_init (&mi);
328   do {
329     hack_draw (&mi);
330     XSync(dpy, False);
331     screenhack_handle_events (dpy);
332     if (mi.pause)
333       usleep(mi.pause);
334     mi.pause = orig_pause;
335
336     if (hack_free)
337       {
338         if (i++ > (mi.batchcount / 4) &&
339             (start + 5) < (now = time((time_t) 0)))
340           {
341             i = 0;
342             start = now;
343             hack_free (&mi);
344             hack_init (&mi);
345             XSync(dpy, False);
346           }
347       }
348
349   } while (1);
350 }