master ohmu/wmutils / src / opt / wname.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 <xcb/xcb.h>
 6#include <err.h>
 7
 8#include "util.h"
 9
10static xcb_connection_t *conn;
11
12static void usage(char *);
13static int get_title(xcb_window_t);
14
15static void
16usage(char *name)
17{
18	fprintf(stderr, "usage: %s <wid>\n", name);
19	exit(1);
20}
21
22static int
23get_title(xcb_window_t win)
24{
25	int len = 0;
26	xcb_get_property_cookie_t cookie;
27	xcb_get_property_reply_t *r;
28
29	cookie = xcb_get_property(conn, 0, win,
30			XCB_ATOM_WM_NAME, XCB_ATOM_ANY, 0L, 32L);
31	r = xcb_get_property_reply(conn, cookie, NULL);
32
33	if (r) {
34		len = xcb_get_property_value_length(r);
35		if (!len)
36			return 1;
37
38		printf("%.*s\n", len, (char *) xcb_get_property_value(r));
39
40		free(r);
41		return 0;
42	}
43
44	warnx("could not get window title");
45	return 1;
46}
47
48int
49main(int argc, char **argv)
50{
51	int i, r = 0;
52
53	if (argc < 2)
54		usage(argv[0]);
55
56	init_xcb(&conn);
57
58	for (i=1; i < argc; i++)
59		r += get_title(strtoul(argv[i], NULL, 16));
60
61	kill_xcb(&conn);
62
63	return r;
64}