master ohmu/wmutils / src / contrib / killwa / util.c
ohmu  ·  2026-07-25
  1#include <err.h>
  2#include <stdlib.h>
  3#include <string.h>
  4#include <xcb/xcb.h>
  5
  6#include "util.h"
  7
  8void
  9init_xcb(xcb_connection_t **con)
 10{
 11	*con = xcb_connect(NULL, NULL);
 12	if (xcb_connection_has_error(*con))
 13		errx(1, "unable connect to the X server");
 14}
 15
 16void
 17kill_xcb(xcb_connection_t **con)
 18{
 19	if (*con)
 20		xcb_disconnect(*con);
 21}
 22
 23void
 24get_screen(xcb_connection_t *con, xcb_screen_t **scr)
 25{
 26	*scr = xcb_setup_roots_iterator(xcb_get_setup(con)).data;
 27	if (*scr == NULL)
 28		errx(1, "unable to retrieve screen informations");
 29}
 30
 31int
 32exists(xcb_connection_t *con, xcb_window_t w)
 33{
 34	xcb_get_window_attributes_cookie_t c;
 35	xcb_get_window_attributes_reply_t  *r;
 36
 37	c = xcb_get_window_attributes(con, w);
 38	r = xcb_get_window_attributes_reply(con, c, NULL);
 39
 40	if (r == NULL)
 41		return 0;
 42
 43	free(r);
 44	return 1;
 45}
 46
 47int
 48mapped(xcb_connection_t *con, xcb_window_t w)
 49{
 50	int ms;
 51	xcb_get_window_attributes_cookie_t c;
 52	xcb_get_window_attributes_reply_t  *r;
 53
 54	c = xcb_get_window_attributes(con, w);
 55	r = xcb_get_window_attributes_reply(con, c, NULL);
 56
 57	if (r == NULL)
 58		return 0;
 59
 60	ms = r->map_state;
 61
 62	free(r);
 63	return ms == XCB_MAP_STATE_VIEWABLE;
 64}
 65
 66int
 67ignore(xcb_connection_t *con, xcb_window_t w)
 68{
 69	int or;
 70	xcb_get_window_attributes_cookie_t c;
 71	xcb_get_window_attributes_reply_t  *r;
 72
 73	c = xcb_get_window_attributes(con, w);
 74	r = xcb_get_window_attributes_reply(con, c, NULL);
 75
 76	if (r == NULL)
 77		return 0;
 78
 79	or = r->override_redirect;
 80
 81	free(r);
 82	return or;
 83}
 84
 85int
 86get_windows(xcb_connection_t *con, xcb_window_t w, xcb_window_t **l)
 87{
 88	uint32_t childnum = 0;
 89	xcb_query_tree_cookie_t c;
 90	xcb_query_tree_reply_t *r;
 91
 92	c = xcb_query_tree(con, w);
 93	r = xcb_query_tree_reply(con, c, NULL);
 94	if (r == NULL)
 95		errx(1, "0x%08x: no such window", w);
 96
 97	*l = malloc(sizeof(xcb_window_t) * r->children_len);
 98	memcpy(*l, xcb_query_tree_children(r),
 99			sizeof(xcb_window_t) * r->children_len);
100
101	childnum = r->children_len;
102
103	free(r);
104	return childnum;
105}