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 override; // normal vs override redirect
43 int mapped; // window is currently mapped
54 static xcb_connection_t *conn;
55 static xcb_key_symbols_t *keysyms;
56 static xcb_colormap_t colormap;
57 static xcb_window_t root;
58 static xcb_event_mask_t events;
59 static list_t *screens;
61 static xcb_pixmap_t colors[NCOLORS];
62 static unsigned int grabbed;
64 /************************
65 * Conversion functions *
66 ************************/
79 { EV_PAGEUP, 0xFF55 },
80 { EV_PAGEDOWN, 0xFF56 },
95 static xcb_keycode_t *event_to_keycodes(event_t ev)
97 xcb_keycode_t *codes = NULL;
100 xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
103 if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
104 warn("no keycode found for %d->%d", ev, keysym);
109 static event_t keycode_to_event(xcb_keycode_t code)
112 xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
115 return map_get(keysym_map, sym,keysym, ev,keysym);
119 static event_t button_to_event(xcb_button_t btn)
121 return EV_MOUSE0 + btn;
124 static xcb_button_t event_to_button(event_t ev)
126 return ev - EV_MOUSE0;
130 static xcb_mod_mask_t mod_to_mask(mod_t mod)
132 xcb_mod_mask_t mask = 0;
133 if (mod.alt) mask |= XCB_MOD_MASK_1;
134 if (mod.ctrl) mask |= XCB_MOD_MASK_CONTROL;
135 if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
136 if (mod.win) mask |= XCB_MOD_MASK_4;
140 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
142 mod_t mod = { .up = up };
143 if (mask & XCB_MOD_MASK_1) mod.alt = 1;
144 if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl = 1;
145 if (mask & XCB_MOD_MASK_SHIFT) mod.shift = 1;
146 if (mask & XCB_MOD_MASK_4) mod.win = 1;
151 static ptr_t list_to_ptr(int16_t *list)
155 ptr.rx = list[0]; // root_x
156 ptr.ry = list[1]; // root_y
157 ptr.x = list[2]; // event_x
158 ptr.y = list[3]; // event_y
163 /********************
165 ********************/
167 static int win_cmp(const void *_a, const void *_b)
171 if (a->sys->xcb < b->sys->xcb) return -1;
172 if (a->sys->xcb > b->sys->xcb) return 1;
176 static win_t *win_get(xcb_window_t xcb)
178 win_sys_t sys = { .xcb = xcb };
179 win_t key = { .sys = &sys };
181 win_t **win = tfind(&key, &cache, win_cmp);
184 warn("no window for %u", xcb);
195 static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
197 xcb_query_tree_cookie_t cookie =
198 xcb_query_tree(conn, win);
199 if (!cookie.sequence)
200 return warn("do_query_tree: %d - bad cookie", win);
202 xcb_query_tree_reply_t *reply =
203 xcb_query_tree_reply(conn, cookie, NULL);
205 return warn("do_query_tree: %d - no reply", win);
207 int nkids = xcb_query_tree_children_length(reply);
208 *kids = xcb_query_tree_children(reply);
209 printf("do_query_tree: %d - n=%d\n", win, nkids);
213 static int do_get_geometry(xcb_window_t win,
214 int *x, int *y, int *w, int *h)
216 xcb_get_geometry_cookie_t cookie =
217 xcb_get_geometry(conn, win);
218 if (!cookie.sequence)
219 return warn("do_get_geometry: %d - bad cookie", win);
221 xcb_get_geometry_reply_t *reply =
222 xcb_get_geometry_reply(conn, cookie, NULL);
224 return warn("do_get_geometry: %d - no reply", win);
226 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
227 win, reply->width, reply->height, reply->x, reply->y);
235 static int do_get_window_attributes(xcb_window_t win,
236 int *override, int *mapped)
238 xcb_get_window_attributes_cookie_t cookie =
239 xcb_get_window_attributes(conn, win);
240 if (!cookie.sequence)
241 return warn("do_get_window_attributes: %d - bad cookie", win);
243 xcb_get_window_attributes_reply_t *reply =
244 xcb_get_window_attributes_reply(conn, cookie, NULL);
246 return warn("do_get_window_attributes: %d - no reply ", win);
248 printf("do_get_window_attributes: %d - %d\n",
249 win, reply->override_redirect);
250 *override = reply->override_redirect;
251 *mapped = reply->map_state != XCB_MAP_STATE_UNMAPPED;
255 static int do_xinerama_check(void)
257 const xcb_query_extension_reply_t *data =
258 xcb_get_extension_data(conn, &xcb_xinerama_id);
259 if (!data || !data->present)
260 return warn("do_xinerama_check: no ext");
262 xcb_xinerama_is_active_cookie_t cookie =
263 xcb_xinerama_is_active(conn);
264 if (!cookie.sequence)
265 return warn("do_xinerama_check: no cookie");
267 xcb_xinerama_is_active_reply_t *reply =
268 xcb_xinerama_is_active_reply(conn, cookie, NULL);
270 warn("do_xinerama_check: no reply");
272 printf("do_xinerama_check: %d\n", reply->state);
273 return reply && reply->state;
276 static int do_query_screens(xcb_xinerama_screen_info_t **info)
278 xcb_xinerama_query_screens_cookie_t cookie =
279 xcb_xinerama_query_screens(conn);
280 if (!cookie.sequence)
281 return warn("do_query_screens: bad cookie");
283 xcb_xinerama_query_screens_reply_t *reply =
284 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
286 return warn("do_query_screens: no reply");
288 int ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
289 *info = xcb_xinerama_query_screens_screen_info(reply);
290 printf("do_query_screens: %d screens\n", ninfo);
294 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
296 uint16_t r = (rgb & 0xFF0000) >> 8;
297 uint16_t g = (rgb & 0x00FF00);
298 uint16_t b = (rgb & 0x0000FF) << 8;
299 xcb_alloc_color_cookie_t cookie =
300 xcb_alloc_color(conn, colormap, r, g, b);
301 if (!cookie.sequence)
302 return warn("do_alloc_color: bad cookie");
304 xcb_alloc_color_reply_t *reply =
305 xcb_alloc_color_reply(conn, cookie, NULL);
307 return warn("do_alloc_color: no reply");
309 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
313 static void do_grab_pointer(xcb_event_mask_t mask)
316 xcb_grab_pointer(conn, 0, root, mask,
319 0, 0, XCB_CURRENT_TIME);
323 static void do_ungrab_pointer(void)
327 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
330 /**************************
331 * Window Manager Helpers *
332 **************************/
334 /* Send event info */
335 static int send_event(event_t ev, xcb_window_t ewin)
337 win_t *win = win_get(ewin);
339 int status = wm_handle_event(win, ev, MOD(), PTR());
344 /* Send event info */
345 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
346 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
348 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
349 win_t *win = win_get(xcb);
350 mod_t mod = mask_to_mod(mask, up);
351 ptr_t ptr = list_to_ptr(pos);
353 int status = wm_handle_event(win, ev, mod, ptr);
358 /* Send pointer motion info */
359 static int send_pointer(int16_t *pos,
360 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
362 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
363 win_t *win = win_get(xcb);
364 ptr_t ptr = list_to_ptr(pos);
366 int status = wm_handle_ptr(win, ptr);
371 /* Send window state info */
372 static int send_state(void)
378 /**********************
379 * X11 Event Handlers *
380 **********************/
382 /* Specific events */
383 static void on_key_event(xcb_key_press_event_t *event, int up)
385 printf("on_key_event: xcb=%-8u\n", event->event);
386 event_t ev = keycode_to_event(event->detail);
387 send_event_info(ev, event->state, up, &event->root_x,
388 event->root, event->event, event->child);
391 static void on_button_event(xcb_button_press_event_t *event, int up)
393 printf("on_button_event: xcb=%-8u\n", event->event);
394 event_t ev = button_to_event(event->detail);
395 if (!send_event_info(ev, event->state, up, &event->root_x,
396 event->root, event->event, event->child))
397 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
399 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
400 XCB_EVENT_MASK_BUTTON_RELEASE);
406 static void on_motion_notify(xcb_motion_notify_event_t *event)
408 printf("on_motion_notify: xcb=%-8u - %d,%d / %d.%d\n", event->event,
409 event->event_x, event->event_y,
410 event->root_x, event->root_y);
411 send_pointer(&event->root_x, event->root, event->event, event->child);
414 static void on_enter_notify(xcb_enter_notify_event_t *event)
416 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
418 printf("on_enter_notify: xcb=%-8u\n", event->event);
419 send_event_info(EV_ENTER, event->state, 0, &event->root_x,
420 event->root, event->event, event->child);
423 static void on_leave_notify(xcb_leave_notify_event_t *event)
425 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
427 printf("on_leave_notify: xcb=%-8u\n", event->event);
428 send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
429 event->root, event->event, event->child);
432 static void on_focus_in(xcb_focus_in_event_t *event)
434 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
435 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
437 printf("on_focus_in: xcb=%-8u mode=%d\n", event->event, event->mode);
438 xcb_change_window_attributes(conn, event->event,
439 XCB_CW_BORDER_PIXEL, &colors[CLR_FOCUS]);
440 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
441 send_event(EV_FOCUS, event->event);
444 static void on_focus_out(xcb_focus_out_event_t *event)
446 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
447 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
449 printf("on_focus_out: xcb=%-8u mode=%d\n", event->event, event->mode);
450 xcb_change_window_attributes(conn, event->event,
451 XCB_CW_BORDER_PIXEL, &colors[CLR_UNFOCUS]);
452 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
453 send_event(EV_UNFOCUS, event->event);
456 static void on_create_notify(xcb_create_notify_event_t *event)
458 win_t *win = new0(win_t);
459 win_sys_t *sys = new0(win_sys_t);
461 printf("on_create_notify: xcb=%-8u -> win=%p\n",
466 win->w = event->width;
467 win->h = event->height;
470 sys->xcb = event->window;
471 sys->override = event->override_redirect;
473 tsearch(win, &cache, win_cmp);
476 static void on_destroy_notify(win_t *win, xcb_destroy_notify_event_t *event)
478 printf("on_destroy_notify: xcb=%-8u -> win=%p\n",
481 tdelete(win, &cache, win_cmp);
487 static void on_map_request(win_t *win, xcb_map_request_event_t *event)
489 printf("on_map_request: xcb=%-8u -> win=%p\n",
492 if (!win->sys->override && !win->sys->mapped)
494 win->sys->mapped = 1;
496 xcb_map_window(conn, win->sys->xcb);
497 sys_move(win, win->x, win->y, win->w, win->h);
500 static void on_configure_request(win_t *win, xcb_configure_request_event_t *event)
502 printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
504 event->width, event->height,
509 win->w = event->width;
510 win->h = event->height;
512 xcb_configure_notify_event_t resp = {
513 .response_type = XCB_CONFIGURE_NOTIFY,
514 .event = win->sys->xcb,
515 .window = win->sys->xcb,
520 .border_width = border,
523 xcb_send_event(conn, 0, win->sys->xcb,
524 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
525 (const char *)&resp);
529 static void on_event(xcb_generic_event_t *event)
533 int type = XCB_EVENT_RESPONSE_TYPE(event);
534 int sent = XCB_EVENT_SENT(event);
535 const char *name = NULL;
540 on_key_event((xcb_key_press_event_t *)event, 0);
542 case XCB_KEY_RELEASE:
543 on_key_event((xcb_key_release_event_t *)event, 1);
545 case XCB_BUTTON_PRESS:
546 on_button_event((xcb_button_press_event_t *)event, 0);
548 case XCB_BUTTON_RELEASE:
549 on_button_event((xcb_button_release_event_t *)event, 1);
551 case XCB_MOTION_NOTIFY:
552 on_motion_notify((xcb_motion_notify_event_t *)event);
554 case XCB_ENTER_NOTIFY:
555 on_enter_notify((xcb_enter_notify_event_t *)event);
557 case XCB_LEAVE_NOTIFY:
558 on_leave_notify((xcb_leave_notify_event_t *)event);
561 on_focus_in((xcb_focus_in_event_t *)event);
564 on_focus_out((xcb_focus_out_event_t *)event);
567 /* Window management */
568 case XCB_CREATE_NOTIFY:
569 on_create_notify((xcb_create_notify_event_t *)event);
571 case XCB_DESTROY_NOTIFY:
572 if ((win = win_get(((xcb_destroy_notify_event_t *)event)->window)))
573 on_destroy_notify(win, (xcb_destroy_notify_event_t *)event);
575 case XCB_MAP_REQUEST:
576 if ((win = win_get(((xcb_map_request_event_t *)event)->window)))
577 on_map_request(win, (xcb_map_request_event_t *)event);
579 case XCB_CONFIGURE_REQUEST:
580 if ((win = win_get(((xcb_configure_request_event_t *)event)->window)))
581 on_configure_request(win, (xcb_configure_request_event_t *)event);
586 name = xcb_event_get_label(type);
587 printf("on_event: %d:%02X -> %s\n",
588 !!sent, type, name?:"unknown_event");
593 /********************
595 ********************/
597 void sys_move(win_t *win, int x, int y, int w, int h)
599 printf("sys_move: %p - %dx%d @ %d,%d\n",
611 uint16_t mask = XCB_CONFIG_WINDOW_X
612 | XCB_CONFIG_WINDOW_Y
613 | XCB_CONFIG_WINDOW_WIDTH
614 | XCB_CONFIG_WINDOW_HEIGHT
615 | XCB_CONFIG_WINDOW_BORDER_WIDTH;
616 uint32_t list[] = {x, y, w, h, border};
618 xcb_configure_window(conn, win->sys->xcb, mask, list);
621 void sys_raise(win_t *win)
623 printf("sys_raise: %p\n", win);
624 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, win->sys->xcb);
627 void sys_focus(win_t *win)
629 printf("sys_focus: %p\n", win);
630 xcb_window_t xcb = win ? win->sys->xcb : root;
632 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
633 xcb, XCB_CURRENT_TIME);
636 void sys_show(win_t *win, state_t state)
638 printf("sys_show: %p - %d\n", win, state);
641 void sys_watch(win_t *win, event_t ev, mod_t mod)
643 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
644 xcb_window_t xcb = win ? win->sys->xcb : root;
645 xcb_event_mask_t *mask = win ? &win->sys->events : &events;
646 xcb_mod_mask_t mods = 0;
647 xcb_button_t btn = 0;
648 xcb_keycode_t *code = 0;
652 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
653 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
657 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
658 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
663 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
664 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
667 case EV_MOUSE0...EV_MOUSE7:
668 btn = event_to_button(ev);
669 mods = mod_to_mask(mod);
670 *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
671 : XCB_EVENT_MASK_BUTTON_PRESS;
672 xcb_grab_button(conn, 0, xcb, *mask,
679 code = event_to_keycodes(ev);
680 mods = mod_to_mask(mod);
681 for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
682 xcb_grab_key(conn, 1, xcb, mods, code[i],
684 XCB_GRAB_MODE_ASYNC);
690 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
692 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
695 list_t *sys_info(void)
697 printf("sys_info\n");
699 if (screens == NULL && do_xinerama_check()) {
700 /* Add Xinerama screens */
701 xcb_xinerama_screen_info_t *info = NULL;
702 int ninfo = do_query_screens(&info);
703 for (int i = 0; i < ninfo; i++) {
704 win_t *screen = new0(win_t);
706 screen->x = info[i].x_org;
707 screen->y = info[i].y_org;
708 screen->w = info[i].width;
709 screen->h = info[i].height;
711 screens = list_insert(NULL, screen);
713 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
714 screen->w, screen->h,
715 screen->x, screen->y);
719 if (screens == NULL) {
720 /* No xinerama support */
721 const xcb_setup_t *setup = xcb_get_setup(conn);
722 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
724 win_t *screen = new0(win_t);
726 screen->w = geom->width_in_pixels;
727 screen->h = geom->height_in_pixels;
729 screens = list_insert(NULL, screen);
731 printf("sys_info: root screen - %dx%d\n",
732 screen->w, screen->h);
740 printf("sys_init\n");
742 /* Load configuration */
743 stack = conf_get_int("main.stack", stack);
744 border = conf_get_int("main.border", border);
745 no_capture = conf_get_int("main.no-capture", no_capture);
747 /* Connect to display */
748 if (!(conn = xcb_connect(NULL, NULL)))
749 error("xcb connect failed");
750 if (xcb_connection_has_error(conn))
751 error("xcb connection has errors");
753 /* Get root window */
754 const xcb_setup_t *setup = xcb_get_setup(conn);
755 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
756 root = iter.data->root;
757 colormap = iter.data->default_colormap;
759 /* Allocate key symbols */
760 if (!(keysyms = xcb_key_symbols_alloc(conn)))
761 error("cannot allocate key symbols");
763 /* Read color information */
764 colors[CLR_FOCUS] = do_alloc_color(0xFF6060);
765 colors[CLR_UNFOCUS] = do_alloc_color(0xD8D8FF);
766 colors[CLR_URGENT] = do_alloc_color(0xFF0000);
768 /* Request substructure redirect */
769 xcb_void_cookie_t cookie;
770 xcb_generic_error_t *err;
771 events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
772 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
773 cookie = xcb_change_window_attributes_checked(conn, root,
774 XCB_CW_EVENT_MASK, &events);
775 if ((err = xcb_request_check(conn, cookie)))
776 error("another window manager is already running");
783 /* Add each initial window */
785 xcb_window_t *kids = NULL;
786 int nkids = do_query_tree(root, &kids);
787 for(int i = 0; i < nkids; i++) {
788 win_t *win = new0(win_t);
789 win->sys = new0(win_sys_t);
790 win->sys->xcb = kids[i];
791 do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
792 do_get_window_attributes(kids[i],
793 &win->sys->override, &win->sys->mapped);
794 tsearch(win, &cache, win_cmp);
795 if (!win->sys->override && win->sys->mapped)
805 xcb_generic_event_t *event;
806 if (!(event = xcb_wait_for_event(conn)))
810 if (!(status = xcb_flush(conn)))
817 printf("sys_exit\n");
819 xcb_disconnect(conn);
825 printf("sys_free\n");
827 xcb_disconnect(conn);