1 /* xscreensaver-command, Copyright (c) 1991-1998
2 * by Jamie Zawinski <jwz@jwz.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
20 #include <sys/types.h>
26 #include <X11/Xproto.h> /* for CARD32 */
28 #include <X11/Xatom.h>
29 #include <X11/Xutil.h> /* for XGetClassHint() */
32 #include <X11/Intrinsic.h> /* only needed to get through xscreensaver.h */
38 ERROR! you must not include vroot.h in this file
44 Atom XA_SCREENSAVER, XA_SCREENSAVER_VERSION, XA_SCREENSAVER_RESPONSE;
45 Atom XA_SCREENSAVER_ID, XA_SCREENSAVER_TIME, XA_SELECT, XA_DEMO;
46 static Atom XA_ACTIVATE, XA_DEACTIVATE, XA_CYCLE, XA_NEXT, XA_PREV, XA_EXIT;
47 static Atom XA_RESTART, XA_PREFS, XA_LOCK;
49 static char *screensaver_version;
50 static char *usage = "\n\
51 usage: %s -<option>\n\
53 This program provides external control of a running xscreensaver process.\n\
54 Version %s, copyright (c) 1991-1998 Jamie Zawinski <jwz@jwz.org>.\n\
56 The xscreensaver program is a daemon that runs in the background.\n\
57 You control a running xscreensaver process by sending it messages\n\
58 with this program, xscreensaver-command. See the man pages for\n\
59 details. These are the arguments understood by xscreensaver-command:\n\
61 -demo Ask the xscreensaver process to enter interactive demo mode.\n\
63 -prefs Ask the xscreensaver process to bring up the preferences\n\
66 -activate Turn on the screensaver (blank the screen), as if the user\n\
67 had been idle for long enough.\n\
69 -deactivate Turns off the screensaver (un-blank the screen), as if user\n\
70 activity had been detected.\n\
72 -cycle If the screensaver is active (the screen is blanked), then\n\
73 stop the current graphics demo and run a new one (chosen\n\
76 -next Like either -activate or -cycle, depending on which is more\n\
77 appropriate, except that the graphics hack that will be run\n\
78 is the next one in the list, instead of a randomly-chosen\n\
79 one. In other words, repeatedly executing -next will cause\n\
80 the xscreensaver process to invoke each graphics demo\n\
81 sequentially. (Though using the -demo option is probably\n\
82 an easier way to accomplish that.)\n\
84 -prev Like -next, but goes in the other direction.\n\
86 -select <N> Like -activate, but runs the Nth element in the list of\n\
87 hacks. By knowing what is in the `programs' list, and in\n\
88 what order, you can use this to activate the screensaver\n\
89 with a particular graphics demo. (The first element in the\n\
90 list is numbered 1, not 0.)\n\
92 -exit Causes the xscreensaver process to exit gracefully. This is\n\
93 roughly the same as killing the process with `kill', but it\n\
94 is easier, since you don't need to first figure out the pid.\n\
95 (Note that one must *never* kill xscreensaver with -9!)\n\
97 -restart Causes the screensaver process to exit and then restart with\n\
98 the same command line arguments as last time. Do this after\n\
99 you've changed your X resource settings, to cause\n\
100 xscreensaver to notice the changes.\n\
102 -lock Tells the running xscreensaver process to lock the screen\n\
103 immediately. This is like -activate, but forces locking as\n\
104 well, even if locking is not the default.\n\
106 -version Prints the version of xscreensaver that is currently running\n\
107 on the display -- that is, the actual version number of the\n\
108 running xscreensaver background process, rather than the\n\
109 version number of xscreensaver-command.\n\
111 -time Prints the time at which the screensaver last activated or\n\
112 deactivated (roughly, how long the user has been idle or\n\
113 non-idle -- but not quite, since it only tells you when the\n\
114 screen became blanked or un-blanked.)\n\
116 See the man page for more details.\n\
117 For updates, check http://www.jwz.org/xscreensaver/\n\
120 #define USAGE() do { \
121 fprintf (stderr, usage, progname, screensaver_version); exit (1); \
125 main (int argc, char **argv)
134 screensaver_version = (char *) malloc (5);
135 memcpy (screensaver_version, screensaver_id + 17, 4);
136 screensaver_version [4] = 0;
138 for (i = 1; i < argc; i++)
140 const char *s = argv [i];
142 if (s[0] == '-' && s[1] == '-') s++;
145 if (!strncmp (s, "-display", L)) dpyname = argv [++i];
146 else if (cmd) USAGE();
147 else if (!strncmp (s, "-activate", L)) cmd = &XA_ACTIVATE;
148 else if (!strncmp (s, "-deactivate", L)) cmd = &XA_DEACTIVATE;
149 else if (!strncmp (s, "-cycle", L)) cmd = &XA_CYCLE;
150 else if (!strncmp (s, "-next", L)) cmd = &XA_NEXT;
151 else if (!strncmp (s, "-prev", L)) cmd = &XA_PREV;
152 else if (!strncmp (s, "-select", L)) cmd = &XA_SELECT;
153 else if (!strncmp (s, "-exit", L)) cmd = &XA_EXIT;
154 else if (!strncmp (s, "-restart", L)) cmd = &XA_RESTART;
155 else if (!strncmp (s, "-demo", L)) cmd = &XA_DEMO;
156 else if (!strncmp (s, "-preferences",L)) cmd = &XA_PREFS;
157 else if (!strncmp (s, "-prefs",L)) cmd = &XA_PREFS;
158 else if (!strncmp (s, "-lock", L)) cmd = &XA_LOCK;
159 else if (!strncmp (s, "-version", L)) cmd = &XA_SCREENSAVER_VERSION;
160 else if (!strncmp (s, "-time", L)) cmd = &XA_SCREENSAVER_TIME;
163 if (cmd == &XA_SELECT || cmd == &XA_DEMO)
167 if (i+1 < argc && (1 == sscanf(argv[i+1], " %ld %c", &a, &c)))
179 /* no command may have a negative argument. */
183 /* SELECT must have a non-zero argument. */
184 if (cmd == &XA_SELECT)
189 /* no command other than SELECT and DEMO may have a non-zero argument. */
190 if (cmd != &XA_DEMO && cmd != &XA_SELECT)
196 /* For backward compatibility: -demo with no arguments used to send a
197 "DEMO 0" ClientMessage to the xscreensaver process, which brought up
198 the built-in demo mode dialog. Now that the demo mode dialog is no
199 longer built in, we bring it up by just running the "xscreensaver-demo"
202 Note that "-DEMO <n>" still sends a ClientMessage.
204 if (cmd == &XA_PREFS ||
205 (cmd == &XA_DEMO && arg == 0))
208 char *new_argv[] = { "xscreensaver-demo", 0, 0, 0, 0, 0 };
213 new_argv[ac++] = "-display";
214 new_argv[ac++] = dpyname;
217 if (cmd == &XA_PREFS)
218 new_argv[ac++] = "-prefs";
222 execvp (new_argv[0], new_argv); /* shouldn't return */
224 sprintf (buf, "%s: could not exec %s", progname, new_argv[0]);
233 if (!dpyname) dpyname = (char *) getenv ("DISPLAY");
234 dpy = XOpenDisplay (dpyname);
237 fprintf (stderr, "%s: can't open display %s\n", progname,
238 (dpyname ? dpyname : "(null)"));
242 XA_VROOT = XInternAtom (dpy, "__SWM_VROOT", False);
243 XA_SCREENSAVER = XInternAtom (dpy, "SCREENSAVER", False);
244 XA_SCREENSAVER_ID = XInternAtom (dpy, "_SCREENSAVER_ID", False);
245 XA_SCREENSAVER_VERSION = XInternAtom (dpy, "_SCREENSAVER_VERSION",False);
246 XA_SCREENSAVER_TIME = XInternAtom (dpy, "_SCREENSAVER_TIME", False);
247 XA_SCREENSAVER_RESPONSE = XInternAtom (dpy, "_SCREENSAVER_RESPONSE", False);
248 XA_ACTIVATE = XInternAtom (dpy, "ACTIVATE", False);
249 XA_DEACTIVATE = XInternAtom (dpy, "DEACTIVATE", False);
250 XA_RESTART = XInternAtom (dpy, "RESTART", False);
251 XA_CYCLE = XInternAtom (dpy, "CYCLE", False);
252 XA_NEXT = XInternAtom (dpy, "NEXT", False);
253 XA_PREV = XInternAtom (dpy, "PREV", False);
254 XA_SELECT = XInternAtom (dpy, "SELECT", False);
255 XA_EXIT = XInternAtom (dpy, "EXIT", False);
256 XA_DEMO = XInternAtom (dpy, "DEMO", False);
257 XA_PREFS = XInternAtom (dpy, "PREFS", False);
258 XA_LOCK = XInternAtom (dpy, "LOCK", False);
262 if (*cmd == XA_ACTIVATE || *cmd == XA_LOCK ||
263 *cmd == XA_NEXT || *cmd == XA_PREV || *cmd == XA_SELECT)
264 /* People never guess that KeyRelease deactivates the screen saver too,
265 so if we're issuing an activation command, wait a second. */
268 i = xscreensaver_command (dpy, *cmd, arg, True);