xkill.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. Copyright 1988, 1998 The Open Group
  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.
  8. The above copyright notice and this permission notice shall be included
  9. in all copies or substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  12. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  13. IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
  14. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  15. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  16. OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall
  18. not be used in advertising or otherwise to promote the sale, use or
  19. other dealings in this Software without prior written authorization
  20. from The Open Group.
  21. */
  22. /*
  23. * xkill - simple program for destroying unwanted clients
  24. * Author: Jim Fulton, MIT X Consortium; Dana Chee, Bellcore
  25. */
  26. /*
  27. * Warning, this is a very dangerous client....
  28. */
  29. #ifdef HAVE_CONFIG_H
  30. # include "config.h"
  31. #endif
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <ctype.h>
  35. #include <X11/Xos.h>
  36. #include <X11/Xlib.h>
  37. #include <X11/cursorfont.h>
  38. #include <X11/Xproto.h>
  39. #include <X11/Xmu/WinUtil.h>
  40. static char *ProgramName;
  41. #define SelectButtonAny (-1)
  42. #define SelectButtonFirst (-2)
  43. static int parse_button ( char *s, int *buttonp );
  44. static XID get_window_id ( Display *dpy, int screen, int button, const char *msg );
  45. static int catch_window_errors ( Display *dpy, XErrorEvent *ev );
  46. static int kill_all_windows ( Display *dpy, int screenno, Bool top );
  47. static int verify_okay_to_kill ( Display *dpy, int screenno );
  48. static Bool wm_state_set ( Display *dpy, Window win );
  49. static Bool wm_running ( Display *dpy, int screenno );
  50. static void _X_NORETURN
  51. Exit(int code, Display *dpy)
  52. {
  53. if (dpy) {
  54. XCloseDisplay (dpy);
  55. }
  56. exit (code);
  57. }
  58. static void _X_NORETURN
  59. usage(void)
  60. {
  61. const char *options =
  62. "where options include:\n"
  63. " -display displayname X server to contact\n"
  64. " -id resource resource whose client is to be killed\n"
  65. " -frame don't ignore window manager frames\n"
  66. " -button number specific button to be pressed to select window\n"
  67. " -all kill all clients with top level windows\n"
  68. " -version print version and exit\n"
  69. "\n";
  70. fprintf (stderr, "usage: %s [-option ...]\n%s",
  71. ProgramName, options);
  72. Exit (1, NULL);
  73. }
  74. int
  75. main(int argc, char *argv[])
  76. {
  77. int i; /* iterator, temp variable */
  78. Display *dpy = NULL;
  79. char *displayname = NULL; /* name of server to contact */
  80. int screenno; /* screen number of dpy */
  81. XID id = None; /* resource to kill */
  82. char *button_name = NULL; /* name of button for window select */
  83. int button; /* button number or negative for all */
  84. Bool kill_all = False;
  85. Bool top = False;
  86. ProgramName = argv[0];
  87. button = SelectButtonFirst;
  88. for (i = 1; i < argc; i++) {
  89. char *arg = argv[i];
  90. if (arg[0] == '-') {
  91. switch (arg[1]) {
  92. case 'd': /* -display displayname */
  93. if (++i >= argc) usage ();
  94. displayname = argv[i];
  95. continue;
  96. case 'i': /* -id resourceid */
  97. if (++i >= argc) usage ();
  98. id = strtoul (argv[i], NULL, 0);
  99. if (id == 0 || id >= 0xFFFFFFFFU) {
  100. fprintf (stderr, "%s: invalid id \"%s\"\n",
  101. ProgramName, argv[i]);
  102. Exit (1, dpy);
  103. }
  104. continue;
  105. case 'b': /* -button number */
  106. if (++i >= argc) usage ();
  107. button_name = argv[i];
  108. continue;
  109. case 'f': /* -frame */
  110. top = True;
  111. continue;
  112. case 'a': /* -all */
  113. kill_all = True;
  114. continue;
  115. case 'v':
  116. puts(PACKAGE_STRING);
  117. exit(0);
  118. default:
  119. usage ();
  120. }
  121. } else {
  122. usage ();
  123. }
  124. } /* end for */
  125. dpy = XOpenDisplay (displayname);
  126. if (!dpy) {
  127. fprintf (stderr, "%s: unable to open display \"%s\"\n",
  128. ProgramName, XDisplayName (displayname));
  129. Exit (1, dpy);
  130. }
  131. screenno = DefaultScreen (dpy);
  132. if (kill_all) {
  133. if (verify_okay_to_kill (dpy, screenno))
  134. kill_all_windows (dpy, screenno, top);
  135. Exit (0, dpy);
  136. }
  137. /*
  138. * if no id was given, we need to choose a window
  139. */
  140. if (id == None) {
  141. if (!button_name)
  142. button_name = XGetDefault (dpy, ProgramName, "Button");
  143. if (button_name && !parse_button (button_name, &button)) {
  144. fprintf (stderr, "%s: invalid button specification \"%s\"\n",
  145. ProgramName, button_name);
  146. Exit (1, dpy);
  147. }
  148. if (button >= 0 || button == SelectButtonFirst) {
  149. unsigned char pointer_map[256]; /* 8 bits of pointer num */
  150. int count, j;
  151. unsigned int ub = (unsigned int) button;
  152. count = XGetPointerMapping (dpy, pointer_map, 256);
  153. if (count <= 0) {
  154. fprintf (stderr,
  155. "%s: no pointer mapping, can't select window\n",
  156. ProgramName);
  157. Exit (1, dpy);
  158. }
  159. if (button >= 0) { /* check button */
  160. for (j = 0; j < count; j++) {
  161. if (ub == (unsigned int) pointer_map[j]) break;
  162. }
  163. if (j == count) {
  164. fprintf (stderr,
  165. "%s: no button number %u in pointer map, can't select window\n",
  166. ProgramName, ub);
  167. Exit (1, dpy);
  168. }
  169. } else { /* get first entry */
  170. button = (int) ((unsigned int) pointer_map[0]);
  171. }
  172. }
  173. if ((id = get_window_id (dpy, screenno, button,
  174. "the window whose client you wish to kill"))) {
  175. if (id == RootWindow(dpy,screenno)) id = None;
  176. else if (!top) {
  177. XID indicated = id;
  178. if ((id = XmuClientWindow(dpy, indicated)) == indicated) {
  179. /* Try not to kill the window manager when the user
  180. * indicates an icon to xkill.
  181. */
  182. if (! wm_state_set(dpy, id) && wm_running(dpy, screenno))
  183. id = None;
  184. }
  185. }
  186. }
  187. }
  188. if (id != None) {
  189. printf ("%s: killing creator of resource 0x%lx\n", ProgramName, id);
  190. XSync (dpy, 0); /* give xterm a chance */
  191. XKillClient (dpy, id);
  192. XSync (dpy, 0);
  193. }
  194. Exit (0, dpy);
  195. /*NOTREACHED*/
  196. return 0;
  197. }
  198. static int
  199. parse_button(char *s, int *buttonp)
  200. {
  201. register char *cp;
  202. /* lower case name */
  203. for (cp = s; *cp; cp++) {
  204. if (isascii (*cp) && isupper (*cp)) {
  205. #ifdef _tolower
  206. *cp = (char) _tolower (*cp);
  207. #else
  208. *cp = (char) tolower (*cp);
  209. #endif /* _tolower */
  210. }
  211. }
  212. if (strcmp (s, "any") == 0) {
  213. *buttonp = SelectButtonAny;
  214. return (1);
  215. }
  216. /* check for non-numeric input */
  217. for (cp = s; *cp; cp++) {
  218. if (!(isascii (*cp) && isdigit (*cp))) return (0); /* bogus name */
  219. }
  220. *buttonp = atoi (s);
  221. return (1);
  222. }
  223. static XID
  224. get_window_id(Display *dpy, int screen, int button, const char *msg)
  225. {
  226. Cursor cursor; /* cursor to use when selecting */
  227. Window root; /* the current root */
  228. Window retwin = None; /* the window that got selected */
  229. int retbutton = -1; /* button used to select window */
  230. int pressed = 0; /* count of number of buttons pressed */
  231. #define MASK (ButtonPressMask | ButtonReleaseMask)
  232. root = RootWindow (dpy, screen);
  233. cursor = XCreateFontCursor (dpy, XC_pirate);
  234. if (cursor == None) {
  235. fprintf (stderr, "%s: unable to create selection cursor\n",
  236. ProgramName);
  237. Exit (1, dpy);
  238. }
  239. printf ("Select %s with ", msg);
  240. if (button == -1)
  241. printf ("any button");
  242. else
  243. printf ("button %d", button);
  244. printf ("....\n");
  245. XSync (dpy, 0); /* give xterm a chance */
  246. if (XGrabPointer (dpy, root, False, MASK, GrabModeSync, GrabModeAsync,
  247. None, cursor, CurrentTime) != GrabSuccess) {
  248. fprintf (stderr, "%s: unable to grab cursor\n", ProgramName);
  249. Exit (1, dpy);
  250. }
  251. /* from dsimple.c in xwininfo */
  252. while (retwin == None || pressed != 0) {
  253. XEvent event;
  254. XAllowEvents (dpy, SyncPointer, CurrentTime);
  255. XWindowEvent (dpy, root, MASK, &event);
  256. switch (event.type) {
  257. case ButtonPress:
  258. if (retwin == None) {
  259. retbutton = event.xbutton.button;
  260. retwin = ((event.xbutton.subwindow != None) ?
  261. event.xbutton.subwindow : root);
  262. }
  263. pressed++;
  264. continue;
  265. case ButtonRelease:
  266. if (pressed > 0) pressed--;
  267. continue;
  268. } /* end switch */
  269. } /* end for */
  270. XUngrabPointer (dpy, CurrentTime);
  271. XFreeCursor (dpy, cursor);
  272. XSync (dpy, 0);
  273. return ((button == -1 || retbutton == button) ? retwin : None);
  274. }
  275. static int
  276. catch_window_errors(_X_UNUSED Display *dpy, _X_UNUSED XErrorEvent *ev)
  277. {
  278. return 0;
  279. }
  280. static int
  281. kill_all_windows(Display *dpy, int screenno, Bool top)
  282. {
  283. Window root = RootWindow (dpy, screenno);
  284. Window dummywindow;
  285. Window *children;
  286. unsigned int nchildren;
  287. unsigned int i;
  288. XWindowAttributes attr;
  289. XSync (dpy, 0);
  290. XSetErrorHandler (catch_window_errors);
  291. nchildren = 0;
  292. XQueryTree (dpy, root, &dummywindow, &dummywindow, &children, &nchildren);
  293. if (!top) {
  294. for (i = 0; i < nchildren; i++) {
  295. if (XGetWindowAttributes(dpy, children[i], &attr) &&
  296. (attr.map_state == IsViewable))
  297. children[i] = XmuClientWindow(dpy, children[i]);
  298. else
  299. children[i] = 0;
  300. }
  301. }
  302. for (i = 0; i < nchildren; i++) {
  303. if (children[i])
  304. XKillClient (dpy, children[i]);
  305. }
  306. XFree ((char *)children);
  307. XSync (dpy, 0);
  308. XSetErrorHandler (NULL); /* pretty stupid way to do things... */
  309. return 0;
  310. }
  311. /*
  312. * ask the user to press in the root with each button in succession
  313. */
  314. static int
  315. verify_okay_to_kill(Display *dpy, int screenno)
  316. {
  317. unsigned char pointer_map[256];
  318. int count = XGetPointerMapping (dpy, pointer_map, 256);
  319. int i;
  320. int button;
  321. const char *msg = "the root window";
  322. Window root = RootWindow (dpy, screenno);
  323. int okay = 0;
  324. for (i = 0; i < count; i++) {
  325. button = (int) pointer_map[i];
  326. if (button == 0) continue; /* disabled */
  327. if (get_window_id (dpy, screenno, button, msg) != root) {
  328. okay = 0;
  329. break;
  330. }
  331. okay++; /* must have at least one button */
  332. }
  333. if (okay) {
  334. return 1;
  335. } else {
  336. printf ("Aborting.\n");
  337. return 0;
  338. }
  339. }
  340. /* Return True if the property WM_STATE is set on the window, otherwise
  341. * return False.
  342. */
  343. static Bool
  344. wm_state_set(Display *dpy, Window win)
  345. {
  346. Atom wm_state;
  347. Atom actual_type;
  348. int success;
  349. int actual_format;
  350. unsigned long nitems, remaining;
  351. unsigned char* prop = NULL;
  352. wm_state = XInternAtom(dpy, "WM_STATE", True);
  353. if (wm_state == None) return False;
  354. success = XGetWindowProperty(dpy, win, wm_state, 0L, 0L, False,
  355. AnyPropertyType, &actual_type, &actual_format,
  356. &nitems, &remaining, &prop);
  357. if (prop) XFree((char *) prop);
  358. return (success == Success && actual_type != None && actual_format);
  359. }
  360. /* Using a heuristic method, return True if a window manager is running,
  361. * otherwise, return False.
  362. */
  363. static Bool
  364. wm_running(Display *dpy, int screenno)
  365. {
  366. XWindowAttributes xwa;
  367. Status status;
  368. status = XGetWindowAttributes(dpy, RootWindow(dpy, screenno), &xwa);
  369. return (status &&
  370. ((xwa.all_event_masks & SubstructureRedirectMask) ||
  371. (xwa.all_event_masks & SubstructureNotifyMask)));
  372. }