1 /* xscreensaver, Copyright (c) 1991-2008 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 * (See comments in screens.c for more details about Xinerama/RANDR stuff.)
51 * While we are waiting for user activity, we also set up timers so that,
52 * after a certain amount of time has passed, we can start a different
53 * screenhack. We do this by killing the running child process with
54 * SIGTERM, and then starting a new one in the same way.
56 * If there was a real virtual root, meaning that we removed the __SWM_VROOT
57 * property from it, meaning we must (absolutely must) restore it before we
58 * exit, then we set up signal handlers for most signals (SIGINT, SIGTERM,
59 * etc.) that do this. Most Xlib and Xt routines are not reentrant, so it
60 * is not generally safe to call them from signal handlers; however, this
61 * program spends most of its time waiting, so the window of opportunity
62 * when code could be called reentrantly is fairly small; and also, the worst
63 * that could happen is that the call would fail. If we've gotten one of
64 * these signals, then we're on our way out anyway. If we didn't restore the
65 * __SWM_VROOT property, that would be very bad, so it's worth a shot. Note
66 * that this means that, if you're using a virtual-root window manager, you
67 * can really fuck up the world by killing this process with "kill -9".
69 * This program accepts ClientMessages of type SCREENSAVER; these messages
70 * may contain the atoms ACTIVATE, DEACTIVATE, etc, meaning to turn the
71 * screensaver on or off now, regardless of the idleness of the user,
72 * and a few other things. The included "xscreensaver-command" program
73 * sends these messsages.
75 * If we don't have the XIdle, MIT-SCREEN-SAVER, or SGI SCREEN_SAVER
76 * extensions, then we do the XAutoLock trick: notice every window that
77 * gets created, and wait 30 seconds or so until its creating process has
78 * settled down, and then select KeyPress events on those windows which
79 * already select for KeyPress events. It's important that we not select
80 * KeyPress on windows which don't select them, because that would
81 * interfere with event propagation. This will break if any program
82 * changes its event mask to contain KeyRelease or PointerMotion more than
83 * 30 seconds after creating the window, but such programs do not seem to
84 * occur in nature (I've never seen it happen in all these years.)
86 * The reason that we can't select KeyPresses on windows that don't have
87 * them already is that, when dispatching a KeyPress event, X finds the
88 * lowest (leafmost) window in the hierarchy on which *any* client selects
89 * for KeyPress, and sends the event to that window. This means that if a
90 * client had a window with subwindows, and expected to receive KeyPress
91 * events on the parent window instead of the subwindows, then that client
92 * would malfunction if some other client selected KeyPress events on the
93 * subwindows. It is an incredible misdesign that one client can make
94 * another client malfunction in this way.
96 * To detect mouse motion, we periodically wake up and poll the mouse
97 * position and button/modifier state, and notice when something has
98 * changed. We make this check every five seconds by default, and since the
99 * screensaver timeout has a granularity of one minute, this makes the
100 * chance of a false positive very small. We could detect mouse motion in
101 * the same way as keyboard activity, but that would suffer from the same
102 * "client changing event mask" problem that the KeyPress events hack does.
103 * I think polling is more reliable.
105 * On systems with /proc/interrupts (Linux) we poll that file and note when
106 * the interrupt counter numbers on the "keyboard" and "PS/2" lines change.
107 * (There is no reliable way, using /proc/interrupts, to detect non-PS/2
108 * mice, so it doesn't help for serial or USB mice.)
110 * None of this crap happens if we're using one of the extensions. Sadly,
111 * the XIdle extension hasn't been available for many years; the SGI
112 * extension only exists on SGIs; and the MIT extension, while widely
113 * deployed, is garbage in several ways.
115 * A third idle-detection option could be implemented (but is not): when
116 * running on the console display ($DISPLAY is `localhost`:0) and we're on a
117 * machine where /dev/tty and /dev/mouse have reasonable last-modification
118 * times, we could just stat() those. But the incremental benefit of
119 * implementing this is really small, so forget I said anything.
122 * - Have a second terminal handy.
123 * - Be careful where you set your breakpoints, you don't want this to
124 * stop under the debugger with the keyboard grabbed or the blackout
126 * - If you run your debugger under XEmacs, try M-ESC (x-grab-keyboard)
127 * to keep your emacs window alive even when xscreensaver has grabbed.
128 * - Go read the code related to `debug_p'.
129 * - You probably can't set breakpoints in functions that are called on
130 * the other side of a call to fork() -- if your subprocesses are
131 * dying with signal 5, Trace/BPT Trap, you're losing in this way.
132 * - If you aren't using a server extension, don't leave this stopped
133 * under the debugger for very long, or the X input buffer will get
134 * huge because of the keypress events it's selecting for. This can
135 * make your X server wedge with "no more input buffers."
137 * ======================================================================== */
145 #include <X11/Xlib.h>
147 #include <X11/Xlibint.h>
149 #include <X11/Xatom.h>
150 #include <X11/Intrinsic.h>
151 #include <X11/StringDefs.h>
152 #include <X11/Shell.h>
155 #include <sys/time.h>
156 #include <netdb.h> /* for gethostbyname() */
157 #include <sys/types.h>
161 # include <X11/Xmu/Error.h>
163 # include <Xmu/Error.h>
165 #else /* !HAVE_XMU */
167 #endif /* !HAVE_XMU */
169 #ifdef HAVE_MIT_SAVER_EXTENSION
170 #include <X11/extensions/scrnsaver.h>
171 #endif /* HAVE_MIT_SAVER_EXTENSION */
173 #ifdef HAVE_XIDLE_EXTENSION
174 # include <X11/extensions/xidle.h>
175 #endif /* HAVE_XIDLE_EXTENSION */
177 #ifdef HAVE_SGI_VC_EXTENSION
178 # include <X11/extensions/XSGIvc.h>
179 #endif /* HAVE_SGI_VC_EXTENSION */
181 #ifdef HAVE_READ_DISPLAY_EXTENSION
182 # include <X11/extensions/readdisplay.h>
183 #endif /* HAVE_READ_DISPLAY_EXTENSION */
185 #ifdef HAVE_XSHM_EXTENSION
186 # include <X11/extensions/XShm.h>
187 #endif /* HAVE_XSHM_EXTENSION */
189 #ifdef HAVE_DPMS_EXTENSION
190 # include <X11/extensions/dpms.h>
191 #endif /* HAVE_DPMS_EXTENSION */
194 #ifdef HAVE_DOUBLE_BUFFER_EXTENSION
195 # include <X11/extensions/Xdbe.h>
196 #endif /* HAVE_DOUBLE_BUFFER_EXTENSION */
198 #ifdef HAVE_XF86VMODE
199 # include <X11/extensions/xf86vmode.h>
200 #endif /* HAVE_XF86VMODE */
202 #ifdef HAVE_XF86MISCSETGRABKEYSSTATE
203 # include <X11/extensions/xf86misc.h>
204 #endif /* HAVE_XF86MISCSETGRABKEYSSTATE */
207 # include <X11/extensions/Xinerama.h>
208 #endif /* HAVE_XINERAMA */
211 # include <X11/extensions/Xrandr.h>
212 #endif /* HAVE_RANDR */
215 #include "xscreensaver.h"
217 #include "yarandom.h"
218 #include "resources.h"
223 saver_info *global_si_kludge = 0; /* I hate C so much... */
230 static Atom XA_SCREENSAVER_RESPONSE;
231 static Atom XA_ACTIVATE, XA_DEACTIVATE, XA_CYCLE, XA_NEXT, XA_PREV;
232 static Atom XA_RESTART, XA_SELECT;
233 static Atom XA_THROTTLE, XA_UNTHROTTLE;
234 Atom XA_DEMO, XA_PREFS, XA_EXIT, XA_LOCK, XA_BLANK;
237 static XrmOptionDescRec options [] = {
239 { "-verbose", ".verbose", XrmoptionNoArg, "on" },
240 { "-silent", ".verbose", XrmoptionNoArg, "off" },
242 /* xscreensaver-demo uses this one */
243 { "-nosplash", ".splash", XrmoptionNoArg, "off" },
244 { "-no-splash", ".splash", XrmoptionNoArg, "off" },
246 /* useful for debugging */
247 { "-no-capture-stderr", ".captureStderr", XrmoptionNoArg, "off" },
248 { "-log", ".logFile", XrmoptionSepArg, 0 },
252 __extension__ /* shut up about "string length is greater than the length
253 ISO C89 compilers are required to support" when including
257 static char *defaults[] = {
258 #include "XScreenSaver_ad.h"
263 ERROR! You must not include vroot.h in this file.
267 do_help (saver_info *si)
272 xscreensaver %s, copyright (c) 1991-2008 by Jamie Zawinski <jwz@jwz.org>\n\
274 All xscreensaver configuration is via the `~/.xscreensaver' file.\n\
275 Rather than editing that file by hand, just run `xscreensaver-demo':\n\
276 that program lets you configure the screen saver graphically,\n\
277 including timeouts, locking, and display modes.\n\
281 Just getting started? Try this:\n\
286 For updates, online manual, and FAQ, please see the web page:\n\
288 http://www.jwz.org/xscreensaver/\n\
300 time_t now = time ((time_t *) 0);
301 char *str = (char *) ctime (&now);
302 char *nl = (char *) strchr (str, '\n');
303 if (nl) *nl = 0; /* take off that dang newline */
307 static Bool blurb_timestamp_p = True; /* kludge */
312 if (!blurb_timestamp_p)
316 static char buf[255];
317 char *ct = timestring();
318 int n = strlen(progname);
320 strncpy(buf, progname, n);
323 strncpy(buf+n, ct+11, 8);
324 strcpy(buf+n+9, ": ");
331 saver_ehandler (Display *dpy, XErrorEvent *error)
333 saver_info *si = global_si_kludge; /* I hate C so much... */
337 if (!real_stderr) real_stderr = stderr;
339 fprintf (real_stderr, "\n"
340 "#######################################"
341 "#######################################\n\n"
342 "%s: X Error! PLEASE REPORT THIS BUG.\n",
345 for (i = 0; i < si->nscreens; i++)
347 saver_screen_info *ssi = &si->screens[i];
348 fprintf (real_stderr, "%s: screen %d/%d: 0x%x, 0x%x, 0x%x\n",
349 blurb(), ssi->real_screen_number, ssi->number,
350 (unsigned int) RootWindowOfScreen (si->screens[i].screen),
351 (unsigned int) si->screens[i].real_vroot,
352 (unsigned int) si->screens[i].screensaver_window);
355 fprintf (real_stderr, "\n"
356 "#######################################"
357 "#######################################\n\n");
359 fatal_p = XmuPrintDefaultErrorMessage (dpy, error, real_stderr);
361 fatal_p = True; /* The only time I've ever seen a supposedly nonfatal error,
362 it has been BadImplementation / Xlib sequence lost, which
363 are in truth pretty damned fatal.
366 fprintf (real_stderr, "\n");
369 fprintf (real_stderr, "%s: nonfatal error.\n\n", blurb());
372 if (si->prefs.xsync_p)
374 saver_exit (si, -1, "because of synchronous X Error");
379 __extension__ /* don't warn about "string length is greater than the
380 length ISO C89 compilers are required to support". */
382 fprintf (real_stderr,
383 "#######################################################################\n"
385 " If at all possible, please re-run xscreensaver with the command\n"
386 " line arguments `-sync -verbose -log log.txt', and reproduce this\n"
387 " bug. That will cause xscreensaver to dump a `core' file to the\n"
388 " current directory. Please include the stack trace from that core\n"
389 " file in your bug report. *DO NOT* mail the core file itself! That\n"
390 " won't work. A \"log.txt\" file will also be written. Please *do*\n"
391 " include the complete \"log.txt\" file with your bug report.\n"
393 " http://www.jwz.org/xscreensaver/bugs.html explains how to create\n"
394 " the most useful bug reports, and how to examine core files.\n"
396 " The more information you can provide, the better. But please\n"
397 " report this bug, regardless!\n"
399 "#######################################################################\n"
403 saver_exit (si, -1, 0);
411 /* This error handler is used only while the X connection is being set up;
412 after we've got a connection, we don't use this handler again. The only
413 reason for having this is so that we can present a more idiot-proof error
414 message than "cannot open display."
417 startup_ehandler (String name, String type, String class,
418 String defalt, /* one can't even spel properly
419 in this joke of a language */
420 String *av, Cardinal *ac)
424 saver_info *si = global_si_kludge; /* I hate C so much... */
425 XrmDatabase *db = XtAppGetErrorDatabase(si->app);
427 XtAppGetErrorDatabaseText(si->app, name, type, class, defalt,
428 fmt, sizeof(fmt)-1, *db);
430 fprintf (stderr, "%s: ", blurb());
432 memset (p, 0, sizeof(p));
433 if (*ac > countof (p)) *ac = countof (p);
434 memcpy ((char *) p, (char *) av, (*ac) * sizeof(*av));
435 fprintf (stderr, fmt, /* Did I mention that I hate C? */
436 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9]);
437 fprintf (stderr, "\n");
439 describe_uids (si, stderr);
441 if (si->orig_uid && !strncmp (si->orig_uid, "root/", 5))
443 fprintf (stderr, "\n"
444 "%s: This is probably because you're logging in as root. You\n"
445 " shouldn't log in as root: you should log in as a normal user,\n"
446 " and then `su' as needed. If you insist on logging in as\n"
447 " root, you will have to turn off X's security features before\n"
448 " xscreensaver will work.\n"
450 " Please read the manual and FAQ for more information:\n",
455 fprintf (stderr, "\n"
456 "%s: Errors at startup are usually authorization problems.\n"
457 " But you're not logging in as root (good!) so something\n"
458 " else must be wrong. Did you read the manual and the FAQ?\n",
462 fprintf (stderr, "\n"
463 " http://www.jwz.org/xscreensaver/faq.html\n"
464 " http://www.jwz.org/xscreensaver/man.html\n"
473 /* The zillions of initializations.
476 /* Set progname, version, etc. This is done very early.
479 set_version_string (saver_info *si, int *argc, char **argv)
481 progclass = "XScreenSaver";
483 /* progname is reset later, after we connect to X. */
484 progname = strrchr(argv[0], '/');
485 if (progname) progname++;
486 else progname = argv[0];
488 if (strlen(progname) > 100) /* keep it short. */
491 /* The X resource database blows up if argv[0] has a "." in it. */
494 while ((s = strchr (s, '.')))
498 si->version = (char *) malloc (5);
499 memcpy (si->version, screensaver_id + 17, 4);
504 /* Initializations that potentially take place as a priveleged user:
505 If the xscreensaver executable is setuid root, then these initializations
506 are run as root, before discarding privileges.
509 privileged_initialization (saver_info *si, int *argc, char **argv)
512 /* before hack_uid() for proper permissions */
513 lock_priv_init (*argc, argv, si->prefs.verbose_p);
514 #endif /* NO_LOCKING */
520 /* Figure out what locking mechanisms are supported.
523 lock_initialization (saver_info *si, int *argc, char **argv)
526 si->locking_disabled_p = True;
527 si->nolock_reason = "not compiled with locking support";
528 #else /* !NO_LOCKING */
530 /* Finish initializing locking, now that we're out of privileged code. */
531 if (! lock_init (*argc, argv, si->prefs.verbose_p))
533 si->locking_disabled_p = True;
534 si->nolock_reason = "error getting password";
537 /* If locking is currently enabled, but the environment indicates that
538 we have been launched as GDM's "Background" program, then disable
539 locking just in case.
541 if (!si->locking_disabled_p && getenv ("RUNNING_UNDER_GDM"))
543 si->locking_disabled_p = True;
544 si->nolock_reason = "running under GDM";
547 /* If the server is XDarwin (MacOS X) then disable locking.
548 (X grabs only affect X programs, so you can use Command-Tab
549 to bring any other Mac program to the front, e.g., Terminal.)
551 if (!si->locking_disabled_p)
553 int op = 0, event = 0, error = 0;
554 Bool macos_p = False;
557 /* Disable locking if *running* on Apple hardware, since we have no
558 reliable way to determine whether the server is running on MacOS.
559 Hopefully __APPLE__ means "MacOS" and not "Linux on Mac hardware"
560 but I'm not really sure about that.
566 /* This extension exists on the Apple X11 server, but not
567 on earlier versions of the XDarwin server. */
568 macos_p = XQueryExtension (si->dpy, "Apple-DRI", &op, &event, &error);
572 si->locking_disabled_p = True;
573 si->nolock_reason = "Cannot lock securely on MacOS X";
577 #endif /* NO_LOCKING */
581 /* Open the connection to the X server, and intern our Atoms.
584 connect_to_server (saver_info *si, int *argc, char **argv)
586 Widget toplevel_shell;
589 char *d = getenv ("DISPLAY");
592 char *ndpy = strdup("DISPLAY=:0.0");
593 /* if (si->prefs.verbose_p) */ /* sigh, too early to test this... */
595 "%s: warning: $DISPLAY is not set: defaulting to \"%s\".\n",
599 /* don't free (ndpy) -- some implementations of putenv (BSD 4.4,
600 glibc 2.0) copy the argument, but some (libc4,5, glibc 2.1.2)
601 do not. So we must leak it (and/or the previous setting). Yay.
604 #endif /* HAVE_PUTENV */
606 XSetErrorHandler (saver_ehandler);
608 XtAppSetErrorMsgHandler (si->app, startup_ehandler);
609 toplevel_shell = XtAppInitialize (&si->app, progclass,
610 options, XtNumber (options),
611 argc, argv, defaults, 0, 0);
612 XtAppSetErrorMsgHandler (si->app, 0);
614 si->dpy = XtDisplay (toplevel_shell);
615 si->prefs.db = XtDatabase (si->dpy);
616 XtGetApplicationNameAndClass (si->dpy, &progname, &progclass);
618 if(strlen(progname) > 100) /* keep it short. */
621 db = si->prefs.db; /* resources.c needs this */
623 XA_VROOT = XInternAtom (si->dpy, "__SWM_VROOT", False);
624 XA_SCREENSAVER = XInternAtom (si->dpy, "SCREENSAVER", False);
625 XA_SCREENSAVER_VERSION = XInternAtom (si->dpy, "_SCREENSAVER_VERSION",False);
626 XA_SCREENSAVER_ID = XInternAtom (si->dpy, "_SCREENSAVER_ID", False);
627 XA_SCREENSAVER_STATUS = XInternAtom (si->dpy, "_SCREENSAVER_STATUS", False);
628 XA_SCREENSAVER_RESPONSE = XInternAtom (si->dpy, "_SCREENSAVER_RESPONSE",
630 XA_XSETROOT_ID = XInternAtom (si->dpy, "_XSETROOT_ID", False);
631 XA_ESETROOT_PMAP_ID = XInternAtom (si->dpy, "ESETROOT_PMAP_ID", False);
632 XA_XROOTPMAP_ID = XInternAtom (si->dpy, "_XROOTPMAP_ID", False);
633 XA_ACTIVATE = XInternAtom (si->dpy, "ACTIVATE", False);
634 XA_DEACTIVATE = XInternAtom (si->dpy, "DEACTIVATE", False);
635 XA_RESTART = XInternAtom (si->dpy, "RESTART", False);
636 XA_CYCLE = XInternAtom (si->dpy, "CYCLE", False);
637 XA_NEXT = XInternAtom (si->dpy, "NEXT", False);
638 XA_PREV = XInternAtom (si->dpy, "PREV", False);
639 XA_SELECT = XInternAtom (si->dpy, "SELECT", False);
640 XA_EXIT = XInternAtom (si->dpy, "EXIT", False);
641 XA_DEMO = XInternAtom (si->dpy, "DEMO", False);
642 XA_PREFS = XInternAtom (si->dpy, "PREFS", False);
643 XA_LOCK = XInternAtom (si->dpy, "LOCK", False);
644 XA_BLANK = XInternAtom (si->dpy, "BLANK", False);
645 XA_THROTTLE = XInternAtom (si->dpy, "THROTTLE", False);
646 XA_UNTHROTTLE = XInternAtom (si->dpy, "UNTHROTTLE", False);
648 return toplevel_shell;
652 /* Handle the command-line arguments that were not handled for us by Xt.
653 Issue an error message and exit if there are unknown options.
656 process_command_line (saver_info *si, int *argc, char **argv)
659 for (i = 1; i < *argc; i++)
661 if (!strcmp (argv[i], "-debug"))
662 /* no resource for this one, out of paranoia. */
663 si->prefs.debug_p = True;
665 else if (!strcmp (argv[i], "-h") ||
666 !strcmp (argv[i], "-help") ||
667 !strcmp (argv[i], "--help"))
672 const char *s = argv[i];
673 fprintf (stderr, "%s: unknown option \"%s\". Try \"-help\".\n",
676 if (s[0] == '-' && s[1] == '-') s++;
677 if (!strcmp (s, "-activate") ||
678 !strcmp (s, "-deactivate") ||
679 !strcmp (s, "-cycle") ||
680 !strcmp (s, "-next") ||
681 !strcmp (s, "-prev") ||
682 !strcmp (s, "-exit") ||
683 !strcmp (s, "-restart") ||
684 !strcmp (s, "-demo") ||
685 !strcmp (s, "-prefs") ||
686 !strcmp (s, "-preferences") ||
687 !strcmp (s, "-lock") ||
688 !strcmp (s, "-version") ||
689 !strcmp (s, "-time"))
692 if (!strcmp (s, "-demo") || !strcmp (s, "-prefs"))
693 fprintf (stderr, "\n\
694 Perhaps you meant to run the `xscreensaver-demo' program instead?\n");
696 fprintf (stderr, "\n\
697 However, `%s' is an option to the `xscreensaver-command' program.\n", s);
700 The `xscreensaver' program is a daemon that runs in the background.\n\
701 You control a running xscreensaver process by sending it messages\n\
702 with `xscreensaver-demo' or `xscreensaver-command'.\n\
703 . See the man pages for details, or check the web page:\n\
704 http://www.jwz.org/xscreensaver/\n\n");
713 /* Print out the xscreensaver banner to the tty if applicable;
714 Issue any other warnings that are called for at this point.
717 print_banner (saver_info *si)
719 saver_preferences *p = &si->prefs;
721 /* This resource gets set some time before the others, so that we know
722 whether to print the banner (and so that the banner gets printed before
723 any resource-database-related error messages.)
725 p->verbose_p = (p->debug_p ||
726 get_boolean_resource (si->dpy, "verbose", "Boolean"));
728 /* Ditto, for the locking_disabled_p message. */
729 p->lock_p = get_boolean_resource (si->dpy, "lock", "Boolean");
733 "%s %s, copyright (c) 1991-2008 "
734 "by Jamie Zawinski <jwz@jwz.org>.\n",
735 progname, si->version);
738 fprintf (stderr, "\n"
739 "%s: Warning: running in DEBUG MODE. Be afraid.\n"
741 "\tNote that in debug mode, the xscreensaver window will only\n"
742 "\tcover the left half of the screen. (The idea is that you\n"
743 "\tcan still see debugging output in a shell, if you position\n"
744 "\tit on the right side of the screen.)\n"
746 "\tDebug mode is NOT SECURE. Do not run with -debug in\n"
747 "\tuntrusted environments.\n"
753 if (!si->uid_message || !*si->uid_message)
754 describe_uids (si, stderr);
757 if (si->orig_uid && *si->orig_uid)
758 fprintf (stderr, "%s: initial effective uid/gid was %s.\n",
759 blurb(), si->orig_uid);
760 fprintf (stderr, "%s: %s\n", blurb(), si->uid_message);
763 fprintf (stderr, "%s: in process %lu.\n", blurb(),
764 (unsigned long) getpid());
769 print_lock_failure_banner (saver_info *si)
771 saver_preferences *p = &si->prefs;
773 /* If locking was not able to be initalized for some reason, explain why.
774 (This has to be done after we've read the lock_p resource.)
776 if (si->locking_disabled_p)
779 fprintf (stderr, "%s: locking is disabled (%s).\n", blurb(),
781 if (strstr (si->nolock_reason, "passw"))
782 fprintf (stderr, "%s: does xscreensaver need to be setuid? "
783 "consult the manual.\n", blurb());
784 else if (strstr (si->nolock_reason, "running as "))
786 "%s: locking only works when xscreensaver is launched\n"
787 "\t by a normal, non-privileged user (e.g., not \"root\".)\n"
788 "\t See the manual for details.\n",
795 /* called from screens.c so that all the Xt crud is here. */
797 initialize_screen_root_widget (saver_screen_info *ssi)
799 saver_info *si = ssi->global;
800 if (ssi->toplevel_shell)
801 XtDestroyWidget (ssi->toplevel_shell);
802 ssi->toplevel_shell =
803 XtVaAppCreateShell (progname, progclass,
804 applicationShellWidgetClass,
806 XtNscreen, ssi->screen,
807 XtNvisual, ssi->current_visual,
808 XtNdepth, visual_depth (ssi->screen,
809 ssi->current_visual),
814 /* Examine all of the display's screens, and populate the `saver_screen_info'
815 structures. Make sure this is called after hack_environment() sets $PATH.
818 initialize_per_screen_info (saver_info *si, Widget toplevel_shell)
822 update_screen_layout (si);
824 /* Check to see whether fading is ever possible -- if any of the
825 screens on the display has a PseudoColor visual, then fading can
826 work (on at least some screens.) If no screen has a PseudoColor
827 visual, then don't bother ever trying to fade, because it will
828 just cause a delay without causing any visible effect.
830 for (i = 0; i < si->nscreens; i++)
832 saver_screen_info *ssi = &si->screens[i];
833 if (has_writable_cells (ssi->screen, ssi->current_visual) ||
834 get_visual (ssi->screen, "PseudoColor", True, False) ||
835 get_visual (ssi->screen, "GrayScale", True, False))
837 si->fading_possible_p = True;
842 #ifdef HAVE_XF86VMODE_GAMMA
843 si->fading_possible_p = True; /* if we can gamma fade, go for it */
848 /* If any server extensions have been requested, try and initialize them.
849 Issue warnings if requests can't be honored.
852 initialize_server_extensions (saver_info *si)
854 saver_preferences *p = &si->prefs;
856 Bool server_has_xidle_extension_p = False;
857 Bool server_has_sgi_saver_extension_p = False;
858 Bool server_has_mit_saver_extension_p = False;
859 Bool system_has_proc_interrupts_p = False;
860 const char *piwhy = 0;
862 si->using_xidle_extension = p->use_xidle_extension;
863 si->using_sgi_saver_extension = p->use_sgi_saver_extension;
864 si->using_mit_saver_extension = p->use_mit_saver_extension;
865 si->using_proc_interrupts = p->use_proc_interrupts;
867 #ifdef HAVE_XIDLE_EXTENSION
870 server_has_xidle_extension_p = XidleQueryExtension (si->dpy, &ev, &er);
873 #ifdef HAVE_SGI_SAVER_EXTENSION
874 server_has_sgi_saver_extension_p =
875 XScreenSaverQueryExtension (si->dpy,
876 &si->sgi_saver_ext_event_number,
877 &si->sgi_saver_ext_error_number);
879 #ifdef HAVE_MIT_SAVER_EXTENSION
880 server_has_mit_saver_extension_p =
881 XScreenSaverQueryExtension (si->dpy,
882 &si->mit_saver_ext_event_number,
883 &si->mit_saver_ext_error_number);
885 #ifdef HAVE_PROC_INTERRUPTS
886 system_has_proc_interrupts_p = query_proc_interrupts_available (si, &piwhy);
889 if (!server_has_xidle_extension_p)
890 si->using_xidle_extension = False;
891 else if (p->verbose_p)
893 if (si->using_xidle_extension)
894 fprintf (stderr, "%s: using XIDLE extension.\n", blurb());
896 fprintf (stderr, "%s: not using server's XIDLE extension.\n", blurb());
899 if (!server_has_sgi_saver_extension_p)
900 si->using_sgi_saver_extension = False;
901 else if (p->verbose_p)
903 if (si->using_sgi_saver_extension)
904 fprintf (stderr, "%s: using SGI SCREEN_SAVER extension.\n", blurb());
907 "%s: not using server's SGI SCREEN_SAVER extension.\n",
911 if (!server_has_mit_saver_extension_p)
912 si->using_mit_saver_extension = False;
913 else if (p->verbose_p)
915 if (si->using_mit_saver_extension)
916 fprintf (stderr, "%s: using lame MIT-SCREEN-SAVER extension.\n",
920 "%s: not using server's lame MIT-SCREEN-SAVER extension.\n",
925 if (XRRQueryExtension (si->dpy,
926 &si->randr_event_number, &si->randr_error_number))
928 int nscreens = ScreenCount (si->dpy); /* number of *real* screens */
932 fprintf (stderr, "%s: selecting RANDR events\n", blurb());
933 for (i = 0; i < nscreens; i++)
934 # ifdef RRScreenChangeNotifyMask /* randr.h 1.5, 2002/09/29 */
935 XRRSelectInput (si->dpy, RootWindow (si->dpy, i),
936 RRScreenChangeNotifyMask);
937 # else /* !RRScreenChangeNotifyMask */ /* Xrandr.h 1.4, 2001/06/07 */
938 XRRScreenChangeSelectInput (si->dpy, RootWindow (si->dpy, i), True);
939 # endif /* !RRScreenChangeNotifyMask */
941 # endif /* HAVE_RANDR */
943 if (!system_has_proc_interrupts_p)
945 si->using_proc_interrupts = False;
946 if (p->verbose_p && piwhy)
947 fprintf (stderr, "%s: not using /proc/interrupts: %s.\n", blurb(),
950 else if (p->verbose_p)
952 if (si->using_proc_interrupts)
954 "%s: consulting /proc/interrupts for keyboard activity.\n",
958 "%s: not consulting /proc/interrupts for keyboard activity.\n",
964 #ifdef DEBUG_MULTISCREEN
966 debug_multiscreen_timer (XtPointer closure, XtIntervalId *id)
968 saver_info *si = (saver_info *) closure;
969 saver_preferences *p = &si->prefs;
970 if (update_screen_layout (si))
974 fprintf (stderr, "%s: new layout:\n", blurb());
975 describe_monitor_layout (si);
977 resize_screensaver_window (si);
979 XtAppAddTimeOut (si->app, 1000*4, debug_multiscreen_timer, (XtPointer) si);
981 #endif /* DEBUG_MULTISCREEN */
984 /* For the case where we aren't using an server extensions, select user events
985 on all the existing windows, and launch timers to select events on
986 newly-created windows as well.
988 If a server extension is being used, this does nothing.
991 select_events (saver_info *si)
993 saver_preferences *p = &si->prefs;
996 if (si->using_xidle_extension ||
997 si->using_mit_saver_extension ||
998 si->using_sgi_saver_extension)
1001 if (p->initial_delay)
1005 fprintf (stderr, "%s: waiting for %d second%s...", blurb(),
1006 (int) p->initial_delay/1000,
1007 (p->initial_delay == 1000 ? "" : "s"));
1011 usleep (p->initial_delay);
1013 fprintf (stderr, " done.\n");
1018 fprintf (stderr, "%s: selecting events on extant windows...", blurb());
1023 /* Select events on the root windows of every screen. This also selects
1024 for window creation events, so that new subwindows will be noticed.
1026 for (i = 0; i < si->nscreens; i++)
1028 saver_screen_info *ssi = &si->screens[i];
1029 if (ssi->real_screen_p)
1030 start_notice_events_timer (si,
1031 RootWindowOfScreen (si->screens[i].screen), False);
1035 fprintf (stderr, " done.\n");
1037 # ifdef DEBUG_MULTISCREEN
1038 if (p->debug_p) debug_multiscreen_timer ((XtPointer) si, 0);
1044 maybe_reload_init_file (saver_info *si)
1046 saver_preferences *p = &si->prefs;
1047 if (init_file_changed_p (p))
1050 fprintf (stderr, "%s: file \"%s\" has changed, reloading.\n",
1051 blurb(), init_file_name());
1053 load_init_file (si->dpy, p);
1055 /* If a server extension is in use, and p->timeout has changed,
1056 we need to inform the server of the new timeout. */
1057 disable_builtin_screensaver (si, False);
1059 /* If the DPMS settings in the init file have changed,
1060 change the settings on the server to match. */
1061 sync_server_dpms_settings (si->dpy,
1062 (p->dpms_enabled_p &&
1063 p->mode != DONT_BLANK),
1064 p->dpms_standby / 1000,
1065 p->dpms_suspend / 1000,
1074 - wait until the user is idle;
1076 - wait until the user is active;
1077 - unblank the screen;
1082 main_loop (saver_info *si)
1084 saver_preferences *p = &si->prefs;
1090 Bool was_locked = False;
1093 fprintf (stderr, "%s: awaiting idleness.\n", blurb());
1095 check_for_leaks ("unblanked A");
1096 sleep_until_idle (si, True);
1097 check_for_leaks ("unblanked B");
1102 fprintf (stderr, "%s: demoing %d at %s.\n", blurb(),
1103 si->selection_mode, timestring());
1105 fprintf (stderr, "%s: blanking screen at %s.\n", blurb(),
1109 maybe_reload_init_file (si);
1111 if (p->mode == DONT_BLANK)
1114 fprintf (stderr, "%s: idle with blanking disabled at %s.\n",
1115 blurb(), timestring());
1117 /* Go around the loop and wait for the next bout of idleness,
1118 or for the init file to change, or for a remote command to
1119 come in, or something.
1121 But, if locked_p is true, go ahead. This can only happen
1122 if we're in "disabled" mode but a "lock" clientmessage came
1123 in: in that case, we should go ahead and blank/lock the screen.
1129 /* Since we're about to blank the screen, kill the de-race timer,
1130 if any. It might still be running if we have unblanked and then
1131 re-blanked in a short period (e.g., when using the "next" button
1132 in xscreensaver-demo.)
1137 fprintf (stderr, "%s: stopping de-race timer (%d remaining.)\n",
1138 blurb(), si->de_race_ticks);
1139 XtRemoveTimeOut (si->de_race_id);
1144 /* Now, try to blank.
1147 if (! blank_screen (si))
1149 /* We were unable to grab either the keyboard or mouse.
1150 This means we did not (and must not) blank the screen.
1151 If we were to blank the screen while some other program
1152 is holding both the mouse and keyboard grabbed, then
1153 we would never be able to un-blank it! We would never
1154 see any events, and the display would be wedged.
1156 So, just go around the loop again and wait for the
1157 next bout of idleness. (If the user remains idle, we
1158 will next try to blank the screen again in no more than
1161 Time retry = 60 * 1000;
1162 if (p->timeout < retry)
1168 "%s: DEBUG MODE: unable to grab -- BLANKING ANYWAY.\n",
1174 "%s: unable to grab keyboard or mouse! Blanking aborted.\n",
1177 schedule_wakeup_event (si, retry, p->debug_p);
1182 for (i = 0; i < si->nscreens; i++)
1183 kill_screenhack (&si->screens[i]);
1185 raise_window (si, True, True, False);
1186 if (si->throttled_p)
1187 fprintf (stderr, "%s: not launching hack (throttled.)\n", blurb());
1189 for (i = 0; i < si->nscreens; i++)
1190 spawn_screenhack (&si->screens[i]);
1192 /* Don't start the cycle timer in demo mode. */
1193 if (!si->demoing_p && p->cycle)
1194 si->cycle_id = XtAppAddTimeOut (si->app,
1196 /* see comment in cycle_timer() */
1204 /* Maybe start locking the screen.
1207 Time lock_timeout = p->lock_timeout;
1209 if (si->emergency_lock_p && p->lock_p && lock_timeout)
1211 int secs = p->lock_timeout / 1000;
1214 "%s: locking now, instead of waiting for %d:%02d:%02d.\n",
1216 (secs / (60 * 60)), ((secs / 60) % 60), (secs % 60));
1220 si->emergency_lock_p = False;
1222 if (!si->demoing_p && /* if not going into demo mode */
1223 p->lock_p && /* and locking is enabled */
1224 !si->locking_disabled_p && /* and locking is possible */
1225 lock_timeout == 0) /* and locking is not timer-deferred */
1226 set_locked_p (si, True); /* then lock right now. */
1228 /* locked_p might be true already because of the above, or because of
1229 the LOCK ClientMessage. But if not, and if we're supposed to lock
1230 after some time, set up a timer to do so.
1235 si->lock_id = XtAppAddTimeOut (si->app, lock_timeout,
1236 activate_lock_timer,
1239 #endif /* !NO_LOCKING */
1242 ok_to_unblank = True;
1245 check_for_leaks ("blanked A");
1246 sleep_until_idle (si, False); /* until not idle */
1247 check_for_leaks ("blanked B");
1249 maybe_reload_init_file (si);
1252 /* Maybe unlock the screen.
1256 saver_screen_info *ssi = si->default_screen;
1257 if (si->locking_disabled_p) abort ();
1260 si->dbox_up_p = True;
1261 for (i = 0; i < si->nscreens; i++)
1262 suspend_screenhack (&si->screens[i], True); /* suspend */
1263 XUndefineCursor (si->dpy, ssi->screensaver_window);
1265 ok_to_unblank = unlock_p (si);
1267 si->dbox_up_p = False;
1268 XDefineCursor (si->dpy, ssi->screensaver_window, ssi->cursor);
1269 for (i = 0; i < si->nscreens; i++)
1270 suspend_screenhack (&si->screens[i], False); /* resume */
1272 if (!ok_to_unblank &&
1273 !screenhack_running_p (si))
1275 /* If the lock dialog has been dismissed and we're not about to
1276 unlock the screen, and there is currently no hack running,
1277 then launch one. (There might be no hack running if DPMS
1278 had kicked in. But DPMS is off now, so bring back the hack)
1281 XtRemoveTimeOut (si->cycle_id);
1283 cycle_timer ((XtPointer) si, 0);
1286 #endif /* !NO_LOCKING */
1288 } while (!ok_to_unblank);
1292 fprintf (stderr, "%s: unblanking screen at %s.\n",
1293 blurb(), timestring ());
1295 /* Kill before unblanking, to stop drawing as soon as possible. */
1296 for (i = 0; i < si->nscreens; i++)
1297 kill_screenhack (&si->screens[i]);
1298 unblank_screen (si);
1300 set_locked_p (si, False);
1301 si->emergency_lock_p = False;
1303 si->selection_mode = 0;
1305 /* If we're throttled, and the user has explicitly unlocked the screen,
1306 then unthrottle. If we weren't locked, then don't unthrottle
1307 automatically, because someone might have just bumped the desk... */
1310 if (si->throttled_p && p->verbose_p)
1311 fprintf (stderr, "%s: unthrottled.\n", blurb());
1312 si->throttled_p = False;
1317 XtRemoveTimeOut (si->cycle_id);
1323 XtRemoveTimeOut (si->lock_id);
1327 /* Since we're unblanked now, break race conditions and make
1328 sure we stay that way (see comment in timers.c.) */
1329 if (! si->de_race_id)
1330 de_race_timer ((XtPointer) si, 0);
1334 static void analyze_display (saver_info *si);
1335 static void fix_fds (void);
1338 main (int argc, char **argv)
1342 saver_info *si = &the_si;
1343 saver_preferences *p = &si->prefs;
1344 struct passwd *spasswd;
1347 memset(si, 0, sizeof(*si));
1348 global_si_kludge = si; /* I hate C so much... */
1352 # undef ya_rand_init
1355 save_argv (argc, argv);
1356 set_version_string (si, &argc, argv);
1357 privileged_initialization (si, &argc, argv);
1358 hack_environment (si);
1360 spasswd = getpwuid(getuid());
1363 fprintf(stderr, "Could not figure out who the current user is!\n");
1367 si->user = strdup(spasswd->pw_name ? spasswd->pw_name : "(unknown)");
1370 si->unlock_cb = gui_auth_conv;
1371 si->auth_finished_cb = auth_finished_cb;
1372 # endif /* !NO_LOCKING */
1374 shell = connect_to_server (si, &argc, argv);
1375 process_command_line (si, &argc, argv);
1376 stderr_log_file (si);
1379 load_init_file(si->dpy, p); /* must be before initialize_per_screen_info() */
1380 blurb_timestamp_p = p->timestamp_p; /* kludge */
1381 initialize_per_screen_info (si, shell); /* also sets si->fading_possible_p */
1383 /* We can only issue this warning now. */
1384 if (p->verbose_p && !si->fading_possible_p && (p->fade_p || p->unfade_p))
1386 "%s: there are no PseudoColor or GrayScale visuals.\n"
1387 "%s: ignoring the request for fading/unfading.\n",
1390 for (i = 0; i < si->nscreens; i++)
1392 saver_screen_info *ssi = &si->screens[i];
1393 if (ssi->real_screen_p)
1394 if (ensure_no_screensaver_running (si->dpy, si->screens[i].screen))
1398 lock_initialization (si, &argc, argv);
1399 print_lock_failure_banner (si);
1401 if (p->xsync_p) XSynchronize (si->dpy, True);
1403 if (p->verbose_p) analyze_display (si);
1404 initialize_server_extensions (si);
1406 si->blank_time = time ((time_t) 0); /* must be before ..._window */
1407 initialize_screensaver_window (si);
1412 disable_builtin_screensaver (si, True);
1413 sync_server_dpms_settings (si->dpy,
1414 (p->dpms_enabled_p &&
1415 p->mode != DONT_BLANK),
1416 p->dpms_standby / 1000,
1417 p->dpms_suspend / 1000,
1421 initialize_stderr (si);
1422 handle_signals (si);
1424 make_splash_dialog (si);
1426 main_loop (si); /* doesn't return */
1433 /* Bad Things Happen if stdin, stdout, and stderr have been closed
1434 (as by the `sh incantation "xscreensaver >&- 2>&-"). When you do
1435 that, the X connection gets allocated to one of these fds, and
1436 then some random library writes to stderr, and random bits get
1437 stuffed down the X pipe, causing "Xlib: sequence lost" errors.
1438 So, we cause the first three file descriptors to be open to
1439 /dev/null if they aren't open to something else already. This
1440 must be done before any other files are opened (or the closing
1441 of that other file will again free up one of the "magic" first
1444 We do this by opening /dev/null three times, and then closing
1445 those fds, *unless* any of them got allocated as #0, #1, or #2,
1446 in which case we leave them open. Gag.
1448 Really, this crap is technically required of *every* X program,
1449 if you want it to be robust in the face of "2>&-".
1451 int fd0 = open ("/dev/null", O_RDWR);
1452 int fd1 = open ("/dev/null", O_RDWR);
1453 int fd2 = open ("/dev/null", O_RDWR);
1454 if (fd0 > 2) close (fd0);
1455 if (fd1 > 2) close (fd1);
1456 if (fd2 > 2) close (fd2);
1461 /* Processing ClientMessage events.
1465 static Bool error_handler_hit_p = False;
1468 ignore_all_errors_ehandler (Display *dpy, XErrorEvent *error)
1470 error_handler_hit_p = True;
1474 /* Sometimes some systems send us ClientMessage events with bogus atoms in
1475 them. We only look up the atom names for printing warning messages,
1476 so don't bomb out when it happens...
1479 XGetAtomName_safe (Display *dpy, Atom atom)
1482 XErrorHandler old_handler;
1483 if (!atom) return 0;
1486 error_handler_hit_p = False;
1487 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
1488 result = XGetAtomName (dpy, atom);
1490 XSetErrorHandler (old_handler);
1492 if (error_handler_hit_p) result = 0;
1499 sprintf (buf, "<<undefined atom 0x%04X>>", (unsigned int) atom);
1500 return strdup (buf);
1506 clientmessage_response (saver_info *si, Window w, Bool error,
1507 const char *stderr_msg,
1508 const char *protocol_msg)
1512 saver_preferences *p = &si->prefs;
1513 XErrorHandler old_handler;
1515 if (error || p->verbose_p)
1516 fprintf (stderr, "%s: %s\n", blurb(), stderr_msg);
1518 L = strlen(protocol_msg);
1519 proto = (char *) malloc (L + 2);
1520 proto[0] = (error ? '-' : '+');
1521 strcpy (proto+1, protocol_msg);
1524 /* Ignore all X errors while sending a response to a ClientMessage.
1525 Pretty much the only way we could get an error here is if the
1526 window we're trying to send the reply on has been deleted, in
1527 which case, the sender of the ClientMessage won't see our response
1530 XSync (si->dpy, False);
1531 error_handler_hit_p = False;
1532 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
1534 XChangeProperty (si->dpy, w, XA_SCREENSAVER_RESPONSE, XA_STRING, 8,
1535 PropModeReplace, (unsigned char *) proto, L);
1537 XSync (si->dpy, False);
1538 XSetErrorHandler (old_handler);
1539 XSync (si->dpy, False);
1546 bogus_clientmessage_warning (saver_info *si, XEvent *event)
1548 #if 0 /* Oh, fuck it. GNOME likes to spew random ClientMessages at us
1549 all the time. This is presumably indicative of an error in
1550 the sender of that ClientMessage: if we're getting it and
1551 ignoring it, then it's not reaching the intended recipient.
1552 But people complain to me about this all the time ("waaah!
1553 xscreensaver is printing to it's stderr and that gets my
1554 panties all in a bunch!") And I'm sick of hearing about it.
1555 So we'll just ignore these messages and let GNOME go right
1556 ahead and continue to stumble along in its malfunction.
1559 saver_preferences *p = &si->prefs;
1560 char *str = XGetAtomName_safe (si->dpy, event->xclient.message_type);
1561 Window w = event->xclient.window;
1564 Bool root_p = False;
1567 for (screen = 0; screen < si->nscreens; screen++)
1568 if (w == si->screens[screen].screensaver_window)
1570 strcpy (wdesc, "xscreensaver");
1573 else if (w == RootWindow (si->dpy, screen))
1575 strcpy (wdesc, "root");
1580 /* If this ClientMessage was sent to the real root window instead of to the
1581 xscreensaver window, then it might be intended for someone else who is
1582 listening on the root window (e.g., the window manager). So only print
1583 the warning if: we are in debug mode; or if the bogus message was
1584 actually sent to one of the xscreensaver-created windows.
1586 if (root_p && !p->debug_p)
1591 XErrorHandler old_handler;
1593 XWindowAttributes xgwa;
1594 memset (&hint, 0, sizeof(hint));
1595 memset (&xgwa, 0, sizeof(xgwa));
1597 XSync (si->dpy, False);
1598 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
1599 XGetClassHint (si->dpy, w, &hint);
1600 XGetWindowAttributes (si->dpy, w, &xgwa);
1601 XSync (si->dpy, False);
1602 XSetErrorHandler (old_handler);
1603 XSync (si->dpy, False);
1605 screen = (xgwa.screen ? screen_number (xgwa.screen) : -1);
1607 sprintf (wdesc, "%.20s / %.20s",
1608 (hint.res_name ? hint.res_name : "(null)"),
1609 (hint.res_class ? hint.res_class : "(null)"));
1610 if (hint.res_name) XFree (hint.res_name);
1611 if (hint.res_class) XFree (hint.res_class);
1614 fprintf (stderr, "%s: %d: unrecognised ClientMessage \"%s\" received\n",
1615 blurb(), screen, (str ? str : "(null)"));
1616 fprintf (stderr, "%s: %d: for window 0x%lx (%s)\n",
1617 blurb(), screen, (unsigned long) w, wdesc);
1618 if (str) XFree (str);
1625 handle_clientmessage (saver_info *si, XEvent *event, Bool until_idle_p)
1627 saver_preferences *p = &si->prefs;
1629 Window window = event->xclient.window;
1631 /* Preferences might affect our handling of client messages. */
1632 maybe_reload_init_file (si);
1634 if (event->xclient.message_type != XA_SCREENSAVER ||
1635 event->xclient.format != 32)
1637 bogus_clientmessage_warning (si, event);
1641 type = event->xclient.data.l[0];
1642 if (type == XA_ACTIVATE)
1646 if (p->mode == DONT_BLANK)
1648 clientmessage_response(si, window, True,
1649 "ACTIVATE ClientMessage received in DONT_BLANK mode.",
1650 "screen blanking is currently disabled.");
1654 clientmessage_response(si, window, False,
1655 "ACTIVATE ClientMessage received.",
1657 si->selection_mode = 0;
1658 si->demoing_p = False;
1660 if (si->throttled_p && p->verbose_p)
1661 fprintf (stderr, "%s: unthrottled.\n", blurb());
1662 si->throttled_p = False;
1664 if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
1666 XForceScreenSaver (si->dpy, ScreenSaverActive);
1674 clientmessage_response(si, window, True,
1675 "ClientMessage ACTIVATE received while already active.",
1678 else if (type == XA_DEACTIVATE)
1682 if (si->throttled_p && p->verbose_p)
1683 fprintf (stderr, "%s: unthrottled.\n", blurb());
1684 si->throttled_p = False;
1686 clientmessage_response(si, window, False,
1687 "DEACTIVATE ClientMessage received.",
1689 if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
1691 XForceScreenSaver (si->dpy, ScreenSaverReset);
1699 clientmessage_response(si, window, False,
1700 "ClientMessage DEACTIVATE received while inactive: resetting idle timer.",
1701 "not active: idle timer reset.");
1704 else if (type == XA_CYCLE)
1708 clientmessage_response(si, window, False,
1709 "CYCLE ClientMessage received.",
1711 si->selection_mode = 0; /* 0 means randomize when its time. */
1712 si->demoing_p = False;
1714 if (si->throttled_p && p->verbose_p)
1715 fprintf (stderr, "%s: unthrottled.\n", blurb());
1716 si->throttled_p = False;
1719 XtRemoveTimeOut (si->cycle_id);
1721 cycle_timer ((XtPointer) si, 0);
1724 clientmessage_response(si, window, True,
1725 "ClientMessage CYCLE received while inactive.",
1728 else if (type == XA_NEXT || type == XA_PREV)
1730 clientmessage_response(si, window, False,
1732 ? "NEXT ClientMessage received."
1733 : "PREV ClientMessage received."),
1735 si->selection_mode = (type == XA_NEXT ? -1 : -2);
1736 si->demoing_p = False;
1738 if (si->throttled_p && p->verbose_p)
1739 fprintf (stderr, "%s: unthrottled.\n", blurb());
1740 si->throttled_p = False;
1745 XtRemoveTimeOut (si->cycle_id);
1747 cycle_timer ((XtPointer) si, 0);
1752 else if (type == XA_SELECT)
1756 long which = event->xclient.data.l[1];
1758 if (p->mode == DONT_BLANK)
1760 clientmessage_response(si, window, True,
1761 "SELECT ClientMessage received in DONT_BLANK mode.",
1762 "screen blanking is currently disabled.");
1766 sprintf (buf, "SELECT %ld ClientMessage received.", which);
1767 sprintf (buf2, "activating (%ld).", which);
1768 clientmessage_response (si, window, False, buf, buf2);
1770 if (which < 0) which = 0; /* 0 == "random" */
1771 si->selection_mode = which;
1772 si->demoing_p = False;
1774 if (si->throttled_p && p->verbose_p)
1775 fprintf (stderr, "%s: unthrottled.\n", blurb());
1776 si->throttled_p = False;
1781 XtRemoveTimeOut (si->cycle_id);
1783 cycle_timer ((XtPointer) si, 0);
1788 else if (type == XA_EXIT)
1790 /* Ignore EXIT message if the screen is locked. */
1791 if (until_idle_p || !si->locked_p)
1793 clientmessage_response (si, window, False,
1794 "EXIT ClientMessage received.",
1799 for (i = 0; i < si->nscreens; i++)
1800 kill_screenhack (&si->screens[i]);
1801 unblank_screen (si);
1802 XSync (si->dpy, False);
1804 saver_exit (si, 0, 0);
1807 clientmessage_response (si, window, True,
1808 "EXIT ClientMessage received while locked.",
1809 "screen is locked.");
1811 else if (type == XA_RESTART)
1813 /* The RESTART message works whether the screensaver is active or not,
1814 unless the screen is locked, in which case it doesn't work.
1816 if (until_idle_p || !si->locked_p)
1818 clientmessage_response (si, window, False,
1819 "RESTART ClientMessage received.",
1824 for (i = 0; i < si->nscreens; i++)
1825 kill_screenhack (&si->screens[i]);
1826 unblank_screen (si);
1827 XSync (si->dpy, False);
1830 restart_process (si); /* does not return */
1834 clientmessage_response (si, window, True,
1835 "RESTART ClientMessage received while locked.",
1836 "screen is locked.");
1838 else if (type == XA_DEMO)
1840 long arg = event->xclient.data.l[1];
1841 Bool demo_one_hack_p = (arg == 5000);
1843 if (demo_one_hack_p)
1847 long which = event->xclient.data.l[2];
1850 sprintf (buf, "DEMO %ld ClientMessage received.", which);
1851 sprintf (buf2, "demoing (%ld).", which);
1852 clientmessage_response (si, window, False, buf, buf2);
1854 if (which < 0) which = 0; /* 0 == "random" */
1855 si->selection_mode = which;
1856 si->demoing_p = True;
1858 if (si->throttled_p && p->verbose_p)
1859 fprintf (stderr, "%s: unthrottled.\n", blurb());
1860 si->throttled_p = False;
1865 clientmessage_response (si, window, True,
1866 "DEMO ClientMessage received while active.",
1871 clientmessage_response (si, window, True,
1872 "obsolete form of DEMO ClientMessage.",
1873 "obsolete form of DEMO ClientMessage.");
1876 else if (type == XA_PREFS)
1878 clientmessage_response (si, window, True,
1879 "the PREFS client-message is obsolete.",
1880 "the PREFS client-message is obsolete.");
1882 else if (type == XA_LOCK)
1885 clientmessage_response (si, window, True,
1886 "not compiled with support for locking.",
1887 "locking not enabled.");
1888 #else /* !NO_LOCKING */
1889 if (si->locking_disabled_p)
1890 clientmessage_response (si, window, True,
1891 "LOCK ClientMessage received, but locking is disabled.",
1892 "locking not enabled.");
1893 else if (si->locked_p)
1894 clientmessage_response (si, window, True,
1895 "LOCK ClientMessage received while already locked.",
1900 char *response = (until_idle_p
1901 ? "activating and locking."
1903 sprintf (buf, "LOCK ClientMessage received; %s", response);
1904 clientmessage_response (si, window, False, buf, response);
1905 set_locked_p (si, True);
1906 si->selection_mode = 0;
1907 si->demoing_p = False;
1909 if (si->lock_id) /* we're doing it now, so lose the timeout */
1911 XtRemoveTimeOut (si->lock_id);
1917 if (si->using_mit_saver_extension ||
1918 si->using_sgi_saver_extension)
1920 XForceScreenSaver (si->dpy, ScreenSaverActive);
1929 #endif /* !NO_LOCKING */
1931 else if (type == XA_THROTTLE)
1933 /* The THROTTLE command is deprecated -- it predates the XDPMS
1934 extension. Instead of using -throttle, users should instead
1935 just power off the monitor (e.g., "xset dpms force off".)
1936 In a few minutes, xscreensaver will notice that the monitor
1937 is off, and cease running hacks.
1939 if (si->throttled_p)
1940 clientmessage_response (si, window, True,
1941 "THROTTLE ClientMessage received, but "
1942 "already throttled.",
1943 "already throttled.");
1947 char *response = "throttled.";
1948 si->throttled_p = True;
1949 si->selection_mode = 0;
1950 si->demoing_p = False;
1951 sprintf (buf, "THROTTLE ClientMessage received; %s", response);
1952 clientmessage_response (si, window, False, buf, response);
1957 XtRemoveTimeOut (si->cycle_id);
1959 cycle_timer ((XtPointer) si, 0);
1963 else if (type == XA_UNTHROTTLE)
1965 if (! si->throttled_p)
1966 clientmessage_response (si, window, True,
1967 "UNTHROTTLE ClientMessage received, but "
1973 char *response = "unthrottled.";
1974 si->throttled_p = False;
1975 si->selection_mode = 0;
1976 si->demoing_p = False;
1977 sprintf (buf, "UNTHROTTLE ClientMessage received; %s", response);
1978 clientmessage_response (si, window, False, buf, response);
1983 XtRemoveTimeOut (si->cycle_id);
1985 cycle_timer ((XtPointer) si, 0);
1993 str = XGetAtomName_safe (si->dpy, type);
1997 if (strlen (str) > 80)
1998 strcpy (str+70, "...");
1999 sprintf (buf, "unrecognised screensaver ClientMessage %s received.",
2006 "unrecognised screensaver ClientMessage 0x%x received.",
2007 (unsigned int) event->xclient.data.l[0]);
2010 clientmessage_response (si, window, True, buf, buf);
2016 /* Some random diagnostics printed in -verbose mode.
2020 analyze_display (saver_info *si)
2024 const char *name; const char *desc;
2026 Status (*version_fn) (Display *, int *majP, int *minP);
2029 { "SCREEN_SAVER", /* underscore */ "SGI Screen-Saver",
2030 # ifdef HAVE_SGI_SAVER_EXTENSION
2035 }, { "SCREEN-SAVER", /* dash */ "SGI Screen-Saver",
2036 # ifdef HAVE_SGI_SAVER_EXTENSION
2041 }, { "MIT-SCREEN-SAVER", "MIT Screen-Saver",
2042 # ifdef HAVE_MIT_SAVER_EXTENSION
2043 True, XScreenSaverQueryVersion
2047 }, { "XIDLE", "XIdle",
2048 # ifdef HAVE_XIDLE_EXTENSION
2053 }, { "SGI-VIDEO-CONTROL", "SGI Video-Control",
2054 # ifdef HAVE_SGI_VC_EXTENSION
2055 True, XSGIvcQueryVersion
2059 }, { "READDISPLAY", "SGI Read-Display",
2060 # ifdef HAVE_READ_DISPLAY_EXTENSION
2061 True, XReadDisplayQueryVersion
2065 }, { "MIT-SHM", "Shared Memory",
2066 # ifdef HAVE_XSHM_EXTENSION
2067 True, (Status (*) (Display*,int*,int*)) XShmQueryVersion /* 4 args */
2071 }, { "DOUBLE-BUFFER", "Double-Buffering",
2072 # ifdef HAVE_DOUBLE_BUFFER_EXTENSION
2073 True, XdbeQueryExtension
2077 }, { "DPMS", "Power Management",
2078 # ifdef HAVE_DPMS_EXTENSION
2079 True, DPMSGetVersion
2089 }, { "XFree86-VidModeExtension", "XF86 Video-Mode",
2090 # ifdef HAVE_XF86VMODE
2091 True, XF86VidModeQueryVersion
2095 }, { "XC-VidModeExtension", "XC Video-Mode",
2096 # ifdef HAVE_XF86VMODE
2097 True, XF86VidModeQueryVersion
2101 }, { "XFree86-MISC", "XF86 Misc",
2102 # ifdef HAVE_XF86MISCSETGRABKEYSSTATE
2103 True, XF86MiscQueryVersion
2107 }, { "XC-MISC", "XC Misc",
2108 # ifdef HAVE_XF86MISCSETGRABKEYSSTATE
2109 True, XF86MiscQueryVersion
2113 }, { "XINERAMA", "Xinerama",
2114 # ifdef HAVE_XINERAMA
2115 True, XineramaQueryVersion
2119 }, { "RANDR", "Resize-and-Rotate",
2121 True, XRRQueryVersion
2127 }, { "Apple-DRI", "Apple-DRI (XDarwin)",
2132 fprintf (stderr, "%s: running on display \"%s\"\n", blurb(),
2133 DisplayString(si->dpy));
2134 fprintf (stderr, "%s: vendor is %s, %d.\n", blurb(),
2135 ServerVendor(si->dpy), VendorRelease(si->dpy));
2137 fprintf (stderr, "%s: useful extensions:\n", blurb());
2138 for (i = 0; i < countof(exts); i++)
2140 int op = 0, event = 0, error = 0;
2142 int maj = 0, min = 0;
2143 int dummy1, dummy2, dummy3;
2146 /* Most of the extension version functions take 3 args,
2147 writing results into args 2 and 3, but some take more.
2148 We only ever care about the first two results, but we
2149 pass in three extra pointers just in case.
2151 Status (*version_fn_2) (Display*,int*,int*,int*,int*,int*) =
2152 (Status (*) (Display*,int*,int*,int*,int*,int*)) exts[i].version_fn;
2154 if (!XQueryExtension (si->dpy, exts[i].name, &op, &event, &error))
2156 sprintf (buf, "%s: ", blurb());
2158 strcat (buf, exts[i].desc);
2162 else if (version_fn_2 (si->dpy, &maj, &min, &dummy1, &dummy2, &dummy3))
2163 sprintf (buf+strlen(buf), " (%d.%d)", maj, min);
2165 strcat (buf, " (unavailable)");
2167 if (!exts[i].useful_p)
2168 strcat (buf, " (disabled at compile time)");
2169 fprintf (stderr, "%s\n", buf);
2172 for (i = 0; i < si->nscreens; i++)
2174 saver_screen_info *ssi = &si->screens[i];
2175 unsigned long colormapped_depths = 0;
2176 unsigned long non_mapped_depths = 0;
2177 XVisualInfo vi_in, *vi_out;
2180 if (!ssi->real_screen_p) continue;
2182 vi_in.screen = ssi->real_screen_number;
2183 vi_out = XGetVisualInfo (si->dpy, VisualScreenMask, &vi_in, &out_count);
2184 if (!vi_out) continue;
2185 for (j = 0; j < out_count; j++)
2186 if (vi_out[j].class == PseudoColor)
2187 colormapped_depths |= (1 << vi_out[j].depth);
2189 non_mapped_depths |= (1 << vi_out[j].depth);
2190 XFree ((char *) vi_out);
2192 if (colormapped_depths)
2194 fprintf (stderr, "%s: screen %d colormapped depths:", blurb(),
2195 ssi->real_screen_number);
2196 for (j = 0; j < 32; j++)
2197 if (colormapped_depths & (1 << j))
2198 fprintf (stderr, " %d", j);
2199 fprintf (stderr, ".\n");
2201 if (non_mapped_depths)
2203 fprintf (stderr, "%s: screen %d non-colormapped depths:",
2204 blurb(), ssi->real_screen_number);
2205 for (j = 0; j < 32; j++)
2206 if (non_mapped_depths & (1 << j))
2207 fprintf (stderr, " %d", j);
2208 fprintf (stderr, ".\n");
2212 describe_monitor_layout (si);
2217 display_is_on_console_p (saver_info *si)
2219 Bool not_on_console = True;
2220 char *dpystr = DisplayString (si->dpy);
2221 char *tail = (char *) strchr (dpystr, ':');
2222 if (! tail || strncmp (tail, ":0", 2))
2223 not_on_console = True;
2226 char dpyname[255], localname[255];
2227 strncpy (dpyname, dpystr, tail-dpystr);
2228 dpyname [tail-dpystr] = 0;
2230 !strcmp(dpyname, "unix") ||
2231 !strcmp(dpyname, "localhost"))
2232 not_on_console = False;
2233 else if (gethostname (localname, sizeof (localname)))
2234 not_on_console = True; /* can't find hostname? */
2235 else if (!strncmp (dpyname, "/tmp/launch-", 12)) /* MacOS X launchd */
2236 not_on_console = False;
2239 /* We have to call gethostbyname() on the result of gethostname()
2240 because the two aren't guarenteed to be the same name for the
2241 same host: on some losing systems, one is a FQDN and the other
2242 is not. Here in the wide wonderful world of Unix it's rocket
2243 science to obtain the local hostname in a portable fashion.
2245 And don't forget, gethostbyname() reuses the structure it
2246 returns, so we have to copy the fucker before calling it again.
2247 Thank you master, may I have another.
2249 struct hostent *h = gethostbyname (dpyname);
2251 not_on_console = True;
2256 strcpy (hn, h->h_name);
2257 l = gethostbyname (localname);
2258 not_on_console = (!l || !!(strcmp (l->h_name, hn)));
2262 return !not_on_console;
2266 /* Do a little bit of heap introspection...
2269 check_for_leaks (const char *where)
2271 #if defined(HAVE_SBRK) && defined(LEAK_PARANOIA)
2272 static unsigned long last_brk = 0;
2273 int b = (unsigned long) sbrk(0);
2274 if (last_brk && last_brk < b)
2275 fprintf (stderr, "%s: %s: brk grew by %luK.\n",
2277 (((b - last_brk) + 1023) / 1024));
2279 #endif /* HAVE_SBRK */