2 #define I3__FILE__ "x.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * x.c: Interface to X11, transfers our in-memory state to X11 (see also
10 * render.c). Basically a big state machine.
16 #define MAX(x, y) ((x) > (y) ? (x) : (y))
19 xcb_window_t ewmh_window;
21 /* Stores the X11 window ID of the currently focused window */
22 xcb_window_t focused_id = XCB_NONE;
24 /* Because 'focused_id' might be reset to force input focus, we separately keep
25 * track of the X11 window ID to be able to always tell whether the focused
26 * window actually changed. */
27 static xcb_window_t last_focused = XCB_NONE;
29 /* Stores coordinates to warp mouse pointer to if set */
33 * Describes the X11 state we may modify (map state, position, window stack).
34 * There is one entry per container. The state represents the current situation
35 * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
36 * which represents the order that will be pushed to X11, while old_state_head
37 * represents the current order). It will be updated in x_push_changes().
40 typedef struct con_state {
47 /** The con for which this state is. */
50 /* For reparenting, we have a flag (need_reparent) and the X ID of the old
51 * frame this window was in. The latter is necessary because we need to
52 * ignore UnmapNotify events (by changing the window event mask). */
54 xcb_window_t old_frame;
63 CIRCLEQ_ENTRY(con_state) state;
64 CIRCLEQ_ENTRY(con_state) old_state;
65 TAILQ_ENTRY(con_state) initial_mapping_order;
68 CIRCLEQ_HEAD(state_head, con_state) state_head =
69 CIRCLEQ_HEAD_INITIALIZER(state_head);
71 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
72 CIRCLEQ_HEAD_INITIALIZER(old_state_head);
74 TAILQ_HEAD(initial_mapping_head, con_state) initial_mapping_head =
75 TAILQ_HEAD_INITIALIZER(initial_mapping_head);
78 * Returns the container state for the given frame. This function always
79 * returns a container state (otherwise, there is a bug in the code and the
80 * container state of a container for which x_con_init() was not called was
84 static con_state *state_for_frame(xcb_window_t window) {
86 CIRCLEQ_FOREACH(state, &state_head, state)
87 if (state->id == window)
90 /* TODO: better error handling? */
91 ELOG("No state found\n");
97 * Initializes the X11 part for the given container. Called exactly once for
98 * every container from con_new().
101 void x_con_init(Con *con, uint16_t depth) {
102 /* TODO: maybe create the window when rendering first? we could then even
103 * get the initial geometry right */
108 /* For custom visuals, we need to create a colormap before creating
109 * this window. It will be freed directly after creating the window. */
110 xcb_visualid_t visual = get_visualid_by_depth(depth);
111 xcb_colormap_t win_colormap = xcb_generate_id(conn);
112 xcb_create_colormap_checked(conn, XCB_COLORMAP_ALLOC_NONE, win_colormap, root, visual);
114 /* We explicitly set a background color and border color (even though we
115 * don’t even have a border) because the X11 server requires us to when
116 * using 32 bit color depths, see
117 * http://stackoverflow.com/questions/3645632 */
118 mask |= XCB_CW_BACK_PIXEL;
119 values[0] = root_screen->black_pixel;
121 mask |= XCB_CW_BORDER_PIXEL;
122 values[1] = root_screen->black_pixel;
124 /* our own frames should not be managed */
125 mask |= XCB_CW_OVERRIDE_REDIRECT;
128 /* see include/xcb.h for the FRAME_EVENT_MASK */
129 mask |= XCB_CW_EVENT_MASK;
130 values[3] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
132 mask |= XCB_CW_COLORMAP;
133 values[4] = win_colormap;
135 Rect dims = {-15, -15, 10, 10};
136 xcb_window_t frame_id = create_window(conn, dims, depth, visual, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER, false, mask, values);
137 draw_util_surface_init(conn, &(con->frame), frame_id, get_visualtype_by_id(visual), dims.width, dims.height);
138 xcb_change_property(conn,
139 XCB_PROP_MODE_REPLACE,
144 (strlen("i3-frame") + 1) * 2,
145 "i3-frame\0i3-frame\0");
147 if (win_colormap != XCB_NONE)
148 xcb_free_colormap(conn, win_colormap);
150 struct con_state *state = scalloc(1, sizeof(struct con_state));
151 state->id = con->frame.id;
152 state->mapped = false;
153 state->initial = true;
154 DLOG("Adding window 0x%08x to lists\n", state->id);
155 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
156 CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
157 TAILQ_INSERT_TAIL(&initial_mapping_head, state, initial_mapping_order);
158 DLOG("adding new state for window id 0x%08x\n", state->id);
162 * Re-initializes the associated X window state for this container. You have
163 * to call this when you assign a client to an empty container to ensure that
164 * its state gets updated correctly.
167 void x_reinit(Con *con) {
168 struct con_state *state;
170 if ((state = state_for_frame(con->frame.id)) == NULL) {
171 ELOG("window state not found\n");
175 DLOG("resetting state %p to initial\n", state);
176 state->initial = true;
177 state->child_mapped = false;
179 memset(&(state->window_rect), 0, sizeof(Rect));
183 * Reparents the child window of the given container (necessary for sticky
184 * containers). The reparenting happens in the next call of x_push_changes().
187 void x_reparent_child(Con *con, Con *old) {
188 struct con_state *state;
189 if ((state = state_for_frame(con->frame.id)) == NULL) {
190 ELOG("window state for con not found\n");
194 state->need_reparent = true;
195 state->old_frame = old->frame.id;
199 * Moves a child window from Container src to Container dest.
202 void x_move_win(Con *src, Con *dest) {
203 struct con_state *state_src, *state_dest;
205 if ((state_src = state_for_frame(src->frame.id)) == NULL) {
206 ELOG("window state for src not found\n");
210 if ((state_dest = state_for_frame(dest->frame.id)) == NULL) {
211 ELOG("window state for dest not found\n");
215 state_dest->con = state_src->con;
216 state_src->con = NULL;
218 Rect zero = {0, 0, 0, 0};
219 if (memcmp(&(state_dest->window_rect), &(zero), sizeof(Rect)) == 0) {
220 memcpy(&(state_dest->window_rect), &(state_src->window_rect), sizeof(Rect));
221 DLOG("COPYING RECT\n");
226 * Kills the window decoration associated with the given container.
229 void x_con_kill(Con *con) {
232 draw_util_surface_free(conn, &(con->frame));
233 draw_util_surface_free(conn, &(con->frame_buffer));
234 xcb_destroy_window(conn, con->frame.id);
235 xcb_free_pixmap(conn, con->frame_buffer.id);
236 state = state_for_frame(con->frame.id);
237 CIRCLEQ_REMOVE(&state_head, state, state);
238 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
239 TAILQ_REMOVE(&initial_mapping_head, state, initial_mapping_order);
243 /* Invalidate focused_id to correctly focus new windows with the same ID */
244 focused_id = last_focused = XCB_NONE;
248 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
251 bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
252 xcb_get_property_cookie_t cookie;
253 xcb_icccm_get_wm_protocols_reply_t protocols;
256 cookie = xcb_icccm_get_wm_protocols(conn, window, A_WM_PROTOCOLS);
257 if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
260 /* Check if the client’s protocols have the requested atom set */
261 for (uint32_t i = 0; i < protocols.atoms_len; i++)
262 if (protocols.atoms[i] == atom)
265 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
271 * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
274 void x_window_kill(xcb_window_t window, kill_window_t kill_window) {
275 /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
276 if (!window_supports_protocol(window, A_WM_DELETE_WINDOW)) {
277 if (kill_window == KILL_WINDOW) {
278 LOG("Killing specific window 0x%08x\n", window);
279 xcb_destroy_window(conn, window);
281 LOG("Killing the X11 client which owns window 0x%08x\n", window);
282 xcb_kill_client(conn, window);
287 /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
288 * In order to properly initialize these bytes, we allocate 32 bytes even
289 * though we only need less for an xcb_configure_notify_event_t */
290 void *event = scalloc(32, 1);
291 xcb_client_message_event_t *ev = event;
293 ev->response_type = XCB_CLIENT_MESSAGE;
295 ev->type = A_WM_PROTOCOLS;
297 ev->data.data32[0] = A_WM_DELETE_WINDOW;
298 ev->data.data32[1] = XCB_CURRENT_TIME;
300 LOG("Sending WM_DELETE to the client\n");
301 xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)ev);
306 static void x_draw_title_border(Con *con, struct deco_render_params *p) {
307 assert(con->parent != NULL);
309 Rect *dr = &(con->deco_rect);
310 adjacent_t borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
311 int deco_diff_l = borders_to_hide & ADJ_LEFT_SCREEN_EDGE ? 0 : con->current_border_width;
312 int deco_diff_r = borders_to_hide & ADJ_RIGHT_SCREEN_EDGE ? 0 : con->current_border_width;
313 if (con->parent->layout == L_TABBED ||
314 (con->parent->layout == L_STACKED && TAILQ_NEXT(con, nodes) != NULL)) {
319 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
320 dr->x, dr->y, dr->width, 1);
322 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
323 dr->x + deco_diff_l, dr->y + dr->height - 1, dr->width - (deco_diff_l + deco_diff_r), 1);
326 static void x_draw_decoration_after_title(Con *con, struct deco_render_params *p) {
327 assert(con->parent != NULL);
329 Rect *dr = &(con->deco_rect);
330 Rect br = con_border_style_rect(con);
332 /* Redraw the right border to cut off any text that went past it.
333 * This is necessary when the text was drawn using XCB since cutting text off
334 * automatically does not work there. For pango rendering, this isn't necessary. */
335 if (!font_is_pango()) {
336 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->background,
337 dr->x + dr->width + br.width, dr->y, -br.width, dr->height);
340 /* Draw a 1px separator line before and after every tab, so that tabs can
341 * be easily distinguished. */
342 if (con->parent->layout == L_TABBED) {
344 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
345 dr->x, dr->y, 1, dr->height);
348 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
349 dr->x + dr->width - 1, dr->y, 1, dr->height);
352 /* Redraw the border. */
353 x_draw_title_border(con, p);
357 * Draws the decoration of the given container onto its parent.
360 void x_draw_decoration(Con *con) {
361 Con *parent = con->parent;
362 bool leaf = con_is_leaf(con);
364 /* This code needs to run for:
366 * • non-leaf containers which are in a stacked/tabbed container
368 * It does not need to run for:
369 * • direct children of outputs or dockareas
370 * • floating containers (they don’t have a decoration)
373 parent->layout != L_STACKED &&
374 parent->layout != L_TABBED) ||
375 parent->type == CT_OUTPUT ||
376 parent->type == CT_DOCKAREA ||
377 con->type == CT_FLOATING_CON)
380 /* Skip containers whose height is 0 (for example empty dockareas) */
381 if (con->rect.height == 0)
384 /* Skip containers whose pixmap has not yet been created (can happen when
385 * decoration rendering happens recursively for a window for which
386 * x_push_node() was not yet called) */
387 if (leaf && con->frame_buffer.id == XCB_NONE)
390 /* 1: build deco_params and compare with cache */
391 struct deco_render_params *p = scalloc(1, sizeof(struct deco_render_params));
393 /* find out which colors to use */
395 p->color = &config.client.urgent;
396 else if (con == focused || con_inside_focused(con))
397 p->color = &config.client.focused;
398 else if (con == TAILQ_FIRST(&(parent->focus_head)))
399 p->color = &config.client.focused_inactive;
401 p->color = &config.client.unfocused;
403 p->border_style = con_border_style(con);
405 Rect *r = &(con->rect);
406 Rect *w = &(con->window_rect);
407 p->con_rect = (struct width_height){r->width, r->height};
408 p->con_window_rect = (struct width_height){w->width, w->height};
409 p->con_deco_rect = con->deco_rect;
410 p->background = config.client.background;
411 p->con_is_leaf = con_is_leaf(con);
412 p->parent_layout = con->parent->layout;
414 if (con->deco_render_params != NULL &&
415 (con->window == NULL || !con->window->name_x_changed) &&
416 !parent->pixmap_recreated &&
417 !con->pixmap_recreated &&
418 !con->mark_changed &&
419 memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
425 while ((next = TAILQ_NEXT(next, nodes))) {
426 FREE(next->deco_render_params);
429 FREE(con->deco_render_params);
430 con->deco_render_params = p;
432 if (con->window != NULL && con->window->name_x_changed)
433 con->window->name_x_changed = false;
435 parent->pixmap_recreated = false;
436 con->pixmap_recreated = false;
437 con->mark_changed = false;
439 /* 2: draw the client.background, but only for the parts around the window_rect */
440 if (con->window != NULL) {
442 draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
443 0, 0, r->width, w->y);
445 draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
446 0, w->y + w->height, r->width, r->height - (w->y + w->height));
448 draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
449 0, 0, w->x, r->height);
451 draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
452 w->x + w->width, 0, r->width - (w->x + w->width), r->height);
455 /* 3: draw a rectangle in border color around the client */
456 if (p->border_style != BS_NONE && p->con_is_leaf) {
457 /* We might hide some borders adjacent to the screen-edge */
458 adjacent_t borders_to_hide = ADJ_NONE;
459 borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
461 Rect br = con_border_style_rect(con);
463 DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
464 DLOG("border_rect spans (%d, %d) with %d x %d\n", br.x, br.y, br.width, br.height);
465 DLOG("window_rect spans (%d, %d) with %d x %d\n", con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
468 /* These rectangles represent the border around the child window
469 * (left, bottom and right part). We don’t just fill the whole
470 * rectangle because some childs are not freely resizable and we want
471 * their background color to "shine through". */
472 if (!(borders_to_hide & ADJ_LEFT_SCREEN_EDGE)) {
473 draw_util_rectangle(conn, &(con->frame_buffer), p->color->child_border, 0, 0, br.x, r->height);
475 if (!(borders_to_hide & ADJ_RIGHT_SCREEN_EDGE)) {
476 draw_util_rectangle(conn, &(con->frame_buffer),
477 p->color->child_border, r->width + (br.width + br.x), 0,
478 -(br.width + br.x), r->height);
480 if (!(borders_to_hide & ADJ_LOWER_SCREEN_EDGE)) {
481 draw_util_rectangle(conn, &(con->frame_buffer),
482 p->color->child_border, br.x, r->height + (br.height + br.y),
483 r->width + br.width, -(br.height + br.y));
485 /* pixel border needs an additional line at the top */
486 if (p->border_style == BS_PIXEL && !(borders_to_hide & ADJ_UPPER_SCREEN_EDGE)) {
487 draw_util_rectangle(conn, &(con->frame_buffer),
488 p->color->child_border, br.x, 0, r->width + br.width, br.y);
491 /* Highlight the side of the border at which the next window will be
492 * opened if we are rendering a single window within a split container
493 * (which is undistinguishable from a single window outside a split
494 * container otherwise. */
495 if (TAILQ_NEXT(con, nodes) == NULL &&
496 TAILQ_PREV(con, nodes_head, nodes) == NULL &&
497 con->parent->type != CT_FLOATING_CON) {
498 if (p->parent_layout == L_SPLITH) {
499 draw_util_rectangle(conn, &(con->frame_buffer), p->color->indicator,
500 r->width + (br.width + br.x), br.y, -(br.width + br.x), r->height + br.height);
501 } else if (p->parent_layout == L_SPLITV) {
502 draw_util_rectangle(conn, &(con->frame_buffer), p->color->indicator,
503 br.x, r->height + (br.height + br.y), r->width + br.width, -(br.height + br.y));
508 /* if this is a borderless/1pixel window, we don’t need to render the
510 if (p->border_style != BS_NORMAL)
513 /* If the parent hasn't been set up yet, skip the decoration rendering
515 if (parent->frame_buffer.id == XCB_NONE)
518 /* For the first child, we clear the parent pixmap to ensure there's no
519 * garbage left on there. This is important to avoid tearing when using
521 if (con == TAILQ_FIRST(&(con->parent->nodes_head))) {
522 draw_util_clear_surface(conn, &(con->parent->frame_buffer), COLOR_TRANSPARENT);
523 FREE(con->parent->deco_render_params);
526 /* 4: paint the bar */
527 draw_util_rectangle(conn, &(parent->frame_buffer), p->color->background,
528 con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height);
530 /* 5: draw two unconnected horizontal lines in border color */
531 x_draw_title_border(con, p);
533 /* 6: draw the title */
534 int text_offset_y = (con->deco_rect.height - config.font.height) / 2;
536 struct Window *win = con->window;
539 if (con->title_format == NULL) {
541 char *tree = con_get_tree_representation(con);
542 sasprintf(&_title, "i3: %s", tree);
545 title = i3string_from_utf8(_title);
548 title = con_parse_title_format(con);
551 draw_util_text(title, &(parent->frame_buffer),
552 p->color->text, p->color->background,
553 con->deco_rect.x + 2, con->deco_rect.y + text_offset_y,
554 con->deco_rect.width - 2);
555 I3STRING_FREE(title);
560 if (win->name == NULL)
563 int indent_level = 0,
565 Con *il_parent = parent;
566 if (il_parent->layout != L_STACKED) {
568 //DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
569 if (il_parent->layout == L_STACKED)
571 if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
573 il_parent = il_parent->parent;
577 //DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
578 int indent_px = (indent_level * 5) * indent_mult;
581 if (config.show_marks && !TAILQ_EMPTY(&(con->marks_head))) {
582 char *formatted_mark = sstrdup("");
583 bool had_visible_mark = false;
586 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
587 if (mark->name[0] == '_')
589 had_visible_mark = true;
592 sasprintf(&buf, "%s[%s]", formatted_mark, mark->name);
593 free(formatted_mark);
594 formatted_mark = buf;
597 if (had_visible_mark) {
598 i3String *mark = i3string_from_utf8(formatted_mark);
599 mark_width = predict_text_width(mark);
601 draw_util_text(mark, &(parent->frame_buffer),
602 p->color->text, p->color->background,
603 con->deco_rect.x + con->deco_rect.width - mark_width - logical_px(2),
604 con->deco_rect.y + text_offset_y, mark_width);
609 FREE(formatted_mark);
612 i3String *title = con->title_format == NULL ? win->name : con_parse_title_format(con);
613 draw_util_text(title, &(parent->frame_buffer),
614 p->color->text, p->color->background,
615 con->deco_rect.x + logical_px(2) + indent_px, con->deco_rect.y + text_offset_y,
616 con->deco_rect.width - logical_px(2) - indent_px - mark_width - logical_px(2));
617 if (con->title_format != NULL)
618 I3STRING_FREE(title);
621 x_draw_decoration_after_title(con, p);
623 draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
627 * Recursively calls x_draw_decoration. This cannot be done in x_push_node
628 * because x_push_node uses focus order to recurse (see the comment above)
629 * while drawing the decoration needs to happen in the actual order.
632 void x_deco_recurse(Con *con) {
634 bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
635 TAILQ_EMPTY(&(con->floating_head));
636 con_state *state = state_for_frame(con->frame.id);
639 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
640 x_deco_recurse(current);
642 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
643 x_deco_recurse(current);
646 draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
650 if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
651 (!leaf || con->mapped))
652 x_draw_decoration(con);
656 * Sets or removes the _NET_WM_STATE_HIDDEN property on con if necessary.
659 static void set_hidden_state(Con *con) {
660 if (con->window == NULL) {
664 con_state *state = state_for_frame(con->frame.id);
665 bool should_be_hidden = con_is_hidden(con);
666 if (should_be_hidden == state->is_hidden)
669 if (should_be_hidden) {
670 DLOG("setting _NET_WM_STATE_HIDDEN for con = %p\n", con);
671 xcb_add_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
673 DLOG("removing _NET_WM_STATE_HIDDEN for con = %p\n", con);
674 xcb_remove_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
677 state->is_hidden = should_be_hidden;
681 * This function pushes the properties of each node of the layout tree to
682 * X11 if they have changed (like the map state, position of the window, …).
683 * It recursively traverses all children of the given node.
686 void x_push_node(Con *con) {
689 Rect rect = con->rect;
691 //DLOG("Pushing changes for node %p / %s\n", con, con->name);
692 state = state_for_frame(con->frame.id);
694 if (state->name != NULL) {
695 DLOG("pushing name %s for con %p\n", state->name, con);
697 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame.id,
698 XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(state->name), state->name);
702 if (con->window == NULL) {
703 /* Calculate the height of all window decorations which will be drawn on to
705 uint32_t max_y = 0, max_height = 0;
706 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
707 Rect *dr = &(current->deco_rect);
708 if (dr->y >= max_y && dr->height >= max_height) {
710 max_height = dr->height;
713 rect.height = max_y + max_height;
714 if (rect.height == 0)
718 /* reparent the child window (when the window was moved due to a sticky
720 if (state->need_reparent && con->window != NULL) {
721 DLOG("Reparenting child window\n");
723 /* Temporarily set the event masks to XCB_NONE so that we won’t get
724 * UnmapNotify events (otherwise the handler would close the container).
725 * These events are generated automatically when reparenting. */
726 uint32_t values[] = {XCB_NONE};
727 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
728 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
730 xcb_reparent_window(conn, con->window->id, con->frame.id, 0, 0);
732 values[0] = FRAME_EVENT_MASK;
733 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
734 values[0] = CHILD_EVENT_MASK;
735 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
737 state->old_frame = XCB_NONE;
738 state->need_reparent = false;
741 DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
742 con, con->window->id, con->ignore_unmap);
745 /* The pixmap of a borderless leaf container will not be used except
746 * for the titlebar in a stack or tabs (issue #1013). */
747 bool is_pixmap_needed = (con->border_style != BS_NONE ||
749 con->parent->layout == L_STACKED ||
750 con->parent->layout == L_TABBED);
752 /* The root con and output cons will never require a pixmap. In particular for the
753 * __i3 output, this will likely not work anyway because it might be ridiculously
754 * large, causing an XCB_ALLOC error. */
755 if (con->type == CT_ROOT || con->type == CT_OUTPUT)
756 is_pixmap_needed = false;
758 bool fake_notify = false;
759 /* Set new position if rect changed (and if height > 0) or if the pixmap
760 * needs to be recreated */
761 if ((is_pixmap_needed && con->frame_buffer.id == XCB_NONE) || (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
763 /* We first create the new pixmap, then render to it, set it as the
764 * background and only afterwards change the window size. This reduces
767 /* As the pixmap only depends on the size and not on the position, it
768 * is enough to check if width/height have changed. Also, we don’t
769 * create a pixmap at all when the window is actually not visible
770 * (height == 0) or when it is not needed. */
771 bool has_rect_changed = (state->rect.width != rect.width || state->rect.height != rect.height);
773 /* Check if the container has an unneeded pixmap left over from
774 * previously having a border or titlebar. */
775 if (!is_pixmap_needed && con->frame_buffer.id != XCB_NONE) {
776 draw_util_surface_free(conn, &(con->frame_buffer));
777 xcb_free_pixmap(conn, con->frame_buffer.id);
778 con->frame_buffer.id = XCB_NONE;
781 if (is_pixmap_needed && (has_rect_changed || con->frame_buffer.id == XCB_NONE)) {
782 if (con->frame_buffer.id == XCB_NONE) {
783 con->frame_buffer.id = xcb_generate_id(conn);
785 draw_util_surface_free(conn, &(con->frame_buffer));
786 xcb_free_pixmap(conn, con->frame_buffer.id);
789 uint16_t win_depth = root_depth;
791 win_depth = con->window->depth;
793 /* Ensure we have valid dimensions for our surface. */
794 // TODO This is probably a bug in the condition above as we should never enter this path
795 // for height == 0. Also, we should probably handle width == 0 the same way.
796 int width = MAX((int32_t)rect.width, 1);
797 int height = MAX((int32_t)rect.height, 1);
799 xcb_create_pixmap_checked(conn, win_depth, con->frame_buffer.id, con->frame.id, width, height);
800 draw_util_surface_init(conn, &(con->frame_buffer), con->frame_buffer.id,
801 get_visualtype_by_id(get_visualid_by_depth(win_depth)), width, height);
803 /* For the graphics context, we disable GraphicsExposure events.
804 * Those will be sent when a CopyArea request cannot be fulfilled
805 * properly due to parts of the source being unmapped or otherwise
806 * unavailable. Since we always copy from pixmaps to windows, this
807 * is not a concern for us. */
808 xcb_change_gc(conn, con->frame_buffer.gc, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
810 draw_util_surface_set_size(&(con->frame), width, height);
811 con->pixmap_recreated = true;
813 /* Don’t render the decoration for windows inside a stack which are
814 * not visible right now */
815 // TODO Should this work the same way for L_TABBED?
817 con->parent->layout != L_STACKED ||
818 TAILQ_FIRST(&(con->parent->focus_head)) == con)
819 /* Render the decoration now to make the correct decoration visible
820 * from the very first moment. Later calls will be cached, so this
821 * doesn’t hurt performance. */
825 DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
826 /* flush to ensure that the following commands are sent in a single
827 * buffer and will be processed directly afterwards (the contents of a
828 * window get lost when resizing it, therefore we want to provide it as
829 * fast as possible) */
831 xcb_set_window_rect(conn, con->frame.id, rect);
832 if (con->frame_buffer.id != XCB_NONE) {
833 draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
837 memcpy(&(state->rect), &rect, sizeof(Rect));
841 /* dito, but for child windows */
842 if (con->window != NULL &&
843 memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
844 DLOG("setting window rect (%d, %d, %d, %d)\n",
845 con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
846 xcb_set_window_rect(conn, con->window->id, con->window_rect);
847 memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
851 /* Map if map state changed, also ensure that the child window
852 * is changed if we are mapped and there is a new, unmapped child window.
853 * Unmaps are handled in x_push_node_unmaps(). */
854 if ((state->mapped != con->mapped || (con->window != NULL && !state->child_mapped)) &&
856 xcb_void_cookie_t cookie;
858 if (con->window != NULL) {
859 /* Set WM_STATE_NORMAL because GTK applications don’t want to
860 * drag & drop if we don’t. Also, xprop(1) needs it. */
861 long data[] = {XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE};
862 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
863 A_WM_STATE, A_WM_STATE, 32, 2, data);
867 if (!state->child_mapped && con->window != NULL) {
868 cookie = xcb_map_window(conn, con->window->id);
870 /* We are interested in EnterNotifys as soon as the window is
872 values[0] = CHILD_EVENT_MASK;
873 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
874 DLOG("mapping child window (serial %d)\n", cookie.sequence);
875 state->child_mapped = true;
878 cookie = xcb_map_window(conn, con->frame.id);
880 values[0] = FRAME_EVENT_MASK;
881 xcb_change_window_attributes(conn, con->frame.id, XCB_CW_EVENT_MASK, values);
883 /* copy the pixmap contents to the frame window immediately after mapping */
884 if (con->frame_buffer.id != XCB_NONE) {
885 draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
889 DLOG("mapping container %08x (serial %d)\n", con->frame.id, cookie.sequence);
890 state->mapped = con->mapped;
893 state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
896 DLOG("Sending fake configure notify\n");
897 fake_absolute_configure_notify(con);
900 set_hidden_state(con);
902 /* Handle all children and floating windows of this node. We recurse
903 * in focus order to display the focused client in a stack first when
904 * switching workspaces (reduces flickering). */
905 TAILQ_FOREACH(current, &(con->focus_head), focused)
906 x_push_node(current);
910 * Same idea as in x_push_node(), but this function only unmaps windows. It is
911 * necessary to split this up to handle new fullscreen clients properly: The
912 * new window needs to be mapped and focus needs to be set *before* the
913 * underlying windows are unmapped. Otherwise, focus will revert to the
914 * PointerRoot and will then be set to the new window, generating unnecessary
915 * FocusIn/FocusOut events.
918 static void x_push_node_unmaps(Con *con) {
922 //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
923 state = state_for_frame(con->frame.id);
925 /* map/unmap if map state changed, also ensure that the child window
926 * is changed if we are mapped *and* in initial state (meaning the
927 * container was empty before, but now got a child) */
928 if (state->unmap_now) {
929 xcb_void_cookie_t cookie;
930 if (con->window != NULL) {
931 /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
932 long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
933 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
934 A_WM_STATE, A_WM_STATE, 32, 2, data);
937 cookie = xcb_unmap_window(conn, con->frame.id);
938 DLOG("unmapping container %p / %s (serial %d)\n", con, con->name, cookie.sequence);
939 /* we need to increase ignore_unmap for this container (if it
940 * contains a window) and for every window "under" this one which
941 * contains a window */
942 if (con->window != NULL) {
944 DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame.id, con->ignore_unmap);
946 state->mapped = con->mapped;
949 /* handle all children and floating windows of this node */
950 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
951 x_push_node_unmaps(current);
953 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
954 x_push_node_unmaps(current);
958 * Returns true if the given container is currently attached to its parent.
960 * TODO: Remove once #1185 has been fixed
962 static bool is_con_attached(Con *con) {
963 if (con->parent == NULL)
967 TAILQ_FOREACH(current, &(con->parent->nodes_head), nodes) {
976 * Pushes all changes (state of each node, see x_push_node() and the window
979 * NOTE: We need to push the stack first so that the windows have the correct
980 * stacking order. This is relevant for workspace switching where we map the
981 * windows because mapping may generate EnterNotify events. When they are
982 * generated in the wrong order, this will cause focus problems when switching
986 void x_push_changes(Con *con) {
988 xcb_query_pointer_cookie_t pointercookie;
990 /* If we need to warp later, we request the pointer position as soon as possible */
992 pointercookie = xcb_query_pointer(conn, root);
995 DLOG("-- PUSHING WINDOW STACK --\n");
996 //DLOG("Disabling EnterNotify\n");
997 /* We need to keep SubstructureRedirect around, otherwise clients can send
998 * ConfigureWindow requests and get them applied directly instead of having
999 * them become ConfigureRequests that i3 handles. */
1000 uint32_t values[1] = {XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT};
1001 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1003 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1005 //DLOG("Done, EnterNotify disabled\n");
1006 bool order_changed = false;
1007 bool stacking_changed = false;
1009 /* count first, necessary to (re)allocate memory for the bottom-to-top
1010 * stack afterwards */
1012 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
1013 if (con_has_managed_window(state->con))
1016 /* The bottom-to-top window stack of all windows which are managed by i3.
1017 * Used for x_get_window_stack(). */
1018 static xcb_window_t *client_list_windows = NULL;
1019 static int client_list_count = 0;
1021 if (cnt != client_list_count) {
1022 client_list_windows = srealloc(client_list_windows, sizeof(xcb_window_t) * cnt);
1023 client_list_count = cnt;
1026 xcb_window_t *walk = client_list_windows;
1028 /* X11 correctly represents the stack if we push it from bottom to top */
1029 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1030 if (con_has_managed_window(state->con))
1031 memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
1033 //DLOG("stack: 0x%08x\n", state->id);
1034 con_state *prev = CIRCLEQ_PREV(state, state);
1035 con_state *old_prev = CIRCLEQ_PREV(state, old_state);
1036 if (prev != old_prev)
1037 order_changed = true;
1038 if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
1039 stacking_changed = true;
1040 //DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
1042 mask |= XCB_CONFIG_WINDOW_SIBLING;
1043 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
1044 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
1046 xcb_configure_window(conn, prev->id, mask, values);
1048 state->initial = false;
1051 /* If we re-stacked something (or a new window appeared), we need to update
1052 * the _NET_CLIENT_LIST and _NET_CLIENT_LIST_STACKING hints */
1053 if (stacking_changed) {
1054 DLOG("Client list changed (%i clients)\n", cnt);
1055 ewmh_update_client_list_stacking(client_list_windows, client_list_count);
1057 walk = client_list_windows;
1059 /* reorder by initial mapping */
1060 TAILQ_FOREACH(state, &initial_mapping_head, initial_mapping_order) {
1061 if (con_has_managed_window(state->con))
1062 *walk++ = state->con->window->id;
1065 ewmh_update_client_list(client_list_windows, client_list_count);
1068 DLOG("PUSHING CHANGES\n");
1072 xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
1073 if (!pointerreply) {
1074 ELOG("Could not query pointer position, not warping pointer\n");
1076 int mid_x = warp_to->x + (warp_to->width / 2);
1077 int mid_y = warp_to->y + (warp_to->height / 2);
1079 Output *current = get_output_containing(pointerreply->root_x, pointerreply->root_y);
1080 Output *target = get_output_containing(mid_x, mid_y);
1081 if (current != target) {
1082 /* Ignore MotionNotify events generated by warping */
1083 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT});
1084 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
1085 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){ROOT_EVENT_MASK});
1093 //DLOG("Re-enabling EnterNotify\n");
1094 values[0] = FRAME_EVENT_MASK;
1095 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1097 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1099 //DLOG("Done, EnterNotify re-enabled\n");
1101 x_deco_recurse(con);
1103 xcb_window_t to_focus = focused->frame.id;
1104 if (focused->window != NULL)
1105 to_focus = focused->window->id;
1107 if (focused_id != to_focus) {
1108 if (!focused->mapped) {
1109 DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
1110 /* Invalidate focused_id to correctly focus new windows with the same ID */
1111 focused_id = XCB_NONE;
1113 if (focused->window != NULL &&
1114 focused->window->needs_take_focus &&
1115 focused->window->doesnt_accept_focus) {
1116 DLOG("Updating focus by sending WM_TAKE_FOCUS to window 0x%08x (focused: %p / %s)\n",
1117 to_focus, focused, focused->name);
1118 send_take_focus(to_focus, last_timestamp);
1120 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1122 if (to_focus != last_focused && is_con_attached(focused))
1123 ipc_send_window_event("focus", focused);
1125 DLOG("Updating focus (focused: %p / %s) to X11 window 0x%08x\n", focused, focused->name, to_focus);
1126 /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
1127 * no focus change events for our own focus changes. We only want
1128 * these generated by the clients. */
1129 if (focused->window != NULL) {
1130 values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
1131 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1133 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
1134 if (focused->window != NULL) {
1135 values[0] = CHILD_EVENT_MASK;
1136 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1139 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1141 if (to_focus != XCB_NONE && to_focus != last_focused && focused->window != NULL && is_con_attached(focused))
1142 ipc_send_window_event("focus", focused);
1145 focused_id = last_focused = to_focus;
1149 if (focused_id == XCB_NONE) {
1150 /* If we still have no window to focus, we focus the EWMH window instead. We use this rather than the
1151 * root window in order to avoid an X11 fallback mechanism causing a ghosting effect (see #1378). */
1152 DLOG("Still no window focused, better set focus to the EWMH support window (%d)\n", ewmh_window);
1153 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, ewmh_window, XCB_CURRENT_TIME);
1154 ewmh_update_active_window(XCB_WINDOW_NONE);
1155 focused_id = ewmh_window;
1159 DLOG("ENDING CHANGES\n");
1161 /* Disable EnterWindow events for windows which will be unmapped in
1162 * x_push_node_unmaps() now. Unmapping windows happens when switching
1163 * workspaces. We want to avoid getting EnterNotifies during that phase
1164 * because they would screw up our focus. One of these cases is having a
1165 * stack with two windows. If the first window is focused and gets
1166 * unmapped, the second one appears under the cursor and therefore gets an
1167 * EnterNotify event. */
1168 values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
1169 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1170 if (!state->unmap_now)
1172 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1175 /* Push all pending unmaps */
1176 x_push_node_unmaps(con);
1178 /* save the current stack as old stack */
1179 CIRCLEQ_FOREACH(state, &state_head, state) {
1180 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
1181 CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
1183 //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
1184 // DLOG("old stack: 0x%08x\n", state->id);
1191 * Raises the specified container in the internal stack of X windows. The
1192 * next call to x_push_changes() will make the change visible in X11.
1195 void x_raise_con(Con *con) {
1197 state = state_for_frame(con->frame.id);
1198 //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
1200 CIRCLEQ_REMOVE(&state_head, state, state);
1201 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
1205 * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
1206 * of the given name. Used for properly tagging the windows for easily spotting
1207 * i3 windows in xwininfo -root -all.
1210 void x_set_name(Con *con, const char *name) {
1211 struct con_state *state;
1213 if ((state = state_for_frame(con->frame.id)) == NULL) {
1214 ELOG("window state not found\n");
1219 state->name = sstrdup(name);
1223 * Set up the I3_SHMLOG_PATH atom.
1226 void update_shmlog_atom() {
1227 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1228 A_I3_SHMLOG_PATH, A_UTF8_STRING, 8,
1229 strlen(shmlogname), shmlogname);
1233 * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
1236 void x_set_i3_atoms(void) {
1237 pid_t pid = getpid();
1238 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
1239 (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
1240 current_socketpath);
1241 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_PID, XCB_ATOM_CARDINAL, 32, 1, &pid);
1242 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
1243 strlen(current_configpath), current_configpath);
1244 update_shmlog_atom();
1248 * Set warp_to coordinates. This will trigger on the next call to
1252 void x_set_warp_to(Rect *rect) {
1253 if (config.mouse_warping != POINTER_WARPING_NONE)
1258 * Applies the given mask to the event mask of every i3 window decoration X11
1259 * window. This is useful to disable EnterNotify while resizing so that focus
1263 void x_mask_event_mask(uint32_t mask) {
1264 uint32_t values[] = {FRAME_EVENT_MASK & mask};
1267 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1269 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);