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