master ohmu/wmutils / src / core / lsw.c
ohmu  ·  2026-07-25
 1/* See LICENSE file for copyright and license details. */
 2
 3#include <stdio.h>
 4#include <stdlib.h>
 5#include <err.h>
 6#include <xcb/xcb.h>
 7#include <xcb/xcb_aux.h>
 8
 9#include "arg.h"
10#include "util.h"
11
12static xcb_connection_t *conn;
13static xcb_screen_t *scrn;
14
15static void usage(char *);
16static int should_list(xcb_window_t, int);
17static void list_windows(xcb_window_t, int);
18
19enum {
20	LIST_HIDDEN = 1 << 0,
21	LIST_IGNORE = 1 << 1,
22	LIST_ALL    = 1 << 2
23};
24
25static void
26usage(char *name)
27{
28	fprintf(stderr, "usage: %s [-houra] [wid...]\n", name);
29	exit(1);
30}
31
32static int
33should_list(xcb_window_t w, int mask)
34{
35	if ((mask & LIST_ALL)
36		|| (!mapped(conn, w) && mask & LIST_HIDDEN)
37		|| (ignore(conn, w) && mask & LIST_IGNORE)
38		|| (mapped(conn, w)
39			&& !ignore(conn, w)
40			&& mask == 0))
41		return 1;
42
43	return 0;
44}
45
46static void
47list_windows(xcb_window_t w, int listmask)
48{
49	int i, wn;
50	xcb_window_t *wc;
51
52	wn = get_windows(conn, w, &wc);
53
54	if (wc == NULL)
55		errx(1, "0x%08x: unable to retrieve children", w);
56
57	for (i=0; i<wn; i++) {
58		if (should_list(wc[i], listmask))
59			printf("0x%08x\n", wc[i]);
60	}
61
62	free(wc);
63}
64
65int
66main(int argc, char **argv)
67{
68	int listmask = 0, rootflag = 0;
69	char *argv0;
70
71	ARGBEGIN {
72		case 'a': listmask |= LIST_ALL; break;
73		case 'u': listmask |= LIST_HIDDEN; break;
74		case 'o': listmask |= LIST_IGNORE; break;
75		case 'r': rootflag = 1; break;
76		default : usage(argv0);
77	} ARGEND;
78
79	init_xcb(&conn);
80	get_screen(conn, &scrn);
81
82	if (rootflag == 1) {
83		printf("0x%08x\n", scrn->root);
84		return 0;
85	}
86
87	if (argc == 0)
88		list_windows(scrn->root, listmask);
89
90	while (*argv)
91		list_windows(strtoul(*argv++, NULL, 16), listmask);
92
93	kill_xcb(&conn);
94
95	return 0;
96}