-
Notifications
You must be signed in to change notification settings - Fork 2
/
window.c
355 lines (288 loc) · 9.67 KB
/
window.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xcb_image.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-x11.h>
#include "window.h"
#include "util/error.h"
#include "util/mem.h"
void scale_to_window(size_t *width, size_t *height, size_t w_width, size_t w_height)
{
double scaling_factor = 0.0;
if (*height > w_height) {
scaling_factor = (double)w_height / (double)*height;
*height = w_height;
*width = *width * scaling_factor;
}
if (*width > w_width) {
size_t t_width = *width;
*width = w_width;
scaling_factor = (double)w_width / (double)t_width;
*height = *height * scaling_factor;
}
}
int init_xkb_state(struct mu_window *window)
{
int32_t device_id = xkb_x11_get_core_keyboard_device_id(window->c);
if (device_id == -1) {
fputs("Failed to get device_id\n", stderr);
return -1;
}
window->keymap = xkb_x11_keymap_new_from_device(window->xkb, window->c, device_id,
XKB_KEYMAP_COMPILE_NO_FLAGS);
if (window->keymap == NULL) {
fputs("Failed to create keymap\n", stderr);
return -1;
}
window->keyboard_state = xkb_x11_state_new_from_device(window->keymap, window->c, device_id);
if (window->keyboard_state == NULL) {
fputs("Failed to create xkb state\n", stderr);
return -1;
}
return 0;
}
int init_xkb(struct mu_window *window)
{
int ret = xkb_x11_setup_xkb_extension(window->c,
XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION,
XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS, NULL, NULL, NULL, NULL);
if (ret != 1) {
fputs("Failed to setup xkb extension\n", stderr);
return -1;
}
window->xkb = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (window->xkb == NULL) {
fputs("Failed to create xkb context\n", stderr);
return -1;
}
return init_xkb_state(window);
}
void deinit_xkb_state(struct mu_window *window)
{
if (window->keyboard_state) {
xkb_state_unref(window->keyboard_state);
window->keyboard_state = NULL;
}
if (window->keymap) {
xkb_keymap_unref(window->keymap);
window->keymap = NULL;
}
}
void deinit_xkb(struct mu_window *window)
{
deinit_xkb_state(window);
if (window->xkb) {
xkb_context_unref(window->xkb);
window->xkb = NULL;
}
}
struct mu_window *create_window(struct mu_error **err, size_t o_width, size_t o_height)
{
struct mu_window *window = mallocz(sizeof(struct mu_window));
xcb_void_cookie_t cookie;
xcb_generic_error_t *xerr;
uint32_t mask = 0;
uint32_t values[2];
int ret = 0;
if (window == NULL) {
MU_PUSH_ERRNO(err, ENOMEM);
return NULL;
}
window->c = xcb_connect(NULL, NULL);
ret = xcb_connection_has_error(window->c);
if (ret != 0) {
MU_PUSH_ERRSTR(err, "Could not create an XCB connection, dying");
free(window);
return NULL;
}
window->screen = xcb_setup_roots_iterator(xcb_get_setup(window->c)).data;
ret = init_xkb(window);
if (ret != 0) {
MU_PUSH_ERRSTR(err, "Could not initialize XKB Extension, dying");
deinit_xkb(window);
free(window);
return NULL;
}
// FIXME: The XKB Documentation mention using this call to listen to keyboard change events,
// but I can't find any documentation on what any of the options mean so...
/* cookie = xcb_xkb_select_events_aux(window->c, j */
window->win = xcb_generate_id(window->c);
mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
values[0] = window->screen->black_pixel;
values[1] = XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_BUTTON_1_MOTION |
XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_STRUCTURE_NOTIFY;
window->width = o_width;
window->height = o_height;
scale_to_window(&window->width, &window->height,
window->screen->width_in_pixels, window->screen->height_in_pixels);
cookie = xcb_create_window(window->c, window->screen->root_depth, window->win,
window->screen->root, 0, 0, window->width, window->height, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT, window->screen->root_visual, mask,
values);
xerr = xcb_request_check(window->c, cookie);
if (xerr) {
MU_PUSH_ERRF(err, "Could not create window: XCB error %d", xerr->error_code);
free(err);
free(window);
return NULL;
}
return window;
}
int update_geometry(struct mu_error **err, struct mu_window *window)
{
xcb_get_geometry_reply_t *geom;
geom = xcb_get_geometry_reply(window->c, xcb_get_geometry(window->c, window->win), NULL);
if (!geom)
MU_RET_ERRSTR(err, "Could not get geometry of window, dying");
printf("window is %hux%hu\n", geom->width, geom->height);
window->width = geom->width;
window->height = geom->height;
free(geom);
return 0;
}
void map_window(struct mu_window *window)
{
xcb_map_window(window->c, window->win);
xcb_flush(window->c);
}
void destroy_window(struct mu_window **window)
{
struct mu_window *w = *window;
if (w->gc)
xcb_free_gc(w->c, w->gc);
if (w->pix)
xcb_free_pixmap(w->c, w->pix);
if (w->win)
xcb_destroy_window(w->c, w->win);
deinit_xkb(w);
xcb_disconnect(w->c);
free(w);
window = NULL;
}
int create_gc(struct mu_error **err, struct mu_window *window)
{
uint32_t mask = 0;
uint32_t values[2];
mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
values[0] = window->screen->black_pixel;
values[1] = 0;
window->gc = xcb_generate_id(window->c);
xcb_create_gc(window->c, window->gc, window->pix, mask, values);
/* xcb_create_gc(window->c, window->gc, window->pix, 0, NULL); */
return 0;
}
int create_pixmap(struct mu_error **err, struct mu_window *window, size_t width, size_t height)
{
window->pix = xcb_generate_id(window->c);
xcb_create_pixmap(window->c, window->screen->root_depth, window->pix, window->win, width, height);
return 0;
}
static int draw_image(struct mu_error **err, struct mu_window *window, uint16_t loc[4], size_t im_width, size_t im_height)
{
uint16_t src_x = loc[0], dst_x = src_x, src_y = loc[1], dst_y = src_y, c_width = loc[2], c_height = loc[3];
if (src_x < window->xoff && c_width > window->xoff) {
dst_x += window->xoff;
c_width -= window->xoff;
} else if (src_x < window->xoff || src_x > im_width + window->xoff) {
return 0;
} else {
src_x -= window->xoff;
}
if (src_y < window->yoff && c_height > window->yoff) {
dst_y += window->yoff;
c_height -= window->yoff;
} else if (src_y < window->yoff || src_y > im_height + window->yoff) {
return 0;
} else {
src_y -= window->yoff;
}
xcb_copy_area(window->c, window->pix, window->win, window->gc, src_x, src_y, dst_x, dst_y, c_width, c_height);
xcb_flush(window->c);
return 0;
}
static int reload_with_offset(struct mu_error **err, struct mu_window *window, size_t width, size_t height)
{
uint16_t loc[4] = { 0, 0, window->width, window->height };
if (width < window->width)
window->xoff = (window->width - width) / 2;
else
window->xoff = 0;
if (height < window->height)
window->yoff = (window->height - height) / 2;
else
window->yoff = 0;
xcb_clear_area(window->c, 0, window->win, 0, 0, window->width, window->height);
return draw_image(err, window, loc, width, height);
}
int load_image(struct mu_error **err, struct mu_window *window, unsigned char *data, size_t len, size_t width, size_t height)
{
xcb_pixmap_t old_pix = window->pix;
xcb_image_t *img;
// Recreate pixmap here
// Create some sort of backing pixmap and then swap them "atomically"?
create_pixmap(err, window, width, height);
img = xcb_image_create_native(window->c, width, height, XCB_IMAGE_FORMAT_Z_PIXMAP, window->screen->root_depth, data, len, data);
xcb_image_put(window->c, window->pix, window->gc, img, 0, 0, 0);
xcb_image_destroy(img);
reload_with_offset(err, window, width, height);
xcb_free_pixmap(window->c, old_pix);
return 0;
}
int handle_expose(struct mu_error **err, struct mu_window *window, size_t width, size_t height, xcb_expose_event_t *ev)
{
uint16_t loc[4] = { ev->x, ev->y, ev->width, ev->height };
return draw_image(err, window, loc, width, height);
}
int resize_window(struct mu_error **err, struct mu_window *window, size_t sizes[4], xcb_configure_notify_event_t *ev)
{
size_t width = sizes[0], height = sizes[1], o_width = sizes[2], o_height = sizes[3];
int ret;
if (window->width == ev->width && window->height == ev->height) {
return 0;
} else if (window->width == ev->width || window->height == ev->height) {
window->width = ev->width;
window->height = ev->height;
return reload_with_offset(err, window, width, height);
} else if (width == o_width && height == o_height && width <= ev->width && height <= ev->height) {
// Unscaled Case
window->width = ev->width;
window->height = ev->height;
return reload_with_offset(err, window, width, height);
} else if ((ev->width <= window->width && width <= ev->width) && (ev->height <= window->height && height <= ev->height)) {
// No need to rescale
window->width = ev->width;
window->height = ev->height;
return reload_with_offset(err, window, width, height);
}
window->width = ev->width;
window->height = ev->height;
ret = reload_with_offset(err, window, width, height);
return ret == 0 ? 1 : ret;
}
int draw_bbox(struct mu_error **err, struct mu_window *window, Point *p1, Point *p2)
{
xcb_rectangle_t rect = { 0, 0, abs(p2->x - p1->x), abs(p2->y - p1->y) };
uint16_t loc[4] = { 0, 0, window->width, window->height };
rect.x = p2->x > p1->x ? p1->x : p2->x;
rect.y = p2->y > p1->y ? p1->y : p2->y;
loc[0] = 0;
loc[1] = 0;
// TODO: maybe optimize this by storing the last box size/pos and only clearing that
draw_image(err, window, loc, window->width, window->height);
xcb_poly_rectangle(window->c, window->win, window->gc, 1, &rect);
xcb_flush(window->c);
return 0;
}
int clear_bbox(struct mu_error **err, struct mu_window *window, Point *p1, Point *p2)
{
uint16_t loc[4] = { 0, 0, window->width, window->height };
(void)p1;
(void)p2;
draw_image(err, window, loc, window->width, window->height);
xcb_flush(window->c);
return 0;
}