793c6e3ac7e6a6616d041c232a04aae9fec28a6d
[xscreensaver] / hacks / screenhack.c
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
68 char *progname;
69 XrmDatabase db;
70 XtAppContext app;
71 Bool mono_p;
72
73 static 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
84 static char *default_defaults[] = {
85   ".root:               false",
86   "*geometry:           600x480", /* this should be .geometry, but nooooo... */
87   "*mono:               false",
88   "*installColormap:    false",
89   "*visualID:           default",
90   "*windowID:           ",
91   0
92 };
93
94 static XrmOptionDescRec *merged_options;
95 static int merged_options_size;
96 static char **merged_defaults;
97
98 static void
99 merge_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 \f
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
153 static int
154 screenhack_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
164 static Bool
165 MapNotify_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
173 extern void pre_merge_options (void);
174 #endif
175
176
177 static 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  */
182 void
183 screenhack_handle_event (Display *dpy, XEvent *event)
184 {
185   switch (event->xany.type)
186     {
187     case KeyPress:
188       {
189         KeySym keysym;
190         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       break;
201     case ButtonPress:
202       XBell (dpy, 0);
203       break;
204     case ClientMessage:
205       {
206         if (event->xclient.message_type != XA_WM_PROTOCOLS)
207           {
208             char *s = XGetAtomName(dpy, event->xclient.message_type);
209             if (!s) s = "(null)";
210             fprintf (stderr, "%s: unknown ClientMessage %s received!\n",
211                      progname, s);
212           }
213         else if (event->xclient.data.l[0] != XA_WM_DELETE_WINDOW)
214           {
215             char *s1 = XGetAtomName(dpy, event->xclient.message_type);
216             char *s2 = XGetAtomName(dpy, event->xclient.data.l[0]);
217             if (!s1) s1 = "(null)";
218             if (!s2) s2 = "(null)";
219             fprintf (stderr, "%s: unknown ClientMessage %s[%s] received!\n",
220                      progname, s1, s2);
221           }
222         else
223           {
224             exit (0);
225           }
226       }
227       break;
228     }
229 }
230
231
232 void
233 screenhack_handle_events (Display *dpy)
234 {
235   while (XPending (dpy))
236     {
237       XEvent event;
238       XNextEvent (dpy, &event);
239       screenhack_handle_event (dpy, &event);
240     }
241 }
242
243
244 static Visual *
245 pick_visual (Screen *screen)
246 {
247 #ifdef USE_GL
248   /* If we're linking against GL (that is, this is the version of screenhack.o
249      that the GL hacks will use, which is different from the one that the
250      non-GL hacks will use) then try to pick the "best" visual by interrogating
251      the GL library instead of by asking Xlib.  GL knows better.
252    */
253   Visual *v = 0;
254   char *string = get_string_resource ("visualID", "VisualID");
255   char *s;
256
257   if (string)
258     for (s = string; *s; s++)
259       if (isupper (*s)) *s = _tolower (*s);
260
261   if (!string || !*string ||
262       !strcmp (string, "gl") ||
263       !strcmp (string, "best") ||
264       !strcmp (string, "color") ||
265       !strcmp (string, "default"))
266     v = get_gl_visual (screen);         /* from ../utils/visual-gl.c */
267
268   if (string)
269     free (string);
270   if (v)
271     return v;
272 #endif /* USE_GL */
273
274   return get_visual_resource (screen, "visualID", "VisualID", False);
275 }
276
277
278 /* Notice when the user has requested a different visual or colormap
279    on a pre-existing window (e.g., "-root -visual truecolor" or
280    "-window-id 0x2c00001 -install") and complain, since when drawing
281    on an existing window, we have no choice about these things.
282  */
283 static void
284 visual_warning (Screen *screen, Window window, Visual *visual, Colormap cmap,
285                 Bool window_p)
286 {
287   char *visual_string = get_string_resource ("visualID", "VisualID");
288   Visual *desired_visual = pick_visual (screen);
289   char win[100];
290   char why[100];
291
292   if (window == RootWindowOfScreen (screen))
293     strcpy (win, "root window");
294   else
295     sprintf (win, "window 0x%x", (unsigned long) window);
296
297   if (window_p)
298     sprintf (why, "-window-id 0x%x", (unsigned long) window);
299   else
300     strcpy (why, "-root");
301
302   if (visual_string && *visual_string)
303     {
304       char *s;
305       for (s = visual_string; *s; s++)
306         if (isupper (*s)) *s = _tolower (*s);
307
308       if (!strcmp (visual_string, "default") ||
309           !strcmp (visual_string, "default") ||
310           !strcmp (visual_string, "best"))
311         /* don't warn about these, just silently DWIM. */
312         ;
313       else if (visual != desired_visual)
314         {
315           fprintf (stderr, "%s: ignoring `-visual %s' because of `%s'.\n",
316                    progname, visual_string, why);
317           fprintf (stderr, "%s: using %s's visual 0x%x.\n",
318                    progname, win, XVisualIDFromVisual (visual));
319         }
320       free (visual_string);
321     }
322
323   if (visual == DefaultVisualOfScreen (screen) &&
324       has_writable_cells (screen, visual) &&
325       get_boolean_resource ("installColormap", "InstallColormap"))
326     {
327       fprintf (stderr, "%s: ignoring `-install' because of `%s'.\n",
328                progname, why);
329       fprintf (stderr, "%s: using %s's colormap 0x%x.\n",
330                progname, win, (unsigned long) cmap);
331     }
332 }
333
334
335 int
336 main (int argc, char **argv)
337 {
338   Widget toplevel;
339   Display *dpy;
340   Window window;
341   Screen *screen;
342   Visual *visual;
343   Colormap cmap;
344   Bool root_p;
345   Window on_window = 0;
346   XEvent event;
347   Boolean dont_clear /*, dont_map */;
348   char version[255];
349
350 #ifdef XLOCKMORE
351   pre_merge_options ();
352 #endif
353   merge_options ();
354
355 #ifdef __sgi
356   /* We have to do this on SGI to prevent the background color from being
357      overridden by the current desktop color scheme (we'd like our backgrounds
358      to be black, thanks.)  This should be the same as setting the
359      "*useSchemes: none" resource, but it's not -- if that resource is
360      present in the `default_defaults' above, it doesn't work, though it
361      does work when passed as an -xrm arg on the command line.  So screw it,
362      turn them off from C instead.
363    */
364   SgiUseSchemes ("none"); 
365 #endif /* __sgi */
366
367   toplevel = XtAppInitialize (&app, progclass, merged_options,
368                               merged_options_size, &argc, argv,
369                               merged_defaults, 0, 0);
370   dpy = XtDisplay (toplevel);
371   screen = XtScreen (toplevel);
372   db = XtDatabase (dpy);
373
374   XtGetApplicationNameAndClass (dpy, &progname, &progclass);
375
376   /* half-assed way of avoiding buffer-overrun attacks. */
377   if (strlen (progname) >= 100) progname[100] = 0;
378
379   XSetErrorHandler (screenhack_ehandler);
380
381   XA_WM_PROTOCOLS = XInternAtom (dpy, "WM_PROTOCOLS", False);
382   XA_WM_DELETE_WINDOW = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
383
384   {
385     char *v = (char *) strdup(strchr(screensaver_id, ' '));
386     char *s1, *s2, *s3, *s4;
387     s1 = (char *) strchr(v,  ' '); s1++;
388     s2 = (char *) strchr(s1, ' ');
389     s3 = (char *) strchr(v,  '('); s3++;
390     s4 = (char *) strchr(s3, ')');
391     *s2 = 0;
392     *s4 = 0;
393     sprintf (version, "%s: from the XScreenSaver %s distribution (%s.)",
394              progclass, s1, s3);
395     free(v);
396   }
397
398   if (argc > 1)
399     {
400       const char *s;
401       int i;
402       int x = 18;
403       int end = 78;
404       Bool help_p = !strcmp(argv[1], "-help");
405       fprintf (stderr, "%s\n", version);
406       for (s = progclass; *s; s++) fprintf(stderr, " ");
407       fprintf (stderr, "  http://www.jwz.org/xscreensaver/\n\n");
408
409       if (!help_p)
410         fprintf(stderr, "Unrecognised option: %s\n", argv[1]);
411       fprintf (stderr, "Options include: ");
412       for (i = 0; i < merged_options_size; i++)
413         {
414           char *sw = merged_options [i].option;
415           Bool argp = (merged_options [i].argKind == XrmoptionSepArg);
416           int size = strlen (sw) + (argp ? 6 : 0) + 2;
417           if (x + size >= end)
418             {
419               fprintf (stderr, "\n\t\t ");
420               x = 18;
421             }
422           x += size;
423           fprintf (stderr, "%s", sw);
424           if (argp) fprintf (stderr, " <arg>");
425           if (i != merged_options_size - 1) fprintf (stderr, ", ");
426         }
427       fprintf (stderr, ".\n");
428       exit (help_p ? 0 : 1);
429     }
430
431   dont_clear = get_boolean_resource ("dontClearRoot", "Boolean");
432 /*dont_map = get_boolean_resource ("dontMapWindow", "Boolean"); */
433   mono_p = get_boolean_resource ("mono", "Boolean");
434   if (CellsOfScreen (DefaultScreenOfDisplay (dpy)) <= 2)
435     mono_p = True;
436
437   root_p = get_boolean_resource ("root", "Boolean");
438
439   {
440     char *s = get_string_resource ("windowID", "WindowID");
441     if (s && *s)
442       on_window = get_integer_resource ("windowID", "WindowID");
443     if (s) free (s);
444   }
445
446   if (on_window)
447     {
448       XWindowAttributes xgwa;
449       window = (Window) on_window;
450       XtDestroyWidget (toplevel);
451       XGetWindowAttributes (dpy, window, &xgwa);
452       cmap = xgwa.colormap;
453       visual = xgwa.visual;
454       visual_warning (screen, window, visual, cmap, True);
455     }
456   else if (root_p)
457     {
458       XWindowAttributes xgwa;
459       window = RootWindowOfScreen (XtScreen (toplevel));
460       XtDestroyWidget (toplevel);
461       XGetWindowAttributes (dpy, window, &xgwa);
462       cmap = xgwa.colormap;
463       visual = xgwa.visual;
464       visual_warning (screen, window, visual, cmap, False);
465     }
466   else
467     {
468       Boolean def_visual_p;
469       visual = pick_visual (screen);
470
471       if (toplevel->core.width <= 0)
472         toplevel->core.width = 600;
473       if (toplevel->core.height <= 0)
474         toplevel->core.height = 480;
475
476       def_visual_p = (visual == DefaultVisualOfScreen (screen));
477
478       if (!def_visual_p)
479         {
480           unsigned int bg, bd;
481           Widget new;
482
483           cmap = XCreateColormap (dpy, RootWindowOfScreen(screen),
484                                   visual, AllocNone);
485           bg = get_pixel_resource ("background", "Background", dpy, cmap);
486           bd = get_pixel_resource ("borderColor", "Foreground", dpy, cmap);
487
488           new = XtVaAppCreateShell (progname, progclass,
489                                     topLevelShellWidgetClass, dpy,
490                                     XtNmappedWhenManaged, False,
491                                     XtNvisual, visual,
492                                     XtNdepth, visual_depth (screen, visual),
493                                     XtNwidth, toplevel->core.width,
494                                     XtNheight, toplevel->core.height,
495                                     XtNcolormap, cmap,
496                                     XtNbackground, (Pixel) bg,
497                                     XtNborderColor, (Pixel) bd,
498                                     XtNinput, True,  /* for WM_HINTS */
499                                     0);
500           XtDestroyWidget (toplevel);
501           toplevel = new;
502           XtRealizeWidget (toplevel);
503           window = XtWindow (toplevel);
504         }
505       else
506         {
507           XtVaSetValues (toplevel,
508                          XtNmappedWhenManaged, False,
509                          XtNinput, True,  /* for WM_HINTS */
510                          0);
511           XtRealizeWidget (toplevel);
512           window = XtWindow (toplevel);
513
514           if (get_boolean_resource ("installColormap", "InstallColormap"))
515             {
516               cmap = XCreateColormap (dpy, window,
517                                    DefaultVisualOfScreen (XtScreen (toplevel)),
518                                       AllocNone);
519               XSetWindowColormap (dpy, window, cmap);
520             }
521           else
522             {
523               cmap = DefaultColormap (dpy, DefaultScreen (dpy));
524             }
525         }
526
527 /*
528       if (dont_map)
529         {
530           XtVaSetValues (toplevel, XtNmappedWhenManaged, False, 0);
531           XtRealizeWidget (toplevel);
532         }
533       else
534 */
535         {
536           XtPopup (toplevel, XtGrabNone);
537         }
538
539       XtVaSetValues(toplevel, XtNtitle, version, 0);
540
541       /* For screenhack_handle_events(): select KeyPress, and
542          announce that we accept WM_DELETE_WINDOW. */
543       {
544         XWindowAttributes xgwa;
545         XGetWindowAttributes (dpy, window, &xgwa);
546         XSelectInput (dpy, window,
547                       xgwa.your_event_mask | KeyPressMask | ButtonPressMask);
548         XChangeProperty (dpy, window, XA_WM_PROTOCOLS, XA_ATOM, 32,
549                          PropModeReplace,
550                          (unsigned char *) &XA_WM_DELETE_WINDOW, 1);
551       }
552     }
553
554   if (!dont_clear)
555     {
556       XSetWindowBackground (dpy, window,
557                             get_pixel_resource ("background", "Background",
558                                                 dpy, cmap));
559       XClearWindow (dpy, window);
560     }
561
562   if (!root_p && !on_window)
563     /* wait for it to be mapped */
564     XIfEvent (dpy, &event, MapNotify_event_p, (XPointer) window);
565
566   XSync (dpy, False);
567
568   /* This is the one and only place that the random-number generator is
569      seeded in any screenhack.  You do not need to seed the RNG again,
570      it is done for you before your code is invoked. */
571 # undef ya_rand_init
572   ya_rand_init (0);
573
574   screenhack (dpy, window); /* doesn't return */
575   return 0;
576 }