1 /* xlockmore.c --- xscreensaver compatibility layer for xlockmore modules.
2 * xscreensaver, Copyright (c) 1997, 1998, 2001 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",
68 for (j = 0; j < countof(args); j++)
69 if (strstr(app_defaults, args[j]+1))
71 XrmOptionDescRec *new = &options[i++];
72 new->option = args[j];
73 new->specifier = strdup(args[j]);
74 new->specifier[0] = '.';
75 if (!strcmp(new->option, "-wireframe"))
77 new->argKind = XrmoptionNoArg;
80 new->option = "-no-wireframe";
81 new->specifier = options[i-2].specifier;
82 new->argKind = XrmoptionNoArg;
85 else if (!strcmp(new->option, "-use3d"))
88 new->argKind = XrmoptionNoArg;
91 new->option = "-no-3d";
92 new->specifier = options[i-2].specifier;
93 new->argKind = XrmoptionNoArg;
96 else if (!strcmp(new->option, "-useSHM"))
99 new->argKind = XrmoptionNoArg;
102 new->option = "-no-shm";
103 new->specifier = options[i-2].specifier;
104 new->argKind = XrmoptionNoArg;
105 new->value = "False";
107 else if (!strcmp(new->option, "-showFPS"))
109 new->option = "-fps";
110 new->argKind = XrmoptionNoArg;
113 new->option = "-no-fps";
114 new->specifier = options[i-2].specifier;
115 new->argKind = XrmoptionNoArg;
116 new->value = "False";
120 new->argKind = XrmoptionSepArg;
127 /* Construct the kind of `defaults' that screenhack.c expects from
128 the xlockmore `vars[]' argument.
132 /* Put on the PROGCLASS.background/foreground resources. */
133 s = (char *) malloc(50);
134 strcpy (s, progclass);
135 strcat (s, ".background: black");
138 s = (char *) malloc(50);
139 strcpy (s, progclass);
140 strcat (s, ".foreground: white");
143 /* Copy the lines out of the `app_defaults' var and into this array. */
144 s = strdup (app_defaults);
153 /* Copy the defaults out of the `xlockmore_opts->' variable. */
154 for (j = 0; j < xlockmore_opts->numvarsdesc; j++)
156 const char *def = xlockmore_opts->vars[j].def;
160 if (strlen(def) > 1000) abort();
162 s = (char *) malloc (strlen (xlockmore_opts->vars[j].name) +
165 strcat (s, xlockmore_opts->vars[j].name);
176 xlockmore_read_resources (void)
179 for (i = 0; i < xlockmore_opts->numvarsdesc; i++)
181 void *var = xlockmore_opts->vars[i].var;
182 Bool *var_b = (Bool *) var;
183 char **var_c = (char **) var;
184 int *var_i = (int *) var;
185 float *var_f = (float *) var;
187 switch (xlockmore_opts->vars[i].type)
190 *var_c = get_string_resource (xlockmore_opts->vars[i].name,
191 xlockmore_opts->vars[i].classname);
194 *var_f = get_float_resource (xlockmore_opts->vars[i].name,
195 xlockmore_opts->vars[i].classname);
198 *var_i = get_integer_resource (xlockmore_opts->vars[i].name,
199 xlockmore_opts->vars[i].classname);
202 *var_b = get_boolean_resource (xlockmore_opts->vars[i].name,
203 xlockmore_opts->vars[i].classname);
213 xlockmore_handle_events (ModeInfo *mi,
214 void (*reshape) (ModeInfo *, int, int))
216 while (XPending (mi->dpy))
219 XNextEvent (mi->dpy, &event);
220 if (reshape && event.xany.type == ConfigureNotify)
222 XGetWindowAttributes (mi->dpy, mi->window, &mi->xgwa);
223 reshape (mi, mi->xgwa.width, mi->xgwa.height);
227 screenhack_handle_event (mi->dpy, &event);
234 xlockmore_screenhack (Display *dpy, Window window,
235 Bool want_writable_colors,
236 Bool want_uniform_colors,
237 Bool want_smooth_colors,
238 Bool want_bright_colors,
239 void (*hack_init) (ModeInfo *),
240 void (*hack_draw) (ModeInfo *),
241 void (*hack_reshape) (ModeInfo *, int, int),
242 void (*hack_free) (ModeInfo *))
251 memset(&mi, 0, sizeof(mi));
254 XGetWindowAttributes (dpy, window, &mi.xgwa);
256 color.flags = DoRed|DoGreen|DoBlue;
257 color.red = color.green = color.blue = 0;
258 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
260 mi.black = color.pixel;
261 color.red = color.green = color.blue = 0xFFFF;
262 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
264 mi.white = color.pixel;
268 static unsigned long pixels[2];
269 static XColor colors[2];
274 pixels[0] = mi.black;
275 pixels[1] = mi.white;
276 colors[0].flags = DoRed|DoGreen|DoBlue;
277 colors[1].flags = DoRed|DoGreen|DoBlue;
278 colors[0].red = colors[0].green = colors[0].blue = 0;
279 colors[1].red = colors[1].green = colors[1].blue = 0xFFFF;
280 mi.writable_p = False;
284 mi.npixels = get_integer_resource ("ncolors", "Integer");
287 else if (mi.npixels > MAX_COLORS)
288 mi.npixels = MAX_COLORS;
290 mi.colors = (XColor *) calloc (mi.npixels, sizeof (*mi.colors));
292 mi.writable_p = want_writable_colors;
294 if (want_uniform_colors)
295 make_uniform_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
296 mi.colors, &mi.npixels,
297 True, &mi.writable_p, True);
298 else if (want_smooth_colors)
299 make_smooth_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
300 mi.colors, &mi.npixels,
301 True, &mi.writable_p, True);
303 make_random_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
304 mi.colors, &mi.npixels,
306 True, &mi.writable_p, True);
313 mi.pixels = (unsigned long *)
314 calloc (mi.npixels, sizeof (*mi.pixels));
315 for (i = 0; i < mi.npixels; i++)
316 mi.pixels[i] = mi.colors[i].pixel;
320 gcv.foreground = mi.white;
321 gcv.background = mi.black;
322 mi.gc = XCreateGC(dpy, window, GCForeground|GCBackground, &gcv);
324 mi.fullrandom = True;
326 mi.pause = get_integer_resource ("delay", "Usecs");
328 mi.cycles = get_integer_resource ("cycles", "Int");
329 mi.batchcount = get_integer_resource ("count", "Int");
330 mi.size = get_integer_resource ("size", "Int");
332 mi.threed = get_boolean_resource ("use3d", "Boolean");
333 mi.threed_delta = get_float_resource ("delta3d", "Boolean");
334 mi.threed_right_color = get_pixel_resource ("right3d", "Color", dpy,
336 mi.threed_left_color = get_pixel_resource ("left3d", "Color", dpy,
338 mi.threed_both_color = get_pixel_resource ("both3d", "Color", dpy,
340 mi.threed_none_color = get_pixel_resource ("none3d", "Color", dpy,
343 mi.wireframe_p = get_boolean_resource ("wireframe", "Boolean");
344 mi.root_p = (window == RootWindowOfScreen (mi.xgwa.screen));
345 mi.fps_p = get_boolean_resource ("showFPS", "Boolean");
346 #ifdef HAVE_XSHM_EXTENSION
347 mi.use_shm = get_boolean_resource ("useSHM", "Boolean");
348 #endif /* !HAVE_XSHM_EXTENSION */
352 else if (mi.pause > 100000000)
353 mi.pause = 100000000;
354 orig_pause = mi.pause;
356 xlockmore_read_resources ();
358 XClearWindow (dpy, window);
361 start = time((time_t) 0);
367 xlockmore_handle_events (&mi, hack_reshape);
370 mi.pause = orig_pause;
374 if (i++ > (mi.batchcount / 4) &&
375 (start + 5) < (now = time((time_t) 0)))