3 * Set and fetch X atom properties
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Edgeware X tools collection.
12 * X tools is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * X tools is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with X tools; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
37 #include <X11/cursorfont.h>
40 #include <mLib/mdwopt.h>
41 #include <mLib/quis.h>
42 #include <mLib/alloc.h>
43 #include <mLib/report.h>
47 /*----- Static variables --------------------------------------------------*/
49 static Display
*dpy
= 0;
50 static Window window
= None
;
52 /*----- Command implementations -------------------------------------------*/
54 static void help(char **);
55 static int c_help(int argc
, char **argv
)
56 { help(argv
+ 1); return (0); }
58 static int c_get(int argc
, char **argv
)
64 die(EXIT_FAILURE
, "Usage: get PROPERTY");
65 if ((p
= XInternAtom(dpy
, argv
[1], True
)) == None
||
66 (a
= xatom_get(dpy
, window
, p
)) == None
)
68 name
= XGetAtomName(dpy
, a
);
73 static int c_set(int argc
, char **argv
)
78 die(EXIT_FAILURE
, "Usage: set PROPERTY VALUE");
79 p
= XInternAtom(dpy
, argv
[1], False
);
80 a
= XInternAtom(dpy
, argv
[2], False
);
81 xatom_set(dpy
, window
, p
, a
);
85 static int c_delete(int argc
, char **argv
)
90 die(EXIT_FAILURE
, "Usage: delete PROPERTY");
91 if ((p
= XInternAtom(dpy
, argv
[1], True
)) == None
)
93 xatom_delete(dpy
, window
, p
);
97 static int c_wait(int argc
, char **argv
)
104 die(EXIT_FAILURE
, "Usage: wait PROPERTY [VALUE...]");
106 p
= XInternAtom(dpy
, argv
[1], False
);
109 aa
= xmalloc(n
* sizeof(Atom
));
110 XInternAtoms(dpy
, argv
+ 2, n
, False
, aa
);
113 a
= xatom_wait(dpy
, window
, p
, aa
, n
);
115 name
= XGetAtomName(dpy
, a
);
124 /*----- Utilities ---------------------------------------------------------*/
126 /* --- @choosewindow@ --- *
130 * Returns: An X window id.
132 * Use: Allows the user to select a window using the mouse.
135 static Window
choosewindow(void)
137 Cursor cross
= XCreateFontCursor(dpy
, XC_crosshair
);
141 DefaultRootWindow(dpy
),
151 XNextEvent(dpy
, &event
);
152 switch (event
.type
) {
154 switch (event
.xbutton
.button
) {
156 XUngrabPointer(dpy
, event
.xbutton
.time
);
157 die(EXIT_FAILURE
, "aborted window selection");
160 window
= event
.xbutton
.subwindow
;
162 window
= event
.xbutton
.window
;
163 XUngrabPointer(dpy
, event
.xbutton
.time
);
171 /* --- @autoreap@ --- *
177 * Use: Causes child processes to be reaped as reports of their
178 * demises come in. Their exit statuses are simply discarded.
180 * This program needs to reap child processes even though it
181 * didn't create them and doesn't know what to do with their
182 * statuses because it's often used in shell scripts of the form
184 * ... start lots of stuff ...
185 * exec xatom wait GODOT ARRIVED
188 static void reap(int sig
)
189 { int e
= errno
; while (waitpid(-1, 0, WNOHANG
) > 0) ; errno
= e
; }
191 static void autoreap(void)
196 sa
.sa_handler
= reap
;
197 sigemptyset(&sa
.sa_mask
);
198 sigaddset(&sa
.sa_mask
, SIGCHLD
);
199 sa
.sa_flags
= SA_NOCLDSTOP
;
201 sa
.sa_flags
|= SA_RESTART
;
203 sigaction(SIGCHLD
, &sa
, 0);
206 sigaddset(&ss
, SIGCHLD
);
207 sigprocmask(SIG_BLOCK
, &ss
, &oss
);
209 sigprocmask(SIG_SETMASK
, &oss
, 0);
212 /*----- Command dispatch --------------------------------------------------*/
214 static const struct cmd
{
216 int (*cmd
)(int, char **);
220 { "help", c_help
, "help [COMMANDS...]" },
221 { "get", c_get
, "get PROPERTY" },
222 { "set", c_set
, "set PROPERTY VALUE" },
223 { "delete", c_delete
, "delete PROPERTY" },
224 { "wait", c_wait
, "wait PROPERTY [VALUE...]" },
228 const struct cmd
*findcmd(const char *name
)
230 const struct cmd
*c
, *chosen
= 0;
231 size_t sz
= strlen(name
);
233 for (c
= cmds
; c
->name
; c
++) {
234 if (strncmp(name
, c
->name
, sz
) == 0) {
235 if (c
->name
[sz
] == 0) {
239 die(EXIT_FAILURE
, "ambiguous command name `%s'", name
);
245 die(EXIT_FAILURE
, "unknown command name `%s'", name
);
249 /*----- Help and version information --------------------------------------*/
251 static void version(void)
252 { pquis(stdout
, "$ version " VERSION
"\n"); }
254 static void usage(FILE *fp
)
255 { pquis(fp
, "Usage: $ [-d DISPLAY] SUBCOMMAND [ARGUMENTS...]\n"); }
257 static void help(char **av
)
261 version(); putchar('\n');
265 Sets, retrieves and waits for properties on an X window.\n\
267 Global command-line options:\n\
269 -h, --help [COMMAND] Display this help, or help on COMMAND.\n\
270 -v, --version Display program's version number.\n\
271 -u, --usage Display short usage summary.\n\
273 -d, --display=DISPLAY Connect to X DISPLAY.\n\
274 -w, --window=WINDOW Use properties on WINDOW instead of root.\n\
276 The following subcommands are understood:\n\n",
278 for (c
= cmds
; c
->name
; c
++)
279 printf("%s\n", c
->usage
);
283 printf("Usage: %s [-OPTIONS] %s\n", QUIS
, c
->usage
);
286 pquis(stdout
, c
->help
);
288 if (*av
) putchar('\n');
293 /*----- Main program ------------------------------------------------------*/
295 int main(int argc
, char *argv
[])
297 const char *display
= 0;
305 /* --- Parse arguments --- */
308 static struct option opt
[] = {
309 { "help", 0, 0, 'h' },
310 { "usage", 0, 0, 'u' },
311 { "version", 0, 0, 'v' },
312 { "display", OPTF_ARGREQ
, 0, 'd' },
313 { "window", OPTF_ARGREQ
, 0, 'w' },
317 int i
= mdwopt(argc
, argv
, "+huvd:w:", opt
, 0, 0, 0);
320 case 'h': help(argv
+ optind
); exit(0);
321 case 'u': usage(stdout
); exit(0);
322 case 'v': version(); exit(0);
323 case 'd': display
= optarg
; break;
324 case 'w': win
= optarg
; break;
325 default: f
|= F_BOGUS
; break;
328 if ((f
& F_BOGUS
) || optind
>= argc
) {
333 /* --- Initialize --- */
336 if ((dpy
= XOpenDisplay(display
)) == 0)
337 die(EXIT_FAILURE
, "couldn't open display");
339 /* --- Select a target window --- */
342 window
= DefaultRootWindow(dpy
);
343 else if (strcmp(win
, "choose") == 0)
344 window
= choosewindow();
346 window
= (Window
)strtoul(win
, 0, 0);
348 /* --- Dispatch the command --- */
353 return (findcmd(argv
[0])->cmd(argc
, argv
));
358 /*----- That's all, folks -------------------------------------------------*/