2 * Copyright (c) 2015 Andy Spencer <andy753421@gmail.com>
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.
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
23 #include <xcb/xcb_event.h>
24 #include <xcb/xcb_keysyms.h>
25 #include <xcb/xinerama.h>
34 static int border = 2;
35 static int stack = 25;
36 static int no_capture = 0;
38 /* Internal structures */
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
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;
60 static xcb_pixmap_t colors[NCOLORS];
61 static unsigned int grabbed;
63 /************************
64 * Conversion functions *
65 ************************/
68 static char *state_map[] = {
89 { EV_PAGEUP, 0xFF55 },
90 { EV_PAGEDOWN, 0xFF56 },
105 /************************
106 * Conversion functions *
107 ************************/
109 static xcb_keycode_t *event_to_keycodes(event_t ev)
111 xcb_keycode_t *codes = NULL;
114 xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
117 if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
118 warn("no keycode found for %d->%d", ev, keysym);
123 static event_t keycode_to_event(xcb_keycode_t code)
126 xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
129 return map_get(keysym_map, sym,keysym, ev,keysym);
133 static event_t button_to_event(xcb_button_t btn)
135 return EV_MOUSE0 + btn;
138 static xcb_button_t event_to_button(event_t ev)
140 return ev - EV_MOUSE0;
144 static xcb_mod_mask_t mod_to_mask(mod_t mod)
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;
154 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
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;
165 static ptr_t list_to_ptr(int16_t *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
177 /********************
179 ********************/
181 static int win_cmp(const void *_a, const void *_b)
185 if (a->sys->xcb < b->sys->xcb) return -1;
186 if (a->sys->xcb > b->sys->xcb) return 1;
190 static win_t *win_get(xcb_window_t xcb)
192 win_sys_t sys = { .xcb = xcb };
193 win_t key = { .sys = &sys };
195 win_t **win = tfind(&key, &cache, win_cmp);
198 warn("no window for %u", xcb);
209 static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
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);
216 xcb_query_tree_reply_t *reply =
217 xcb_query_tree_reply(conn, cookie, NULL);
219 return warn("do_query_tree: %d - no reply", win);
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);
227 static int do_get_geometry(xcb_window_t win,
228 int *x, int *y, int *w, int *h)
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);
235 xcb_get_geometry_reply_t *reply =
236 xcb_get_geometry_reply(conn, cookie, NULL);
238 return warn("do_get_geometry: %d - no reply", win);
240 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
241 win, reply->width, reply->height, reply->x, reply->y);
249 static int do_get_window_attributes(xcb_window_t win,
250 int *override, int *mapped)
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);
257 xcb_get_window_attributes_reply_t *reply =
258 xcb_get_window_attributes_reply(conn, cookie, NULL);
260 return warn("do_get_window_attributes: %d - no reply ", win);
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;
269 static int do_xinerama_check(void)
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");
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");
281 xcb_xinerama_is_active_reply_t *reply =
282 xcb_xinerama_is_active_reply(conn, cookie, NULL);
284 warn("do_xinerama_check: no reply");
286 printf("do_xinerama_check: %d\n", reply->state);
287 return reply && reply->state;
290 static int do_query_screens(xcb_xinerama_screen_info_t **info)
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");
297 xcb_xinerama_query_screens_reply_t *reply =
298 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
300 return warn("do_query_screens: no reply");
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);
308 static int do_get_input_focus(void)
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");
315 xcb_get_input_focus_reply_t *reply =
316 xcb_get_input_focus_reply(conn, cookie, NULL);
318 return warn("do_get_input_focus: no reply");
323 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
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");
333 xcb_alloc_color_reply_t *reply =
334 xcb_alloc_color_reply(conn, cookie, NULL);
336 return warn("do_alloc_color: no reply");
338 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
342 static void do_grab_pointer(xcb_event_mask_t mask)
345 xcb_grab_pointer(conn, 0, root, mask,
348 0, 0, XCB_CURRENT_TIME);
352 static void do_ungrab_pointer(void)
356 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
359 static void do_configure_window(xcb_window_t win,
360 int x, int y, int w, int h,
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 },
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];
382 xcb_configure_window(conn, win, mask, list);
385 /**************************
386 * Window Manager Helpers *
387 **************************/
389 /* Send event info */
390 static int send_event(event_t ev, xcb_window_t ewin)
392 win_t *win = win_get(ewin);
394 int status = wm_handle_event(win, ev, MOD(), PTR());
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)
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);
408 int status = wm_handle_event(win, ev, mod, ptr);
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)
417 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
418 win_t *win = win_get(xcb);
419 ptr_t ptr = list_to_ptr(pos);
421 int status = wm_handle_ptr(win, ptr);
426 /* Send window state info */
427 static void send_manage(win_t *win, int managed)
429 if (win->sys->managed == managed)
435 win->sys->managed = managed;
438 /* Send window state info */
439 static void send_state(win_t *win, state_t next)
441 if (!win->sys->managed)
443 if (win->state == next)
445 state_t prev = win->state;
447 wm_handle_state(win, prev, next);
450 /**********************
451 * X11 Event Handlers *
452 **********************/
454 /* Specific events */
455 static void on_key_event(xcb_key_press_event_t *event, int up)
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);
464 static void on_button_event(xcb_button_press_event_t *event, int up)
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);
472 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
473 XCB_EVENT_MASK_BUTTON_RELEASE);
479 static void on_motion_notify(xcb_motion_notify_event_t *event)
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);
487 static void on_enter_notify(xcb_enter_notify_event_t *event)
489 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
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);
496 static void on_leave_notify(xcb_leave_notify_event_t *event)
498 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
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);
505 static void on_focus_in(xcb_focus_in_event_t *event)
507 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
508 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
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);
517 static void on_focus_out(xcb_focus_out_event_t *event)
519 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
520 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
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);
529 static void on_create_notify(xcb_create_notify_event_t *event)
531 win_t *win = new0(win_t);
532 win_sys_t *sys = new0(win_sys_t);
534 printf("on_create_notify: xcb=%-8u -> win=%p\n",
539 win->w = event->width;
540 win->h = event->height;
543 sys->xcb = event->window;
545 if (!event->override_redirect)
548 tsearch(win, &cache, win_cmp);
551 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
553 win_t *win = win_get(event->window);
554 printf("on_destroy_notify: xcb=%-8u -> win=%p\n",
560 tdelete(win, &cache, win_cmp);
566 static void on_map_request(xcb_map_request_event_t *event)
568 win_t *win = win_get(event->window);
569 printf("on_map_request: xcb=%-8u -> win=%p\n",
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);
578 static void on_configure_request(xcb_configure_request_event_t *event)
580 win_t *win = win_get(event->window);
581 printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
583 event->width, event->height,
589 win->w = event->width;
590 win->h = event->height;
592 xcb_configure_notify_event_t resp = {
593 .response_type = XCB_CONFIGURE_NOTIFY,
594 .event = win->sys->xcb,
595 .window = win->sys->xcb,
600 .border_width = border,
603 xcb_send_event(conn, 0, win->sys->xcb,
604 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
605 (const char *)&resp);
609 static void on_event(xcb_generic_event_t *event)
611 int type = XCB_EVENT_RESPONSE_TYPE(event);
616 on_key_event((xcb_key_press_event_t *)event, 0);
618 case XCB_KEY_RELEASE:
619 on_key_event((xcb_key_release_event_t *)event, 1);
621 case XCB_BUTTON_PRESS:
622 on_button_event((xcb_button_press_event_t *)event, 0);
624 case XCB_BUTTON_RELEASE:
625 on_button_event((xcb_button_release_event_t *)event, 1);
627 case XCB_MOTION_NOTIFY:
628 on_motion_notify((xcb_motion_notify_event_t *)event);
630 case XCB_ENTER_NOTIFY:
631 on_enter_notify((xcb_enter_notify_event_t *)event);
633 case XCB_LEAVE_NOTIFY:
634 on_leave_notify((xcb_leave_notify_event_t *)event);
637 on_focus_in((xcb_focus_in_event_t *)event);
640 on_focus_out((xcb_focus_out_event_t *)event);
643 /* Window management */
644 case XCB_CREATE_NOTIFY:
645 on_create_notify((xcb_create_notify_event_t *)event);
647 case XCB_DESTROY_NOTIFY:
648 on_destroy_notify((xcb_destroy_notify_event_t *)event);
650 case XCB_MAP_REQUEST:
651 on_map_request((xcb_map_request_event_t *)event);
653 case XCB_CONFIGURE_REQUEST:
654 on_configure_request((xcb_configure_request_event_t *)event);
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");
667 /********************
669 ********************/
671 void sys_move(win_t *win, int x, int y, int w, int h)
673 printf("sys_move: %p - %dx%d @ %d,%d\n",
685 do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
688 void sys_raise(win_t *win)
690 printf("sys_raise: %p\n", win);
692 uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
693 uint32_t list = XCB_STACK_MODE_ABOVE;
695 xcb_configure_window(conn, win->sys->xcb, mask, &list);
698 void sys_focus(win_t *win)
700 printf("sys_focus: %p\n", win);
701 xcb_window_t xcb = win ? win->sys->xcb : root;
703 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
704 xcb, XCB_CURRENT_TIME);
707 void sys_show(win_t *win, state_t state)
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;
714 win_t *screen = NULL;
715 if (state == ST_FULL || state == ST_MAX) {
716 for (list_t *cur = screens; cur; cur = cur->next) {
718 if (win->x >= screen->x && win->x <= screen->x+screen->w &&
719 win->y >= screen->y && win->y <= screen->y+screen->h)
724 /* Change window state */
727 xcb_unmap_window(conn, xcb);
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),
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);
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);
754 xcb_map_window(conn, xcb);
755 do_configure_window(xcb, -1, -1, -1, stack,
760 xcb_map_window(conn, xcb);
761 do_configure_window(xcb, -1, -1, 100, 100,
766 xcb_kill_client(conn, xcb);
774 void sys_watch(win_t *win, event_t ev, mod_t mod)
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;
785 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
786 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
790 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
791 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
796 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
797 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
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,
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],
817 XCB_GRAB_MODE_ASYNC);
823 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
825 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
828 list_t *sys_info(void)
830 printf("sys_info\n");
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);
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;
844 screens = list_insert(NULL, screen);
846 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
847 screen->w, screen->h,
848 screen->x, screen->y);
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;
857 win_t *screen = new0(win_t);
859 screen->w = geom->width_in_pixels;
860 screen->h = geom->height_in_pixels;
862 screens = list_insert(NULL, screen);
864 printf("sys_info: root screen - %dx%d\n",
865 screen->w, screen->h);
873 printf("sys_init\n");
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);
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");
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;
892 /* Setup for for ST_CLOSE */
893 xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
895 /* Allocate key symbols */
896 if (!(keysyms = xcb_key_symbols_alloc(conn)))
897 error("cannot allocate key symbols");
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);
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");
919 /* Add each initial window */
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);
934 send_state(win, ST_SHOW);
943 xcb_generic_event_t *event;
944 if (!(event = xcb_wait_for_event(conn)))
948 if (!(status = xcb_flush(conn)))
955 printf("sys_exit\n");
957 xcb_disconnect(conn);
963 printf("sys_free\n");
965 xcb_disconnect(conn);