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 state_t state; // window state if not mapped
53 int mapped; // window is managed by wm
54 int managed; // window is managed by wm
58 static xcb_connection_t *conn;
59 static xcb_ewmh_connection_t ewmh;
60 static xcb_key_symbols_t *keysyms;
61 static xcb_colormap_t colormap;
62 static xcb_window_t root;
63 static xcb_event_mask_t events;
64 static list_t *screens;
65 static list_t *struts;
67 static unsigned int grabbed;
69 static xcb_window_t control;
71 static xcb_pixmap_t clr_focus;
72 static xcb_pixmap_t clr_unfocus;
73 static xcb_pixmap_t clr_urgent;
75 static xcb_atom_t wm_protos;
76 static xcb_atom_t wm_delete;
77 static xcb_atom_t wm_nhints;
79 /************************
80 * Conversion functions *
81 ************************/
84 static char *state_map[] = {
100 { EV_RIGHT, 0xFF53 },
105 { EV_PAGEUP, 0xFF55 },
106 { EV_PAGEDOWN, 0xFF56 },
121 /************************
122 * Conversion functions *
123 ************************/
125 static xcb_keycode_t *event_to_keycodes(event_t ev)
127 xcb_keycode_t *codes = NULL;
130 xcb_keysym_t keysym = map_get(keysym_map, ev, ev, sym, ev);
133 if (!(codes = xcb_key_symbols_get_keycode(keysyms, keysym)))
134 warn("no keycode found for %d->%d", ev, keysym);
139 static event_t keycode_to_event(xcb_keycode_t code)
142 xcb_keysym_t keysym = xcb_key_symbols_get_keysym(keysyms, code, 0);
145 return map_get(keysym_map, sym,keysym, ev,keysym);
149 static event_t button_to_event(xcb_button_t btn)
151 return EV_MOUSE0 + btn;
154 static xcb_button_t event_to_button(event_t ev)
156 return ev - EV_MOUSE0;
160 static xcb_mod_mask_t mod_to_mask(mod_t mod)
162 xcb_mod_mask_t mask = 0;
163 if (mod.alt) mask |= XCB_MOD_MASK_1;
164 if (mod.ctrl) mask |= XCB_MOD_MASK_CONTROL;
165 if (mod.shift) mask |= XCB_MOD_MASK_SHIFT;
166 if (mod.win) mask |= XCB_MOD_MASK_4;
170 static mod_t mask_to_mod(xcb_mod_mask_t mask, int up)
172 mod_t mod = { .up = up };
173 if (mask & XCB_MOD_MASK_1) mod.alt = 1;
174 if (mask & XCB_MOD_MASK_CONTROL) mod.ctrl = 1;
175 if (mask & XCB_MOD_MASK_SHIFT) mod.shift = 1;
176 if (mask & XCB_MOD_MASK_4) mod.win = 1;
181 static ptr_t list_to_ptr(int16_t *list)
185 ptr.rx = list[0]; // root_x
186 ptr.ry = list[1]; // root_y
187 ptr.x = list[2]; // event_x
188 ptr.y = list[3]; // event_y
193 /********************
195 ********************/
197 static int win_cmp(const void *_a, const void *_b)
201 if (a->sys->xcb < b->sys->xcb) return -1;
202 if (a->sys->xcb > b->sys->xcb) return 1;
206 static win_t *win_get(xcb_window_t xcb)
208 win_sys_t sys = { .xcb = xcb };
209 win_t key = { .sys = &sys };
211 win_t **win = tfind(&key, &cache, win_cmp);
214 warn("no window for %u", xcb);
221 static win_t *win_new(xcb_window_t xcb)
223 win_t *win = new0(win_t);
224 win->sys = new0(win_sys_t);
227 win_t **old = tfind(win, &cache, win_cmp);
229 warn("duplicate window for %u\n", xcb);
235 tsearch(win, &cache, win_cmp);
236 printf("win_new: xcb=%-8u -> win=%p\n",
241 static void win_free(win_t *win)
243 printf("win_free: xcb=%-8u -> win=%p\n",
249 static void win_add_strut(win_t *win)
251 win->type = TYPE_TOOLBAR;
252 for (list_t *cur = screens; cur; cur = cur->next) {
253 win_t *screen = cur->data;
254 strut_t *strut = &win->sys->strut;
255 screen->x += strut->left;
256 screen->y += strut->top;
257 screen->w -= strut->left + strut->right;
258 screen->h -= strut->top + strut->bottom;
260 struts = list_insert(struts, win);
264 static void win_del_strut(win_t *win)
266 list_t *link = list_find(struts, win);
269 for (list_t *cur = screens; cur; cur = cur->next) {
270 win_t *screen = cur->data;
271 strut_t *strut = &win->sys->strut;
272 screen->x -= strut->left;
273 screen->y -= strut->top;
274 screen->w += strut->left + strut->right;
275 screen->h += strut->top + strut->bottom;
277 struts = list_remove(struts, link, 0);
284 static void *do_query_tree(xcb_window_t xcb, xcb_window_t **kids, int *nkids)
286 xcb_query_tree_cookie_t cookie =
287 xcb_query_tree(conn, xcb);
288 if (!cookie.sequence)
289 return warn("do_query_tree: %d - bad cookie", xcb), NULL;
291 xcb_query_tree_reply_t *reply =
292 xcb_query_tree_reply(conn, cookie, NULL);
294 return warn("do_query_tree: %d - no reply", xcb), NULL;
296 *nkids = xcb_query_tree_children_length(reply);
297 *kids = xcb_query_tree_children(reply);
298 printf("do_query_tree: %d - n=%d\n", xcb, *nkids);
302 static int do_get_geometry(xcb_window_t xcb,
303 int *x, int *y, int *w, int *h)
305 xcb_get_geometry_cookie_t cookie =
306 xcb_get_geometry(conn, xcb);
307 if (!cookie.sequence)
308 return warn("do_get_geometry: %d - bad cookie", xcb);
310 xcb_get_geometry_reply_t *reply =
311 xcb_get_geometry_reply(conn, cookie, NULL);
313 return warn("do_get_geometry: %d - no reply", xcb);
315 printf("do_get_geometry: %d - %dx%d @ %d,%d\n",
316 xcb, reply->width, reply->height, reply->x, reply->y);
325 static int do_get_window_attributes(xcb_window_t xcb,
326 int *override, int *mapped)
328 xcb_get_window_attributes_cookie_t cookie =
329 xcb_get_window_attributes(conn, xcb);
330 if (!cookie.sequence)
331 return warn("do_get_window_attributes: %d - bad cookie", xcb);
333 xcb_get_window_attributes_reply_t *reply =
334 xcb_get_window_attributes_reply(conn, cookie, NULL);
336 return warn("do_get_window_attributes: %d - no reply ", xcb);
338 printf("do_get_window_attributes: %d - %d\n",
339 xcb, reply->override_redirect);
340 *override = reply->override_redirect;
341 *mapped = reply->map_state != XCB_MAP_STATE_UNMAPPED;
346 static int do_xinerama_check(void)
348 const xcb_query_extension_reply_t *data =
349 xcb_get_extension_data(conn, &xcb_xinerama_id);
350 if (!data || !data->present)
351 return warn("do_xinerama_check: no ext");
353 xcb_xinerama_is_active_cookie_t cookie =
354 xcb_xinerama_is_active(conn);
355 if (!cookie.sequence)
356 return warn("do_xinerama_check: no cookie");
358 xcb_xinerama_is_active_reply_t *reply =
359 xcb_xinerama_is_active_reply(conn, cookie, NULL);
361 return warn("do_xinerama_check: no reply");
363 printf("do_xinerama_check: %d\n", reply->state);
364 int state = reply->state;
369 static void *do_query_screens(xcb_xinerama_screen_info_t **info, int *ninfo)
371 xcb_xinerama_query_screens_cookie_t cookie =
372 xcb_xinerama_query_screens(conn);
373 if (!cookie.sequence)
374 return warn("do_query_screens: bad cookie"), NULL;
376 xcb_xinerama_query_screens_reply_t *reply =
377 xcb_xinerama_query_screens_reply(conn, cookie, NULL);
379 return warn("do_query_screens: no reply"), NULL;
381 *ninfo = xcb_xinerama_query_screens_screen_info_length(reply);
382 *info = xcb_xinerama_query_screens_screen_info(reply);
383 printf("do_query_screens: %d screens\n", *ninfo);
387 static int do_get_input_focus(void)
389 xcb_get_input_focus_cookie_t cookie =
390 xcb_get_input_focus(conn);
391 if (!cookie.sequence)
392 return warn("do_get_input_focus: bad cookie");
394 xcb_get_input_focus_reply_t *reply =
395 xcb_get_input_focus_reply(conn, cookie, NULL);
397 return warn("do_get_input_focus: no reply");
399 int focus = reply->focus;
404 static xcb_atom_t do_intern_atom(const char *name)
406 xcb_intern_atom_cookie_t cookie =
407 xcb_intern_atom(conn, 0, strlen(name), name);
408 if (!cookie.sequence)
409 return warn("do_intern_atom: bad cookie");
411 xcb_intern_atom_reply_t *reply =
412 xcb_intern_atom_reply(conn, cookie, NULL);
414 return warn("do_intern_atom: no reply");
416 xcb_atom_t atom = reply->atom;
421 static char *do_get_atom_name(xcb_atom_t atom)
423 xcb_get_atom_name_cookie_t cookie =
424 xcb_get_atom_name(conn, atom);
425 if (!cookie.sequence)
426 return warn("do_get_atom_name: bad cookie"), NULL;
428 xcb_get_atom_name_reply_t *reply =
429 xcb_get_atom_name_reply(conn, cookie, NULL);
431 return warn("do_get_atom_name: no reply"), NULL;
433 char *name = xcb_get_atom_name_name(reply);
434 int len = xcb_get_atom_name_name_length(reply);
435 char *str = strndup(name, len);
440 static int do_ewmh_init_atoms(void)
442 xcb_intern_atom_cookie_t *cookies =
443 xcb_ewmh_init_atoms(conn, &ewmh);
445 return warn("do_ewmh_init_atoms: no cookies");
448 xcb_ewmh_init_atoms_replies(&ewmh, cookies, NULL);
450 return warn("do_ewmh_init_atoms: no status");
454 static int do_get_type(xcb_window_t xcb, type_t *type)
456 xcb_get_property_cookie_t cookie =
457 xcb_ewmh_get_wm_window_type(&ewmh, xcb);
458 if (!cookie.sequence)
459 return warn("do_get_type: bad cookie");
461 xcb_ewmh_get_atoms_reply_t reply;
462 if (!xcb_ewmh_get_wm_window_type_reply(&ewmh, cookie, &reply, NULL))
463 return warn("do_get_type: no reply");
466 for (int i = 0; i < reply.atoms_len; i++) {
467 if (reply.atoms[i] == ewmh._NET_WM_WINDOW_TYPE_DOCK)
468 *type = TYPE_TOOLBAR;
469 if (reply.atoms[i] == ewmh._NET_WM_WINDOW_TYPE_TOOLBAR)
470 *type = TYPE_TOOLBAR;
471 if (reply.atoms[i] == ewmh._NET_WM_WINDOW_TYPE_DIALOG)
474 printf("do_get_type: %d -> %d\n", prev, *type);
478 static int do_get_icccm_state(xcb_window_t xcb, state_t *state)
480 xcb_get_property_cookie_t cookie;
482 cookie = xcb_icccm_get_wm_normal_hints(conn, xcb);
483 if (!cookie.sequence)
484 return warn("do_get_icccm_state: bad cookie1");
486 xcb_size_hints_t sizes;
487 if (!xcb_icccm_get_wm_normal_hints_reply(conn, cookie, &sizes, NULL))
488 return warn("do_get_icccm_state: no sizes");
490 state_t prev = *state;
491 printf("do_get_icccm_state: %s -> %s\n",
492 state_map[prev], state_map[*state]);
496 static int do_get_ewmh_state(xcb_window_t xcb, state_t *state)
498 xcb_get_property_cookie_t cookie;
500 cookie = xcb_ewmh_get_wm_state(&ewmh, xcb);
501 if (!cookie.sequence)
502 return warn("do_get_ewmh_state: bad cookie2");
504 xcb_ewmh_get_atoms_reply_t states;
505 if (!xcb_ewmh_get_wm_state_reply(&ewmh, cookie, &states, NULL))
506 return warn("do_get_ewmh_state: no reply2");
508 state_t prev = *state;
509 for (int i = 0; i < states.atoms_len; i++)
510 if (states.atoms[i] == ewmh._NET_WM_STATE_FULLSCREEN)
513 printf("do_get_ewmh_state: %s -> %s\n",
514 state_map[prev], state_map[*state]);
518 static int do_get_strut(xcb_window_t xcb, strut_t *strut)
520 xcb_get_property_cookie_t cookie =
521 xcb_ewmh_get_wm_strut(&ewmh, xcb);
522 if (!cookie.sequence)
523 return warn("do_get_strut: bad cookie");
525 xcb_ewmh_get_extents_reply_t ext = {};
527 xcb_ewmh_get_wm_strut_reply(&ewmh, cookie, &ext, NULL);
529 return warn("do_get_strut: no status");
531 strut->left = ext.left;
532 strut->right = ext.right;
533 strut->top = ext.top;
534 strut->bottom = ext.bottom;
536 return ext.left || ext.right || ext.top || ext.bottom;
539 static xcb_pixmap_t do_alloc_color(uint32_t rgb)
541 uint16_t r = (rgb & 0xFF0000) >> 8;
542 uint16_t g = (rgb & 0x00FF00);
543 uint16_t b = (rgb & 0x0000FF) << 8;
544 xcb_alloc_color_cookie_t cookie =
545 xcb_alloc_color(conn, colormap, r, g, b);
546 if (!cookie.sequence)
547 return warn("do_alloc_color: bad cookie");
549 xcb_alloc_color_reply_t *reply =
550 xcb_alloc_color_reply(conn, cookie, NULL);
552 return warn("do_alloc_color: no reply");
554 printf("do_alloc_color: %06x -> %06x\n", rgb, reply->pixel);
555 xcb_pixmap_t pixel = reply->pixel;
560 static void do_grab_pointer(xcb_event_mask_t mask)
563 xcb_grab_pointer(conn, 0, root, mask,
566 0, 0, XCB_CURRENT_TIME);
570 static void do_ungrab_pointer(void)
574 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
577 static void do_configure_window(xcb_window_t xcb,
578 int x, int y, int w, int h,
582 { x, XCB_CONFIG_WINDOW_X },
583 { y, XCB_CONFIG_WINDOW_Y },
584 { w, XCB_CONFIG_WINDOW_WIDTH },
585 { h, XCB_CONFIG_WINDOW_HEIGHT },
586 { b, XCB_CONFIG_WINDOW_BORDER_WIDTH },
587 { s, XCB_CONFIG_WINDOW_SIBLING },
588 { r, XCB_CONFIG_WINDOW_STACK_MODE },
592 uint32_t list[countof(table)];
593 for (int i=0,j=0; i < countof(table); i++) {
594 if (table[i][0] >= 0) {
595 list[j++] = table[i][0];
600 xcb_configure_window(conn, xcb, mask, list);
603 static int do_client_message(xcb_window_t xcb, xcb_atom_t atom)
606 xcb_get_property_cookie_t cookie =
607 xcb_icccm_get_wm_protocols(conn, xcb, wm_protos);
608 if (!cookie.sequence)
609 return warn("do_client_message: %d - bad cookie", xcb);
611 xcb_icccm_get_wm_protocols_reply_t protos = {};
612 if (!xcb_icccm_get_wm_protocols_reply(conn, cookie, &protos, NULL))
613 return warn("do_client_message: %d - no reply", xcb);
615 /* Search for the atom */
617 for (int i = 0; i < protos.atoms_len; i++)
618 if (protos.atoms[i] == atom)
620 xcb_icccm_get_wm_protocols_reply_wipe(&protos);
622 return warn("do_client_message: %d - no atom", xcb);
624 /* Send the message */
625 xcb_client_message_event_t msg = {
626 .response_type = XCB_CLIENT_MESSAGE,
630 .data.data32[0] = atom,
631 .data.data32[1] = XCB_CURRENT_TIME,
633 xcb_send_event(conn, 0, xcb, XCB_EVENT_MASK_NO_EVENT,
638 /**************************
639 * Window Manager Helpers *
640 **************************/
642 /* Send event info */
643 static int send_event(event_t ev, xcb_window_t ewin)
645 win_t *win = win_get(ewin);
647 int status = wm_handle_event(win, ev, MOD(), PTR());
652 /* Send event info */
653 static int send_event_info(event_t ev, xcb_mod_mask_t mask, int up, int16_t *pos,
654 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
656 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
657 win_t *win = win_get(xcb);
658 mod_t mod = mask_to_mod(mask, up);
659 ptr_t ptr = list_to_ptr(pos);
661 int status = wm_handle_event(win, ev, mod, ptr);
666 /* Send pointer motion info */
667 static int send_pointer(int16_t *pos,
668 xcb_window_t rwin, xcb_window_t ewin, xcb_window_t cwin)
670 xcb_window_t xcb = ewin == rwin ? cwin : ewin;
671 win_t *win = win_get(xcb);
672 ptr_t ptr = list_to_ptr(pos);
674 int status = wm_handle_ptr(win, ptr);
679 /* Send window state info */
680 static void send_manage(win_t *win, int managed)
682 if (win->sys->managed == managed)
688 win->sys->managed = managed;
691 /* Send window state info */
692 static void send_state(win_t *win, state_t next)
694 if (!win->sys->managed)
696 if (!win->sys->mapped)
698 if (win->state == next)
700 state_t prev = win->state;
702 wm_handle_state(win, prev, next);
705 /**********************
706 * X11 Event Handlers *
707 **********************/
709 /* Specific events */
710 static void on_key_event(xcb_key_press_event_t *event, int up)
712 printf("on_key_event: xcb=%-8u\n", event->event);
713 xcb_window_t focus = do_get_input_focus();
714 event_t ev = keycode_to_event(event->detail);
715 send_event_info(ev, event->state, up, &event->root_x,
716 event->root, focus, event->child);
719 static void on_button_event(xcb_button_press_event_t *event, int up)
721 printf("on_button_event: xcb=%-8u\n", event->event);
722 event_t ev = button_to_event(event->detail);
723 if (!send_event_info(ev, event->state, up, &event->root_x,
724 event->root, event->event, event->child))
725 xcb_allow_events(conn, XCB_ALLOW_REPLAY_POINTER, event->time);
727 do_grab_pointer(XCB_EVENT_MASK_POINTER_MOTION |
728 XCB_EVENT_MASK_BUTTON_RELEASE);
734 static void on_motion_notify(xcb_motion_notify_event_t *event)
736 printf("on_motion_notify: xcb=%-8u - %d,%d / %d.%d\n", event->event,
737 event->event_x, event->event_y,
738 event->root_x, event->root_y);
739 send_pointer(&event->root_x, event->root, event->event, event->child);
742 static void on_enter_notify(xcb_enter_notify_event_t *event)
744 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
746 printf("on_enter_notify: xcb=%-8u\n", event->event);
747 send_event_info(EV_ENTER, event->state, 0, &event->root_x,
748 event->root, event->event, event->child);
751 static void on_leave_notify(xcb_leave_notify_event_t *event)
753 if (event->mode != XCB_NOTIFY_MODE_NORMAL)
755 printf("on_leave_notify: xcb=%-8u\n", event->event);
756 send_event_info(EV_LEAVE, event->state, 0, &event->root_x,
757 event->root, event->event, event->child);
760 static void on_focus_in(xcb_focus_in_event_t *event)
762 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
763 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
765 printf("on_focus_in: xcb=%-8u mode=%d\n", event->event, event->mode);
766 xcb_change_window_attributes(conn, event->event,
767 XCB_CW_BORDER_PIXEL, &clr_focus);
768 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
769 send_event(EV_FOCUS, event->event);
772 static void on_focus_out(xcb_focus_out_event_t *event)
774 if (event->mode != XCB_NOTIFY_MODE_NORMAL &&
775 event->mode != XCB_NOTIFY_MODE_WHILE_GRABBED)
777 printf("on_focus_out: xcb=%-8u mode=%d\n", event->event, event->mode);
778 xcb_change_window_attributes(conn, event->event,
779 XCB_CW_BORDER_PIXEL, &clr_unfocus);
780 if (event->mode == XCB_NOTIFY_MODE_NORMAL)
781 send_event(EV_UNFOCUS, event->event);
784 static void on_create_notify(xcb_create_notify_event_t *event)
786 printf("on_create_notify: xcb=%-8u\n", event->window);
788 win_t *win = win_new(event->window);
792 win->w = event->width;
793 win->h = event->height;
795 win->sys->state = ST_SHOW;
796 win->sys->events = XCB_EVENT_MASK_PROPERTY_CHANGE;
797 xcb_change_window_attributes(conn, event->window,
798 XCB_CW_EVENT_MASK, &win->sys->events);
800 if (!event->override_redirect)
804 static void on_destroy_notify(xcb_destroy_notify_event_t *event)
806 win_t *win = win_get(event->window);
807 printf("on_destroy_notify: xcb=%-8u -> win=%p\n",
812 tdelete(win, &cache, win_cmp);
816 static void on_unmap_notify(xcb_unmap_notify_event_t *event)
818 win_t *win = win_get(event->window);
819 printf("on_unmap_notify: xcb=%-8u -> win=%p\n",
824 send_state(win, ST_HIDE);
825 win->sys->mapped = 0;
828 static void on_map_notify(xcb_map_notify_event_t *event)
830 win_t *win = win_get(event->window);
831 printf("on_map_notify: xcb=%-8u -> win=%p\n",
835 win->sys->mapped = 1;
836 send_state(win, win->sys->state);
839 static void on_map_request(xcb_map_request_event_t *event)
841 win_t *win = win_get(event->window);
842 printf("on_map_request: xcb=%-8u -> win=%p\n",
846 win->sys->mapped = 1;
847 send_state(win, win->sys->state);
848 xcb_map_window(conn, win->sys->xcb);
849 if (!win->sys->managed)
850 sys_move(win, win->x, win->y, win->w, win->h);
853 static void on_configure_request(xcb_configure_request_event_t *event)
855 win_t *win = win_get(event->window);
856 printf("on_configure_request: xcb=%-8u -> win=%p -- %dx%d @ %d,%d\n",
858 event->width, event->height,
861 printf("on_configure_request: xcb=%-8u -> win=%p << %dx%d @ %d,%d\n",
866 if (!win->sys->managed) {
869 win->w = event->width;
870 win->h = event->height;
873 xcb_configure_notify_event_t resp = {
874 .response_type = XCB_CONFIGURE_NOTIFY,
875 .event = win->sys->xcb,
876 .window = win->sys->xcb,
881 .border_width = border,
884 xcb_send_event(conn, 0, win->sys->xcb,
885 XCB_EVENT_MASK_STRUCTURE_NOTIFY,
886 (const char *)&resp);
889 static void on_property_notify(xcb_property_notify_event_t *event)
891 win_t *win = win_get(event->window);
892 char *name = do_get_atom_name(event->atom);
893 printf("on_property_notify: xcb=%-8u -> win=%p - %s\n",
894 event->window, win, name);
895 if (name) free(name);
898 /* Check window type */
899 if (event->atom == ewmh._NET_WM_WINDOW_TYPE)
900 do_get_type(event->window, &win->type);
903 if (event->atom == ewmh._NET_WM_STRUT)
904 if (do_get_strut(win->sys->xcb, &win->sys->strut))
907 /* Check window state */
908 if (event->atom == wm_nhints)
909 if (do_get_icccm_state(event->window, &win->sys->state))
910 send_state(win, win->sys->state);
911 if (event->atom == ewmh._NET_WM_STATE)
912 if (do_get_ewmh_state(event->window, &win->sys->state))
913 send_state(win, win->sys->state);
916 static void on_client_message(xcb_client_message_event_t *event)
918 win_t *win = win_get(event->window);
919 char *name = do_get_atom_name(event->type);
920 printf("on_client_message: xcb=%-8u -> win=%p - %s=[%d,%d,%d,%d]\n",
921 event->window, win, name,
922 event->data.data32[0], event->data.data32[1],
923 event->data.data32[2], event->data.data32[3]);
924 if (name) free(name);
928 if (event->window == control &&
929 event->type == wm_protos &&
930 event->data.data32[0] == wm_delete) {
931 printf("on_client_message: shutdown request");
936 if (event->type == ewmh._NET_CLOSE_WINDOW) {
937 printf("on_client_message: close request");
938 sys_show(win, ST_CLOSE);
941 /* Fullscreen request */
942 if ((event->type == ewmh._NET_WM_STATE) &&
943 (event->data.data32[1] == ewmh._NET_WM_STATE_FULLSCREEN ||
944 event->data.data32[2] == ewmh._NET_WM_STATE_FULLSCREEN)) {
945 printf("on_client_message: fullscreen request");
946 int full = win->state == ST_FULL;
947 switch (event->data.data32[0]) {
948 case XCB_EWMH_WM_STATE_REMOVE: full = 0; break;
949 case XCB_EWMH_WM_STATE_ADD: full = 1; break;
950 case XCB_EWMH_WM_STATE_TOGGLE: full ^= 1; break;
952 win->sys->state = full ? ST_FULL : ST_SHOW;
953 send_state(win, win->sys->state);
954 sys_show(win, win->sys->state);
960 static void on_event(xcb_generic_event_t *event)
962 int type = XCB_EVENT_RESPONSE_TYPE(event);
967 on_key_event((xcb_key_press_event_t *)event, 0);
969 case XCB_KEY_RELEASE:
970 on_key_event((xcb_key_release_event_t *)event, 1);
972 case XCB_BUTTON_PRESS:
973 on_button_event((xcb_button_press_event_t *)event, 0);
975 case XCB_BUTTON_RELEASE:
976 on_button_event((xcb_button_release_event_t *)event, 1);
978 case XCB_MOTION_NOTIFY:
979 on_motion_notify((xcb_motion_notify_event_t *)event);
981 case XCB_ENTER_NOTIFY:
982 on_enter_notify((xcb_enter_notify_event_t *)event);
984 case XCB_LEAVE_NOTIFY:
985 on_leave_notify((xcb_leave_notify_event_t *)event);
988 on_focus_in((xcb_focus_in_event_t *)event);
991 on_focus_out((xcb_focus_out_event_t *)event);
994 /* Window management */
995 case XCB_CREATE_NOTIFY:
996 on_create_notify((xcb_create_notify_event_t *)event);
998 case XCB_DESTROY_NOTIFY:
999 on_destroy_notify((xcb_destroy_notify_event_t *)event);
1001 case XCB_UNMAP_NOTIFY:
1002 on_unmap_notify((xcb_unmap_notify_event_t *)event);
1004 case XCB_MAP_NOTIFY:
1005 on_map_notify((xcb_map_notify_event_t *)event);
1007 case XCB_MAP_REQUEST:
1008 on_map_request((xcb_map_request_event_t *)event);
1010 case XCB_CONFIGURE_REQUEST:
1011 on_configure_request((xcb_configure_request_event_t *)event);
1013 case XCB_PROPERTY_NOTIFY:
1014 on_property_notify((xcb_property_notify_event_t *)event);
1016 case XCB_CLIENT_MESSAGE:
1017 on_client_message((xcb_client_message_event_t *)event);
1020 /* Unknown events */
1022 printf("on_event: %d:%02X -> %s\n",
1023 XCB_EVENT_SENT(event) != 0,
1024 XCB_EVENT_RESPONSE_TYPE(event),
1025 xcb_event_get_label(type) ?: "unknown_event");
1030 /********************
1031 * System functions *
1032 ********************/
1034 void sys_move(win_t *win, int x, int y, int w, int h)
1036 printf("sys_move: %p - %dx%d @ %d,%d\n",
1043 win->w = MAX(w,1+b);
1044 win->h = MAX(h,1+b);
1048 do_configure_window(win->sys->xcb, x, y, w, h, -1, -1, -1);
1051 void sys_raise(win_t *win)
1053 printf("sys_raise: %p\n", win);
1055 uint16_t mask = XCB_CONFIG_WINDOW_STACK_MODE;
1056 uint32_t list = XCB_STACK_MODE_ABOVE;
1058 xcb_configure_window(conn, win->sys->xcb, mask, &list);
1059 for (list_t *cur = struts; cur; cur = cur->next)
1060 xcb_configure_window(conn,
1061 ((win_t*)cur->data)->sys->xcb, mask, &list);
1064 void sys_focus(win_t *win)
1066 printf("sys_focus: %p\n", win);
1067 xcb_window_t xcb = win ? win->sys->xcb : root;
1069 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT,
1070 xcb, XCB_CURRENT_TIME);
1073 void sys_show(win_t *win, state_t state)
1075 printf("sys_show: %p - %s -> %s\n", win,
1076 state_map[win->state], state_map[state]);
1077 xcb_window_t xcb = win ? win->sys->xcb : root;
1081 if (state == ST_FULL || state == ST_MAX) {
1082 for (list_t *cur = screens; cur; cur = cur->next) {
1083 full = max = *(win_t*)cur->data;
1084 if (win->x >= max.x && win->x <= max.x+max.w &&
1085 win->y >= max.y && win->y <= max.y+max.h)
1088 for (list_t *cur = struts; cur; cur = cur->next) {
1089 strut_t *strut = &((win_t*)cur->data)->sys->strut;
1090 full.x -= strut->left;
1091 full.y -= strut->top;
1092 full.w += strut->left + strut->right;
1093 full.h += strut->top + strut->bottom;
1097 /* Change window state */
1100 xcb_unmap_window(conn, xcb);
1104 xcb_map_window(conn, xcb);
1105 do_configure_window(xcb, win->x, win->y,
1106 MAX(win->w - 2*border, 1),
1107 MAX(win->h - 2*border, 1),
1112 xcb_map_window(conn, xcb);
1113 do_configure_window(xcb, full.x, full.y, full.w, full.h,
1114 0, -1, XCB_STACK_MODE_ABOVE);
1118 xcb_map_window(conn, xcb);
1119 do_configure_window(xcb, max.x, max.y,
1120 MAX(max.w - 2*border, 1),
1121 MAX(max.h - 2*border, 1),
1122 border, -1, XCB_STACK_MODE_ABOVE);
1126 xcb_map_window(conn, xcb);
1127 do_configure_window(xcb, -1, -1, -1, stack,
1132 xcb_map_window(conn, xcb);
1133 do_configure_window(xcb, -1, -1, 100, 100,
1138 if (!do_client_message(xcb, wm_delete))
1139 xcb_kill_client(conn, xcb);
1144 win->sys->state = win->state = state;
1147 void sys_watch(win_t *win, event_t ev, mod_t mod)
1149 printf("sys_watch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
1150 xcb_window_t xcb = win ? win->sys->xcb : root;
1151 xcb_event_mask_t *mask = win ? &win->sys->events : &events;
1152 xcb_mod_mask_t mods = 0;
1153 xcb_button_t btn = 0;
1154 xcb_keycode_t *code = 0;
1158 *mask |= XCB_EVENT_MASK_ENTER_WINDOW;
1159 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
1163 *mask |= XCB_EVENT_MASK_LEAVE_WINDOW;
1164 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
1169 *mask |= XCB_EVENT_MASK_FOCUS_CHANGE;
1170 xcb_change_window_attributes(conn, xcb, XCB_CW_EVENT_MASK, mask);
1173 case EV_MOUSE0...EV_MOUSE7:
1174 btn = event_to_button(ev);
1175 mods = mod_to_mask(mod);
1176 *mask |= mod.up ? XCB_EVENT_MASK_BUTTON_RELEASE
1177 : XCB_EVENT_MASK_BUTTON_PRESS;
1178 xcb_grab_button(conn, 0, xcb, *mask,
1179 XCB_GRAB_MODE_ASYNC,
1180 XCB_GRAB_MODE_ASYNC,
1185 code = event_to_keycodes(ev);
1186 mods = mod_to_mask(mod);
1187 for (int i = 0; code && code[i] != XCB_NO_SYMBOL; i++)
1188 xcb_grab_key(conn, 1, xcb, mods, code[i],
1189 XCB_GRAB_MODE_ASYNC,
1190 XCB_GRAB_MODE_ASYNC);
1196 void sys_unwatch(win_t *win, event_t ev, mod_t mod)
1198 printf("sys_unwatch: %p - 0x%X,0x%X\n", win, ev, mod2int(mod));
1201 list_t *sys_info(void)
1203 printf("sys_info\n");
1205 if (screens == NULL && do_xinerama_check()) {
1206 /* Add Xinerama screens */
1208 xcb_xinerama_screen_info_t *info = NULL;
1209 void *reply = do_query_screens(&info, &ninfo);
1210 for (int i = 0; i < ninfo; i++) {
1211 win_t *screen = new0(win_t);
1213 screen->x = info[i].x_org;
1214 screen->y = info[i].y_org;
1215 screen->w = info[i].width;
1216 screen->h = info[i].height;
1218 screens = list_insert(NULL, screen);
1220 printf("sys_info: xinerama screen - %dx%d @ %d,%d\n",
1221 screen->w, screen->h,
1222 screen->x, screen->y);
1227 if (screens == NULL) {
1228 /* No xinerama support */
1229 const xcb_setup_t *setup = xcb_get_setup(conn);
1230 xcb_screen_t *geom = xcb_setup_roots_iterator(setup).data;
1232 win_t *screen = new0(win_t);
1234 screen->w = geom->width_in_pixels;
1235 screen->h = geom->height_in_pixels;
1237 screens = list_insert(NULL, screen);
1239 printf("sys_info: root screen - %dx%d\n",
1240 screen->w, screen->h);
1248 printf("sys_init\n");
1250 xcb_void_cookie_t cookie;
1251 xcb_generic_error_t *err;
1253 /* Load configuration */
1254 stack = conf_get_int("main.stack", stack);
1255 border = conf_get_int("main.border", border);
1256 no_capture = conf_get_int("main.no-capture", no_capture);
1258 /* Connect to display */
1259 if (!(conn = xcb_connect(NULL, NULL)))
1260 error("xcb connect failed");
1261 if (xcb_connection_has_error(conn))
1262 error("xcb connection has errors");
1264 /* Get root window */
1265 const xcb_setup_t *setup = xcb_get_setup(conn);
1266 xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
1267 root = iter.data->root;
1268 colormap = iter.data->default_colormap;
1270 /* Request substructure redirect */
1271 events = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT |
1272 XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
1273 cookie = xcb_change_window_attributes_checked(conn, root,
1274 XCB_CW_EVENT_MASK, &events);
1275 if ((err = xcb_request_check(conn, cookie)))
1276 error("another window manager is already running");
1279 wm_protos = do_intern_atom("WM_PROTOCOLS");
1280 wm_delete = do_intern_atom("WM_DELETE_WINDOW");
1281 wm_nhints = do_intern_atom("WM_NORMAL_HINTS");
1282 if (!wm_protos || !wm_delete || !wm_nhints)
1283 error("unable to setup atoms");
1285 /* Setup EWMH connection */
1286 if (!do_ewmh_init_atoms())
1287 error("ewmh setup failed");
1288 xcb_ewmh_set_supported(&ewmh, 0, 82, &ewmh._NET_SUPPORTED);
1290 /* Set EWMH wm window */
1291 uint32_t override = 1;
1292 control = xcb_generate_id(conn);
1293 printf("control window: %d\n", control);
1294 cookie = xcb_create_window_checked(conn, 0, control, root,
1295 0, 0, 1, 1, 0, 0, 0,
1296 XCB_CW_OVERRIDE_REDIRECT, &override);
1297 if ((err = xcb_request_check(conn, cookie)))
1298 error("can't create control window");
1299 cookie = xcb_ewmh_set_wm_name_checked(&ewmh, control, 5, "wmpus");
1300 if ((err = xcb_request_check(conn, cookie)))
1301 error("can't set wm name");
1302 cookie = xcb_ewmh_set_supporting_wm_check_checked(&ewmh, root, control);
1303 if ((err = xcb_request_check(conn, cookie)))
1304 error("can't set control window");
1306 /* Setup for for ST_CLOSE */
1307 xcb_set_close_down_mode(conn, XCB_CLOSE_DOWN_DESTROY_ALL);
1309 /* Allocate key symbols */
1310 if (!(keysyms = xcb_key_symbols_alloc(conn)))
1311 error("cannot allocate key symbols");
1313 /* Read color information */
1314 clr_focus = do_alloc_color(0xFF6060);
1315 clr_unfocus = do_alloc_color(0xD8D8FF);
1316 clr_urgent = do_alloc_color(0xFF0000);
1321 printf("sys_run\n");
1323 /* Add each initial window */
1326 xcb_window_t *kids = NULL;
1327 void *reply = do_query_tree(root, &kids, &nkids);
1328 for(int i = 0; i < nkids; i++) {
1329 int override=0, mapped=0;
1330 if (kids[i] == control)
1332 win_t *win = win_new(kids[i]);
1333 if (do_get_strut(win->sys->xcb, &win->sys->strut))
1335 do_get_geometry(kids[i], &win->x, &win->y, &win->w, &win->h);
1336 do_get_window_attributes(kids[i], &override, &mapped);
1337 printf(" found %-8u %dx%d @ %d,%d --%s%s\n", kids[i],
1338 win->w, win->h, win->x, win->y,
1339 override ? " override" : "",
1340 mapped ? " mapped" : "");
1341 state_t state = mapped ? ST_SHOW : ST_HIDE;
1342 win->sys->mapped = mapped;
1343 do_get_type(kids[i], &win->type);
1344 do_get_icccm_state(kids[i], &state);
1345 do_get_ewmh_state(kids[i], &state);
1346 send_manage(win, !override);
1347 send_state(win, state);
1358 xcb_generic_event_t *event;
1359 if (!(event = xcb_wait_for_event(conn)))
1363 if (!(status = xcb_flush(conn)))
1370 printf("sys_exit\n");
1372 xcb_client_message_event_t msg = {
1373 .response_type = XCB_CLIENT_MESSAGE,
1377 .data.data32[0] = wm_delete,
1378 .data.data32[1] = XCB_CURRENT_TIME,
1380 xcb_send_event(conn, 0, control, XCB_EVENT_MASK_NO_EVENT,
1381 (const char *)&msg);
1387 printf("sys_free\n");
1389 xcb_void_cookie_t cookie;
1390 xcb_generic_error_t *err;
1393 cookie = xcb_delete_property_checked(conn, root,
1394 ewmh._NET_SUPPORTING_WM_CHECK);
1395 if ((err = xcb_request_check(conn, cookie)))
1396 warn("can't remove control window");
1398 cookie = xcb_destroy_window_checked(conn, control);
1399 if ((err = xcb_request_check(conn, cookie)))
1400 warn("can't destroy control window");
1402 /* close connection */
1403 xcb_ewmh_connection_wipe(&ewmh);
1404 xcb_key_symbols_free(keysyms);
1405 xcb_disconnect(conn);
1407 /* free local data */
1409 screens = list_remove(screens, screens, 1);
1410 tdestroy(cache, (void(*)(void*))win_free);