]> Pileus Git - wmpus/blob - sys-xcb.c
Add focus and enter events
[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_event_mask_t   events;
59 static list_t            *screens;
60 static void              *cache;
61 static xcb_pixmap_t       colors[NCOLORS];
62 static unsigned int       grabbed;
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         if (list) {
155                 ptr.rx = list[0]; // root_x
156                 ptr.ry = list[1]; // root_y
157                 ptr.x  = list[2]; // event_x
158                 ptr.y  = list[3]; // event_y
159         }
160         return ptr;
161 }
162
163 /********************
164  * Window functions *
165  ********************/
166
167 static int win_cmp(const void *_a, const void *_b)
168 {
169         const win_t *a = _a;
170         const win_t *b = _b;
171         if (a->sys->xcb < b->sys->xcb) return -1;
172         if (a->sys->xcb > b->sys->xcb) return  1;
173         return 0;
174 }
175
176 static win_t *win_get(xcb_window_t xcb)
177 {
178         win_sys_t sys = { .xcb =  xcb };
179         win_t     key = { .sys = &sys };
180
181         win_t   **win = tfind(&key, &cache, win_cmp);
182
183         if (!win) {
184                 warn("no window for %u", xcb);
185                 return NULL;
186         }
187
188         return *win;
189 }
190
191 /****************
192  * XCB Wrappers *
193  ****************/
194
195 static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
196 {
197         xcb_query_tree_cookie_t cookie =
198                 xcb_query_tree(conn, win);
199         if (!cookie.sequence)
200                 return warn("do_query_tree: %d - bad cookie", win);
201
202         xcb_query_tree_reply_t *reply =
203                 xcb_query_tree_reply(conn, cookie, NULL);
204         if (!reply)
205                 return warn("do_query_tree: %d - no reply", win);
206
207         int nkids = xcb_query_tree_children_length(reply);
208         *kids = xcb_query_tree_children(reply);
209         printf("do_query_tree: %d - n=%d\n", win, nkids);
210         return nkids;
211 }
212
213 static int do_get_geometry(xcb_window_t win,
214                 int *x, int *y, int *w, int *h)
215 {
216         xcb_get_geometry_cookie_t cookie =
217                 xcb_get_geometry(conn, win);
218         if (!cookie.sequence)
219                 return warn("do_get_geometry: %d - bad cookie", win);
220
221         xcb_get_geometry_reply_t *reply =
222                 xcb_get_geometry_reply(conn, cookie, NULL);
223         if (!reply)
224                 return warn("do_get_geometry: %d - no reply", win);
225
226         printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
227                         win, reply->width, reply->height, reply->x, reply->y);
228         *x = reply->x;
229         *y = reply->y;
230         *w = reply->width;
231         *h = reply->height;
232         return 1;
233 }
234
235 static int do_get_window_attributes(xcb_window_t win,
236                 int *override, int *mapped)
237 {
238         xcb_get_window_attributes_cookie_t cookie =
239                 xcb_get_window_attributes(conn, win);
240         if (!cookie.sequence)
241                 return warn("do_get_window_attributes: %d - bad cookie", win);
242
243         xcb_get_window_attributes_reply_t *reply =
244                 xcb_get_window_attributes_reply(conn, cookie, NULL);
245         if (!reply)
246                 return warn("do_get_window_attributes: %d - no reply ", win);
247
248         printf("do_get_window_attributes: %d - %d\n",
249                         win, reply->override_redirect);
250         *override = reply->override_redirect;
251         *mapped   = reply->map_state != XCB_MAP_STATE_UNMAPPED;
252         return 1;
253 }
254
255 static int do_xinerama_check(void)
256 {
257         const xcb_query_extension_reply_t *data =
258                 xcb_get_extension_data(conn, &xcb_xinerama_id);
259         if (!data || !data->present)
260                 return warn("do_xinerama_check: no ext");
261
262         xcb_xinerama_is_active_cookie_t cookie =
263                 xcb_xinerama_is_active(conn);
264         if (!cookie.sequence)
265                 return warn("do_xinerama_check: no cookie");
266
267         xcb_xinerama_is_active_reply_t *reply =
268                 xcb_xinerama_is_active_reply(conn, cookie, NULL);
269         if (!reply)
270                 warn("do_xinerama_check: no reply");
271
272         printf("do_xinerama_check: %d\n", reply->state);
273         return reply && reply->state;
274 }
275
276 static int do_query_screens(xcb_xinerama_screen_info_t **info)
277 {
278         xcb_xinerama_query_screens_cookie_t cookie =
279                 xcb_xinerama_query_screens(conn);
280         if (!cookie.sequence)
281                 return warn("do_query_screens: bad cookie");
282
283         xcb_xinerama_query_screens_reply_t *reply =
284                 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
285         if (!reply)
286                 return warn("do_query_screens: no reply");
287
288         int ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
289         *info = xcb_xinerama_query_screens_screen_info(reply);
290         printf("do_query_screens: %d screens\n", ninfo);
291         return ninfo;
292 }
293
294 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
295 {
296         uint16_t r = (rgb & 0xFF0000) >> 8;
297         uint16_t g = (rgb & 0x00FF00);
298         uint16_t b = (rgb & 0x0000FF) << 8;
299         xcb_alloc_color_cookie_t cookie =
300                 xcb_alloc_color(conn, colormap, r, g, b);
301         if (!cookie.sequence)
302                 return warn("do_alloc_color: bad cookie");
303
304         xcb_alloc_color_reply_t *reply =
305                 xcb_alloc_color_reply(conn, cookie, NULL);
306         if (!reply)
307                 return warn("do_alloc_color: no reply");
308
309         printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
310         return reply->pixel;
311 }
312
313 static void do_grab_pointer(xcb_event_mask_t mask)
314 {
315         if (!grabbed)
316                 xcb_grab_pointer(conn, 0, root, mask,
317                                 XCB_GRAB_MODE_ASYNC,
318                                 XCB_GRAB_MODE_ASYNC,
319                                 0, 0, XCB_CURRENT_TIME);
320         grabbed++;
321 }
322
323 static void do_ungrab_pointer(void)
324 {
325         grabbed--;
326         if (!grabbed)
327                 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
328 }
329
330 /**************************
331  * Window Manager Helpers *
332  **************************/
333
334 /* Send event info */
335 static int send_event(event_t ev, xcb_window_t ewin)
336 {
337         win_t *win = win_get(ewin);
338         do_grab_pointer(0);
339         int status = wm_handle_event(win, ev, MOD(), PTR());
340         do_ungrab_pointer();
341         return status;
342 }
343
344 /* Send event info */
345 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
346                 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
347 {
348         xcb_window_t xcb = ewin == rwin ? cwin : ewin;
349         win_t *win = win_get(xcb);
350         mod_t  mod = mask_to_mod(mask, up);
351         ptr_t  ptr = list_to_ptr(pos);
352         do_grab_pointer(0);
353         int status = wm_handle_event(win, ev, mod, ptr);
354         do_ungrab_pointer();
355         return status;
356 }
357
358 /* Send pointer motion info */
359 static int send_pointer(int16_t *pos,
360                 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
361 {
362         xcb_window_t xcb = ewin == rwin ? cwin : ewin;
363         win_t *win = win_get(xcb);
364         ptr_t  ptr = list_to_ptr(pos);
365         do_grab_pointer(0);
366         int status = wm_handle_ptr(win, ptr);
367         do_ungrab_pointer();
368         return status;
369 }
370
371 /* Send window state info */
372 static int send_state(void)
373 {
374         return 0;
375 }
376
377
378 /**********************
379  * X11 Event Handlers *
380  **********************/
381
382 /* Specific events */
383 static void on_key_event(xcb_key_press_event_t *event, int up)
384 {
385         printf("on_key_event:         xcb=%-8u\n", event->event);
386         event_t ev = keycode_to_event(event->detail);
387         send_event_info(ev, event->state, up, &event->root_x,
388                 event->root, event->event, event->child);
389 }
390
391 static void on_button_event(xcb_button_press_event_t *event, int up)
392 {
393         printf("on_button_event:      xcb=%-8u\n", event->event);
394         event_t ev = button_to_event(event->detail);
395         if (!send_event_info(ev, event->state, up, &event->root_x,
396                                 event->root, event->event, event->child))
397                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
398         else if (!up)
399                 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
400                                 XCB_EVENT_MASK_BUTTON_RELEASE);
401         else
402                 do_ungrab_pointer();
403
404 }
405
406 static void on_motion_notify(xcb_motion_notify_event_t *event)
407 {
408         printf("on_motion_notify:     xcb=%-8u - %d,%d / %d.%d\n", event->event,
409                         event->event_x, event->event_y,
410                         event->root_x,  event->root_y);
411         send_pointer(&event->root_x, event->root, event->event, event->child);
412 }
413
414 static void on_enter_notify(xcb_enter_notify_event_t *event)
415 {
416         if (event->mode != XCB_NOTIFY_MODE_NORMAL)
417                 return;
418         printf("on_enter_notify:      xcb=%-8u\n", event->event);
419         send_event_info(EV_ENTER, event->state, 0, &event->root_x,
420                 event->root, event->event, event->child);
421 }
422
423 static void on_leave_notify(xcb_leave_notify_event_t *event)
424 {
425         if (event->mode != XCB_NOTIFY_MODE_NORMAL)
426                 return;
427         printf("on_leave_notify:      xcb=%-8u\n", event->event);
428         send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
429                 event->root, event->event, event->child);
430 }
431
432 static void on_focus_in(xcb_focus_in_event_t *event)
433 {
434         if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
435             event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
436                 return;
437         printf("on_focus_in:          xcb=%-8u mode=%d\n", event->event, event->mode);
438         xcb_change_window_attributes(conn, event->event,
439                         XCB_CW_BORDER_PIXEL, &colors[CLR_FOCUS]);
440         if (event->mode == XCB_NOTIFY_MODE_NORMAL)
441                 send_event(EV_FOCUS, event->event);
442 }
443
444 static void on_focus_out(xcb_focus_out_event_t *event)
445 {
446         if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
447             event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
448                 return;
449         printf("on_focus_out:         xcb=%-8u mode=%d\n", event->event, event->mode);
450         xcb_change_window_attributes(conn, event->event,
451                         XCB_CW_BORDER_PIXEL, &colors[CLR_UNFOCUS]);
452         if (event->mode == XCB_NOTIFY_MODE_NORMAL)
453                 send_event(EV_UNFOCUS, event->event);
454 }
455
456 static void on_create_notify(xcb_create_notify_event_t *event)
457 {
458         win_t     *win = new0(win_t);
459         win_sys_t *sys = new0(win_sys_t);
460
461         printf("on_create_notify:     xcb=%-8u -> win=%p\n",
462                         event->window, win);
463
464         win->x        = event->x;
465         win->y        = event->y;
466         win->w        = event->width;
467         win->h        = event->height;
468         win->sys      = sys;
469
470         sys->xcb      = event->window;
471         sys->override = event->override_redirect;
472
473         tsearch(win, &cache, win_cmp);
474 }
475
476 static void on_destroy_notify(win_t *win, xcb_destroy_notify_event_t *event)
477 {
478         printf("on_destroy_notify:    xcb=%-8u -> win=%p\n",
479                         event->window, win);
480
481         tdelete(win, &cache, win_cmp);
482
483         free(win->sys);
484         free(win);
485 }
486
487 static void on_map_request(win_t *win, xcb_map_request_event_t *event)
488 {
489         printf("on_map_request:       xcb=%-8u -> win=%p\n",
490                         event->window, win);
491
492         if (!win->sys->override && !win->sys->mapped)
493                 wm_insert(win);
494         win->sys->mapped = 1;
495
496         xcb_map_window(conn, win->sys->xcb);
497         sys_move(win, win->x, win->y, win->w, win->h);
498 }
499
500 static void on_configure_request(win_t *win, xcb_configure_request_event_t *event)
501 {
502         printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
503                         event->window, win,
504                         event->width, event->height,
505                         event->x, event->y);
506
507         win->x = event->x;
508         win->y = event->y;
509         win->w = event->width;
510         win->h = event->height;
511
512         xcb_configure_notify_event_t resp = {
513                 .response_type = XCB_CONFIGURE_NOTIFY,
514                 .event         = win->sys->xcb,
515                 .window        = win->sys->xcb,
516                 .x             = win->x,
517                 .y             = win->y,
518                 .width         = win->w,
519                 .height        = win->h,
520                 .border_width  = border,
521         };
522
523         xcb_send_event(conn, 0, win->sys->xcb,
524                         XCB_EVENT_MASK_STRUCTURE_NOTIFY,
525                         (const char *)&resp);
526 }
527
528 /* Generic Event */
529 static void on_event(xcb_generic_event_t *event)
530 {
531         win_t   *win = NULL;
532
533         int type = XCB_EVENT_RESPONSE_TYPE(event);
534         int sent = XCB_EVENT_SENT(event);
535         const char *name = NULL;
536
537         switch (type) {
538                 /* Input handling */
539                 case XCB_KEY_PRESS:
540                         on_key_event((xcb_key_press_event_t *)event, 0);
541                         break;
542                 case XCB_KEY_RELEASE:
543                         on_key_event((xcb_key_release_event_t *)event, 1);
544                         break;
545                 case XCB_BUTTON_PRESS:
546                         on_button_event((xcb_button_press_event_t *)event, 0);
547                         break;
548                 case XCB_BUTTON_RELEASE:
549                         on_button_event((xcb_button_release_event_t *)event, 1);
550                         break;
551                 case XCB_MOTION_NOTIFY:
552                         on_motion_notify((xcb_motion_notify_event_t *)event);
553                         break;
554                 case XCB_ENTER_NOTIFY:
555                         on_enter_notify((xcb_enter_notify_event_t *)event);
556                         break;
557                 case XCB_LEAVE_NOTIFY:
558                         on_leave_notify((xcb_leave_notify_event_t *)event);
559                         break;
560                 case XCB_FOCUS_IN:
561                         on_focus_in((xcb_focus_in_event_t *)event);
562                         break;
563                 case XCB_FOCUS_OUT:
564                         on_focus_out((xcb_focus_out_event_t *)event);
565                         break;
566
567                 /* Window management */
568                 case XCB_CREATE_NOTIFY:
569                         on_create_notify((xcb_create_notify_event_t *)event);
570                         break;
571                 case XCB_DESTROY_NOTIFY:
572                         if ((win = win_get(((xcb_destroy_notify_event_t *)event)->window)))
573                                 on_destroy_notify(win, (xcb_destroy_notify_event_t *)event);
574                         break;
575                 case XCB_MAP_REQUEST:
576                         if ((win = win_get(((xcb_map_request_event_t *)event)->window)))
577                                 on_map_request(win, (xcb_map_request_event_t *)event);
578                         break;
579                 case XCB_CONFIGURE_REQUEST:
580                         if ((win = win_get(((xcb_configure_request_event_t *)event)->window)))
581                                 on_configure_request(win, (xcb_configure_request_event_t *)event);
582                         break;
583
584                 /* Unknown events */
585                 default:
586                         name = xcb_event_get_label(type);
587                         printf("on_event: %d:%02X -> %s\n",
588                                 !!sent, type, name?:"unknown_event");
589                         break;
590         }
591 }
592
593 /********************
594  * System functions *
595  ********************/
596
597 void sys_move(win_t *win, int x, int y, int w, int h)
598 {
599         printf("sys_move:  %p - %dx%d @ %d,%d\n",
600                         win, w, h, x, y);
601
602         int b = 2*border;
603
604         win->x = x;
605         win->y = y;
606         win->w = MAX(w,1+b);
607         win->h = MAX(h,1+b);
608         w      = MAX(w-b,1);
609         h      = MAX(h-b,1);
610
611         uint16_t mask   = XCB_CONFIG_WINDOW_X
612                         | XCB_CONFIG_WINDOW_Y
613                         | XCB_CONFIG_WINDOW_WIDTH
614                         | XCB_CONFIG_WINDOW_HEIGHT
615                         | XCB_CONFIG_WINDOW_BORDER_WIDTH;
616         uint32_t list[] = {x, y, w, h, border};
617
618         xcb_configure_window(conn, win->sys->xcb, mask, list);
619 }
620
621 void sys_raise(win_t *win)
622 {
623         printf("sys_raise: %p\n", win);
624         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, win->sys->xcb);
625 }
626
627 void sys_focus(win_t *win)
628 {
629         printf("sys_focus: %p\n", win);
630         xcb_window_t xcb = win ? win->sys->xcb : root;
631
632         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
633                         xcb, XCB_CURRENT_TIME);
634 }
635
636 void sys_show(win_t *win, state_t state)
637 {
638         printf("sys_show:  %p - %d\n", win, state);
639 }
640
641 void sys_watch(win_t *win, event_t ev, mod_t mod)
642 {
643         printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
644         xcb_window_t      xcb  = win ? win->sys->xcb     : root;
645         xcb_event_mask_t *mask = win ? &win->sys->events : &events;
646         xcb_mod_mask_t    mods = 0;
647         xcb_button_t      btn  = 0;
648         xcb_keycode_t    *code = 0;
649
650         switch (ev) {
651                 case EV_ENTER:
652                         *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
653                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
654                         break;
655
656                 case EV_LEAVE:
657                         *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
658                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
659                         break;
660
661                 case EV_FOCUS:
662                 case EV_UNFOCUS:
663                         *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
664                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
665                         break;
666
667                 case EV_MOUSE0...EV_MOUSE7:
668                         btn    = event_to_button(ev);
669                         mods   = mod_to_mask(mod);
670                         *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
671                                         : XCB_EVENT_MASK_BUTTON_PRESS;
672                         xcb_grab_button(conn, 0, xcb, *mask,
673                                         XCB_GRAB_MODE_ASYNC,
674                                         XCB_GRAB_MODE_ASYNC,
675                                         0, 0, btn, mods);
676                         break;
677
678                 default:
679                         code = event_to_keycodes(ev);
680                         mods = mod_to_mask(mod);
681                         for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
682                                 xcb_grab_key(conn, 1, xcb, mods, code[i],
683                                                 XCB_GRAB_MODE_ASYNC,
684                                                 XCB_GRAB_MODE_ASYNC);
685
686                         break;
687         }
688 }
689
690 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
691 {
692         printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
693 }
694
695 list_t *sys_info(void)
696 {
697         printf("sys_info\n");
698
699         if (screens == NULL && do_xinerama_check()) {
700                 /* Add Xinerama screens */
701                 xcb_xinerama_screen_info_t *info = NULL;
702                 int ninfo = do_query_screens(&info);
703                 for (int i = 0; i < ninfo; i++) {
704                         win_t *screen = new0(win_t);
705
706                         screen->x = info[i].x_org;
707                         screen->y = info[i].y_org;
708                         screen->w = info[i].width;
709                         screen->h = info[i].height;
710
711                         screens = list_insert(NULL, screen);
712
713                         printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
714                                         screen->w, screen->h,
715                                         screen->x, screen->y);
716                 }
717         }
718
719         if (screens == NULL) {
720                 /* No xinerama support */
721                 const xcb_setup_t *setup = xcb_get_setup(conn);
722                 xcb_screen_t      *geom  = xcb_setup_roots_iterator(setup).data;
723
724                 win_t *screen = new0(win_t);
725
726                 screen->w = geom->width_in_pixels;
727                 screen->h = geom->height_in_pixels;
728
729                 screens = list_insert(NULL, screen);
730
731                 printf("sys_info: root screen - %dx%d\n",
732                                 screen->w, screen->h);
733         }
734
735         return screens;
736 }
737
738 void sys_init(void)
739 {
740         printf("sys_init\n");
741
742         /* Load configuration */
743         stack      = conf_get_int("main.stack",      stack);
744         border     = conf_get_int("main.border",     border);
745         no_capture = conf_get_int("main.no-capture", no_capture);
746
747         /* Connect to display */
748         if (!(conn = xcb_connect(NULL, NULL)))
749                 error("xcb connect failed");
750         if (xcb_connection_has_error(conn))
751                 error("xcb connection has errors");
752
753         /* Get root window */
754         const xcb_setup_t     *setup = xcb_get_setup(conn);
755         xcb_screen_iterator_t  iter  = xcb_setup_roots_iterator(setup);
756         root     = iter.data->root;
757         colormap = iter.data->default_colormap;
758
759         /* Allocate key symbols */
760         if (!(keysyms = xcb_key_symbols_alloc(conn)))
761                 error("cannot allocate key symbols");
762
763         /* Read color information */
764         colors[CLR_FOCUS]   = do_alloc_color(0xFF6060);
765         colors[CLR_UNFOCUS] = do_alloc_color(0xD8D8FF);
766         colors[CLR_URGENT]  = do_alloc_color(0xFF0000);
767
768         /* Request substructure redirect */
769         xcb_void_cookie_t cookie;
770         xcb_generic_error_t *err;
771         events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
772                  XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
773         cookie = xcb_change_window_attributes_checked(conn, root,
774                         XCB_CW_EVENT_MASK, &events);
775         if ((err = xcb_request_check(conn, cookie)))
776                 error("another window manager is already running");
777 }
778
779 void sys_run(void)
780 {
781         printf("sys_run\n");
782
783         /* Add each initial window */
784         if (!no_capture) {
785                 xcb_window_t *kids = NULL;
786                 int nkids = do_query_tree(root, &kids);
787                 for(int i = 0; i < nkids; i++) {
788                         win_t *win = new0(win_t);
789                         win->sys = new0(win_sys_t);
790                         win->sys->xcb = kids[i];
791                         do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
792                         do_get_window_attributes(kids[i],
793                                 &win->sys->override, &win->sys->mapped);
794                         tsearch(win, &cache, win_cmp);
795                         if (!win->sys->override && win->sys->mapped)
796                                 wm_insert(win);
797                 }
798                 xcb_flush(conn);
799         }
800
801         /* Main loop */
802         while (1)
803         {
804                 int status;
805                 xcb_generic_event_t *event;
806                 if (!(event = xcb_wait_for_event(conn)))
807                         break;
808                 on_event(event);
809                 free(event);
810                 if (!(status = xcb_flush(conn)))
811                         break;
812         }
813 }
814
815 void sys_exit(void)
816 {
817         printf("sys_exit\n");
818         if (conn)
819                 xcb_disconnect(conn);
820         conn = NULL;
821 }
822
823 void sys_free(void)
824 {
825         printf("sys_free\n");
826         if (conn)
827                 xcb_disconnect(conn);
828         conn = NULL;
829 }