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 #define MAX_COLORS (1L<<13)
29 extern ModeSpecOpt xlockmore_opts[];
30 extern const char *app_defaults;
33 pre_merge_options (void)
38 /* Translate the xlockmore `opts[]' argument to a form that
41 for (i = 0; i < xlockmore_opts->numopts; i++)
43 XrmOptionDescRec *old = &xlockmore_opts->opts[i];
44 XrmOptionDescRec *new = &options[i];
46 if (old->option[0] == '-')
47 new->option = old->option;
50 /* Convert "+foo" to "-no-foo". */
51 new->option = (char *) malloc (strlen(old->option) + 5);
52 strcpy (new->option, "-no-");
53 strcat (new->option, old->option + 1);
56 new->specifier = strrchr (old->specifier, '.');
57 if (!new->specifier) abort();
59 new->argKind = old->argKind;
60 new->value = old->value;
63 /* Add extra args, if they're mentioned in the defaults... */
65 char *args[] = { "-count", "-cycles", "-delay", "-ncolors",
66 "-size", "-wireframe", "-use3d", "-useSHM" };
67 for (j = 0; j < countof(args); j++)
68 if (strstr(app_defaults, args[j]+1))
70 XrmOptionDescRec *new = &options[i++];
71 new->option = args[j];
72 new->specifier = strdup(args[j]);
73 new->specifier[0] = '.';
74 if (!strcmp(new->option, "-wireframe"))
76 new->argKind = XrmoptionNoArg;
79 new->option = "-no-wireframe";
80 new->specifier = options[i-2].specifier;
81 new->argKind = XrmoptionNoArg;
84 else if (!strcmp(new->option, "-use3d"))
87 new->argKind = XrmoptionNoArg;
90 new->option = "-no-3d";
91 new->specifier = options[i-2].specifier;
92 new->argKind = XrmoptionNoArg;
95 else if (!strcmp(new->option, "-useSHM"))
98 new->argKind = XrmoptionNoArg;
101 new->option = "-no-shm";
102 new->specifier = options[i-2].specifier;
103 new->argKind = XrmoptionNoArg;
104 new->value = "False";
108 new->argKind = XrmoptionSepArg;
115 /* Construct the kind of `defaults' that screenhack.c expects from
116 the xlockmore `vars[]' argument.
120 /* Put on the PROGCLASS.background/foreground resources. */
121 s = (char *) malloc(50);
122 strcpy (s, progclass);
123 strcat (s, ".background: black");
126 s = (char *) malloc(50);
127 strcpy (s, progclass);
128 strcat (s, ".foreground: white");
131 /* Copy the lines out of the `app_defaults' var and into this array. */
132 s = strdup (app_defaults);
141 /* Copy the defaults out of the `xlockmore_opts->' variable. */
142 for (j = 0; j < xlockmore_opts->numvarsdesc; j++)
144 const char *def = xlockmore_opts->vars[j].def;
148 if (strlen(def) > 1000) abort();
150 s = (char *) malloc (strlen (xlockmore_opts->vars[j].name) +
153 strcat (s, xlockmore_opts->vars[j].name);
164 xlockmore_read_resources (void)
167 for (i = 0; i < xlockmore_opts->numvarsdesc; i++)
169 void *var = xlockmore_opts->vars[i].var;
170 Bool *var_b = (Bool *) var;
171 char **var_c = (char **) var;
172 int *var_i = (int *) var;
173 float *var_f = (float *) var;
175 switch (xlockmore_opts->vars[i].type)
178 *var_c = get_string_resource (xlockmore_opts->vars[i].name,
179 xlockmore_opts->vars[i].classname);
182 *var_f = get_float_resource (xlockmore_opts->vars[i].name,
183 xlockmore_opts->vars[i].classname);
186 *var_i = get_integer_resource (xlockmore_opts->vars[i].name,
187 xlockmore_opts->vars[i].classname);
190 *var_b = get_boolean_resource (xlockmore_opts->vars[i].name,
191 xlockmore_opts->vars[i].classname);
202 xlockmore_screenhack (Display *dpy, Window window,
203 Bool want_writable_colors,
204 Bool want_uniform_colors,
205 Bool want_smooth_colors,
206 Bool want_bright_colors,
207 void (*hack_init) (ModeInfo *),
208 void (*hack_draw) (ModeInfo *),
209 void (*hack_free) (ModeInfo *))
218 memset(&mi, 0, sizeof(mi));
221 XGetWindowAttributes (dpy, window, &mi.xgwa);
223 color.flags = DoRed|DoGreen|DoBlue;
224 color.red = color.green = color.blue = 0;
225 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
227 mi.black = color.pixel;
228 color.red = color.green = color.blue = 0xFFFF;
229 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
231 mi.white = color.pixel;
235 static unsigned long pixels[2];
236 static XColor colors[2];
241 pixels[0] = mi.black;
242 pixels[1] = mi.white;
243 colors[0].flags = DoRed|DoGreen|DoBlue;
244 colors[1].flags = DoRed|DoGreen|DoBlue;
245 colors[0].red = colors[0].green = colors[0].blue = 0;
246 colors[1].red = colors[1].green = colors[1].blue = 0xFFFF;
247 mi.writable_p = False;
251 mi.npixels = get_integer_resource ("ncolors", "Integer");
254 else if (mi.npixels > MAX_COLORS)
255 mi.npixels = MAX_COLORS;
257 mi.colors = (XColor *) calloc (mi.npixels, sizeof (*mi.colors));
259 mi.writable_p = want_writable_colors;
261 if (want_uniform_colors)
262 make_uniform_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
263 mi.colors, &mi.npixels,
264 True, &mi.writable_p, True);
265 else if (want_smooth_colors)
266 make_smooth_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
267 mi.colors, &mi.npixels,
268 True, &mi.writable_p, True);
270 make_random_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
271 mi.colors, &mi.npixels,
273 True, &mi.writable_p, True);
280 mi.pixels = (unsigned long *)
281 calloc (mi.npixels, sizeof (*mi.pixels));
282 for (i = 0; i < mi.npixels; i++)
283 mi.pixels[i] = mi.colors[i].pixel;
287 gcv.foreground = mi.white;
288 gcv.background = mi.black;
289 mi.gc = XCreateGC(dpy, window, GCForeground|GCBackground, &gcv);
291 mi.fullrandom = True;
293 mi.pause = get_integer_resource ("delay", "Usecs");
295 mi.cycles = get_integer_resource ("cycles", "Int");
296 mi.batchcount = get_integer_resource ("count", "Int");
297 mi.size = get_integer_resource ("size", "Int");
299 mi.threed = get_boolean_resource ("use3d", "Boolean");
300 mi.threed_delta = get_float_resource ("delta3d", "Boolean");
301 mi.threed_right_color = get_pixel_resource ("right3d", "Color", dpy,
303 mi.threed_left_color = get_pixel_resource ("left3d", "Color", dpy,
305 mi.threed_both_color = get_pixel_resource ("both3d", "Color", dpy,
307 mi.threed_none_color = get_pixel_resource ("none3d", "Color", dpy,
310 mi.wireframe_p = get_boolean_resource ("wireframe", "Boolean");
311 mi.root_p = (window == RootWindowOfScreen (mi.xgwa.screen));
312 #ifdef HAVE_XSHM_EXTENSION
313 mi.use_shm = get_boolean_resource ("useSHM", "Boolean");
314 #endif /* !HAVE_XSHM_EXTENSION */
318 else if (mi.pause > 100000000)
319 mi.pause = 100000000;
320 orig_pause = mi.pause;
322 xlockmore_read_resources ();
324 XClearWindow (dpy, window);
327 start = time((time_t) 0);
333 screenhack_handle_events (dpy);
336 mi.pause = orig_pause;
340 if (i++ > (mi.batchcount / 4) &&
341 (start + 5) < (now = time((time_t) 0)))