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;
74 static xcb_window_t control;
75 static xcb_atom_t wm_protos;
76 static xcb_atom_t wm_delete;
78 /************************
79 * Conversion functions *
80 ************************/
83 static char *state_map[] = {
104 { EV_PAGEUP, 0xFF55 },
105 { EV_PAGEDOWN, 0xFF56 },
120 /************************
121 * Conversion functions *
122 ************************/
124 static xcb_keycode_t *event_to_keycodes(event_t ev)
126 xcb_keycode_t *codes = NULL;
129 xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
132 if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
133 warn("no keycode found for %d->%d", ev, keysym);
138 static event_t keycode_to_event(xcb_keycode_t code)
141 xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
144 return map_get(keysym_map, sym,keysym, ev,keysym);
148 static event_t button_to_event(xcb_button_t btn)
150 return EV_MOUSE0 + btn;
153 static xcb_button_t event_to_button(event_t ev)
155 return ev - EV_MOUSE0;
159 static xcb_mod_mask_t mod_to_mask(mod_t mod)
161 xcb_mod_mask_t mask = 0;
162 if (mod.alt) mask |= XCB_MOD_MASK_1;
163 if (mod.ctrl) mask |= XCB_MOD_MASK_CONTROL;
164 if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
165 if (mod.win) mask |= XCB_MOD_MASK_4;
169 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
171 mod_t mod = { .up = up };
172 if (mask & XCB_MOD_MASK_1) mod.alt = 1;
173 if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl = 1;
174 if (mask & XCB_MOD_MASK_SHIFT) mod.shift = 1;
175 if (mask & XCB_MOD_MASK_4) mod.win = 1;
180 static ptr_t list_to_ptr(int16_t *list)
184 ptr.rx = list[0]; // root_x
185 ptr.ry = list[1]; // root_y
186 ptr.x = list[2]; // event_x
187 ptr.y = list[3]; // event_y
192 /********************
194 ********************/
196 static int win_cmp(const void *_a, const void *_b)
200 if (a->sys->xcb < b->sys->xcb) return -1;
201 if (a->sys->xcb > b->sys->xcb) return 1;
205 static win_t *win_get(xcb_window_t xcb)
207 win_sys_t sys = { .xcb = xcb };
208 win_t key = { .sys = &sys };
210 win_t **win = tfind(&key, &cache, win_cmp);
213 warn("no window for %u", xcb);
220 static win_t *win_new(xcb_window_t xcb)
222 win_t *win = new0(win_t);
223 win->sys = new0(win_sys_t);
226 win_t **old = tfind(win, &cache, win_cmp);
228 warn("duplicate window for %u\n", xcb);
234 tsearch(win, &cache, win_cmp);
235 printf("win_new: xcb=%-8u -> win=%p\n",
240 static void win_free(win_t *win)
242 printf("win_free: xcb=%-8u -> win=%p\n",
252 static void *do_query_tree(xcb_window_t win, xcb_window_t **kids, int *nkids)
254 xcb_query_tree_cookie_t cookie =
255 xcb_query_tree(conn, win);
256 if (!cookie.sequence)
257 return warn("do_query_tree: %d - bad cookie", win), NULL;
259 xcb_query_tree_reply_t *reply =
260 xcb_query_tree_reply(conn, cookie, NULL);
262 return warn("do_query_tree: %d - no reply", win), NULL;
264 *nkids = xcb_query_tree_children_length(reply);
265 *kids = xcb_query_tree_children(reply);
266 printf("do_query_tree: %d - n=%d\n", win, *nkids);
270 static int do_get_geometry(xcb_window_t win,
271 int *x, int *y, int *w, int *h)
273 xcb_get_geometry_cookie_t cookie =
274 xcb_get_geometry(conn, win);
275 if (!cookie.sequence)
276 return warn("do_get_geometry: %d - bad cookie", win);
278 xcb_get_geometry_reply_t *reply =
279 xcb_get_geometry_reply(conn, cookie, NULL);
281 return warn("do_get_geometry: %d - no reply", win);
283 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
284 win, reply->width, reply->height, reply->x, reply->y);
293 static int do_get_window_attributes(xcb_window_t win,
294 int *override, int *mapped)
296 xcb_get_window_attributes_cookie_t cookie =
297 xcb_get_window_attributes(conn, win);
298 if (!cookie.sequence)
299 return warn("do_get_window_attributes: %d - bad cookie", win);
301 xcb_get_window_attributes_reply_t *reply =
302 xcb_get_window_attributes_reply(conn, cookie, NULL);
304 return warn("do_get_window_attributes: %d - no reply ", win);
306 printf("do_get_window_attributes: %d - %d\n",
307 win, reply->override_redirect);
308 *override = reply->override_redirect;
309 *mapped = reply->map_state != XCB_MAP_STATE_UNMAPPED;
314 static int do_xinerama_check(void)
316 const xcb_query_extension_reply_t *data =
317 xcb_get_extension_data(conn, &xcb_xinerama_id);
318 if (!data || !data->present)
319 return warn("do_xinerama_check: no ext");
321 xcb_xinerama_is_active_cookie_t cookie =
322 xcb_xinerama_is_active(conn);
323 if (!cookie.sequence)
324 return warn("do_xinerama_check: no cookie");
326 xcb_xinerama_is_active_reply_t *reply =
327 xcb_xinerama_is_active_reply(conn, cookie, NULL);
329 return warn("do_xinerama_check: no reply");
331 printf("do_xinerama_check: %d\n", reply->state);
332 int state = reply->state;
337 static void *do_query_screens(xcb_xinerama_screen_info_t **info, int *ninfo)
339 xcb_xinerama_query_screens_cookie_t cookie =
340 xcb_xinerama_query_screens(conn);
341 if (!cookie.sequence)
342 return warn("do_query_screens: bad cookie"), NULL;
344 xcb_xinerama_query_screens_reply_t *reply =
345 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
347 return warn("do_query_screens: no reply"), NULL;
349 *ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
350 *info = xcb_xinerama_query_screens_screen_info(reply);
351 printf("do_query_screens: %d screens\n", *ninfo);
355 static int do_get_input_focus(void)
357 xcb_get_input_focus_cookie_t cookie =
358 xcb_get_input_focus(conn);
359 if (!cookie.sequence)
360 return warn("do_get_input_focus: bad cookie");
362 xcb_get_input_focus_reply_t *reply =
363 xcb_get_input_focus_reply(conn, cookie, NULL);
365 return warn("do_get_input_focus: no reply");
367 int focus = reply->focus;
372 static xcb_atom_t do_intern_atom(const char *name)
374 xcb_intern_atom_cookie_t cookie =
375 xcb_intern_atom(conn, 0, strlen(name), name);
376 if (!cookie.sequence)
377 return warn("do_intern_atom: bad cookie");
379 xcb_intern_atom_reply_t *reply =
380 xcb_intern_atom_reply(conn, cookie, NULL);
382 return warn("do_intern_atom: no reply");
384 xcb_atom_t atom = reply->atom;
389 static int do_ewmh_init_atoms(void)
391 xcb_intern_atom_cookie_t *cookies =
392 xcb_ewmh_init_atoms(conn, &ewmh);
394 return warn("do_ewmh_init_atoms: no cookies");
397 xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
399 return warn("do_ewmh_init_atoms: no status");
403 static int do_get_strut(xcb_window_t win, strut_t *strut)
405 xcb_get_property_cookie_t cookie =
406 xcb_ewmh_get_wm_strut(&ewmh, win);
407 if (!cookie.sequence)
408 return warn("do_get_strut: bad cookie");
410 xcb_ewmh_get_extents_reply_t ext = {};
412 xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
414 return warn("do_get_strut: no status");
416 strut->left = ext.left;
417 strut->right = ext.right;
418 strut->top = ext.top;
419 strut->bottom = ext.bottom;
421 return ext.left || ext.right || ext.top || ext.bottom;
424 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
426 uint16_t r = (rgb & 0xFF0000) >> 8;
427 uint16_t g = (rgb & 0x00FF00);
428 uint16_t b = (rgb & 0x0000FF) << 8;
429 xcb_alloc_color_cookie_t cookie =
430 xcb_alloc_color(conn, colormap, r, g, b);
431 if (!cookie.sequence)
432 return warn("do_alloc_color: bad cookie");
434 xcb_alloc_color_reply_t *reply =
435 xcb_alloc_color_reply(conn, cookie, NULL);
437 return warn("do_alloc_color: no reply");
439 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
440 xcb_pixmap_t pixel = reply->pixel;
445 static void do_grab_pointer(xcb_event_mask_t mask)
448 xcb_grab_pointer(conn, 0, root, mask,
451 0, 0, XCB_CURRENT_TIME);
455 static void do_ungrab_pointer(void)
459 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
462 static void do_configure_window(xcb_window_t win,
463 int x, int y, int w, int h,
467 { x, XCB_CONFIG_WINDOW_X },
468 { y, XCB_CONFIG_WINDOW_Y },
469 { w, XCB_CONFIG_WINDOW_WIDTH },
470 { h, XCB_CONFIG_WINDOW_HEIGHT },
471 { b, XCB_CONFIG_WINDOW_BORDER_WIDTH },
472 { s, XCB_CONFIG_WINDOW_SIBLING },
473 { r, XCB_CONFIG_WINDOW_STACK_MODE },
477 uint32_t list[7] = {};
478 for (int i = 0; i < 7; i++) {
479 if (table[i][0] >= 0) {
480 list[i] = table[i][0];
485 xcb_configure_window(conn, win, mask, list);
488 static int do_client_message(xcb_window_t win, xcb_atom_t atom)
491 xcb_get_property_cookie_t cookie =
492 xcb_icccm_get_wm_protocols(conn, win, wm_protos);
493 if (!cookie.sequence)
494 return warn("do_client_message: %d - bad cookie", win);
496 xcb_icccm_get_wm_protocols_reply_t protos = {};
497 if (!xcb_icccm_get_wm_protocols_reply(conn, cookie, &protos, NULL))
498 return warn("do_client_message: %d - no reply", win);
500 /* Search for the atom */
502 for (int i = 0; i < protos.atoms_len; i++)
503 if (protos.atoms[i] == atom)
505 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
507 return warn("do_client_message: %d - no atom", win);
509 /* Send the message */
510 xcb_client_message_event_t msg = {
511 .response_type = XCB_CLIENT_MESSAGE,
515 .data.data32[0] = atom,
516 .data.data32[1] = XCB_CURRENT_TIME,
518 xcb_send_event(conn, 0, win, XCB_EVENT_MASK_NO_EVENT,
523 /**************************
524 * Window Manager Helpers *
525 **************************/
527 /* Send event info */
528 static int send_event(event_t ev, xcb_window_t ewin)
530 win_t *win = win_get(ewin);
532 int status = wm_handle_event(win, ev, MOD(), PTR());
537 /* Send event info */
538 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
539 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
541 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
542 win_t *win = win_get(xcb);
543 mod_t mod = mask_to_mod(mask, up);
544 ptr_t ptr = list_to_ptr(pos);
546 int status = wm_handle_event(win, ev, mod, ptr);
551 /* Send pointer motion info */
552 static int send_pointer(int16_t *pos,
553 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
555 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
556 win_t *win = win_get(xcb);
557 ptr_t ptr = list_to_ptr(pos);
559 int status = wm_handle_ptr(win, ptr);
564 /* Send window state info */
565 static void send_manage(win_t *win, int managed)
567 if (win->sys->managed == managed)
573 win->sys->managed = managed;
576 /* Send window state info */
577 static void send_state(win_t *win, state_t next)
579 if (!win->sys->managed)
581 if (win->state == next)
583 state_t prev = win->state;
585 wm_handle_state(win, prev, next);
588 /**********************
589 * X11 Event Handlers *
590 **********************/
592 /* Specific events */
593 static void on_key_event(xcb_key_press_event_t *event, int up)
595 printf("on_key_event: xcb=%-8u\n", event->event);
596 xcb_window_t focus = do_get_input_focus();
597 event_t ev = keycode_to_event(event->detail);
598 send_event_info(ev, event->state, up, &event->root_x,
599 event->root, focus, event->child);
602 static void on_button_event(xcb_button_press_event_t *event, int up)
604 printf("on_button_event: xcb=%-8u\n", event->event);
605 event_t ev = button_to_event(event->detail);
606 if (!send_event_info(ev, event->state, up, &event->root_x,
607 event->root, event->event, event->child))
608 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
610 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
611 XCB_EVENT_MASK_BUTTON_RELEASE);
617 static void on_motion_notify(xcb_motion_notify_event_t *event)
619 printf("on_motion_notify: xcb=%-8u - %d,%d / %d.%d\n", event->event,
620 event->event_x, event->event_y,
621 event->root_x, event->root_y);
622 send_pointer(&event->root_x, event->root, event->event, event->child);
625 static void on_enter_notify(xcb_enter_notify_event_t *event)
627 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
629 printf("on_enter_notify: xcb=%-8u\n", event->event);
630 send_event_info(EV_ENTER, event->state, 0, &event->root_x,
631 event->root, event->event, event->child);
634 static void on_leave_notify(xcb_leave_notify_event_t *event)
636 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
638 printf("on_leave_notify: xcb=%-8u\n", event->event);
639 send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
640 event->root, event->event, event->child);
643 static void on_focus_in(xcb_focus_in_event_t *event)
645 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
646 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
648 printf("on_focus_in: xcb=%-8u mode=%d\n", event->event, event->mode);
649 xcb_change_window_attributes(conn, event->event,
650 XCB_CW_BORDER_PIXEL, &colors[CLR_FOCUS]);
651 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
652 send_event(EV_FOCUS, event->event);
655 static void on_focus_out(xcb_focus_out_event_t *event)
657 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
658 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
660 printf("on_focus_out: xcb=%-8u mode=%d\n", event->event, event->mode);
661 xcb_change_window_attributes(conn, event->event,
662 XCB_CW_BORDER_PIXEL, &colors[CLR_UNFOCUS]);
663 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
664 send_event(EV_UNFOCUS, event->event);
667 static void on_create_notify(xcb_create_notify_event_t *event)
669 printf("on_create_notify: xcb=%-8u\n", event->window);
671 win_t *win = win_new(event->window);
675 win->w = event->width;
676 win->h = event->height;
678 if (!event->override_redirect)
682 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
684 win_t *win = win_get(event->window);
685 printf("on_destroy_notify: xcb=%-8u -> win=%p\n",
690 tdelete(win, &cache, win_cmp);
694 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
696 win_t *win = win_get(event->window);
697 printf("on_unmap_notify: xcb=%-8u -> win=%p\n",
701 send_state(win, ST_HIDE);
704 static void on_map_notify(xcb_map_notify_event_t *event)
706 win_t *win = win_get(event->window);
707 printf("on_map_notify: xcb=%-8u -> win=%p\n",
711 send_state(win, ST_SHOW);
714 static void on_map_request(xcb_map_request_event_t *event)
716 win_t *win = win_get(event->window);
717 printf("on_map_request: xcb=%-8u -> win=%p\n",
721 if (do_get_strut(win->sys->xcb, &win->sys->strut))
722 printf("Map: Got a strut!\n");
724 printf("Map: No struts here!\n");
726 send_state(win, ST_SHOW);
727 xcb_map_window(conn, win->sys->xcb);
728 sys_move(win, win->x, win->y, win->w, win->h);
731 static void on_configure_request(xcb_configure_request_event_t *event)
733 win_t *win = win_get(event->window);
734 printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
736 event->width, event->height,
740 if (!win->sys->managed) {
743 win->w = event->width;
744 win->h = event->height;
747 xcb_configure_notify_event_t resp = {
748 .response_type = XCB_CONFIGURE_NOTIFY,
749 .event = win->sys->xcb,
750 .window = win->sys->xcb,
755 .border_width = border,
758 xcb_send_event(conn, 0, win->sys->xcb,
759 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
760 (const char *)&resp);
763 static void on_client_message(xcb_client_message_event_t *event)
765 printf("on_client_message: xcb=%-8u\n", event->window);
766 if (event->window == control &&
767 event->type == wm_protos &&
768 event->data.data32[0] == wm_delete)
773 static void on_event(xcb_generic_event_t *event)
775 int type = XCB_EVENT_RESPONSE_TYPE(event);
780 on_key_event((xcb_key_press_event_t *)event, 0);
782 case XCB_KEY_RELEASE:
783 on_key_event((xcb_key_release_event_t *)event, 1);
785 case XCB_BUTTON_PRESS:
786 on_button_event((xcb_button_press_event_t *)event, 0);
788 case XCB_BUTTON_RELEASE:
789 on_button_event((xcb_button_release_event_t *)event, 1);
791 case XCB_MOTION_NOTIFY:
792 on_motion_notify((xcb_motion_notify_event_t *)event);
794 case XCB_ENTER_NOTIFY:
795 on_enter_notify((xcb_enter_notify_event_t *)event);
797 case XCB_LEAVE_NOTIFY:
798 on_leave_notify((xcb_leave_notify_event_t *)event);
801 on_focus_in((xcb_focus_in_event_t *)event);
804 on_focus_out((xcb_focus_out_event_t *)event);
807 /* Window management */
808 case XCB_CREATE_NOTIFY:
809 on_create_notify((xcb_create_notify_event_t *)event);
811 case XCB_DESTROY_NOTIFY:
812 on_destroy_notify((xcb_destroy_notify_event_t *)event);
814 case XCB_UNMAP_NOTIFY:
815 on_unmap_notify((xcb_unmap_notify_event_t *)event);
818 on_map_notify((xcb_map_notify_event_t *)event);
820 case XCB_MAP_REQUEST:
821 on_map_request((xcb_map_request_event_t *)event);
823 case XCB_CONFIGURE_REQUEST:
824 on_configure_request((xcb_configure_request_event_t *)event);
826 case XCB_CLIENT_MESSAGE:
827 on_client_message((xcb_client_message_event_t *)event);
832 printf("on_event: %d:%02X -> %s\n",
833 XCB_EVENT_SENT(event) != 0,
834 XCB_EVENT_RESPONSE_TYPE(event),
835 xcb_event_get_label(type) ?: "unknown_event");
840 /********************
842 ********************/
844 void sys_move(win_t *win, int x, int y, int w, int h)
846 printf("sys_move: %p - %dx%d @ %d,%d\n",
858 do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
861 void sys_raise(win_t *win)
863 printf("sys_raise: %p\n", win);
865 uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
866 uint32_t list = XCB_STACK_MODE_ABOVE;
868 xcb_configure_window(conn, win->sys->xcb, mask, &list);
871 void sys_focus(win_t *win)
873 printf("sys_focus: %p\n", win);
874 xcb_window_t xcb = win ? win->sys->xcb : root;
876 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
877 xcb, XCB_CURRENT_TIME);
880 void sys_show(win_t *win, state_t state)
882 printf("sys_show: %p - %s -> %s\n", win,
883 state_map[win->state], state_map[state]);
884 xcb_window_t xcb = win ? win->sys->xcb : root;
887 win_t *screen = NULL;
888 if (state == ST_FULL || state == ST_MAX) {
889 for (list_t *cur = screens; cur; cur = cur->next) {
891 if (win->x >= screen->x && win->x <= screen->x+screen->w &&
892 win->y >= screen->y && win->y <= screen->y+screen->h)
897 /* Change window state */
900 xcb_unmap_window(conn, xcb);
904 xcb_map_window(conn, xcb);
905 do_configure_window(xcb, win->x, win->y,
906 MAX(win->w - 2*border, 1),
907 MAX(win->h - 2*border, 1),
912 xcb_map_window(conn, xcb);
913 do_configure_window(xcb, screen->x, screen->y, screen->w, screen->h,
914 0, -1, XCB_STACK_MODE_ABOVE);
915 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, xcb);
919 xcb_map_window(conn, xcb);
920 do_configure_window(xcb, screen->x, screen->y,
921 MAX(screen->w - 2*border, 1),
922 MAX(screen->h - 2*border, 1),
923 border, -1, XCB_STACK_MODE_ABOVE);
927 xcb_map_window(conn, xcb);
928 do_configure_window(xcb, -1, -1, -1, stack,
933 xcb_map_window(conn, xcb);
934 do_configure_window(xcb, -1, -1, 100, 100,
939 if (!do_client_message(xcb, wm_delete))
940 xcb_kill_client(conn, xcb);
948 void sys_watch(win_t *win, event_t ev, mod_t mod)
950 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
951 xcb_window_t xcb = win ? win->sys->xcb : root;
952 xcb_event_mask_t *mask = win ? &win->sys->events : &events;
953 xcb_mod_mask_t mods = 0;
954 xcb_button_t btn = 0;
955 xcb_keycode_t *code = 0;
959 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
960 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
964 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
965 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
970 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
971 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
974 case EV_MOUSE0...EV_MOUSE7:
975 btn = event_to_button(ev);
976 mods = mod_to_mask(mod);
977 *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
978 : XCB_EVENT_MASK_BUTTON_PRESS;
979 xcb_grab_button(conn, 0, xcb, *mask,
986 code = event_to_keycodes(ev);
987 mods = mod_to_mask(mod);
988 for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
989 xcb_grab_key(conn, 1, xcb, mods, code[i],
991 XCB_GRAB_MODE_ASYNC);
997 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
999 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
1002 list_t *sys_info(void)
1004 printf("sys_info\n");
1006 if (screens == NULL && do_xinerama_check()) {
1007 /* Add Xinerama screens */
1009 xcb_xinerama_screen_info_t *info = NULL;
1010 void *reply = do_query_screens(&info, &ninfo);
1011 for (int i = 0; i < ninfo; i++) {
1012 win_t *screen = new0(win_t);
1014 screen->x = info[i].x_org;
1015 screen->y = info[i].y_org;
1016 screen->w = info[i].width;
1017 screen->h = info[i].height;
1019 screens = list_insert(NULL, screen);
1021 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
1022 screen->w, screen->h,
1023 screen->x, screen->y);
1028 if (screens == NULL) {
1029 /* No xinerama support */
1030 const xcb_setup_t *setup = xcb_get_setup(conn);
1031 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
1033 win_t *screen = new0(win_t);
1035 screen->w = geom->width_in_pixels;
1036 screen->h = geom->height_in_pixels;
1038 screens = list_insert(NULL, screen);
1040 printf("sys_info: root screen - %dx%d\n",
1041 screen->w, screen->h);
1049 printf("sys_init\n");
1051 xcb_void_cookie_t cookie;
1052 xcb_generic_error_t *err;
1054 /* Load configuration */
1055 stack = conf_get_int("main.stack", stack);
1056 border = conf_get_int("main.border", border);
1057 no_capture = conf_get_int("main.no-capture", no_capture);
1059 /* Connect to display */
1060 if (!(conn = xcb_connect(NULL, NULL)))
1061 error("xcb connect failed");
1062 if (xcb_connection_has_error(conn))
1063 error("xcb connection has errors");
1065 /* Get root window */
1066 const xcb_setup_t *setup = xcb_get_setup(conn);
1067 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
1068 root = iter.data->root;
1069 colormap = iter.data->default_colormap;
1071 /* Request substructure redirect */
1072 events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1073 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
1074 cookie = xcb_change_window_attributes_checked(conn, root,
1075 XCB_CW_EVENT_MASK, &events);
1076 if ((err = xcb_request_check(conn, cookie)))
1077 error("another window manager is already running");
1080 wm_protos = do_intern_atom("WM_PROTOCOLS");
1081 wm_delete = do_intern_atom("WM_DELETE_WINDOW");
1082 if (!wm_protos || !wm_delete)
1083 error("unable to setup atoms");
1085 /* Setup EWMH connection */
1086 if (!do_ewmh_init_atoms())
1087 error("ewmh setup failed");
1089 /* Set EWMH wm window */
1090 uint32_t override = 1;
1091 control = xcb_generate_id(conn);
1092 printf("control window: %d\n", control);
1093 cookie = xcb_create_window_checked(conn, 0, control, root,
1094 0, 0, 1, 1, 0, 0, 0,
1095 XCB_CW_OVERRIDE_REDIRECT, &override);
1096 if ((err = xcb_request_check(conn, cookie)))
1097 error("can't create control window");
1098 cookie = xcb_ewmh_set_wm_name_checked(&ewmh, control, 5, "wmpus");
1099 if ((err = xcb_request_check(conn, cookie)))
1100 error("can't set wm name");
1101 cookie = xcb_ewmh_set_supporting_wm_check_checked(&ewmh, root, control);
1102 if ((err = xcb_request_check(conn, cookie)))
1103 error("can't set control window");
1105 /* Setup for for ST_CLOSE */
1106 xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
1108 /* Allocate key symbols */
1109 if (!(keysyms = xcb_key_symbols_alloc(conn)))
1110 error("cannot allocate key symbols");
1112 /* Read color information */
1113 colors[CLR_FOCUS] = do_alloc_color(0xFF6060);
1114 colors[CLR_UNFOCUS] = do_alloc_color(0xD8D8FF);
1115 colors[CLR_URGENT] = do_alloc_color(0xFF0000);
1120 printf("sys_run\n");
1122 /* Add each initial window */
1125 xcb_window_t *kids = NULL;
1126 void *reply = do_query_tree(root, &kids, &nkids);
1127 for(int i = 0; i < nkids; i++) {
1128 int override=0, mapped=0;
1129 if (kids[i] == control)
1131 win_t *win = win_new(kids[i]);
1132 do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
1133 do_get_window_attributes(kids[i], &override, &mapped);
1134 printf(" found %-8u %dx%d @ %d,%d --%s%s\n", kids[i],
1135 win->w, win->h, win->x, win->y,
1136 override ? " override" : "",
1137 mapped ? " mapped" : "");
1139 send_manage(win, 1);
1141 send_state(win, ST_SHOW);
1152 xcb_generic_event_t *event;
1153 if (!(event = xcb_wait_for_event(conn)))
1157 if (!(status = xcb_flush(conn)))
1164 printf("sys_exit\n");
1166 xcb_client_message_event_t msg = {
1167 .response_type = XCB_CLIENT_MESSAGE,
1171 .data.data32[0] = wm_delete,
1172 .data.data32[1] = XCB_CURRENT_TIME,
1174 xcb_send_event(conn, 0, control, XCB_EVENT_MASK_NO_EVENT,
1175 (const char *)&msg);
1181 printf("sys_free\n");
1183 xcb_void_cookie_t cookie;
1184 xcb_generic_error_t *err;
1187 cookie = xcb_delete_property_checked(conn, root,
1188 ewmh._NET_SUPPORTING_WM_CHECK);
1189 if ((err = xcb_request_check(conn, cookie)))
1190 warn("can't remove control window");
1192 cookie = xcb_destroy_window_checked(conn, control);
1193 if ((err = xcb_request_check(conn, cookie)))
1194 warn("can't destroy control window");
1196 /* close connection */
1197 xcb_ewmh_connection_wipe(&ewmh);
1198 xcb_key_symbols_free(keysyms);
1199 xcb_disconnect(conn);
1201 /* free local data */
1203 screens = list_remove(screens, screens, 1);
1204 tdestroy(cache, (void(*)(void*))win_free);