1 /* xscreensaver, Copyright (c) 1991-2003 Jamie Zawinski <jwz@jwz.org>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
12 /* ========================================================================
13 * First we wait until the keyboard and mouse become idle for the specified
14 * amount of time. We do this in one of three different ways: periodically
15 * checking with the XIdle server extension; selecting key and mouse events
16 * on (nearly) all windows; or by waiting for the MIT-SCREEN-SAVER extension
17 * to send us a "you are idle" event.
19 * Then, we map a full screen black window.
21 * We place a __SWM_VROOT property on this window, so that newly-started
22 * clients will think that this window is a "virtual root" window (as per
23 * the logic in the historical "vroot.h" header.)
25 * If there is an existing "virtual root" window (one that already had
26 * an __SWM_VROOT property) then we remove that property from that window.
27 * Otherwise, clients would see that window (the real virtual root) instead
28 * of ours (the impostor.)
30 * Then we pick a random program to run, and start it. Two assumptions
31 * are made about this program: that it has been specified with whatever
32 * command-line options are necessary to make it run on the root window;
33 * and that it has been compiled with vroot.h, so that it is able to find
34 * the root window when a virtual-root window manager (or this program) is
37 * Then, we wait for keyboard or mouse events to be generated on the window.
38 * When they are, we kill the inferior process, unmap the window, and restore
39 * the __SWM_VROOT property to the real virtual root window if there was one.
41 * On multi-screen systems, we do the above on each screen, and start
42 * multiple programs, each with a different value of $DISPLAY.
44 * On Xinerama systems, we do a similar thing, but instead create multiple
45 * windows on the (only) display, and tell the subprocess which one to use
46 * via the $XSCREENSAVER_WINDOW environment variable -- this trick requires
47 * a recent (Aug 2003) revision of vroot.h.
49 * While we are waiting for user activity, we also set up timers so that,
50 * after a certain amount of time has passed, we can start a different
51 * screenhack. We do this by killing the running child process with
52 * SIGTERM, and then starting a new one in the same way.
54 * If there was a real virtual root, meaning that we removed the __SWM_VROOT
55 * property from it, meaning we must (absolutely must) restore it before we
56 * exit, then we set up signal handlers for most signals (SIGINT, SIGTERM,
57 * etc.) that do this. Most Xlib and Xt routines are not reentrant, so it
58 * is not generally safe to call them from signal handlers; however, this
59 * program spends most of its time waiting, so the window of opportunity
60 * when code could be called reentrantly is fairly small; and also, the worst
61 * that could happen is that the call would fail. If we've gotten one of
62 * these signals, then we're on our way out anyway. If we didn't restore the
63 * __SWM_VROOT property, that would be very bad, so it's worth a shot. Note
64 * that this means that, if you're using a virtual-root window manager, you
65 * can really fuck up the world by killing this process with "kill -9".
67 * This program accepts ClientMessages of type SCREENSAVER; these messages
68 * may contain the atoms ACTIVATE, DEACTIVATE, etc, meaning to turn the
69 * screensaver on or off now, regardless of the idleness of the user,
70 * and a few other things. The included "xscreensaver-command" program
71 * sends these messsages.
73 * If we don't have the XIdle, MIT-SCREEN-SAVER, or SGI SCREEN_SAVER
74 * extensions, then we do the XAutoLock trick: notice every window that
75 * gets created, and wait 30 seconds or so until its creating process has
76 * settled down, and then select KeyPress events on those windows which
77 * already select for KeyPress events. It's important that we not select
78 * KeyPress on windows which don't select them, because that would
79 * interfere with event propagation. This will break if any program
80 * changes its event mask to contain KeyRelease or PointerMotion more than
81 * 30 seconds after creating the window, but such programs do not seem to
82 * occur in nature (I've never seen it happen in all these years.)
84 * The reason that we can't select KeyPresses on windows that don't have
85 * them already is that, when dispatching a KeyPress event, X finds the
86 * lowest (leafmost) window in the hierarchy on which *any* client selects
87 * for KeyPress, and sends the event to that window. This means that if a
88 * client had a window with subwindows, and expected to receive KeyPress
89 * events on the parent window instead of the subwindows, then that client
90 * would malfunction if some other client selected KeyPress events on the
91 * subwindows. It is an incredible misdesign that one client can make
92 * another client malfunction in this way.
94 * To detect mouse motion, we periodically wake up and poll the mouse
95 * position and button/modifier state, and notice when something has
96 * changed. We make this check every five seconds by default, and since the
97 * screensaver timeout has a granularity of one minute, this makes the
98 * chance of a false positive very small. We could detect mouse motion in
99 * the same way as keyboard activity, but that would suffer from the same
100 * "client changing event mask" problem that the KeyPress events hack does.
101 * I think polling is more reliable.
103 * On systems with /proc/interrupts (Linux) we poll that file and note when
104 * the interrupt counter numbers on the "keyboard" and "PS/2" lines change.
105 * (There is no reliable way, using /proc/interrupts, to detect non-PS/2
106 * mice, so it doesn't help for serial or USB mice.)
108 * None of this crap happens if we're using one of the extensions. Sadly,
109 * the XIdle extension hasn't been available for many years; the SGI
110 * extension only exists on SGIs; and the MIT extension, while widely
111 * deployed, is garbage in several ways.
113 * A third idle-detection option could be implemented (but is not): when
114 * running on the console display ($DISPLAY is `localhost`:0) and we're on a
115 * machine where /dev/tty and /dev/mouse have reasonable last-modification
116 * times, we could just stat() those. But the incremental benefit of
117 * implementing this is really small, so forget I said anything.
120 * - Have a second terminal handy.
121 * - Be careful where you set your breakpoints, you don't want this to
122 * stop under the debugger with the keyboard grabbed or the blackout
124 * - If you run your debugger under XEmacs, try M-ESC (x-grab-keyboard)
125 * to keep your emacs window alive even when xscreensaver has grabbed.
126 * - Go read the code related to `debug_p'.
127 * - You probably can't set breakpoints in functions that are called on
128 * the other side of a call to fork() -- if your subprocesses are
129 * dying with signal 5, Trace/BPT Trap, you're losing in this way.
130 * - If you aren't using a server extension, don't leave this stopped
131 * under the debugger for very long, or the X input buffer will get
132 * huge because of the keypress events it's selecting for. This can
133 * make your X server wedge with "no more input buffers."
135 * ======================================================================== */
143 #include <X11/Xlib.h>
145 #include <X11/Xlibint.h>
147 #include <X11/Xatom.h>
148 #include <X11/Intrinsic.h>
149 #include <X11/StringDefs.h>
150 #include <X11/Shell.h>
153 #include <sys/time.h>
154 #include <netdb.h> /* for gethostbyname() */
157 # include <X11/Xmu/Error.h>
159 # include <Xmu/Error.h>
161 #else /* !HAVE_XMU */
163 #endif /* !HAVE_XMU */
165 #ifdef HAVE_XIDLE_EXTENSION
166 # include <X11/extensions/xidle.h>
167 #endif /* HAVE_XIDLE_EXTENSION */
170 # include <X11/extensions/Xinerama.h>
171 #endif /* HAVE_XINERAMA */
173 #include "xscreensaver.h"
175 #include "yarandom.h"
176 #include "resources.h"
180 saver_info *global_si_kludge = 0; /* I hate C so much... */
187 static Atom XA_SCREENSAVER_RESPONSE;
188 static Atom XA_ACTIVATE, XA_DEACTIVATE, XA_CYCLE, XA_NEXT, XA_PREV;
189 static Atom XA_RESTART, XA_SELECT;
190 static Atom XA_THROTTLE, XA_UNTHROTTLE;
191 Atom XA_DEMO, XA_PREFS, XA_EXIT, XA_LOCK, XA_BLANK;
194 static XrmOptionDescRec options [] = {
196 { "-verbose", ".verbose", XrmoptionNoArg, "on" },
197 { "-silent", ".verbose", XrmoptionNoArg, "off" },
199 /* xscreensaver-demo uses this one */
200 { "-nosplash", ".splash", XrmoptionNoArg, "off" },
201 { "-no-splash", ".splash", XrmoptionNoArg, "off" },
203 /* useful for debugging */
204 { "-no-capture-stderr", ".captureStderr", XrmoptionNoArg, "off" },
206 /* There's really no reason to have these command-line args; they just
207 lead to confusion when the .xscreensaver file has conflicting values.
210 { "-splash", ".splash", XrmoptionNoArg, "on" },
211 { "-capture-stderr", ".captureStderr", XrmoptionNoArg, "on" },
212 { "-timeout", ".timeout", XrmoptionSepArg, 0 },
213 { "-cycle", ".cycle", XrmoptionSepArg, 0 },
214 { "-lock-mode", ".lock", XrmoptionNoArg, "on" },
215 { "-no-lock-mode", ".lock", XrmoptionNoArg, "off" },
216 { "-no-lock", ".lock", XrmoptionNoArg, "off" },
217 { "-lock-timeout", ".lockTimeout", XrmoptionSepArg, 0 },
218 { "-lock-vts", ".lockVTs", XrmoptionNoArg, "on" },
219 { "-no-lock-vts", ".lockVTs", XrmoptionNoArg, "off" },
220 { "-visual", ".visualID", XrmoptionSepArg, 0 },
221 { "-install", ".installColormap", XrmoptionNoArg, "on" },
222 { "-no-install", ".installColormap", XrmoptionNoArg, "off" },
223 { "-timestamp", ".timestamp", XrmoptionNoArg, "on" },
224 { "-xidle-extension", ".xidleExtension", XrmoptionNoArg, "on" },
225 { "-no-xidle-extension", ".xidleExtension", XrmoptionNoArg, "off" },
226 { "-mit-extension", ".mitSaverExtension",XrmoptionNoArg, "on" },
227 { "-no-mit-extension", ".mitSaverExtension",XrmoptionNoArg, "off" },
228 { "-sgi-extension", ".sgiSaverExtension",XrmoptionNoArg, "on" },
229 { "-no-sgi-extension", ".sgiSaverExtension",XrmoptionNoArg, "off" },
230 { "-proc-interrupts", ".procInterrupts", XrmoptionNoArg, "on" },
231 { "-no-proc-interrupts", ".procInterrupts", XrmoptionNoArg, "off" },
232 { "-idelay", ".initialDelay", XrmoptionSepArg, 0 },
233 { "-nice", ".nice", XrmoptionSepArg, 0 },
238 __extension__ /* shut up about "string length is greater than the length
239 ISO C89 compilers are required to support" when including
243 static char *defaults[] = {
244 #include "XScreenSaver_ad.h"
249 ERROR! You must not include vroot.h in this file.
253 do_help (saver_info *si)
258 xscreensaver %s, copyright (c) 1991-2004 by Jamie Zawinski <jwz@jwz.org>\n\
260 All xscreensaver configuration is via the `~/.xscreensaver' file.\n\
261 Rather than editing that file by hand, just run `xscreensaver-demo':\n\
262 that program lets you configure the screen saver graphically,\n\
263 including timeouts, locking, and display modes.\n\
267 Just getting started? Try this:\n\
272 For updates, online manual, and FAQ, please see the web page:\n\
274 http://www.jwz.org/xscreensaver/\n\
286 time_t now = time ((time_t *) 0);
287 char *str = (char *) ctime (&now);
288 char *nl = (char *) strchr (str, '\n');
289 if (nl) *nl = 0; /* take off that dang newline */
293 static Bool blurb_timestamp_p = False; /* kludge */
298 if (!blurb_timestamp_p)
302 static char buf[255];
303 char *ct = timestring();
304 int n = strlen(progname);
306 strncpy(buf, progname, n);
309 strncpy(buf+n, ct+11, 8);
310 strcpy(buf+n+9, ": ");
317 saver_ehandler (Display *dpy, XErrorEvent *error)
319 saver_info *si = global_si_kludge; /* I hate C so much... */
323 if (!real_stderr) real_stderr = stderr;
325 fprintf (real_stderr, "\n"
326 "#######################################"
327 "#######################################\n\n"
328 "%s: X Error! PLEASE REPORT THIS BUG.\n",
331 for (i = 0; i < si->nscreens; i++)
333 saver_screen_info *ssi = &si->screens[i];
334 fprintf (real_stderr, "%s: screen %d/%d: 0x%x, 0x%x, 0x%x\n",
335 blurb(), ssi->real_screen_number, ssi->number,
336 (unsigned int) RootWindowOfScreen (si->screens[i].screen),
337 (unsigned int) si->screens[i].real_vroot,
338 (unsigned int) si->screens[i].screensaver_window);
341 fprintf (real_stderr, "\n"
342 "#######################################"
343 "#######################################\n\n");
345 fatal_p = XmuPrintDefaultErrorMessage (dpy, error, real_stderr);
347 fatal_p = True; /* The only time I've ever seen a supposedly nonfatal error,
348 it has been BadImplementation / Xlib sequence lost, which
349 are in truth pretty damned fatal.
352 fprintf (real_stderr, "\n");
355 fprintf (real_stderr, "%s: nonfatal error.\n\n", blurb());
358 if (si->prefs.xsync_p)
360 saver_exit (si, -1, "because of synchronous X Error");
364 fprintf (real_stderr,
365 "#######################################"
366 "#######################################\n\n");
367 fprintf (real_stderr,
368 " If at all possible, please re-run xscreensaver with the command\n"
369 " line arguments `-sync -verbose -no-capture', and reproduce this\n"
370 " bug. That will cause xscreensaver to dump a `core' file to the\n"
371 " current directory. Please include the stack trace from that core\n"
372 " file in your bug report. *DO NOT* mail the core file itself!\n"
373 " That won't work.\n");
374 fprintf (real_stderr,
376 " http://www.jwz.org/xscreensaver/bugs.html explains how to create\n"
377 " the most useful bug reports, and how to examine core files.\n"
379 " The more information you can provide, the better. But please\n"
380 " report this bug, regardless!\n"
382 fprintf (real_stderr,
383 "#######################################"
384 "#######################################\n\n");
386 saver_exit (si, -1, 0);
394 /* This error handler is used only while the X connection is being set up;
395 after we've got a connection, we don't use this handler again. The only
396 reason for having this is so that we can present a more idiot-proof error
397 message than "cannot open display."
400 startup_ehandler (String name, String type, String class,
401 String defalt, /* one can't even spel properly
402 in this joke of a language */
403 String *av, Cardinal *ac)
407 saver_info *si = global_si_kludge; /* I hate C so much... */
408 XrmDatabase *db = XtAppGetErrorDatabase(si->app);
410 XtAppGetErrorDatabaseText(si->app, name, type, class, defalt,
411 fmt, sizeof(fmt)-1, *db);
413 fprintf (stderr, "%s: ", blurb());
415 memset (p, 0, sizeof(p));
416 if (*ac > countof (p)) *ac = countof (p);
417 memcpy ((char *) p, (char *) av, (*ac) * sizeof(*av));
418 fprintf (stderr, fmt, /* Did I mention that I hate C? */
419 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9]);
420 fprintf (stderr, "\n");
422 describe_uids (si, stderr);
424 if (si->orig_uid && !strncmp (si->orig_uid, "root/", 5))
426 fprintf (stderr, "\n"
427 "%s: This is probably because you're logging in as root. You\n"
428 " shouldn't log in as root: you should log in as a normal user,\n"
429 " and then `su' as needed. If you insist on logging in as\n"
430 " root, you will have to turn off X's security features before\n"
431 " xscreensaver will work.\n"
433 " Please read the manual and FAQ for more information:\n",
438 fprintf (stderr, "\n"
439 "%s: Errors at startup are usually authorization problems.\n"
440 " But you're not logging in as root (good!) so something\n"
441 " else must be wrong. Did you read the manual and the FAQ?\n",
445 fprintf (stderr, "\n"
446 " http://www.jwz.org/xscreensaver/faq.html\n"
447 " http://www.jwz.org/xscreensaver/man.html\n"
456 /* The zillions of initializations.
459 /* Set progname, version, etc. This is done very early.
462 set_version_string (saver_info *si, int *argc, char **argv)
464 progclass = "XScreenSaver";
466 /* progname is reset later, after we connect to X. */
467 progname = strrchr(argv[0], '/');
468 if (progname) progname++;
469 else progname = argv[0];
471 if (strlen(progname) > 100) /* keep it short. */
474 /* The X resource database blows up if argv[0] has a "." in it. */
477 while ((s = strchr (s, '.')))
481 si->version = (char *) malloc (5);
482 memcpy (si->version, screensaver_id + 17, 4);
487 /* Initializations that potentially take place as a priveleged user:
488 If the xscreensaver executable is setuid root, then these initializations
489 are run as root, before discarding privileges.
492 privileged_initialization (saver_info *si, int *argc, char **argv)
495 /* before hack_uid() for proper permissions */
496 lock_priv_init (*argc, argv, si->prefs.verbose_p);
497 #endif /* NO_LOCKING */
503 /* Figure out what locking mechanisms are supported.
506 lock_initialization (saver_info *si, int *argc, char **argv)
509 si->locking_disabled_p = True;
510 si->nolock_reason = "not compiled with locking support";
511 #else /* !NO_LOCKING */
513 /* Finish initializing locking, now that we're out of privileged code. */
514 if (! lock_init (*argc, argv, si->prefs.verbose_p))
516 si->locking_disabled_p = True;
517 si->nolock_reason = "error getting password";
520 /* If locking is currently enabled, but the environment indicates that
521 we have been launched as GDM's "Background" program, then disable
522 locking just in case.
524 if (!si->locking_disabled_p && getenv ("RUNNING_UNDER_GDM"))
526 si->locking_disabled_p = True;
527 si->nolock_reason = "running under GDM";
530 /* If the server is XDarwin (MacOS X) then disable locking.
531 (X grabs only affect X programs, so you can use Command-Tab
532 to bring any other Mac program to the front, e.g., Terminal.)
534 if (!si->locking_disabled_p)
536 int op = 0, event = 0, error = 0;
537 Bool macos_p = False;
540 /* Disable locking if *running* on Apple hardware, since we have no
541 reliable way to determine whether the server is running on MacOS.
542 Hopefully __APPLE__ means "MacOS" and not "Linux on Mac hardware"
543 but I'm not really sure about that.
549 /* This extension exists on the Apple X11 server, but not
550 on earlier versions of the XDarwin server. */
551 macos_p = XQueryExtension (si->dpy, "Apple-DRI", &op, &event, &error);
555 si->locking_disabled_p = True;
556 si->nolock_reason = "Cannot lock securely on MacOS X";
560 #endif /* NO_LOCKING */
564 /* Open the connection to the X server, and intern our Atoms.
567 connect_to_server (saver_info *si, int *argc, char **argv)
569 Widget toplevel_shell;
572 char *d = getenv ("DISPLAY");
575 char *ndpy = strdup("DISPLAY=:0.0");
576 /* if (si->prefs.verbose_p) */ /* sigh, too early to test this... */
578 "%s: warning: $DISPLAY is not set: defaulting to \"%s\".\n",
582 /* don't free (ndpy) -- some implementations of putenv (BSD 4.4,
583 glibc 2.0) copy the argument, but some (libc4,5, glibc 2.1.2)
584 do not. So we must leak it (and/or the previous setting). Yay.
587 #endif /* HAVE_PUTENV */
589 XSetErrorHandler (saver_ehandler);
591 XtAppSetErrorMsgHandler (si->app, startup_ehandler);
592 toplevel_shell = XtAppInitialize (&si->app, progclass,
593 options, XtNumber (options),
594 argc, argv, defaults, 0, 0);
595 XtAppSetErrorMsgHandler (si->app, 0);
597 si->dpy = XtDisplay (toplevel_shell);
598 si->prefs.db = XtDatabase (si->dpy);
599 XtGetApplicationNameAndClass (si->dpy, &progname, &progclass);
601 if(strlen(progname) > 100) /* keep it short. */
604 db = si->prefs.db; /* resources.c needs this */
606 XA_VROOT = XInternAtom (si->dpy, "__SWM_VROOT", False);
607 XA_SCREENSAVER = XInternAtom (si->dpy, "SCREENSAVER", False);
608 XA_SCREENSAVER_VERSION = XInternAtom (si->dpy, "_SCREENSAVER_VERSION",False);
609 XA_SCREENSAVER_ID = XInternAtom (si->dpy, "_SCREENSAVER_ID", False);
610 XA_SCREENSAVER_STATUS = XInternAtom (si->dpy, "_SCREENSAVER_STATUS", False);
611 XA_SCREENSAVER_RESPONSE = XInternAtom (si->dpy, "_SCREENSAVER_RESPONSE",
613 XA_XSETROOT_ID = XInternAtom (si->dpy, "_XSETROOT_ID", False);
614 XA_ESETROOT_PMAP_ID = XInternAtom (si->dpy, "ESETROOT_PMAP_ID", False);
615 XA_XROOTPMAP_ID = XInternAtom (si->dpy, "_XROOTPMAP_ID", False);
616 XA_ACTIVATE = XInternAtom (si->dpy, "ACTIVATE", False);
617 XA_DEACTIVATE = XInternAtom (si->dpy, "DEACTIVATE", False);
618 XA_RESTART = XInternAtom (si->dpy, "RESTART", False);
619 XA_CYCLE = XInternAtom (si->dpy, "CYCLE", False);
620 XA_NEXT = XInternAtom (si->dpy, "NEXT", False);
621 XA_PREV = XInternAtom (si->dpy, "PREV", False);
622 XA_SELECT = XInternAtom (si->dpy, "SELECT", False);
623 XA_EXIT = XInternAtom (si->dpy, "EXIT", False);
624 XA_DEMO = XInternAtom (si->dpy, "DEMO", False);
625 XA_PREFS = XInternAtom (si->dpy, "PREFS", False);
626 XA_LOCK = XInternAtom (si->dpy, "LOCK", False);
627 XA_BLANK = XInternAtom (si->dpy, "BLANK", False);
628 XA_THROTTLE = XInternAtom (si->dpy, "THROTTLE", False);
629 XA_UNTHROTTLE = XInternAtom (si->dpy, "UNTHROTTLE", False);
631 return toplevel_shell;
635 /* Handle the command-line arguments that were not handled for us by Xt.
636 Issue an error message and exit if there are unknown options.
639 process_command_line (saver_info *si, int *argc, char **argv)
642 for (i = 1; i < *argc; i++)
644 if (!strcmp (argv[i], "-debug"))
645 /* no resource for this one, out of paranoia. */
646 si->prefs.debug_p = True;
648 else if (!strcmp (argv[i], "-h") ||
649 !strcmp (argv[i], "-help") ||
650 !strcmp (argv[i], "--help"))
655 const char *s = argv[i];
656 fprintf (stderr, "%s: unknown option \"%s\". Try \"-help\".\n",
659 if (s[0] == '-' && s[1] == '-') s++;
660 if (!strcmp (s, "-activate") ||
661 !strcmp (s, "-deactivate") ||
662 !strcmp (s, "-cycle") ||
663 !strcmp (s, "-next") ||
664 !strcmp (s, "-prev") ||
665 !strcmp (s, "-exit") ||
666 !strcmp (s, "-restart") ||
667 !strcmp (s, "-demo") ||
668 !strcmp (s, "-prefs") ||
669 !strcmp (s, "-preferences") ||
670 !strcmp (s, "-lock") ||
671 !strcmp (s, "-version") ||
672 !strcmp (s, "-time"))
675 if (!strcmp (s, "-demo") || !strcmp (s, "-prefs"))
676 fprintf (stderr, "\n\
677 Perhaps you meant to run the `xscreensaver-demo' program instead?\n");
679 fprintf (stderr, "\n\
680 However, `%s' is an option to the `xscreensaver-command' program.\n", s);
683 The `xscreensaver' program is a daemon that runs in the background.\n\
684 You control a running xscreensaver process by sending it messages\n\
685 with `xscreensaver-demo' or `xscreensaver-command'.\n\
686 . See the man pages for details, or check the web page:\n\
687 http://www.jwz.org/xscreensaver/\n\n");
689 /* Since version 1.21 renamed the "-lock" option to "-lock-mode",
690 suggest that explicitly. */
691 if (!strcmp (s, "-lock"))
693 Or perhaps you meant either the \"-lock-mode\" or the\n\
694 \"-lock-timeout <minutes>\" options to xscreensaver?\n\n");
703 /* Print out the xscreensaver banner to the tty if applicable;
704 Issue any other warnings that are called for at this point.
707 print_banner (saver_info *si)
709 saver_preferences *p = &si->prefs;
711 /* This resource gets set some time before the others, so that we know
712 whether to print the banner (and so that the banner gets printed before
713 any resource-database-related error messages.)
715 p->verbose_p = (p->debug_p || get_boolean_resource ("verbose", "Boolean"));
717 /* Ditto, for the locking_disabled_p message. */
718 p->lock_p = get_boolean_resource ("lock", "Boolean");
722 "%s %s, copyright (c) 1991-2004 "
723 "by Jamie Zawinski <jwz@jwz.org>.\n",
724 progname, si->version);
727 fprintf (stderr, "\n"
728 "%s: Warning: running in DEBUG MODE. Be afraid.\n"
730 "\tNote that in debug mode, the xscreensaver window will only\n"
731 "\tcover the left half of the screen. (The idea is that you\n"
732 "\tcan still see debugging output in a shell, if you position\n"
733 "\tit on the right side of the screen.)\n"
735 "\tDebug mode is NOT SECURE. Do not run with -debug in\n"
736 "\tuntrusted environments.\n"
742 if (!si->uid_message || !*si->uid_message)
743 describe_uids (si, stderr);
746 if (si->orig_uid && *si->orig_uid)
747 fprintf (stderr, "%s: initial effective uid/gid was %s.\n",
748 blurb(), si->orig_uid);
749 fprintf (stderr, "%s: %s\n", blurb(), si->uid_message);
752 fprintf (stderr, "%s: in process %lu.\n", blurb(),
753 (unsigned long) getpid());
758 print_lock_failure_banner (saver_info *si)
760 saver_preferences *p = &si->prefs;
762 /* If locking was not able to be initalized for some reason, explain why.
763 (This has to be done after we've read the lock_p resource.)
765 if (si->locking_disabled_p)
768 fprintf (stderr, "%s: locking is disabled (%s).\n", blurb(),
770 if (strstr (si->nolock_reason, "passw"))
771 fprintf (stderr, "%s: does xscreensaver need to be setuid? "
772 "consult the manual.\n", blurb());
773 else if (strstr (si->nolock_reason, "running as "))
775 "%s: locking only works when xscreensaver is launched\n"
776 "\t by a normal, non-privileged user (e.g., not \"root\".)\n"
777 "\t See the manual for details.\n",
784 /* Examine all of the display's screens, and populate the `saver_screen_info'
785 structures. Make sure this is called after hack_environment() sets $PATH.
788 initialize_per_screen_info (saver_info *si, Widget toplevel_shell)
790 Bool found_any_writable_cells = False;
793 # ifdef HAVE_XINERAMA
796 si->xinerama_p = (XineramaQueryExtension (si->dpy, &event, &error) &&
797 XineramaIsActive (si->dpy));
800 if (si->xinerama_p && ScreenCount (si->dpy) != 1)
802 si->xinerama_p = False;
803 if (si->prefs.verbose_p)
805 "%s: Xinerama AND %d screens? Disabling Xinerama support!\n",
806 blurb(), ScreenCount(si->dpy));
811 XineramaScreenInfo *xsi = XineramaQueryScreens (si->dpy, &si->nscreens);
813 si->xinerama_p = False;
816 si->screens = (saver_screen_info *)
817 calloc(sizeof(saver_screen_info), si->nscreens);
818 for (i = 0; i < si->nscreens; i++)
820 si->screens[i].x = xsi[i].x_org;
821 si->screens[i].y = xsi[i].y_org;
822 si->screens[i].width = xsi[i].width;
823 si->screens[i].height = xsi[i].height;
827 si->default_screen = &si->screens[0];
828 si->default_screen->real_screen_p = True;
830 # endif /* !HAVE_XINERAMA */
834 si->nscreens = ScreenCount(si->dpy);
835 si->screens = (saver_screen_info *)
836 calloc(sizeof(saver_screen_info), si->nscreens);
837 si->default_screen = &si->screens[DefaultScreen(si->dpy)];
839 for (i = 0; i < si->nscreens; i++)
841 saver_screen_info *ssi = &si->screens[i];
842 ssi->width = DisplayWidth (si->dpy, i);
843 ssi->height = DisplayHeight (si->dpy, i);
844 ssi->real_screen_p = True;
845 ssi->real_screen_number = i;
850 /* In "quad mode", we use the Xinerama code to pretend that there are 4
851 screens for every physical screen, and run four times as many hacks...
853 if (si->prefs.quad_p)
855 int ns2 = si->nscreens * 4;
856 saver_screen_info *ssi2 = (saver_screen_info *)
857 calloc(sizeof(saver_screen_info), ns2);
859 for (i = 0; i < si->nscreens; i++)
861 saver_screen_info *old = &si->screens[i];
863 if (si->prefs.debug_p) old->width = old->width / 2;
870 ssi2[i*4 ].width /= 2;
871 ssi2[i*4 ].height /= 2;
873 ssi2[i*4+1].x += ssi2[i*4 ].width;
874 ssi2[i*4+1].width -= ssi2[i*4 ].width;
875 ssi2[i*4+1].height /= 2;
877 ssi2[i*4+2].y += ssi2[i*4 ].height;
878 ssi2[i*4+2].width /= 2;
879 ssi2[i*4+2].height -= ssi2[i*4 ].height;
881 ssi2[i*4+3].x += ssi2[i*4+2].width;
882 ssi2[i*4+3].y += ssi2[i*4+2].height;
883 ssi2[i*4+3].width -= ssi2[i*4+2].width;
884 ssi2[i*4+3].height -= ssi2[i*4+2].height;
886 ssi2[i*4+1].real_screen_p = False;
887 ssi2[i*4+2].real_screen_p = False;
888 ssi2[i*4+3].real_screen_p = False;
894 si->default_screen = &si->screens[DefaultScreen(si->dpy) * 4];
895 si->xinerama_p = True;
898 /* finish initializing the screens.
900 for (i = 0; i < si->nscreens; i++)
902 saver_screen_info *ssi = &si->screens[i];
906 ssi->screen = ScreenOfDisplay (si->dpy, ssi->real_screen_number);
910 ssi->width = WidthOfScreen (ssi->screen);
911 ssi->height = HeightOfScreen (ssi->screen);
914 /* Note: we can't use the resource ".visual" because Xt is SO FUCKED. */
915 ssi->default_visual =
916 get_visual_resource (ssi->screen, "visualID", "VisualID", False);
918 ssi->current_visual = ssi->default_visual;
919 ssi->current_depth = visual_depth (ssi->screen, ssi->current_visual);
921 /* Execute a subprocess to find the GL visual. */
922 ssi->best_gl_visual = get_best_gl_visual (ssi);
924 if (ssi == si->default_screen)
925 /* Since this is the default screen, use the one already created. */
926 ssi->toplevel_shell = toplevel_shell;
928 /* Otherwise, each screen must have its own unmapped root widget. */
929 ssi->toplevel_shell =
930 XtVaAppCreateShell (progname, progclass, applicationShellWidgetClass,
932 XtNscreen, ssi->screen,
933 XtNvisual, ssi->current_visual,
934 XtNdepth, visual_depth (ssi->screen,
935 ssi->current_visual),
938 if (! found_any_writable_cells)
940 /* Check to see whether fading is ever possible -- if any of the
941 screens on the display has a PseudoColor visual, then fading can
942 work (on at least some screens.) If no screen has a PseudoColor
943 visual, then don't bother ever trying to fade, because it will
944 just cause a delay without causing any visible effect.
946 if (has_writable_cells (ssi->screen, ssi->current_visual) ||
947 get_visual (ssi->screen, "PseudoColor", True, False) ||
948 get_visual (ssi->screen, "GrayScale", True, False))
949 found_any_writable_cells = True;
953 si->fading_possible_p = found_any_writable_cells;
955 #ifdef HAVE_XF86VMODE_GAMMA
956 si->fading_possible_p = True; /* if we can gamma fade, go for it */
961 /* If any server extensions have been requested, try and initialize them.
962 Issue warnings if requests can't be honored.
965 initialize_server_extensions (saver_info *si)
967 saver_preferences *p = &si->prefs;
969 Bool server_has_xidle_extension_p = False;
970 Bool server_has_sgi_saver_extension_p = False;
971 Bool server_has_mit_saver_extension_p = False;
972 Bool system_has_proc_interrupts_p = False;
973 const char *piwhy = 0;
975 si->using_xidle_extension = p->use_xidle_extension;
976 si->using_sgi_saver_extension = p->use_sgi_saver_extension;
977 si->using_mit_saver_extension = p->use_mit_saver_extension;
978 si->using_proc_interrupts = p->use_proc_interrupts;
980 #ifdef HAVE_XIDLE_EXTENSION
981 server_has_xidle_extension_p = query_xidle_extension (si);
983 #ifdef HAVE_SGI_SAVER_EXTENSION
984 server_has_sgi_saver_extension_p = query_sgi_saver_extension (si);
986 #ifdef HAVE_MIT_SAVER_EXTENSION
987 server_has_mit_saver_extension_p = query_mit_saver_extension (si);
989 #ifdef HAVE_PROC_INTERRUPTS
990 system_has_proc_interrupts_p = query_proc_interrupts_available (si, &piwhy);
993 if (!server_has_xidle_extension_p)
994 si->using_xidle_extension = False;
995 else if (p->verbose_p)
997 if (si->using_xidle_extension)
998 fprintf (stderr, "%s: using XIDLE extension.\n", blurb());
1000 fprintf (stderr, "%s: not using server's XIDLE extension.\n", blurb());
1003 if (!server_has_sgi_saver_extension_p)
1004 si->using_sgi_saver_extension = False;
1005 else if (p->verbose_p)
1007 if (si->using_sgi_saver_extension)
1008 fprintf (stderr, "%s: using SGI SCREEN_SAVER extension.\n", blurb());
1011 "%s: not using server's SGI SCREEN_SAVER extension.\n",
1015 if (!server_has_mit_saver_extension_p)
1016 si->using_mit_saver_extension = False;
1017 else if (p->verbose_p)
1019 if (si->using_mit_saver_extension)
1020 fprintf (stderr, "%s: using lame MIT-SCREEN-SAVER extension.\n",
1024 "%s: not using server's lame MIT-SCREEN-SAVER extension.\n",
1028 /* These are incompatible (or at least, our support for them is...) */
1029 if (si->xinerama_p && si->using_mit_saver_extension)
1031 si->using_mit_saver_extension = False;
1033 fprintf (stderr, "%s: Xinerama in use: disabling MIT-SCREEN-SAVER.\n",
1038 query_randr_extension (si);
1041 if (!system_has_proc_interrupts_p)
1043 si->using_proc_interrupts = False;
1044 if (p->verbose_p && piwhy)
1045 fprintf (stderr, "%s: not using /proc/interrupts: %s.\n", blurb(),
1048 else if (p->verbose_p)
1050 if (si->using_proc_interrupts)
1052 "%s: consulting /proc/interrupts for keyboard activity.\n",
1056 "%s: not consulting /proc/interrupts for keyboard activity.\n",
1062 /* For the case where we aren't using an server extensions, select user events
1063 on all the existing windows, and launch timers to select events on
1064 newly-created windows as well.
1066 If a server extension is being used, this does nothing.
1069 select_events (saver_info *si)
1071 saver_preferences *p = &si->prefs;
1074 if (si->using_xidle_extension ||
1075 si->using_mit_saver_extension ||
1076 si->using_sgi_saver_extension)
1079 if (p->initial_delay)
1083 fprintf (stderr, "%s: waiting for %d second%s...", blurb(),
1084 (int) p->initial_delay/1000,
1085 (p->initial_delay == 1000 ? "" : "s"));
1089 usleep (p->initial_delay);
1091 fprintf (stderr, " done.\n");
1096 fprintf (stderr, "%s: selecting events on extant windows...", blurb());
1101 /* Select events on the root windows of every screen. This also selects
1102 for window creation events, so that new subwindows will be noticed.
1104 for (i = 0; i < si->nscreens; i++)
1106 saver_screen_info *ssi = &si->screens[i];
1107 if (ssi->real_screen_p)
1108 start_notice_events_timer (si,
1109 RootWindowOfScreen (si->screens[i].screen), False);
1113 fprintf (stderr, " done.\n");
1118 maybe_reload_init_file (saver_info *si)
1120 saver_preferences *p = &si->prefs;
1121 if (init_file_changed_p (p))
1124 fprintf (stderr, "%s: file \"%s\" has changed, reloading.\n",
1125 blurb(), init_file_name());
1129 /* If a server extension is in use, and p->timeout has changed,
1130 we need to inform the server of the new timeout. */
1131 disable_builtin_screensaver (si, False);
1133 /* If the DPMS settings in the init file have changed,
1134 change the settings on the server to match. */
1135 sync_server_dpms_settings (si->dpy,
1136 (p->dpms_enabled_p &&
1137 p->mode != DONT_BLANK),
1138 p->dpms_standby / 1000,
1139 p->dpms_suspend / 1000,
1148 - wait until the user is idle;
1150 - wait until the user is active;
1151 - unblank the screen;
1156 main_loop (saver_info *si)
1158 saver_preferences *p = &si->prefs;
1163 Bool was_locked = False;
1166 fprintf (stderr, "%s: awaiting idleness.\n", blurb());
1168 check_for_leaks ("unblanked A");
1169 sleep_until_idle (si, True);
1170 check_for_leaks ("unblanked B");
1175 fprintf (stderr, "%s: demoing %d at %s.\n", blurb(),
1176 si->selection_mode, timestring());
1178 fprintf (stderr, "%s: blanking screen at %s.\n", blurb(),
1182 maybe_reload_init_file (si);
1184 if (p->mode == DONT_BLANK)
1187 fprintf (stderr, "%s: idle with blanking disabled at %s.\n",
1188 blurb(), timestring());
1190 /* Go around the loop and wait for the next bout of idleness,
1191 or for the init file to change, or for a remote command to
1192 come in, or something.
1197 /* Since we're about to blank the screen, kill the de-race timer,
1198 if any. It might still be running if we have unblanked and then
1199 re-blanked in a short period (e.g., when using the "next" button
1200 in xscreensaver-demo.)
1205 fprintf (stderr, "%s: stopping de-race timer (%d remaining.)\n",
1206 blurb(), si->de_race_ticks);
1207 XtRemoveTimeOut (si->de_race_id);
1212 /* Now, try to blank.
1215 if (! blank_screen (si))
1217 /* We were unable to grab either the keyboard or mouse.
1218 This means we did not (and must not) blank the screen.
1219 If we were to blank the screen while some other program
1220 is holding both the mouse and keyboard grabbed, then
1221 we would never be able to un-blank it! We would never
1222 see any events, and the display would be wedged.
1224 So, just go around the loop again and wait for the
1225 next bout of idleness. (If the user remains idle, we
1226 will next try to blank the screen again in no more than
1229 Time retry = 60 * 1000;
1230 if (p->timeout < retry)
1236 "%s: DEBUG MODE: unable to grab -- BLANKING ANYWAY.\n",
1242 "%s: unable to grab keyboard or mouse! Blanking aborted.\n",
1245 schedule_wakeup_event (si, retry, p->debug_p);
1250 kill_screenhack (si);
1252 if (!si->throttled_p)
1253 spawn_screenhack (si, True);
1254 else if (p->verbose_p)
1255 fprintf (stderr, "%s: not launching hack (throttled.)\n", blurb());
1257 /* Don't start the cycle timer in demo mode. */
1258 if (!si->demoing_p && p->cycle)
1259 si->cycle_id = XtAppAddTimeOut (si->app,
1261 /* see comment in cycle_timer() */
1269 /* Maybe start locking the screen.
1272 Time lock_timeout = p->lock_timeout;
1274 if (si->emergency_lock_p && p->lock_p && lock_timeout)
1276 int secs = p->lock_timeout / 1000;
1279 "%s: locking now, instead of waiting for %d:%02d:%02d.\n",
1281 (secs / (60 * 60)), ((secs / 60) % 60), (secs % 60));
1285 si->emergency_lock_p = False;
1287 if (!si->demoing_p && /* if not going into demo mode */
1288 p->lock_p && /* and locking is enabled */
1289 !si->locking_disabled_p && /* and locking is possible */
1290 lock_timeout == 0) /* and locking is not timer-deferred */
1291 set_locked_p (si, True); /* then lock right now. */
1293 /* locked_p might be true already because of the above, or because of
1294 the LOCK ClientMessage. But if not, and if we're supposed to lock
1295 after some time, set up a timer to do so.
1300 si->lock_id = XtAppAddTimeOut (si->app, lock_timeout,
1301 activate_lock_timer,
1304 #endif /* !NO_LOCKING */
1307 ok_to_unblank = True;
1310 check_for_leaks ("blanked A");
1311 sleep_until_idle (si, False); /* until not idle */
1312 check_for_leaks ("blanked B");
1314 maybe_reload_init_file (si);
1317 /* Maybe unlock the screen.
1321 saver_screen_info *ssi = si->default_screen;
1322 if (si->locking_disabled_p) abort ();
1325 si->dbox_up_p = True;
1326 suspend_screenhack (si, True);
1327 XUndefineCursor (si->dpy, ssi->screensaver_window);
1329 ok_to_unblank = unlock_p (si);
1331 si->dbox_up_p = False;
1332 XDefineCursor (si->dpy, ssi->screensaver_window, ssi->cursor);
1333 suspend_screenhack (si, False); /* resume */
1335 if (!ok_to_unblank &&
1336 !screenhack_running_p (si))
1338 /* If the lock dialog has been dismissed and we're not about to
1339 unlock the screen, and there is currently no hack running,
1340 then launch one. (There might be no hack running if DPMS
1341 had kicked in. But DPMS is off now, so bring back the hack)
1344 XtRemoveTimeOut (si->cycle_id);
1346 cycle_timer ((XtPointer) si, 0);
1349 #endif /* !NO_LOCKING */
1351 } while (!ok_to_unblank);
1355 fprintf (stderr, "%s: unblanking screen at %s.\n",
1356 blurb(), timestring ());
1358 /* Kill before unblanking, to stop drawing as soon as possible. */
1359 kill_screenhack (si);
1360 unblank_screen (si);
1362 set_locked_p (si, False);
1363 si->emergency_lock_p = False;
1365 si->selection_mode = 0;
1367 /* If we're throttled, and the user has explicitly unlocked the screen,
1368 then unthrottle. If we weren't locked, then don't unthrottle
1369 automatically, because someone might have just bumped the desk... */
1372 if (si->throttled_p && p->verbose_p)
1373 fprintf (stderr, "%s: unthrottled.\n", blurb());
1374 si->throttled_p = False;
1379 XtRemoveTimeOut (si->cycle_id);
1385 XtRemoveTimeOut (si->lock_id);
1389 /* Since we're unblanked now, break race conditions and make
1390 sure we stay that way (see comment in timers.c.) */
1391 if (! si->de_race_id)
1392 de_race_timer ((XtPointer) si, 0);
1396 static void analyze_display (saver_info *si);
1397 static void fix_fds (void);
1400 main (int argc, char **argv)
1404 saver_info *si = &the_si;
1405 saver_preferences *p = &si->prefs;
1408 memset(si, 0, sizeof(*si));
1409 global_si_kludge = si; /* I hate C so much... */
1413 # undef ya_rand_init
1416 save_argv (argc, argv);
1417 set_version_string (si, &argc, argv);
1418 privileged_initialization (si, &argc, argv);
1419 hack_environment (si);
1421 shell = connect_to_server (si, &argc, argv);
1422 process_command_line (si, &argc, argv);
1425 load_init_file (p); /* must be before initialize_per_screen_info() */
1426 blurb_timestamp_p = p->timestamp_p; /* kludge */
1427 initialize_per_screen_info (si, shell); /* also sets si->fading_possible_p */
1429 /* We can only issue this warning now. */
1430 if (p->verbose_p && !si->fading_possible_p && (p->fade_p || p->unfade_p))
1432 "%s: there are no PseudoColor or GrayScale visuals.\n"
1433 "%s: ignoring the request for fading/unfading.\n",
1436 for (i = 0; i < si->nscreens; i++)
1438 saver_screen_info *ssi = &si->screens[i];
1439 if (ssi->real_screen_p)
1440 if (ensure_no_screensaver_running (si->dpy, si->screens[i].screen))
1444 lock_initialization (si, &argc, argv);
1445 print_lock_failure_banner (si);
1447 if (p->xsync_p) XSynchronize (si->dpy, True);
1449 if (p->verbose_p) analyze_display (si);
1450 initialize_server_extensions (si);
1452 si->blank_time = time ((time_t) 0); /* must be before ..._window */
1453 initialize_screensaver_window (si);
1458 disable_builtin_screensaver (si, True);
1459 sync_server_dpms_settings (si->dpy,
1460 (p->dpms_enabled_p &&
1461 p->mode != DONT_BLANK),
1462 p->dpms_standby / 1000,
1463 p->dpms_suspend / 1000,
1467 initialize_stderr (si);
1468 handle_signals (si);
1470 make_splash_dialog (si);
1472 main_loop (si); /* doesn't return */
1479 /* Bad Things Happen if stdin, stdout, and stderr have been closed
1480 (as by the `sh incantation "xscreensaver >&- 2>&-"). When you do
1481 that, the X connection gets allocated to one of these fds, and
1482 then some random library writes to stderr, and random bits get
1483 stuffed down the X pipe, causing "Xlib: sequence lost" errors.
1484 So, we cause the first three file descriptors to be open to
1485 /dev/null if they aren't open to something else already. This
1486 must be done before any other files are opened (or the closing
1487 of that other file will again free up one of the "magic" first
1490 We do this by opening /dev/null three times, and then closing
1491 those fds, *unless* any of them got allocated as #0, #1, or #2,
1492 in which case we leave them open. Gag.
1494 Really, this crap is technically required of *every* X program,
1495 if you want it to be robust in the face of "2>&-".
1497 int fd0 = open ("/dev/null", O_RDWR);
1498 int fd1 = open ("/dev/null", O_RDWR);
1499 int fd2 = open ("/dev/null", O_RDWR);
1500 if (fd0 > 2) close (fd0);
1501 if (fd1 > 2) close (fd1);
1502 if (fd2 > 2) close (fd2);
1507 /* Processing ClientMessage events.
1511 static Bool error_handler_hit_p = False;
1514 ignore_all_errors_ehandler (Display *dpy, XErrorEvent *error)
1516 error_handler_hit_p = True;
1520 /* Sometimes some systems send us ClientMessage events with bogus atoms in
1521 them. We only look up the atom names for printing warning messages,
1522 so don't bomb out when it happens...
1525 XGetAtomName_safe (Display *dpy, Atom atom)
1528 XErrorHandler old_handler;
1529 if (!atom) return 0;
1532 error_handler_hit_p = False;
1533 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
1534 result = XGetAtomName (dpy, atom);
1536 XSetErrorHandler (old_handler);
1538 if (error_handler_hit_p) result = 0;
1545 sprintf (buf, "<<undefined atom 0x%04X>>", (unsigned int) atom);
1546 return strdup (buf);
1552 clientmessage_response (saver_info *si, Window w, Bool error,
1553 const char *stderr_msg,
1554 const char *protocol_msg)
1558 saver_preferences *p = &si->prefs;
1559 XErrorHandler old_handler;
1561 if (error || p->verbose_p)
1562 fprintf (stderr, "%s: %s\n", blurb(), stderr_msg);
1564 L = strlen(protocol_msg);
1565 proto = (char *) malloc (L + 2);
1566 proto[0] = (error ? '-' : '+');
1567 strcpy (proto+1, protocol_msg);
1570 /* Ignore all X errors while sending a response to a ClientMessage.
1571 Pretty much the only way we could get an error here is if the
1572 window we're trying to send the reply on has been deleted, in
1573 which case, the sender of the ClientMessage won't see our response
1576 XSync (si->dpy, False);
1577 error_handler_hit_p = False;
1578 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
1580 XChangeProperty (si->dpy, w, XA_SCREENSAVER_RESPONSE, XA_STRING, 8,
1581 PropModeReplace, (unsigned char *) proto, L);
1583 XSync (si->dpy, False);
1584 XSetErrorHandler (old_handler);
1585 XSync (si->dpy, False);
1592 bogus_clientmessage_warning (saver_info *si, XEvent *event)
1594 char *str = XGetAtomName_safe (si->dpy, event->xclient.message_type);
1595 Window w = event->xclient.window;
1600 for (screen = 0; screen < si->nscreens; screen++)
1601 if (w == si->screens[screen].screensaver_window)
1603 strcpy (wdesc, "xscreensaver");
1606 else if (w == RootWindow (si->dpy, screen))
1608 strcpy (wdesc, "root");
1614 XErrorHandler old_handler;
1616 XWindowAttributes xgwa;
1617 memset (&hint, 0, sizeof(hint));
1618 memset (&xgwa, 0, sizeof(xgwa));
1620 XSync (si->dpy, False);
1621 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
1622 XGetClassHint (si->dpy, w, &hint);
1623 XGetWindowAttributes (si->dpy, w, &xgwa);
1624 XSync (si->dpy, False);
1625 XSetErrorHandler (old_handler);
1626 XSync (si->dpy, False);
1628 screen = (xgwa.screen ? screen_number (xgwa.screen) : -1);
1630 sprintf (wdesc, "%.20s / %.20s",
1631 (hint.res_name ? hint.res_name : "(null)"),
1632 (hint.res_class ? hint.res_class : "(null)"));
1633 if (hint.res_name) XFree (hint.res_name);
1634 if (hint.res_class) XFree (hint.res_class);
1637 fprintf (stderr, "%s: %d: unrecognised ClientMessage \"%s\" received\n",
1638 blurb(), screen, (str ? str : "(null)"));
1639 fprintf (stderr, "%s: %d: for window 0x%lx (%s)\n",
1640 blurb(), screen, (unsigned long) w, wdesc);
1641 if (str) XFree (str);
1645 handle_clientmessage (saver_info *si, XEvent *event, Bool until_idle_p)
1647 saver_preferences *p = &si->prefs;
1649 Window window = event->xclient.window;
1651 /* Preferences might affect our handling of client messages. */
1652 maybe_reload_init_file (si);
1654 if (event->xclient.message_type != XA_SCREENSAVER ||
1655 event->xclient.format != 32)
1657 bogus_clientmessage_warning (si, event);
1661 type = event->xclient.data.l[0];
1662 if (type == XA_ACTIVATE)
1666 if (p->mode == DONT_BLANK)
1668 clientmessage_response(si, window, True,
1669 "ACTIVATE ClientMessage received in DONT_BLANK mode.",
1670 "screen blanking is currently disabled.");
1674 clientmessage_response(si, window, False,
1675 "ACTIVATE ClientMessage received.",
1677 si->selection_mode = 0;
1678 si->demoing_p = False;
1680 if (si->throttled_p && p->verbose_p)
1681 fprintf (stderr, "%s: unthrottled.\n", blurb());
1682 si->throttled_p = False;
1684 if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
1686 XForceScreenSaver (si->dpy, ScreenSaverActive);
1694 clientmessage_response(si, window, True,
1695 "ClientMessage ACTIVATE received while already active.",
1698 else if (type == XA_DEACTIVATE)
1702 if (si->throttled_p && p->verbose_p)
1703 fprintf (stderr, "%s: unthrottled.\n", blurb());
1704 si->throttled_p = False;
1706 clientmessage_response(si, window, False,
1707 "DEACTIVATE ClientMessage received.",
1709 if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
1711 XForceScreenSaver (si->dpy, ScreenSaverReset);
1719 clientmessage_response(si, window, False,
1720 "ClientMessage DEACTIVATE received while inactive: resetting idle timer.",
1721 "not active: idle timer reset.");
1724 else if (type == XA_CYCLE)
1728 clientmessage_response(si, window, False,
1729 "CYCLE ClientMessage received.",
1731 si->selection_mode = 0; /* 0 means randomize when its time. */
1732 si->demoing_p = False;
1734 if (si->throttled_p && p->verbose_p)
1735 fprintf (stderr, "%s: unthrottled.\n", blurb());
1736 si->throttled_p = False;
1739 XtRemoveTimeOut (si->cycle_id);
1741 cycle_timer ((XtPointer) si, 0);
1744 clientmessage_response(si, window, True,
1745 "ClientMessage CYCLE received while inactive.",
1748 else if (type == XA_NEXT || type == XA_PREV)
1750 clientmessage_response(si, window, False,
1752 ? "NEXT ClientMessage received."
1753 : "PREV ClientMessage received."),
1755 si->selection_mode = (type == XA_NEXT ? -1 : -2);
1756 si->demoing_p = False;
1758 if (si->throttled_p && p->verbose_p)
1759 fprintf (stderr, "%s: unthrottled.\n", blurb());
1760 si->throttled_p = False;
1765 XtRemoveTimeOut (si->cycle_id);
1767 cycle_timer ((XtPointer) si, 0);
1772 else if (type == XA_SELECT)
1776 long which = event->xclient.data.l[1];
1778 if (p->mode == DONT_BLANK)
1780 clientmessage_response(si, window, True,
1781 "SELECT ClientMessage received in DONT_BLANK mode.",
1782 "screen blanking is currently disabled.");
1786 sprintf (buf, "SELECT %ld ClientMessage received.", which);
1787 sprintf (buf2, "activating (%ld).", which);
1788 clientmessage_response (si, window, False, buf, buf2);
1790 if (which < 0) which = 0; /* 0 == "random" */
1791 si->selection_mode = which;
1792 si->demoing_p = False;
1794 if (si->throttled_p && p->verbose_p)
1795 fprintf (stderr, "%s: unthrottled.\n", blurb());
1796 si->throttled_p = False;
1801 XtRemoveTimeOut (si->cycle_id);
1803 cycle_timer ((XtPointer) si, 0);
1808 else if (type == XA_EXIT)
1810 /* Ignore EXIT message if the screen is locked. */
1811 if (until_idle_p || !si->locked_p)
1813 clientmessage_response (si, window, False,
1814 "EXIT ClientMessage received.",
1818 unblank_screen (si);
1819 kill_screenhack (si);
1820 XSync (si->dpy, False);
1822 saver_exit (si, 0, 0);
1825 clientmessage_response (si, window, True,
1826 "EXIT ClientMessage received while locked.",
1827 "screen is locked.");
1829 else if (type == XA_RESTART)
1831 /* The RESTART message works whether the screensaver is active or not,
1832 unless the screen is locked, in which case it doesn't work.
1834 if (until_idle_p || !si->locked_p)
1836 clientmessage_response (si, window, False,
1837 "RESTART ClientMessage received.",
1841 unblank_screen (si);
1842 kill_screenhack (si);
1843 XSync (si->dpy, False);
1846 restart_process (si); /* does not return */
1850 clientmessage_response (si, window, True,
1851 "RESTART ClientMessage received while locked.",
1852 "screen is locked.");
1854 else if (type == XA_DEMO)
1856 long arg = event->xclient.data.l[1];
1857 Bool demo_one_hack_p = (arg == 300);
1859 if (demo_one_hack_p)
1863 long which = event->xclient.data.l[2];
1866 sprintf (buf, "DEMO %ld ClientMessage received.", which);
1867 sprintf (buf2, "demoing (%ld).", which);
1868 clientmessage_response (si, window, False, buf, buf2);
1870 if (which < 0) which = 0; /* 0 == "random" */
1871 si->selection_mode = which;
1872 si->demoing_p = True;
1874 if (si->throttled_p && p->verbose_p)
1875 fprintf (stderr, "%s: unthrottled.\n", blurb());
1876 si->throttled_p = False;
1881 clientmessage_response (si, window, True,
1882 "DEMO ClientMessage received while active.",
1887 clientmessage_response (si, window, True,
1888 "obsolete form of DEMO ClientMessage.",
1889 "obsolete form of DEMO ClientMessage.");
1892 else if (type == XA_PREFS)
1894 clientmessage_response (si, window, True,
1895 "the PREFS client-message is obsolete.",
1896 "the PREFS client-message is obsolete.");
1898 else if (type == XA_LOCK)
1901 clientmessage_response (si, window, True,
1902 "not compiled with support for locking.",
1903 "locking not enabled.");
1904 #else /* !NO_LOCKING */
1905 if (p->mode == DONT_BLANK)
1906 clientmessage_response(si, window, True,
1907 "LOCK ClientMessage received in DONT_BLANK mode.",
1908 "screen blanking is currently disabled.");
1909 else if (si->locking_disabled_p)
1910 clientmessage_response (si, window, True,
1911 "LOCK ClientMessage received, but locking is disabled.",
1912 "locking not enabled.");
1913 else if (si->locked_p)
1914 clientmessage_response (si, window, True,
1915 "LOCK ClientMessage received while already locked.",
1920 char *response = (until_idle_p
1921 ? "activating and locking."
1923 sprintf (buf, "LOCK ClientMessage received; %s", response);
1924 clientmessage_response (si, window, False, buf, response);
1925 set_locked_p (si, True);
1926 si->selection_mode = 0;
1927 si->demoing_p = False;
1929 if (si->lock_id) /* we're doing it now, so lose the timeout */
1931 XtRemoveTimeOut (si->lock_id);
1937 if (si->using_mit_saver_extension ||
1938 si->using_sgi_saver_extension)
1940 XForceScreenSaver (si->dpy, ScreenSaverActive);
1949 #endif /* !NO_LOCKING */
1951 else if (type == XA_THROTTLE)
1953 /* The THROTTLE command is deprecated -- it predates the XDPMS
1954 extension. Instead of using -throttle, users should instead
1955 just power off the monitor (e.g., "xset dpms force off".)
1956 In a few minutes, xscreensaver will notice that the monitor
1957 is off, and cease running hacks.
1959 if (si->throttled_p)
1960 clientmessage_response (si, window, True,
1961 "THROTTLE ClientMessage received, but "
1962 "already throttled.",
1963 "already throttled.");
1967 char *response = "throttled.";
1968 si->throttled_p = True;
1969 si->selection_mode = 0;
1970 si->demoing_p = False;
1971 sprintf (buf, "THROTTLE ClientMessage received; %s", response);
1972 clientmessage_response (si, window, False, buf, response);
1977 XtRemoveTimeOut (si->cycle_id);
1979 cycle_timer ((XtPointer) si, 0);
1983 else if (type == XA_UNTHROTTLE)
1985 if (! si->throttled_p)
1986 clientmessage_response (si, window, True,
1987 "UNTHROTTLE ClientMessage received, but "
1993 char *response = "unthrottled.";
1994 si->throttled_p = False;
1995 si->selection_mode = 0;
1996 si->demoing_p = False;
1997 sprintf (buf, "UNTHROTTLE ClientMessage received; %s", response);
1998 clientmessage_response (si, window, False, buf, response);
2003 XtRemoveTimeOut (si->cycle_id);
2005 cycle_timer ((XtPointer) si, 0);
2013 str = XGetAtomName_safe (si->dpy, type);
2017 if (strlen (str) > 80)
2018 strcpy (str+70, "...");
2019 sprintf (buf, "unrecognised screensaver ClientMessage %s received.",
2026 "unrecognised screensaver ClientMessage 0x%x received.",
2027 (unsigned int) event->xclient.data.l[0]);
2030 clientmessage_response (si, window, True, buf, buf);
2036 /* Some random diagnostics printed in -verbose mode.
2040 analyze_display (saver_info *si)
2044 const char *name; const char *desc; Bool useful_p;
2047 { "SCREEN_SAVER", /* underscore */ "SGI Screen-Saver",
2048 # ifdef HAVE_SGI_SAVER_EXTENSION
2053 }, { "SCREEN-SAVER", /* dash */ "SGI Screen-Saver",
2054 # ifdef HAVE_SGI_SAVER_EXTENSION
2059 }, { "MIT-SCREEN-SAVER", "MIT Screen-Saver",
2060 # ifdef HAVE_MIT_SAVER_EXTENSION
2065 }, { "XIDLE", "XIdle",
2066 # ifdef HAVE_XIDLE_EXTENSION
2071 }, { "SGI-VIDEO-CONTROL", "SGI Video-Control",
2072 # ifdef HAVE_SGI_VC_EXTENSION
2077 }, { "READDISPLAY", "SGI Read-Display",
2078 # ifdef HAVE_READ_DISPLAY_EXTENSION
2083 }, { "MIT-SHM", "Shared Memory",
2084 # ifdef HAVE_XSHM_EXTENSION
2089 }, { "DOUBLE-BUFFER", "Double-Buffering",
2090 # ifdef HAVE_DOUBLE_BUFFER_EXTENSION
2095 }, { "DPMS", "Power Management",
2096 # ifdef HAVE_DPMS_EXTENSION
2107 }, { "XFree86-VidModeExtension", "XF86 Video-Mode",
2108 # ifdef HAVE_XF86VMODE
2113 }, { "XINERAMA", "Xinerama",
2114 # ifdef HAVE_XINERAMA
2119 }, { "RANDR", "Resize-and-Rotate",
2125 }, { "Apple-DRI", "Apple-DRI (XDarwin)",
2130 fprintf (stderr, "%s: running on display \"%s\" (%d %sscreen%s).\n",
2132 DisplayString(si->dpy),
2134 (si->xinerama_p ? "Xinerama " : ""),
2135 (si->nscreens == 1 ? "" : "s"));
2136 fprintf (stderr, "%s: vendor is %s, %d.\n", blurb(),
2137 ServerVendor(si->dpy), VendorRelease(si->dpy));
2139 fprintf (stderr, "%s: useful extensions:\n", blurb());
2140 for (i = 0; i < countof(exts); i++)
2142 int op = 0, event = 0, error = 0;
2145 if (!XQueryExtension (si->dpy, exts[i].name, &op, &event, &error))
2147 sprintf (buf, "%s: ", blurb());
2149 strcat (buf, exts[i].desc);
2150 if (!exts[i].useful_p)
2153 while (strlen (buf) < k) strcat (buf, " ");
2154 strcat (buf, "<-- not supported at compile time!");
2156 fprintf (stderr, "%s\n", buf);
2159 for (i = 0; i < si->nscreens; i++)
2161 saver_screen_info *ssi = &si->screens[i];
2162 unsigned long colormapped_depths = 0;
2163 unsigned long non_mapped_depths = 0;
2164 XVisualInfo vi_in, *vi_out;
2167 if (!ssi->real_screen_p) continue;
2169 vi_in.screen = ssi->real_screen_number;
2170 vi_out = XGetVisualInfo (si->dpy, VisualScreenMask, &vi_in, &out_count);
2171 if (!vi_out) continue;
2172 for (j = 0; j < out_count; j++)
2173 if (vi_out[j].class == PseudoColor)
2174 colormapped_depths |= (1 << vi_out[j].depth);
2176 non_mapped_depths |= (1 << vi_out[j].depth);
2177 XFree ((char *) vi_out);
2179 if (colormapped_depths)
2181 fprintf (stderr, "%s: screen %d colormapped depths:", blurb(),
2182 ssi->real_screen_number);
2183 for (j = 0; j < 32; j++)
2184 if (colormapped_depths & (1 << j))
2185 fprintf (stderr, " %d", j);
2186 fprintf (stderr, ".\n");
2188 if (non_mapped_depths)
2190 fprintf (stderr, "%s: screen %d non-colormapped depths:",
2191 blurb(), ssi->real_screen_number);
2192 for (j = 0; j < 32; j++)
2193 if (non_mapped_depths & (1 << j))
2194 fprintf (stderr, " %d", j);
2195 fprintf (stderr, ".\n");
2201 fprintf (stderr, "%s: Xinerama layout:\n", blurb());
2202 for (i = 0; i < si->nscreens; i++)
2204 saver_screen_info *ssi = &si->screens[i];
2205 fprintf (stderr, "%s: %c %d/%d: %dx%d+%d+%d\n",
2207 (ssi->real_screen_p ? '+' : ' '),
2208 ssi->number, ssi->real_screen_number,
2209 ssi->width, ssi->height, ssi->x, ssi->y);
2215 display_is_on_console_p (saver_info *si)
2217 Bool not_on_console = True;
2218 char *dpystr = DisplayString (si->dpy);
2219 char *tail = (char *) strchr (dpystr, ':');
2220 if (! tail || strncmp (tail, ":0", 2))
2221 not_on_console = True;
2224 char dpyname[255], localname[255];
2225 strncpy (dpyname, dpystr, tail-dpystr);
2226 dpyname [tail-dpystr] = 0;
2228 !strcmp(dpyname, "unix") ||
2229 !strcmp(dpyname, "localhost"))
2230 not_on_console = False;
2231 else if (gethostname (localname, sizeof (localname)))
2232 not_on_console = True; /* can't find hostname? */
2235 /* We have to call gethostbyname() on the result of gethostname()
2236 because the two aren't guarenteed to be the same name for the
2237 same host: on some losing systems, one is a FQDN and the other
2238 is not. Here in the wide wonderful world of Unix it's rocket
2239 science to obtain the local hostname in a portable fashion.
2241 And don't forget, gethostbyname() reuses the structure it
2242 returns, so we have to copy the fucker before calling it again.
2243 Thank you master, may I have another.
2245 struct hostent *h = gethostbyname (dpyname);
2247 not_on_console = True;
2252 strcpy (hn, h->h_name);
2253 l = gethostbyname (localname);
2254 not_on_console = (!l || !!(strcmp (l->h_name, hn)));
2258 return !not_on_console;
2262 /* Do a little bit of heap introspection...
2265 check_for_leaks (const char *where)
2268 static unsigned long last_brk = 0;
2269 int b = (unsigned long) sbrk(0);
2270 if (last_brk && last_brk < b)
2271 fprintf (stderr, "%s: %s: brk grew by %luK.\n",
2273 (((b - last_brk) + 1023) / 1024));
2275 #endif /* HAVE_SBRK */