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
56 static xcb_connection_t *conn;
57 static xcb_ewmh_connection_t ewmh;
58 static xcb_key_symbols_t *keysyms;
59 static xcb_colormap_t colormap;
60 static xcb_window_t root;
61 static xcb_event_mask_t events;
62 static list_t *screens;
63 static list_t *struts;
65 static unsigned int grabbed;
67 static xcb_window_t control;
69 static xcb_pixmap_t clr_focus;
70 static xcb_pixmap_t clr_unfocus;
71 static xcb_pixmap_t clr_urgent;
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);
218 static win_t *win_new(xcb_window_t xcb)
220 win_t *win = new0(win_t);
221 win->sys = new0(win_sys_t);
224 win_t **old = tfind(win, &cache, win_cmp);
226 warn("duplicate window for %u\n", xcb);
232 tsearch(win, &cache, win_cmp);
233 printf("win_new: xcb=%-8u -> win=%p\n",
238 static void win_free(win_t *win)
240 printf("win_free: xcb=%-8u -> win=%p\n",
246 static void win_add_strut(win_t *win)
248 win->type = TYPE_TOOLBAR;
249 for (list_t *cur = screens; cur; cur = cur->next) {
250 win_t *screen = cur->data;
251 strut_t *strut = &win->sys->strut;
252 screen->x += strut->left;
253 screen->y += strut->top;
254 screen->w -= strut->left + strut->right;
255 screen->h -= strut->top + strut->bottom;
257 struts = list_insert(struts, win);
261 static void win_del_strut(win_t *win)
263 list_t *link = list_find(struts, win);
266 for (list_t *cur = screens; cur; cur = cur->next) {
267 win_t *screen = cur->data;
268 strut_t *strut = &win->sys->strut;
269 screen->x -= strut->left;
270 screen->y -= strut->top;
271 screen->w += strut->left + strut->right;
272 screen->h += strut->top + strut->bottom;
274 struts = list_remove(struts, link, 0);
281 static void *do_query_tree(xcb_window_t win, xcb_window_t **kids, int *nkids)
283 xcb_query_tree_cookie_t cookie =
284 xcb_query_tree(conn, win);
285 if (!cookie.sequence)
286 return warn("do_query_tree: %d - bad cookie", win), NULL;
288 xcb_query_tree_reply_t *reply =
289 xcb_query_tree_reply(conn, cookie, NULL);
291 return warn("do_query_tree: %d - no reply", win), NULL;
293 *nkids = xcb_query_tree_children_length(reply);
294 *kids = xcb_query_tree_children(reply);
295 printf("do_query_tree: %d - n=%d\n", win, *nkids);
299 static int do_get_geometry(xcb_window_t win,
300 int *x, int *y, int *w, int *h)
302 xcb_get_geometry_cookie_t cookie =
303 xcb_get_geometry(conn, win);
304 if (!cookie.sequence)
305 return warn("do_get_geometry: %d - bad cookie", win);
307 xcb_get_geometry_reply_t *reply =
308 xcb_get_geometry_reply(conn, cookie, NULL);
310 return warn("do_get_geometry: %d - no reply", win);
312 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
313 win, reply->width, reply->height, reply->x, reply->y);
322 static int do_get_window_attributes(xcb_window_t win,
323 int *override, int *mapped)
325 xcb_get_window_attributes_cookie_t cookie =
326 xcb_get_window_attributes(conn, win);
327 if (!cookie.sequence)
328 return warn("do_get_window_attributes: %d - bad cookie", win);
330 xcb_get_window_attributes_reply_t *reply =
331 xcb_get_window_attributes_reply(conn, cookie, NULL);
333 return warn("do_get_window_attributes: %d - no reply ", win);
335 printf("do_get_window_attributes: %d - %d\n",
336 win, reply->override_redirect);
337 *override = reply->override_redirect;
338 *mapped = reply->map_state != XCB_MAP_STATE_UNMAPPED;
343 static int do_xinerama_check(void)
345 const xcb_query_extension_reply_t *data =
346 xcb_get_extension_data(conn, &xcb_xinerama_id);
347 if (!data || !data->present)
348 return warn("do_xinerama_check: no ext");
350 xcb_xinerama_is_active_cookie_t cookie =
351 xcb_xinerama_is_active(conn);
352 if (!cookie.sequence)
353 return warn("do_xinerama_check: no cookie");
355 xcb_xinerama_is_active_reply_t *reply =
356 xcb_xinerama_is_active_reply(conn, cookie, NULL);
358 return warn("do_xinerama_check: no reply");
360 printf("do_xinerama_check: %d\n", reply->state);
361 int state = reply->state;
366 static void *do_query_screens(xcb_xinerama_screen_info_t **info, int *ninfo)
368 xcb_xinerama_query_screens_cookie_t cookie =
369 xcb_xinerama_query_screens(conn);
370 if (!cookie.sequence)
371 return warn("do_query_screens: bad cookie"), NULL;
373 xcb_xinerama_query_screens_reply_t *reply =
374 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
376 return warn("do_query_screens: no reply"), NULL;
378 *ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
379 *info = xcb_xinerama_query_screens_screen_info(reply);
380 printf("do_query_screens: %d screens\n", *ninfo);
384 static int do_get_input_focus(void)
386 xcb_get_input_focus_cookie_t cookie =
387 xcb_get_input_focus(conn);
388 if (!cookie.sequence)
389 return warn("do_get_input_focus: bad cookie");
391 xcb_get_input_focus_reply_t *reply =
392 xcb_get_input_focus_reply(conn, cookie, NULL);
394 return warn("do_get_input_focus: no reply");
396 int focus = reply->focus;
401 static xcb_atom_t do_intern_atom(const char *name)
403 xcb_intern_atom_cookie_t cookie =
404 xcb_intern_atom(conn, 0, strlen(name), name);
405 if (!cookie.sequence)
406 return warn("do_intern_atom: bad cookie");
408 xcb_intern_atom_reply_t *reply =
409 xcb_intern_atom_reply(conn, cookie, NULL);
411 return warn("do_intern_atom: no reply");
413 xcb_atom_t atom = reply->atom;
418 static int do_ewmh_init_atoms(void)
420 xcb_intern_atom_cookie_t *cookies =
421 xcb_ewmh_init_atoms(conn, &ewmh);
423 return warn("do_ewmh_init_atoms: no cookies");
426 xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
428 return warn("do_ewmh_init_atoms: no status");
432 static int do_get_strut(xcb_window_t win, strut_t *strut)
434 xcb_get_property_cookie_t cookie =
435 xcb_ewmh_get_wm_strut(&ewmh, win);
436 if (!cookie.sequence)
437 return warn("do_get_strut: bad cookie");
439 xcb_ewmh_get_extents_reply_t ext = {};
441 xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
443 return warn("do_get_strut: no status");
445 strut->left = ext.left;
446 strut->right = ext.right;
447 strut->top = ext.top;
448 strut->bottom = ext.bottom;
450 return ext.left || ext.right || ext.top || ext.bottom;
453 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
455 uint16_t r = (rgb & 0xFF0000) >> 8;
456 uint16_t g = (rgb & 0x00FF00);
457 uint16_t b = (rgb & 0x0000FF) << 8;
458 xcb_alloc_color_cookie_t cookie =
459 xcb_alloc_color(conn, colormap, r, g, b);
460 if (!cookie.sequence)
461 return warn("do_alloc_color: bad cookie");
463 xcb_alloc_color_reply_t *reply =
464 xcb_alloc_color_reply(conn, cookie, NULL);
466 return warn("do_alloc_color: no reply");
468 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
469 xcb_pixmap_t pixel = reply->pixel;
474 static void do_grab_pointer(xcb_event_mask_t mask)
477 xcb_grab_pointer(conn, 0, root, mask,
480 0, 0, XCB_CURRENT_TIME);
484 static void do_ungrab_pointer(void)
488 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
491 static void do_configure_window(xcb_window_t win,
492 int x, int y, int w, int h,
496 { x, XCB_CONFIG_WINDOW_X },
497 { y, XCB_CONFIG_WINDOW_Y },
498 { w, XCB_CONFIG_WINDOW_WIDTH },
499 { h, XCB_CONFIG_WINDOW_HEIGHT },
500 { b, XCB_CONFIG_WINDOW_BORDER_WIDTH },
501 { s, XCB_CONFIG_WINDOW_SIBLING },
502 { r, XCB_CONFIG_WINDOW_STACK_MODE },
506 uint32_t list[7] = {};
507 for (int i = 0; i < 7; i++) {
508 if (table[i][0] >= 0) {
509 list[i] = table[i][0];
514 xcb_configure_window(conn, win, mask, list);
517 static int do_client_message(xcb_window_t win, xcb_atom_t atom)
520 xcb_get_property_cookie_t cookie =
521 xcb_icccm_get_wm_protocols(conn, win, wm_protos);
522 if (!cookie.sequence)
523 return warn("do_client_message: %d - bad cookie", win);
525 xcb_icccm_get_wm_protocols_reply_t protos = {};
526 if (!xcb_icccm_get_wm_protocols_reply(conn, cookie, &protos, NULL))
527 return warn("do_client_message: %d - no reply", win);
529 /* Search for the atom */
531 for (int i = 0; i < protos.atoms_len; i++)
532 if (protos.atoms[i] == atom)
534 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
536 return warn("do_client_message: %d - no atom", win);
538 /* Send the message */
539 xcb_client_message_event_t msg = {
540 .response_type = XCB_CLIENT_MESSAGE,
544 .data.data32[0] = atom,
545 .data.data32[1] = XCB_CURRENT_TIME,
547 xcb_send_event(conn, 0, win, XCB_EVENT_MASK_NO_EVENT,
552 /**************************
553 * Window Manager Helpers *
554 **************************/
556 /* Send event info */
557 static int send_event(event_t ev, xcb_window_t ewin)
559 win_t *win = win_get(ewin);
561 int status = wm_handle_event(win, ev, MOD(), PTR());
566 /* Send event info */
567 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
568 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
570 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
571 win_t *win = win_get(xcb);
572 mod_t mod = mask_to_mod(mask, up);
573 ptr_t ptr = list_to_ptr(pos);
575 int status = wm_handle_event(win, ev, mod, ptr);
580 /* Send pointer motion info */
581 static int send_pointer(int16_t *pos,
582 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
584 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
585 win_t *win = win_get(xcb);
586 ptr_t ptr = list_to_ptr(pos);
588 int status = wm_handle_ptr(win, ptr);
593 /* Send window state info */
594 static void send_manage(win_t *win, int managed)
596 if (win->sys->managed == managed)
602 win->sys->managed = managed;
605 /* Send window state info */
606 static void send_state(win_t *win, state_t next)
608 if (!win->sys->managed)
610 if (win->state == next)
612 state_t prev = win->state;
614 wm_handle_state(win, prev, next);
617 /**********************
618 * X11 Event Handlers *
619 **********************/
621 /* Specific events */
622 static void on_key_event(xcb_key_press_event_t *event, int up)
624 printf("on_key_event: xcb=%-8u\n", event->event);
625 xcb_window_t focus = do_get_input_focus();
626 event_t ev = keycode_to_event(event->detail);
627 send_event_info(ev, event->state, up, &event->root_x,
628 event->root, focus, event->child);
631 static void on_button_event(xcb_button_press_event_t *event, int up)
633 printf("on_button_event: xcb=%-8u\n", event->event);
634 event_t ev = button_to_event(event->detail);
635 if (!send_event_info(ev, event->state, up, &event->root_x,
636 event->root, event->event, event->child))
637 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
639 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
640 XCB_EVENT_MASK_BUTTON_RELEASE);
646 static void on_motion_notify(xcb_motion_notify_event_t *event)
648 printf("on_motion_notify: xcb=%-8u - %d,%d / %d.%d\n", event->event,
649 event->event_x, event->event_y,
650 event->root_x, event->root_y);
651 send_pointer(&event->root_x, event->root, event->event, event->child);
654 static void on_enter_notify(xcb_enter_notify_event_t *event)
656 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
658 printf("on_enter_notify: xcb=%-8u\n", event->event);
659 send_event_info(EV_ENTER, event->state, 0, &event->root_x,
660 event->root, event->event, event->child);
663 static void on_leave_notify(xcb_leave_notify_event_t *event)
665 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
667 printf("on_leave_notify: xcb=%-8u\n", event->event);
668 send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
669 event->root, event->event, event->child);
672 static void on_focus_in(xcb_focus_in_event_t *event)
674 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
675 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
677 printf("on_focus_in: xcb=%-8u mode=%d\n", event->event, event->mode);
678 xcb_change_window_attributes(conn, event->event,
679 XCB_CW_BORDER_PIXEL, &clr_focus);
680 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
681 send_event(EV_FOCUS, event->event);
684 static void on_focus_out(xcb_focus_out_event_t *event)
686 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
687 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
689 printf("on_focus_out: xcb=%-8u mode=%d\n", event->event, event->mode);
690 xcb_change_window_attributes(conn, event->event,
691 XCB_CW_BORDER_PIXEL, &clr_unfocus);
692 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
693 send_event(EV_UNFOCUS, event->event);
696 static void on_create_notify(xcb_create_notify_event_t *event)
698 printf("on_create_notify: xcb=%-8u\n", event->window);
700 win_t *win = win_new(event->window);
704 win->w = event->width;
705 win->h = event->height;
707 if (!event->override_redirect)
711 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
713 win_t *win = win_get(event->window);
714 printf("on_destroy_notify: xcb=%-8u -> win=%p\n",
719 tdelete(win, &cache, win_cmp);
723 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
725 win_t *win = win_get(event->window);
726 printf("on_unmap_notify: xcb=%-8u -> win=%p\n",
731 send_state(win, ST_HIDE);
734 static void on_map_notify(xcb_map_notify_event_t *event)
736 win_t *win = win_get(event->window);
737 printf("on_map_notify: xcb=%-8u -> win=%p\n",
741 send_state(win, ST_SHOW);
744 static void on_map_request(xcb_map_request_event_t *event)
746 win_t *win = win_get(event->window);
747 printf("on_map_request: xcb=%-8u -> win=%p\n",
751 if (do_get_strut(win->sys->xcb, &win->sys->strut))
753 send_state(win, ST_SHOW);
754 xcb_map_window(conn, win->sys->xcb);
755 sys_move(win, win->x, win->y, win->w, win->h);
758 static void on_configure_request(xcb_configure_request_event_t *event)
760 win_t *win = win_get(event->window);
761 printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
763 event->width, event->height,
767 if (!win->sys->managed) {
770 win->w = event->width;
771 win->h = event->height;
774 xcb_configure_notify_event_t resp = {
775 .response_type = XCB_CONFIGURE_NOTIFY,
776 .event = win->sys->xcb,
777 .window = win->sys->xcb,
782 .border_width = border,
785 xcb_send_event(conn, 0, win->sys->xcb,
786 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
787 (const char *)&resp);
790 static void on_client_message(xcb_client_message_event_t *event)
792 printf("on_client_message: xcb=%-8u\n", event->window);
793 if (event->window == control &&
794 event->type == wm_protos &&
795 event->data.data32[0] == wm_delete)
800 static void on_event(xcb_generic_event_t *event)
802 int type = XCB_EVENT_RESPONSE_TYPE(event);
807 on_key_event((xcb_key_press_event_t *)event, 0);
809 case XCB_KEY_RELEASE:
810 on_key_event((xcb_key_release_event_t *)event, 1);
812 case XCB_BUTTON_PRESS:
813 on_button_event((xcb_button_press_event_t *)event, 0);
815 case XCB_BUTTON_RELEASE:
816 on_button_event((xcb_button_release_event_t *)event, 1);
818 case XCB_MOTION_NOTIFY:
819 on_motion_notify((xcb_motion_notify_event_t *)event);
821 case XCB_ENTER_NOTIFY:
822 on_enter_notify((xcb_enter_notify_event_t *)event);
824 case XCB_LEAVE_NOTIFY:
825 on_leave_notify((xcb_leave_notify_event_t *)event);
828 on_focus_in((xcb_focus_in_event_t *)event);
831 on_focus_out((xcb_focus_out_event_t *)event);
834 /* Window management */
835 case XCB_CREATE_NOTIFY:
836 on_create_notify((xcb_create_notify_event_t *)event);
838 case XCB_DESTROY_NOTIFY:
839 on_destroy_notify((xcb_destroy_notify_event_t *)event);
841 case XCB_UNMAP_NOTIFY:
842 on_unmap_notify((xcb_unmap_notify_event_t *)event);
845 on_map_notify((xcb_map_notify_event_t *)event);
847 case XCB_MAP_REQUEST:
848 on_map_request((xcb_map_request_event_t *)event);
850 case XCB_CONFIGURE_REQUEST:
851 on_configure_request((xcb_configure_request_event_t *)event);
853 case XCB_CLIENT_MESSAGE:
854 on_client_message((xcb_client_message_event_t *)event);
859 printf("on_event: %d:%02X -> %s\n",
860 XCB_EVENT_SENT(event) != 0,
861 XCB_EVENT_RESPONSE_TYPE(event),
862 xcb_event_get_label(type) ?: "unknown_event");
867 /********************
869 ********************/
871 void sys_move(win_t *win, int x, int y, int w, int h)
873 printf("sys_move: %p - %dx%d @ %d,%d\n",
885 do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
888 void sys_raise(win_t *win)
890 printf("sys_raise: %p\n", win);
892 uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
893 uint32_t list = XCB_STACK_MODE_ABOVE;
895 xcb_configure_window(conn, win->sys->xcb, mask, &list);
898 void sys_focus(win_t *win)
900 printf("sys_focus: %p\n", win);
901 xcb_window_t xcb = win ? win->sys->xcb : root;
903 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
904 xcb, XCB_CURRENT_TIME);
907 void sys_show(win_t *win, state_t state)
909 printf("sys_show: %p - %s -> %s\n", win,
910 state_map[win->state], state_map[state]);
911 xcb_window_t xcb = win ? win->sys->xcb : root;
914 win_t *screen = NULL;
915 if (state == ST_FULL || state == ST_MAX) {
916 for (list_t *cur = screens; cur; cur = cur->next) {
918 if (win->x >= screen->x && win->x <= screen->x+screen->w &&
919 win->y >= screen->y && win->y <= screen->y+screen->h)
924 /* Change window state */
927 xcb_unmap_window(conn, xcb);
931 xcb_map_window(conn, xcb);
932 do_configure_window(xcb, win->x, win->y,
933 MAX(win->w - 2*border, 1),
934 MAX(win->h - 2*border, 1),
939 xcb_map_window(conn, xcb);
940 do_configure_window(xcb, screen->x, screen->y, screen->w, screen->h,
941 0, -1, XCB_STACK_MODE_ABOVE);
942 xcb_circulate_window(conn, XCB_CIRCULATE_RAISE_LOWEST, xcb);
946 xcb_map_window(conn, xcb);
947 do_configure_window(xcb, screen->x, screen->y,
948 MAX(screen->w - 2*border, 1),
949 MAX(screen->h - 2*border, 1),
950 border, -1, XCB_STACK_MODE_ABOVE);
954 xcb_map_window(conn, xcb);
955 do_configure_window(xcb, -1, -1, -1, stack,
960 xcb_map_window(conn, xcb);
961 do_configure_window(xcb, -1, -1, 100, 100,
966 if (!do_client_message(xcb, wm_delete))
967 xcb_kill_client(conn, xcb);
975 void sys_watch(win_t *win, event_t ev, mod_t mod)
977 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
978 xcb_window_t xcb = win ? win->sys->xcb : root;
979 xcb_event_mask_t *mask = win ? &win->sys->events : &events;
980 xcb_mod_mask_t mods = 0;
981 xcb_button_t btn = 0;
982 xcb_keycode_t *code = 0;
986 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
987 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
991 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
992 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
997 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
998 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
1001 case EV_MOUSE0...EV_MOUSE7:
1002 btn = event_to_button(ev);
1003 mods = mod_to_mask(mod);
1004 *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
1005 : XCB_EVENT_MASK_BUTTON_PRESS;
1006 xcb_grab_button(conn, 0, xcb, *mask,
1007 XCB_GRAB_MODE_ASYNC,
1008 XCB_GRAB_MODE_ASYNC,
1013 code = event_to_keycodes(ev);
1014 mods = mod_to_mask(mod);
1015 for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
1016 xcb_grab_key(conn, 1, xcb, mods, code[i],
1017 XCB_GRAB_MODE_ASYNC,
1018 XCB_GRAB_MODE_ASYNC);
1024 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
1026 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
1029 list_t *sys_info(void)
1031 printf("sys_info\n");
1033 if (screens == NULL && do_xinerama_check()) {
1034 /* Add Xinerama screens */
1036 xcb_xinerama_screen_info_t *info = NULL;
1037 void *reply = do_query_screens(&info, &ninfo);
1038 for (int i = 0; i < ninfo; i++) {
1039 win_t *screen = new0(win_t);
1041 screen->x = info[i].x_org;
1042 screen->y = info[i].y_org;
1043 screen->w = info[i].width;
1044 screen->h = info[i].height;
1046 screens = list_insert(NULL, screen);
1048 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
1049 screen->w, screen->h,
1050 screen->x, screen->y);
1055 if (screens == NULL) {
1056 /* No xinerama support */
1057 const xcb_setup_t *setup = xcb_get_setup(conn);
1058 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
1060 win_t *screen = new0(win_t);
1062 screen->w = geom->width_in_pixels;
1063 screen->h = geom->height_in_pixels;
1065 screens = list_insert(NULL, screen);
1067 printf("sys_info: root screen - %dx%d\n",
1068 screen->w, screen->h);
1076 printf("sys_init\n");
1078 xcb_void_cookie_t cookie;
1079 xcb_generic_error_t *err;
1081 /* Load configuration */
1082 stack = conf_get_int("main.stack", stack);
1083 border = conf_get_int("main.border", border);
1084 no_capture = conf_get_int("main.no-capture", no_capture);
1086 /* Connect to display */
1087 if (!(conn = xcb_connect(NULL, NULL)))
1088 error("xcb connect failed");
1089 if (xcb_connection_has_error(conn))
1090 error("xcb connection has errors");
1092 /* Get root window */
1093 const xcb_setup_t *setup = xcb_get_setup(conn);
1094 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
1095 root = iter.data->root;
1096 colormap = iter.data->default_colormap;
1098 /* Request substructure redirect */
1099 events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1100 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
1101 cookie = xcb_change_window_attributes_checked(conn, root,
1102 XCB_CW_EVENT_MASK, &events);
1103 if ((err = xcb_request_check(conn, cookie)))
1104 error("another window manager is already running");
1107 wm_protos = do_intern_atom("WM_PROTOCOLS");
1108 wm_delete = do_intern_atom("WM_DELETE_WINDOW");
1109 if (!wm_protos || !wm_delete)
1110 error("unable to setup atoms");
1112 /* Setup EWMH connection */
1113 if (!do_ewmh_init_atoms())
1114 error("ewmh setup failed");
1116 /* Set EWMH wm window */
1117 uint32_t override = 1;
1118 control = xcb_generate_id(conn);
1119 printf("control window: %d\n", control);
1120 cookie = xcb_create_window_checked(conn, 0, control, root,
1121 0, 0, 1, 1, 0, 0, 0,
1122 XCB_CW_OVERRIDE_REDIRECT, &override);
1123 if ((err = xcb_request_check(conn, cookie)))
1124 error("can't create control window");
1125 cookie = xcb_ewmh_set_wm_name_checked(&ewmh, control, 5, "wmpus");
1126 if ((err = xcb_request_check(conn, cookie)))
1127 error("can't set wm name");
1128 cookie = xcb_ewmh_set_supporting_wm_check_checked(&ewmh, root, control);
1129 if ((err = xcb_request_check(conn, cookie)))
1130 error("can't set control window");
1132 /* Setup for for ST_CLOSE */
1133 xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
1135 /* Allocate key symbols */
1136 if (!(keysyms = xcb_key_symbols_alloc(conn)))
1137 error("cannot allocate key symbols");
1139 /* Read color information */
1140 clr_focus = do_alloc_color(0xFF6060);
1141 clr_unfocus = do_alloc_color(0xD8D8FF);
1142 clr_urgent = do_alloc_color(0xFF0000);
1147 printf("sys_run\n");
1149 /* Add each initial window */
1152 xcb_window_t *kids = NULL;
1153 void *reply = do_query_tree(root, &kids, &nkids);
1154 for(int i = 0; i < nkids; i++) {
1155 int override=0, mapped=0;
1156 if (kids[i] == control)
1158 win_t *win = win_new(kids[i]);
1159 if (do_get_strut(win->sys->xcb, &win->sys->strut))
1161 do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
1162 do_get_window_attributes(kids[i], &override, &mapped);
1163 printf(" found %-8u %dx%d @ %d,%d --%s%s\n", kids[i],
1164 win->w, win->h, win->x, win->y,
1165 override ? " override" : "",
1166 mapped ? " mapped" : "");
1168 send_manage(win, 1);
1170 send_state(win, ST_SHOW);
1181 xcb_generic_event_t *event;
1182 if (!(event = xcb_wait_for_event(conn)))
1186 if (!(status = xcb_flush(conn)))
1193 printf("sys_exit\n");
1195 xcb_client_message_event_t msg = {
1196 .response_type = XCB_CLIENT_MESSAGE,
1200 .data.data32[0] = wm_delete,
1201 .data.data32[1] = XCB_CURRENT_TIME,
1203 xcb_send_event(conn, 0, control, XCB_EVENT_MASK_NO_EVENT,
1204 (const char *)&msg);
1210 printf("sys_free\n");
1212 xcb_void_cookie_t cookie;
1213 xcb_generic_error_t *err;
1216 cookie = xcb_delete_property_checked(conn, root,
1217 ewmh._NET_SUPPORTING_WM_CHECK);
1218 if ((err = xcb_request_check(conn, cookie)))
1219 warn("can't remove control window");
1221 cookie = xcb_destroy_window_checked(conn, control);
1222 if ((err = xcb_request_check(conn, cookie)))
1223 warn("can't destroy control window");
1225 /* close connection */
1226 xcb_ewmh_connection_wipe(&ewmh);
1227 xcb_key_symbols_free(keysyms);
1228 xcb_disconnect(conn);
1230 /* free local data */
1232 screens = list_remove(screens, screens, 1);
1233 tdestroy(cache, (void(*)(void*))win_free);