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