1 /* xlockmore.c --- xscreensaver compatibility layer for xlockmore modules.
2 * xscreensaver, Copyright (c) 1997, 1998 Jamie Zawinski <jwz@jwz.org>
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
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...)
22 #include "screenhack.h"
23 #include "xlockmoreI.h"
25 #define countof(x) (sizeof((x))/sizeof(*(x)))
27 extern ModeSpecOpt xlockmore_opts[];
28 extern const char *app_defaults;
31 pre_merge_options (void)
36 /* Translate the xlockmore `opts[]' argument to a form that
39 for (i = 0; i < xlockmore_opts->numopts; i++)
41 XrmOptionDescRec *old = &xlockmore_opts->opts[i];
42 XrmOptionDescRec *new = &options[i];
44 if (old->option[0] == '-')
45 new->option = old->option;
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);
54 new->specifier = strrchr (old->specifier, '.');
55 if (!new->specifier) abort();
57 new->argKind = old->argKind;
58 new->value = old->value;
61 /* Add extra args, if they're mentioned in the defaults... */
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))
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"))
74 new->argKind = XrmoptionNoArg;
77 new->option = "-no-wireframe";
78 new->specifier = options[i-2].specifier;
79 new->argKind = XrmoptionNoArg;
82 else if (!strcmp(new->option, "-use3d"))
85 new->argKind = XrmoptionNoArg;
88 new->option = "-no-3d";
89 new->specifier = options[i-2].specifier;
90 new->argKind = XrmoptionNoArg;
93 else if (!strcmp(new->option, "-useSHM"))
96 new->argKind = XrmoptionNoArg;
99 new->option = "-no-shm";
100 new->specifier = options[i-2].specifier;
101 new->argKind = XrmoptionNoArg;
102 new->value = "False";
106 new->argKind = XrmoptionSepArg;
113 /* Construct the kind of `defaults' that screenhack.c expects from
114 the xlockmore `vars[]' argument.
118 /* Put on the PROGCLASS.background/foreground resources. */
119 s = (char *) malloc(50);
120 strcpy (s, progclass);
121 strcat (s, ".background: black");
124 s = (char *) malloc(50);
125 strcpy (s, progclass);
126 strcat (s, ".foreground: white");
129 /* Copy the lines out of the `app_defaults' var and into this array. */
130 s = strdup (app_defaults);
139 /* Copy the defaults out of the `xlockmore_opts->' variable. */
140 for (j = 0; j < xlockmore_opts->numvarsdesc; j++)
142 const char *def = xlockmore_opts->vars[j].def;
143 if (!def) def = "False";
144 if (def == ((char*) 1)) def = "True";
145 s = (char *) malloc (strlen (xlockmore_opts->vars[j].name) +
148 strcat (s, xlockmore_opts->vars[j].name);
159 xlockmore_read_resources (void)
162 for (i = 0; i < xlockmore_opts->numvarsdesc; i++)
164 void *var = xlockmore_opts->vars[i].var;
165 Bool *var_b = (Bool *) var;
166 char **var_c = (char **) var;
167 int *var_i = (int *) var;
168 float *var_f = (float *) var;
170 switch (xlockmore_opts->vars[i].type)
173 *var_c = get_string_resource (xlockmore_opts->vars[i].name,
174 xlockmore_opts->vars[i].classname);
177 *var_f = get_float_resource (xlockmore_opts->vars[i].name,
178 xlockmore_opts->vars[i].classname);
181 *var_i = get_integer_resource (xlockmore_opts->vars[i].name,
182 xlockmore_opts->vars[i].classname);
185 *var_b = get_boolean_resource (xlockmore_opts->vars[i].name,
186 xlockmore_opts->vars[i].classname);
197 xlockmore_screenhack (Display *dpy, Window window,
198 Bool want_writable_colors,
199 Bool want_uniform_colors,
200 Bool want_smooth_colors,
201 Bool want_bright_colors,
202 void (*hack_init) (ModeInfo *),
203 void (*hack_draw) (ModeInfo *),
204 void (*hack_free) (ModeInfo *))
213 memset(&mi, 0, sizeof(mi));
216 XGetWindowAttributes (dpy, window, &mi.xgwa);
218 color.flags = DoRed|DoGreen|DoBlue;
219 color.red = color.green = color.blue = 0;
220 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
222 mi.black = color.pixel;
223 color.red = color.green = color.blue = 0xFFFF;
224 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
226 mi.white = color.pixel;
230 static unsigned long pixels[2];
231 static XColor colors[2];
236 pixels[0] = mi.black;
237 pixels[1] = mi.white;
238 colors[0].flags = DoRed|DoGreen|DoBlue;
239 colors[1].flags = DoRed|DoGreen|DoBlue;
240 colors[0].red = colors[0].green = colors[0].blue = 0;
241 colors[1].red = colors[1].green = colors[1].blue = 0xFFFF;
242 mi.writable_p = False;
246 mi.npixels = get_integer_resource ("ncolors", "Integer");
249 else if (mi.npixels > 256)
252 mi.colors = (XColor *) calloc (mi.npixels, sizeof (*mi.colors));
254 mi.writable_p = want_writable_colors;
256 if (want_uniform_colors)
257 make_uniform_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
258 mi.colors, &mi.npixels,
259 True, &mi.writable_p, True);
260 else if (want_smooth_colors)
261 make_smooth_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
262 mi.colors, &mi.npixels,
263 True, &mi.writable_p, True);
265 make_random_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
266 mi.colors, &mi.npixels,
268 True, &mi.writable_p, True);
275 mi.pixels = (unsigned long *)
276 calloc (mi.npixels, sizeof (*mi.pixels));
277 for (i = 0; i < mi.npixels; i++)
278 mi.pixels[i] = mi.colors[i].pixel;
282 gcv.foreground = mi.white;
283 gcv.background = mi.black;
284 mi.gc = XCreateGC(dpy, window, GCForeground|GCBackground, &gcv);
286 mi.fullrandom = True;
288 mi.pause = get_integer_resource ("delay", "Usecs");
290 mi.cycles = get_integer_resource ("cycles", "Int");
291 mi.batchcount = get_integer_resource ("count", "Int");
292 mi.size = get_integer_resource ("size", "Int");
294 mi.threed = get_boolean_resource ("use3d", "Boolean");
295 mi.threed_delta = get_float_resource ("delta3d", "Boolean");
296 mi.threed_right_color = get_pixel_resource ("right3d", "Color", dpy,
298 mi.threed_left_color = get_pixel_resource ("left3d", "Color", dpy,
300 mi.threed_both_color = get_pixel_resource ("both3d", "Color", dpy,
302 mi.threed_none_color = get_pixel_resource ("none3d", "Color", dpy,
305 mi.wireframe_p = get_boolean_resource ("wireframe", "Boolean");
306 mi.root_p = (window == RootWindowOfScreen (mi.xgwa.screen));
307 #ifdef HAVE_XSHM_EXTENSION
308 mi.use_shm = get_boolean_resource ("useSHM", "Boolean");
309 #endif /* !HAVE_XSHM_EXTENSION */
313 else if (mi.pause > 100000000)
314 mi.pause = 100000000;
315 orig_pause = mi.pause;
317 xlockmore_read_resources ();
319 XClearWindow (dpy, window);
322 start = time((time_t) 0);
330 mi.pause = orig_pause;
334 if (i++ > (mi.batchcount / 4) &&
335 (start + 5) < (now = time((time_t) 0)))