1 /* xlockmore.c --- xscreensaver compatibility layer for xlockmore modules.
2 * xscreensaver, Copyright (c) 1997, 1998, 2001, 2002
3 * Jamie Zawinski <jwz@jwz.org>
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
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...)
25 #include "screenhack.h"
26 #include "xlockmoreI.h"
28 #define countof(x) (sizeof((x))/sizeof(*(x)))
30 #define MAX_COLORS (1L<<13)
32 extern ModeSpecOpt xlockmore_opts[];
33 extern const char *app_defaults;
36 pre_merge_options (void)
41 /* Translate the xlockmore `opts[]' argument to a form that
44 for (i = 0; i < xlockmore_opts->numopts; i++)
46 XrmOptionDescRec *old = &xlockmore_opts->opts[i];
47 XrmOptionDescRec *new = &options[i];
49 if (old->option[0] == '-')
50 new->option = old->option;
53 /* Convert "+foo" to "-no-foo". */
54 new->option = (char *) malloc (strlen(old->option) + 5);
55 strcpy (new->option, "-no-");
56 strcat (new->option, old->option + 1);
59 new->specifier = strrchr (old->specifier, '.');
60 if (!new->specifier) abort();
62 new->argKind = old->argKind;
63 new->value = old->value;
66 /* Add extra args, if they're mentioned in the defaults... */
68 char *args[] = { "-count", "-cycles", "-delay", "-ncolors",
69 "-size", "-wireframe", "-use3d", "-useSHM",
71 for (j = 0; j < countof(args); j++)
72 if (strstr(app_defaults, args[j]+1))
74 XrmOptionDescRec *new = &options[i++];
75 new->option = args[j];
76 new->specifier = strdup(args[j]);
77 new->specifier[0] = '.';
78 if (!strcmp(new->option, "-wireframe"))
80 new->argKind = XrmoptionNoArg;
83 new->option = "-no-wireframe";
84 new->specifier = options[i-2].specifier;
85 new->argKind = XrmoptionNoArg;
88 else if (!strcmp(new->option, "-use3d"))
91 new->argKind = XrmoptionNoArg;
94 new->option = "-no-3d";
95 new->specifier = options[i-2].specifier;
96 new->argKind = XrmoptionNoArg;
99 else if (!strcmp(new->option, "-useSHM"))
101 new->option = "-shm";
102 new->argKind = XrmoptionNoArg;
105 new->option = "-no-shm";
106 new->specifier = options[i-2].specifier;
107 new->argKind = XrmoptionNoArg;
108 new->value = "False";
110 else if (!strcmp(new->option, "-showFPS"))
112 new->option = "-fps";
113 new->argKind = XrmoptionNoArg;
116 new->option = "-no-fps";
117 new->specifier = options[i-2].specifier;
118 new->argKind = XrmoptionNoArg;
119 new->value = "False";
123 new->argKind = XrmoptionSepArg;
130 /* Construct the kind of `defaults' that screenhack.c expects from
131 the xlockmore `vars[]' argument.
135 /* Put on the PROGCLASS.background/foreground resources. */
136 s = (char *) malloc(50);
137 strcpy (s, progclass);
138 strcat (s, ".background: black");
141 s = (char *) malloc(50);
142 strcpy (s, progclass);
143 strcat (s, ".foreground: white");
146 /* Copy the lines out of the `app_defaults' var and into this array. */
147 s = strdup (app_defaults);
156 /* Copy the defaults out of the `xlockmore_opts->' variable. */
157 for (j = 0; j < xlockmore_opts->numvarsdesc; j++)
159 const char *def = xlockmore_opts->vars[j].def;
163 if (strlen(def) > 1000) abort();
165 s = (char *) malloc (strlen (xlockmore_opts->vars[j].name) +
168 strcat (s, xlockmore_opts->vars[j].name);
179 xlockmore_read_resources (void)
182 for (i = 0; i < xlockmore_opts->numvarsdesc; i++)
184 void *var = xlockmore_opts->vars[i].var;
185 Bool *var_b = (Bool *) var;
186 char **var_c = (char **) var;
187 int *var_i = (int *) var;
188 float *var_f = (float *) var;
190 switch (xlockmore_opts->vars[i].type)
193 *var_c = get_string_resource (xlockmore_opts->vars[i].name,
194 xlockmore_opts->vars[i].classname);
197 *var_f = get_float_resource (xlockmore_opts->vars[i].name,
198 xlockmore_opts->vars[i].classname);
201 *var_i = get_integer_resource (xlockmore_opts->vars[i].name,
202 xlockmore_opts->vars[i].classname);
205 *var_b = get_boolean_resource (xlockmore_opts->vars[i].name,
206 xlockmore_opts->vars[i].classname);
216 xlockmore_handle_events (ModeInfo *mi,
217 void (*reshape) (ModeInfo *, int, int),
218 Bool (*hook) (ModeInfo *, XEvent *))
220 while (XPending (mi->dpy))
223 XNextEvent (mi->dpy, &event);
224 if (reshape && event.xany.type == ConfigureNotify)
226 XGetWindowAttributes (mi->dpy, mi->window, &mi->xgwa);
227 reshape (mi, mi->xgwa.width, mi->xgwa.height);
229 else if (hook && hook (mi, &event))
234 screenhack_handle_event (mi->dpy, &event);
241 xlockmore_screenhack (Display *dpy, Window window,
242 Bool want_writable_colors,
243 Bool want_uniform_colors,
244 Bool want_smooth_colors,
245 Bool want_bright_colors,
246 unsigned long event_mask,
247 void (*hack_init) (ModeInfo *),
248 void (*hack_draw) (ModeInfo *),
249 void (*hack_reshape) (ModeInfo *, int, int),
250 Bool (*hack_handle_events) (ModeInfo *, XEvent *),
251 void (*hack_free) (ModeInfo *))
261 memset(&mi, 0, sizeof(mi));
264 XGetWindowAttributes (dpy, window, &mi.xgwa);
265 root_p = (window == RootWindowOfScreen (mi.xgwa.screen));
267 /* If this is the root window, don't allow ButtonPress to be selected.
268 Bad Things Happen. */
270 event_mask &= (~(ButtonPressMask|ButtonReleaseMask));
272 /* If this hack wants additional events, select them. */
273 if (event_mask && ! (mi.xgwa.your_event_mask & event_mask))
274 XSelectInput (dpy, window, (mi.xgwa.your_event_mask | event_mask));
276 color.flags = DoRed|DoGreen|DoBlue;
277 color.red = color.green = color.blue = 0;
278 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
280 mi.black = color.pixel;
281 color.red = color.green = color.blue = 0xFFFF;
282 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
284 mi.white = color.pixel;
288 static unsigned long pixels[2];
289 static XColor colors[2];
294 pixels[0] = mi.black;
295 pixels[1] = mi.white;
296 colors[0].flags = DoRed|DoGreen|DoBlue;
297 colors[1].flags = DoRed|DoGreen|DoBlue;
298 colors[0].red = colors[0].green = colors[0].blue = 0;
299 colors[1].red = colors[1].green = colors[1].blue = 0xFFFF;
300 mi.writable_p = False;
304 mi.npixels = get_integer_resource ("ncolors", "Integer");
307 else if (mi.npixels > MAX_COLORS)
308 mi.npixels = MAX_COLORS;
310 mi.colors = (XColor *) calloc (mi.npixels, sizeof (*mi.colors));
312 mi.writable_p = want_writable_colors;
314 if (want_uniform_colors)
315 make_uniform_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
316 mi.colors, &mi.npixels,
317 True, &mi.writable_p, True);
318 else if (want_smooth_colors)
319 make_smooth_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
320 mi.colors, &mi.npixels,
321 True, &mi.writable_p, True);
323 make_random_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
324 mi.colors, &mi.npixels,
326 True, &mi.writable_p, True);
333 mi.pixels = (unsigned long *)
334 calloc (mi.npixels, sizeof (*mi.pixels));
335 for (i = 0; i < mi.npixels; i++)
336 mi.pixels[i] = mi.colors[i].pixel;
340 gcv.foreground = mi.white;
341 gcv.background = mi.black;
342 mi.gc = XCreateGC(dpy, window, GCForeground|GCBackground, &gcv);
344 mi.fullrandom = True;
346 mi.pause = get_integer_resource ("delay", "Usecs");
348 mi.cycles = get_integer_resource ("cycles", "Int");
349 mi.batchcount = get_integer_resource ("count", "Int");
350 mi.size = get_integer_resource ("size", "Int");
352 mi.threed = get_boolean_resource ("use3d", "Boolean");
353 mi.threed_delta = get_float_resource ("delta3d", "Boolean");
354 mi.threed_right_color = get_pixel_resource ("right3d", "Color", dpy,
356 mi.threed_left_color = get_pixel_resource ("left3d", "Color", dpy,
358 mi.threed_both_color = get_pixel_resource ("both3d", "Color", dpy,
360 mi.threed_none_color = get_pixel_resource ("none3d", "Color", dpy,
363 mi.wireframe_p = get_boolean_resource ("wireframe", "Boolean");
365 mi.fps_p = get_boolean_resource ("showFPS", "Boolean");
366 #ifdef HAVE_XSHM_EXTENSION
367 mi.use_shm = get_boolean_resource ("useSHM", "Boolean");
368 #endif /* !HAVE_XSHM_EXTENSION */
372 else if (mi.pause > 100000000)
373 mi.pause = 100000000;
374 orig_pause = mi.pause;
376 xlockmore_read_resources ();
378 XClearWindow (dpy, window);
381 start = time((time_t) 0);
387 xlockmore_handle_events (&mi, hack_reshape, hack_handle_events);
390 mi.pause = orig_pause;
394 if (i++ > (mi.batchcount / 4) &&
395 (start + 5) < (now = time((time_t) 0)))