-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxutil.c
165 lines (137 loc) · 4.42 KB
/
xutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Copyright (c) 2013 John Vogel, see README for licence details */
/* xutil.c -- x utility functions. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xcb_util.h>
#include <xcb/xcb_image.h>
#include <xcb/xcb_icccm.h>
#include "dat.h"
#include "fns.h"
xcb_atom_t xinternatom(xcb_connection_t *c, char *name, uint8_t only_if_exists)
{
xcb_atom_t atom;
xcb_intern_atom_cookie_t cookie;
xcb_intern_atom_reply_t *reply;
xcb_generic_error_t *errorp;
cookie = xcb_intern_atom(c, only_if_exists, strlen(name), name);
reply = xcb_intern_atom_reply(c, cookie, &errorp);
if (reply) {
return reply->atom;
free(reply);
}
else if (errorp)
handler(errorp);
return 0;
}
xcb_query_font_reply_t *xloadqueryfont (xcb_connection_t *c, char *fname, xcb_font_t *ret)
{
xcb_void_cookie_t cookie;
xcb_generic_error_t *errorp;
xcb_query_font_cookie_t qf_c;
xcb_query_font_reply_t *qf_r;
xcb_font_t font;
font = xcb_generate_id(c);
cookie = xcb_open_font_checked(c, font, strlen(fname), fname);
errorp = xcb_request_check(c, cookie);
if (errorp)
return NULL;
qf_c = xcb_query_font(c, font);
qf_r = xcb_query_font_reply(c, qf_c, &errorp);
if (!qf_r) {
xcb_close_font(c, font);
return NULL;
}
*ret = font;
return qf_r;
}
void xselectinput(xcb_connection_t *c, xcb_window_t w, uint32_t mask)
{
xcb_change_window_attributes(c, w, XCB_CW_EVENT_MASK, &mask);
}
void xmovewindow(xcb_connection_t *c, xcb_window_t w, int x, int y)
{
xcb_params_configure_window_t wc;
wc.x = x;
wc.y = y;
xcb_aux_configure_window(c, w, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, &wc);
}
void xresizewindow(xcb_connection_t *c, xcb_window_t w, uint32_t width, uint32_t height)
{
xcb_params_configure_window_t wc;
wc.width = width;
wc.height = height;
xcb_aux_configure_window(c, w, XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT, &wc);
}
void xmoveresizewindow(xcb_connection_t *c, xcb_window_t w,
int32_t x, int32_t y, uint32_t width, uint32_t height)
{
xcb_params_configure_window_t wc;
wc.x = x;
wc.y = y;
wc.width = width;
wc.height = height;
xcb_aux_configure_window(c, w,
(XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y |
XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT), &wc);
}
void xmapraised(xcb_connection_t *c, xcb_window_t w)
{
uint32_t value = XCB_STACK_MODE_ABOVE;
xcb_configure_window(c, w, XCB_CONFIG_WINDOW_STACK_MODE, &value);
xcb_map_window(c, w);
}
void xraisewindow(xcb_connection_t *c, xcb_window_t w)
{
uint32_t value = XCB_STACK_MODE_ABOVE;
xcb_configure_window(c, w, XCB_CONFIG_WINDOW_STACK_MODE, &value);
}
xcb_window_t xcreatesimplewindow(xcb_connection_t *c, xcb_window_t parent, uint32_t x, uint32_t y,
uint32_t width, uint32_t height, uint32_t border_width, uint32_t border, uint32_t background) {
xcb_window_t wid = xcb_generate_id(c);
uint32_t values[] = { background, border };
xcb_create_window(c, 0, wid, parent, x, y, width, height, border_width,
XCB_WINDOW_CLASS_COPY_FROM_PARENT, XCB_COPY_FROM_PARENT,
(XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL), values);
return wid;
}
uint32_t textwidth(xcb_connection_t *c, xcb_font_t font, int len, char *str)
{
xcb_query_text_extents_cookie_t qte_c;
xcb_query_text_extents_reply_t *qte_r;
xcb_query_font_cookie_t qf_c;
xcb_query_font_reply_t *qf_r;
xcb_generic_error_t *errorp;
int i;
uint32_t width;
xcb_char2b_t *wstr;
wstr = xalloc(len * sizeof(xcb_char2b_t));
for (i = 0; i < len; i++) {
wstr[i].byte1 = 0;
wstr[i].byte2 = str[i];
}
qte_c = xcb_query_text_extents(c, font, len, wstr);
qte_r = xcb_query_text_extents_reply(c, qte_c, &errorp);
if (qte_r) {
width = qte_r->overall_width;
free(qte_r);
}
else {
fprintf(stderr, "9wm: xtextwidth: failed to get text extents\n");
if (errorp)
handler(errorp);
qf_c = xcb_query_font(c, font);
qf_r = xcb_query_font_reply(c, qf_c, NULL);
if (!qf_r) {
fprintf(stderr, "9wm: xtextwidth: query font failed\n");
if (errorp)
handler(errorp);
return 0;
}
width = len * qf_r->max_bounds.character_width;
free(qf_r);
}
free(wstr);
return width;
}