lsw.c (1466B)
1 /* See LICENSE file for copyright and license details. */ 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <X11/Xlib.h> 6 #include <X11/Xutil.h> 7 8 static Atom netwmname; 9 static Display *dpy; 10 11 static char * 12 getname(Window win) 13 { 14 static char buf[BUFSIZ]; 15 char **list; 16 int n; 17 XTextProperty prop; 18 19 if (!XGetTextProperty(dpy, win, &prop, netwmname) || !prop.nitems) { 20 if (!XGetWMName(dpy, win, &prop) || !prop.nitems) 21 return ""; 22 } 23 if (!XmbTextPropertyToTextList(dpy, &prop, &list, &n) && n > 0) { 24 strncpy(buf, list[0], sizeof(buf)); 25 XFreeStringList(list); 26 } else { 27 strncpy(buf, (char *)prop.value, sizeof(buf)); 28 } 29 XFree(prop.value); 30 buf[sizeof(buf) - 1] = '\0'; 31 32 return buf; 33 } 34 35 static void 36 lsw(Window win) 37 { 38 unsigned int n; 39 Window *wins, *w, dw; 40 XWindowAttributes wa; 41 42 if (!XQueryTree(dpy, win, &dw, &dw, &wins, &n) || !n) 43 return; 44 for (w = &wins[n-1]; w >= &wins[0]; w--) { 45 if (XGetWindowAttributes(dpy, *w, &wa) 46 && !wa.override_redirect && wa.map_state == IsViewable) 47 printf("0x%07lx %s\n", *w, getname(*w)); 48 } 49 XFree(wins); 50 } 51 52 int 53 main(int argc, char *argv[]) 54 { 55 int i; 56 57 if (!(dpy = XOpenDisplay(NULL))) { 58 fprintf(stderr, "%s: cannot open display\n", argv[0]); 59 exit(EXIT_FAILURE); 60 } 61 netwmname = XInternAtom(dpy, "_NET_WM_NAME", False); 62 63 if (argc < 2) { 64 lsw(DefaultRootWindow(dpy)); 65 } else { 66 for (i = 1; i < argc; i++) 67 lsw(strtol(argv[i], NULL, 0)); 68 } 69 70 XCloseDisplay(dpy); 71 72 return EXIT_SUCCESS; 73 }