commit a037939

ohmu  ·  2026-07-15 19:04:55 +0300 MSK
parent 19683d6
added some core utils
6 files changed,  +246, -0
A TODO
A TODO
+16, -0
 1@@ -0,0 +1,16 @@
 2+core:
 3+chwb
 4+chwso
 5+mapw
 6+wmp
 7+wmv
 8+wrs
 9+wtf
10+wtp
11+
12+opt:
13+chwb2
14+mwmd
15+mwtitle
16+wew
17+wname
+24, -0
 1@@ -0,0 +1,24 @@
 2+CC      = clang
 3+CFLAGS  = -O2 -pipe -std=c99 -D_POSIX_C_SOURCE=200809L
 4+LDFLAGS = -lxcb
 5+PREFIX  = /usr/local
 6+
 7+BIN = wa fw lw ra
 8+SRC = wa.c fw.c lw.c ra.c
 9+
10+all: $(BIN)
11+
12+%: %.c
13+	$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)
14+
15+install: all
16+	mkdir -p $(DESTDIR)$(PREFIX)/bin
17+	cp $(BIN) $(DESTDIR)$(PREFIX)/bin/
18+
19+clean:
20+	rm -f $(BIN) *.o
21+
22+uninstall:
23+	cd $(DESTDIR)$(PREFIX)/bin && rm -f $(BIN)
24+
25+.PHONY: all clean install uninstall
+21, -0
 1@@ -0,0 +1,21 @@
 2+// focused window
 3+
 4+#include <stdio.h>
 5+#include <xcb/xcb.h>
 6+#include <stdlib.h>
 7+
 8+xcb_connection_t *connection;
 9+
10+int
11+main(int argc, char *argv[]) 
12+{
13+    xcb_get_input_focus_cookie_t cookie;
14+    xcb_get_input_focus_reply_t *reply;
15+    connection = xcb_connect(NULL,NULL);
16+    cookie = xcb_get_input_focus(connection);
17+    reply = xcb_get_input_focus_reply(connection, cookie, NULL);
18+    printf("0x%08x\n", reply->focus);
19+    free(reply);
20+    xcb_disconnect(connection);
21+    return 0;
22+}
+75, -0
 1@@ -0,0 +1,75 @@
 2+// list windows
 3+
 4+#include <stdio.h>
 5+#include <xcb/xcb.h>
 6+#include <stdlib.h>
 7+#include <string.h>
 8+
 9+xcb_connection_t *connection;
10+xcb_screen_t *screen;
11+xcb_get_window_attributes_cookie_t cookie;
12+xcb_get_window_attributes_reply_t  *reply;
13+
14+int
15+mapped(xcb_window_t window)
16+{
17+    int map_state;
18+	cookie = xcb_get_window_attributes(connection, window);
19+	reply = xcb_get_window_attributes_reply(connection, cookie, NULL);
20+    if (!reply)
21+        return 0;
22+    map_state = reply->map_state;
23+    free(reply);
24+	return map_state == XCB_MAP_STATE_VIEWABLE;
25+}
26+
27+int
28+ignore(xcb_window_t window)
29+{
30+    int override_redirect;
31+	cookie = xcb_get_window_attributes(connection, window);
32+	reply = xcb_get_window_attributes_reply(connection, cookie, NULL);
33+    if (!reply)
34+        return 0;
35+    override_redirect = reply->override_redirect;
36+    free(reply);
37+    return override_redirect;
38+}
39+
40+int
41+get_windows(xcb_window_t window, xcb_window_t **window_children)
42+{
43+    int children_len;
44+	xcb_query_tree_cookie_t tree_cookie;
45+	xcb_query_tree_reply_t *tree_reply;
46+
47+	tree_cookie = xcb_query_tree(connection, window);
48+	tree_reply = xcb_query_tree_reply(connection, tree_cookie, NULL);
49+    if (!tree_reply)
50+        return 0;
51+	*window_children = malloc(sizeof(xcb_window_t) * tree_reply->children_len);
52+	memcpy(*window_children, xcb_query_tree_children(tree_reply),
53+			sizeof(xcb_window_t) * tree_reply->children_len);
54+    children_len = tree_reply->children_len;
55+    return children_len;
56+}
57+
58+int
59+main(int argc, char *argv[]) 
60+{
61+    int child_windows_count;
62+    xcb_window_t *window_children;
63+
64+    connection = xcb_connect(NULL,NULL);
65+    screen = xcb_setup_roots_iterator(xcb_get_setup(connection)).data;
66+    child_windows_count = get_windows(screen->root, &window_children);
67+    for (int i = 0; i < child_windows_count; i++) {
68+        if (mapped(window_children[i]) && !ignore(window_children[i])) {
69+            printf("0x%08x\n", window_children[i]);
70+        }
71+    }
72+
73+    free(window_children);
74+    xcb_disconnect(connection);
75+    return 0;
76+}
+46, -0
 1@@ -0,0 +1,46 @@
 2+// root attributes
 3+
 4+#include <stdio.h>
 5+#include <xcb/xcb.h>
 6+#include <stdlib.h>
 7+#include <unistd.h>
 8+
 9+xcb_connection_t *connection;
10+
11+int
12+get_root_info(char attr_name)
13+{
14+    int attr;
15+    const xcb_setup_t *setup = xcb_get_setup(connection);
16+    xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
17+    xcb_screen_t *screen = iter.data;
18+    switch (attr_name) {
19+        case 'w':
20+            attr = screen->width_in_pixels;
21+            break;
22+        case 'h':
23+            attr = screen->height_in_pixels;
24+            break;
25+        case '?':
26+            fprintf(stderr, "wrong attr\n");
27+            exit(1);
28+    }
29+    return attr;
30+}
31+
32+int
33+main(int argc, char *argv[]) 
34+{
35+    int opt;
36+    if (argc < 2) {
37+        fprintf(stderr, "usage: %s -wh\n", argv[0]);
38+        exit(1);
39+    }
40+    connection = xcb_connect(NULL, NULL);
41+    while ((opt = getopt(argc, argv, "wh")) != -1) {
42+        printf("%d ", get_root_info(opt));
43+    }
44+    printf("\n");
45+    xcb_disconnect(connection);
46+    return 0;
47+}
+64, -0
 1@@ -0,0 +1,64 @@
 2+// window attributes
 3+
 4+#include <stdio.h>
 5+#include <stdlib.h>
 6+#include <xcb/xcb.h>
 7+#include <unistd.h>
 8+
 9+xcb_connection_t *connection;
10+
11+int
12+get_window_info(xcb_window_t window, char attr_name)
13+{
14+    int attr;
15+    xcb_get_geometry_cookie_t cookie;
16+    xcb_get_geometry_reply_t *reply;
17+    cookie = xcb_get_geometry(connection, window);
18+    reply = xcb_get_geometry_reply(connection, cookie, NULL);
19+    switch (attr_name) {
20+        case 'x':
21+            attr = reply->x;
22+            break;
23+        case 'y':
24+            attr = reply->y;
25+            break;
26+        case 'w':
27+            attr = reply->width;
28+            break;
29+        case 'h':
30+            attr = reply->height;
31+            break;
32+        case 'b':
33+            attr = reply->border_width;
34+            break;
35+        case '?':
36+            fprintf(stderr, "wrong attr\n");
37+            free(reply);
38+            exit(1);
39+     }
40+    free(reply);
41+    return attr;
42+}
43+
44+int
45+main(int argc, char *argv[]) 
46+{
47+    int opt;
48+    if (argc < 2) {
49+        fprintf(stderr, "usage: %s -xywhb winid\n", argv[0]);
50+        exit(1);
51+    }
52+    xcb_window_t window;
53+    connection = xcb_connect(NULL,NULL);
54+    window = strtoul(argv[argc-1], NULL, 16);
55+    if (!window) {
56+        fprintf(stderr, "window id, please\n");
57+        exit(1);
58+    }
59+    while ((opt = getopt(argc, argv, "xywhb")) != -1) {
60+        printf("%d ", get_window_info(window, opt));
61+    }
62+    printf("\n");
63+    xcb_disconnect(connection);
64+    return 0;
65+}