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