1 /* xscreensaver-command, Copyright (c) 1991-1994 Jamie Zawinski <jwz@lucid.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@lucid.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); }
103 char *message = 0, *dpyname = 0;
104 screensaver_version = (char *) malloc (5);
105 memcpy (screensaver_version, screensaver_id + 17, 4);
106 screensaver_version [4] = 0;
107 for (i = 1; i < argc; i++)
112 if (!strncmp (s, "-display", L)) dpyname = argv [++i];
113 else if (message) USAGE ()
114 else if (!strncmp (s, "-activate", L)) message = "ACTIVATE";
115 else if (!strncmp (s, "-deactivate", L)) message = "DEACTIVATE";
116 else if (!strncmp (s, "-cycle", L)) message = "CYCLE";
117 else if (!strncmp (s, "-next", L)) message = "NEXT";
118 else if (!strncmp (s, "-prev", L)) message = "PREV";
119 else if (!strncmp (s, "-exit", L)) message = "EXIT";
120 else if (!strncmp (s, "-restart", L)) message = "RESTART";
121 else if (!strncmp (s, "-demo", L)) message = "DEMO";
122 else if (!strncmp (s, "-lock", L)) message = "LOCK";
125 if (! message) USAGE ();
126 if (!dpyname) dpyname = (char *) getenv ("DISPLAY");
127 dpy = XOpenDisplay (dpyname);
130 fprintf (stderr, "%s: can't open display %s\n", argv[0],
131 (dpyname ? dpyname : "(null)"));
134 window = find_screensaver_window (dpy, argv[0]);
136 event.xany.type = ClientMessage;
137 event.xclient.display = dpy;
138 event.xclient.window = window;
139 event.xclient.message_type = XInternAtom (dpy, "SCREENSAVER", False);
140 event.xclient.format = 32;
141 event.xclient.data.l[0] = (long) XInternAtom (dpy, message, False);
142 if (! XSendEvent (dpy, window, False, 0L, &event))
144 fprintf (stderr, "%s: XSendEvent(dpy, 0x%x ...) failed.\n", argv [0],
145 (unsigned int) window);