master ohmu/mwmutils / core / lw.c
ohmu  ·  2026-07-15
 1// list windows
 2
 3#include <stdio.h>
 4#include <xcb/xcb.h>
 5#include <stdlib.h>
 6#include <string.h>
 7
 8xcb_connection_t *connection;
 9xcb_screen_t *screen;
10xcb_get_window_attributes_cookie_t cookie;
11xcb_get_window_attributes_reply_t  *reply;
12
13int
14mapped(xcb_window_t window)
15{
16    int map_state;
17	cookie = xcb_get_window_attributes(connection, window);
18	reply = xcb_get_window_attributes_reply(connection, cookie, NULL);
19    if (!reply)
20        return 0;
21    map_state = reply->map_state;
22    free(reply);
23	return map_state == XCB_MAP_STATE_VIEWABLE;
24}
25
26int
27ignore(xcb_window_t window)
28{
29    int override_redirect;
30	cookie = xcb_get_window_attributes(connection, window);
31	reply = xcb_get_window_attributes_reply(connection, cookie, NULL);
32    if (!reply)
33        return 0;
34    override_redirect = reply->override_redirect;
35    free(reply);
36    return override_redirect;
37}
38
39int
40get_windows(xcb_window_t window, xcb_window_t **window_children)
41{
42    int children_len;
43	xcb_query_tree_cookie_t tree_cookie;
44	xcb_query_tree_reply_t *tree_reply;
45
46	tree_cookie = xcb_query_tree(connection, window);
47	tree_reply = xcb_query_tree_reply(connection, tree_cookie, NULL);
48    if (!tree_reply)
49        return 0;
50	*window_children = malloc(sizeof(xcb_window_t) * tree_reply->children_len);
51	memcpy(*window_children, xcb_query_tree_children(tree_reply),
52			sizeof(xcb_window_t) * tree_reply->children_len);
53    children_len = tree_reply->children_len;
54    return children_len;
55}
56
57int
58main(int argc, char *argv[]) 
59{
60    int child_windows_count;
61    xcb_window_t *window_children;
62
63    connection = xcb_connect(NULL,NULL);
64    screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
65    child_windows_count = get_windows(screen->root, &window_children);
66    for (int i = 0; i < child_windows_count; i++) {
67        if (mapped(window_children[i]) && !ignore(window_children[i])) {
68            printf("0x%08x\n", window_children[i]);
69        }
70    }
71
72    free(window_children);
73    xcb_disconnect(connection);
74    return 0;
75}