1 /* xscreensaver-command, Copyright (c) 1991-1994 Jamie Zawinski <jwz@mcom.com>
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
15 #include <X11/Xatom.h>
22 ERROR! you must not include vroot.h in this file
25 static char *screensaver_version;
26 static char *usage = "usage: %s -<switch>\n\
28 This program provides external control of a running xscreensaver process.\n\
29 Version %s, copyright (c) 1991-1994 Jamie Zawinski <jwz@mcom.com>.\n\
31 -demo Enter interactive demo mode.\n\
32 -deactivate Turns off the screensaver if it is on, as user input would.\n\
33 -activate Turns it on as if the user had been idle for long enough.\n\
34 -cycle Stops the current hack and runs a new one.\n\
35 -next Like either -activate or -cycle, depending on which is more\n\
36 appropriate, except that the screenhack that will be run is\n\
37 the next one in the list of hacks, instead of a randomly-\n\
38 chosen one. This option is good for looking at a demo of\n\
39 each of the hacks in place.\n\
40 -prev Like -next, but goes in the other direction.\n\
41 -exit Causes the screensaver process to exit. It should be ok to\n\
42 just kill the process (NOT with -9!) but this is a slightly\n\
44 -restart Causes the screensaver process to exit and then restart with\n\
45 the same command line arguments. This is a good way of \n\
46 causing the screensaver to re-read the resource database.\n\
47 -lock Same as -activate, but with immediate locking.\n\
49 See the man page for more details.\n\n";
52 find_screensaver_window (dpy, progname)
57 Window root = RootWindowOfScreen (DefaultScreenOfDisplay (dpy));
58 Window root2, parent, *kids;
61 if (! XQueryTree (dpy, root, &root2, &parent, &kids, &nkids))
67 if (! (kids && nkids))
69 for (i = 0; i < nkids; i++)
73 unsigned long nitems, bytesafter;
76 if (XGetWindowProperty (dpy, kids[i],
77 XInternAtom (dpy, "_SCREENSAVER_VERSION", False),
78 0, 1, False, XA_STRING,
79 &type, &format, &nitems, &bytesafter,
80 (unsigned char **) &version)
85 fprintf (stderr, "%s: no screensaver is running on display %s", progname,
92 { fprintf (stderr, usage, argv[0], screensaver_version); exit (1); }
107 char *message = 0, *dpyname = 0;
108 screensaver_version = (char *) malloc (5);
109 memcpy (screensaver_version, screensaver_id + 17, 4);
110 screensaver_version [4] = 0;
111 for (i = 1; i < argc; i++)
116 if (!strncmp (s, "-display", L)) dpyname = argv [++i];
117 else if (message) USAGE ()
118 else if (!strncmp (s, "-activate", L)) message = "ACTIVATE";
119 else if (!strncmp (s, "-deactivate", L)) message = "DEACTIVATE";
120 else if (!strncmp (s, "-cycle", L)) message = "CYCLE";
121 else if (!strncmp (s, "-next", L)) message = "NEXT";
122 else if (!strncmp (s, "-prev", L)) message = "PREV";
123 else if (!strncmp (s, "-exit", L)) message = "EXIT";
124 else if (!strncmp (s, "-restart", L)) message = "RESTART";
125 else if (!strncmp (s, "-demo", L)) message = "DEMO";
126 else if (!strncmp (s, "-lock", L)) message = "LOCK";
129 if (! message) USAGE ();
130 if (!dpyname) dpyname = (char *) getenv ("DISPLAY");
131 dpy = XOpenDisplay (dpyname);
134 fprintf (stderr, "%s: can't open display %s\n", argv[0],
135 (dpyname ? dpyname : "(null)"));
138 window = find_screensaver_window (dpy, argv[0]);
140 event.xany.type = ClientMessage;
141 event.xclient.display = dpy;
142 event.xclient.window = window;
143 event.xclient.message_type = XInternAtom (dpy, "SCREENSAVER", False);
144 event.xclient.format = 32;
145 event.xclient.data.l[0] = (long) XInternAtom (dpy, message, False);
146 if (! XSendEvent (dpy, window, False, 0L, &event))
148 fprintf (stderr, "%s: XSendEvent(dpy, 0x%x ...) failed.\n", argv [0],
149 (unsigned int) window);