[hackers] [lsw][PATCH] Only list names for windows with WM_STATE set.

From: Samuel Fadel <samuelfadel_AT_gmail.com>
Date: Mon, 25 Apr 2016 13:52:02 -0300

Checking whether WM_STATE is set allows us to determine if this is a "proper"
client window. This usually means that it has a _NET_WM_NAME (this is done by
tools such as xprop) that is not empty.

In addition, we set the locale explicitly, allowing the
XmbTextPropertyToTextList function to retrieve properly encoded strings.
---
 lsw.c | 31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)
diff --git a/lsw.c b/lsw.c
index fc40fef..88160b4 100644
--- a/lsw.c
+++ b/lsw.c
_AT_@ -1,4 +1,5 @@
 /* See LICENSE file for copyright and license details. */
+#include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
_AT_@ -6,9 +7,10 @@
 #include <X11/Xutil.h>
 
 static const char *getname(Window);
+static Bool hasproperty(Window, Atom);
 static void lsw(Window);
 
-static Atom netwmname;
+static Atom netwmname, wmstate;
 static Display *dpy;
 
 int
_AT_@ -20,6 +22,8 @@ main(int argc, char *argv[]) {
 		exit(EXIT_FAILURE);
 	}
 	netwmname = XInternAtom(dpy, "_NET_WM_NAME", False);
+	wmstate = XInternAtom(dpy, "WM_STATE", False);
+	setlocale(LC_ALL, "");
 
 	if(argc < 2)
 		lsw(DefaultRootWindow(dpy));
_AT_@ -40,8 +44,13 @@ lsw(Window win) {
 		return;
 	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));
+		&& !wa.override_redirect && wa.map_state == IsViewable) {
+			/* if WM_STATE is not set, look for children */
+			if(!hasproperty(*w, wmstate))
+				lsw(*w);
+			else
+				printf("0x%07lx %s\n", *w, getname(*w));
+		}
 	XFree(wins);
 }
 
_AT_@ -64,3 +73,19 @@ getname(Window win) {
 	buf[sizeof buf - 1] = '\0';
 	return buf;
 }
+
+Bool
+hasproperty(Window win, Atom atom) {
+	Atom atom_type;
+	int format;
+	unsigned char *prop;
+	unsigned long bytes_after, num;
+
+	atom_type = None;
+	prop = NULL;
+	XGetWindowProperty(dpy, win, atom, 0, 0, False, AnyPropertyType,
+			&atom_type, &format, &num, &bytes_after, &prop);
+	if (prop)
+		XFree(prop);
+	return (atom_type != None) ? True : False;
+}
-- 
2.8.0
Received on Mon Apr 25 2016 - 18:52:02 CEST

This archive was generated by hypermail 2.3.0 : Mon Apr 25 2016 - 19:00:16 CEST