summaryrefslogtreecommitdiff
path: root/uisimulator/screenhack.c
diff options
context:
space:
mode:
Diffstat (limited to 'uisimulator/screenhack.c')
-rw-r--r--uisimulator/screenhack.c581
1 files changed, 581 insertions, 0 deletions
diff --git a/uisimulator/screenhack.c b/uisimulator/screenhack.c
new file mode 100644
index 0000000000..538400944e
--- /dev/null
+++ b/uisimulator/screenhack.c
@@ -0,0 +1,581 @@
1/* xscreensaver, Copyright (c) 1992, 1995, 1997, 1998
2 * Jamie Zawinski <jwz@jwz.org>
3 *
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
10 * implied warranty.
11 *
12 * And remember: X Windows is to graphics hacking as roman numerals are to
13 * the square root of pi.
14 */
15
16/* This file contains simple code to open a window or draw on the root.
17 The idea being that, when writing a graphics hack, you can just link
18 with this .o to get all of the uninteresting junk out of the way.
19
20 - create a procedure `screenhack(dpy, window)'
21
22 - create a variable `char *progclass' which names this program's
23 resource class.
24
25 - create a variable `char defaults []' for the default resources, and
26 null-terminate it.
27
28 - create a variable `XrmOptionDescRec options[]' for the command-line,
29 and null-terminate it.
30
31 And that's it...
32 */
33
34#include <stdio.h>
35#include <X11/Intrinsic.h>
36#include <X11/IntrinsicP.h>
37#include <X11/CoreP.h>
38#include <X11/Shell.h>
39#include <X11/StringDefs.h>
40#include <X11/Xutil.h>
41#include <X11/keysym.h>
42
43#ifdef __sgi
44# include <X11/SGIScheme.h> /* for SgiUseSchemes() */
45#endif /* __sgi */
46
47#ifdef HAVE_XMU
48# ifndef VMS
49# include <X11/Xmu/Error.h>
50# else /* VMS */
51# include <Xmu/Error.h>
52# endif
53#else
54# include "xmu.h"
55#endif
56#include "screenhack.h"
57#include "version.h"
58#include "vroot.h"
59
60#ifndef isupper
61# define isupper(c) ((c) >= 'A' && (c) <= 'Z')
62#endif
63#ifndef _tolower
64# define _tolower(c) ((c) - 'A' + 'a')
65#endif
66
67
68char *progname;
69XrmDatabase db;
70XtAppContext app;
71Bool mono_p;
72
73static XrmOptionDescRec default_options [] = {
74 { "-root", ".root", XrmoptionNoArg, "True" },
75 { "-window", ".root", XrmoptionNoArg, "False" },
76 { "-mono", ".mono", XrmoptionNoArg, "True" },
77 { "-install", ".installColormap", XrmoptionNoArg, "True" },
78 { "-noinstall",".installColormap", XrmoptionNoArg, "False" },
79 { "-visual", ".visualID", XrmoptionSepArg, 0 },
80 { "-window-id", ".windowID", XrmoptionSepArg, 0 },
81 { 0, 0, 0, 0 }
82};
83
84static char *default_defaults[] = {
85 ".root: false",
86 "*geometry: 100x200", /* this should be .geometry, but nooooo... */
87 "*mono: false",
88 "*installColormap: false",
89 "*visualID: default",
90 "*windowID: ",
91 0
92};
93
94static XrmOptionDescRec *merged_options;
95static int merged_options_size;
96static char **merged_defaults;
97
98static void
99merge_options (void)
100{
101 int def_opts_size, opts_size;
102 int def_defaults_size, defaults_size;
103
104 for (def_opts_size = 0; default_options[def_opts_size].option;
105 def_opts_size++)
106 ;
107 for (opts_size = 0; options[opts_size].option; opts_size++)
108 ;
109
110 merged_options_size = def_opts_size + opts_size;
111 merged_options = (XrmOptionDescRec *)
112 malloc ((merged_options_size + 1) * sizeof(*default_options));
113 memcpy (merged_options, default_options,
114 (def_opts_size * sizeof(*default_options)));
115 memcpy (merged_options + def_opts_size, options,
116 ((opts_size + 1) * sizeof(*default_options)));
117
118 for (def_defaults_size = 0; default_defaults[def_defaults_size];
119 def_defaults_size++)
120 ;
121 for (defaults_size = 0; defaults[defaults_size]; defaults_size++)
122 ;
123 merged_defaults = (char **)
124 malloc ((def_defaults_size + defaults_size + 1) * sizeof (*defaults));;
125 memcpy (merged_defaults, default_defaults,
126 def_defaults_size * sizeof(*defaults));
127 memcpy (merged_defaults + def_defaults_size, defaults,
128 (defaults_size + 1) * sizeof(*defaults));
129
130 /* This totally sucks. Xt should behave like this by default.
131 If the string in `defaults' looks like ".foo", change that
132 to "Progclass.foo".
133 */
134 {
135 char **s;
136 for (s = merged_defaults; *s; s++)
137 if (**s == '.')
138 {
139 const char *oldr = *s;
140 char *newr = (char *) malloc(strlen(oldr) + strlen(progclass) + 3);
141 strcpy (newr, progclass);
142 strcat (newr, oldr);
143 *s = newr;
144 }
145 }
146}
147
148
149/* Make the X errors print out the name of this program, so we have some
150 clue which one has a bug when they die under the screensaver.
151 */
152
153static int
154screenhack_ehandler (Display *dpy, XErrorEvent *error)
155{
156 fprintf (stderr, "\nX error in %s:\n", progname);
157 if (XmuPrintDefaultErrorMessage (dpy, error, stderr))
158 exit (-1);
159 else
160 fprintf (stderr, " (nonfatal.)\n");
161 return 0;
162}
163
164static Bool
165MapNotify_event_p (Display *dpy, XEvent *event, XPointer window)
166{
167 return (event->xany.type == MapNotify &&
168 event->xvisibility.window == (Window) window);
169}
170
171
172#ifdef XLOCKMORE
173extern void pre_merge_options (void);
174#endif
175
176
177static Atom XA_WM_PROTOCOLS, XA_WM_DELETE_WINDOW;
178
179/* Dead-trivial event handling: exits if "q" or "ESC" are typed.
180 Exit if the WM_PROTOCOLS WM_DELETE_WINDOW ClientMessage is received.
181 */
182void
183screenhack_handle_event (Display *dpy, XEvent *event)
184{
185 switch (event->xany.type)
186 {
187 case KeyPress:
188 {
189 KeySym keysym;
190 unsigned char c = 0;
191 XLookupString (&event->xkey, &c, 1, &keysym, 0);
192 if (c == 'q' ||
193 c == 'Q' ||
194 c == 3 || /* ^C */
195 c == 27) /* ESC */
196 exit (0);
197 else if (! (keysym >= XK_Shift_L && keysym <= XK_Hyper_R))
198 XBell (dpy, 0); /* beep for non-chord keys */
199
200 fprintf(stderr, "KEY PRESSED: %c (%02x)\n", c, c);
201 }
202 break;
203 case ResizeRequest:
204 screen_resized(event->xresizerequest.width, event->xresizerequest.height);
205 screen_redraw();
206 fprintf(stderr, "WINDOW RESIZED to width %d height %d\n",
207 event->xresizerequest.width, event->xresizerequest.height);
208 break;
209 default:
210 fprintf(stderr, "EVENT: %d (see /usr/include/X11/X.h)\n",
211 event->xany.type);
212 break;
213 case Expose:
214 screen_redraw();
215 fprintf(stderr, "EXPOSE: x: %d y: %d width: %d height: %d\n",
216 event->xexpose.x, event->xexpose.y,
217 event->xexpose.width, event->xexpose.height);
218 break;
219 case ButtonPress:
220 fprintf(stderr, "BUTTON PRESSED\n");
221 break;
222 case ClientMessage:
223 {
224 if (event->xclient.message_type != XA_WM_PROTOCOLS)
225 {
226 char *s = XGetAtomName(dpy, event->xclient.message_type);
227 if (!s) s = "(null)";
228 fprintf (stderr, "%s: unknown ClientMessage %s received!\n",
229 progname, s);
230 }
231 else if (event->xclient.data.l[0] != XA_WM_DELETE_WINDOW)
232 {
233 char *s1 = XGetAtomName(dpy, event->xclient.message_type);
234 char *s2 = XGetAtomName(dpy, event->xclient.data.l[0]);
235 if (!s1) s1 = "(null)";
236 if (!s2) s2 = "(null)";
237 fprintf (stderr, "%s: unknown ClientMessage %s[%s] received!\n",
238 progname, s1, s2);
239 }
240 else
241 {
242 exit (0);
243 }
244 }
245 break;
246 }
247}
248
249
250void
251screenhack_handle_events (Display *dpy)
252{
253 while (XPending (dpy))
254 {
255 XEvent event;
256 XNextEvent (dpy, &event);
257 screenhack_handle_event (dpy, &event);
258 }
259}
260
261
262static Visual *
263pick_visual (Screen *screen)
264{
265#ifdef USE_GL
266 /* If we're linking against GL (that is, this is the version of screenhack.o
267 that the GL hacks will use, which is different from the one that the
268 non-GL hacks will use) then try to pick the "best" visual by interrogating
269 the GL library instead of by asking Xlib. GL knows better.
270 */
271 Visual *v = 0;
272 char *string = get_string_resource ("visualID", "VisualID");
273 char *s;
274
275 if (string)
276 for (s = string; *s; s++)
277 if (isupper (*s)) *s = _tolower (*s);
278
279 if (!string || !*string ||
280 !strcmp (string, "gl") ||
281 !strcmp (string, "best") ||
282 !strcmp (string, "color") ||
283 !strcmp (string, "default"))
284 v = get_gl_visual (screen); /* from ../utils/visual-gl.c */
285
286 if (string)
287 free (string);
288 if (v)
289 return v;
290#endif /* USE_GL */
291
292 return get_visual_resource (screen, "visualID", "VisualID", False);
293}
294
295
296/* Notice when the user has requested a different visual or colormap
297 on a pre-existing window (e.g., "-root -visual truecolor" or
298 "-window-id 0x2c00001 -install") and complain, since when drawing
299 on an existing window, we have no choice about these things.
300 */
301static void
302visual_warning (Screen *screen, Window window, Visual *visual, Colormap cmap,
303 Bool window_p)
304{
305 char *visual_string = get_string_resource ("visualID", "VisualID");
306 Visual *desired_visual = pick_visual (screen);
307 char win[100];
308 char why[100];
309
310 if (window == RootWindowOfScreen (screen))
311 strcpy (win, "root window");
312 else
313 sprintf (win, "window 0x%x", (unsigned long) window);
314
315 if (window_p)
316 sprintf (why, "-window-id 0x%x", (unsigned long) window);
317 else
318 strcpy (why, "-root");
319
320 if (visual_string && *visual_string)
321 {
322 char *s;
323 for (s = visual_string; *s; s++)
324 if (isupper (*s)) *s = _tolower (*s);
325
326 if (!strcmp (visual_string, "default") ||
327 !strcmp (visual_string, "default") ||
328 !strcmp (visual_string, "best"))
329 /* don't warn about these, just silently DWIM. */
330 ;
331 else if (visual != desired_visual)
332 {
333 fprintf (stderr, "%s: ignoring `-visual %s' because of `%s'.\n",
334 progname, visual_string, why);
335 fprintf (stderr, "%s: using %s's visual 0x%x.\n",
336 progname, win, XVisualIDFromVisual (visual));
337 }
338 free (visual_string);
339 }
340
341 if (visual == DefaultVisualOfScreen (screen) &&
342 has_writable_cells (screen, visual) &&
343 get_boolean_resource ("installColormap", "InstallColormap"))
344 {
345 fprintf (stderr, "%s: ignoring `-install' because of `%s'.\n",
346 progname, why);
347 fprintf (stderr, "%s: using %s's colormap 0x%x.\n",
348 progname, win, (unsigned long) cmap);
349 }
350}
351
352
353int
354main (int argc, char **argv)
355{
356 Widget toplevel;
357 Display *dpy;
358 Window window;
359 Screen *screen;
360 Visual *visual;
361 Colormap cmap;
362 Bool root_p;
363 Window on_window = 0;
364 XEvent event;
365 Boolean dont_clear /*, dont_map */;
366 char version[255];
367
368#ifdef XLOCKMORE
369 pre_merge_options ();
370#endif
371 merge_options ();
372
373#ifdef __sgi
374 /* We have to do this on SGI to prevent the background color from being
375 overridden by the current desktop color scheme (we'd like our backgrounds
376 to be black, thanks.) This should be the same as setting the
377 "*useSchemes: none" resource, but it's not -- if that resource is
378 present in the `default_defaults' above, it doesn't work, though it
379 does work when passed as an -xrm arg on the command line. So screw it,
380 turn them off from C instead.
381 */
382 SgiUseSchemes ("none");
383#endif /* __sgi */
384
385 toplevel = XtAppInitialize (&app, progclass, merged_options,
386 merged_options_size, &argc, argv,
387 merged_defaults, 0, 0);
388 dpy = XtDisplay (toplevel);
389 screen = XtScreen (toplevel);
390 db = XtDatabase (dpy);
391
392 XtGetApplicationNameAndClass (dpy, &progname, &progclass);
393
394 /* half-assed way of avoiding buffer-overrun attacks. */
395 if (strlen (progname) >= 100) progname[100] = 0;
396
397 XSetErrorHandler (screenhack_ehandler);
398
399 XA_WM_PROTOCOLS = XInternAtom (dpy, "WM_PROTOCOLS", False);
400 XA_WM_DELETE_WINDOW = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
401
402
403 if (argc > 1)
404 {
405 const char *s;
406 int i;
407 int x = 18;
408 int end = 78;
409 Bool help_p = !strcmp(argv[1], "-help");
410 fprintf (stderr, "%s\n", version);
411 for (s = progclass; *s; s++) fprintf(stderr, " ");
412 fprintf (stderr, " eXcellent GUI\n\n");
413
414 if (!help_p)
415 fprintf(stderr, "Unrecognised option: %s\n", argv[1]);
416 fprintf (stderr, "Options include: ");
417 for (i = 0; i < merged_options_size; i++)
418 {
419 char *sw = merged_options [i].option;
420 Bool argp = (merged_options [i].argKind == XrmoptionSepArg);
421 int size = strlen (sw) + (argp ? 6 : 0) + 2;
422 if (x + size >= end)
423 {
424 fprintf (stderr, "\n\t\t ");
425 x = 18;
426 }
427 x += size;
428 fprintf (stderr, "%s", sw);
429 if (argp) fprintf (stderr, " <arg>");
430 if (i != merged_options_size - 1) fprintf (stderr, ", ");
431 }
432 fprintf (stderr, ".\n");
433 exit (help_p ? 0 : 1);
434 }
435
436 dont_clear = get_boolean_resource ("dontClearRoot", "Boolean");
437/*dont_map = get_boolean_resource ("dontMapWindow", "Boolean"); */
438 mono_p = get_boolean_resource ("mono", "Boolean");
439 if (CellsOfScreen (DefaultScreenOfDisplay (dpy)) <= 2)
440 mono_p = True;
441
442 root_p = get_boolean_resource ("root", "Boolean");
443
444 {
445 char *s = get_string_resource ("windowID", "WindowID");
446 if (s && *s)
447 on_window = get_integer_resource ("windowID", "WindowID");
448 if (s) free (s);
449 }
450
451 if (on_window)
452 {
453 XWindowAttributes xgwa;
454 window = (Window) on_window;
455 XtDestroyWidget (toplevel);
456 XGetWindowAttributes (dpy, window, &xgwa);
457 cmap = xgwa.colormap;
458 visual = xgwa.visual;
459 visual_warning (screen, window, visual, cmap, True);
460 }
461 else if (root_p)
462 {
463 XWindowAttributes xgwa;
464 window = RootWindowOfScreen (XtScreen (toplevel));
465 XtDestroyWidget (toplevel);
466 XGetWindowAttributes (dpy, window, &xgwa);
467 cmap = xgwa.colormap;
468 visual = xgwa.visual;
469 visual_warning (screen, window, visual, cmap, False);
470 }
471 else
472 {
473 Boolean def_visual_p;
474 visual = pick_visual (screen);
475
476 if (toplevel->core.width <= 0)
477 toplevel->core.width = 600;
478 if (toplevel->core.height <= 0)
479 toplevel->core.height = 480;
480
481 def_visual_p = (visual == DefaultVisualOfScreen (screen));
482
483 if (!def_visual_p)
484 {
485 unsigned int bg, bd;
486 Widget new;
487
488 cmap = XCreateColormap (dpy, RootWindowOfScreen(screen),
489 visual, AllocNone);
490 bg = get_pixel_resource ("background", "Background", dpy, cmap);
491 bd = get_pixel_resource ("borderColor", "Foreground", dpy, cmap);
492
493 new = XtVaAppCreateShell (progname, progclass,
494 topLevelShellWidgetClass, dpy,
495 XtNmappedWhenManaged, False,
496 XtNvisual, visual,
497 XtNdepth, visual_depth (screen, visual),
498 XtNwidth, toplevel->core.width,
499 XtNheight, toplevel->core.height,
500 XtNcolormap, cmap,
501 XtNbackground, (Pixel) bg,
502 XtNborderColor, (Pixel) bd,
503 XtNinput, True, /* for WM_HINTS */
504 0);
505 XtDestroyWidget (toplevel);
506 toplevel = new;
507 XtRealizeWidget (toplevel);
508 window = XtWindow (toplevel);
509 }
510 else
511 {
512 XtVaSetValues (toplevel,
513 XtNmappedWhenManaged, False,
514 XtNinput, True, /* for WM_HINTS */
515 0);
516 XtRealizeWidget (toplevel);
517 window = XtWindow (toplevel);
518
519 if (get_boolean_resource ("installColormap", "InstallColormap"))
520 {
521 cmap = XCreateColormap (dpy, window,
522 DefaultVisualOfScreen (XtScreen (toplevel)),
523 AllocNone);
524 XSetWindowColormap (dpy, window, cmap);
525 }
526 else
527 {
528 cmap = DefaultColormap (dpy, DefaultScreen (dpy));
529 }
530 }
531
532/*
533 if (dont_map)
534 {
535 XtVaSetValues (toplevel, XtNmappedWhenManaged, False, 0);
536 XtRealizeWidget (toplevel);
537 }
538 else
539*/
540 {
541 XtPopup (toplevel, XtGrabNone);
542 }
543
544 XtVaSetValues(toplevel, XtNtitle, version, 0);
545
546 /* For screenhack_handle_events(): select KeyPress, and
547 announce that we accept WM_DELETE_WINDOW. */
548 {
549 XWindowAttributes xgwa;
550 XGetWindowAttributes (dpy, window, &xgwa);
551 XSelectInput (dpy, window,
552 xgwa.your_event_mask | KeyPressMask | ButtonPressMask | ResizeRedirectMask | ExposureMask);
553 XChangeProperty (dpy, window, XA_WM_PROTOCOLS, XA_ATOM, 32,
554 PropModeReplace,
555 (unsigned char *) &XA_WM_DELETE_WINDOW, 1);
556 }
557 }
558
559 if (!dont_clear)
560 {
561 XSetWindowBackground (dpy, window,
562 get_pixel_resource ("background", "Background",
563 dpy, cmap));
564 XClearWindow (dpy, window);
565 }
566
567 if (!root_p && !on_window)
568 /* wait for it to be mapped */
569 XIfEvent (dpy, &event, MapNotify_event_p, (XPointer) window);
570
571 XSync (dpy, False);
572
573 /* This is the one and only place that the random-number generator is
574 seeded in any screenhack. You do not need to seed the RNG again,
575 it is done for you before your code is invoked. */
576# undef ya_rand_init
577 ya_rand_init ((int) time ((time_t *) 0));
578
579 screenhack (dpy, window); /* doesn't return */
580 return 0;
581}