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_icccm.h>
26 #include <xcb/xcb_ewmh.h>
27 #include <xcb/xinerama.h>
36 static int border = 2;
37 static int stack = 25;
38 static int no_capture = 0;
40 /* Internal structures */
49 xcb_window_t xcb; // xcb window id
50 xcb_event_mask_t events; // currently watch events
51 strut_t strut; // toolbar struts
52 int managed; // window is managed by wm
63 static xcb_connection_t *conn;
64 static xcb_ewmh_connection_t ewmh;
65 static xcb_key_symbols_t *keysyms;
66 static xcb_colormap_t colormap;
67 static xcb_window_t root;
68 static xcb_event_mask_t events;
69 static list_t *screens;
71 static xcb_pixmap_t colors[NCOLORS];
72 static unsigned int grabbed;
73 static xcb_atom_t wm_protos;
74 static xcb_atom_t wm_delete;
76 /************************
77 * Conversion functions *
78 ************************/
81 static char *state_map[] = {
102 { EV_PAGEUP, 0xFF55 },
103 { EV_PAGEDOWN, 0xFF56 },
118 /************************
119 * Conversion functions *
120 ************************/
122 static xcb_keycode_t *event_to_keycodes(event_t ev)
124 xcb_keycode_t *codes = NULL;
127 xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
130 if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
131 warn("no keycode found for %d->%d", ev, keysym);
136 static event_t keycode_to_event(xcb_keycode_t code)
139 xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
142 return map_get(keysym_map, sym,keysym, ev,keysym);
146 static event_t button_to_event(xcb_button_t btn)
148 return EV_MOUSE0 + btn;
151 static xcb_button_t event_to_button(event_t ev)
153 return ev - EV_MOUSE0;
157 static xcb_mod_mask_t mod_to_mask(mod_t mod)
159 xcb_mod_mask_t mask = 0;
160 if (mod.alt) mask |= XCB_MOD_MASK_1;
161 if (mod.ctrl) mask |= XCB_MOD_MASK_CONTROL;
162 if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
163 if (mod.win) mask |= XCB_MOD_MASK_4;
167 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
169 mod_t mod = { .up = up };
170 if (mask & XCB_MOD_MASK_1) mod.alt = 1;
171 if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl = 1;
172 if (mask & XCB_MOD_MASK_SHIFT) mod.shift = 1;
173 if (mask & XCB_MOD_MASK_4) mod.win = 1;
178 static ptr_t list_to_ptr(int16_t *list)
182 ptr.rx = list[0]; // root_x
183 ptr.ry = list[1]; // root_y
184 ptr.x = list[2]; // event_x
185 ptr.y = list[3]; // event_y
190 /********************
192 ********************/
194 static int win_cmp(const void *_a, const void *_b)
198 if (a->sys->xcb < b->sys->xcb) return -1;
199 if (a->sys->xcb > b->sys->xcb) return 1;
203 static win_t *win_get(xcb_window_t xcb)
205 win_sys_t sys = { .xcb = xcb };
206 win_t key = { .sys = &sys };
208 win_t **win = tfind(&key, &cache, win_cmp);
211 warn("no window for %u", xcb);
222 static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
224 xcb_query_tree_cookie_t cookie =
225 xcb_query_tree(conn, win);
226 if (!cookie.sequence)
227 return warn("do_query_tree: %d - bad cookie", win);
229 xcb_query_tree_reply_t *reply =
230 xcb_query_tree_reply(conn, cookie, NULL);
232 return warn("do_query_tree: %d - no reply", win);
234 int nkids = xcb_query_tree_children_length(reply);
235 *kids = xcb_query_tree_children(reply);
236 printf("do_query_tree: %d - n=%d\n", win, nkids);
240 static int do_get_geometry(xcb_window_t win,
241 int *x, int *y, int *w, int *h)
243 xcb_get_geometry_cookie_t cookie =
244 xcb_get_geometry(conn, win);
245 if (!cookie.sequence)
246 return warn("do_get_geometry: %d - bad cookie", win);
248 xcb_get_geometry_reply_t *reply =
249 xcb_get_geometry_reply(conn, cookie, NULL);
251 return warn("do_get_geometry: %d - no reply", win);
253 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
254 win, reply->width, reply->height, reply->x, reply->y);
262 static int do_get_window_attributes(xcb_window_t win,
263 int *override, int *mapped)
265 xcb_get_window_attributes_cookie_t cookie =
266 xcb_get_window_attributes(conn, win);
267 if (!cookie.sequence)
268 return warn("do_get_window_attributes: %d - bad cookie", win);
270 xcb_get_window_attributes_reply_t *reply =
271 xcb_get_window_attributes_reply(conn, cookie, NULL);
273 return warn("do_get_window_attributes: %d - no reply ", win);
275 printf("do_get_window_attributes: %d - %d\n",
276 win, reply->override_redirect);
277 *override = reply->override_redirect;
278 *mapped = reply->map_state != XCB_MAP_STATE_UNMAPPED;
282 static int do_xinerama_check(void)
284 const xcb_query_extension_reply_t *data =
285 xcb_get_extension_data(conn, &xcb_xinerama_id);
286 if (!data || !data->present)
287 return warn("do_xinerama_check: no ext");
289 xcb_xinerama_is_active_cookie_t cookie =
290 xcb_xinerama_is_active(conn);
291 if (!cookie.sequence)
292 return warn("do_xinerama_check: no cookie");
294 xcb_xinerama_is_active_reply_t *reply =
295 xcb_xinerama_is_active_reply(conn, cookie, NULL);
297 warn("do_xinerama_check: no reply");
299 printf("do_xinerama_check: %d\n", reply->state);
300 return reply && reply->state;
303 static int do_query_screens(xcb_xinerama_screen_info_t **info)
305 xcb_xinerama_query_screens_cookie_t cookie =
306 xcb_xinerama_query_screens(conn);
307 if (!cookie.sequence)
308 return warn("do_query_screens: bad cookie");
310 xcb_xinerama_query_screens_reply_t *reply =
311 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
313 return warn("do_query_screens: no reply");
315 int ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
316 *info = xcb_xinerama_query_screens_screen_info(reply);
317 printf("do_query_screens: %d screens\n", ninfo);
321 static int do_get_input_focus(void)
323 xcb_get_input_focus_cookie_t cookie =
324 xcb_get_input_focus(conn);
325 if (!cookie.sequence)
326 return warn("do_get_input_focus: bad cookie");
328 xcb_get_input_focus_reply_t *reply =
329 xcb_get_input_focus_reply(conn, cookie, NULL);
331 return warn("do_get_input_focus: no reply");
336 static xcb_atom_t do_intern_atom(const char *name)
338 xcb_intern_atom_cookie_t cookie =
339 xcb_intern_atom(conn, 0, strlen(name), name);
340 if (!cookie.sequence)
341 return warn("do_intern_atom: bad cookie");
343 xcb_intern_atom_reply_t *reply =
344 xcb_intern_atom_reply(conn, cookie, NULL);
346 return warn("do_intern_atom: no reply");
351 static int do_ewmh_init_atoms(void)
353 xcb_intern_atom_cookie_t *cookies =
354 xcb_ewmh_init_atoms(conn, &ewmh);
356 return warn("do_ewmh_init_atoms: no cookies");
359 xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
361 return warn("do_ewmh_init_atoms: no status");
365 static int do_get_strut(xcb_window_t win, strut_t *strut)
367 xcb_get_property_cookie_t cookie =
368 xcb_ewmh_get_wm_strut(&ewmh, win);
369 if (!cookie.sequence)
370 return warn("do_get_strut: bad cookie");
372 xcb_ewmh_get_extents_reply_t ext = {};
374 xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
376 return warn("do_get_strut: no status");
378 strut->left = ext.left;
379 strut->right = ext.right;
380 strut->top = ext.top;
381 strut->bottom = ext.bottom;
383 return ext.left || ext.right || ext.top || ext.bottom;
386 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
388 uint16_t r = (rgb & 0xFF0000) >> 8;
389 uint16_t g = (rgb & 0x00FF00);
390 uint16_t b = (rgb & 0x0000FF) << 8;
391 xcb_alloc_color_cookie_t cookie =
392 xcb_alloc_color(conn, colormap, r, g, b);
393 if (!cookie.sequence)
394 return warn("do_alloc_color: bad cookie");
396 xcb_alloc_color_reply_t *reply =
397 xcb_alloc_color_reply(conn, cookie, NULL);
399 return warn("do_alloc_color: no reply");
401 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
405 static void do_grab_pointer(xcb_event_mask_t mask)
408 xcb_grab_pointer(conn, 0, root, mask,
411 0, 0, XCB_CURRENT_TIME);
415 static void do_ungrab_pointer(void)
419 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
422 static void do_configure_window(xcb_window_t win,
423 int x, int y, int w, int h,
427 { x, XCB_CONFIG_WINDOW_X },
428 { y, XCB_CONFIG_WINDOW_Y },
429 { w, XCB_CONFIG_WINDOW_WIDTH },
430 { h, XCB_CONFIG_WINDOW_HEIGHT },
431 { b, XCB_CONFIG_WINDOW_BORDER_WIDTH },
432 { s, XCB_CONFIG_WINDOW_SIBLING },
433 { r, XCB_CONFIG_WINDOW_STACK_MODE },
437 uint32_t list[7] = {};
438 for (int i = 0; i < 7; i++) {
439 if (table[i][0] >= 0) {
440 list[i] = table[i][0];
445 xcb_configure_window(conn, win, mask, list);
448 static int do_client_message(xcb_window_t win, xcb_atom_t atom)
451 xcb_get_property_cookie_t cookie =
452 xcb_icccm_get_wm_protocols(conn, win, wm_protos);
453 if (!cookie.sequence)
454 return warn("do_client_message: %d - bad cookie", win);
456 xcb_icccm_get_wm_protocols_reply_t protos = {};
457 if (!xcb_icccm_get_wm_protocols_reply(conn, cookie, &protos, NULL))
458 return warn("do_client_message: %d - no reply", win);
460 /* Search for the atom */
462 for (int i = 0; i < protos.atoms_len; i++)
463 if (protos.atoms[i] == atom)
466 return warn("do_client_message: %d - no atom", win);
468 /* Send the message */
469 xcb_client_message_event_t msg = {
470 .response_type = XCB_CLIENT_MESSAGE,
474 .data.data32[0] = atom,
475 .data.data32[1] = XCB_CURRENT_TIME,
477 xcb_send_event(conn, 0, win, XCB_EVENT_MASK_NO_EVENT,
482 /**************************
483 * Window Manager Helpers *
484 **************************/
486 /* Send event info */
487 static int send_event(event_t ev, xcb_window_t ewin)
489 win_t *win = win_get(ewin);
491 int status = wm_handle_event(win, ev, MOD(), PTR());
496 /* Send event info */
497 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
498 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
500 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
501 win_t *win = win_get(xcb);
502 mod_t mod = mask_to_mod(mask, up);
503 ptr_t ptr = list_to_ptr(pos);
505 int status = wm_handle_event(win, ev, mod, ptr);
510 /* Send pointer motion info */
511 static int send_pointer(int16_t *pos,
512 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
514 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
515 win_t *win = win_get(xcb);
516 ptr_t ptr = list_to_ptr(pos);
518 int status = wm_handle_ptr(win, ptr);
523 /* Send window state info */
524 static void send_manage(win_t *win, int managed)
526 if (win->sys->managed == managed)
532 win->sys->managed = managed;
535 /* Send window state info */
536 static void send_state(win_t *win, state_t next)
538 if (!win->sys->managed)
540 if (win->state == next)
542 state_t prev = win->state;
544 wm_handle_state(win, prev, next);
547 /**********************
548 * X11 Event Handlers *
549 **********************/
551 /* Specific events */
552 static void on_key_event(xcb_key_press_event_t *event, int up)
554 printf("on_key_event: xcb=%-8u\n", event->event);
555 xcb_window_t focus = do_get_input_focus();
556 event_t ev = keycode_to_event(event->detail);
557 send_event_info(ev, event->state, up, &event->root_x,
558 event->root, focus, event->child);
561 static void on_button_event(xcb_button_press_event_t *event, int up)
563 printf("on_button_event: xcb=%-8u\n", event->event);
564 event_t ev = button_to_event(event->detail);
565 if (!send_event_info(ev, event->state, up, &event->root_x,
566 event->root, event->event, event->child))
567 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
569 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
570 XCB_EVENT_MASK_BUTTON_RELEASE);
576 static void on_motion_notify(xcb_motion_notify_event_t *event)
578 printf("on_motion_notify: xcb=%-8u - %d,%d / %d.%d\n", event->event,
579 event->event_x, event->event_y,
580 event->root_x, event->root_y);
581 send_pointer(&event->root_x, event->root, event->event, event->child);
584 static void on_enter_notify(xcb_enter_notify_event_t *event)
586 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
588 printf("on_enter_notify: xcb=%-8u\n", event->event);
589 send_event_info(EV_ENTER, event->state, 0, &event->root_x,
590 event->root, event->event, event->child);
593 static void on_leave_notify(xcb_leave_notify_event_t *event)
595 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
597 printf("on_leave_notify: xcb=%-8u\n", event->event);
598 send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
599 event->root, event->event, event->child);
602 static void on_focus_in(xcb_focus_in_event_t *event)
604 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
605 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
607 printf("on_focus_in: xcb=%-8u mode=%d\n", event->event, event->mode);
608 xcb_change_window_attributes(conn, event->event,
609 XCB_CW_BORDER_PIXEL, &colors[CLR_FOCUS]);
610 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
611 send_event(EV_FOCUS, event->event);
614 static void on_focus_out(xcb_focus_out_event_t *event)
616 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
617 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
619 printf("on_focus_out: xcb=%-8u mode=%d\n", event->event, event->mode);
620 xcb_change_window_attributes(conn, event->event,
621 XCB_CW_BORDER_PIXEL, &colors[CLR_UNFOCUS]);
622 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
623 send_event(EV_UNFOCUS, event->event);
626 static void on_create_notify(xcb_create_notify_event_t *event)
628 win_t *win = new0(win_t);
629 win_sys_t *sys = new0(win_sys_t);
631 printf("on_create_notify: xcb=%-8u -> win=%p\n",
636 win->w = event->width;
637 win->h = event->height;
640 sys->xcb = event->window;
642 if (!event->override_redirect)
645 tsearch(win, &cache, win_cmp);
648 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
650 win_t *win = win_get(event->window);
651 printf("on_destroy_notify: xcb=%-8u -> win=%p\n",
657 tdelete(win, &cache, win_cmp);
663 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
665 win_t *win = win_get(event->window);
666 printf("on_unmap_notify: xcb=%-8u -> win=%p\n",
670 send_state(win, ST_HIDE);
673 static void on_map_notify(xcb_map_notify_event_t *event)
675 win_t *win = win_get(event->window);
676 printf("on_map_notify: xcb=%-8u -> win=%p\n",
680 send_state(win, ST_SHOW);
683 static void on_map_request(xcb_map_request_event_t *event)
685 win_t *win = win_get(event->window);
686 printf("on_map_request: xcb=%-8u -> win=%p\n",
690 if (do_get_strut(win->sys->xcb, &win->sys->strut))
691 printf("Map: Got a strut!\n");
693 printf("Map: No struts here!\n");
695 send_state(win, ST_SHOW);
696 xcb_map_window(conn, win->sys->xcb);
697 sys_move(win, win->x, win->y, win->w, win->h);
700 static void on_configure_request(xcb_configure_request_event_t *event)
702 win_t *win = win_get(event->window);
703 printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
705 event->width, event->height,
711 win->w = event->width;
712 win->h = event->height;
714 xcb_configure_notify_event_t resp = {
715 .response_type = XCB_CONFIGURE_NOTIFY,
716 .event = win->sys->xcb,
717 .window = win->sys->xcb,
722 .border_width = border,
725 xcb_send_event(conn, 0, win->sys->xcb,
726 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
727 (const char *)&resp);
731 static void on_event(xcb_generic_event_t *event)
733 int type = XCB_EVENT_RESPONSE_TYPE(event);
738 on_key_event((xcb_key_press_event_t *)event, 0);
740 case XCB_KEY_RELEASE:
741 on_key_event((xcb_key_release_event_t *)event, 1);
743 case XCB_BUTTON_PRESS:
744 on_button_event((xcb_button_press_event_t *)event, 0);
746 case XCB_BUTTON_RELEASE:
747 on_button_event((xcb_button_release_event_t *)event, 1);
749 case XCB_MOTION_NOTIFY:
750 on_motion_notify((xcb_motion_notify_event_t *)event);
752 case XCB_ENTER_NOTIFY:
753 on_enter_notify((xcb_enter_notify_event_t *)event);
755 case XCB_LEAVE_NOTIFY:
756 on_leave_notify((xcb_leave_notify_event_t *)event);
759 on_focus_in((xcb_focus_in_event_t *)event);
762 on_focus_out((xcb_focus_out_event_t *)event);
765 /* Window management */
766 case XCB_CREATE_NOTIFY:
767 on_create_notify((xcb_create_notify_event_t *)event);
769 case XCB_DESTROY_NOTIFY:
770 on_destroy_notify((xcb_destroy_notify_event_t *)event);
772 case XCB_UNMAP_NOTIFY:
773 on_unmap_notify((xcb_unmap_notify_event_t *)event);
776 on_map_notify((xcb_map_notify_event_t *)event);
778 case XCB_MAP_REQUEST:
779 on_map_request((xcb_map_request_event_t *)event);
781 case XCB_CONFIGURE_REQUEST:
782 on_configure_request((xcb_configure_request_event_t *)event);
787 printf("on_event: %d:%02X -> %s\n",
788 XCB_EVENT_SENT(event) != 0,
789 XCB_EVENT_RESPONSE_TYPE(event),
790 xcb_event_get_label(type) ?: "unknown_event");
795 /********************
797 ********************/
799 void sys_move(win_t *win, int x, int y, int w, int h)
801 printf("sys_move: %p - %dx%d @ %d,%d\n",
813 do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
816 void sys_raise(win_t *win)
818 printf("sys_raise: %p\n", win);
820 uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
821 uint32_t list = XCB_STACK_MODE_ABOVE;
823 xcb_configure_window(conn, win->sys->xcb, mask, &list);
826 void sys_focus(win_t *win)
828 printf("sys_focus: %p\n", win);
829 xcb_window_t xcb = win ? win->sys->xcb : root;
831 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
832 xcb, XCB_CURRENT_TIME);
835 void sys_show(win_t *win, state_t state)
837 printf("sys_show: %p - %s -> %s\n", win,
838 state_map[win->state], state_map[state]);
839 xcb_window_t xcb = win ? win->sys->xcb : root;
842 win_t *screen = NULL;
843 if (state == ST_FULL || state == ST_MAX) {
844 for (list_t *cur = screens; cur; cur = cur->next) {
846 if (win->x >= screen->x && win->x <= screen->x+screen->w &&
847 win->y >= screen->y && win->y <= screen->y+screen->h)
852 /* Change window state */
855 xcb_unmap_window(conn, xcb);
859 xcb_map_window(conn, xcb);
860 do_configure_window(xcb, win->x, win->y,
861 MAX(win->w - 2*border, 1),
862 MAX(win->h - 2*border, 1),
867 xcb_map_window(conn, xcb);
868 do_configure_window(xcb, screen->x, screen->y, screen->w, screen->h,
869 0, -1, XCB_STACK_MODE_ABOVE);
870 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, xcb);
874 xcb_map_window(conn, xcb);
875 do_configure_window(xcb, screen->x, screen->y,
876 MAX(screen->w - 2*border, 1),
877 MAX(screen->h - 2*border, 1),
878 border, -1, XCB_STACK_MODE_ABOVE);
882 xcb_map_window(conn, xcb);
883 do_configure_window(xcb, -1, -1, -1, stack,
888 xcb_map_window(conn, xcb);
889 do_configure_window(xcb, -1, -1, 100, 100,
894 if (!do_client_message(xcb, wm_delete))
895 xcb_kill_client(conn, xcb);
903 void sys_watch(win_t *win, event_t ev, mod_t mod)
905 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
906 xcb_window_t xcb = win ? win->sys->xcb : root;
907 xcb_event_mask_t *mask = win ? &win->sys->events : &events;
908 xcb_mod_mask_t mods = 0;
909 xcb_button_t btn = 0;
910 xcb_keycode_t *code = 0;
914 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
915 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
919 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
920 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
925 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
926 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
929 case EV_MOUSE0...EV_MOUSE7:
930 btn = event_to_button(ev);
931 mods = mod_to_mask(mod);
932 *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
933 : XCB_EVENT_MASK_BUTTON_PRESS;
934 xcb_grab_button(conn, 0, xcb, *mask,
941 code = event_to_keycodes(ev);
942 mods = mod_to_mask(mod);
943 for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
944 xcb_grab_key(conn, 1, xcb, mods, code[i],
946 XCB_GRAB_MODE_ASYNC);
952 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
954 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
957 list_t *sys_info(void)
959 printf("sys_info\n");
961 if (screens == NULL && do_xinerama_check()) {
962 /* Add Xinerama screens */
963 xcb_xinerama_screen_info_t *info = NULL;
964 int ninfo = do_query_screens(&info);
965 for (int i = 0; i < ninfo; i++) {
966 win_t *screen = new0(win_t);
968 screen->x = info[i].x_org;
969 screen->y = info[i].y_org;
970 screen->w = info[i].width;
971 screen->h = info[i].height;
973 screens = list_insert(NULL, screen);
975 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
976 screen->w, screen->h,
977 screen->x, screen->y);
981 if (screens == NULL) {
982 /* No xinerama support */
983 const xcb_setup_t *setup = xcb_get_setup(conn);
984 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
986 win_t *screen = new0(win_t);
988 screen->w = geom->width_in_pixels;
989 screen->h = geom->height_in_pixels;
991 screens = list_insert(NULL, screen);
993 printf("sys_info: root screen - %dx%d\n",
994 screen->w, screen->h);
1002 printf("sys_init\n");
1004 /* Load configuration */
1005 stack = conf_get_int("main.stack", stack);
1006 border = conf_get_int("main.border", border);
1007 no_capture = conf_get_int("main.no-capture", no_capture);
1009 /* Connect to display */
1010 if (!(conn = xcb_connect(NULL, NULL)))
1011 error("xcb connect failed");
1012 if (xcb_connection_has_error(conn))
1013 error("xcb connection has errors");
1015 /* Get root window */
1016 const xcb_setup_t *setup = xcb_get_setup(conn);
1017 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
1018 root = iter.data->root;
1019 colormap = iter.data->default_colormap;
1022 wm_protos = do_intern_atom("WM_PROTOCOLS");
1023 wm_delete = do_intern_atom("WM_DELETE_WINDOW");
1024 if (!wm_protos || !wm_delete)
1025 error("unable to setup atoms");
1027 /* Setup EWMH connection */
1028 if (!do_ewmh_init_atoms())
1029 error("ewmh setup failed");
1031 /* Setup for for ST_CLOSE */
1032 xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
1034 /* Allocate key symbols */
1035 if (!(keysyms = xcb_key_symbols_alloc(conn)))
1036 error("cannot allocate key symbols");
1038 /* Read color information */
1039 colors[CLR_FOCUS] = do_alloc_color(0xFF6060);
1040 colors[CLR_UNFOCUS] = do_alloc_color(0xD8D8FF);
1041 colors[CLR_URGENT] = do_alloc_color(0xFF0000);
1043 /* Request substructure redirect */
1044 xcb_void_cookie_t cookie;
1045 xcb_generic_error_t *err;
1046 events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1047 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
1048 cookie = xcb_change_window_attributes_checked(conn, root,
1049 XCB_CW_EVENT_MASK, &events);
1050 if ((err = xcb_request_check(conn, cookie)))
1051 error("another window manager is already running");
1056 printf("sys_run\n");
1058 /* Add each initial window */
1060 xcb_window_t *kids = NULL;
1061 int nkids = do_query_tree(root, &kids);
1062 for(int i = 0; i < nkids; i++) {
1063 int override=0, mapped=0;
1064 win_t *win = new0(win_t);
1065 win->sys = new0(win_sys_t);
1066 win->sys->xcb = kids[i];
1067 tsearch(win, &cache, win_cmp);
1068 do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
1069 do_get_window_attributes(kids[i], &override, &mapped);
1071 send_manage(win, 1);
1073 send_state(win, ST_SHOW);
1082 xcb_generic_event_t *event;
1083 if (!(event = xcb_wait_for_event(conn)))
1087 if (!(status = xcb_flush(conn)))
1094 printf("sys_exit\n");
1096 xcb_disconnect(conn);
1102 printf("sys_free\n");
1104 xcb_disconnect(conn);