close-window.c 939 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <X11/Xutil.h>
  2. #include <X11/Xlib.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. int close_window(Display *d, XID window) {
  6. XClientMessageEvent ev;
  7. memset(&ev, 0, sizeof(ev));
  8. ev.type = ClientMessage;
  9. ev.display = d;
  10. ev.window = window;
  11. ev.format = 32;
  12. ev.message_type = XInternAtom(d, "WM_PROTOCOLS", False);
  13. ev.data.l[0] = XInternAtom(d, "WM_DELETE_WINDOW", False);;
  14. return XSendEvent(ev.display, ev.window, True, 0, (XEvent *) & ev);
  15. }
  16. int is_window_visible(Display *d, XID window) {
  17. XWindowAttributes xwa;
  18. if (!XGetWindowAttributes(d, window, &xwa))
  19. return 0;
  20. return xwa.map_state == IsViewable;
  21. }
  22. int main(int argc, char **argv) {
  23. int i;
  24. Display *d;
  25. XID w;
  26. d = XOpenDisplay(NULL);
  27. if (!d)
  28. exit(1);
  29. for (i=1; i<argc; i++) {
  30. w = strtoul(argv[i], NULL, 0);
  31. if (is_window_visible(d, w))
  32. close_window(d, w);
  33. }
  34. XSync(d, False);
  35. XCloseDisplay(d);
  36. return 0;
  37. }