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