1 /* xscreensaver, Copyright (c) 1991-2006 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() */
155 #include <sys/types.h>
159 # include <X11/Xmu/Error.h>
161 # include <Xmu/Error.h>
163 #else /* !HAVE_XMU */
165 #endif /* !HAVE_XMU */
167 #ifdef HAVE_XIDLE_EXTENSION
168 # include <X11/extensions/xidle.h>
169 #endif /* HAVE_XIDLE_EXTENSION */
172 # include <X11/extensions/Xinerama.h>
173 #endif /* HAVE_XINERAMA */
175 #include "xscreensaver.h"
177 #include "yarandom.h"
178 #include "resources.h"
183 saver_info *global_si_kludge = 0; /* I hate C so much... */
190 static Atom XA_SCREENSAVER_RESPONSE;
191 static Atom XA_ACTIVATE, XA_DEACTIVATE, XA_CYCLE, XA_NEXT, XA_PREV;
192 static Atom XA_RESTART, XA_SELECT;
193 static Atom XA_THROTTLE, XA_UNTHROTTLE;
194 Atom XA_DEMO, XA_PREFS, XA_EXIT, XA_LOCK, XA_BLANK;
197 static XrmOptionDescRec options [] = {
199 { "-verbose", ".verbose", XrmoptionNoArg, "on" },
200 { "-silent", ".verbose", XrmoptionNoArg, "off" },
202 /* xscreensaver-demo uses this one */
203 { "-nosplash", ".splash", XrmoptionNoArg, "off" },
204 { "-no-splash", ".splash", XrmoptionNoArg, "off" },
206 /* useful for debugging */
207 { "-no-capture-stderr", ".captureStderr", XrmoptionNoArg, "off" },
211 __extension__ /* shut up about "string length is greater than the length
212 ISO C89 compilers are required to support" when including
216 static char *defaults[] = {
217 #include "XScreenSaver_ad.h"
222 ERROR! You must not include vroot.h in this file.
226 do_help (saver_info *si)
231 xscreensaver %s, copyright (c) 1991-2006 by Jamie Zawinski <jwz@jwz.org>\n\
233 All xscreensaver configuration is via the `~/.xscreensaver' file.\n\
234 Rather than editing that file by hand, just run `xscreensaver-demo':\n\
235 that program lets you configure the screen saver graphically,\n\
236 including timeouts, locking, and display modes.\n\
240 Just getting started? Try this:\n\
245 For updates, online manual, and FAQ, please see the web page:\n\
247 http://www.jwz.org/xscreensaver/\n\
259 time_t now = time ((time_t *) 0);
260 char *str = (char *) ctime (&now);
261 char *nl = (char *) strchr (str, '\n');
262 if (nl) *nl = 0; /* take off that dang newline */
266 static Bool blurb_timestamp_p = False; /* kludge */
271 if (!blurb_timestamp_p)
275 static char buf[255];
276 char *ct = timestring();
277 int n = strlen(progname);
279 strncpy(buf, progname, n);
282 strncpy(buf+n, ct+11, 8);
283 strcpy(buf+n+9, ": ");
290 saver_ehandler (Display *dpy, XErrorEvent *error)
292 saver_info *si = global_si_kludge; /* I hate C so much... */
296 if (!real_stderr) real_stderr = stderr;
298 fprintf (real_stderr, "\n"
299 "#######################################"
300 "#######################################\n\n"
301 "%s: X Error! PLEASE REPORT THIS BUG.\n",
304 for (i = 0; i < si->nscreens; i++)
306 saver_screen_info *ssi = &si->screens[i];
307 fprintf (real_stderr, "%s: screen %d/%d: 0x%x, 0x%x, 0x%x\n",
308 blurb(), ssi->real_screen_number, ssi->number,
309 (unsigned int) RootWindowOfScreen (si->screens[i].screen),
310 (unsigned int) si->screens[i].real_vroot,
311 (unsigned int) si->screens[i].screensaver_window);
314 fprintf (real_stderr, "\n"
315 "#######################################"
316 "#######################################\n\n");
318 fatal_p = XmuPrintDefaultErrorMessage (dpy, error, real_stderr);
320 fatal_p = True; /* The only time I've ever seen a supposedly nonfatal error,
321 it has been BadImplementation / Xlib sequence lost, which
322 are in truth pretty damned fatal.
325 fprintf (real_stderr, "\n");
328 fprintf (real_stderr, "%s: nonfatal error.\n\n", blurb());
331 if (si->prefs.xsync_p)
333 saver_exit (si, -1, "because of synchronous X Error");
337 fprintf (real_stderr,
338 "#######################################"
339 "#######################################\n\n");
340 fprintf (real_stderr,
341 " If at all possible, please re-run xscreensaver with the command\n"
342 " line arguments `-sync -verbose -no-capture', and reproduce this\n"
343 " bug. That will cause xscreensaver to dump a `core' file to the\n"
344 " current directory. Please include the stack trace from that core\n"
345 " file in your bug report. *DO NOT* mail the core file itself!\n"
346 " That won't work.\n");
347 fprintf (real_stderr,
349 " http://www.jwz.org/xscreensaver/bugs.html explains how to create\n"
350 " the most useful bug reports, and how to examine core files.\n"
352 " The more information you can provide, the better. But please\n"
353 " report this bug, regardless!\n"
355 fprintf (real_stderr,
356 "#######################################"
357 "#######################################\n\n");
359 saver_exit (si, -1, 0);
367 /* This error handler is used only while the X connection is being set up;
368 after we've got a connection, we don't use this handler again. The only
369 reason for having this is so that we can present a more idiot-proof error
370 message than "cannot open display."
373 startup_ehandler (String name, String type, String class,
374 String defalt, /* one can't even spel properly
375 in this joke of a language */
376 String *av, Cardinal *ac)
380 saver_info *si = global_si_kludge; /* I hate C so much... */
381 XrmDatabase *db = XtAppGetErrorDatabase(si->app);
383 XtAppGetErrorDatabaseText(si->app, name, type, class, defalt,
384 fmt, sizeof(fmt)-1, *db);
386 fprintf (stderr, "%s: ", blurb());
388 memset (p, 0, sizeof(p));
389 if (*ac > countof (p)) *ac = countof (p);
390 memcpy ((char *) p, (char *) av, (*ac) * sizeof(*av));
391 fprintf (stderr, fmt, /* Did I mention that I hate C? */
392 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9]);
393 fprintf (stderr, "\n");
395 describe_uids (si, stderr);
397 if (si->orig_uid && !strncmp (si->orig_uid, "root/", 5))
399 fprintf (stderr, "\n"
400 "%s: This is probably because you're logging in as root. You\n"
401 " shouldn't log in as root: you should log in as a normal user,\n"
402 " and then `su' as needed. If you insist on logging in as\n"
403 " root, you will have to turn off X's security features before\n"
404 " xscreensaver will work.\n"
406 " Please read the manual and FAQ for more information:\n",
411 fprintf (stderr, "\n"
412 "%s: Errors at startup are usually authorization problems.\n"
413 " But you're not logging in as root (good!) so something\n"
414 " else must be wrong. Did you read the manual and the FAQ?\n",
418 fprintf (stderr, "\n"
419 " http://www.jwz.org/xscreensaver/faq.html\n"
420 " http://www.jwz.org/xscreensaver/man.html\n"
429 /* The zillions of initializations.
432 /* Set progname, version, etc. This is done very early.
435 set_version_string (saver_info *si, int *argc, char **argv)
437 progclass = "XScreenSaver";
439 /* progname is reset later, after we connect to X. */
440 progname = strrchr(argv[0], '/');
441 if (progname) progname++;
442 else progname = argv[0];
444 if (strlen(progname) > 100) /* keep it short. */
447 /* The X resource database blows up if argv[0] has a "." in it. */
450 while ((s = strchr (s, '.')))
454 si->version = (char *) malloc (5);
455 memcpy (si->version, screensaver_id + 17, 4);
460 /* Initializations that potentially take place as a priveleged user:
461 If the xscreensaver executable is setuid root, then these initializations
462 are run as root, before discarding privileges.
465 privileged_initialization (saver_info *si, int *argc, char **argv)
468 /* before hack_uid() for proper permissions */
469 lock_priv_init (*argc, argv, si->prefs.verbose_p);
470 #endif /* NO_LOCKING */
476 /* Figure out what locking mechanisms are supported.
479 lock_initialization (saver_info *si, int *argc, char **argv)
482 si->locking_disabled_p = True;
483 si->nolock_reason = "not compiled with locking support";
484 #else /* !NO_LOCKING */
486 /* Finish initializing locking, now that we're out of privileged code. */
487 if (! lock_init (*argc, argv, si->prefs.verbose_p))
489 si->locking_disabled_p = True;
490 si->nolock_reason = "error getting password";
493 /* If locking is currently enabled, but the environment indicates that
494 we have been launched as GDM's "Background" program, then disable
495 locking just in case.
497 if (!si->locking_disabled_p && getenv ("RUNNING_UNDER_GDM"))
499 si->locking_disabled_p = True;
500 si->nolock_reason = "running under GDM";
503 /* If the server is XDarwin (MacOS X) then disable locking.
504 (X grabs only affect X programs, so you can use Command-Tab
505 to bring any other Mac program to the front, e.g., Terminal.)
507 if (!si->locking_disabled_p)
509 int op = 0, event = 0, error = 0;
510 Bool macos_p = False;
513 /* Disable locking if *running* on Apple hardware, since we have no
514 reliable way to determine whether the server is running on MacOS.
515 Hopefully __APPLE__ means "MacOS" and not "Linux on Mac hardware"
516 but I'm not really sure about that.
522 /* This extension exists on the Apple X11 server, but not
523 on earlier versions of the XDarwin server. */
524 macos_p = XQueryExtension (si->dpy, "Apple-DRI", &op, &event, &error);
528 si->locking_disabled_p = True;
529 si->nolock_reason = "Cannot lock securely on MacOS X";
533 #endif /* NO_LOCKING */
537 /* Open the connection to the X server, and intern our Atoms.
540 connect_to_server (saver_info *si, int *argc, char **argv)
542 Widget toplevel_shell;
545 char *d = getenv ("DISPLAY");
548 char *ndpy = strdup("DISPLAY=:0.0");
549 /* if (si->prefs.verbose_p) */ /* sigh, too early to test this... */
551 "%s: warning: $DISPLAY is not set: defaulting to \"%s\".\n",
555 /* don't free (ndpy) -- some implementations of putenv (BSD 4.4,
556 glibc 2.0) copy the argument, but some (libc4,5, glibc 2.1.2)
557 do not. So we must leak it (and/or the previous setting). Yay.
560 #endif /* HAVE_PUTENV */
562 XSetErrorHandler (saver_ehandler);
564 XtAppSetErrorMsgHandler (si->app, startup_ehandler);
565 toplevel_shell = XtAppInitialize (&si->app, progclass,
566 options, XtNumber (options),
567 argc, argv, defaults, 0, 0);
568 XtAppSetErrorMsgHandler (si->app, 0);
570 si->dpy = XtDisplay (toplevel_shell);
571 si->prefs.db = XtDatabase (si->dpy);
572 XtGetApplicationNameAndClass (si->dpy, &progname, &progclass);
574 if(strlen(progname) > 100) /* keep it short. */
577 db = si->prefs.db; /* resources.c needs this */
579 XA_VROOT = XInternAtom (si->dpy, "__SWM_VROOT", False);
580 XA_SCREENSAVER = XInternAtom (si->dpy, "SCREENSAVER", False);
581 XA_SCREENSAVER_VERSION = XInternAtom (si->dpy, "_SCREENSAVER_VERSION",False);
582 XA_SCREENSAVER_ID = XInternAtom (si->dpy, "_SCREENSAVER_ID", False);
583 XA_SCREENSAVER_STATUS = XInternAtom (si->dpy, "_SCREENSAVER_STATUS", False);
584 XA_SCREENSAVER_RESPONSE = XInternAtom (si->dpy, "_SCREENSAVER_RESPONSE",
586 XA_XSETROOT_ID = XInternAtom (si->dpy, "_XSETROOT_ID", False);
587 XA_ESETROOT_PMAP_ID = XInternAtom (si->dpy, "ESETROOT_PMAP_ID", False);
588 XA_XROOTPMAP_ID = XInternAtom (si->dpy, "_XROOTPMAP_ID", False);
589 XA_ACTIVATE = XInternAtom (si->dpy, "ACTIVATE", False);
590 XA_DEACTIVATE = XInternAtom (si->dpy, "DEACTIVATE", False);
591 XA_RESTART = XInternAtom (si->dpy, "RESTART", False);
592 XA_CYCLE = XInternAtom (si->dpy, "CYCLE", False);
593 XA_NEXT = XInternAtom (si->dpy, "NEXT", False);
594 XA_PREV = XInternAtom (si->dpy, "PREV", False);
595 XA_SELECT = XInternAtom (si->dpy, "SELECT", False);
596 XA_EXIT = XInternAtom (si->dpy, "EXIT", False);
597 XA_DEMO = XInternAtom (si->dpy, "DEMO", False);
598 XA_PREFS = XInternAtom (si->dpy, "PREFS", False);
599 XA_LOCK = XInternAtom (si->dpy, "LOCK", False);
600 XA_BLANK = XInternAtom (si->dpy, "BLANK", False);
601 XA_THROTTLE = XInternAtom (si->dpy, "THROTTLE", False);
602 XA_UNTHROTTLE = XInternAtom (si->dpy, "UNTHROTTLE", False);
604 return toplevel_shell;
608 /* Handle the command-line arguments that were not handled for us by Xt.
609 Issue an error message and exit if there are unknown options.
612 process_command_line (saver_info *si, int *argc, char **argv)
615 for (i = 1; i < *argc; i++)
617 if (!strcmp (argv[i], "-debug"))
618 /* no resource for this one, out of paranoia. */
619 si->prefs.debug_p = True;
621 else if (!strcmp (argv[i], "-h") ||
622 !strcmp (argv[i], "-help") ||
623 !strcmp (argv[i], "--help"))
628 const char *s = argv[i];
629 fprintf (stderr, "%s: unknown option \"%s\". Try \"-help\".\n",
632 if (s[0] == '-' && s[1] == '-') s++;
633 if (!strcmp (s, "-activate") ||
634 !strcmp (s, "-deactivate") ||
635 !strcmp (s, "-cycle") ||
636 !strcmp (s, "-next") ||
637 !strcmp (s, "-prev") ||
638 !strcmp (s, "-exit") ||
639 !strcmp (s, "-restart") ||
640 !strcmp (s, "-demo") ||
641 !strcmp (s, "-prefs") ||
642 !strcmp (s, "-preferences") ||
643 !strcmp (s, "-lock") ||
644 !strcmp (s, "-version") ||
645 !strcmp (s, "-time"))
648 if (!strcmp (s, "-demo") || !strcmp (s, "-prefs"))
649 fprintf (stderr, "\n\
650 Perhaps you meant to run the `xscreensaver-demo' program instead?\n");
652 fprintf (stderr, "\n\
653 However, `%s' is an option to the `xscreensaver-command' program.\n", s);
656 The `xscreensaver' program is a daemon that runs in the background.\n\
657 You control a running xscreensaver process by sending it messages\n\
658 with `xscreensaver-demo' or `xscreensaver-command'.\n\
659 . See the man pages for details, or check the web page:\n\
660 http://www.jwz.org/xscreensaver/\n\n");
662 /* Since version 1.21 renamed the "-lock" option to "-lock-mode",
663 suggest that explicitly. */
664 if (!strcmp (s, "-lock"))
666 Or perhaps you meant either the \"-lock-mode\" or the\n\
667 \"-lock-timeout <minutes>\" options to xscreensaver?\n\n");
676 /* Print out the xscreensaver banner to the tty if applicable;
677 Issue any other warnings that are called for at this point.
680 print_banner (saver_info *si)
682 saver_preferences *p = &si->prefs;
684 /* This resource gets set some time before the others, so that we know
685 whether to print the banner (and so that the banner gets printed before
686 any resource-database-related error messages.)
688 p->verbose_p = (p->debug_p ||
689 get_boolean_resource (si->dpy, "verbose", "Boolean"));
691 /* Ditto, for the locking_disabled_p message. */
692 p->lock_p = get_boolean_resource (si->dpy, "lock", "Boolean");
696 "%s %s, copyright (c) 1991-2006 "
697 "by Jamie Zawinski <jwz@jwz.org>.\n",
698 progname, si->version);
701 fprintf (stderr, "\n"
702 "%s: Warning: running in DEBUG MODE. Be afraid.\n"
704 "\tNote that in debug mode, the xscreensaver window will only\n"
705 "\tcover the left half of the screen. (The idea is that you\n"
706 "\tcan still see debugging output in a shell, if you position\n"
707 "\tit on the right side of the screen.)\n"
709 "\tDebug mode is NOT SECURE. Do not run with -debug in\n"
710 "\tuntrusted environments.\n"
716 if (!si->uid_message || !*si->uid_message)
717 describe_uids (si, stderr);
720 if (si->orig_uid && *si->orig_uid)
721 fprintf (stderr, "%s: initial effective uid/gid was %s.\n",
722 blurb(), si->orig_uid);
723 fprintf (stderr, "%s: %s\n", blurb(), si->uid_message);
726 fprintf (stderr, "%s: in process %lu.\n", blurb(),
727 (unsigned long) getpid());
732 print_lock_failure_banner (saver_info *si)
734 saver_preferences *p = &si->prefs;
736 /* If locking was not able to be initalized for some reason, explain why.
737 (This has to be done after we've read the lock_p resource.)
739 if (si->locking_disabled_p)
742 fprintf (stderr, "%s: locking is disabled (%s).\n", blurb(),
744 if (strstr (si->nolock_reason, "passw"))
745 fprintf (stderr, "%s: does xscreensaver need to be setuid? "
746 "consult the manual.\n", blurb());
747 else if (strstr (si->nolock_reason, "running as "))
749 "%s: locking only works when xscreensaver is launched\n"
750 "\t by a normal, non-privileged user (e.g., not \"root\".)\n"
751 "\t See the manual for details.\n",
758 /* Examine all of the display's screens, and populate the `saver_screen_info'
759 structures. Make sure this is called after hack_environment() sets $PATH.
762 initialize_per_screen_info (saver_info *si, Widget toplevel_shell)
764 Bool found_any_writable_cells = False;
767 # ifdef HAVE_XINERAMA
770 si->xinerama_p = (XineramaQueryExtension (si->dpy, &event, &error) &&
771 XineramaIsActive (si->dpy));
774 if (si->xinerama_p && ScreenCount (si->dpy) != 1)
776 si->xinerama_p = False;
777 if (si->prefs.verbose_p)
779 "%s: Xinerama AND %d screens? Disabling Xinerama support!\n",
780 blurb(), ScreenCount(si->dpy));
785 XineramaScreenInfo *xsi = XineramaQueryScreens (si->dpy, &si->nscreens);
787 si->xinerama_p = False;
790 si->screens = (saver_screen_info *)
791 calloc(sizeof(saver_screen_info), si->nscreens);
792 for (i = 0; i < si->nscreens; i++)
794 si->screens[i].x = xsi[i].x_org;
795 si->screens[i].y = xsi[i].y_org;
796 si->screens[i].width = xsi[i].width;
797 si->screens[i].height = xsi[i].height;
801 si->default_screen = &si->screens[0];
802 si->default_screen->real_screen_p = True;
804 # endif /* !HAVE_XINERAMA */
808 si->nscreens = ScreenCount(si->dpy);
809 si->screens = (saver_screen_info *)
810 calloc(sizeof(saver_screen_info), si->nscreens);
811 si->default_screen = &si->screens[DefaultScreen(si->dpy)];
813 for (i = 0; i < si->nscreens; i++)
815 saver_screen_info *ssi = &si->screens[i];
816 ssi->width = DisplayWidth (si->dpy, i);
817 ssi->height = DisplayHeight (si->dpy, i);
818 ssi->real_screen_p = True;
819 ssi->real_screen_number = i;
825 /* In "quad mode", we use the Xinerama code to pretend that there are 4
826 screens for every physical screen, and run four times as many hacks...
828 if (si->prefs.quad_p)
830 int ns2 = si->nscreens * 4;
831 saver_screen_info *ssi2 = (saver_screen_info *)
832 calloc(sizeof(saver_screen_info), ns2);
834 for (i = 0; i < si->nscreens; i++)
836 saver_screen_info *old = &si->screens[i];
838 if (si->prefs.debug_p) old->width = old->width / 2;
845 ssi2[i*4 ].width /= 2;
846 ssi2[i*4 ].height /= 2;
848 ssi2[i*4+1].x += ssi2[i*4 ].width;
849 ssi2[i*4+1].width -= ssi2[i*4 ].width;
850 ssi2[i*4+1].height /= 2;
852 ssi2[i*4+2].y += ssi2[i*4 ].height;
853 ssi2[i*4+2].width /= 2;
854 ssi2[i*4+2].height -= ssi2[i*4 ].height;
856 ssi2[i*4+3].x += ssi2[i*4+2].width;
857 ssi2[i*4+3].y += ssi2[i*4+2].height;
858 ssi2[i*4+3].width -= ssi2[i*4+2].width;
859 ssi2[i*4+3].height -= ssi2[i*4+2].height;
861 ssi2[i*4+1].real_screen_p = False;
862 ssi2[i*4+2].real_screen_p = False;
863 ssi2[i*4+3].real_screen_p = False;
869 si->default_screen = &si->screens[DefaultScreen(si->dpy) * 4];
870 si->xinerama_p = True;
872 # endif /* QUAD_MODE */
874 /* finish initializing the screens.
876 for (i = 0; i < si->nscreens; i++)
878 saver_screen_info *ssi = &si->screens[i];
882 ssi->screen = ScreenOfDisplay (si->dpy, ssi->real_screen_number);
883 ssi->poll_mouse_last_root_x = -1;
884 ssi->poll_mouse_last_root_y = -1;
888 ssi->width = WidthOfScreen (ssi->screen);
889 ssi->height = HeightOfScreen (ssi->screen);
892 /* Note: we can't use the resource ".visual" because Xt is SO FUCKED. */
893 ssi->default_visual =
894 get_visual_resource (ssi->screen, "visualID", "VisualID", False);
896 ssi->current_visual = ssi->default_visual;
897 ssi->current_depth = visual_depth (ssi->screen, ssi->current_visual);
899 /* Execute a subprocess to find the GL visual. */
900 ssi->best_gl_visual = get_best_gl_visual (ssi);
902 if (ssi == si->default_screen)
903 /* Since this is the default screen, use the one already created. */
904 ssi->toplevel_shell = toplevel_shell;
906 /* Otherwise, each screen must have its own unmapped root widget. */
907 ssi->toplevel_shell =
908 XtVaAppCreateShell (progname, progclass, applicationShellWidgetClass,
910 XtNscreen, ssi->screen,
911 XtNvisual, ssi->current_visual,
912 XtNdepth, visual_depth (ssi->screen,
913 ssi->current_visual),
916 if (! found_any_writable_cells)
918 /* Check to see whether fading is ever possible -- if any of the
919 screens on the display has a PseudoColor visual, then fading can
920 work (on at least some screens.) If no screen has a PseudoColor
921 visual, then don't bother ever trying to fade, because it will
922 just cause a delay without causing any visible effect.
924 if (has_writable_cells (ssi->screen, ssi->current_visual) ||
925 get_visual (ssi->screen, "PseudoColor", True, False) ||
926 get_visual (ssi->screen, "GrayScale", True, False))
927 found_any_writable_cells = True;
931 si->fading_possible_p = found_any_writable_cells;
933 #ifdef HAVE_XF86VMODE_GAMMA
934 si->fading_possible_p = True; /* if we can gamma fade, go for it */
939 /* If any server extensions have been requested, try and initialize them.
940 Issue warnings if requests can't be honored.
943 initialize_server_extensions (saver_info *si)
945 saver_preferences *p = &si->prefs;
947 Bool server_has_xidle_extension_p = False;
948 Bool server_has_sgi_saver_extension_p = False;
949 Bool server_has_mit_saver_extension_p = False;
950 Bool system_has_proc_interrupts_p = False;
951 const char *piwhy = 0;
953 si->using_xidle_extension = p->use_xidle_extension;
954 si->using_sgi_saver_extension = p->use_sgi_saver_extension;
955 si->using_mit_saver_extension = p->use_mit_saver_extension;
956 si->using_proc_interrupts = p->use_proc_interrupts;
958 #ifdef HAVE_XIDLE_EXTENSION
959 server_has_xidle_extension_p = query_xidle_extension (si);
961 #ifdef HAVE_SGI_SAVER_EXTENSION
962 server_has_sgi_saver_extension_p = query_sgi_saver_extension (si);
964 #ifdef HAVE_MIT_SAVER_EXTENSION
965 server_has_mit_saver_extension_p = query_mit_saver_extension (si);
967 #ifdef HAVE_PROC_INTERRUPTS
968 system_has_proc_interrupts_p = query_proc_interrupts_available (si, &piwhy);
971 if (!server_has_xidle_extension_p)
972 si->using_xidle_extension = False;
973 else if (p->verbose_p)
975 if (si->using_xidle_extension)
976 fprintf (stderr, "%s: using XIDLE extension.\n", blurb());
978 fprintf (stderr, "%s: not using server's XIDLE extension.\n", blurb());
981 if (!server_has_sgi_saver_extension_p)
982 si->using_sgi_saver_extension = False;
983 else if (p->verbose_p)
985 if (si->using_sgi_saver_extension)
986 fprintf (stderr, "%s: using SGI SCREEN_SAVER extension.\n", blurb());
989 "%s: not using server's SGI SCREEN_SAVER extension.\n",
993 if (!server_has_mit_saver_extension_p)
994 si->using_mit_saver_extension = False;
995 else if (p->verbose_p)
997 if (si->using_mit_saver_extension)
998 fprintf (stderr, "%s: using lame MIT-SCREEN-SAVER extension.\n",
1002 "%s: not using server's lame MIT-SCREEN-SAVER extension.\n",
1006 /* These are incompatible (or at least, our support for them is...) */
1007 if (si->xinerama_p && si->using_mit_saver_extension)
1009 si->using_mit_saver_extension = False;
1011 fprintf (stderr, "%s: Xinerama in use: disabling MIT-SCREEN-SAVER.\n",
1016 query_randr_extension (si);
1019 if (!system_has_proc_interrupts_p)
1021 si->using_proc_interrupts = False;
1022 if (p->verbose_p && piwhy)
1023 fprintf (stderr, "%s: not using /proc/interrupts: %s.\n", blurb(),
1026 else if (p->verbose_p)
1028 if (si->using_proc_interrupts)
1030 "%s: consulting /proc/interrupts for keyboard activity.\n",
1034 "%s: not consulting /proc/interrupts for keyboard activity.\n",
1040 /* For the case where we aren't using an server extensions, select user events
1041 on all the existing windows, and launch timers to select events on
1042 newly-created windows as well.
1044 If a server extension is being used, this does nothing.
1047 select_events (saver_info *si)
1049 saver_preferences *p = &si->prefs;
1052 if (si->using_xidle_extension ||
1053 si->using_mit_saver_extension ||
1054 si->using_sgi_saver_extension)
1057 if (p->initial_delay)
1061 fprintf (stderr, "%s: waiting for %d second%s...", blurb(),
1062 (int) p->initial_delay/1000,
1063 (p->initial_delay == 1000 ? "" : "s"));
1067 usleep (p->initial_delay);
1069 fprintf (stderr, " done.\n");
1074 fprintf (stderr, "%s: selecting events on extant windows...", blurb());
1079 /* Select events on the root windows of every screen. This also selects
1080 for window creation events, so that new subwindows will be noticed.
1082 for (i = 0; i < si->nscreens; i++)
1084 saver_screen_info *ssi = &si->screens[i];
1085 if (ssi->real_screen_p)
1086 start_notice_events_timer (si,
1087 RootWindowOfScreen (si->screens[i].screen), False);
1091 fprintf (stderr, " done.\n");
1096 maybe_reload_init_file (saver_info *si)
1098 saver_preferences *p = &si->prefs;
1099 if (init_file_changed_p (p))
1102 fprintf (stderr, "%s: file \"%s\" has changed, reloading.\n",
1103 blurb(), init_file_name());
1105 load_init_file (si->dpy, p);
1107 /* If a server extension is in use, and p->timeout has changed,
1108 we need to inform the server of the new timeout. */
1109 disable_builtin_screensaver (si, False);
1111 /* If the DPMS settings in the init file have changed,
1112 change the settings on the server to match. */
1113 sync_server_dpms_settings (si->dpy,
1114 (p->dpms_enabled_p &&
1115 p->mode != DONT_BLANK),
1116 p->dpms_standby / 1000,
1117 p->dpms_suspend / 1000,
1126 - wait until the user is idle;
1128 - wait until the user is active;
1129 - unblank the screen;
1134 main_loop (saver_info *si)
1136 saver_preferences *p = &si->prefs;
1141 Bool was_locked = False;
1144 fprintf (stderr, "%s: awaiting idleness.\n", blurb());
1146 check_for_leaks ("unblanked A");
1147 sleep_until_idle (si, True);
1148 check_for_leaks ("unblanked B");
1153 fprintf (stderr, "%s: demoing %d at %s.\n", blurb(),
1154 si->selection_mode, timestring());
1156 fprintf (stderr, "%s: blanking screen at %s.\n", blurb(),
1160 maybe_reload_init_file (si);
1162 if (p->mode == DONT_BLANK)
1165 fprintf (stderr, "%s: idle with blanking disabled at %s.\n",
1166 blurb(), timestring());
1168 /* Go around the loop and wait for the next bout of idleness,
1169 or for the init file to change, or for a remote command to
1170 come in, or something.
1172 But, if locked_p is true, go ahead. This can only happen
1173 if we're in "disabled" mode but a "lock" clientmessage came
1174 in: in that case, we should go ahead and blank/lock the screen.
1180 /* Since we're about to blank the screen, kill the de-race timer,
1181 if any. It might still be running if we have unblanked and then
1182 re-blanked in a short period (e.g., when using the "next" button
1183 in xscreensaver-demo.)
1188 fprintf (stderr, "%s: stopping de-race timer (%d remaining.)\n",
1189 blurb(), si->de_race_ticks);
1190 XtRemoveTimeOut (si->de_race_id);
1195 /* Now, try to blank.
1198 if (! blank_screen (si))
1200 /* We were unable to grab either the keyboard or mouse.
1201 This means we did not (and must not) blank the screen.
1202 If we were to blank the screen while some other program
1203 is holding both the mouse and keyboard grabbed, then
1204 we would never be able to un-blank it! We would never
1205 see any events, and the display would be wedged.
1207 So, just go around the loop again and wait for the
1208 next bout of idleness. (If the user remains idle, we
1209 will next try to blank the screen again in no more than
1212 Time retry = 60 * 1000;
1213 if (p->timeout < retry)
1219 "%s: DEBUG MODE: unable to grab -- BLANKING ANYWAY.\n",
1225 "%s: unable to grab keyboard or mouse! Blanking aborted.\n",
1228 schedule_wakeup_event (si, retry, p->debug_p);
1233 kill_screenhack (si);
1235 if (!si->throttled_p)
1236 spawn_screenhack (si, True);
1237 else if (p->verbose_p)
1238 fprintf (stderr, "%s: not launching hack (throttled.)\n", blurb());
1240 /* Don't start the cycle timer in demo mode. */
1241 if (!si->demoing_p && p->cycle)
1242 si->cycle_id = XtAppAddTimeOut (si->app,
1244 /* see comment in cycle_timer() */
1252 /* Maybe start locking the screen.
1255 Time lock_timeout = p->lock_timeout;
1257 if (si->emergency_lock_p && p->lock_p && lock_timeout)
1259 int secs = p->lock_timeout / 1000;
1262 "%s: locking now, instead of waiting for %d:%02d:%02d.\n",
1264 (secs / (60 * 60)), ((secs / 60) % 60), (secs % 60));
1268 si->emergency_lock_p = False;
1270 if (!si->demoing_p && /* if not going into demo mode */
1271 p->lock_p && /* and locking is enabled */
1272 !si->locking_disabled_p && /* and locking is possible */
1273 lock_timeout == 0) /* and locking is not timer-deferred */
1274 set_locked_p (si, True); /* then lock right now. */
1276 /* locked_p might be true already because of the above, or because of
1277 the LOCK ClientMessage. But if not, and if we're supposed to lock
1278 after some time, set up a timer to do so.
1283 si->lock_id = XtAppAddTimeOut (si->app, lock_timeout,
1284 activate_lock_timer,
1287 #endif /* !NO_LOCKING */
1290 ok_to_unblank = True;
1293 check_for_leaks ("blanked A");
1294 sleep_until_idle (si, False); /* until not idle */
1295 check_for_leaks ("blanked B");
1297 maybe_reload_init_file (si);
1300 /* Maybe unlock the screen.
1304 saver_screen_info *ssi = si->default_screen;
1305 if (si->locking_disabled_p) abort ();
1308 si->dbox_up_p = True;
1309 suspend_screenhack (si, True);
1310 XUndefineCursor (si->dpy, ssi->screensaver_window);
1312 ok_to_unblank = unlock_p (si);
1314 si->dbox_up_p = False;
1315 XDefineCursor (si->dpy, ssi->screensaver_window, ssi->cursor);
1316 suspend_screenhack (si, False); /* resume */
1318 if (!ok_to_unblank &&
1319 !screenhack_running_p (si))
1321 /* If the lock dialog has been dismissed and we're not about to
1322 unlock the screen, and there is currently no hack running,
1323 then launch one. (There might be no hack running if DPMS
1324 had kicked in. But DPMS is off now, so bring back the hack)
1327 XtRemoveTimeOut (si->cycle_id);
1329 cycle_timer ((XtPointer) si, 0);
1332 #endif /* !NO_LOCKING */
1334 } while (!ok_to_unblank);
1338 fprintf (stderr, "%s: unblanking screen at %s.\n",
1339 blurb(), timestring ());
1341 /* Kill before unblanking, to stop drawing as soon as possible. */
1342 kill_screenhack (si);
1343 unblank_screen (si);
1345 set_locked_p (si, False);
1346 si->emergency_lock_p = False;
1348 si->selection_mode = 0;
1350 /* If we're throttled, and the user has explicitly unlocked the screen,
1351 then unthrottle. If we weren't locked, then don't unthrottle
1352 automatically, because someone might have just bumped the desk... */
1355 if (si->throttled_p && p->verbose_p)
1356 fprintf (stderr, "%s: unthrottled.\n", blurb());
1357 si->throttled_p = False;
1362 XtRemoveTimeOut (si->cycle_id);
1368 XtRemoveTimeOut (si->lock_id);
1372 /* Since we're unblanked now, break race conditions and make
1373 sure we stay that way (see comment in timers.c.) */
1374 if (! si->de_race_id)
1375 de_race_timer ((XtPointer) si, 0);
1379 static void analyze_display (saver_info *si);
1380 static void fix_fds (void);
1383 main (int argc, char **argv)
1387 saver_info *si = &the_si;
1388 saver_preferences *p = &si->prefs;
1389 struct passwd *spasswd;
1392 memset(si, 0, sizeof(*si));
1393 global_si_kludge = si; /* I hate C so much... */
1397 # undef ya_rand_init
1400 save_argv (argc, argv);
1401 set_version_string (si, &argc, argv);
1402 privileged_initialization (si, &argc, argv);
1403 hack_environment (si);
1405 spasswd = getpwuid(getuid());
1408 fprintf(stderr, "Could not figure out who the current user is!\n");
1409 fprintf(stderr, "spasswd is %x\n", (unsigned int) spasswd);
1413 si->user = strdup(spasswd->pw_name ? spasswd->pw_name : "(unknown)");
1416 si->unlock_cb = gui_auth_conv;
1417 si->auth_finished_cb = auth_finished_cb;
1418 # endif /* !NO_LOCKING */
1420 shell = connect_to_server (si, &argc, argv);
1421 process_command_line (si, &argc, argv);
1424 load_init_file(si->dpy, p); /* must be before initialize_per_screen_info() */
1425 blurb_timestamp_p = p->timestamp_p; /* kludge */
1426 initialize_per_screen_info (si, shell); /* also sets si->fading_possible_p */
1428 /* We can only issue this warning now. */
1429 if (p->verbose_p && !si->fading_possible_p && (p->fade_p || p->unfade_p))
1431 "%s: there are no PseudoColor or GrayScale visuals.\n"
1432 "%s: ignoring the request for fading/unfading.\n",
1435 for (i = 0; i < si->nscreens; i++)
1437 saver_screen_info *ssi = &si->screens[i];
1438 if (ssi->real_screen_p)
1439 if (ensure_no_screensaver_running (si->dpy, si->screens[i].screen))
1443 lock_initialization (si, &argc, argv);
1444 print_lock_failure_banner (si);
1446 if (p->xsync_p) XSynchronize (si->dpy, True);
1448 if (p->verbose_p) analyze_display (si);
1449 initialize_server_extensions (si);
1451 si->blank_time = time ((time_t) 0); /* must be before ..._window */
1452 initialize_screensaver_window (si);
1457 disable_builtin_screensaver (si, True);
1458 sync_server_dpms_settings (si->dpy,
1459 (p->dpms_enabled_p &&
1460 p->mode != DONT_BLANK),
1461 p->dpms_standby / 1000,
1462 p->dpms_suspend / 1000,
1466 initialize_stderr (si);
1467 handle_signals (si);
1469 make_splash_dialog (si);
1471 main_loop (si); /* doesn't return */
1478 /* Bad Things Happen if stdin, stdout, and stderr have been closed
1479 (as by the `sh incantation "xscreensaver >&- 2>&-"). When you do
1480 that, the X connection gets allocated to one of these fds, and
1481 then some random library writes to stderr, and random bits get
1482 stuffed down the X pipe, causing "Xlib: sequence lost" errors.
1483 So, we cause the first three file descriptors to be open to
1484 /dev/null if they aren't open to something else already. This
1485 must be done before any other files are opened (or the closing
1486 of that other file will again free up one of the "magic" first
1489 We do this by opening /dev/null three times, and then closing
1490 those fds, *unless* any of them got allocated as #0, #1, or #2,
1491 in which case we leave them open. Gag.
1493 Really, this crap is technically required of *every* X program,
1494 if you want it to be robust in the face of "2>&-".
1496 int fd0 = open ("/dev/null", O_RDWR);
1497 int fd1 = open ("/dev/null", O_RDWR);
1498 int fd2 = open ("/dev/null", O_RDWR);
1499 if (fd0 > 2) close (fd0);
1500 if (fd1 > 2) close (fd1);
1501 if (fd2 > 2) close (fd2);
1506 /* Processing ClientMessage events.
1510 static Bool error_handler_hit_p = False;
1513 ignore_all_errors_ehandler (Display *dpy, XErrorEvent *error)
1515 error_handler_hit_p = True;
1519 /* Sometimes some systems send us ClientMessage events with bogus atoms in
1520 them. We only look up the atom names for printing warning messages,
1521 so don't bomb out when it happens...
1524 XGetAtomName_safe (Display *dpy, Atom atom)
1527 XErrorHandler old_handler;
1528 if (!atom) return 0;
1531 error_handler_hit_p = False;
1532 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
1533 result = XGetAtomName (dpy, atom);
1535 XSetErrorHandler (old_handler);
1537 if (error_handler_hit_p) result = 0;
1544 sprintf (buf, "<<undefined atom 0x%04X>>", (unsigned int) atom);
1545 return strdup (buf);
1551 clientmessage_response (saver_info *si, Window w, Bool error,
1552 const char *stderr_msg,
1553 const char *protocol_msg)
1557 saver_preferences *p = &si->prefs;
1558 XErrorHandler old_handler;
1560 if (error || p->verbose_p)
1561 fprintf (stderr, "%s: %s\n", blurb(), stderr_msg);
1563 L = strlen(protocol_msg);
1564 proto = (char *) malloc (L + 2);
1565 proto[0] = (error ? '-' : '+');
1566 strcpy (proto+1, protocol_msg);
1569 /* Ignore all X errors while sending a response to a ClientMessage.
1570 Pretty much the only way we could get an error here is if the
1571 window we're trying to send the reply on has been deleted, in
1572 which case, the sender of the ClientMessage won't see our response
1575 XSync (si->dpy, False);
1576 error_handler_hit_p = False;
1577 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
1579 XChangeProperty (si->dpy, w, XA_SCREENSAVER_RESPONSE, XA_STRING, 8,
1580 PropModeReplace, (unsigned char *) proto, L);
1582 XSync (si->dpy, False);
1583 XSetErrorHandler (old_handler);
1584 XSync (si->dpy, False);
1591 bogus_clientmessage_warning (saver_info *si, XEvent *event)
1593 saver_preferences *p = &si->prefs;
1594 char *str = XGetAtomName_safe (si->dpy, event->xclient.message_type);
1595 Window w = event->xclient.window;
1598 Bool root_p = False;
1601 for (screen = 0; screen < si->nscreens; screen++)
1602 if (w == si->screens[screen].screensaver_window)
1604 strcpy (wdesc, "xscreensaver");
1607 else if (w == RootWindow (si->dpy, screen))
1609 strcpy (wdesc, "root");
1614 /* If this ClientMessage was sent to the real root window instead of to the
1615 xscreensaver window, then it might be intended for someone else who is
1616 listening on the root window (e.g., the window manager). So only print
1617 the warning if: we are in debug mode; or if the bogus message was
1618 actually sent to one of the xscreensaver-created windows.
1620 if (root_p && !p->debug_p)
1625 XErrorHandler old_handler;
1627 XWindowAttributes xgwa;
1628 memset (&hint, 0, sizeof(hint));
1629 memset (&xgwa, 0, sizeof(xgwa));
1631 XSync (si->dpy, False);
1632 old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
1633 XGetClassHint (si->dpy, w, &hint);
1634 XGetWindowAttributes (si->dpy, w, &xgwa);
1635 XSync (si->dpy, False);
1636 XSetErrorHandler (old_handler);
1637 XSync (si->dpy, False);
1639 screen = (xgwa.screen ? screen_number (xgwa.screen) : -1);
1641 sprintf (wdesc, "%.20s / %.20s",
1642 (hint.res_name ? hint.res_name : "(null)"),
1643 (hint.res_class ? hint.res_class : "(null)"));
1644 if (hint.res_name) XFree (hint.res_name);
1645 if (hint.res_class) XFree (hint.res_class);
1648 fprintf (stderr, "%s: %d: unrecognised ClientMessage \"%s\" received\n",
1649 blurb(), screen, (str ? str : "(null)"));
1650 fprintf (stderr, "%s: %d: for window 0x%lx (%s)\n",
1651 blurb(), screen, (unsigned long) w, wdesc);
1652 if (str) XFree (str);
1656 handle_clientmessage (saver_info *si, XEvent *event, Bool until_idle_p)
1658 saver_preferences *p = &si->prefs;
1660 Window window = event->xclient.window;
1662 /* Preferences might affect our handling of client messages. */
1663 maybe_reload_init_file (si);
1665 if (event->xclient.message_type != XA_SCREENSAVER ||
1666 event->xclient.format != 32)
1668 bogus_clientmessage_warning (si, event);
1672 type = event->xclient.data.l[0];
1673 if (type == XA_ACTIVATE)
1677 if (p->mode == DONT_BLANK)
1679 clientmessage_response(si, window, True,
1680 "ACTIVATE ClientMessage received in DONT_BLANK mode.",
1681 "screen blanking is currently disabled.");
1685 clientmessage_response(si, window, False,
1686 "ACTIVATE ClientMessage received.",
1688 si->selection_mode = 0;
1689 si->demoing_p = False;
1691 if (si->throttled_p && p->verbose_p)
1692 fprintf (stderr, "%s: unthrottled.\n", blurb());
1693 si->throttled_p = False;
1695 if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
1697 XForceScreenSaver (si->dpy, ScreenSaverActive);
1705 clientmessage_response(si, window, True,
1706 "ClientMessage ACTIVATE received while already active.",
1709 else if (type == XA_DEACTIVATE)
1713 if (si->throttled_p && p->verbose_p)
1714 fprintf (stderr, "%s: unthrottled.\n", blurb());
1715 si->throttled_p = False;
1717 clientmessage_response(si, window, False,
1718 "DEACTIVATE ClientMessage received.",
1720 if (si->using_mit_saver_extension || si->using_sgi_saver_extension)
1722 XForceScreenSaver (si->dpy, ScreenSaverReset);
1730 clientmessage_response(si, window, False,
1731 "ClientMessage DEACTIVATE received while inactive: resetting idle timer.",
1732 "not active: idle timer reset.");
1735 else if (type == XA_CYCLE)
1739 clientmessage_response(si, window, False,
1740 "CYCLE ClientMessage received.",
1742 si->selection_mode = 0; /* 0 means randomize when its time. */
1743 si->demoing_p = False;
1745 if (si->throttled_p && p->verbose_p)
1746 fprintf (stderr, "%s: unthrottled.\n", blurb());
1747 si->throttled_p = False;
1750 XtRemoveTimeOut (si->cycle_id);
1752 cycle_timer ((XtPointer) si, 0);
1755 clientmessage_response(si, window, True,
1756 "ClientMessage CYCLE received while inactive.",
1759 else if (type == XA_NEXT || type == XA_PREV)
1761 clientmessage_response(si, window, False,
1763 ? "NEXT ClientMessage received."
1764 : "PREV ClientMessage received."),
1766 si->selection_mode = (type == XA_NEXT ? -1 : -2);
1767 si->demoing_p = False;
1769 if (si->throttled_p && p->verbose_p)
1770 fprintf (stderr, "%s: unthrottled.\n", blurb());
1771 si->throttled_p = False;
1776 XtRemoveTimeOut (si->cycle_id);
1778 cycle_timer ((XtPointer) si, 0);
1783 else if (type == XA_SELECT)
1787 long which = event->xclient.data.l[1];
1789 if (p->mode == DONT_BLANK)
1791 clientmessage_response(si, window, True,
1792 "SELECT ClientMessage received in DONT_BLANK mode.",
1793 "screen blanking is currently disabled.");
1797 sprintf (buf, "SELECT %ld ClientMessage received.", which);
1798 sprintf (buf2, "activating (%ld).", which);
1799 clientmessage_response (si, window, False, buf, buf2);
1801 if (which < 0) which = 0; /* 0 == "random" */
1802 si->selection_mode = which;
1803 si->demoing_p = False;
1805 if (si->throttled_p && p->verbose_p)
1806 fprintf (stderr, "%s: unthrottled.\n", blurb());
1807 si->throttled_p = False;
1812 XtRemoveTimeOut (si->cycle_id);
1814 cycle_timer ((XtPointer) si, 0);
1819 else if (type == XA_EXIT)
1821 /* Ignore EXIT message if the screen is locked. */
1822 if (until_idle_p || !si->locked_p)
1824 clientmessage_response (si, window, False,
1825 "EXIT ClientMessage received.",
1829 unblank_screen (si);
1830 kill_screenhack (si);
1831 XSync (si->dpy, False);
1833 saver_exit (si, 0, 0);
1836 clientmessage_response (si, window, True,
1837 "EXIT ClientMessage received while locked.",
1838 "screen is locked.");
1840 else if (type == XA_RESTART)
1842 /* The RESTART message works whether the screensaver is active or not,
1843 unless the screen is locked, in which case it doesn't work.
1845 if (until_idle_p || !si->locked_p)
1847 clientmessage_response (si, window, False,
1848 "RESTART ClientMessage received.",
1852 unblank_screen (si);
1853 kill_screenhack (si);
1854 XSync (si->dpy, False);
1857 restart_process (si); /* does not return */
1861 clientmessage_response (si, window, True,
1862 "RESTART ClientMessage received while locked.",
1863 "screen is locked.");
1865 else if (type == XA_DEMO)
1867 long arg = event->xclient.data.l[1];
1868 Bool demo_one_hack_p = (arg == 5000);
1870 if (demo_one_hack_p)
1874 long which = event->xclient.data.l[2];
1877 sprintf (buf, "DEMO %ld ClientMessage received.", which);
1878 sprintf (buf2, "demoing (%ld).", which);
1879 clientmessage_response (si, window, False, buf, buf2);
1881 if (which < 0) which = 0; /* 0 == "random" */
1882 si->selection_mode = which;
1883 si->demoing_p = True;
1885 if (si->throttled_p && p->verbose_p)
1886 fprintf (stderr, "%s: unthrottled.\n", blurb());
1887 si->throttled_p = False;
1892 clientmessage_response (si, window, True,
1893 "DEMO ClientMessage received while active.",
1898 clientmessage_response (si, window, True,
1899 "obsolete form of DEMO ClientMessage.",
1900 "obsolete form of DEMO ClientMessage.");
1903 else if (type == XA_PREFS)
1905 clientmessage_response (si, window, True,
1906 "the PREFS client-message is obsolete.",
1907 "the PREFS client-message is obsolete.");
1909 else if (type == XA_LOCK)
1912 clientmessage_response (si, window, True,
1913 "not compiled with support for locking.",
1914 "locking not enabled.");
1915 #else /* !NO_LOCKING */
1916 if (si->locking_disabled_p)
1917 clientmessage_response (si, window, True,
1918 "LOCK ClientMessage received, but locking is disabled.",
1919 "locking not enabled.");
1920 else if (si->locked_p)
1921 clientmessage_response (si, window, True,
1922 "LOCK ClientMessage received while already locked.",
1927 char *response = (until_idle_p
1928 ? "activating and locking."
1930 sprintf (buf, "LOCK ClientMessage received; %s", response);
1931 clientmessage_response (si, window, False, buf, response);
1932 set_locked_p (si, True);
1933 si->selection_mode = 0;
1934 si->demoing_p = False;
1936 if (si->lock_id) /* we're doing it now, so lose the timeout */
1938 XtRemoveTimeOut (si->lock_id);
1944 if (si->using_mit_saver_extension ||
1945 si->using_sgi_saver_extension)
1947 XForceScreenSaver (si->dpy, ScreenSaverActive);
1956 #endif /* !NO_LOCKING */
1958 else if (type == XA_THROTTLE)
1960 /* The THROTTLE command is deprecated -- it predates the XDPMS
1961 extension. Instead of using -throttle, users should instead
1962 just power off the monitor (e.g., "xset dpms force off".)
1963 In a few minutes, xscreensaver will notice that the monitor
1964 is off, and cease running hacks.
1966 if (si->throttled_p)
1967 clientmessage_response (si, window, True,
1968 "THROTTLE ClientMessage received, but "
1969 "already throttled.",
1970 "already throttled.");
1974 char *response = "throttled.";
1975 si->throttled_p = True;
1976 si->selection_mode = 0;
1977 si->demoing_p = False;
1978 sprintf (buf, "THROTTLE ClientMessage received; %s", response);
1979 clientmessage_response (si, window, False, buf, response);
1984 XtRemoveTimeOut (si->cycle_id);
1986 cycle_timer ((XtPointer) si, 0);
1990 else if (type == XA_UNTHROTTLE)
1992 if (! si->throttled_p)
1993 clientmessage_response (si, window, True,
1994 "UNTHROTTLE ClientMessage received, but "
2000 char *response = "unthrottled.";
2001 si->throttled_p = False;
2002 si->selection_mode = 0;
2003 si->demoing_p = False;
2004 sprintf (buf, "UNTHROTTLE ClientMessage received; %s", response);
2005 clientmessage_response (si, window, False, buf, response);
2010 XtRemoveTimeOut (si->cycle_id);
2012 cycle_timer ((XtPointer) si, 0);
2020 str = XGetAtomName_safe (si->dpy, type);
2024 if (strlen (str) > 80)
2025 strcpy (str+70, "...");
2026 sprintf (buf, "unrecognised screensaver ClientMessage %s received.",
2033 "unrecognised screensaver ClientMessage 0x%x received.",
2034 (unsigned int) event->xclient.data.l[0]);
2037 clientmessage_response (si, window, True, buf, buf);
2043 /* Some random diagnostics printed in -verbose mode.
2047 analyze_display (saver_info *si)
2051 const char *name; const char *desc; Bool useful_p;
2054 { "SCREEN_SAVER", /* underscore */ "SGI Screen-Saver",
2055 # ifdef HAVE_SGI_SAVER_EXTENSION
2060 }, { "SCREEN-SAVER", /* dash */ "SGI Screen-Saver",
2061 # ifdef HAVE_SGI_SAVER_EXTENSION
2066 }, { "MIT-SCREEN-SAVER", "MIT Screen-Saver",
2067 # ifdef HAVE_MIT_SAVER_EXTENSION
2072 }, { "XIDLE", "XIdle",
2073 # ifdef HAVE_XIDLE_EXTENSION
2078 }, { "SGI-VIDEO-CONTROL", "SGI Video-Control",
2079 # ifdef HAVE_SGI_VC_EXTENSION
2084 }, { "READDISPLAY", "SGI Read-Display",
2085 # ifdef HAVE_READ_DISPLAY_EXTENSION
2090 }, { "MIT-SHM", "Shared Memory",
2091 # ifdef HAVE_XSHM_EXTENSION
2096 }, { "DOUBLE-BUFFER", "Double-Buffering",
2097 # ifdef HAVE_DOUBLE_BUFFER_EXTENSION
2102 }, { "DPMS", "Power Management",
2103 # ifdef HAVE_DPMS_EXTENSION
2114 }, { "XFree86-VidModeExtension", "XF86 Video-Mode",
2115 # ifdef HAVE_XF86VMODE
2120 }, { "XINERAMA", "Xinerama",
2121 # ifdef HAVE_XINERAMA
2126 }, { "RANDR", "Resize-and-Rotate",
2132 }, { "Apple-DRI", "Apple-DRI (XDarwin)",
2137 fprintf (stderr, "%s: running on display \"%s\" (%d %sscreen%s).\n",
2139 DisplayString(si->dpy),
2141 (si->xinerama_p ? "Xinerama " : ""),
2142 (si->nscreens == 1 ? "" : "s"));
2143 fprintf (stderr, "%s: vendor is %s, %d.\n", blurb(),
2144 ServerVendor(si->dpy), VendorRelease(si->dpy));
2146 fprintf (stderr, "%s: useful extensions:\n", blurb());
2147 for (i = 0; i < countof(exts); i++)
2149 int op = 0, event = 0, error = 0;
2152 if (!XQueryExtension (si->dpy, exts[i].name, &op, &event, &error))
2154 sprintf (buf, "%s: ", blurb());
2156 strcat (buf, exts[i].desc);
2157 if (!exts[i].useful_p)
2160 while (strlen (buf) < k) strcat (buf, " ");
2161 strcat (buf, "<-- not supported at compile time!");
2163 fprintf (stderr, "%s\n", buf);
2166 for (i = 0; i < si->nscreens; i++)
2168 saver_screen_info *ssi = &si->screens[i];
2169 unsigned long colormapped_depths = 0;
2170 unsigned long non_mapped_depths = 0;
2171 XVisualInfo vi_in, *vi_out;
2174 if (!ssi->real_screen_p) continue;
2176 vi_in.screen = ssi->real_screen_number;
2177 vi_out = XGetVisualInfo (si->dpy, VisualScreenMask, &vi_in, &out_count);
2178 if (!vi_out) continue;
2179 for (j = 0; j < out_count; j++)
2180 if (vi_out[j].class == PseudoColor)
2181 colormapped_depths |= (1 << vi_out[j].depth);
2183 non_mapped_depths |= (1 << vi_out[j].depth);
2184 XFree ((char *) vi_out);
2186 if (colormapped_depths)
2188 fprintf (stderr, "%s: screen %d colormapped depths:", blurb(),
2189 ssi->real_screen_number);
2190 for (j = 0; j < 32; j++)
2191 if (colormapped_depths & (1 << j))
2192 fprintf (stderr, " %d", j);
2193 fprintf (stderr, ".\n");
2195 if (non_mapped_depths)
2197 fprintf (stderr, "%s: screen %d non-colormapped depths:",
2198 blurb(), ssi->real_screen_number);
2199 for (j = 0; j < 32; j++)
2200 if (non_mapped_depths & (1 << j))
2201 fprintf (stderr, " %d", j);
2202 fprintf (stderr, ".\n");
2208 fprintf (stderr, "%s: Xinerama layout:\n", blurb());
2209 for (i = 0; i < si->nscreens; i++)
2211 saver_screen_info *ssi = &si->screens[i];
2212 fprintf (stderr, "%s: %c %d/%d: %dx%d+%d+%d\n",
2214 (ssi->real_screen_p ? '+' : ' '),
2215 ssi->number, ssi->real_screen_number,
2216 ssi->width, ssi->height, ssi->x, ssi->y);
2222 display_is_on_console_p (saver_info *si)
2224 Bool not_on_console = True;
2225 char *dpystr = DisplayString (si->dpy);
2226 char *tail = (char *) strchr (dpystr, ':');
2227 if (! tail || strncmp (tail, ":0", 2))
2228 not_on_console = True;
2231 char dpyname[255], localname[255];
2232 strncpy (dpyname, dpystr, tail-dpystr);
2233 dpyname [tail-dpystr] = 0;
2235 !strcmp(dpyname, "unix") ||
2236 !strcmp(dpyname, "localhost"))
2237 not_on_console = False;
2238 else if (gethostname (localname, sizeof (localname)))
2239 not_on_console = True; /* can't find hostname? */
2242 /* We have to call gethostbyname() on the result of gethostname()
2243 because the two aren't guarenteed to be the same name for the
2244 same host: on some losing systems, one is a FQDN and the other
2245 is not. Here in the wide wonderful world of Unix it's rocket
2246 science to obtain the local hostname in a portable fashion.
2248 And don't forget, gethostbyname() reuses the structure it
2249 returns, so we have to copy the fucker before calling it again.
2250 Thank you master, may I have another.
2252 struct hostent *h = gethostbyname (dpyname);
2254 not_on_console = True;
2259 strcpy (hn, h->h_name);
2260 l = gethostbyname (localname);
2261 not_on_console = (!l || !!(strcmp (l->h_name, hn)));
2265 return !not_on_console;
2269 /* Do a little bit of heap introspection...
2272 check_for_leaks (const char *where)
2274 #if defined(HAVE_SBRK) && defined(LEAK_PARANOIA)
2275 static unsigned long last_brk = 0;
2276 int b = (unsigned long) sbrk(0);
2277 if (last_brk && last_brk < b)
2278 fprintf (stderr, "%s: %s: brk grew by %luK.\n",
2280 (((b - last_brk) + 1023) / 1024));
2282 #endif /* HAVE_SBRK */