lsw

lists window titles of X clients to stdout
git clone git://git.suckless.org/lsw
Log | Files | Refs | README | LICENSE

commit 0ae7a4520d111c776a2dc1136f7a4c6fcebb056d
parent 4d98d964ba18e713967c9f1e046540b254071da8
Author: Connor Lane Smith <cls@lubutu.com>
Date:   Sat, 18 Jun 2011 18:47:03 +0100

reverse list
Diffstat:
lsw.c | 32+++++++++++++++-----------------
1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/lsw.c b/lsw.c @@ -5,8 +5,8 @@ #include <X11/Xlib.h> #include <X11/Xutil.h> -static void getname(Window win, char *buf, size_t size); -static void lsw(Window win); +static const char *getname(Window); +static void lsw(Window); static Atom netwmname; static Display *dpy; @@ -32,37 +32,35 @@ main(int argc, char *argv[]) { void lsw(Window win) { - char buf[BUFSIZ]; - unsigned int i, n; - Window *wins, dw; + unsigned int n; + Window *wins, *w, dw; XWindowAttributes wa; if(!XQueryTree(dpy, win, &dw, &dw, &wins, &n)) return; - for(i = 0; i < n; i++) - if(XGetWindowAttributes(dpy, wins[i], &wa) - && !wa.override_redirect && wa.map_state == IsViewable) { - getname(wins[i], buf, sizeof buf); - printf("0x%07lx %s\n", wins[i], buf); - } + for(w = &wins[n-1]; w >= &wins[0]; w--) + if(XGetWindowAttributes(dpy, *w, &wa) + && !wa.override_redirect && wa.map_state == IsViewable) + printf("0x%07lx %s\n", *w, getname(*w)); XFree(wins); } -void -getname(Window win, char *buf, size_t size) { +const char * +getname(Window win) { + static char buf[BUFSIZ]; char **list; int n; XTextProperty prop; - buf[0] = '\0'; if(!XGetTextProperty(dpy, win, &prop, netwmname) || prop.nitems == 0) if(!XGetWMName(dpy, win, &prop) || prop.nitems == 0) - return; + return ""; if(!XmbTextPropertyToTextList(dpy, &prop, &list, &n) && n > 0) { - strncpy(buf, list[0], size); + strncpy(buf, list[0], sizeof buf); XFreeStringList(list); } else - strncpy(buf, (char *)prop.value, size); + strncpy(buf, (char *)prop.value, sizeof buf); XFree(prop.value); + return buf; }