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_window_t last;
59 static xcb_event_mask_t events;
60 static list_t *screens;
62 static xcb_pixmap_t colors[NCOLORS];
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)
154 ptr.rx = list[0]; // root_x
155 ptr.ry = list[1]; // root_y
156 ptr.x = list[2]; // event_x
157 ptr.y = list[3]; // event_y
161 /********************
163 ********************/
165 static int win_cmp(const void *_a, const void *_b)
169 if (a->sys->xcb < b->sys->xcb) return -1;
170 if (a->sys->xcb > b->sys->xcb) return 1;
174 static win_t *win_get(xcb_window_t xcb)
176 win_sys_t sys = { .xcb = xcb };
177 win_t key = { .sys = &sys };
179 win_t **win = tfind(&key, &cache, win_cmp);
182 warn("no window for %u", xcb);
193 static int do_query_tree(xcb_window_t win, xcb_window_t **kids)
195 xcb_query_tree_cookie_t cookie =
196 xcb_query_tree(conn, win);
197 if (!cookie.sequence)
198 return warn("do_query_tree: %d - bad cookie", win);
200 xcb_query_tree_reply_t *reply =
201 xcb_query_tree_reply(conn, cookie, NULL);
203 return warn("do_query_tree: %d - no reply", win);
205 int nkids = xcb_query_tree_children_length(reply);
206 *kids = xcb_query_tree_children(reply);
207 printf("do_query_tree: %d - n=%d\n", win, nkids);
211 static int do_get_geometry(xcb_window_t win,
212 int *x, int *y, int *w, int *h)
214 xcb_get_geometry_cookie_t cookie =
215 xcb_get_geometry(conn, win);
216 if (!cookie.sequence)
217 return warn("do_get_geometry: %d - bad cookie", win);
219 xcb_get_geometry_reply_t *reply =
220 xcb_get_geometry_reply(conn, cookie, NULL);
222 return warn("do_get_geometry: %d - no reply", win);
224 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
225 win, reply->width, reply->height, reply->x, reply->y);
233 static int do_get_window_attributes(xcb_window_t win,
234 int *override, int *mapped)
236 xcb_get_window_attributes_cookie_t cookie =
237 xcb_get_window_attributes(conn, win);
238 if (!cookie.sequence)
239 return warn("do_get_window_attributes: %d - bad cookie", win);
241 xcb_get_window_attributes_reply_t *reply =
242 xcb_get_window_attributes_reply(conn, cookie, NULL);
244 return warn("do_get_window_attributes: %d - no reply ", win);
246 printf("do_get_window_attributes: %d - %d\n",
247 win, reply->override_redirect);
248 *override = reply->override_redirect;
249 *mapped = reply->map_state != XCB_MAP_STATE_UNMAPPED;
253 static int do_xinerama_check(void)
255 const xcb_query_extension_reply_t *data =
256 xcb_get_extension_data(conn, &xcb_xinerama_id);
257 if (!data || !data->present)
258 return warn("do_xinerama_check: no ext");
260 xcb_xinerama_is_active_cookie_t cookie =
261 xcb_xinerama_is_active(conn);
262 if (!cookie.sequence)
263 return warn("do_xinerama_check: no cookie");
265 xcb_xinerama_is_active_reply_t *reply =
266 xcb_xinerama_is_active_reply(conn, cookie, NULL);
268 warn("do_xinerama_check: no reply");
270 printf("do_xinerama_check: %d\n", reply->state);
271 return reply && reply->state;
274 static int do_query_screens(xcb_xinerama_screen_info_t **info)
276 xcb_xinerama_query_screens_cookie_t cookie =
277 xcb_xinerama_query_screens(conn);
278 if (!cookie.sequence)
279 return warn("do_query_screens: bad cookie");
281 xcb_xinerama_query_screens_reply_t *reply =
282 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
284 return warn("do_query_screens: no reply");
286 int ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
287 *info = xcb_xinerama_query_screens_screen_info(reply);
288 printf("do_query_screens: %d screens\n", ninfo);
292 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
294 uint16_t r = (rgb & 0xFF0000) >> 8;
295 uint16_t g = (rgb & 0x00FF00);
296 uint16_t b = (rgb & 0x0000FF) << 8;
297 xcb_alloc_color_cookie_t cookie =
298 xcb_alloc_color(conn, colormap, r, g, b);
299 if (!cookie.sequence)
300 return warn("do_alloc_color: bad cookie");
302 xcb_alloc_color_reply_t *reply =
303 xcb_alloc_color_reply(conn, cookie, NULL);
305 return warn("do_alloc_color: no reply");
307 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
311 /**********************
312 * X11 Event Handlers *
313 **********************/
315 /* Specific events */
316 static void on_key_event(xcb_key_press_event_t *event, int up)
318 xcb_window_t xcb = event->event == root ?
319 event->child : event->event;
320 win_t *win = win_get(xcb);
321 event_t ev = keycode_to_event(event->detail);
322 mod_t mod = mask_to_mod(event->state, up);
323 ptr_t ptr = list_to_ptr(&event->root_x);
324 printf("on_key_event: xcb=%u -> win=%p\n", xcb, win);
325 wm_handle_event(win, ev, mod, ptr);
328 static void on_button_event(xcb_button_press_event_t *event, int up)
330 xcb_window_t xcb = event->event == root ?
331 event->child : event->event;
332 win_t *win = win_get(xcb);
333 event_t ev = button_to_event(event->detail);
334 mod_t mod = mask_to_mod(event->state, up);
335 ptr_t ptr = list_to_ptr(&event->root_x);
336 printf("on_button_event: xcb=%u -> win=%p\n", xcb, win);
338 if (!wm_handle_event(win, ev, mod, ptr))
339 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
341 xcb_grab_pointer(conn, 1, xcb,
342 XCB_EVENT_MASK_POINTER_MOTION |
343 XCB_EVENT_MASK_BUTTON_RELEASE,
346 0, 0, XCB_CURRENT_TIME);
348 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
352 static void on_motion_notify(xcb_motion_notify_event_t *event)
354 xcb_window_t xcb = event->event == root ?
355 event->child : event->event;
356 win_t *win = win_get(xcb);
357 ptr_t ptr = list_to_ptr(&event->root_x);
358 printf("on_motion_notify: xcb=%u -> win=%p - %d,%d / %d.%d\n", xcb, win,
359 ptr.x, ptr.y, ptr.rx, ptr.ry);
360 wm_handle_ptr(win, ptr);
363 static void on_create_notify(xcb_create_notify_event_t *event)
365 win_t *win = new0(win_t);
366 win_sys_t *sys = new0(win_sys_t);
368 printf("on_create_notify: xcb=%u -> win=%p\n",
373 win->w = event->width;
374 win->h = event->height;
377 sys->xcb = event->window;
378 sys->override = event->override_redirect;
380 tsearch(win, &cache, win_cmp);
383 static void on_destroy_notify(win_t *win, xcb_destroy_notify_event_t *event)
385 printf("on_destroy_notify: xcb=%u -> win=%p\n",
388 tdelete(win, &cache, win_cmp);
390 if (win->sys->xcb == last)
397 static void on_map_request(win_t *win, xcb_map_request_event_t *event)
399 printf("on_map_request: xcb=%u -> win=%p\n",
402 if (!win->sys->override && !win->sys->mapped)
404 win->sys->mapped = 1;
406 xcb_map_window(conn, win->sys->xcb);
407 sys_move(win, win->x, win->y, win->w, win->h);
410 static void on_configure_request(win_t *win, xcb_configure_request_event_t *event)
412 printf("on_configure_request: xcb=%u -> win=%p -- %dx%d @ %d,%d\n",
414 event->width, event->height,
419 win->w = event->width;
420 win->h = event->height;
422 xcb_configure_notify_event_t resp = {
423 .response_type = XCB_CONFIGURE_NOTIFY,
424 .event = win->sys->xcb,
425 .window = win->sys->xcb,
430 .border_width = border,
433 xcb_send_event(conn, 0, win->sys->xcb,
434 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
435 (const char *)&resp);
439 static void on_event(xcb_generic_event_t *event)
443 int type = XCB_EVENT_RESPONSE_TYPE(event);
444 int sent = XCB_EVENT_SENT(event);
445 const char *name = NULL;
450 on_key_event((xcb_key_press_event_t *)event, 0);
452 case XCB_KEY_RELEASE:
453 on_key_event((xcb_key_release_event_t *)event, 1);
455 case XCB_BUTTON_PRESS:
456 on_button_event((xcb_button_press_event_t *)event, 0);
458 case XCB_BUTTON_RELEASE:
459 on_button_event((xcb_button_release_event_t *)event, 1);
461 case XCB_MOTION_NOTIFY:
462 on_motion_notify((xcb_motion_notify_event_t *)event);
465 /* Window management */
466 case XCB_CREATE_NOTIFY:
467 on_create_notify((xcb_create_notify_event_t *)event);
469 case XCB_DESTROY_NOTIFY:
470 if ((win = win_get(((xcb_destroy_notify_event_t *)event)->window)))
471 on_destroy_notify(win, (xcb_destroy_notify_event_t *)event);
473 case XCB_MAP_REQUEST:
474 if ((win = win_get(((xcb_map_request_event_t *)event)->window)))
475 on_map_request(win, (xcb_map_request_event_t *)event);
477 case XCB_CONFIGURE_REQUEST:
478 if ((win = win_get(((xcb_configure_request_event_t *)event)->window)))
479 on_configure_request(win, (xcb_configure_request_event_t *)event);
484 name = xcb_event_get_label(type);
485 printf("on_event: %d:%02X -> %s\n",
486 !!sent, type, name?:"unknown_event");
491 /********************
493 ********************/
495 void sys_move(win_t *win, int x, int y, int w, int h)
497 printf("sys_move: %p - %dx%d @ %d,%d\n",
509 uint16_t mask = XCB_CONFIG_WINDOW_X
510 | XCB_CONFIG_WINDOW_Y
511 | XCB_CONFIG_WINDOW_WIDTH
512 | XCB_CONFIG_WINDOW_HEIGHT
513 | XCB_CONFIG_WINDOW_BORDER_WIDTH;
514 uint32_t list[] = {x, y, w, h, border};
516 xcb_configure_window(conn, win->sys->xcb, mask, list);
519 void sys_raise(win_t *win)
521 printf("sys_raise: %p\n", win);
522 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, win->sys->xcb);
525 void sys_focus(win_t *win)
527 printf("sys_focus: %p\n", win);
528 xcb_window_t xcb = win ? win->sys->xcb : root;
530 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
531 xcb, XCB_CURRENT_TIME);
534 xcb_change_window_attributes(conn, last, XCB_CW_BORDER_PIXEL, &colors[CLR_UNFOCUS]);
535 xcb_change_window_attributes(conn, xcb, XCB_CW_BORDER_PIXEL, &colors[CLR_FOCUS]);
539 void sys_show(win_t *win, state_t state)
541 printf("sys_show: %p - %d\n", win, state);
544 void sys_watch(win_t *win, event_t ev, mod_t mod)
546 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
547 xcb_window_t xcb = win ? win->sys->xcb : root;
548 xcb_event_mask_t *mask = win ? &win->sys->events : &events;
549 xcb_mod_mask_t mods = 0;
550 xcb_button_t btn = 0;
551 xcb_keycode_t *code = 0;
555 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
556 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
560 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
561 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
566 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
567 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
570 case EV_MOUSE0...EV_MOUSE7:
571 btn = event_to_button(ev);
572 mods = mod_to_mask(mod);
573 *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
574 : XCB_EVENT_MASK_BUTTON_PRESS;
575 xcb_grab_button(conn, 0, xcb, *mask,
582 code = event_to_keycodes(ev);
583 mods = mod_to_mask(mod);
584 for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
585 xcb_grab_key(conn, 1, xcb, mods, code[i],
587 XCB_GRAB_MODE_ASYNC);
593 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
595 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
598 list_t *sys_info(void)
600 printf("sys_info\n");
602 if (screens == NULL && do_xinerama_check()) {
603 /* Add Xinerama screens */
604 xcb_xinerama_screen_info_t *info = NULL;
605 int ninfo = do_query_screens(&info);
606 for (int i = 0; i < ninfo; i++) {
607 win_t *screen = new0(win_t);
609 screen->x = info[i].x_org;
610 screen->y = info[i].y_org;
611 screen->w = info[i].width;
612 screen->h = info[i].height;
614 screens = list_insert(NULL, screen);
616 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
617 screen->w, screen->h,
618 screen->x, screen->y);
622 if (screens == NULL) {
623 /* No xinerama support */
624 const xcb_setup_t *setup = xcb_get_setup(conn);
625 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
627 win_t *screen = new0(win_t);
629 screen->w = geom->width_in_pixels;
630 screen->h = geom->height_in_pixels;
632 screens = list_insert(NULL, screen);
634 printf("sys_info: root screen - %dx%d\n",
635 screen->w, screen->h);
643 printf("sys_init\n");
645 /* Load configuration */
646 stack = conf_get_int("main.stack", stack);
647 border = conf_get_int("main.border", border);
648 no_capture = conf_get_int("main.no-capture", no_capture);
650 /* Connect to display */
651 if (!(conn = xcb_connect(NULL, NULL)))
652 error("xcb connect failed");
653 if (xcb_connection_has_error(conn))
654 error("xcb connection has errors");
656 /* Get root window */
657 const xcb_setup_t *setup = xcb_get_setup(conn);
658 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
659 root = iter.data->root;
660 colormap = iter.data->default_colormap;
662 /* Allocate key symbols */
663 if (!(keysyms = xcb_key_symbols_alloc(conn)))
664 error("cannot allocate key symbols");
666 /* Read color information */
667 colors[CLR_FOCUS] = do_alloc_color(0xFF6060);
668 colors[CLR_UNFOCUS] = do_alloc_color(0xD8D8FF);
669 colors[CLR_URGENT] = do_alloc_color(0xFF0000);
671 /* Request substructure redirect */
672 xcb_void_cookie_t cookie;
673 xcb_generic_error_t *err;
674 events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
675 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
676 cookie = xcb_change_window_attributes_checked(conn, root,
677 XCB_CW_EVENT_MASK, &events);
678 if ((err = xcb_request_check(conn, cookie)))
679 error("another window manager is already running");
686 /* Add each initial window */
688 xcb_window_t *kids = NULL;
689 int nkids = do_query_tree(root, &kids);
690 for(int i = 0; i < nkids; i++) {
691 win_t *win = new0(win_t);
692 win->sys = new0(win_sys_t);
693 win->sys->xcb = kids[i];
694 do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
695 do_get_window_attributes(kids[i],
696 &win->sys->override, &win->sys->mapped);
697 tsearch(win, &cache, win_cmp);
698 if (!win->sys->override && win->sys->mapped)
708 xcb_generic_event_t *event;
709 if (!(event = xcb_wait_for_event(conn)))
713 if (!(status = xcb_flush(conn)))
720 printf("sys_exit\n");
722 xcb_disconnect(conn);
728 printf("sys_free\n");
730 xcb_disconnect(conn);