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/xcb_ewmh.h>
26 #include <xcb/xinerama.h>
35 static int border = 2;
36 static int stack = 25;
37 static int no_capture = 0;
39 /* Internal structures */
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
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;
70 static xcb_pixmap_t colors[NCOLORS];
71 static unsigned int grabbed;
73 /************************
74 * Conversion functions *
75 ************************/
78 static char *state_map[] = {
99 { EV_PAGEUP, 0xFF55 },
100 { EV_PAGEDOWN, 0xFF56 },
115 /************************
116 * Conversion functions *
117 ************************/
119 static xcb_keycode_t *event_to_keycodes(event_t ev)
121 xcb_keycode_t *codes = NULL;
124 xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
127 if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
128 warn("no keycode found for %d->%d", ev, keysym);
133 static event_t keycode_to_event(xcb_keycode_t code)
136 xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
139 return map_get(keysym_map, sym,keysym, ev,keysym);
143 static event_t button_to_event(xcb_button_t btn)
145 return EV_MOUSE0 + btn;
148 static xcb_button_t event_to_button(event_t ev)
150 return ev - EV_MOUSE0;
154 static xcb_mod_mask_t mod_to_mask(mod_t mod)
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;
164 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
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;
175 static ptr_t list_to_ptr(int16_t *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
187 /********************
189 ********************/
191 static int win_cmp(const void *_a, const void *_b)
195 if (a->sys->xcb < b->sys->xcb) return -1;
196 if (a->sys->xcb > b->sys->xcb) return 1;
200 static win_t *win_get(xcb_window_t xcb)
202 win_sys_t sys = { .xcb = xcb };
203 win_t key = { .sys = &sys };
205 win_t **win = tfind(&key, &cache, win_cmp);
208 warn("no window for %u", xcb);
219 static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
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);
226 xcb_query_tree_reply_t *reply =
227 xcb_query_tree_reply(conn, cookie, NULL);
229 return warn("do_query_tree: %d - no reply", win);
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);
237 static int do_get_geometry(xcb_window_t win,
238 int *x, int *y, int *w, int *h)
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);
245 xcb_get_geometry_reply_t *reply =
246 xcb_get_geometry_reply(conn, cookie, NULL);
248 return warn("do_get_geometry: %d - no reply", win);
250 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
251 win, reply->width, reply->height, reply->x, reply->y);
259 static int do_get_window_attributes(xcb_window_t win,
260 int *override, int *mapped)
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);
267 xcb_get_window_attributes_reply_t *reply =
268 xcb_get_window_attributes_reply(conn, cookie, NULL);
270 return warn("do_get_window_attributes: %d - no reply ", win);
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;
279 static int do_xinerama_check(void)
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");
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");
291 xcb_xinerama_is_active_reply_t *reply =
292 xcb_xinerama_is_active_reply(conn, cookie, NULL);
294 warn("do_xinerama_check: no reply");
296 printf("do_xinerama_check: %d\n", reply->state);
297 return reply && reply->state;
300 static int do_query_screens(xcb_xinerama_screen_info_t **info)
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");
307 xcb_xinerama_query_screens_reply_t *reply =
308 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
310 return warn("do_query_screens: no reply");
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);
318 static int do_get_input_focus(void)
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");
325 xcb_get_input_focus_reply_t *reply =
326 xcb_get_input_focus_reply(conn, cookie, NULL);
328 return warn("do_get_input_focus: no reply");
333 static int do_ewmh_init_atoms(void)
335 xcb_intern_atom_cookie_t *cookies =
336 xcb_ewmh_init_atoms(conn, &ewmh);
338 return warn("do_ewmh_init_atoms: no cookies");
341 xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
343 return warn("do_ewmh_init_atoms: no status");
347 static int do_get_strut(xcb_window_t win, strut_t *strut)
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");
354 xcb_ewmh_get_extents_reply_t ext = {};
356 xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
358 return warn("do_get_strut: no status");
360 strut->left = ext.left;
361 strut->right = ext.right;
362 strut->top = ext.top;
363 strut->bottom = ext.bottom;
365 return ext.left || ext.right || ext.top || ext.bottom;
368 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
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");
378 xcb_alloc_color_reply_t *reply =
379 xcb_alloc_color_reply(conn, cookie, NULL);
381 return warn("do_alloc_color: no reply");
383 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
387 static void do_grab_pointer(xcb_event_mask_t mask)
390 xcb_grab_pointer(conn, 0, root, mask,
393 0, 0, XCB_CURRENT_TIME);
397 static void do_ungrab_pointer(void)
401 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
404 static void do_configure_window(xcb_window_t win,
405 int x, int y, int w, int h,
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 },
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];
427 xcb_configure_window(conn, win, mask, list);
430 /**************************
431 * Window Manager Helpers *
432 **************************/
434 /* Send event info */
435 static int send_event(event_t ev, xcb_window_t ewin)
437 win_t *win = win_get(ewin);
439 int status = wm_handle_event(win, ev, MOD(), PTR());
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)
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);
453 int status = wm_handle_event(win, ev, mod, ptr);
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)
462 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
463 win_t *win = win_get(xcb);
464 ptr_t ptr = list_to_ptr(pos);
466 int status = wm_handle_ptr(win, ptr);
471 /* Send window state info */
472 static void send_manage(win_t *win, int managed)
474 if (win->sys->managed == managed)
480 win->sys->managed = managed;
483 /* Send window state info */
484 static void send_state(win_t *win, state_t next)
486 if (!win->sys->managed)
488 if (win->state == next)
490 state_t prev = win->state;
492 wm_handle_state(win, prev, next);
495 /**********************
496 * X11 Event Handlers *
497 **********************/
499 /* Specific events */
500 static void on_key_event(xcb_key_press_event_t *event, int up)
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);
509 static void on_button_event(xcb_button_press_event_t *event, int up)
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);
517 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
518 XCB_EVENT_MASK_BUTTON_RELEASE);
524 static void on_motion_notify(xcb_motion_notify_event_t *event)
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);
532 static void on_enter_notify(xcb_enter_notify_event_t *event)
534 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
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);
541 static void on_leave_notify(xcb_leave_notify_event_t *event)
543 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
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);
550 static void on_focus_in(xcb_focus_in_event_t *event)
552 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
553 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
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);
562 static void on_focus_out(xcb_focus_out_event_t *event)
564 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
565 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
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);
574 static void on_create_notify(xcb_create_notify_event_t *event)
576 win_t *win = new0(win_t);
577 win_sys_t *sys = new0(win_sys_t);
579 printf("on_create_notify: xcb=%-8u -> win=%p\n",
584 win->w = event->width;
585 win->h = event->height;
588 sys->xcb = event->window;
590 if (!event->override_redirect)
593 tsearch(win, &cache, win_cmp);
596 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
598 win_t *win = win_get(event->window);
599 printf("on_destroy_notify: xcb=%-8u -> win=%p\n",
605 tdelete(win, &cache, win_cmp);
611 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
613 win_t *win = win_get(event->window);
614 printf("on_unmap_notify: xcb=%-8u -> win=%p\n",
618 send_state(win, ST_HIDE);
621 static void on_map_notify(xcb_map_notify_event_t *event)
623 win_t *win = win_get(event->window);
624 printf("on_map_notify: xcb=%-8u -> win=%p\n",
628 send_state(win, ST_SHOW);
631 static void on_map_request(xcb_map_request_event_t *event)
633 win_t *win = win_get(event->window);
634 printf("on_map_request: xcb=%-8u -> win=%p\n",
638 if (do_get_strut(win->sys->xcb, &win->sys->strut))
639 printf("Map: Got a strut!\n");
641 printf("Map: No struts here!\n");
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);
648 static void on_configure_request(xcb_configure_request_event_t *event)
650 win_t *win = win_get(event->window);
651 printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
653 event->width, event->height,
659 win->w = event->width;
660 win->h = event->height;
662 xcb_configure_notify_event_t resp = {
663 .response_type = XCB_CONFIGURE_NOTIFY,
664 .event = win->sys->xcb,
665 .window = win->sys->xcb,
670 .border_width = border,
673 xcb_send_event(conn, 0, win->sys->xcb,
674 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
675 (const char *)&resp);
679 static void on_event(xcb_generic_event_t *event)
681 int type = XCB_EVENT_RESPONSE_TYPE(event);
686 on_key_event((xcb_key_press_event_t *)event, 0);
688 case XCB_KEY_RELEASE:
689 on_key_event((xcb_key_release_event_t *)event, 1);
691 case XCB_BUTTON_PRESS:
692 on_button_event((xcb_button_press_event_t *)event, 0);
694 case XCB_BUTTON_RELEASE:
695 on_button_event((xcb_button_release_event_t *)event, 1);
697 case XCB_MOTION_NOTIFY:
698 on_motion_notify((xcb_motion_notify_event_t *)event);
700 case XCB_ENTER_NOTIFY:
701 on_enter_notify((xcb_enter_notify_event_t *)event);
703 case XCB_LEAVE_NOTIFY:
704 on_leave_notify((xcb_leave_notify_event_t *)event);
707 on_focus_in((xcb_focus_in_event_t *)event);
710 on_focus_out((xcb_focus_out_event_t *)event);
713 /* Window management */
714 case XCB_CREATE_NOTIFY:
715 on_create_notify((xcb_create_notify_event_t *)event);
717 case XCB_DESTROY_NOTIFY:
718 on_destroy_notify((xcb_destroy_notify_event_t *)event);
720 case XCB_UNMAP_NOTIFY:
721 on_unmap_notify((xcb_unmap_notify_event_t *)event);
724 on_map_notify((xcb_map_notify_event_t *)event);
726 case XCB_MAP_REQUEST:
727 on_map_request((xcb_map_request_event_t *)event);
729 case XCB_CONFIGURE_REQUEST:
730 on_configure_request((xcb_configure_request_event_t *)event);
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");
743 /********************
745 ********************/
747 void sys_move(win_t *win, int x, int y, int w, int h)
749 printf("sys_move: %p - %dx%d @ %d,%d\n",
761 do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
764 void sys_raise(win_t *win)
766 printf("sys_raise: %p\n", win);
768 uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
769 uint32_t list = XCB_STACK_MODE_ABOVE;
771 xcb_configure_window(conn, win->sys->xcb, mask, &list);
774 void sys_focus(win_t *win)
776 printf("sys_focus: %p\n", win);
777 xcb_window_t xcb = win ? win->sys->xcb : root;
779 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
780 xcb, XCB_CURRENT_TIME);
783 void sys_show(win_t *win, state_t state)
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;
790 win_t *screen = NULL;
791 if (state == ST_FULL || state == ST_MAX) {
792 for (list_t *cur = screens; cur; cur = cur->next) {
794 if (win->x >= screen->x && win->x <= screen->x+screen->w &&
795 win->y >= screen->y && win->y <= screen->y+screen->h)
800 /* Change window state */
803 xcb_unmap_window(conn, xcb);
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),
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);
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);
830 xcb_map_window(conn, xcb);
831 do_configure_window(xcb, -1, -1, -1, stack,
836 xcb_map_window(conn, xcb);
837 do_configure_window(xcb, -1, -1, 100, 100,
842 xcb_kill_client(conn, xcb);
850 void sys_watch(win_t *win, event_t ev, mod_t mod)
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;
861 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
862 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
866 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
867 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
872 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
873 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
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,
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],
893 XCB_GRAB_MODE_ASYNC);
899 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
901 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
904 list_t *sys_info(void)
906 printf("sys_info\n");
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);
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;
920 screens = list_insert(NULL, screen);
922 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
923 screen->w, screen->h,
924 screen->x, screen->y);
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;
933 win_t *screen = new0(win_t);
935 screen->w = geom->width_in_pixels;
936 screen->h = geom->height_in_pixels;
938 screens = list_insert(NULL, screen);
940 printf("sys_info: root screen - %dx%d\n",
941 screen->w, screen->h);
949 printf("sys_init\n");
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);
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");
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;
968 /* Setup EWMH connection */
969 if (!do_ewmh_init_atoms())
970 error("ewmh setup failed");
972 /* Setup for for ST_CLOSE */
973 xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
975 /* Allocate key symbols */
976 if (!(keysyms = xcb_key_symbols_alloc(conn)))
977 error("cannot allocate key symbols");
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);
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");
999 /* Add each initial window */
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);
1012 send_manage(win, 1);
1014 send_state(win, ST_SHOW);
1023 xcb_generic_event_t *event;
1024 if (!(event = xcb_wait_for_event(conn)))
1028 if (!(status = xcb_flush(conn)))
1035 printf("sys_exit\n");
1037 xcb_disconnect(conn);
1043 printf("sys_free\n");
1045 xcb_disconnect(conn);