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