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");
348 xcb_atom_t atom = reply->atom;
353 static int do_ewmh_init_atoms(void)
355 xcb_intern_atom_cookie_t *cookies =
356 xcb_ewmh_init_atoms(conn, &ewmh);
358 return warn("do_ewmh_init_atoms: no cookies");
361 xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
363 return warn("do_ewmh_init_atoms: no status");
367 static int do_get_strut(xcb_window_t win, strut_t *strut)
369 xcb_get_property_cookie_t cookie =
370 xcb_ewmh_get_wm_strut(&ewmh, win);
371 if (!cookie.sequence)
372 return warn("do_get_strut: bad cookie");
374 xcb_ewmh_get_extents_reply_t ext = {};
376 xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
378 return warn("do_get_strut: no status");
380 strut->left = ext.left;
381 strut->right = ext.right;
382 strut->top = ext.top;
383 strut->bottom = ext.bottom;
385 return ext.left || ext.right || ext.top || ext.bottom;
388 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
390 uint16_t r = (rgb & 0xFF0000) >> 8;
391 uint16_t g = (rgb & 0x00FF00);
392 uint16_t b = (rgb & 0x0000FF) << 8;
393 xcb_alloc_color_cookie_t cookie =
394 xcb_alloc_color(conn, colormap, r, g, b);
395 if (!cookie.sequence)
396 return warn("do_alloc_color: bad cookie");
398 xcb_alloc_color_reply_t *reply =
399 xcb_alloc_color_reply(conn, cookie, NULL);
401 return warn("do_alloc_color: no reply");
403 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
404 xcb_pixmap_t pixel = reply->pixel;
409 static void do_grab_pointer(xcb_event_mask_t mask)
412 xcb_grab_pointer(conn, 0, root, mask,
415 0, 0, XCB_CURRENT_TIME);
419 static void do_ungrab_pointer(void)
423 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
426 static void do_configure_window(xcb_window_t win,
427 int x, int y, int w, int h,
431 { x, XCB_CONFIG_WINDOW_X },
432 { y, XCB_CONFIG_WINDOW_Y },
433 { w, XCB_CONFIG_WINDOW_WIDTH },
434 { h, XCB_CONFIG_WINDOW_HEIGHT },
435 { b, XCB_CONFIG_WINDOW_BORDER_WIDTH },
436 { s, XCB_CONFIG_WINDOW_SIBLING },
437 { r, XCB_CONFIG_WINDOW_STACK_MODE },
441 uint32_t list[7] = {};
442 for (int i = 0; i < 7; i++) {
443 if (table[i][0] >= 0) {
444 list[i] = table[i][0];
449 xcb_configure_window(conn, win, mask, list);
452 static int do_client_message(xcb_window_t win, xcb_atom_t atom)
455 xcb_get_property_cookie_t cookie =
456 xcb_icccm_get_wm_protocols(conn, win, wm_protos);
457 if (!cookie.sequence)
458 return warn("do_client_message: %d - bad cookie", win);
460 xcb_icccm_get_wm_protocols_reply_t protos = {};
461 if (!xcb_icccm_get_wm_protocols_reply(conn, cookie, &protos, NULL))
462 return warn("do_client_message: %d - no reply", win);
464 /* Search for the atom */
466 for (int i = 0; i < protos.atoms_len; i++)
467 if (protos.atoms[i] == atom)
470 return warn("do_client_message: %d - no atom", win);
472 /* Send the message */
473 xcb_client_message_event_t msg = {
474 .response_type = XCB_CLIENT_MESSAGE,
478 .data.data32[0] = atom,
479 .data.data32[1] = XCB_CURRENT_TIME,
481 xcb_send_event(conn, 0, win, XCB_EVENT_MASK_NO_EVENT,
486 /**************************
487 * Window Manager Helpers *
488 **************************/
490 /* Send event info */
491 static int send_event(event_t ev, xcb_window_t ewin)
493 win_t *win = win_get(ewin);
495 int status = wm_handle_event(win, ev, MOD(), PTR());
500 /* Send event info */
501 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
502 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
504 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
505 win_t *win = win_get(xcb);
506 mod_t mod = mask_to_mod(mask, up);
507 ptr_t ptr = list_to_ptr(pos);
509 int status = wm_handle_event(win, ev, mod, ptr);
514 /* Send pointer motion info */
515 static int send_pointer(int16_t *pos,
516 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
518 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
519 win_t *win = win_get(xcb);
520 ptr_t ptr = list_to_ptr(pos);
522 int status = wm_handle_ptr(win, ptr);
527 /* Send window state info */
528 static void send_manage(win_t *win, int managed)
530 if (win->sys->managed == managed)
536 win->sys->managed = managed;
539 /* Send window state info */
540 static void send_state(win_t *win, state_t next)
542 if (!win->sys->managed)
544 if (win->state == next)
546 state_t prev = win->state;
548 wm_handle_state(win, prev, next);
551 /**********************
552 * X11 Event Handlers *
553 **********************/
555 /* Specific events */
556 static void on_key_event(xcb_key_press_event_t *event, int up)
558 printf("on_key_event: xcb=%-8u\n", event->event);
559 xcb_window_t focus = do_get_input_focus();
560 event_t ev = keycode_to_event(event->detail);
561 send_event_info(ev, event->state, up, &event->root_x,
562 event->root, focus, event->child);
565 static void on_button_event(xcb_button_press_event_t *event, int up)
567 printf("on_button_event: xcb=%-8u\n", event->event);
568 event_t ev = button_to_event(event->detail);
569 if (!send_event_info(ev, event->state, up, &event->root_x,
570 event->root, event->event, event->child))
571 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
573 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
574 XCB_EVENT_MASK_BUTTON_RELEASE);
580 static void on_motion_notify(xcb_motion_notify_event_t *event)
582 printf("on_motion_notify: xcb=%-8u - %d,%d / %d.%d\n", event->event,
583 event->event_x, event->event_y,
584 event->root_x, event->root_y);
585 send_pointer(&event->root_x, event->root, event->event, event->child);
588 static void on_enter_notify(xcb_enter_notify_event_t *event)
590 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
592 printf("on_enter_notify: xcb=%-8u\n", event->event);
593 send_event_info(EV_ENTER, event->state, 0, &event->root_x,
594 event->root, event->event, event->child);
597 static void on_leave_notify(xcb_leave_notify_event_t *event)
599 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
601 printf("on_leave_notify: xcb=%-8u\n", event->event);
602 send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
603 event->root, event->event, event->child);
606 static void on_focus_in(xcb_focus_in_event_t *event)
608 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
609 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
611 printf("on_focus_in: xcb=%-8u mode=%d\n", event->event, event->mode);
612 xcb_change_window_attributes(conn, event->event,
613 XCB_CW_BORDER_PIXEL, &colors[CLR_FOCUS]);
614 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
615 send_event(EV_FOCUS, event->event);
618 static void on_focus_out(xcb_focus_out_event_t *event)
620 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
621 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
623 printf("on_focus_out: xcb=%-8u mode=%d\n", event->event, event->mode);
624 xcb_change_window_attributes(conn, event->event,
625 XCB_CW_BORDER_PIXEL, &colors[CLR_UNFOCUS]);
626 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
627 send_event(EV_UNFOCUS, event->event);
630 static void on_create_notify(xcb_create_notify_event_t *event)
632 win_t *win = new0(win_t);
633 win_sys_t *sys = new0(win_sys_t);
635 printf("on_create_notify: xcb=%-8u -> win=%p\n",
640 win->w = event->width;
641 win->h = event->height;
644 sys->xcb = event->window;
646 if (!event->override_redirect)
649 tsearch(win, &cache, win_cmp);
652 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
654 win_t *win = win_get(event->window);
655 printf("on_destroy_notify: xcb=%-8u -> win=%p\n",
661 tdelete(win, &cache, win_cmp);
667 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
669 win_t *win = win_get(event->window);
670 printf("on_unmap_notify: xcb=%-8u -> win=%p\n",
674 send_state(win, ST_HIDE);
677 static void on_map_notify(xcb_map_notify_event_t *event)
679 win_t *win = win_get(event->window);
680 printf("on_map_notify: xcb=%-8u -> win=%p\n",
684 send_state(win, ST_SHOW);
687 static void on_map_request(xcb_map_request_event_t *event)
689 win_t *win = win_get(event->window);
690 printf("on_map_request: xcb=%-8u -> win=%p\n",
694 if (do_get_strut(win->sys->xcb, &win->sys->strut))
695 printf("Map: Got a strut!\n");
697 printf("Map: No struts here!\n");
699 send_state(win, ST_SHOW);
700 xcb_map_window(conn, win->sys->xcb);
701 sys_move(win, win->x, win->y, win->w, win->h);
704 static void on_configure_request(xcb_configure_request_event_t *event)
706 win_t *win = win_get(event->window);
707 printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
709 event->width, event->height,
715 win->w = event->width;
716 win->h = event->height;
718 xcb_configure_notify_event_t resp = {
719 .response_type = XCB_CONFIGURE_NOTIFY,
720 .event = win->sys->xcb,
721 .window = win->sys->xcb,
726 .border_width = border,
729 xcb_send_event(conn, 0, win->sys->xcb,
730 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
731 (const char *)&resp);
735 static void on_event(xcb_generic_event_t *event)
737 int type = XCB_EVENT_RESPONSE_TYPE(event);
742 on_key_event((xcb_key_press_event_t *)event, 0);
744 case XCB_KEY_RELEASE:
745 on_key_event((xcb_key_release_event_t *)event, 1);
747 case XCB_BUTTON_PRESS:
748 on_button_event((xcb_button_press_event_t *)event, 0);
750 case XCB_BUTTON_RELEASE:
751 on_button_event((xcb_button_release_event_t *)event, 1);
753 case XCB_MOTION_NOTIFY:
754 on_motion_notify((xcb_motion_notify_event_t *)event);
756 case XCB_ENTER_NOTIFY:
757 on_enter_notify((xcb_enter_notify_event_t *)event);
759 case XCB_LEAVE_NOTIFY:
760 on_leave_notify((xcb_leave_notify_event_t *)event);
763 on_focus_in((xcb_focus_in_event_t *)event);
766 on_focus_out((xcb_focus_out_event_t *)event);
769 /* Window management */
770 case XCB_CREATE_NOTIFY:
771 on_create_notify((xcb_create_notify_event_t *)event);
773 case XCB_DESTROY_NOTIFY:
774 on_destroy_notify((xcb_destroy_notify_event_t *)event);
776 case XCB_UNMAP_NOTIFY:
777 on_unmap_notify((xcb_unmap_notify_event_t *)event);
780 on_map_notify((xcb_map_notify_event_t *)event);
782 case XCB_MAP_REQUEST:
783 on_map_request((xcb_map_request_event_t *)event);
785 case XCB_CONFIGURE_REQUEST:
786 on_configure_request((xcb_configure_request_event_t *)event);
791 printf("on_event: %d:%02X -> %s\n",
792 XCB_EVENT_SENT(event) != 0,
793 XCB_EVENT_RESPONSE_TYPE(event),
794 xcb_event_get_label(type) ?: "unknown_event");
799 /********************
801 ********************/
803 void sys_move(win_t *win, int x, int y, int w, int h)
805 printf("sys_move: %p - %dx%d @ %d,%d\n",
817 do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
820 void sys_raise(win_t *win)
822 printf("sys_raise: %p\n", win);
824 uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
825 uint32_t list = XCB_STACK_MODE_ABOVE;
827 xcb_configure_window(conn, win->sys->xcb, mask, &list);
830 void sys_focus(win_t *win)
832 printf("sys_focus: %p\n", win);
833 xcb_window_t xcb = win ? win->sys->xcb : root;
835 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
836 xcb, XCB_CURRENT_TIME);
839 void sys_show(win_t *win, state_t state)
841 printf("sys_show: %p - %s -> %s\n", win,
842 state_map[win->state], state_map[state]);
843 xcb_window_t xcb = win ? win->sys->xcb : root;
846 win_t *screen = NULL;
847 if (state == ST_FULL || state == ST_MAX) {
848 for (list_t *cur = screens; cur; cur = cur->next) {
850 if (win->x >= screen->x && win->x <= screen->x+screen->w &&
851 win->y >= screen->y && win->y <= screen->y+screen->h)
856 /* Change window state */
859 xcb_unmap_window(conn, xcb);
863 xcb_map_window(conn, xcb);
864 do_configure_window(xcb, win->x, win->y,
865 MAX(win->w - 2*border, 1),
866 MAX(win->h - 2*border, 1),
871 xcb_map_window(conn, xcb);
872 do_configure_window(xcb, screen->x, screen->y, screen->w, screen->h,
873 0, -1, XCB_STACK_MODE_ABOVE);
874 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, xcb);
878 xcb_map_window(conn, xcb);
879 do_configure_window(xcb, screen->x, screen->y,
880 MAX(screen->w - 2*border, 1),
881 MAX(screen->h - 2*border, 1),
882 border, -1, XCB_STACK_MODE_ABOVE);
886 xcb_map_window(conn, xcb);
887 do_configure_window(xcb, -1, -1, -1, stack,
892 xcb_map_window(conn, xcb);
893 do_configure_window(xcb, -1, -1, 100, 100,
898 if (!do_client_message(xcb, wm_delete))
899 xcb_kill_client(conn, xcb);
907 void sys_watch(win_t *win, event_t ev, mod_t mod)
909 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
910 xcb_window_t xcb = win ? win->sys->xcb : root;
911 xcb_event_mask_t *mask = win ? &win->sys->events : &events;
912 xcb_mod_mask_t mods = 0;
913 xcb_button_t btn = 0;
914 xcb_keycode_t *code = 0;
918 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
919 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
923 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
924 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
929 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
930 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
933 case EV_MOUSE0...EV_MOUSE7:
934 btn = event_to_button(ev);
935 mods = mod_to_mask(mod);
936 *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
937 : XCB_EVENT_MASK_BUTTON_PRESS;
938 xcb_grab_button(conn, 0, xcb, *mask,
945 code = event_to_keycodes(ev);
946 mods = mod_to_mask(mod);
947 for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
948 xcb_grab_key(conn, 1, xcb, mods, code[i],
950 XCB_GRAB_MODE_ASYNC);
956 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
958 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
961 list_t *sys_info(void)
963 printf("sys_info\n");
965 if (screens == NULL && do_xinerama_check()) {
966 /* Add Xinerama screens */
967 xcb_xinerama_screen_info_t *info = NULL;
968 int ninfo = do_query_screens(&info);
969 for (int i = 0; i < ninfo; i++) {
970 win_t *screen = new0(win_t);
972 screen->x = info[i].x_org;
973 screen->y = info[i].y_org;
974 screen->w = info[i].width;
975 screen->h = info[i].height;
977 screens = list_insert(NULL, screen);
979 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
980 screen->w, screen->h,
981 screen->x, screen->y);
985 if (screens == NULL) {
986 /* No xinerama support */
987 const xcb_setup_t *setup = xcb_get_setup(conn);
988 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
990 win_t *screen = new0(win_t);
992 screen->w = geom->width_in_pixels;
993 screen->h = geom->height_in_pixels;
995 screens = list_insert(NULL, screen);
997 printf("sys_info: root screen - %dx%d\n",
998 screen->w, screen->h);
1006 printf("sys_init\n");
1008 /* Load configuration */
1009 stack = conf_get_int("main.stack", stack);
1010 border = conf_get_int("main.border", border);
1011 no_capture = conf_get_int("main.no-capture", no_capture);
1013 /* Connect to display */
1014 if (!(conn = xcb_connect(NULL, NULL)))
1015 error("xcb connect failed");
1016 if (xcb_connection_has_error(conn))
1017 error("xcb connection has errors");
1019 /* Get root window */
1020 const xcb_setup_t *setup = xcb_get_setup(conn);
1021 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
1022 root = iter.data->root;
1023 colormap = iter.data->default_colormap;
1026 wm_protos = do_intern_atom("WM_PROTOCOLS");
1027 wm_delete = do_intern_atom("WM_DELETE_WINDOW");
1028 if (!wm_protos || !wm_delete)
1029 error("unable to setup atoms");
1031 /* Setup EWMH connection */
1032 if (!do_ewmh_init_atoms())
1033 error("ewmh setup failed");
1035 /* Setup for for ST_CLOSE */
1036 xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
1038 /* Allocate key symbols */
1039 if (!(keysyms = xcb_key_symbols_alloc(conn)))
1040 error("cannot allocate key symbols");
1042 /* Read color information */
1043 colors[CLR_FOCUS] = do_alloc_color(0xFF6060);
1044 colors[CLR_UNFOCUS] = do_alloc_color(0xD8D8FF);
1045 colors[CLR_URGENT] = do_alloc_color(0xFF0000);
1047 /* Request substructure redirect */
1048 xcb_void_cookie_t cookie;
1049 xcb_generic_error_t *err;
1050 events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1051 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
1052 cookie = xcb_change_window_attributes_checked(conn, root,
1053 XCB_CW_EVENT_MASK, &events);
1054 if ((err = xcb_request_check(conn, cookie)))
1055 error("another window manager is already running");
1060 printf("sys_run\n");
1062 /* Add each initial window */
1064 xcb_window_t *kids = NULL;
1065 int nkids = do_query_tree(root, &kids);
1066 for(int i = 0; i < nkids; i++) {
1067 int override=0, mapped=0;
1068 win_t *win = new0(win_t);
1069 win->sys = new0(win_sys_t);
1070 win->sys->xcb = kids[i];
1071 tsearch(win, &cache, win_cmp);
1072 do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
1073 do_get_window_attributes(kids[i], &override, &mapped);
1075 send_manage(win, 1);
1077 send_state(win, ST_SHOW);
1086 xcb_generic_event_t *event;
1087 if (!(event = xcb_wait_for_event(conn)))
1091 if (!(status = xcb_flush(conn)))
1098 printf("sys_exit\n");
1100 xcb_disconnect(conn);
1106 printf("sys_free\n");
1108 xcb_ewmh_connection_wipe(&ewmh);
1109 xcb_disconnect(conn);