]> Pileus Git - wmpus/blob - sys-xcb.c
1dcb102be4b81233936d3603911d9aa081ea121e
[wmpus] / sys-xcb.c
1 /*
2  * Copyright (c) 2015 Andy Spencer <andy753421@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  */
15
16 #define _GNU_SOURCE
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <search.h>
20 #include <string.h>
21
22 #include <xcb/xcb.h>
23 #include <xcb/xcb_event.h>
24 #include <xcb/xcb_keysyms.h>
25 #include <xcb/xinerama.h>
26
27 #include "util.h"
28 #include "conf.h"
29 #include "types.h"
30 #include "sys.h"
31 #include "wm.h"
32
33 /* Configuration */
34 static int border     = 2;
35 static int stack      = 25;
36 static int no_capture = 0;
37
38 /* Internal structures */
39 struct win_sys {
40         xcb_window_t     xcb;    // xcb window id
41         xcb_event_mask_t events; // currently watch events
42         int override;            // normal vs override redirect
43         int mapped;              // window is currently mapped
44 };
45
46 typedef enum {
47         CLR_FOCUS,
48         CLR_UNFOCUS,
49         CLR_URGENT,
50         NCOLORS
51 } color_t;
52
53 /* Global data */
54 static xcb_connection_t  *conn;
55 static xcb_key_symbols_t *keysyms;
56 static xcb_colormap_t     colormap;
57 static xcb_window_t       root;
58 static xcb_window_t       last;
59 static xcb_event_mask_t   events;
60 static list_t            *screens;
61 static void              *cache;
62 static xcb_pixmap_t       colors[NCOLORS];
63
64 /************************
65  * Conversion functions *
66  ************************/
67
68 /* Key presses */
69 static struct {
70         event_t      ev;
71         xcb_keysym_t sym;
72 } keysym_map[] = {
73         { EV_LEFT,     0xFF51 },
74         { EV_RIGHT,    0xFF53 },
75         { EV_UP,       0xFF52 },
76         { EV_DOWN,     0xFF54 },
77         { EV_HOME,     0xFF50 },
78         { EV_END,      0xFF57 },
79         { EV_PAGEUP,   0xFF55 },
80         { EV_PAGEDOWN, 0xFF56 },
81         { EV_F1,       0xFFBE },
82         { EV_F2,       0xFFBF },
83         { EV_F3,       0xFFC0 },
84         { EV_F4,       0xFFC1 },
85         { EV_F5,       0xFFC2 },
86         { EV_F6,       0xFFC3 },
87         { EV_F7,       0xFFC4 },
88         { EV_F8,       0xFFC5 },
89         { EV_F9,       0xFFC6 },
90         { EV_F10,      0xFFC7 },
91         { EV_F11,      0xFFC8 },
92         { EV_F12,      0xFFC9 },
93 };
94
95 static xcb_keycode_t *event_to_keycodes(event_t ev)
96 {
97         xcb_keycode_t *codes = NULL;
98
99         /* Get keysym */
100         xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
101
102         /* Get keycodes */
103         if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
104                 warn("no keycode found for %d->%d", ev, keysym);
105
106         return codes;
107 }
108
109 static event_t keycode_to_event(xcb_keycode_t code)
110 {
111         /* Get keysym */
112         xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
113
114         /* Get event */
115         return map_get(keysym_map, sym,keysym, ev,keysym);
116 }
117
118 /* Button presses */
119 static event_t button_to_event(xcb_button_t btn)
120 {
121         return EV_MOUSE0 + btn;
122 }
123
124 static xcb_button_t event_to_button(event_t ev)
125 {
126         return ev - EV_MOUSE0;
127 }
128
129 /* Modifier masks */
130 static xcb_mod_mask_t mod_to_mask(mod_t mod)
131 {
132         xcb_mod_mask_t mask = 0;
133         if (mod.alt)   mask |= XCB_MOD_MASK_1;
134         if (mod.ctrl)  mask |= XCB_MOD_MASK_CONTROL;
135         if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
136         if (mod.win)   mask |= XCB_MOD_MASK_4;
137         return mask;
138 }
139
140 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
141 {
142         mod_t mod = { .up = up };
143         if (mask & XCB_MOD_MASK_1)       mod.alt   = 1;
144         if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl  = 1;
145         if (mask & XCB_MOD_MASK_SHIFT)   mod.shift = 1;
146         if (mask & XCB_MOD_MASK_4)       mod.win   = 1;
147         return mod;
148 }
149
150 /* Mouse pointers */
151 static ptr_t list_to_ptr(int16_t *list)
152 {
153         ptr_t ptr = {};
154         ptr.rx = list[0]; // root_x
155         ptr.ry = list[1]; // root_y
156         ptr.x  = list[2]; // event_x
157         ptr.y  = list[3]; // event_y
158         return ptr;
159 }
160
161 /********************
162  * Window functions *
163  ********************/
164
165 static int win_cmp(const void *_a, const void *_b)
166 {
167         const win_t *a = _a;
168         const win_t *b = _b;
169         if (a->sys->xcb < b->sys->xcb) return -1;
170         if (a->sys->xcb > b->sys->xcb) return  1;
171         return 0;
172 }
173
174 static win_t *win_get(xcb_window_t xcb)
175 {
176         win_sys_t sys = { .xcb =  xcb };
177         win_t     key = { .sys = &sys };
178
179         win_t   **win = tfind(&key, &cache, win_cmp);
180
181         if (!win) {
182                 warn("no window for %u", xcb);
183                 return NULL;
184         }
185
186         return *win;
187 }
188
189 /****************
190  * XCB Wrappers *
191  ****************/
192
193 static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
194 {
195         xcb_query_tree_cookie_t cookie =
196                 xcb_query_tree(conn, win);
197         if (!cookie.sequence)
198                 return warn("do_query_tree: %d - bad cookie", win);
199
200         xcb_query_tree_reply_t *reply =
201                 xcb_query_tree_reply(conn, cookie, NULL);
202         if (!reply)
203                 return warn("do_query_tree: %d - no reply", win);
204
205         int nkids = xcb_query_tree_children_length(reply);
206         *kids = xcb_query_tree_children(reply);
207         printf("do_query_tree: %d - n=%d\n", win, nkids);
208         return nkids;
209 }
210
211 static int do_get_geometry(xcb_window_t win,
212                 int *x, int *y, int *w, int *h)
213 {
214         xcb_get_geometry_cookie_t cookie =
215                 xcb_get_geometry(conn, win);
216         if (!cookie.sequence)
217                 return warn("do_get_geometry: %d - bad cookie", win);
218
219         xcb_get_geometry_reply_t *reply =
220                 xcb_get_geometry_reply(conn, cookie, NULL);
221         if (!reply)
222                 return warn("do_get_geometry: %d - no reply", win);
223
224         printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
225                         win, reply->width, reply->height, reply->x, reply->y);
226         *x = reply->x;
227         *y = reply->y;
228         *w = reply->width;
229         *h = reply->height;
230         return 1;
231 }
232
233 static int do_get_window_attributes(xcb_window_t win,
234                 int *override, int *mapped)
235 {
236         xcb_get_window_attributes_cookie_t cookie =
237                 xcb_get_window_attributes(conn, win);
238         if (!cookie.sequence)
239                 return warn("do_get_window_attributes: %d - bad cookie", win);
240
241         xcb_get_window_attributes_reply_t *reply =
242                 xcb_get_window_attributes_reply(conn, cookie, NULL);
243         if (!reply)
244                 return warn("do_get_window_attributes: %d - no reply ", win);
245
246         printf("do_get_window_attributes: %d - %d\n",
247                         win, reply->override_redirect);
248         *override = reply->override_redirect;
249         *mapped   = reply->map_state != XCB_MAP_STATE_UNMAPPED;
250         return 1;
251 }
252
253 static int do_xinerama_check(void)
254 {
255         const xcb_query_extension_reply_t *data =
256                 xcb_get_extension_data(conn, &xcb_xinerama_id);
257         if (!data || !data->present)
258                 return warn("do_xinerama_check: no ext");
259
260         xcb_xinerama_is_active_cookie_t cookie =
261                 xcb_xinerama_is_active(conn);
262         if (!cookie.sequence)
263                 return warn("do_xinerama_check: no cookie");
264
265         xcb_xinerama_is_active_reply_t *reply =
266                 xcb_xinerama_is_active_reply(conn, cookie, NULL);
267         if (!reply)
268                 warn("do_xinerama_check: no reply");
269
270         printf("do_xinerama_check: %d\n", reply->state);
271         return reply && reply->state;
272 }
273
274 static int do_query_screens(xcb_xinerama_screen_info_t **info)
275 {
276         xcb_xinerama_query_screens_cookie_t cookie =
277                 xcb_xinerama_query_screens(conn);
278         if (!cookie.sequence)
279                 return warn("do_query_screens: bad cookie");
280
281         xcb_xinerama_query_screens_reply_t *reply =
282                 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
283         if (!reply)
284                 return warn("do_query_screens: no reply");
285
286         int ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
287         *info = xcb_xinerama_query_screens_screen_info(reply);
288         printf("do_query_screens: %d screens\n", ninfo);
289         return ninfo;
290 }
291
292 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
293 {
294         uint16_t r = (rgb & 0xFF0000) >> 8;
295         uint16_t g = (rgb & 0x00FF00);
296         uint16_t b = (rgb & 0x0000FF) << 8;
297         xcb_alloc_color_cookie_t cookie =
298                 xcb_alloc_color(conn, colormap, r, g, b);
299         if (!cookie.sequence)
300                 return warn("do_alloc_color: bad cookie");
301
302         xcb_alloc_color_reply_t *reply =
303                 xcb_alloc_color_reply(conn, cookie, NULL);
304         if (!reply)
305                 return warn("do_alloc_color: no reply");
306
307         printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
308         return reply->pixel;
309 }
310
311 /**********************
312  * X11 Event Handlers *
313  **********************/
314
315 /* Specific events */
316 static void on_key_event(xcb_key_press_event_t *event, int up)
317 {
318         xcb_window_t xcb = event->event == root ?
319                 event->child : event->event;
320         win_t  *win = win_get(xcb);
321         event_t ev  = keycode_to_event(event->detail);
322         mod_t   mod = mask_to_mod(event->state, up);
323         ptr_t   ptr = list_to_ptr(&event->root_x);
324         printf("on_key_event:         xcb=%u -> win=%p\n", xcb, win);
325         wm_handle_event(win, ev, mod, ptr);
326 }
327
328 static void on_button_event(xcb_button_press_event_t *event, int up)
329 {
330         xcb_window_t xcb = event->event == root ?
331                 event->child : event->event;
332         win_t  *win = win_get(xcb);
333         event_t ev  = button_to_event(event->detail);
334         mod_t   mod = mask_to_mod(event->state, up);
335         ptr_t   ptr = list_to_ptr(&event->root_x);
336         printf("on_button_event:      xcb=%u -> win=%p\n", xcb, win);
337
338         if (!wm_handle_event(win, ev, mod, ptr))
339                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
340         else if (!up)
341                 xcb_grab_pointer(conn, 1, xcb,
342                                 XCB_EVENT_MASK_POINTER_MOTION |
343                                 XCB_EVENT_MASK_BUTTON_RELEASE,
344                                 XCB_GRAB_MODE_ASYNC,
345                                 XCB_GRAB_MODE_ASYNC,
346                                 0, 0, XCB_CURRENT_TIME);
347         else
348                 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
349
350 }
351
352 static void on_motion_notify(xcb_motion_notify_event_t *event)
353 {
354         xcb_window_t xcb = event->event == root ?
355                 event->child : event->event;
356         win_t *win = win_get(xcb);
357         ptr_t  ptr = list_to_ptr(&event->root_x);
358         printf("on_motion_notify:     xcb=%u -> win=%p - %d,%d / %d.%d\n", xcb, win,
359                         ptr.x, ptr.y, ptr.rx, ptr.ry);
360         wm_handle_ptr(win, ptr);
361 }
362
363 static void on_create_notify(xcb_create_notify_event_t *event)
364 {
365         win_t     *win = new0(win_t);
366         win_sys_t *sys = new0(win_sys_t);
367
368         printf("on_create_notify:     xcb=%u -> win=%p\n",
369                         event->window, win);
370
371         win->x        = event->x;
372         win->y        = event->y;
373         win->w        = event->width;
374         win->h        = event->height;
375         win->sys      = sys;
376
377         sys->xcb      = event->window;
378         sys->override = event->override_redirect;
379
380         tsearch(win, &cache, win_cmp);
381 }
382
383 static void on_destroy_notify(win_t *win, xcb_destroy_notify_event_t *event)
384 {
385         printf("on_destroy_notify:    xcb=%u -> win=%p\n",
386                         event->window, win);
387
388         tdelete(win, &cache, win_cmp);
389
390         if (win->sys->xcb == last)
391                 last = 0;
392
393         free(win->sys);
394         free(win);
395 }
396
397 static void on_map_request(win_t *win, xcb_map_request_event_t *event)
398 {
399         printf("on_map_request:       xcb=%u -> win=%p\n",
400                         event->window, win);
401
402         if (!win->sys->override && !win->sys->mapped)
403                 wm_insert(win);
404         win->sys->mapped = 1;
405
406         xcb_map_window(conn, win->sys->xcb);
407         sys_move(win, win->x, win->y, win->w, win->h);
408 }
409
410 static void on_configure_request(win_t *win, xcb_configure_request_event_t *event)
411 {
412         printf("on_configure_request: xcb=%u -> win=%p -- %dx%d @ %d,%d\n",
413                         event->window, win,
414                         event->width, event->height,
415                         event->x, event->y);
416
417         win->x = event->x;
418         win->y = event->y;
419         win->w = event->width;
420         win->h = event->height;
421
422         xcb_configure_notify_event_t resp = {
423                 .response_type = XCB_CONFIGURE_NOTIFY,
424                 .event         = win->sys->xcb,
425                 .window        = win->sys->xcb,
426                 .x             = win->x,
427                 .y             = win->y,
428                 .width         = win->w,
429                 .height        = win->h,
430                 .border_width  = border,
431         };
432
433         xcb_send_event(conn, 0, win->sys->xcb,
434                         XCB_EVENT_MASK_STRUCTURE_NOTIFY,
435                         (const char *)&resp);
436 }
437
438 /* Generic Event */
439 static void on_event(xcb_generic_event_t *event)
440 {
441         win_t   *win = NULL;
442
443         int type = XCB_EVENT_RESPONSE_TYPE(event);
444         int sent = XCB_EVENT_SENT(event);
445         const char *name = NULL;
446
447         switch (type) {
448                 /* Input handling */
449                 case XCB_KEY_PRESS:
450                         on_key_event((xcb_key_press_event_t *)event, 0);
451                         break;
452                 case XCB_KEY_RELEASE:
453                         on_key_event((xcb_key_release_event_t *)event, 1);
454                         break;
455                 case XCB_BUTTON_PRESS:
456                         on_button_event((xcb_button_press_event_t *)event, 0);
457                         break;
458                 case XCB_BUTTON_RELEASE:
459                         on_button_event((xcb_button_release_event_t *)event, 1);
460                         break;
461                 case XCB_MOTION_NOTIFY:
462                         on_motion_notify((xcb_motion_notify_event_t *)event);
463                         break;
464
465                 /* Window management */
466                 case XCB_CREATE_NOTIFY:
467                         on_create_notify((xcb_create_notify_event_t *)event);
468                         break;
469                 case XCB_DESTROY_NOTIFY:
470                         if ((win = win_get(((xcb_destroy_notify_event_t *)event)->window)))
471                                 on_destroy_notify(win, (xcb_destroy_notify_event_t *)event);
472                         break;
473                 case XCB_MAP_REQUEST:
474                         if ((win = win_get(((xcb_map_request_event_t *)event)->window)))
475                                 on_map_request(win, (xcb_map_request_event_t *)event);
476                         break;
477                 case XCB_CONFIGURE_REQUEST:
478                         if ((win = win_get(((xcb_configure_request_event_t *)event)->window)))
479                                 on_configure_request(win, (xcb_configure_request_event_t *)event);
480                         break;
481
482                 /* Unknown events */
483                 default:
484                         name = xcb_event_get_label(type);
485                         printf("on_event: %d:%02X -> %s\n",
486                                 !!sent, type, name?:"unknown_event");
487                         break;
488         }
489 }
490
491 /********************
492  * System functions *
493  ********************/
494
495 void sys_move(win_t *win, int x, int y, int w, int h)
496 {
497         printf("sys_move:  %p - %dx%d @ %d,%d\n",
498                         win, w, h, x, y);
499
500         int b = 2*border;
501
502         win->x = x;
503         win->y = y;
504         win->w = MAX(w,1+b);
505         win->h = MAX(h,1+b);
506         w      = MAX(w-b,1);
507         h      = MAX(h-b,1);
508
509         uint16_t mask   = XCB_CONFIG_WINDOW_X
510                         | XCB_CONFIG_WINDOW_Y
511                         | XCB_CONFIG_WINDOW_WIDTH
512                         | XCB_CONFIG_WINDOW_HEIGHT
513                         | XCB_CONFIG_WINDOW_BORDER_WIDTH;
514         uint32_t list[] = {x, y, w, h, border};
515
516         xcb_configure_window(conn, win->sys->xcb, mask, list);
517 }
518
519 void sys_raise(win_t *win)
520 {
521         printf("sys_raise: %p\n", win);
522         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, win->sys->xcb);
523 }
524
525 void sys_focus(win_t *win)
526 {
527         printf("sys_focus: %p\n", win);
528         xcb_window_t xcb = win ? win->sys->xcb : root;
529
530         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
531                         xcb, XCB_CURRENT_TIME);
532
533         if (last)
534                 xcb_change_window_attributes(conn, last, XCB_CW_BORDER_PIXEL, &colors[CLR_UNFOCUS]);
535         xcb_change_window_attributes(conn, xcb, XCB_CW_BORDER_PIXEL, &colors[CLR_FOCUS]);
536         last = xcb;
537 }
538
539 void sys_show(win_t *win, state_t state)
540 {
541         printf("sys_show:  %p - %d\n", win, state);
542 }
543
544 void sys_watch(win_t *win, event_t ev, mod_t mod)
545 {
546         printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
547         xcb_window_t      xcb  = win ? win->sys->xcb     : root;
548         xcb_event_mask_t *mask = win ? &win->sys->events : &events;
549         xcb_mod_mask_t    mods = 0;
550         xcb_button_t      btn  = 0;
551         xcb_keycode_t    *code = 0;
552
553         switch (ev) {
554                 case EV_ENTER:
555                         *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
556                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
557                         break;
558
559                 case EV_LEAVE:
560                         *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
561                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
562                         break;
563
564                 case EV_FOCUS:
565                 case EV_UNFOCUS:
566                         *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
567                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
568                         break;
569
570                 case EV_MOUSE0...EV_MOUSE7:
571                         btn    = event_to_button(ev);
572                         mods   = mod_to_mask(mod);
573                         *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
574                                         : XCB_EVENT_MASK_BUTTON_PRESS;
575                         xcb_grab_button(conn, 0, xcb, *mask,
576                                         XCB_GRAB_MODE_ASYNC,
577                                         XCB_GRAB_MODE_ASYNC,
578                                         0, 0, btn, mods);
579                         break;
580
581                 default:
582                         code = event_to_keycodes(ev);
583                         mods = mod_to_mask(mod);
584                         for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
585                                 xcb_grab_key(conn, 1, xcb, mods, code[i],
586                                                 XCB_GRAB_MODE_ASYNC,
587                                                 XCB_GRAB_MODE_ASYNC);
588
589                         break;
590         }
591 }
592
593 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
594 {
595         printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
596 }
597
598 list_t *sys_info(void)
599 {
600         printf("sys_info\n");
601
602         if (screens == NULL && do_xinerama_check()) {
603                 /* Add Xinerama screens */
604                 xcb_xinerama_screen_info_t *info = NULL;
605                 int ninfo = do_query_screens(&info);
606                 for (int i = 0; i < ninfo; i++) {
607                         win_t *screen = new0(win_t);
608
609                         screen->x = info[i].x_org;
610                         screen->y = info[i].y_org;
611                         screen->w = info[i].width;
612                         screen->h = info[i].height;
613
614                         screens = list_insert(NULL, screen);
615
616                         printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
617                                         screen->w, screen->h,
618                                         screen->x, screen->y);
619                 }
620         }
621
622         if (screens == NULL) {
623                 /* No xinerama support */
624                 const xcb_setup_t *setup = xcb_get_setup(conn);
625                 xcb_screen_t      *geom  = xcb_setup_roots_iterator(setup).data;
626
627                 win_t *screen = new0(win_t);
628
629                 screen->w = geom->width_in_pixels;
630                 screen->h = geom->height_in_pixels;
631
632                 screens = list_insert(NULL, screen);
633
634                 printf("sys_info: root screen - %dx%d\n",
635                                 screen->w, screen->h);
636         }
637
638         return screens;
639 }
640
641 void sys_init(void)
642 {
643         printf("sys_init\n");
644
645         /* Load configuration */
646         stack      = conf_get_int("main.stack",      stack);
647         border     = conf_get_int("main.border",     border);
648         no_capture = conf_get_int("main.no-capture", no_capture);
649
650         /* Connect to display */
651         if (!(conn = xcb_connect(NULL, NULL)))
652                 error("xcb connect failed");
653         if (xcb_connection_has_error(conn))
654                 error("xcb connection has errors");
655
656         /* Get root window */
657         const xcb_setup_t     *setup = xcb_get_setup(conn);
658         xcb_screen_iterator_t  iter  = xcb_setup_roots_iterator(setup);
659         root     = iter.data->root;
660         colormap = iter.data->default_colormap;
661
662         /* Allocate key symbols */
663         if (!(keysyms = xcb_key_symbols_alloc(conn)))
664                 error("cannot allocate key symbols");
665
666         /* Read color information */
667         colors[CLR_FOCUS]   = do_alloc_color(0xFF6060);
668         colors[CLR_UNFOCUS] = do_alloc_color(0xD8D8FF);
669         colors[CLR_URGENT]  = do_alloc_color(0xFF0000);
670
671         /* Request substructure redirect */
672         xcb_void_cookie_t cookie;
673         xcb_generic_error_t *err;
674         events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
675                  XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
676         cookie = xcb_change_window_attributes_checked(conn, root,
677                         XCB_CW_EVENT_MASK, &events);
678         if ((err = xcb_request_check(conn, cookie)))
679                 error("another window manager is already running");
680 }
681
682 void sys_run(void)
683 {
684         printf("sys_run\n");
685
686         /* Add each initial window */
687         if (!no_capture) {
688                 xcb_window_t *kids = NULL;
689                 int nkids = do_query_tree(root, &kids);
690                 for(int i = 0; i < nkids; i++) {
691                         win_t *win = new0(win_t);
692                         win->sys = new0(win_sys_t);
693                         win->sys->xcb = kids[i];
694                         do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
695                         do_get_window_attributes(kids[i],
696                                 &win->sys->override, &win->sys->mapped);
697                         tsearch(win, &cache, win_cmp);
698                         if (!win->sys->override && win->sys->mapped)
699                                 wm_insert(win);
700                 }
701                 xcb_flush(conn);
702         }
703
704         /* Main loop */
705         while (1)
706         {
707                 int status;
708                 xcb_generic_event_t *event;
709                 if (!(event = xcb_wait_for_event(conn)))
710                         break;
711                 on_event(event);
712                 free(event);
713                 if (!(status = xcb_flush(conn)))
714                         break;
715         }
716 }
717
718 void sys_exit(void)
719 {
720         printf("sys_exit\n");
721         if (conn)
722                 xcb_disconnect(conn);
723         conn = NULL;
724 }
725
726 void sys_free(void)
727 {
728         printf("sys_free\n");
729         if (conn)
730                 xcb_disconnect(conn);
731         conn = NULL;
732 }