]> Pileus Git - wmpus/blob - sys-xcb.c
Various icccm and ewmh properties
[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/xcb_icccm.h>
26 #include <xcb/xcb_ewmh.h>
27 #include <xcb/xinerama.h>
28
29 #include "util.h"
30 #include "conf.h"
31 #include "types.h"
32 #include "sys.h"
33 #include "wm.h"
34
35 /* Configuration */
36 static int border     = 2;
37 static int stack      = 25;
38 static int no_capture = 0;
39
40 /* Internal structures */
41 typedef struct {
42         int left;
43         int right;
44         int top;
45         int bottom;
46 } strut_t;
47
48 struct win_sys {
49         xcb_window_t     xcb;    // xcb window id
50         xcb_event_mask_t events; // currently watch events
51         strut_t          strut;  // toolbar struts
52         state_t          state;  // window state if not mapped
53         int mapped;              // window is managed by wm
54         int managed;             // window is managed by wm
55 };
56
57 /* Global data */
58 static xcb_connection_t      *conn;
59 static xcb_ewmh_connection_t  ewmh;
60 static xcb_key_symbols_t     *keysyms;
61 static xcb_colormap_t         colormap;
62 static xcb_window_t           root;
63 static xcb_event_mask_t       events;
64 static list_t                *screens;
65 static list_t                *struts;
66 static void                  *cache;
67 static unsigned int           grabbed;
68 static int                    running;
69 static xcb_window_t           control;
70
71 static xcb_pixmap_t           clr_focus;
72 static xcb_pixmap_t           clr_unfocus;
73 static xcb_pixmap_t           clr_urgent;
74
75 static xcb_atom_t             wm_protos;
76 static xcb_atom_t             wm_delete;
77 static xcb_atom_t             wm_nhints;
78
79 /************************
80  * Conversion functions *
81  ************************/
82
83 /* State names */
84 static char *state_map[] = {
85         [ST_HIDE ] "hide ",
86         [ST_SHOW ] "show ",
87         [ST_FULL ] "full ",
88         [ST_MAX  ] "max  ",
89         [ST_SHADE] "shade",
90         [ST_ICON ] "icon ",
91         [ST_CLOSE] "close",
92 };
93
94 /* Key presses */
95 static struct {
96         event_t      ev;
97         xcb_keysym_t sym;
98 } keysym_map[] = {
99         { EV_LEFT,     0xFF51 },
100         { EV_RIGHT,    0xFF53 },
101         { EV_UP,       0xFF52 },
102         { EV_DOWN,     0xFF54 },
103         { EV_HOME,     0xFF50 },
104         { EV_END,      0xFF57 },
105         { EV_PAGEUP,   0xFF55 },
106         { EV_PAGEDOWN, 0xFF56 },
107         { EV_F1,       0xFFBE },
108         { EV_F2,       0xFFBF },
109         { EV_F3,       0xFFC0 },
110         { EV_F4,       0xFFC1 },
111         { EV_F5,       0xFFC2 },
112         { EV_F6,       0xFFC3 },
113         { EV_F7,       0xFFC4 },
114         { EV_F8,       0xFFC5 },
115         { EV_F9,       0xFFC6 },
116         { EV_F10,      0xFFC7 },
117         { EV_F11,      0xFFC8 },
118         { EV_F12,      0xFFC9 },
119 };
120
121 /************************
122  * Conversion functions *
123  ************************/
124
125 static xcb_keycode_t *event_to_keycodes(event_t ev)
126 {
127         xcb_keycode_t *codes = NULL;
128
129         /* Get keysym */
130         xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
131
132         /* Get keycodes */
133         if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
134                 warn("no keycode found for %d->%d", ev, keysym);
135
136         return codes;
137 }
138
139 static event_t keycode_to_event(xcb_keycode_t code)
140 {
141         /* Get keysym */
142         xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
143
144         /* Get event */
145         return map_get(keysym_map, sym,keysym, ev,keysym);
146 }
147
148 /* Button presses */
149 static event_t button_to_event(xcb_button_t btn)
150 {
151         return EV_MOUSE0 + btn;
152 }
153
154 static xcb_button_t event_to_button(event_t ev)
155 {
156         return ev - EV_MOUSE0;
157 }
158
159 /* Modifier masks */
160 static xcb_mod_mask_t mod_to_mask(mod_t mod)
161 {
162         xcb_mod_mask_t mask = 0;
163         if (mod.alt)   mask |= XCB_MOD_MASK_1;
164         if (mod.ctrl)  mask |= XCB_MOD_MASK_CONTROL;
165         if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
166         if (mod.win)   mask |= XCB_MOD_MASK_4;
167         return mask;
168 }
169
170 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
171 {
172         mod_t mod = { .up = up };
173         if (mask & XCB_MOD_MASK_1)       mod.alt   = 1;
174         if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl  = 1;
175         if (mask & XCB_MOD_MASK_SHIFT)   mod.shift = 1;
176         if (mask & XCB_MOD_MASK_4)       mod.win   = 1;
177         return mod;
178 }
179
180 /* Mouse pointers */
181 static ptr_t list_to_ptr(int16_t *list)
182 {
183         ptr_t ptr = {};
184         if (list) {
185                 ptr.rx = list[0]; // root_x
186                 ptr.ry = list[1]; // root_y
187                 ptr.x  = list[2]; // event_x
188                 ptr.y  = list[3]; // event_y
189         }
190         return ptr;
191 }
192
193 /********************
194  * Window functions *
195  ********************/
196
197 static int win_cmp(const void *_a, const void *_b)
198 {
199         const win_t *a = _a;
200         const win_t *b = _b;
201         if (a->sys->xcb < b->sys->xcb) return -1;
202         if (a->sys->xcb > b->sys->xcb) return  1;
203         return 0;
204 }
205
206 static win_t *win_get(xcb_window_t xcb)
207 {
208         win_sys_t sys = { .xcb =  xcb };
209         win_t     key = { .sys = &sys };
210
211         win_t   **win = tfind(&key, &cache, win_cmp);
212
213         if (!win) {
214                 warn("no window for %u", xcb);
215                 return NULL;
216         }
217
218         return *win;
219 }
220
221 static win_t *win_new(xcb_window_t xcb)
222 {
223         win_t *win = new0(win_t);
224         win->sys = new0(win_sys_t);
225         win->sys->xcb = xcb;
226
227         win_t **old = tfind(win, &cache, win_cmp);
228         if (old) {
229                 warn("duplicate window for %u\n", xcb);
230                 free(win->sys);
231                 free(win);
232                 return *old;
233         }
234
235         tsearch(win, &cache, win_cmp);
236         printf("win_new: xcb=%-8u -> win=%p\n",
237                         win->sys->xcb, win);
238         return win;
239 }
240
241 static void win_free(win_t *win)
242 {
243         printf("win_free: xcb=%-8u -> win=%p\n",
244                         win->sys->xcb, win);
245         free(win->sys);
246         free(win);
247 }
248
249 static void win_add_strut(win_t *win)
250 {
251         win->type = TYPE_TOOLBAR;
252         for (list_t *cur = screens; cur; cur = cur->next) {
253                 win_t   *screen = cur->data;
254                 strut_t *strut  = &win->sys->strut;
255                 screen->x += strut->left;
256                 screen->y += strut->top;
257                 screen->w -= strut->left + strut->right;
258                 screen->h -= strut->top  + strut->bottom;
259         }
260         struts = list_insert(struts, win);
261
262 }
263
264 static void win_del_strut(win_t *win)
265 {
266         list_t *link = list_find(struts, win);
267         if (!link)
268                 return;
269         for (list_t *cur = screens; cur; cur = cur->next) {
270                 win_t   *screen = cur->data;
271                 strut_t *strut  = &win->sys->strut;
272                 screen->x -= strut->left;
273                 screen->y -= strut->top;
274                 screen->w += strut->left + strut->right;
275                 screen->h += strut->top  + strut->bottom;
276         }
277         struts = list_remove(struts, link, 0);
278 }
279
280 /****************
281  * XCB Wrappers *
282  ****************/
283
284 static void *do_query_tree(xcb_window_t xcb, xcb_window_t **kids, int *nkids)
285 {
286         xcb_query_tree_cookie_t cookie =
287                 xcb_query_tree(conn, xcb);
288         if (!cookie.sequence)
289                 return warn("do_query_tree: %d - bad cookie", xcb), NULL;
290
291         xcb_query_tree_reply_t *reply =
292                 xcb_query_tree_reply(conn, cookie, NULL);
293         if (!reply)
294                 return warn("do_query_tree: %d - no reply", xcb), NULL;
295
296         *nkids = xcb_query_tree_children_length(reply);
297         *kids  = xcb_query_tree_children(reply);
298         printf("do_query_tree: %d - n=%d\n", xcb, *nkids);
299         return reply;
300 }
301
302 static int do_get_geometry(xcb_window_t xcb,
303                 int *x, int *y, int *w, int *h)
304 {
305         xcb_get_geometry_cookie_t cookie =
306                 xcb_get_geometry(conn, xcb);
307         if (!cookie.sequence)
308                 return warn("do_get_geometry: %d - bad cookie", xcb);
309
310         xcb_get_geometry_reply_t *reply =
311                 xcb_get_geometry_reply(conn, cookie, NULL);
312         if (!reply)
313                 return warn("do_get_geometry: %d - no reply", xcb);
314
315         printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
316                         xcb, reply->width, reply->height, reply->x, reply->y);
317         *x = reply->x;
318         *y = reply->y;
319         *w = reply->width;
320         *h = reply->height;
321         free(reply);
322         return 1;
323 }
324
325 static int do_get_window_attributes(xcb_window_t xcb,
326                 int *override, int *mapped)
327 {
328         xcb_get_window_attributes_cookie_t cookie =
329                 xcb_get_window_attributes(conn, xcb);
330         if (!cookie.sequence)
331                 return warn("do_get_window_attributes: %d - bad cookie", xcb);
332
333         xcb_get_window_attributes_reply_t *reply =
334                 xcb_get_window_attributes_reply(conn, cookie, NULL);
335         if (!reply)
336                 return warn("do_get_window_attributes: %d - no reply ", xcb);
337
338         printf("do_get_window_attributes: %d - %d\n",
339                         xcb, reply->override_redirect);
340         *override = reply->override_redirect;
341         *mapped   = reply->map_state != XCB_MAP_STATE_UNMAPPED;
342         free(reply);
343         return 1;
344 }
345
346 static int do_xinerama_check(void)
347 {
348         const xcb_query_extension_reply_t *data =
349                 xcb_get_extension_data(conn, &xcb_xinerama_id);
350         if (!data || !data->present)
351                 return warn("do_xinerama_check: no ext");
352
353         xcb_xinerama_is_active_cookie_t cookie =
354                 xcb_xinerama_is_active(conn);
355         if (!cookie.sequence)
356                 return warn("do_xinerama_check: no cookie");
357
358         xcb_xinerama_is_active_reply_t *reply =
359                 xcb_xinerama_is_active_reply(conn, cookie, NULL);
360         if (!reply)
361                 return warn("do_xinerama_check: no reply");
362
363         printf("do_xinerama_check: %d\n", reply->state);
364         int state = reply->state;
365         free(reply);
366         return state;
367 }
368
369 static void *do_query_screens(xcb_xinerama_screen_info_t **info, int *ninfo)
370 {
371         xcb_xinerama_query_screens_cookie_t cookie =
372                 xcb_xinerama_query_screens(conn);
373         if (!cookie.sequence)
374                 return warn("do_query_screens: bad cookie"), NULL;
375
376         xcb_xinerama_query_screens_reply_t *reply =
377                 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
378         if (!reply)
379                 return warn("do_query_screens: no reply"), NULL;
380
381         *ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
382         *info  = xcb_xinerama_query_screens_screen_info(reply);
383         printf("do_query_screens: %d screens\n", *ninfo);
384         return reply;
385 }
386
387 static int do_get_input_focus(void)
388 {
389         xcb_get_input_focus_cookie_t cookie =
390                 xcb_get_input_focus(conn);
391         if (!cookie.sequence)
392                 return warn("do_get_input_focus: bad cookie");
393
394         xcb_get_input_focus_reply_t *reply =
395                 xcb_get_input_focus_reply(conn, cookie, NULL);
396         if (!reply)
397                 return warn("do_get_input_focus: no reply");
398
399         int focus = reply->focus;
400         free(reply);
401         return focus;
402 }
403
404 static xcb_atom_t do_intern_atom(const char *name)
405 {
406         xcb_intern_atom_cookie_t cookie =
407                 xcb_intern_atom(conn, 0, strlen(name), name);
408         if (!cookie.sequence)
409                 return warn("do_intern_atom: bad cookie");
410
411         xcb_intern_atom_reply_t *reply =
412                 xcb_intern_atom_reply(conn, cookie, NULL);
413         if (!reply)
414                 return warn("do_intern_atom: no reply");
415
416         xcb_atom_t atom = reply->atom;
417         free(reply);
418         return atom;
419 }
420
421 static char *do_get_atom_name(xcb_atom_t atom)
422 {
423         xcb_get_atom_name_cookie_t cookie =
424                 xcb_get_atom_name(conn, atom);
425         if (!cookie.sequence)
426                 return warn("do_get_atom_name: bad cookie"), NULL;
427
428         xcb_get_atom_name_reply_t *reply =
429                 xcb_get_atom_name_reply(conn, cookie, NULL);
430         if (!reply)
431                 return warn("do_get_atom_name: no reply"), NULL;
432
433         char *name = xcb_get_atom_name_name(reply);
434         int len = xcb_get_atom_name_name_length(reply);
435         char *str = strndup(name, len);
436         free(reply);
437         return str;
438 }
439
440 static int do_ewmh_init_atoms(void)
441 {
442         xcb_intern_atom_cookie_t *cookies =
443                 xcb_ewmh_init_atoms(conn, &ewmh);
444         if (!cookies)
445                 return warn("do_ewmh_init_atoms: no cookies");
446
447         int status =
448                 xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
449         if (!status)
450                 return warn("do_ewmh_init_atoms: no status");
451         return status;
452 }
453
454 static int do_get_type(xcb_window_t xcb, type_t *type)
455 {
456         xcb_get_property_cookie_t cookie =
457                 xcb_ewmh_get_wm_window_type(&ewmh, xcb);
458         if (!cookie.sequence)
459                 return warn("do_get_type: bad cookie");
460
461         xcb_ewmh_get_atoms_reply_t reply;
462         if (!xcb_ewmh_get_wm_window_type_reply(&ewmh, cookie, &reply, NULL))
463                 return warn("do_get_type: no reply");
464
465         type_t prev = *type;
466         for (int i = 0; i < reply.atoms_len; i++) {
467                 if (reply.atoms[i] == ewmh._NET_WM_WINDOW_TYPE_DOCK)
468                         *type = TYPE_TOOLBAR;
469                 if (reply.atoms[i] == ewmh._NET_WM_WINDOW_TYPE_TOOLBAR)
470                         *type = TYPE_TOOLBAR;
471                 if (reply.atoms[i] == ewmh._NET_WM_WINDOW_TYPE_DIALOG)
472                         *type = TYPE_DIALOG;
473         }
474         printf("do_get_type: %d -> %d\n", prev, *type);
475         return 1;
476 }
477
478 static int do_get_icccm_state(xcb_window_t xcb, state_t *state)
479 {
480         xcb_get_property_cookie_t cookie;
481
482         cookie = xcb_icccm_get_wm_normal_hints(conn, xcb);
483         if (!cookie.sequence)
484                 return warn("do_get_icccm_state: bad cookie1");
485
486         xcb_size_hints_t sizes;
487         if (!xcb_icccm_get_wm_normal_hints_reply(conn, cookie, &sizes, NULL))
488                 return warn("do_get_icccm_state: no sizes");
489
490         state_t prev = *state;
491         printf("do_get_icccm_state: %s -> %s\n",
492                         state_map[prev], state_map[*state]);
493         return 1;
494 }
495
496 static int do_get_ewmh_state(xcb_window_t xcb, state_t *state)
497 {
498         xcb_get_property_cookie_t cookie;
499
500         cookie = xcb_ewmh_get_wm_state(&ewmh, xcb);
501         if (!cookie.sequence)
502                 return warn("do_get_ewmh_state: bad cookie2");
503
504         xcb_ewmh_get_atoms_reply_t states;
505         if (!xcb_ewmh_get_wm_state_reply(&ewmh, cookie, &states, NULL))
506                 return warn("do_get_ewmh_state: no reply2");
507
508         state_t prev = *state;
509         for (int i = 0; i < states.atoms_len; i++)
510                 if (states.atoms[i] == ewmh._NET_WM_STATE_FULLSCREEN)
511                         *state = ST_FULL;
512
513         printf("do_get_ewmh_state: %s -> %s\n",
514                         state_map[prev], state_map[*state]);
515         return 1;
516 }
517
518 static int do_get_strut(xcb_window_t xcb, strut_t *strut)
519 {
520         xcb_get_property_cookie_t cookie =
521                 xcb_ewmh_get_wm_strut(&ewmh, xcb);
522         if (!cookie.sequence)
523                 return warn("do_get_strut: bad cookie");
524
525         xcb_ewmh_get_extents_reply_t ext = {};
526         int status =
527                 xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
528         if (!status)
529                 return warn("do_get_strut: no status");
530
531         strut->left   = ext.left;
532         strut->right  = ext.right;
533         strut->top    = ext.top;
534         strut->bottom = ext.bottom;
535
536         return ext.left || ext.right || ext.top || ext.bottom;
537 }
538
539 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
540 {
541         uint16_t r = (rgb & 0xFF0000) >> 8;
542         uint16_t g = (rgb & 0x00FF00);
543         uint16_t b = (rgb & 0x0000FF) << 8;
544         xcb_alloc_color_cookie_t cookie =
545                 xcb_alloc_color(conn, colormap, r, g, b);
546         if (!cookie.sequence)
547                 return warn("do_alloc_color: bad cookie");
548
549         xcb_alloc_color_reply_t *reply =
550                 xcb_alloc_color_reply(conn, cookie, NULL);
551         if (!reply)
552                 return warn("do_alloc_color: no reply");
553
554         printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
555         xcb_pixmap_t pixel = reply->pixel;
556         free(reply);
557         return pixel;
558 }
559
560 static void do_grab_pointer(xcb_event_mask_t mask)
561 {
562         if (!grabbed)
563                 xcb_grab_pointer(conn, 0, root, mask,
564                                 XCB_GRAB_MODE_ASYNC,
565                                 XCB_GRAB_MODE_ASYNC,
566                                 0, 0, XCB_CURRENT_TIME);
567         grabbed++;
568 }
569
570 static void do_ungrab_pointer(void)
571 {
572         grabbed--;
573         if (!grabbed)
574                 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
575 }
576
577 static void do_configure_window(xcb_window_t xcb,
578                 int x, int y, int w, int h,
579                 int b, int s, int r)
580 {
581         int table[][2] = {
582                 { x, XCB_CONFIG_WINDOW_X            },
583                 { y, XCB_CONFIG_WINDOW_Y            },
584                 { w, XCB_CONFIG_WINDOW_WIDTH        },
585                 { h, XCB_CONFIG_WINDOW_HEIGHT       },
586                 { b, XCB_CONFIG_WINDOW_BORDER_WIDTH },
587                 { s, XCB_CONFIG_WINDOW_SIBLING      },
588                 { r, XCB_CONFIG_WINDOW_STACK_MODE   },
589         };
590
591         uint16_t mask = 0;
592         uint32_t list[countof(table)];
593         for (int i=0,j=0; i < countof(table); i++) {
594                 if (table[i][0] >= 0) {
595                         list[j++] = table[i][0];
596                         mask     |= table[i][1];
597                 }
598         }
599
600         xcb_configure_window(conn, xcb, mask, list);
601 }
602
603 static int do_client_message(xcb_window_t xcb, xcb_atom_t atom)
604 {
605         /* Get protocols */
606         xcb_get_property_cookie_t cookie =
607                 xcb_icccm_get_wm_protocols(conn, xcb, wm_protos);
608         if (!cookie.sequence)
609                 return warn("do_client_message: %d - bad cookie", xcb);
610
611         xcb_icccm_get_wm_protocols_reply_t protos = {};
612         if (!xcb_icccm_get_wm_protocols_reply(conn, cookie, &protos, NULL))
613                 return warn("do_client_message: %d - no reply", xcb);
614
615         /* Search for the atom */
616         int found = 0;
617         for (int i = 0; i < protos.atoms_len; i++)
618                 if (protos.atoms[i] == atom)
619                         found = 1;
620         xcb_icccm_get_wm_protocols_reply_wipe(&protos);
621         if (!found)
622                 return warn("do_client_message: %d - no atom", xcb);
623
624         /* Send the message */
625         xcb_client_message_event_t msg = {
626                 .response_type  = XCB_CLIENT_MESSAGE,
627                 .format         = 32,
628                 .window         = xcb,
629                 .type           = wm_protos,
630                 .data.data32[0] = atom,
631                 .data.data32[1] = XCB_CURRENT_TIME,
632         };
633         xcb_send_event(conn, 0, xcb, XCB_EVENT_MASK_NO_EVENT,
634                         (const char *)&msg);
635         return 1;
636 }
637
638 /**************************
639  * Window Manager Helpers *
640  **************************/
641
642 /* Send event info */
643 static int send_event(event_t ev, xcb_window_t ewin)
644 {
645         win_t *win = win_get(ewin);
646         do_grab_pointer(0);
647         int status = wm_handle_event(win, ev, MOD(), PTR());
648         do_ungrab_pointer();
649         return status;
650 }
651
652 /* Send event info */
653 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
654                 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
655 {
656         xcb_window_t xcb = ewin == rwin ? cwin : ewin;
657         win_t *win = win_get(xcb);
658         mod_t  mod = mask_to_mod(mask, up);
659         ptr_t  ptr = list_to_ptr(pos);
660         do_grab_pointer(0);
661         int status = wm_handle_event(win, ev, mod, ptr);
662         do_ungrab_pointer();
663         return status;
664 }
665
666 /* Send pointer motion info */
667 static int send_pointer(int16_t *pos,
668                 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
669 {
670         xcb_window_t xcb = ewin == rwin ? cwin : ewin;
671         win_t *win = win_get(xcb);
672         ptr_t  ptr = list_to_ptr(pos);
673         do_grab_pointer(0);
674         int status = wm_handle_ptr(win, ptr);
675         do_ungrab_pointer();
676         return status;
677 }
678
679 /* Send window state info */
680 static void send_manage(win_t *win, int managed)
681 {
682         if (win->sys->managed == managed)
683                 return;
684         if (managed)
685                 wm_insert(win);
686         else
687                 wm_remove(win);
688         win->sys->managed = managed;
689 }
690
691 /* Send window state info */
692 static void send_state(win_t *win, state_t next)
693 {
694         if (!win->sys->managed)
695                 return;
696         if (!win->sys->mapped)
697                 return;
698         if (win->state == next)
699                 return;
700         state_t prev = win->state;
701         win->state = next;
702         wm_handle_state(win, prev, next);
703 }
704
705 /**********************
706  * X11 Event Handlers *
707  **********************/
708
709 /* Specific events */
710 static void on_key_event(xcb_key_press_event_t *event, int up)
711 {
712         printf("on_key_event:         xcb=%-8u\n", event->event);
713         xcb_window_t focus = do_get_input_focus();
714         event_t ev = keycode_to_event(event->detail);
715         send_event_info(ev, event->state, up, &event->root_x,
716                 event->root, focus, event->child);
717 }
718
719 static void on_button_event(xcb_button_press_event_t *event, int up)
720 {
721         printf("on_button_event:      xcb=%-8u\n", event->event);
722         event_t ev = button_to_event(event->detail);
723         if (!send_event_info(ev, event->state, up, &event->root_x,
724                                 event->root, event->event, event->child))
725                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
726         else if (!up)
727                 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
728                                 XCB_EVENT_MASK_BUTTON_RELEASE);
729         else
730                 do_ungrab_pointer();
731
732 }
733
734 static void on_motion_notify(xcb_motion_notify_event_t *event)
735 {
736         printf("on_motion_notify:     xcb=%-8u - %d,%d / %d.%d\n", event->event,
737                         event->event_x, event->event_y,
738                         event->root_x,  event->root_y);
739         send_pointer(&event->root_x, event->root, event->event, event->child);
740 }
741
742 static void on_enter_notify(xcb_enter_notify_event_t *event)
743 {
744         if (event->mode != XCB_NOTIFY_MODE_NORMAL)
745                 return;
746         printf("on_enter_notify:      xcb=%-8u\n", event->event);
747         send_event_info(EV_ENTER, event->state, 0, &event->root_x,
748                 event->root, event->event, event->child);
749 }
750
751 static void on_leave_notify(xcb_leave_notify_event_t *event)
752 {
753         if (event->mode != XCB_NOTIFY_MODE_NORMAL)
754                 return;
755         printf("on_leave_notify:      xcb=%-8u\n", event->event);
756         send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
757                 event->root, event->event, event->child);
758 }
759
760 static void on_focus_in(xcb_focus_in_event_t *event)
761 {
762         if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
763             event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
764                 return;
765         printf("on_focus_in:          xcb=%-8u mode=%d\n", event->event, event->mode);
766         xcb_change_window_attributes(conn, event->event,
767                         XCB_CW_BORDER_PIXEL, &clr_focus);
768         if (event->mode == XCB_NOTIFY_MODE_NORMAL)
769                 send_event(EV_FOCUS, event->event);
770 }
771
772 static void on_focus_out(xcb_focus_out_event_t *event)
773 {
774         if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
775             event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
776                 return;
777         printf("on_focus_out:         xcb=%-8u mode=%d\n", event->event, event->mode);
778         xcb_change_window_attributes(conn, event->event,
779                         XCB_CW_BORDER_PIXEL, &clr_unfocus);
780         if (event->mode == XCB_NOTIFY_MODE_NORMAL)
781                 send_event(EV_UNFOCUS, event->event);
782 }
783
784 static void on_create_notify(xcb_create_notify_event_t *event)
785 {
786         printf("on_create_notify:     xcb=%-8u\n", event->window);
787
788         win_t *win = win_new(event->window);
789
790         win->x = event->x;
791         win->y = event->y;
792         win->w = event->width;
793         win->h = event->height;
794
795         win->sys->state  = ST_SHOW;
796         win->sys->events = XCB_EVENT_MASK_PROPERTY_CHANGE;
797         xcb_change_window_attributes(conn, event->window,
798                         XCB_CW_EVENT_MASK, &win->sys->events);
799
800         if (!event->override_redirect)
801                 send_manage(win, 1);
802 }
803
804 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
805 {
806         win_t *win = win_get(event->window);
807         printf("on_destroy_notify:    xcb=%-8u -> win=%p\n",
808                         event->window, win);
809         if (!win) return;
810
811         send_manage(win, 0);
812         tdelete(win, &cache, win_cmp);
813         win_free(win);
814 }
815
816 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
817 {
818         win_t *win = win_get(event->window);
819         printf("on_unmap_notify:      xcb=%-8u -> win=%p\n",
820                         event->window, win);
821         if (!win) return;
822
823         win_del_strut(win);
824         send_state(win, ST_HIDE);
825         win->sys->mapped = 0;
826 }
827
828 static void on_map_notify(xcb_map_notify_event_t *event)
829 {
830         win_t *win = win_get(event->window);
831         printf("on_map_notify:        xcb=%-8u -> win=%p\n",
832                         event->window, win);
833         if (!win) return;
834
835         win->sys->mapped = 1;
836         send_state(win, win->sys->state);
837 }
838
839 static void on_map_request(xcb_map_request_event_t *event)
840 {
841         win_t *win = win_get(event->window);
842         printf("on_map_request:       xcb=%-8u -> win=%p\n",
843                         event->window, win);
844         if (!win) return;
845
846         win->sys->mapped = 1;
847         send_state(win, win->sys->state);
848         xcb_map_window(conn, win->sys->xcb);
849         if (!win->sys->managed)
850                 sys_move(win, win->x, win->y, win->w, win->h);
851 }
852
853 static void on_configure_request(xcb_configure_request_event_t *event)
854 {
855         win_t *win = win_get(event->window);
856         printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
857                         event->window, win,
858                         event->width, event->height,
859                         event->x, event->y);
860         if (!win) return;
861         printf("on_configure_request: xcb=%-8u -> win=%p << %dx%d @ %d,%d\n",
862                         event->window, win,
863                         win->w, win->h,
864                         win->x, win->y);
865
866         if (!win->sys->managed) {
867                 win->x = event->x;
868                 win->y = event->y;
869                 win->w = event->width;
870                 win->h = event->height;
871         }
872
873         xcb_configure_notify_event_t resp = {
874                 .response_type = XCB_CONFIGURE_NOTIFY,
875                 .event         = win->sys->xcb,
876                 .window        = win->sys->xcb,
877                 .x             = win->x,
878                 .y             = win->y,
879                 .width         = win->w,
880                 .height        = win->h,
881                 .border_width  = border,
882         };
883
884         xcb_send_event(conn, 0, win->sys->xcb,
885                         XCB_EVENT_MASK_STRUCTURE_NOTIFY,
886                         (const char *)&resp);
887 }
888
889 static void on_property_notify(xcb_property_notify_event_t *event)
890 {
891         win_t *win = win_get(event->window);
892         char *name = do_get_atom_name(event->atom);
893         printf("on_property_notify: xcb=%-8u -> win=%p - %s\n",
894                         event->window, win, name);
895         if (name) free(name);
896         if (!win) return;
897
898         /* Check window type */
899         if (event->atom == ewmh._NET_WM_WINDOW_TYPE)
900                 do_get_type(event->window, &win->type);
901
902         /* Check struts */
903         if (event->atom == ewmh._NET_WM_STRUT)
904                 if (do_get_strut(win->sys->xcb, &win->sys->strut))
905                         win_add_strut(win);
906
907         /* Check window state */
908         if (event->atom == wm_nhints)
909                 if (do_get_icccm_state(event->window, &win->sys->state))
910                         send_state(win, win->sys->state);
911         if (event->atom == ewmh._NET_WM_STATE)
912                 if (do_get_ewmh_state(event->window, &win->sys->state))
913                         send_state(win, win->sys->state);
914 }
915
916 static void on_client_message(xcb_client_message_event_t *event)
917 {
918         win_t *win = win_get(event->window);
919         char *name = do_get_atom_name(event->type);
920         printf("on_client_message: xcb=%-8u -> win=%p - %s=[%d,%d,%d,%d]\n",
921                         event->window, win, name,
922                         event->data.data32[0], event->data.data32[1],
923                         event->data.data32[2], event->data.data32[3]);
924         if (name) free(name);
925         if (!win) return;
926
927         /* Exit request */
928         if (event->window         == control   &&
929             event->type           == wm_protos &&
930             event->data.data32[0] == wm_delete) {
931                 printf("on_client_message: shutdown request");
932                 running = 0;
933         }
934
935         /* Close request */
936         if (event->type == ewmh._NET_CLOSE_WINDOW) {
937                 printf("on_client_message: close request");
938                 sys_show(win, ST_CLOSE);
939         }
940
941         /* Fullscreen request  */
942         if ((event->type           == ewmh._NET_WM_STATE) &&
943             (event->data.data32[1] == ewmh._NET_WM_STATE_FULLSCREEN ||
944              event->data.data32[2] == ewmh._NET_WM_STATE_FULLSCREEN)) {
945                 printf("on_client_message: fullscreen request");
946                 int full = win->state == ST_FULL;
947                 switch (event->data.data32[0]) {
948                         case XCB_EWMH_WM_STATE_REMOVE: full  = 0; break;
949                         case XCB_EWMH_WM_STATE_ADD:    full  = 1; break;
950                         case XCB_EWMH_WM_STATE_TOGGLE: full ^= 1; break;
951                 }
952                 win->sys->state = full ? ST_FULL : ST_SHOW;
953                 send_state(win, win->sys->state);
954                 sys_show(win, win->sys->state);
955         }
956
957 }
958
959 /* Generic Event */
960 static void on_event(xcb_generic_event_t *event)
961 {
962         int type = XCB_EVENT_RESPONSE_TYPE(event);
963
964         switch (type) {
965                 /* Input handling */
966                 case XCB_KEY_PRESS:
967                         on_key_event((xcb_key_press_event_t *)event, 0);
968                         break;
969                 case XCB_KEY_RELEASE:
970                         on_key_event((xcb_key_release_event_t *)event, 1);
971                         break;
972                 case XCB_BUTTON_PRESS:
973                         on_button_event((xcb_button_press_event_t *)event, 0);
974                         break;
975                 case XCB_BUTTON_RELEASE:
976                         on_button_event((xcb_button_release_event_t *)event, 1);
977                         break;
978                 case XCB_MOTION_NOTIFY:
979                         on_motion_notify((xcb_motion_notify_event_t *)event);
980                         break;
981                 case XCB_ENTER_NOTIFY:
982                         on_enter_notify((xcb_enter_notify_event_t *)event);
983                         break;
984                 case XCB_LEAVE_NOTIFY:
985                         on_leave_notify((xcb_leave_notify_event_t *)event);
986                         break;
987                 case XCB_FOCUS_IN:
988                         on_focus_in((xcb_focus_in_event_t *)event);
989                         break;
990                 case XCB_FOCUS_OUT:
991                         on_focus_out((xcb_focus_out_event_t *)event);
992                         break;
993
994                 /* Window management */
995                 case XCB_CREATE_NOTIFY:
996                         on_create_notify((xcb_create_notify_event_t *)event);
997                         break;
998                 case XCB_DESTROY_NOTIFY:
999                         on_destroy_notify((xcb_destroy_notify_event_t *)event);
1000                         break;
1001                 case XCB_UNMAP_NOTIFY:
1002                         on_unmap_notify((xcb_unmap_notify_event_t *)event);
1003                         break;
1004                 case XCB_MAP_NOTIFY:
1005                         on_map_notify((xcb_map_notify_event_t *)event);
1006                         break;
1007                 case XCB_MAP_REQUEST:
1008                         on_map_request((xcb_map_request_event_t *)event);
1009                         break;
1010                 case XCB_CONFIGURE_REQUEST:
1011                         on_configure_request((xcb_configure_request_event_t *)event);
1012                         break;
1013                 case XCB_PROPERTY_NOTIFY:
1014                         on_property_notify((xcb_property_notify_event_t *)event);
1015                         break;
1016                 case XCB_CLIENT_MESSAGE:
1017                         on_client_message((xcb_client_message_event_t *)event);
1018                         break;
1019
1020                 /* Unknown events */
1021                 default:
1022                         printf("on_event: %d:%02X -> %s\n",
1023                                 XCB_EVENT_SENT(event) != 0,
1024                                 XCB_EVENT_RESPONSE_TYPE(event),
1025                                 xcb_event_get_label(type) ?: "unknown_event");
1026                         break;
1027         }
1028 }
1029
1030 /********************
1031  * System functions *
1032  ********************/
1033
1034 void sys_move(win_t *win, int x, int y, int w, int h)
1035 {
1036         printf("sys_move:  %p - %dx%d @ %d,%d\n",
1037                         win, w, h, x, y);
1038
1039         int b = 2*border;
1040
1041         win->x = x;
1042         win->y = y;
1043         win->w = MAX(w,1+b);
1044         win->h = MAX(h,1+b);
1045         w      = MAX(w-b,1);
1046         h      = MAX(h-b,1);
1047
1048         do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
1049 }
1050
1051 void sys_raise(win_t *win)
1052 {
1053         printf("sys_raise: %p\n", win);
1054
1055         uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
1056         uint32_t list = XCB_STACK_MODE_ABOVE;
1057
1058         xcb_configure_window(conn, win->sys->xcb, mask, &list);
1059         for (list_t *cur = struts; cur; cur = cur->next)
1060                 xcb_configure_window(conn,
1061                         ((win_t*)cur->data)->sys->xcb, mask, &list);
1062 }
1063
1064 void sys_focus(win_t *win)
1065 {
1066         printf("sys_focus: %p\n", win);
1067         xcb_window_t xcb = win ? win->sys->xcb : root;
1068
1069         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
1070                         xcb, XCB_CURRENT_TIME);
1071 }
1072
1073 void sys_show(win_t *win, state_t state)
1074 {
1075         printf("sys_show:  %p - %s -> %s\n", win,
1076                         state_map[win->state], state_map[state]);
1077         xcb_window_t xcb = win ? win->sys->xcb : root;
1078
1079         /* Find screen */
1080         win_t full, max;
1081         if (state == ST_FULL || state == ST_MAX) {
1082                 for (list_t *cur = screens; cur; cur = cur->next) {
1083                         full = max = *(win_t*)cur->data;
1084                         if (win->x >= max.x && win->x <= max.x+max.w &&
1085                             win->y >= max.y && win->y <= max.y+max.h)
1086                                 break;
1087                 }
1088                 for (list_t *cur = struts; cur; cur = cur->next) {
1089                         strut_t *strut = &((win_t*)cur->data)->sys->strut;
1090                         full.x -= strut->left;
1091                         full.y -= strut->top;
1092                         full.w += strut->left + strut->right;
1093                         full.h += strut->top  + strut->bottom;
1094                 }
1095         }
1096
1097         /* Change window state */
1098         switch (state) {
1099                 case ST_HIDE:
1100                         xcb_unmap_window(conn, xcb);
1101                         break;
1102
1103                 case ST_SHOW:
1104                         xcb_map_window(conn, xcb);
1105                         do_configure_window(xcb, win->x, win->y,
1106                                         MAX(win->w - 2*border, 1),
1107                                         MAX(win->h - 2*border, 1),
1108                                         border, -1, -1);
1109                         break;
1110
1111                 case ST_FULL:
1112                         xcb_map_window(conn, xcb);
1113                         do_configure_window(xcb, full.x, full.y, full.w, full.h,
1114                                         0, -1, XCB_STACK_MODE_ABOVE);
1115                         break;
1116
1117                 case ST_MAX:
1118                         xcb_map_window(conn, xcb);
1119                         do_configure_window(xcb, max.x, max.y,
1120                                         MAX(max.w - 2*border, 1),
1121                                         MAX(max.h - 2*border, 1),
1122                                         border, -1, XCB_STACK_MODE_ABOVE);
1123                         break;
1124
1125                 case ST_SHADE:
1126                         xcb_map_window(conn, xcb);
1127                         do_configure_window(xcb, -1, -1, -1, stack,
1128                                         border, -1, -1);
1129                         break;
1130
1131                 case ST_ICON:
1132                         xcb_map_window(conn, xcb);
1133                         do_configure_window(xcb, -1, -1, 100, 100,
1134                                         border, -1, -1);
1135                         break;
1136
1137                 case ST_CLOSE:
1138                         if (!do_client_message(xcb, wm_delete))
1139                                 xcb_kill_client(conn, xcb);
1140                         break;
1141         }
1142
1143         /* Update state */
1144         win->sys->state = win->state = state;
1145 }
1146
1147 void sys_watch(win_t *win, event_t ev, mod_t mod)
1148 {
1149         printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
1150         xcb_window_t      xcb  = win ? win->sys->xcb     : root;
1151         xcb_event_mask_t *mask = win ? &win->sys->events : &events;
1152         xcb_mod_mask_t    mods = 0;
1153         xcb_button_t      btn  = 0;
1154         xcb_keycode_t    *code = 0;
1155
1156         switch (ev) {
1157                 case EV_ENTER:
1158                         *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
1159                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
1160                         break;
1161
1162                 case EV_LEAVE:
1163                         *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
1164                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
1165                         break;
1166
1167                 case EV_FOCUS:
1168                 case EV_UNFOCUS:
1169                         *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
1170                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
1171                         break;
1172
1173                 case EV_MOUSE0...EV_MOUSE7:
1174                         btn    = event_to_button(ev);
1175                         mods   = mod_to_mask(mod);
1176                         *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
1177                                         : XCB_EVENT_MASK_BUTTON_PRESS;
1178                         xcb_grab_button(conn, 0, xcb, *mask,
1179                                         XCB_GRAB_MODE_ASYNC,
1180                                         XCB_GRAB_MODE_ASYNC,
1181                                         0, 0, btn, mods);
1182                         break;
1183
1184                 default:
1185                         code = event_to_keycodes(ev);
1186                         mods = mod_to_mask(mod);
1187                         for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
1188                                 xcb_grab_key(conn, 1, xcb, mods, code[i],
1189                                                 XCB_GRAB_MODE_ASYNC,
1190                                                 XCB_GRAB_MODE_ASYNC);
1191                         free(code);
1192                         break;
1193         }
1194 }
1195
1196 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
1197 {
1198         printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
1199 }
1200
1201 list_t *sys_info(void)
1202 {
1203         printf("sys_info\n");
1204
1205         if (screens == NULL && do_xinerama_check()) {
1206                 /* Add Xinerama screens */
1207                 int ninfo = 0;
1208                 xcb_xinerama_screen_info_t *info = NULL;
1209                 void *reply = do_query_screens(&info, &ninfo);
1210                 for (int i = 0; i < ninfo; i++) {
1211                         win_t *screen = new0(win_t);
1212
1213                         screen->x = info[i].x_org;
1214                         screen->y = info[i].y_org;
1215                         screen->w = info[i].width;
1216                         screen->h = info[i].height;
1217
1218                         screens = list_insert(NULL, screen);
1219
1220                         printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
1221                                         screen->w, screen->h,
1222                                         screen->x, screen->y);
1223                 }
1224                 free(reply);
1225         }
1226
1227         if (screens == NULL) {
1228                 /* No xinerama support */
1229                 const xcb_setup_t *setup = xcb_get_setup(conn);
1230                 xcb_screen_t      *geom  = xcb_setup_roots_iterator(setup).data;
1231
1232                 win_t *screen = new0(win_t);
1233
1234                 screen->w = geom->width_in_pixels;
1235                 screen->h = geom->height_in_pixels;
1236
1237                 screens = list_insert(NULL, screen);
1238
1239                 printf("sys_info: root screen - %dx%d\n",
1240                                 screen->w, screen->h);
1241         }
1242
1243         return screens;
1244 }
1245
1246 void sys_init(void)
1247 {
1248         printf("sys_init\n");
1249
1250         xcb_void_cookie_t cookie;
1251         xcb_generic_error_t *err;
1252
1253         /* Load configuration */
1254         stack      = conf_get_int("main.stack",      stack);
1255         border     = conf_get_int("main.border",     border);
1256         no_capture = conf_get_int("main.no-capture", no_capture);
1257
1258         /* Connect to display */
1259         if (!(conn = xcb_connect(NULL, NULL)))
1260                 error("xcb connect failed");
1261         if (xcb_connection_has_error(conn))
1262                 error("xcb connection has errors");
1263
1264         /* Get root window */
1265         const xcb_setup_t     *setup = xcb_get_setup(conn);
1266         xcb_screen_iterator_t  iter  = xcb_setup_roots_iterator(setup);
1267         root     = iter.data->root;
1268         colormap = iter.data->default_colormap;
1269
1270         /* Request substructure redirect */
1271         events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1272                  XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
1273         cookie = xcb_change_window_attributes_checked(conn, root,
1274                         XCB_CW_EVENT_MASK, &events);
1275         if ((err = xcb_request_check(conn, cookie)))
1276                 error("another window manager is already running");
1277
1278         /* Setup X Atoms */
1279         wm_protos = do_intern_atom("WM_PROTOCOLS");
1280         wm_delete = do_intern_atom("WM_DELETE_WINDOW");
1281         wm_nhints = do_intern_atom("WM_NORMAL_HINTS");
1282         if (!wm_protos || !wm_delete || !wm_nhints)
1283                 error("unable to setup atoms");
1284
1285         /* Setup EWMH connection */
1286         if (!do_ewmh_init_atoms())
1287                 error("ewmh setup failed");
1288         xcb_ewmh_set_supported(&ewmh, 0, 82, &ewmh._NET_SUPPORTED);
1289
1290         /* Set EWMH wm window */
1291         uint32_t override = 1;
1292         control = xcb_generate_id(conn);
1293         printf("control window: %d\n", control);
1294         cookie  = xcb_create_window_checked(conn, 0, control, root,
1295                         0, 0, 1, 1, 0, 0, 0,
1296                         XCB_CW_OVERRIDE_REDIRECT, &override);
1297         if ((err = xcb_request_check(conn, cookie)))
1298                 error("can't create control window");
1299         cookie = xcb_ewmh_set_wm_name_checked(&ewmh, control, 5, "wmpus");
1300         if ((err = xcb_request_check(conn, cookie)))
1301                 error("can't set wm name");
1302         cookie = xcb_ewmh_set_supporting_wm_check_checked(&ewmh, root, control);
1303         if ((err = xcb_request_check(conn, cookie)))
1304                 error("can't set control window");
1305
1306         /* Setup for for ST_CLOSE */
1307         xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
1308
1309         /* Allocate key symbols */
1310         if (!(keysyms = xcb_key_symbols_alloc(conn)))
1311                 error("cannot allocate key symbols");
1312
1313         /* Read color information */
1314         clr_focus   = do_alloc_color(0xFF6060);
1315         clr_unfocus = do_alloc_color(0xD8D8FF);
1316         clr_urgent  = do_alloc_color(0xFF0000);
1317 }
1318
1319 void sys_run(void)
1320 {
1321         printf("sys_run\n");
1322
1323         /* Add each initial window */
1324         if (!no_capture) {
1325                 int nkids = 0;
1326                 xcb_window_t *kids = NULL;
1327                 void *reply = do_query_tree(root, &kids, &nkids);
1328                 for(int i = 0; i < nkids; i++) {
1329                         int override=0, mapped=0;
1330                         if (kids[i] == control)
1331                                 continue;
1332                         win_t *win = win_new(kids[i]);
1333                         if (do_get_strut(win->sys->xcb, &win->sys->strut))
1334                                 win_add_strut(win);
1335                         do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
1336                         do_get_window_attributes(kids[i], &override, &mapped);
1337                         printf("  found %-8u %dx%d @ %d,%d --%s%s\n", kids[i],
1338                                         win->w, win->h, win->x, win->y,
1339                                         override ? " override" : "",
1340                                         mapped   ? " mapped"   : "");
1341                         state_t state = mapped ? ST_SHOW : ST_HIDE;
1342                         win->sys->mapped = mapped;
1343                         do_get_type(kids[i], &win->type);
1344                         do_get_icccm_state(kids[i], &state);
1345                         do_get_ewmh_state(kids[i], &state);
1346                         send_manage(win, !override);
1347                         send_state(win, state);
1348                 }
1349                 free(reply);
1350                 xcb_flush(conn);
1351         }
1352
1353         /* Main loop */
1354         running = 1;
1355         while (running)
1356         {
1357                 int status;
1358                 xcb_generic_event_t *event;
1359                 if (!(event = xcb_wait_for_event(conn)))
1360                         break;
1361                 on_event(event);
1362                 free(event);
1363                 if (!(status = xcb_flush(conn)))
1364                         break;
1365         }
1366 }
1367
1368 void sys_exit(void)
1369 {
1370         printf("sys_exit\n");
1371
1372         xcb_client_message_event_t msg = {
1373                 .response_type  = XCB_CLIENT_MESSAGE,
1374                 .format         = 32,
1375                 .window         = control,
1376                 .type           = wm_protos,
1377                 .data.data32[0] = wm_delete,
1378                 .data.data32[1] = XCB_CURRENT_TIME,
1379         };
1380         xcb_send_event(conn, 0, control, XCB_EVENT_MASK_NO_EVENT,
1381                         (const char *)&msg);
1382         xcb_flush(conn);
1383 }
1384
1385 void sys_free(void)
1386 {
1387         printf("sys_free\n");
1388
1389         xcb_void_cookie_t cookie;
1390         xcb_generic_error_t *err;
1391
1392         /* unregister wm */ 
1393         cookie = xcb_delete_property_checked(conn, root,
1394                         ewmh._NET_SUPPORTING_WM_CHECK);
1395         if ((err = xcb_request_check(conn, cookie)))
1396                 warn("can't remove control window");
1397
1398         cookie = xcb_destroy_window_checked(conn, control);
1399         if ((err = xcb_request_check(conn, cookie)))
1400                 warn("can't destroy control window");
1401
1402         /* close connection */
1403         xcb_ewmh_connection_wipe(&ewmh);
1404         xcb_key_symbols_free(keysyms);
1405         xcb_disconnect(conn);
1406
1407         /* free local data */
1408         while (screens)
1409                 screens = list_remove(screens, screens, 1);
1410         tdestroy(cache, (void(*)(void*))win_free);
1411 }