1 /* xlockmore.c --- xscreensaver compatibility layer for xlockmore modules.
2 * xscreensaver, Copyright (c) 1997, 1998, 2001, 2002, 2004
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"
27 #include <X11/Intrinsic.h>
29 #define countof(x) (sizeof((x))/sizeof(*(x)))
31 #define MAX_COLORS (1L<<13)
33 extern XtAppContext app;
34 extern ModeSpecOpt xlockmore_opts[];
35 extern const char *app_defaults;
38 pre_merge_options (void)
43 /* Translate the xlockmore `opts[]' argument to a form that
46 for (i = 0; i < xlockmore_opts->numopts; i++)
48 XrmOptionDescRec *old = &xlockmore_opts->opts[i];
49 XrmOptionDescRec *new = &options[i];
51 if (old->option[0] == '-')
52 new->option = old->option;
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);
61 new->specifier = strrchr (old->specifier, '.');
62 if (!new->specifier) abort();
64 new->argKind = old->argKind;
65 new->value = old->value;
68 /* Add extra args, if they're mentioned in the defaults... */
70 char *args[] = { "-count", "-cycles", "-delay", "-ncolors",
71 "-size", "-wireframe", "-use3d", "-useSHM",
73 for (j = 0; j < countof(args); j++)
74 if (strstr(app_defaults, args[j]+1))
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"))
82 new->argKind = XrmoptionNoArg;
85 new->option = "-no-wireframe";
86 new->specifier = options[i-2].specifier;
87 new->argKind = XrmoptionNoArg;
90 else if (!strcmp(new->option, "-use3d"))
93 new->argKind = XrmoptionNoArg;
96 new->option = "-no-3d";
97 new->specifier = options[i-2].specifier;
98 new->argKind = XrmoptionNoArg;
101 else if (!strcmp(new->option, "-useSHM"))
103 new->option = "-shm";
104 new->argKind = XrmoptionNoArg;
107 new->option = "-no-shm";
108 new->specifier = options[i-2].specifier;
109 new->argKind = XrmoptionNoArg;
110 new->value = "False";
112 else if (!strcmp(new->option, "-showFPS"))
114 new->option = "-fps";
115 new->argKind = XrmoptionNoArg;
118 new->option = "-no-fps";
119 new->specifier = options[i-2].specifier;
120 new->argKind = XrmoptionNoArg;
121 new->value = "False";
125 new->argKind = XrmoptionSepArg;
132 /* Construct the kind of `defaults' that screenhack.c expects from
133 the xlockmore `vars[]' argument.
137 /* Put on the PROGCLASS.background/foreground resources. */
138 s = (char *) malloc(50);
139 strcpy (s, progclass);
140 strcat (s, ".background: black");
143 s = (char *) malloc(50);
144 strcpy (s, progclass);
145 strcat (s, ".foreground: white");
148 /* Copy the lines out of the `app_defaults' var and into this array. */
149 s = strdup (app_defaults);
158 /* Copy the defaults out of the `xlockmore_opts->' variable. */
159 for (j = 0; j < xlockmore_opts->numvarsdesc; j++)
161 const char *def = xlockmore_opts->vars[j].def;
165 if (strlen(def) > 1000) abort();
167 s = (char *) malloc (strlen (xlockmore_opts->vars[j].name) +
170 strcat (s, xlockmore_opts->vars[j].name);
181 xlockmore_read_resources (void)
184 for (i = 0; i < xlockmore_opts->numvarsdesc; i++)
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;
192 switch (xlockmore_opts->vars[i].type)
195 *var_c = get_string_resource (xlockmore_opts->vars[i].name,
196 xlockmore_opts->vars[i].classname);
199 *var_f = get_float_resource (xlockmore_opts->vars[i].name,
200 xlockmore_opts->vars[i].classname);
203 *var_i = get_integer_resource (xlockmore_opts->vars[i].name,
204 xlockmore_opts->vars[i].classname);
207 *var_b = get_boolean_resource (xlockmore_opts->vars[i].name,
208 xlockmore_opts->vars[i].classname);
218 xlockmore_handle_events (ModeInfo *mi,
219 void (*reshape) (ModeInfo *, int, int),
220 Bool (*hook) (ModeInfo *, XEvent *))
222 if (XtAppPending (app) & (XtIMTimer|XtIMAlternateInput))
223 XtAppProcessEvent (app, XtIMTimer|XtIMAlternateInput);
225 while (XPending (mi->dpy))
228 XNextEvent (mi->dpy, &event);
229 if (reshape && event.xany.type == ConfigureNotify)
231 XGetWindowAttributes (mi->dpy, mi->window, &mi->xgwa);
232 reshape (mi, mi->xgwa.width, mi->xgwa.height);
234 else if (hook && hook (mi, &event))
239 screenhack_handle_event (mi->dpy, &event);
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 *))
266 memset(&mi, 0, sizeof(mi));
269 XGetWindowAttributes (dpy, window, &mi.xgwa);
270 root_p = (window == RootWindowOfScreen (mi.xgwa.screen));
272 /* If this is the root window, don't allow ButtonPress to be selected.
273 Bad Things Happen. */
275 event_mask &= (~(ButtonPressMask|ButtonReleaseMask));
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));
281 color.flags = DoRed|DoGreen|DoBlue;
282 color.red = color.green = color.blue = 0;
283 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
285 mi.black = color.pixel;
286 color.red = color.green = color.blue = 0xFFFF;
287 if (!XAllocColor(dpy, mi.xgwa.colormap, &color))
289 mi.white = color.pixel;
293 static unsigned long pixels[2];
294 static XColor colors[2];
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;
309 mi.npixels = get_integer_resource ("ncolors", "Integer");
312 else if (mi.npixels > MAX_COLORS)
313 mi.npixels = MAX_COLORS;
315 mi.colors = (XColor *) calloc (mi.npixels, sizeof (*mi.colors));
317 mi.writable_p = want_writable_colors;
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);
328 make_random_colormap (dpy, mi.xgwa.visual, mi.xgwa.colormap,
329 mi.colors, &mi.npixels,
331 True, &mi.writable_p, True);
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;
344 gcv.foreground = mi.white;
345 gcv.background = mi.black;
346 mi.gc = XCreateGC(dpy, window, GCForeground|GCBackground, &gcv);
348 mi.fullrandom = True;
350 mi.pause = get_integer_resource ("delay", "Usecs");
352 mi.cycles = get_integer_resource ("cycles", "Int");
353 mi.batchcount = get_integer_resource ("count", "Int");
354 mi.size = get_integer_resource ("size", "Int");
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,
360 mi.threed_left_color = get_pixel_resource ("left3d", "Color", dpy,
362 mi.threed_both_color = get_pixel_resource ("both3d", "Color", dpy,
364 mi.threed_none_color = get_pixel_resource ("none3d", "Color", dpy,
367 mi.wireframe_p = get_boolean_resource ("wireframe", "Boolean");
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 */
376 else if (mi.pause > 100000000)
377 mi.pause = 100000000;
378 orig_pause = mi.pause;
380 xlockmore_read_resources ();
382 XClearWindow (dpy, window);
385 start = time((time_t) 0);
391 xlockmore_handle_events (&mi, hack_reshape, hack_handle_events);
394 mi.pause = orig_pause;
398 if (i++ > (mi.batchcount / 4) &&
399 (start + 5) < (now = time((time_t) 0)))