File: lsw.c

package info (click to toggle)
suckless-tools 48-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 948 kB
  • sloc: ansic: 5,415; makefile: 490; sh: 80
file content (66 lines) | stat: -rw-r--r-- 1,481 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

static const char *getname(Window);
static void lsw(Window);

static Atom netwmname;
static Display *dpy;

int
main(int argc, char *argv[]) {
	int i;

	if(!(dpy = XOpenDisplay(NULL))) {
		fprintf(stderr, "%s: cannot open display\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	netwmname = XInternAtom(dpy, "_NET_WM_NAME", False);

	if(argc < 2)
		lsw(DefaultRootWindow(dpy));
	else for(i = 1; i < argc; i++)
		lsw(strtol(argv[i], NULL, 0));

	XCloseDisplay(dpy);
	return EXIT_SUCCESS;
}

void
lsw(Window win) {
	unsigned int n;
	Window *wins, *w, dw;
	XWindowAttributes wa;

	if(!XQueryTree(dpy, win, &dw, &dw, &wins, &n))
		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));
	XFree(wins);
}

const char *
getname(Window win) {
	static char buf[BUFSIZ];
	char **list;
	int n;
	XTextProperty prop;

	if(!XGetTextProperty(dpy, win, &prop, netwmname) || prop.nitems == 0)
		if(!XGetWMName(dpy, win, &prop) || prop.nitems == 0)
			return "";
	if(!XmbTextPropertyToTextList(dpy, &prop, &list, &n) && n > 0) {
		strncpy(buf, list[0], sizeof buf);
		XFreeStringList(list);
	} else
		strncpy(buf, (char *)prop.value, sizeof buf);
	XFree(prop.value);
	buf[sizeof buf - 1] = '\0';
	return buf;
}