2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * x.c: Interface to X11, transfers our in-memory state to X11 (see also
8 * render.c). Basically a big state machine.
14 #define MAX(x, y) ((x) > (y) ? (x) : (y))
17 xcb_window_t ewmh_window;
19 /* Stores the X11 window ID of the currently focused window */
20 xcb_window_t focused_id = XCB_NONE;
22 /* Because 'focused_id' might be reset to force input focus, we separately keep
23 * track of the X11 window ID to be able to always tell whether the focused
24 * window actually changed. */
25 static xcb_window_t last_focused = XCB_NONE;
27 /* Stores coordinates to warp mouse pointer to if set */
31 * Describes the X11 state we may modify (map state, position, window stack).
32 * There is one entry per container. The state represents the current situation
33 * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
34 * which represents the order that will be pushed to X11, while old_state_head
35 * represents the current order). It will be updated in x_push_changes().
38 typedef struct con_state {
45 /* The con for which this state is. */
48 /* For reparenting, we have a flag (need_reparent) and the X ID of the old
49 * frame this window was in. The latter is necessary because we need to
50 * ignore UnmapNotify events (by changing the window event mask). */
52 xcb_window_t old_frame;
61 CIRCLEQ_ENTRY(con_state)
64 CIRCLEQ_ENTRY(con_state)
67 TAILQ_ENTRY(con_state)
68 initial_mapping_order;
71 CIRCLEQ_HEAD(state_head, con_state)
73 CIRCLEQ_HEAD_INITIALIZER(state_head);
75 CIRCLEQ_HEAD(old_state_head, con_state)
77 CIRCLEQ_HEAD_INITIALIZER(old_state_head);
79 TAILQ_HEAD(initial_mapping_head, con_state)
80 initial_mapping_head =
81 TAILQ_HEAD_INITIALIZER(initial_mapping_head);
84 * Returns the container state for the given frame. This function always
85 * returns a container state (otherwise, there is a bug in the code and the
86 * container state of a container for which x_con_init() was not called was
90 static con_state *state_for_frame(xcb_window_t window) {
92 CIRCLEQ_FOREACH(state, &state_head, state)
93 if (state->id == window)
96 /* TODO: better error handling? */
97 ELOG("No state found\n");
103 * Changes the atoms on the root window and the windows themselves to properly
104 * reflect the current focus for ewmh compliance.
107 static void change_ewmh_focus(xcb_window_t new_focus, xcb_window_t old_focus) {
108 if (new_focus == old_focus) {
112 ewmh_update_active_window(new_focus);
114 if (new_focus != XCB_WINDOW_NONE) {
115 ewmh_update_focused(new_focus, true);
118 if (old_focus != XCB_WINDOW_NONE) {
119 ewmh_update_focused(old_focus, false);
124 * Initializes the X11 part for the given container. Called exactly once for
125 * every container from con_new().
128 void x_con_init(Con *con) {
129 /* TODO: maybe create the window when rendering first? we could then even
130 * get the initial geometry right */
135 xcb_visualid_t visual = get_visualid_by_depth(con->depth);
136 xcb_colormap_t win_colormap;
137 if (con->depth != root_depth) {
138 /* We need to create a custom colormap. */
139 win_colormap = xcb_generate_id(conn);
140 xcb_create_colormap(conn, XCB_COLORMAP_ALLOC_NONE, win_colormap, root, visual);
141 con->colormap = win_colormap;
143 /* Use the default colormap. */
144 win_colormap = colormap;
145 con->colormap = XCB_NONE;
148 /* We explicitly set a background color and border color (even though we
149 * don’t even have a border) because the X11 server requires us to when
150 * using 32 bit color depths, see
151 * https://stackoverflow.com/questions/3645632 */
152 mask |= XCB_CW_BACK_PIXEL;
153 values[0] = root_screen->black_pixel;
155 mask |= XCB_CW_BORDER_PIXEL;
156 values[1] = root_screen->black_pixel;
158 /* our own frames should not be managed */
159 mask |= XCB_CW_OVERRIDE_REDIRECT;
162 /* see include/xcb.h for the FRAME_EVENT_MASK */
163 mask |= XCB_CW_EVENT_MASK;
164 values[3] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
166 mask |= XCB_CW_COLORMAP;
167 values[4] = win_colormap;
169 Rect dims = {-15, -15, 10, 10};
170 xcb_window_t frame_id = create_window(conn, dims, con->depth, visual, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER, false, mask, values);
171 draw_util_surface_init(conn, &(con->frame), frame_id, get_visualtype_by_id(visual), dims.width, dims.height);
172 xcb_change_property(conn,
173 XCB_PROP_MODE_REPLACE,
178 (strlen("i3-frame") + 1) * 2,
179 "i3-frame\0i3-frame\0");
181 struct con_state *state = scalloc(1, sizeof(struct con_state));
182 state->id = con->frame.id;
183 state->mapped = false;
184 state->initial = true;
185 DLOG("Adding window 0x%08x to lists\n", state->id);
186 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
187 CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
188 TAILQ_INSERT_TAIL(&initial_mapping_head, state, initial_mapping_order);
189 DLOG("adding new state for window id 0x%08x\n", state->id);
193 * Re-initializes the associated X window state for this container. You have
194 * to call this when you assign a client to an empty container to ensure that
195 * its state gets updated correctly.
198 void x_reinit(Con *con) {
199 struct con_state *state;
201 if ((state = state_for_frame(con->frame.id)) == NULL) {
202 ELOG("window state not found\n");
206 DLOG("resetting state %p to initial\n", state);
207 state->initial = true;
208 state->child_mapped = false;
210 memset(&(state->window_rect), 0, sizeof(Rect));
214 * Reparents the child window of the given container (necessary for sticky
215 * containers). The reparenting happens in the next call of x_push_changes().
218 void x_reparent_child(Con *con, Con *old) {
219 struct con_state *state;
220 if ((state = state_for_frame(con->frame.id)) == NULL) {
221 ELOG("window state for con not found\n");
225 state->need_reparent = true;
226 state->old_frame = old->frame.id;
230 * Moves a child window from Container src to Container dest.
233 void x_move_win(Con *src, Con *dest) {
234 struct con_state *state_src, *state_dest;
236 if ((state_src = state_for_frame(src->frame.id)) == NULL) {
237 ELOG("window state for src not found\n");
241 if ((state_dest = state_for_frame(dest->frame.id)) == NULL) {
242 ELOG("window state for dest not found\n");
246 state_dest->con = state_src->con;
247 state_src->con = NULL;
249 Rect zero = {0, 0, 0, 0};
250 if (memcmp(&(state_dest->window_rect), &(zero), sizeof(Rect)) == 0) {
251 memcpy(&(state_dest->window_rect), &(state_src->window_rect), sizeof(Rect));
252 DLOG("COPYING RECT\n");
256 static void _x_con_kill(Con *con) {
259 if (con->colormap != XCB_NONE) {
260 xcb_free_colormap(conn, con->colormap);
263 draw_util_surface_free(conn, &(con->frame));
264 draw_util_surface_free(conn, &(con->frame_buffer));
265 xcb_free_pixmap(conn, con->frame_buffer.id);
266 state = state_for_frame(con->frame.id);
267 CIRCLEQ_REMOVE(&state_head, state, state);
268 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
269 TAILQ_REMOVE(&initial_mapping_head, state, initial_mapping_order);
273 /* Invalidate focused_id to correctly focus new windows with the same ID */
274 focused_id = last_focused = XCB_NONE;
278 * Kills the window decoration associated with the given container.
281 void x_con_kill(Con *con) {
283 xcb_destroy_window(conn, con->frame.id);
287 * Completely reinitializes the container's frame, without destroying the old window.
290 void x_con_reframe(Con *con) {
296 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
299 bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
300 xcb_get_property_cookie_t cookie;
301 xcb_icccm_get_wm_protocols_reply_t protocols;
304 cookie = xcb_icccm_get_wm_protocols(conn, window, A_WM_PROTOCOLS);
305 if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
308 /* Check if the client’s protocols have the requested atom set */
309 for (uint32_t i = 0; i < protocols.atoms_len; i++)
310 if (protocols.atoms[i] == atom)
313 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
319 * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
322 void x_window_kill(xcb_window_t window, kill_window_t kill_window) {
323 /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
324 if (!window_supports_protocol(window, A_WM_DELETE_WINDOW)) {
325 if (kill_window == KILL_WINDOW) {
326 LOG("Killing specific window 0x%08x\n", window);
327 xcb_destroy_window(conn, window);
329 LOG("Killing the X11 client which owns window 0x%08x\n", window);
330 xcb_kill_client(conn, window);
335 /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
336 * In order to properly initialize these bytes, we allocate 32 bytes even
337 * though we only need less for an xcb_configure_notify_event_t */
338 void *event = scalloc(32, 1);
339 xcb_client_message_event_t *ev = event;
341 ev->response_type = XCB_CLIENT_MESSAGE;
343 ev->type = A_WM_PROTOCOLS;
345 ev->data.data32[0] = A_WM_DELETE_WINDOW;
346 ev->data.data32[1] = XCB_CURRENT_TIME;
348 LOG("Sending WM_DELETE to the client\n");
349 xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)ev);
354 static void x_draw_title_border(Con *con, struct deco_render_params *p) {
355 assert(con->parent != NULL);
357 Rect *dr = &(con->deco_rect);
360 draw_util_rectangle(&(con->parent->frame_buffer), p->color->border,
361 dr->x, dr->y, 1, dr->height);
364 draw_util_rectangle(&(con->parent->frame_buffer), p->color->border,
365 dr->x + dr->width - 1, dr->y, 1, dr->height);
368 draw_util_rectangle(&(con->parent->frame_buffer), p->color->border,
369 dr->x, dr->y, dr->width, 1);
372 draw_util_rectangle(&(con->parent->frame_buffer), p->color->border,
373 dr->x, dr->y + dr->height - 1, dr->width, 1);
376 static void x_draw_decoration_after_title(Con *con, struct deco_render_params *p) {
377 assert(con->parent != NULL);
379 Rect *dr = &(con->deco_rect);
381 /* Redraw the right border to cut off any text that went past it.
382 * This is necessary when the text was drawn using XCB since cutting text off
383 * automatically does not work there. For pango rendering, this isn't necessary. */
384 if (!font_is_pango()) {
385 /* We actually only redraw the far right two pixels as that is the
386 * distance we keep from the edge (not the entire border width).
387 * Redrawing the entire border would cause text to be cut off. */
388 draw_util_rectangle(&(con->parent->frame_buffer), p->color->background,
389 dr->x + dr->width - 2 * logical_px(1),
395 /* Redraw the border. */
396 x_draw_title_border(con, p);
400 * Draws the decoration of the given container onto its parent.
403 void x_draw_decoration(Con *con) {
404 Con *parent = con->parent;
405 bool leaf = con_is_leaf(con);
407 /* This code needs to run for:
409 * • non-leaf containers which are in a stacked/tabbed container
411 * It does not need to run for:
412 * • direct children of outputs or dockareas
413 * • floating containers (they don’t have a decoration)
416 parent->layout != L_STACKED &&
417 parent->layout != L_TABBED) ||
418 parent->type == CT_OUTPUT ||
419 parent->type == CT_DOCKAREA ||
420 con->type == CT_FLOATING_CON)
423 /* Skip containers whose height is 0 (for example empty dockareas) */
424 if (con->rect.height == 0)
427 /* Skip containers whose pixmap has not yet been created (can happen when
428 * decoration rendering happens recursively for a window for which
429 * x_push_node() was not yet called) */
430 if (leaf && con->frame_buffer.id == XCB_NONE)
433 /* 1: build deco_params and compare with cache */
434 struct deco_render_params *p = scalloc(1, sizeof(struct deco_render_params));
436 /* find out which colors to use */
438 p->color = &config.client.urgent;
439 else if (con == focused || con_inside_focused(con))
440 p->color = &config.client.focused;
441 else if (con == TAILQ_FIRST(&(parent->focus_head)))
442 p->color = &config.client.focused_inactive;
444 p->color = &config.client.unfocused;
446 p->border_style = con_border_style(con);
448 Rect *r = &(con->rect);
449 Rect *w = &(con->window_rect);
450 p->con_rect = (struct width_height){r->width, r->height};
451 p->con_window_rect = (struct width_height){w->width, w->height};
452 p->con_deco_rect = con->deco_rect;
453 p->background = config.client.background;
454 p->con_is_leaf = con_is_leaf(con);
455 p->parent_layout = con->parent->layout;
457 if (con->deco_render_params != NULL &&
458 (con->window == NULL || !con->window->name_x_changed) &&
459 !parent->pixmap_recreated &&
460 !con->pixmap_recreated &&
461 !con->mark_changed &&
462 memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
468 while ((next = TAILQ_NEXT(next, nodes))) {
469 FREE(next->deco_render_params);
472 FREE(con->deco_render_params);
473 con->deco_render_params = p;
475 if (con->window != NULL && con->window->name_x_changed)
476 con->window->name_x_changed = false;
478 parent->pixmap_recreated = false;
479 con->pixmap_recreated = false;
480 con->mark_changed = false;
482 /* 2: draw the client.background, but only for the parts around the window_rect */
483 if (con->window != NULL) {
485 draw_util_rectangle(&(con->frame_buffer), config.client.background,
486 0, 0, r->width, w->y);
488 draw_util_rectangle(&(con->frame_buffer), config.client.background,
489 0, w->y + w->height, r->width, r->height - (w->y + w->height));
491 draw_util_rectangle(&(con->frame_buffer), config.client.background,
492 0, 0, w->x, r->height);
494 draw_util_rectangle(&(con->frame_buffer), config.client.background,
495 w->x + w->width, 0, r->width - (w->x + w->width), r->height);
498 /* 3: draw a rectangle in border color around the client */
499 if (p->border_style != BS_NONE && p->con_is_leaf) {
500 /* We might hide some borders adjacent to the screen-edge */
501 adjacent_t borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
502 Rect br = con_border_style_rect(con);
504 /* These rectangles represent the border around the child window
505 * (left, bottom and right part). We don’t just fill the whole
506 * rectangle because some children are not freely resizable and we want
507 * their background color to "shine through". */
508 if (!(borders_to_hide & ADJ_LEFT_SCREEN_EDGE)) {
509 draw_util_rectangle(&(con->frame_buffer), p->color->child_border, 0, 0, br.x, r->height);
511 if (!(borders_to_hide & ADJ_RIGHT_SCREEN_EDGE)) {
512 draw_util_rectangle(&(con->frame_buffer),
513 p->color->child_border, r->width + (br.width + br.x), 0,
514 -(br.width + br.x), r->height);
516 if (!(borders_to_hide & ADJ_LOWER_SCREEN_EDGE)) {
517 draw_util_rectangle(&(con->frame_buffer),
518 p->color->child_border, br.x, r->height + (br.height + br.y),
519 r->width + br.width, -(br.height + br.y));
521 /* pixel border needs an additional line at the top */
522 if (p->border_style == BS_PIXEL && !(borders_to_hide & ADJ_UPPER_SCREEN_EDGE)) {
523 draw_util_rectangle(&(con->frame_buffer),
524 p->color->child_border, br.x, 0, r->width + br.width, br.y);
527 /* Highlight the side of the border at which the next window will be
528 * opened if we are rendering a single window within a split container
529 * (which is undistinguishable from a single window outside a split
530 * container otherwise. */
531 if (TAILQ_NEXT(con, nodes) == NULL &&
532 TAILQ_PREV(con, nodes_head, nodes) == NULL &&
533 con->parent->type != CT_FLOATING_CON) {
534 if (p->parent_layout == L_SPLITH) {
535 draw_util_rectangle(&(con->frame_buffer), p->color->indicator,
536 r->width + (br.width + br.x), br.y, -(br.width + br.x), r->height + br.height);
537 } else if (p->parent_layout == L_SPLITV) {
538 draw_util_rectangle(&(con->frame_buffer), p->color->indicator,
539 br.x, r->height + (br.height + br.y), r->width + br.width, -(br.height + br.y));
544 /* if this is a borderless/1pixel window, we don’t need to render the
546 if (p->border_style != BS_NORMAL)
549 /* If the parent hasn't been set up yet, skip the decoration rendering
551 if (parent->frame_buffer.id == XCB_NONE)
554 /* For the first child, we clear the parent pixmap to ensure there's no
555 * garbage left on there. This is important to avoid tearing when using
557 if (con == TAILQ_FIRST(&(con->parent->nodes_head))) {
558 draw_util_clear_surface(&(con->parent->frame_buffer), COLOR_TRANSPARENT);
559 FREE(con->parent->deco_render_params);
562 /* 4: paint the bar */
563 draw_util_rectangle(&(parent->frame_buffer), p->color->background,
564 con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height);
566 /* 5: draw title border */
567 x_draw_title_border(con, p);
569 /* 6: draw the title */
570 int text_offset_y = (con->deco_rect.height - config.font.height) / 2;
572 struct Window *win = con->window;
575 if (con->title_format == NULL) {
577 char *tree = con_get_tree_representation(con);
578 sasprintf(&_title, "i3: %s", tree);
581 title = i3string_from_utf8(_title);
584 title = con_parse_title_format(con);
587 draw_util_text(title, &(parent->frame_buffer),
588 p->color->text, p->color->background,
589 con->deco_rect.x + logical_px(2),
590 con->deco_rect.y + text_offset_y,
591 con->deco_rect.width - 2 * logical_px(2));
592 I3STRING_FREE(title);
597 const int title_padding = logical_px(2);
598 const int deco_width = (int)con->deco_rect.width;
600 if (config.show_marks && !TAILQ_EMPTY(&(con->marks_head))) {
601 char *formatted_mark = sstrdup("");
602 bool had_visible_mark = false;
605 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
606 if (mark->name[0] == '_')
608 had_visible_mark = true;
611 sasprintf(&buf, "%s[%s]", formatted_mark, mark->name);
612 free(formatted_mark);
613 formatted_mark = buf;
616 if (had_visible_mark) {
617 i3String *mark = i3string_from_utf8(formatted_mark);
618 mark_width = predict_text_width(mark);
620 int mark_offset_x = (config.title_align == ALIGN_RIGHT)
622 : deco_width - mark_width - title_padding;
624 draw_util_text(mark, &(parent->frame_buffer),
625 p->color->text, p->color->background,
626 con->deco_rect.x + mark_offset_x,
627 con->deco_rect.y + text_offset_y, mark_width);
630 mark_width += title_padding;
633 FREE(formatted_mark);
636 i3String *title = con->title_format == NULL ? win->name : con_parse_title_format(con);
642 switch (config.title_align) {
644 /* (pad)[text ](pad)[mark + its pad) */
645 title_offset_x = title_padding;
648 /* (pad)[ text ](pad)[mark + its pad)
649 * To center the text inside its allocated space, the surface
650 * between the brackets, we use the formula
651 * (surface_width - predict_text_width) / 2
652 * where surface_width = deco_width - 2 * pad - mark_width
653 * so, offset = pad + (surface_width - predict_text_width) / 2 =
654 * = … = (deco_width - mark_width - predict_text_width) / 2 */
655 title_offset_x = max(title_padding, (deco_width - mark_width - predict_text_width(title)) / 2);
658 /* [mark + its pad](pad)[ text](pad) */
659 title_offset_x = max(title_padding + mark_width, deco_width - title_padding - predict_text_width(title));
663 draw_util_text(title, &(parent->frame_buffer),
664 p->color->text, p->color->background,
665 con->deco_rect.x + title_offset_x,
666 con->deco_rect.y + text_offset_y,
667 deco_width - mark_width - 2 * title_padding);
669 if (con->title_format != NULL) {
670 I3STRING_FREE(title);
674 x_draw_decoration_after_title(con, p);
676 draw_util_copy_surface(&(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
680 * Recursively calls x_draw_decoration. This cannot be done in x_push_node
681 * because x_push_node uses focus order to recurse (see the comment above)
682 * while drawing the decoration needs to happen in the actual order.
685 void x_deco_recurse(Con *con) {
687 bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
688 TAILQ_EMPTY(&(con->floating_head));
689 con_state *state = state_for_frame(con->frame.id);
692 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
693 x_deco_recurse(current);
695 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
696 x_deco_recurse(current);
699 draw_util_copy_surface(&(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
703 if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
704 (!leaf || con->mapped))
705 x_draw_decoration(con);
709 * Sets or removes the _NET_WM_STATE_HIDDEN property on con if necessary.
712 static void set_hidden_state(Con *con) {
713 if (con->window == NULL) {
717 con_state *state = state_for_frame(con->frame.id);
718 bool should_be_hidden = con_is_hidden(con);
719 if (should_be_hidden == state->is_hidden)
722 if (should_be_hidden) {
723 DLOG("setting _NET_WM_STATE_HIDDEN for con = %p\n", con);
724 xcb_add_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
726 DLOG("removing _NET_WM_STATE_HIDDEN for con = %p\n", con);
727 xcb_remove_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
730 state->is_hidden = should_be_hidden;
734 * This function pushes the properties of each node of the layout tree to
735 * X11 if they have changed (like the map state, position of the window, …).
736 * It recursively traverses all children of the given node.
739 void x_push_node(Con *con) {
742 Rect rect = con->rect;
744 //DLOG("Pushing changes for node %p / %s\n", con, con->name);
745 state = state_for_frame(con->frame.id);
747 if (state->name != NULL) {
748 DLOG("pushing name %s for con %p\n", state->name, con);
750 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame.id,
751 XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(state->name), state->name);
755 if (con->window == NULL) {
756 /* Calculate the height of all window decorations which will be drawn on to
758 uint32_t max_y = 0, max_height = 0;
759 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
760 Rect *dr = &(current->deco_rect);
761 if (dr->y >= max_y && dr->height >= max_height) {
763 max_height = dr->height;
766 rect.height = max_y + max_height;
767 if (rect.height == 0)
771 /* reparent the child window (when the window was moved due to a sticky
773 if (state->need_reparent && con->window != NULL) {
774 DLOG("Reparenting child window\n");
776 /* Temporarily set the event masks to XCB_NONE so that we won’t get
777 * UnmapNotify events (otherwise the handler would close the container).
778 * These events are generated automatically when reparenting. */
779 uint32_t values[] = {XCB_NONE};
780 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
781 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
783 xcb_reparent_window(conn, con->window->id, con->frame.id, 0, 0);
785 values[0] = FRAME_EVENT_MASK;
786 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
787 values[0] = CHILD_EVENT_MASK;
788 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
790 state->old_frame = XCB_NONE;
791 state->need_reparent = false;
794 DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
795 con, con->window->id, con->ignore_unmap);
798 /* The pixmap of a borderless leaf container will not be used except
799 * for the titlebar in a stack or tabs (issue #1013). */
800 bool is_pixmap_needed = (con->border_style != BS_NONE ||
802 con->parent->layout == L_STACKED ||
803 con->parent->layout == L_TABBED);
805 /* The root con and output cons will never require a pixmap. In particular for the
806 * __i3 output, this will likely not work anyway because it might be ridiculously
807 * large, causing an XCB_ALLOC error. */
808 if (con->type == CT_ROOT || con->type == CT_OUTPUT)
809 is_pixmap_needed = false;
811 bool fake_notify = false;
812 /* Set new position if rect changed (and if height > 0) or if the pixmap
813 * needs to be recreated */
814 if ((is_pixmap_needed && con->frame_buffer.id == XCB_NONE) || (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
816 /* We first create the new pixmap, then render to it, set it as the
817 * background and only afterwards change the window size. This reduces
820 bool has_rect_changed = (state->rect.x != rect.x || state->rect.y != rect.y ||
821 state->rect.width != rect.width || state->rect.height != rect.height);
823 /* Check if the container has an unneeded pixmap left over from
824 * previously having a border or titlebar. */
825 if (!is_pixmap_needed && con->frame_buffer.id != XCB_NONE) {
826 draw_util_surface_free(conn, &(con->frame_buffer));
827 xcb_free_pixmap(conn, con->frame_buffer.id);
828 con->frame_buffer.id = XCB_NONE;
831 if (is_pixmap_needed && (has_rect_changed || con->frame_buffer.id == XCB_NONE)) {
832 if (con->frame_buffer.id == XCB_NONE) {
833 con->frame_buffer.id = xcb_generate_id(conn);
835 draw_util_surface_free(conn, &(con->frame_buffer));
836 xcb_free_pixmap(conn, con->frame_buffer.id);
839 uint16_t win_depth = root_depth;
841 win_depth = con->window->depth;
843 /* Ensure we have valid dimensions for our surface. */
844 // TODO This is probably a bug in the condition above as we should never enter this path
845 // for height == 0. Also, we should probably handle width == 0 the same way.
846 int width = MAX((int32_t)rect.width, 1);
847 int height = MAX((int32_t)rect.height, 1);
849 xcb_create_pixmap(conn, win_depth, con->frame_buffer.id, con->frame.id, width, height);
850 draw_util_surface_init(conn, &(con->frame_buffer), con->frame_buffer.id,
851 get_visualtype_by_id(get_visualid_by_depth(win_depth)), width, height);
853 /* For the graphics context, we disable GraphicsExposure events.
854 * Those will be sent when a CopyArea request cannot be fulfilled
855 * properly due to parts of the source being unmapped or otherwise
856 * unavailable. Since we always copy from pixmaps to windows, this
857 * is not a concern for us. */
858 xcb_change_gc(conn, con->frame_buffer.gc, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
860 draw_util_surface_set_size(&(con->frame), width, height);
861 con->pixmap_recreated = true;
863 /* Don’t render the decoration for windows inside a stack which are
864 * not visible right now */
865 // TODO Should this work the same way for L_TABBED?
867 con->parent->layout != L_STACKED ||
868 TAILQ_FIRST(&(con->parent->focus_head)) == con)
869 /* Render the decoration now to make the correct decoration visible
870 * from the very first moment. Later calls will be cached, so this
871 * doesn’t hurt performance. */
875 DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
876 /* flush to ensure that the following commands are sent in a single
877 * buffer and will be processed directly afterwards (the contents of a
878 * window get lost when resizing it, therefore we want to provide it as
879 * fast as possible) */
881 xcb_set_window_rect(conn, con->frame.id, rect);
882 if (con->frame_buffer.id != XCB_NONE) {
883 draw_util_copy_surface(&(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
887 memcpy(&(state->rect), &rect, sizeof(Rect));
891 /* dito, but for child windows */
892 if (con->window != NULL &&
893 memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
894 DLOG("setting window rect (%d, %d, %d, %d)\n",
895 con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
896 xcb_set_window_rect(conn, con->window->id, con->window_rect);
897 memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
901 /* Map if map state changed, also ensure that the child window
902 * is changed if we are mapped and there is a new, unmapped child window.
903 * Unmaps are handled in x_push_node_unmaps(). */
904 if ((state->mapped != con->mapped || (con->window != NULL && !state->child_mapped)) &&
906 xcb_void_cookie_t cookie;
908 if (con->window != NULL) {
909 /* Set WM_STATE_NORMAL because GTK applications don’t want to
910 * drag & drop if we don’t. Also, xprop(1) needs it. */
911 long data[] = {XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE};
912 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
913 A_WM_STATE, A_WM_STATE, 32, 2, data);
917 if (!state->child_mapped && con->window != NULL) {
918 cookie = xcb_map_window(conn, con->window->id);
920 /* We are interested in EnterNotifys as soon as the window is
922 values[0] = CHILD_EVENT_MASK;
923 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
924 DLOG("mapping child window (serial %d)\n", cookie.sequence);
925 state->child_mapped = true;
928 cookie = xcb_map_window(conn, con->frame.id);
930 values[0] = FRAME_EVENT_MASK;
931 xcb_change_window_attributes(conn, con->frame.id, XCB_CW_EVENT_MASK, values);
933 /* copy the pixmap contents to the frame window immediately after mapping */
934 if (con->frame_buffer.id != XCB_NONE) {
935 draw_util_copy_surface(&(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
939 DLOG("mapping container %08x (serial %d)\n", con->frame.id, cookie.sequence);
940 state->mapped = con->mapped;
943 state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
946 DLOG("Sending fake configure notify\n");
947 fake_absolute_configure_notify(con);
950 set_hidden_state(con);
952 /* Handle all children and floating windows of this node. We recurse
953 * in focus order to display the focused client in a stack first when
954 * switching workspaces (reduces flickering). */
955 TAILQ_FOREACH(current, &(con->focus_head), focused) {
956 x_push_node(current);
961 * Same idea as in x_push_node(), but this function only unmaps windows. It is
962 * necessary to split this up to handle new fullscreen clients properly: The
963 * new window needs to be mapped and focus needs to be set *before* the
964 * underlying windows are unmapped. Otherwise, focus will revert to the
965 * PointerRoot and will then be set to the new window, generating unnecessary
966 * FocusIn/FocusOut events.
969 static void x_push_node_unmaps(Con *con) {
973 //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
974 state = state_for_frame(con->frame.id);
976 /* map/unmap if map state changed, also ensure that the child window
977 * is changed if we are mapped *and* in initial state (meaning the
978 * container was empty before, but now got a child) */
979 if (state->unmap_now) {
980 xcb_void_cookie_t cookie;
981 if (con->window != NULL) {
982 /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
983 long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
984 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
985 A_WM_STATE, A_WM_STATE, 32, 2, data);
988 cookie = xcb_unmap_window(conn, con->frame.id);
989 DLOG("unmapping container %p / %s (serial %d)\n", con, con->name, cookie.sequence);
990 /* we need to increase ignore_unmap for this container (if it
991 * contains a window) and for every window "under" this one which
992 * contains a window */
993 if (con->window != NULL) {
995 DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame.id, con->ignore_unmap);
997 state->mapped = con->mapped;
1000 /* handle all children and floating windows of this node */
1001 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
1002 x_push_node_unmaps(current);
1004 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
1005 x_push_node_unmaps(current);
1009 * Returns true if the given container is currently attached to its parent.
1011 * TODO: Remove once #1185 has been fixed
1013 static bool is_con_attached(Con *con) {
1014 if (con->parent == NULL)
1018 TAILQ_FOREACH(current, &(con->parent->nodes_head), nodes) {
1027 * Pushes all changes (state of each node, see x_push_node() and the window
1030 * NOTE: We need to push the stack first so that the windows have the correct
1031 * stacking order. This is relevant for workspace switching where we map the
1032 * windows because mapping may generate EnterNotify events. When they are
1033 * generated in the wrong order, this will cause focus problems when switching
1037 void x_push_changes(Con *con) {
1039 xcb_query_pointer_cookie_t pointercookie;
1041 /* If we need to warp later, we request the pointer position as soon as possible */
1043 pointercookie = xcb_query_pointer(conn, root);
1046 DLOG("-- PUSHING WINDOW STACK --\n");
1047 //DLOG("Disabling EnterNotify\n");
1048 /* We need to keep SubstructureRedirect around, otherwise clients can send
1049 * ConfigureWindow requests and get them applied directly instead of having
1050 * them become ConfigureRequests that i3 handles. */
1051 uint32_t values[1] = {XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT};
1052 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1054 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1056 //DLOG("Done, EnterNotify disabled\n");
1057 bool order_changed = false;
1058 bool stacking_changed = false;
1060 /* count first, necessary to (re)allocate memory for the bottom-to-top
1061 * stack afterwards */
1063 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
1064 if (con_has_managed_window(state->con))
1067 /* The bottom-to-top window stack of all windows which are managed by i3.
1068 * Used for x_get_window_stack(). */
1069 static xcb_window_t *client_list_windows = NULL;
1070 static int client_list_count = 0;
1072 if (cnt != client_list_count) {
1073 client_list_windows = srealloc(client_list_windows, sizeof(xcb_window_t) * cnt);
1074 client_list_count = cnt;
1077 xcb_window_t *walk = client_list_windows;
1079 /* X11 correctly represents the stack if we push it from bottom to top */
1080 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1081 if (con_has_managed_window(state->con))
1082 memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
1084 //DLOG("stack: 0x%08x\n", state->id);
1085 con_state *prev = CIRCLEQ_PREV(state, state);
1086 con_state *old_prev = CIRCLEQ_PREV(state, old_state);
1087 if (prev != old_prev)
1088 order_changed = true;
1089 if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
1090 stacking_changed = true;
1091 //DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
1093 mask |= XCB_CONFIG_WINDOW_SIBLING;
1094 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
1095 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
1097 xcb_configure_window(conn, prev->id, mask, values);
1099 state->initial = false;
1102 /* If we re-stacked something (or a new window appeared), we need to update
1103 * the _NET_CLIENT_LIST and _NET_CLIENT_LIST_STACKING hints */
1104 if (stacking_changed) {
1105 DLOG("Client list changed (%i clients)\n", cnt);
1106 ewmh_update_client_list_stacking(client_list_windows, client_list_count);
1108 walk = client_list_windows;
1110 /* reorder by initial mapping */
1111 TAILQ_FOREACH(state, &initial_mapping_head, initial_mapping_order) {
1112 if (con_has_managed_window(state->con))
1113 *walk++ = state->con->window->id;
1116 ewmh_update_client_list(client_list_windows, client_list_count);
1119 DLOG("PUSHING CHANGES\n");
1123 xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
1124 if (!pointerreply) {
1125 ELOG("Could not query pointer position, not warping pointer\n");
1127 int mid_x = warp_to->x + (warp_to->width / 2);
1128 int mid_y = warp_to->y + (warp_to->height / 2);
1130 Output *current = get_output_containing(pointerreply->root_x, pointerreply->root_y);
1131 Output *target = get_output_containing(mid_x, mid_y);
1132 if (current != target) {
1133 /* Ignore MotionNotify events generated by warping */
1134 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT});
1135 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
1136 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){ROOT_EVENT_MASK});
1144 //DLOG("Re-enabling EnterNotify\n");
1145 values[0] = FRAME_EVENT_MASK;
1146 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1148 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1150 //DLOG("Done, EnterNotify re-enabled\n");
1152 x_deco_recurse(con);
1154 xcb_window_t to_focus = focused->frame.id;
1155 if (focused->window != NULL)
1156 to_focus = focused->window->id;
1158 if (focused_id != to_focus) {
1159 if (!focused->mapped) {
1160 DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
1161 /* Invalidate focused_id to correctly focus new windows with the same ID */
1162 focused_id = XCB_NONE;
1164 if (focused->window != NULL &&
1165 focused->window->needs_take_focus &&
1166 focused->window->doesnt_accept_focus) {
1167 DLOG("Updating focus by sending WM_TAKE_FOCUS to window 0x%08x (focused: %p / %s)\n",
1168 to_focus, focused, focused->name);
1169 send_take_focus(to_focus, last_timestamp);
1171 change_ewmh_focus((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE), last_focused);
1173 if (to_focus != last_focused && is_con_attached(focused))
1174 ipc_send_window_event("focus", focused);
1176 DLOG("Updating focus (focused: %p / %s) to X11 window 0x%08x\n", focused, focused->name, to_focus);
1177 /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
1178 * no focus change events for our own focus changes. We only want
1179 * these generated by the clients. */
1180 if (focused->window != NULL) {
1181 values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
1182 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1184 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, last_timestamp);
1185 if (focused->window != NULL) {
1186 values[0] = CHILD_EVENT_MASK;
1187 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1190 change_ewmh_focus((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE), last_focused);
1192 if (to_focus != XCB_NONE && to_focus != last_focused && focused->window != NULL && is_con_attached(focused))
1193 ipc_send_window_event("focus", focused);
1196 focused_id = last_focused = to_focus;
1200 if (focused_id == XCB_NONE) {
1201 /* If we still have no window to focus, we focus the EWMH window instead. We use this rather than the
1202 * root window in order to avoid an X11 fallback mechanism causing a ghosting effect (see #1378). */
1203 DLOG("Still no window focused, better set focus to the EWMH support window (%d)\n", ewmh_window);
1204 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, ewmh_window, last_timestamp);
1205 change_ewmh_focus(XCB_WINDOW_NONE, last_focused);
1207 focused_id = ewmh_window;
1211 DLOG("ENDING CHANGES\n");
1213 /* Disable EnterWindow events for windows which will be unmapped in
1214 * x_push_node_unmaps() now. Unmapping windows happens when switching
1215 * workspaces. We want to avoid getting EnterNotifies during that phase
1216 * because they would screw up our focus. One of these cases is having a
1217 * stack with two windows. If the first window is focused and gets
1218 * unmapped, the second one appears under the cursor and therefore gets an
1219 * EnterNotify event. */
1220 values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
1221 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1222 if (!state->unmap_now)
1224 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1227 /* Push all pending unmaps */
1228 x_push_node_unmaps(con);
1230 /* save the current stack as old stack */
1231 CIRCLEQ_FOREACH(state, &state_head, state) {
1232 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
1233 CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
1235 //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
1236 // DLOG("old stack: 0x%08x\n", state->id);
1243 * Raises the specified container in the internal stack of X windows. The
1244 * next call to x_push_changes() will make the change visible in X11.
1247 void x_raise_con(Con *con) {
1249 state = state_for_frame(con->frame.id);
1250 //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
1252 CIRCLEQ_REMOVE(&state_head, state, state);
1253 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
1257 * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
1258 * of the given name. Used for properly tagging the windows for easily spotting
1259 * i3 windows in xwininfo -root -all.
1262 void x_set_name(Con *con, const char *name) {
1263 struct con_state *state;
1265 if ((state = state_for_frame(con->frame.id)) == NULL) {
1266 ELOG("window state not found\n");
1271 state->name = sstrdup(name);
1275 * Set up the I3_SHMLOG_PATH atom.
1278 void update_shmlog_atom(void) {
1279 if (*shmlogname == '\0') {
1280 xcb_delete_property(conn, root, A_I3_SHMLOG_PATH);
1282 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1283 A_I3_SHMLOG_PATH, A_UTF8_STRING, 8,
1284 strlen(shmlogname), shmlogname);
1289 * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
1292 void x_set_i3_atoms(void) {
1293 pid_t pid = getpid();
1294 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
1295 (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
1296 current_socketpath);
1297 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_PID, XCB_ATOM_CARDINAL, 32, 1, &pid);
1298 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
1299 strlen(current_configpath), current_configpath);
1300 update_shmlog_atom();
1304 * Set warp_to coordinates. This will trigger on the next call to
1308 void x_set_warp_to(Rect *rect) {
1309 if (config.mouse_warping != POINTER_WARPING_NONE)
1314 * Applies the given mask to the event mask of every i3 window decoration X11
1315 * window. This is useful to disable EnterNotify while resizing so that focus
1319 void x_mask_event_mask(uint32_t mask) {
1320 uint32_t values[] = {FRAME_EVENT_MASK & mask};
1323 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1325 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);