]> Pileus Git - wmpus/blob - sys-xcb.c
Start adding support for atoms
[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_ewmh.h>
26 #include <xcb/xinerama.h>
27
28 #include "util.h"
29 #include "conf.h"
30 #include "types.h"
31 #include "sys.h"
32 #include "wm.h"
33
34 /* Configuration */
35 static int border     = 2;
36 static int stack      = 25;
37 static int no_capture = 0;
38
39 /* Internal structures */
40 typedef struct {
41         int left;
42         int right;
43         int top;
44         int bottom;
45 } strut_t;
46
47 struct win_sys {
48         xcb_window_t     xcb;    // xcb window id
49         xcb_event_mask_t events; // currently watch events
50         strut_t          strut;  // toolbar struts
51         int managed;             // window is managed by wm
52 };
53
54 typedef enum {
55         CLR_FOCUS,
56         CLR_UNFOCUS,
57         CLR_URGENT,
58         NCOLORS
59 } color_t;
60
61 /* Global data */
62 static xcb_connection_t      *conn;
63 static xcb_ewmh_connection_t  ewmh;
64 static xcb_key_symbols_t     *keysyms;
65 static xcb_colormap_t         colormap;
66 static xcb_window_t           root;
67 static xcb_event_mask_t       events;
68 static list_t                *screens;
69 static void                  *cache;
70 static xcb_pixmap_t           colors[NCOLORS];
71 static unsigned int           grabbed;
72
73 /************************
74  * Conversion functions *
75  ************************/
76
77 /* State names */
78 static char *state_map[] = {
79         [ST_HIDE ] "hide ",
80         [ST_SHOW ] "show ",
81         [ST_FULL ] "full ",
82         [ST_MAX  ] "max  ",
83         [ST_SHADE] "shade",
84         [ST_ICON ] "icon ",
85         [ST_CLOSE] "close",
86 };
87
88 /* Key presses */
89 static struct {
90         event_t      ev;
91         xcb_keysym_t sym;
92 } keysym_map[] = {
93         { EV_LEFT,     0xFF51 },
94         { EV_RIGHT,    0xFF53 },
95         { EV_UP,       0xFF52 },
96         { EV_DOWN,     0xFF54 },
97         { EV_HOME,     0xFF50 },
98         { EV_END,      0xFF57 },
99         { EV_PAGEUP,   0xFF55 },
100         { EV_PAGEDOWN, 0xFF56 },
101         { EV_F1,       0xFFBE },
102         { EV_F2,       0xFFBF },
103         { EV_F3,       0xFFC0 },
104         { EV_F4,       0xFFC1 },
105         { EV_F5,       0xFFC2 },
106         { EV_F6,       0xFFC3 },
107         { EV_F7,       0xFFC4 },
108         { EV_F8,       0xFFC5 },
109         { EV_F9,       0xFFC6 },
110         { EV_F10,      0xFFC7 },
111         { EV_F11,      0xFFC8 },
112         { EV_F12,      0xFFC9 },
113 };
114
115 /************************
116  * Conversion functions *
117  ************************/
118
119 static xcb_keycode_t *event_to_keycodes(event_t ev)
120 {
121         xcb_keycode_t *codes = NULL;
122
123         /* Get keysym */
124         xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
125
126         /* Get keycodes */
127         if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
128                 warn("no keycode found for %d->%d", ev, keysym);
129
130         return codes;
131 }
132
133 static event_t keycode_to_event(xcb_keycode_t code)
134 {
135         /* Get keysym */
136         xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
137
138         /* Get event */
139         return map_get(keysym_map, sym,keysym, ev,keysym);
140 }
141
142 /* Button presses */
143 static event_t button_to_event(xcb_button_t btn)
144 {
145         return EV_MOUSE0 + btn;
146 }
147
148 static xcb_button_t event_to_button(event_t ev)
149 {
150         return ev - EV_MOUSE0;
151 }
152
153 /* Modifier masks */
154 static xcb_mod_mask_t mod_to_mask(mod_t mod)
155 {
156         xcb_mod_mask_t mask = 0;
157         if (mod.alt)   mask |= XCB_MOD_MASK_1;
158         if (mod.ctrl)  mask |= XCB_MOD_MASK_CONTROL;
159         if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
160         if (mod.win)   mask |= XCB_MOD_MASK_4;
161         return mask;
162 }
163
164 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
165 {
166         mod_t mod = { .up = up };
167         if (mask & XCB_MOD_MASK_1)       mod.alt   = 1;
168         if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl  = 1;
169         if (mask & XCB_MOD_MASK_SHIFT)   mod.shift = 1;
170         if (mask & XCB_MOD_MASK_4)       mod.win   = 1;
171         return mod;
172 }
173
174 /* Mouse pointers */
175 static ptr_t list_to_ptr(int16_t *list)
176 {
177         ptr_t ptr = {};
178         if (list) {
179                 ptr.rx = list[0]; // root_x
180                 ptr.ry = list[1]; // root_y
181                 ptr.x  = list[2]; // event_x
182                 ptr.y  = list[3]; // event_y
183         }
184         return ptr;
185 }
186
187 /********************
188  * Window functions *
189  ********************/
190
191 static int win_cmp(const void *_a, const void *_b)
192 {
193         const win_t *a = _a;
194         const win_t *b = _b;
195         if (a->sys->xcb < b->sys->xcb) return -1;
196         if (a->sys->xcb > b->sys->xcb) return  1;
197         return 0;
198 }
199
200 static win_t *win_get(xcb_window_t xcb)
201 {
202         win_sys_t sys = { .xcb =  xcb };
203         win_t     key = { .sys = &sys };
204
205         win_t   **win = tfind(&key, &cache, win_cmp);
206
207         if (!win) {
208                 warn("no window for %u", xcb);
209                 return NULL;
210         }
211
212         return *win;
213 }
214
215 /****************
216  * XCB Wrappers *
217  ****************/
218
219 static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
220 {
221         xcb_query_tree_cookie_t cookie =
222                 xcb_query_tree(conn, win);
223         if (!cookie.sequence)
224                 return warn("do_query_tree: %d - bad cookie", win);
225
226         xcb_query_tree_reply_t *reply =
227                 xcb_query_tree_reply(conn, cookie, NULL);
228         if (!reply)
229                 return warn("do_query_tree: %d - no reply", win);
230
231         int nkids = xcb_query_tree_children_length(reply);
232         *kids = xcb_query_tree_children(reply);
233         printf("do_query_tree: %d - n=%d\n", win, nkids);
234         return nkids;
235 }
236
237 static int do_get_geometry(xcb_window_t win,
238                 int *x, int *y, int *w, int *h)
239 {
240         xcb_get_geometry_cookie_t cookie =
241                 xcb_get_geometry(conn, win);
242         if (!cookie.sequence)
243                 return warn("do_get_geometry: %d - bad cookie", win);
244
245         xcb_get_geometry_reply_t *reply =
246                 xcb_get_geometry_reply(conn, cookie, NULL);
247         if (!reply)
248                 return warn("do_get_geometry: %d - no reply", win);
249
250         printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
251                         win, reply->width, reply->height, reply->x, reply->y);
252         *x = reply->x;
253         *y = reply->y;
254         *w = reply->width;
255         *h = reply->height;
256         return 1;
257 }
258
259 static int do_get_window_attributes(xcb_window_t win,
260                 int *override, int *mapped)
261 {
262         xcb_get_window_attributes_cookie_t cookie =
263                 xcb_get_window_attributes(conn, win);
264         if (!cookie.sequence)
265                 return warn("do_get_window_attributes: %d - bad cookie", win);
266
267         xcb_get_window_attributes_reply_t *reply =
268                 xcb_get_window_attributes_reply(conn, cookie, NULL);
269         if (!reply)
270                 return warn("do_get_window_attributes: %d - no reply ", win);
271
272         printf("do_get_window_attributes: %d - %d\n",
273                         win, reply->override_redirect);
274         *override = reply->override_redirect;
275         *mapped   = reply->map_state != XCB_MAP_STATE_UNMAPPED;
276         return 1;
277 }
278
279 static int do_xinerama_check(void)
280 {
281         const xcb_query_extension_reply_t *data =
282                 xcb_get_extension_data(conn, &xcb_xinerama_id);
283         if (!data || !data->present)
284                 return warn("do_xinerama_check: no ext");
285
286         xcb_xinerama_is_active_cookie_t cookie =
287                 xcb_xinerama_is_active(conn);
288         if (!cookie.sequence)
289                 return warn("do_xinerama_check: no cookie");
290
291         xcb_xinerama_is_active_reply_t *reply =
292                 xcb_xinerama_is_active_reply(conn, cookie, NULL);
293         if (!reply)
294                 warn("do_xinerama_check: no reply");
295
296         printf("do_xinerama_check: %d\n", reply->state);
297         return reply && reply->state;
298 }
299
300 static int do_query_screens(xcb_xinerama_screen_info_t **info)
301 {
302         xcb_xinerama_query_screens_cookie_t cookie =
303                 xcb_xinerama_query_screens(conn);
304         if (!cookie.sequence)
305                 return warn("do_query_screens: bad cookie");
306
307         xcb_xinerama_query_screens_reply_t *reply =
308                 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
309         if (!reply)
310                 return warn("do_query_screens: no reply");
311
312         int ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
313         *info = xcb_xinerama_query_screens_screen_info(reply);
314         printf("do_query_screens: %d screens\n", ninfo);
315         return ninfo;
316 }
317
318 static int do_get_input_focus(void)
319 {
320         xcb_get_input_focus_cookie_t cookie =
321                 xcb_get_input_focus(conn);
322         if (!cookie.sequence)
323                 return warn("do_get_input_focus: bad cookie");
324
325         xcb_get_input_focus_reply_t *reply =
326                 xcb_get_input_focus_reply(conn, cookie, NULL);
327         if (!reply)
328                 return warn("do_get_input_focus: no reply");
329
330         return reply->focus;
331 }
332
333 static int do_ewmh_init_atoms(void)
334 {
335         xcb_intern_atom_cookie_t *cookies =
336                 xcb_ewmh_init_atoms(conn, &ewmh);
337         if (!cookies)
338                 return warn("do_ewmh_init_atoms: no cookies");
339
340         int status =
341                 xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
342         if (!status)
343                 return warn("do_ewmh_init_atoms: no status");
344         return status;
345 }
346
347 static int do_get_strut(xcb_window_t win, strut_t *strut)
348 {
349         xcb_get_property_cookie_t cookie =
350                 xcb_ewmh_get_wm_strut(&ewmh, win);
351         if (!cookie.sequence)
352                 return warn("do_get_strut: bad cookie");
353
354         xcb_ewmh_get_extents_reply_t ext = {};
355         int status =
356                 xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
357         if (!status)
358                 return warn("do_get_strut: no status");
359
360         strut->left   = ext.left;
361         strut->right  = ext.right;
362         strut->top    = ext.top;
363         strut->bottom = ext.bottom;
364
365         return ext.left || ext.right || ext.top || ext.bottom;
366 }
367
368 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
369 {
370         uint16_t r = (rgb & 0xFF0000) >> 8;
371         uint16_t g = (rgb & 0x00FF00);
372         uint16_t b = (rgb & 0x0000FF) << 8;
373         xcb_alloc_color_cookie_t cookie =
374                 xcb_alloc_color(conn, colormap, r, g, b);
375         if (!cookie.sequence)
376                 return warn("do_alloc_color: bad cookie");
377
378         xcb_alloc_color_reply_t *reply =
379                 xcb_alloc_color_reply(conn, cookie, NULL);
380         if (!reply)
381                 return warn("do_alloc_color: no reply");
382
383         printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
384         return reply->pixel;
385 }
386
387 static void do_grab_pointer(xcb_event_mask_t mask)
388 {
389         if (!grabbed)
390                 xcb_grab_pointer(conn, 0, root, mask,
391                                 XCB_GRAB_MODE_ASYNC,
392                                 XCB_GRAB_MODE_ASYNC,
393                                 0, 0, XCB_CURRENT_TIME);
394         grabbed++;
395 }
396
397 static void do_ungrab_pointer(void)
398 {
399         grabbed--;
400         if (!grabbed)
401                 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
402 }
403
404 static void do_configure_window(xcb_window_t win,
405                 int x, int y, int w, int h,
406                 int b, int s, int r)
407 {
408         int table[][2] = {
409                 { x, XCB_CONFIG_WINDOW_X            },
410                 { y, XCB_CONFIG_WINDOW_Y            },
411                 { w, XCB_CONFIG_WINDOW_WIDTH        },
412                 { h, XCB_CONFIG_WINDOW_HEIGHT       },
413                 { b, XCB_CONFIG_WINDOW_BORDER_WIDTH },
414                 { s, XCB_CONFIG_WINDOW_SIBLING      },
415                 { r, XCB_CONFIG_WINDOW_STACK_MODE   },
416         };
417
418         uint16_t mask    = 0;
419         uint32_t list[7] = {};
420         for (int i = 0; i < 7; i++) {
421                 if (table[i][0] >= 0) {
422                         list[i] = table[i][0];
423                         mask   |= table[i][1];
424                 }
425         }
426
427         xcb_configure_window(conn, win, mask, list);
428 }
429
430 /**************************
431  * Window Manager Helpers *
432  **************************/
433
434 /* Send event info */
435 static int send_event(event_t ev, xcb_window_t ewin)
436 {
437         win_t *win = win_get(ewin);
438         do_grab_pointer(0);
439         int status = wm_handle_event(win, ev, MOD(), PTR());
440         do_ungrab_pointer();
441         return status;
442 }
443
444 /* Send event info */
445 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
446                 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
447 {
448         xcb_window_t xcb = ewin == rwin ? cwin : ewin;
449         win_t *win = win_get(xcb);
450         mod_t  mod = mask_to_mod(mask, up);
451         ptr_t  ptr = list_to_ptr(pos);
452         do_grab_pointer(0);
453         int status = wm_handle_event(win, ev, mod, ptr);
454         do_ungrab_pointer();
455         return status;
456 }
457
458 /* Send pointer motion info */
459 static int send_pointer(int16_t *pos,
460                 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
461 {
462         xcb_window_t xcb = ewin == rwin ? cwin : ewin;
463         win_t *win = win_get(xcb);
464         ptr_t  ptr = list_to_ptr(pos);
465         do_grab_pointer(0);
466         int status = wm_handle_ptr(win, ptr);
467         do_ungrab_pointer();
468         return status;
469 }
470
471 /* Send window state info */
472 static void send_manage(win_t *win, int managed)
473 {
474         if (win->sys->managed == managed)
475                 return;
476         if (managed)
477                 wm_insert(win);
478         else
479                 wm_remove(win);
480         win->sys->managed = managed;
481 }
482
483 /* Send window state info */
484 static void send_state(win_t *win, state_t next)
485 {
486         if (!win->sys->managed)
487                 return;
488         if (win->state == next)
489                 return;
490         state_t prev = win->state;
491         win->state = next;
492         wm_handle_state(win, prev, next);
493 }
494
495 /**********************
496  * X11 Event Handlers *
497  **********************/
498
499 /* Specific events */
500 static void on_key_event(xcb_key_press_event_t *event, int up)
501 {
502         printf("on_key_event:         xcb=%-8u\n", event->event);
503         xcb_window_t focus = do_get_input_focus();
504         event_t ev = keycode_to_event(event->detail);
505         send_event_info(ev, event->state, up, &event->root_x,
506                 event->root, focus, event->child);
507 }
508
509 static void on_button_event(xcb_button_press_event_t *event, int up)
510 {
511         printf("on_button_event:      xcb=%-8u\n", event->event);
512         event_t ev = button_to_event(event->detail);
513         if (!send_event_info(ev, event->state, up, &event->root_x,
514                                 event->root, event->event, event->child))
515                 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
516         else if (!up)
517                 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
518                                 XCB_EVENT_MASK_BUTTON_RELEASE);
519         else
520                 do_ungrab_pointer();
521
522 }
523
524 static void on_motion_notify(xcb_motion_notify_event_t *event)
525 {
526         printf("on_motion_notify:     xcb=%-8u - %d,%d / %d.%d\n", event->event,
527                         event->event_x, event->event_y,
528                         event->root_x,  event->root_y);
529         send_pointer(&event->root_x, event->root, event->event, event->child);
530 }
531
532 static void on_enter_notify(xcb_enter_notify_event_t *event)
533 {
534         if (event->mode != XCB_NOTIFY_MODE_NORMAL)
535                 return;
536         printf("on_enter_notify:      xcb=%-8u\n", event->event);
537         send_event_info(EV_ENTER, event->state, 0, &event->root_x,
538                 event->root, event->event, event->child);
539 }
540
541 static void on_leave_notify(xcb_leave_notify_event_t *event)
542 {
543         if (event->mode != XCB_NOTIFY_MODE_NORMAL)
544                 return;
545         printf("on_leave_notify:      xcb=%-8u\n", event->event);
546         send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
547                 event->root, event->event, event->child);
548 }
549
550 static void on_focus_in(xcb_focus_in_event_t *event)
551 {
552         if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
553             event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
554                 return;
555         printf("on_focus_in:          xcb=%-8u mode=%d\n", event->event, event->mode);
556         xcb_change_window_attributes(conn, event->event,
557                         XCB_CW_BORDER_PIXEL, &colors[CLR_FOCUS]);
558         if (event->mode == XCB_NOTIFY_MODE_NORMAL)
559                 send_event(EV_FOCUS, event->event);
560 }
561
562 static void on_focus_out(xcb_focus_out_event_t *event)
563 {
564         if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
565             event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
566                 return;
567         printf("on_focus_out:         xcb=%-8u mode=%d\n", event->event, event->mode);
568         xcb_change_window_attributes(conn, event->event,
569                         XCB_CW_BORDER_PIXEL, &colors[CLR_UNFOCUS]);
570         if (event->mode == XCB_NOTIFY_MODE_NORMAL)
571                 send_event(EV_UNFOCUS, event->event);
572 }
573
574 static void on_create_notify(xcb_create_notify_event_t *event)
575 {
576         win_t     *win = new0(win_t);
577         win_sys_t *sys = new0(win_sys_t);
578
579         printf("on_create_notify:     xcb=%-8u -> win=%p\n",
580                         event->window, win);
581
582         win->x        = event->x;
583         win->y        = event->y;
584         win->w        = event->width;
585         win->h        = event->height;
586         win->sys      = sys;
587
588         sys->xcb      = event->window;
589
590         if (!event->override_redirect)
591                 send_manage(win, 1);
592
593         tsearch(win, &cache, win_cmp);
594 }
595
596 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
597 {
598         win_t *win = win_get(event->window);
599         printf("on_destroy_notify:    xcb=%-8u -> win=%p\n",
600                         event->window, win);
601         if (!win) return;
602
603         send_manage(win, 0);
604
605         tdelete(win, &cache, win_cmp);
606
607         free(win->sys);
608         free(win);
609 }
610
611 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
612 {
613         win_t *win = win_get(event->window);
614         printf("on_unmap_notify:      xcb=%-8u -> win=%p\n",
615                         event->window, win);
616         if (!win) return;
617
618         send_state(win, ST_HIDE);
619 }
620
621 static void on_map_notify(xcb_map_notify_event_t *event)
622 {
623         win_t *win = win_get(event->window);
624         printf("on_map_notify:        xcb=%-8u -> win=%p\n",
625                         event->window, win);
626         if (!win) return;
627
628         send_state(win, ST_SHOW);
629 }
630
631 static void on_map_request(xcb_map_request_event_t *event)
632 {
633         win_t *win = win_get(event->window);
634         printf("on_map_request:       xcb=%-8u -> win=%p\n",
635                         event->window, win);
636         if (!win) return;
637
638         if (do_get_strut(win->sys->xcb, &win->sys->strut))
639                 printf("Map: Got a strut!\n");
640         else
641                 printf("Map: No struts here!\n");
642
643         send_state(win, ST_SHOW);
644         xcb_map_window(conn, win->sys->xcb);
645         sys_move(win, win->x, win->y, win->w, win->h);
646 }
647
648 static void on_configure_request(xcb_configure_request_event_t *event)
649 {
650         win_t *win = win_get(event->window);
651         printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
652                         event->window, win,
653                         event->width, event->height,
654                         event->x, event->y);
655         if (!win) return;
656
657         win->x = event->x;
658         win->y = event->y;
659         win->w = event->width;
660         win->h = event->height;
661
662         xcb_configure_notify_event_t resp = {
663                 .response_type = XCB_CONFIGURE_NOTIFY,
664                 .event         = win->sys->xcb,
665                 .window        = win->sys->xcb,
666                 .x             = win->x,
667                 .y             = win->y,
668                 .width         = win->w,
669                 .height        = win->h,
670                 .border_width  = border,
671         };
672
673         xcb_send_event(conn, 0, win->sys->xcb,
674                         XCB_EVENT_MASK_STRUCTURE_NOTIFY,
675                         (const char *)&resp);
676 }
677
678 /* Generic Event */
679 static void on_event(xcb_generic_event_t *event)
680 {
681         int type = XCB_EVENT_RESPONSE_TYPE(event);
682
683         switch (type) {
684                 /* Input handling */
685                 case XCB_KEY_PRESS:
686                         on_key_event((xcb_key_press_event_t *)event, 0);
687                         break;
688                 case XCB_KEY_RELEASE:
689                         on_key_event((xcb_key_release_event_t *)event, 1);
690                         break;
691                 case XCB_BUTTON_PRESS:
692                         on_button_event((xcb_button_press_event_t *)event, 0);
693                         break;
694                 case XCB_BUTTON_RELEASE:
695                         on_button_event((xcb_button_release_event_t *)event, 1);
696                         break;
697                 case XCB_MOTION_NOTIFY:
698                         on_motion_notify((xcb_motion_notify_event_t *)event);
699                         break;
700                 case XCB_ENTER_NOTIFY:
701                         on_enter_notify((xcb_enter_notify_event_t *)event);
702                         break;
703                 case XCB_LEAVE_NOTIFY:
704                         on_leave_notify((xcb_leave_notify_event_t *)event);
705                         break;
706                 case XCB_FOCUS_IN:
707                         on_focus_in((xcb_focus_in_event_t *)event);
708                         break;
709                 case XCB_FOCUS_OUT:
710                         on_focus_out((xcb_focus_out_event_t *)event);
711                         break;
712
713                 /* Window management */
714                 case XCB_CREATE_NOTIFY:
715                         on_create_notify((xcb_create_notify_event_t *)event);
716                         break;
717                 case XCB_DESTROY_NOTIFY:
718                         on_destroy_notify((xcb_destroy_notify_event_t *)event);
719                         break;
720                 case XCB_UNMAP_NOTIFY:
721                         on_unmap_notify((xcb_unmap_notify_event_t *)event);
722                         break;
723                 case XCB_MAP_NOTIFY:
724                         on_map_notify((xcb_map_notify_event_t *)event);
725                         break;
726                 case XCB_MAP_REQUEST:
727                         on_map_request((xcb_map_request_event_t *)event);
728                         break;
729                 case XCB_CONFIGURE_REQUEST:
730                         on_configure_request((xcb_configure_request_event_t *)event);
731                         break;
732
733                 /* Unknown events */
734                 default:
735                         printf("on_event: %d:%02X -> %s\n",
736                                 XCB_EVENT_SENT(event) != 0,
737                                 XCB_EVENT_RESPONSE_TYPE(event),
738                                 xcb_event_get_label(type) ?: "unknown_event");
739                         break;
740         }
741 }
742
743 /********************
744  * System functions *
745  ********************/
746
747 void sys_move(win_t *win, int x, int y, int w, int h)
748 {
749         printf("sys_move:  %p - %dx%d @ %d,%d\n",
750                         win, w, h, x, y);
751
752         int b = 2*border;
753
754         win->x = x;
755         win->y = y;
756         win->w = MAX(w,1+b);
757         win->h = MAX(h,1+b);
758         w      = MAX(w-b,1);
759         h      = MAX(h-b,1);
760
761         do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
762 }
763
764 void sys_raise(win_t *win)
765 {
766         printf("sys_raise: %p\n", win);
767
768         uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
769         uint32_t list = XCB_STACK_MODE_ABOVE;
770
771         xcb_configure_window(conn, win->sys->xcb, mask, &list);
772 }
773
774 void sys_focus(win_t *win)
775 {
776         printf("sys_focus: %p\n", win);
777         xcb_window_t xcb = win ? win->sys->xcb : root;
778
779         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
780                         xcb, XCB_CURRENT_TIME);
781 }
782
783 void sys_show(win_t *win, state_t state)
784 {
785         printf("sys_show:  %p - %s -> %s\n", win,
786                         state_map[win->state], state_map[state]);
787         xcb_window_t xcb = win ? win->sys->xcb : root;
788
789         /* Find screen */
790         win_t *screen = NULL;
791         if (state == ST_FULL || state == ST_MAX) {
792                 for (list_t *cur = screens; cur; cur = cur->next) {
793                         screen = cur->data;
794                         if (win->x >= screen->x && win->x <= screen->x+screen->w &&
795                             win->y >= screen->y && win->y <= screen->y+screen->h)
796                                 break;
797                 }
798         }
799
800         /* Change window state */
801         switch (state) {
802                 case ST_HIDE:
803                         xcb_unmap_window(conn, xcb);
804                         break;
805
806                 case ST_SHOW:
807                         xcb_map_window(conn, xcb);
808                         do_configure_window(xcb, win->x, win->y,
809                                         MAX(win->w - 2*border, 1),
810                                         MAX(win->h - 2*border, 1),
811                                         border, -1, -1);
812                         break;
813
814                 case ST_FULL:
815                         xcb_map_window(conn, xcb);
816                         do_configure_window(xcb, screen->x, screen->y, screen->w, screen->h,
817                                         0, -1, XCB_STACK_MODE_ABOVE);
818                         xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, xcb);
819                         break;
820
821                 case ST_MAX:
822                         xcb_map_window(conn, xcb);
823                         do_configure_window(xcb, screen->x, screen->y,
824                                         MAX(screen->w - 2*border, 1),
825                                         MAX(screen->h - 2*border, 1),
826                                         border, -1, XCB_STACK_MODE_ABOVE);
827                         break;
828
829                 case ST_SHADE:
830                         xcb_map_window(conn, xcb);
831                         do_configure_window(xcb, -1, -1, -1, stack,
832                                         border, -1, -1);
833                         break;
834
835                 case ST_ICON:
836                         xcb_map_window(conn, xcb);
837                         do_configure_window(xcb, -1, -1, 100, 100,
838                                         border, -1, -1);
839                         break;
840
841                 case ST_CLOSE:
842                         xcb_kill_client(conn, xcb);
843                         break;
844         }
845
846         /* Update state */
847         win->state = state;
848 }
849
850 void sys_watch(win_t *win, event_t ev, mod_t mod)
851 {
852         printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
853         xcb_window_t      xcb  = win ? win->sys->xcb     : root;
854         xcb_event_mask_t *mask = win ? &win->sys->events : &events;
855         xcb_mod_mask_t    mods = 0;
856         xcb_button_t      btn  = 0;
857         xcb_keycode_t    *code = 0;
858
859         switch (ev) {
860                 case EV_ENTER:
861                         *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
862                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
863                         break;
864
865                 case EV_LEAVE:
866                         *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
867                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
868                         break;
869
870                 case EV_FOCUS:
871                 case EV_UNFOCUS:
872                         *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
873                         xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
874                         break;
875
876                 case EV_MOUSE0...EV_MOUSE7:
877                         btn    = event_to_button(ev);
878                         mods   = mod_to_mask(mod);
879                         *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
880                                         : XCB_EVENT_MASK_BUTTON_PRESS;
881                         xcb_grab_button(conn, 0, xcb, *mask,
882                                         XCB_GRAB_MODE_ASYNC,
883                                         XCB_GRAB_MODE_ASYNC,
884                                         0, 0, btn, mods);
885                         break;
886
887                 default:
888                         code = event_to_keycodes(ev);
889                         mods = mod_to_mask(mod);
890                         for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
891                                 xcb_grab_key(conn, 1, xcb, mods, code[i],
892                                                 XCB_GRAB_MODE_ASYNC,
893                                                 XCB_GRAB_MODE_ASYNC);
894
895                         break;
896         }
897 }
898
899 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
900 {
901         printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
902 }
903
904 list_t *sys_info(void)
905 {
906         printf("sys_info\n");
907
908         if (screens == NULL && do_xinerama_check()) {
909                 /* Add Xinerama screens */
910                 xcb_xinerama_screen_info_t *info = NULL;
911                 int ninfo = do_query_screens(&info);
912                 for (int i = 0; i < ninfo; i++) {
913                         win_t *screen = new0(win_t);
914
915                         screen->x = info[i].x_org;
916                         screen->y = info[i].y_org;
917                         screen->w = info[i].width;
918                         screen->h = info[i].height;
919
920                         screens = list_insert(NULL, screen);
921
922                         printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
923                                         screen->w, screen->h,
924                                         screen->x, screen->y);
925                 }
926         }
927
928         if (screens == NULL) {
929                 /* No xinerama support */
930                 const xcb_setup_t *setup = xcb_get_setup(conn);
931                 xcb_screen_t      *geom  = xcb_setup_roots_iterator(setup).data;
932
933                 win_t *screen = new0(win_t);
934
935                 screen->w = geom->width_in_pixels;
936                 screen->h = geom->height_in_pixels;
937
938                 screens = list_insert(NULL, screen);
939
940                 printf("sys_info: root screen - %dx%d\n",
941                                 screen->w, screen->h);
942         }
943
944         return screens;
945 }
946
947 void sys_init(void)
948 {
949         printf("sys_init\n");
950
951         /* Load configuration */
952         stack      = conf_get_int("main.stack",      stack);
953         border     = conf_get_int("main.border",     border);
954         no_capture = conf_get_int("main.no-capture", no_capture);
955
956         /* Connect to display */
957         if (!(conn = xcb_connect(NULL, NULL)))
958                 error("xcb connect failed");
959         if (xcb_connection_has_error(conn))
960                 error("xcb connection has errors");
961
962         /* Get root window */
963         const xcb_setup_t     *setup = xcb_get_setup(conn);
964         xcb_screen_iterator_t  iter  = xcb_setup_roots_iterator(setup);
965         root     = iter.data->root;
966         colormap = iter.data->default_colormap;
967
968         /* Setup EWMH connection */
969         if (!do_ewmh_init_atoms())
970                 error("ewmh setup failed");
971
972         /* Setup for for ST_CLOSE */
973         xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
974
975         /* Allocate key symbols */
976         if (!(keysyms = xcb_key_symbols_alloc(conn)))
977                 error("cannot allocate key symbols");
978
979         /* Read color information */
980         colors[CLR_FOCUS]   = do_alloc_color(0xFF6060);
981         colors[CLR_UNFOCUS] = do_alloc_color(0xD8D8FF);
982         colors[CLR_URGENT]  = do_alloc_color(0xFF0000);
983
984         /* Request substructure redirect */
985         xcb_void_cookie_t cookie;
986         xcb_generic_error_t *err;
987         events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
988                  XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
989         cookie = xcb_change_window_attributes_checked(conn, root,
990                         XCB_CW_EVENT_MASK, &events);
991         if ((err = xcb_request_check(conn, cookie)))
992                 error("another window manager is already running");
993 }
994
995 void sys_run(void)
996 {
997         printf("sys_run\n");
998
999         /* Add each initial window */
1000         if (!no_capture) {
1001                 xcb_window_t *kids = NULL;
1002                 int nkids = do_query_tree(root, &kids);
1003                 for(int i = 0; i < nkids; i++) {
1004                         int override=0, mapped=0;
1005                         win_t *win = new0(win_t);
1006                         win->sys = new0(win_sys_t);
1007                         win->sys->xcb = kids[i];
1008                         tsearch(win, &cache, win_cmp);
1009                         do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
1010                         do_get_window_attributes(kids[i], &override, &mapped);
1011                         if (!override)
1012                                 send_manage(win, 1);
1013                         if (mapped)
1014                                 send_state(win, ST_SHOW);
1015                 }
1016                 xcb_flush(conn);
1017         }
1018
1019         /* Main loop */
1020         while (1)
1021         {
1022                 int status;
1023                 xcb_generic_event_t *event;
1024                 if (!(event = xcb_wait_for_event(conn)))
1025                         break;
1026                 on_event(event);
1027                 free(event);
1028                 if (!(status = xcb_flush(conn)))
1029                         break;
1030         }
1031 }
1032
1033 void sys_exit(void)
1034 {
1035         printf("sys_exit\n");
1036         if (conn)
1037                 xcb_disconnect(conn);
1038         conn = NULL;
1039 }
1040
1041 void sys_free(void)
1042 {
1043         printf("sys_free\n");
1044         if (conn)
1045                 xcb_disconnect(conn);
1046         conn = NULL;
1047 }