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.
15 xcb_window_t ewmh_window;
17 /* Stores the X11 window ID of the currently focused window */
18 xcb_window_t focused_id = XCB_NONE;
20 /* Because 'focused_id' might be reset to force input focus, we separately keep
21 * track of the X11 window ID to be able to always tell whether the focused
22 * window actually changed. */
23 static xcb_window_t last_focused = XCB_NONE;
25 /* Stores coordinates to warp mouse pointer to if set */
29 * Describes the X11 state we may modify (map state, position, window stack).
30 * There is one entry per container. The state represents the current situation
31 * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
32 * which represents the order that will be pushed to X11, while old_state_head
33 * represents the current order). It will be updated in x_push_changes().
36 typedef struct con_state {
43 /** The con for which this state is. */
46 /* For reparenting, we have a flag (need_reparent) and the X ID of the old
47 * frame this window was in. The latter is necessary because we need to
48 * ignore UnmapNotify events (by changing the window event mask). */
50 xcb_window_t old_frame;
59 CIRCLEQ_ENTRY(con_state) state;
60 CIRCLEQ_ENTRY(con_state) old_state;
61 TAILQ_ENTRY(con_state) initial_mapping_order;
64 CIRCLEQ_HEAD(state_head, con_state) state_head =
65 CIRCLEQ_HEAD_INITIALIZER(state_head);
67 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
68 CIRCLEQ_HEAD_INITIALIZER(old_state_head);
70 TAILQ_HEAD(initial_mapping_head, con_state) initial_mapping_head =
71 TAILQ_HEAD_INITIALIZER(initial_mapping_head);
74 * Returns the container state for the given frame. This function always
75 * returns a container state (otherwise, there is a bug in the code and the
76 * container state of a container for which x_con_init() was not called was
80 static con_state *state_for_frame(xcb_window_t window) {
82 CIRCLEQ_FOREACH(state, &state_head, state)
83 if (state->id == window)
86 /* TODO: better error handling? */
87 ELOG("No state found\n");
93 * Initializes the X11 part for the given container. Called exactly once for
94 * every container from con_new().
97 void x_con_init(Con *con, uint16_t depth) {
98 /* TODO: maybe create the window when rendering first? we could then even
99 * get the initial geometry right */
104 xcb_visualid_t visual = XCB_COPY_FROM_PARENT;
105 xcb_colormap_t win_colormap = XCB_NONE;
106 if (depth != root_depth && depth != XCB_COPY_FROM_PARENT) {
107 /* For custom visuals, we need to create a colormap before creating
108 * this window. It will be freed directly after creating the window. */
109 visual = get_visualid_by_depth(depth);
110 win_colormap = xcb_generate_id(conn);
111 xcb_create_colormap_checked(conn, XCB_COLORMAP_ALLOC_NONE, win_colormap, root, visual);
113 /* We explicitly set a background color and border color (even though we
114 * don’t even have a border) because the X11 server requires us to when
115 * using 32 bit color depths, see
116 * http://stackoverflow.com/questions/3645632 */
117 mask |= XCB_CW_BACK_PIXEL;
118 values[0] = root_screen->black_pixel;
120 mask |= XCB_CW_BORDER_PIXEL;
121 values[1] = root_screen->black_pixel;
123 /* our own frames should not be managed */
124 mask |= XCB_CW_OVERRIDE_REDIRECT;
127 /* see include/xcb.h for the FRAME_EVENT_MASK */
128 mask |= XCB_CW_EVENT_MASK;
129 values[3] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
131 mask |= XCB_CW_COLORMAP;
132 values[4] = win_colormap;
134 /* our own frames should not be managed */
135 mask = XCB_CW_OVERRIDE_REDIRECT;
138 /* see include/xcb.h for the FRAME_EVENT_MASK */
139 mask |= XCB_CW_EVENT_MASK;
140 values[1] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
142 mask |= XCB_CW_COLORMAP;
143 values[2] = colormap;
146 Rect dims = {-15, -15, 10, 10};
147 xcb_window_t frame_id = create_window(conn, dims, depth, visual, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER, false, mask, values);
148 draw_util_surface_init(conn, &(con->frame), frame_id, get_visualtype_by_id(visual), dims.width, dims.height);
149 xcb_change_property(conn,
150 XCB_PROP_MODE_REPLACE,
155 (strlen("i3-frame") + 1) * 2,
156 "i3-frame\0i3-frame\0");
158 if (win_colormap != XCB_NONE)
159 xcb_free_colormap(conn, win_colormap);
161 struct con_state *state = scalloc(1, sizeof(struct con_state));
162 state->id = con->frame.id;
163 state->mapped = false;
164 state->initial = true;
165 DLOG("Adding window 0x%08x to lists\n", state->id);
166 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
167 CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
168 TAILQ_INSERT_TAIL(&initial_mapping_head, state, initial_mapping_order);
169 DLOG("adding new state for window id 0x%08x\n", state->id);
173 * Re-initializes the associated X window state for this container. You have
174 * to call this when you assign a client to an empty container to ensure that
175 * its state gets updated correctly.
178 void x_reinit(Con *con) {
179 struct con_state *state;
181 if ((state = state_for_frame(con->frame.id)) == NULL) {
182 ELOG("window state not found\n");
186 DLOG("resetting state %p to initial\n", state);
187 state->initial = true;
188 state->child_mapped = false;
190 memset(&(state->window_rect), 0, sizeof(Rect));
194 * Reparents the child window of the given container (necessary for sticky
195 * containers). The reparenting happens in the next call of x_push_changes().
198 void x_reparent_child(Con *con, Con *old) {
199 struct con_state *state;
200 if ((state = state_for_frame(con->frame.id)) == NULL) {
201 ELOG("window state for con not found\n");
205 state->need_reparent = true;
206 state->old_frame = old->frame.id;
210 * Moves a child window from Container src to Container dest.
213 void x_move_win(Con *src, Con *dest) {
214 struct con_state *state_src, *state_dest;
216 if ((state_src = state_for_frame(src->frame.id)) == NULL) {
217 ELOG("window state for src not found\n");
221 if ((state_dest = state_for_frame(dest->frame.id)) == NULL) {
222 ELOG("window state for dest not found\n");
226 state_dest->con = state_src->con;
227 state_src->con = NULL;
229 Rect zero = {0, 0, 0, 0};
230 if (memcmp(&(state_dest->window_rect), &(zero), sizeof(Rect)) == 0) {
231 memcpy(&(state_dest->window_rect), &(state_src->window_rect), sizeof(Rect));
232 DLOG("COPYING RECT\n");
237 * Kills the window decoration associated with the given container.
240 void x_con_kill(Con *con) {
243 draw_util_surface_free(conn, &(con->frame));
244 draw_util_surface_free(conn, &(con->frame_buffer));
245 xcb_destroy_window(conn, con->frame.id);
246 xcb_free_pixmap(conn, con->frame_buffer.id);
247 state = state_for_frame(con->frame.id);
248 CIRCLEQ_REMOVE(&state_head, state, state);
249 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
250 TAILQ_REMOVE(&initial_mapping_head, state, initial_mapping_order);
254 /* Invalidate focused_id to correctly focus new windows with the same ID */
255 focused_id = last_focused = XCB_NONE;
259 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
262 bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
263 xcb_get_property_cookie_t cookie;
264 xcb_icccm_get_wm_protocols_reply_t protocols;
267 cookie = xcb_icccm_get_wm_protocols(conn, window, A_WM_PROTOCOLS);
268 if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
271 /* Check if the client’s protocols have the requested atom set */
272 for (uint32_t i = 0; i < protocols.atoms_len; i++)
273 if (protocols.atoms[i] == atom)
276 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
282 * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
285 void x_window_kill(xcb_window_t window, kill_window_t kill_window) {
286 /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
287 if (!window_supports_protocol(window, A_WM_DELETE_WINDOW)) {
288 if (kill_window == KILL_WINDOW) {
289 LOG("Killing specific window 0x%08x\n", window);
290 xcb_destroy_window(conn, window);
292 LOG("Killing the X11 client which owns window 0x%08x\n", window);
293 xcb_kill_client(conn, window);
298 /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
299 * In order to properly initialize these bytes, we allocate 32 bytes even
300 * though we only need less for an xcb_configure_notify_event_t */
301 void *event = scalloc(32, 1);
302 xcb_client_message_event_t *ev = event;
304 ev->response_type = XCB_CLIENT_MESSAGE;
306 ev->type = A_WM_PROTOCOLS;
308 ev->data.data32[0] = A_WM_DELETE_WINDOW;
309 ev->data.data32[1] = XCB_CURRENT_TIME;
311 LOG("Sending WM_DELETE to the client\n");
312 xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)ev);
317 static void x_draw_decoration_border(Con *con, struct deco_render_params *p) {
318 assert(con->parent != NULL);
320 Rect *dr = &(con->deco_rect);
321 adjacent_t borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
322 int deco_diff_l = borders_to_hide & ADJ_LEFT_SCREEN_EDGE ? 0 : con->current_border_width;
323 int deco_diff_r = borders_to_hide & ADJ_RIGHT_SCREEN_EDGE ? 0 : con->current_border_width;
324 if (con->parent->layout == L_TABBED ||
325 (con->parent->layout == L_STACKED && TAILQ_NEXT(con, nodes) != NULL)) {
330 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
331 dr->x, dr->y, dr->width, 1);
333 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
334 dr->x + deco_diff_l, dr->y + dr->height - 1, dr->width - (deco_diff_l + deco_diff_r), 1);
337 static void x_draw_decoration_after_title(Con *con, struct deco_render_params *p) {
338 assert(con->parent != NULL);
340 Rect *dr = &(con->deco_rect);
341 Rect br = con_border_style_rect(con);
343 /* Redraw the right border to cut off any text that went past it.
344 * This is necessary when the text was drawn using XCB since cutting text off
345 * automatically does not work there. For pango rendering, this isn't necessary. */
346 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->background,
347 dr->x + dr->width + br.width, dr->y, -br.width, dr->height);
349 /* Draw a 1px separator line before and after every tab, so that tabs can
350 * be easily distinguished. */
351 if (con->parent->layout == L_TABBED) {
353 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
354 dr->x, dr->y, 1, dr->height);
357 draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
358 dr->x + dr->width - 1, dr->y, 1, dr->height);
361 /* Redraw the border. */
362 x_draw_decoration_border(con, p);
366 * Draws the decoration of the given container onto its parent.
369 void x_draw_decoration(Con *con) {
370 Con *parent = con->parent;
371 bool leaf = con_is_leaf(con);
373 /* This code needs to run for:
375 * • non-leaf containers which are in a stacked/tabbed container
377 * It does not need to run for:
378 * • direct children of outputs or dockareas
379 * • floating containers (they don’t have a decoration)
382 parent->layout != L_STACKED &&
383 parent->layout != L_TABBED) ||
384 parent->type == CT_OUTPUT ||
385 parent->type == CT_DOCKAREA ||
386 con->type == CT_FLOATING_CON)
389 /* Skip containers whose height is 0 (for example empty dockareas) */
390 if (con->rect.height == 0)
393 /* Skip containers whose pixmap has not yet been created (can happen when
394 * decoration rendering happens recursively for a window for which
395 * x_push_node() was not yet called) */
396 if (leaf && con->frame_buffer.id == XCB_NONE)
399 /* 1: build deco_params and compare with cache */
400 struct deco_render_params *p = scalloc(1, sizeof(struct deco_render_params));
402 /* find out which colors to use */
404 p->color = &config.client.urgent;
405 else if (con == focused || con_inside_focused(con))
406 p->color = &config.client.focused;
407 else if (con == TAILQ_FIRST(&(parent->focus_head)))
408 p->color = &config.client.focused_inactive;
410 p->color = &config.client.unfocused;
412 p->border_style = con_border_style(con);
414 Rect *r = &(con->rect);
415 Rect *w = &(con->window_rect);
416 p->con_rect = (struct width_height){r->width, r->height};
417 p->con_window_rect = (struct width_height){w->width, w->height};
418 p->con_deco_rect = con->deco_rect;
419 p->background = config.client.background;
420 p->con_is_leaf = con_is_leaf(con);
421 p->parent_layout = con->parent->layout;
423 if (con->deco_render_params != NULL &&
424 (con->window == NULL || !con->window->name_x_changed) &&
425 !parent->pixmap_recreated &&
426 !con->pixmap_recreated &&
427 !con->mark_changed &&
428 memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
434 while ((next = TAILQ_NEXT(next, nodes))) {
435 FREE(next->deco_render_params);
438 FREE(con->deco_render_params);
439 con->deco_render_params = p;
441 if (con->window != NULL && con->window->name_x_changed)
442 con->window->name_x_changed = false;
444 parent->pixmap_recreated = false;
445 con->pixmap_recreated = false;
446 con->mark_changed = false;
448 /* 2: draw the client.background, but only for the parts around the window_rect */
449 if (con->window != NULL) {
451 draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
452 0, 0, r->width, w->y);
454 draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
455 0, w->y + w->height, r->width, r->height - (w->y + w->height));
457 draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
458 0, 0, w->x, r->height);
460 draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
461 w->x + w->width, 0, r->width - (w->x + w->width), r->height);
464 /* 3: draw a rectangle in border color around the client */
465 if (p->border_style != BS_NONE && p->con_is_leaf) {
466 /* We might hide some borders adjacent to the screen-edge */
467 adjacent_t borders_to_hide = ADJ_NONE;
468 borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
470 Rect br = con_border_style_rect(con);
472 DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
473 DLOG("border_rect spans (%d, %d) with %d x %d\n", br.x, br.y, br.width, br.height);
474 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);
477 /* These rectangles represent the border around the child window
478 * (left, bottom and right part). We don’t just fill the whole
479 * rectangle because some childs are not freely resizable and we want
480 * their background color to "shine through". */
481 if (!(borders_to_hide & ADJ_LEFT_SCREEN_EDGE)) {
482 draw_util_rectangle(conn, &(con->frame_buffer), p->color->background,
483 0, 0, br.x, r->height);
485 if (!(borders_to_hide & ADJ_RIGHT_SCREEN_EDGE)) {
486 draw_util_rectangle(conn, &(con->frame_buffer), p->color->background,
487 r->width + (br.width + br.x), 0, -(br.width + br.x), r->height);
489 if (!(borders_to_hide & ADJ_LOWER_SCREEN_EDGE)) {
490 draw_util_rectangle(conn, &(con->frame_buffer), p->color->background,
491 br.x, r->height + (br.height + br.y), r->width + br.width, -(br.height + br.y));
493 /* pixel border needs an additional line at the top */
494 if (p->border_style == BS_PIXEL && !(borders_to_hide & ADJ_UPPER_SCREEN_EDGE)) {
495 draw_util_rectangle(conn, &(con->frame_buffer), p->color->background,
496 br.x, 0, r->width + br.width, br.y);
499 /* Highlight the side of the border at which the next window will be
500 * opened if we are rendering a single window within a split container
501 * (which is undistinguishable from a single window outside a split
502 * container otherwise. */
503 if (TAILQ_NEXT(con, nodes) == NULL &&
504 TAILQ_PREV(con, nodes_head, nodes) == NULL &&
505 con->parent->type != CT_FLOATING_CON) {
506 if (p->parent_layout == L_SPLITH) {
507 draw_util_rectangle(conn, &(con->frame_buffer), p->color->indicator,
508 r->width + (br.width + br.x), br.y, -(br.width + br.x), r->height + br.height);
509 } else if (p->parent_layout == L_SPLITV) {
510 draw_util_rectangle(conn, &(con->frame_buffer), p->color->indicator,
511 br.x, r->height + (br.height + br.y), r->width + br.width, -(br.height + br.y));
516 /* if this is a borderless/1pixel window, we don’t need to render the
518 if (p->border_style != BS_NORMAL)
521 /* If the parent hasn't been set up yet, skip the decoration rendering
523 if (parent->frame_buffer.id == XCB_NONE)
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_decoration_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;
538 /* we have a split container which gets a representation
539 * of its children as title
542 char *tree = con_get_tree_representation(con);
543 sasprintf(&_title, "i3: %s", tree);
546 i3String *title = i3string_from_utf8(_title);
547 draw_util_text(title, &(parent->frame_buffer),
548 p->color->text, p->color->background,
549 con->deco_rect.x + 2, con->deco_rect.y + text_offset_y,
550 con->deco_rect.width - 2);
552 I3STRING_FREE(title);
557 if (win->name == NULL)
560 int indent_level = 0,
562 Con *il_parent = parent;
563 if (il_parent->layout != L_STACKED) {
565 //DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
566 if (il_parent->layout == L_STACKED)
568 if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
570 il_parent = il_parent->parent;
574 //DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
575 int indent_px = (indent_level * 5) * indent_mult;
578 if (config.show_marks && !TAILQ_EMPTY(&(con->marks_head))) {
579 char *formatted_mark = sstrdup("");
580 bool had_visible_mark = false;
583 TAILQ_FOREACH(mark, &(con->marks_head), marks) {
584 if (mark->name[0] == '_')
586 had_visible_mark = true;
589 sasprintf(&buf, "%s[%s]", formatted_mark, mark->name);
590 free(formatted_mark);
591 formatted_mark = buf;
594 if (had_visible_mark) {
595 i3String *mark = i3string_from_utf8(formatted_mark);
596 mark_width = predict_text_width(mark);
598 draw_util_text(mark, &(parent->frame_buffer),
599 p->color->text, p->color->background,
600 con->deco_rect.x + con->deco_rect.width - mark_width - logical_px(2),
601 con->deco_rect.y + text_offset_y, mark_width);
606 FREE(formatted_mark);
609 i3String *title = win->title_format == NULL ? win->name : window_parse_title_format(win);
610 draw_util_text(title, &(parent->frame_buffer),
611 p->color->text, p->color->background,
612 con->deco_rect.x + logical_px(2) + indent_px, con->deco_rect.y + text_offset_y,
613 con->deco_rect.width - logical_px(2) - indent_px - mark_width - logical_px(2));
614 if (win->title_format != NULL)
615 I3STRING_FREE(title);
618 x_draw_decoration_after_title(con, p);
620 draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
624 * Recursively calls x_draw_decoration. This cannot be done in x_push_node
625 * because x_push_node uses focus order to recurse (see the comment above)
626 * while drawing the decoration needs to happen in the actual order.
629 void x_deco_recurse(Con *con) {
631 bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
632 TAILQ_EMPTY(&(con->floating_head));
633 con_state *state = state_for_frame(con->frame.id);
636 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
637 x_deco_recurse(current);
639 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
640 x_deco_recurse(current);
643 draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
647 if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
648 (!leaf || con->mapped))
649 x_draw_decoration(con);
653 * Sets or removes the _NET_WM_STATE_HIDDEN property on con if necessary.
656 static void set_hidden_state(Con *con) {
657 if (con->window == NULL) {
661 con_state *state = state_for_frame(con->frame.id);
662 bool should_be_hidden = con_is_hidden(con);
663 if (should_be_hidden == state->is_hidden)
666 if (should_be_hidden) {
667 DLOG("setting _NET_WM_STATE_HIDDEN for con = %p\n", con);
668 xcb_add_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
670 DLOG("removing _NET_WM_STATE_HIDDEN for con = %p\n", con);
671 xcb_remove_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
674 state->is_hidden = should_be_hidden;
678 * This function pushes the properties of each node of the layout tree to
679 * X11 if they have changed (like the map state, position of the window, …).
680 * It recursively traverses all children of the given node.
683 void x_push_node(Con *con) {
686 Rect rect = con->rect;
688 //DLOG("Pushing changes for node %p / %s\n", con, con->name);
689 state = state_for_frame(con->frame.id);
691 if (state->name != NULL) {
692 DLOG("pushing name %s for con %p\n", state->name, con);
694 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame.id,
695 XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(state->name), state->name);
699 if (con->window == NULL) {
700 /* Calculate the height of all window decorations which will be drawn on to
702 uint32_t max_y = 0, max_height = 0;
703 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
704 Rect *dr = &(current->deco_rect);
705 if (dr->y >= max_y && dr->height >= max_height) {
707 max_height = dr->height;
710 rect.height = max_y + max_height;
711 if (rect.height == 0)
715 /* reparent the child window (when the window was moved due to a sticky
717 if (state->need_reparent && con->window != NULL) {
718 DLOG("Reparenting child window\n");
720 /* Temporarily set the event masks to XCB_NONE so that we won’t get
721 * UnmapNotify events (otherwise the handler would close the container).
722 * These events are generated automatically when reparenting. */
723 uint32_t values[] = {XCB_NONE};
724 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
725 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
727 xcb_reparent_window(conn, con->window->id, con->frame.id, 0, 0);
729 values[0] = FRAME_EVENT_MASK;
730 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
731 values[0] = CHILD_EVENT_MASK;
732 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
734 state->old_frame = XCB_NONE;
735 state->need_reparent = false;
738 DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
739 con, con->window->id, con->ignore_unmap);
742 /* The pixmap of a borderless leaf container will not be used except
743 * for the titlebar in a stack or tabs (issue #1013). */
744 bool is_pixmap_needed = (con->border_style != BS_NONE ||
746 con->parent->layout == L_STACKED ||
747 con->parent->layout == L_TABBED);
749 bool fake_notify = false;
750 /* Set new position if rect changed (and if height > 0) or if the pixmap
751 * needs to be recreated */
752 if ((is_pixmap_needed && con->frame_buffer.id == XCB_NONE) || (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
754 /* We first create the new pixmap, then render to it, set it as the
755 * background and only afterwards change the window size. This reduces
758 /* As the pixmap only depends on the size and not on the position, it
759 * is enough to check if width/height have changed. Also, we don’t
760 * create a pixmap at all when the window is actually not visible
761 * (height == 0) or when it is not needed. */
762 bool has_rect_changed = (state->rect.width != rect.width || state->rect.height != rect.height);
764 /* Check if the container has an unneeded pixmap left over from
765 * previously having a border or titlebar. */
766 if (!is_pixmap_needed && con->frame_buffer.id != XCB_NONE) {
767 draw_util_surface_free(conn, &(con->frame_buffer));
768 xcb_free_pixmap(conn, con->frame_buffer.id);
769 con->frame_buffer.id = XCB_NONE;
772 if (is_pixmap_needed && (has_rect_changed || con->frame_buffer.id == XCB_NONE)) {
773 if (con->frame_buffer.id == XCB_NONE) {
774 con->frame_buffer.id = xcb_generate_id(conn);
776 draw_util_surface_free(conn, &(con->frame_buffer));
777 xcb_free_pixmap(conn, con->frame_buffer.id);
780 uint16_t win_depth = root_depth;
782 win_depth = con->window->depth;
784 /* Ensure we have valid dimensions for our surface. */
785 // TODO This is probably a bug in the condition above as we should never enter this path
786 // for height == 0. Also, we should probably handle width == 0 the same way.
787 int width = MAX(rect.width, 1);
788 int height = MAX(rect.height, 1);
790 xcb_create_pixmap_checked(conn, win_depth, con->frame_buffer.id, con->frame.id, width, height);
791 draw_util_surface_init(conn, &(con->frame_buffer), con->frame_buffer.id,
792 get_visualtype_by_id(get_visualid_by_depth(win_depth)), width, height);
794 /* For the graphics context, we disable GraphicsExposure events.
795 * Those will be sent when a CopyArea request cannot be fulfilled
796 * properly due to parts of the source being unmapped or otherwise
797 * unavailable. Since we always copy from pixmaps to windows, this
798 * is not a concern for us. */
799 xcb_change_gc(conn, con->frame_buffer.gc, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
801 draw_util_surface_set_size(&(con->frame), width, height);
802 con->pixmap_recreated = true;
804 /* Don’t render the decoration for windows inside a stack which are
805 * not visible right now */
806 // TODO Should this work the same way for L_TABBED?
808 con->parent->layout != L_STACKED ||
809 TAILQ_FIRST(&(con->parent->focus_head)) == con)
810 /* Render the decoration now to make the correct decoration visible
811 * from the very first moment. Later calls will be cached, so this
812 * doesn’t hurt performance. */
816 DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
817 /* flush to ensure that the following commands are sent in a single
818 * buffer and will be processed directly afterwards (the contents of a
819 * window get lost when resizing it, therefore we want to provide it as
820 * fast as possible) */
822 xcb_set_window_rect(conn, con->frame.id, rect);
823 if (con->frame_buffer.id != XCB_NONE) {
824 draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
828 memcpy(&(state->rect), &rect, sizeof(Rect));
832 /* dito, but for child windows */
833 if (con->window != NULL &&
834 memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
835 DLOG("setting window rect (%d, %d, %d, %d)\n",
836 con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
837 xcb_set_window_rect(conn, con->window->id, con->window_rect);
838 memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
842 /* Map if map state changed, also ensure that the child window
843 * is changed if we are mapped and there is a new, unmapped child window.
844 * Unmaps are handled in x_push_node_unmaps(). */
845 if ((state->mapped != con->mapped || (con->window != NULL && !state->child_mapped)) &&
847 xcb_void_cookie_t cookie;
849 if (con->window != NULL) {
850 /* Set WM_STATE_NORMAL because GTK applications don’t want to
851 * drag & drop if we don’t. Also, xprop(1) needs it. */
852 long data[] = {XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE};
853 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
854 A_WM_STATE, A_WM_STATE, 32, 2, data);
858 if (!state->child_mapped && con->window != NULL) {
859 cookie = xcb_map_window(conn, con->window->id);
861 /* We are interested in EnterNotifys as soon as the window is
863 values[0] = CHILD_EVENT_MASK;
864 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
865 DLOG("mapping child window (serial %d)\n", cookie.sequence);
866 state->child_mapped = true;
869 cookie = xcb_map_window(conn, con->frame.id);
871 values[0] = FRAME_EVENT_MASK;
872 xcb_change_window_attributes(conn, con->frame.id, XCB_CW_EVENT_MASK, values);
874 /* copy the pixmap contents to the frame window immediately after mapping */
875 if (con->frame_buffer.id != XCB_NONE) {
876 draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
880 DLOG("mapping container %08x (serial %d)\n", con->frame.id, cookie.sequence);
881 state->mapped = con->mapped;
884 state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
887 DLOG("Sending fake configure notify\n");
888 fake_absolute_configure_notify(con);
891 set_hidden_state(con);
893 /* Handle all children and floating windows of this node. We recurse
894 * in focus order to display the focused client in a stack first when
895 * switching workspaces (reduces flickering). */
896 TAILQ_FOREACH(current, &(con->focus_head), focused)
897 x_push_node(current);
901 * Same idea as in x_push_node(), but this function only unmaps windows. It is
902 * necessary to split this up to handle new fullscreen clients properly: The
903 * new window needs to be mapped and focus needs to be set *before* the
904 * underlying windows are unmapped. Otherwise, focus will revert to the
905 * PointerRoot and will then be set to the new window, generating unnecessary
906 * FocusIn/FocusOut events.
909 static void x_push_node_unmaps(Con *con) {
913 //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
914 state = state_for_frame(con->frame.id);
916 /* map/unmap if map state changed, also ensure that the child window
917 * is changed if we are mapped *and* in initial state (meaning the
918 * container was empty before, but now got a child) */
919 if (state->unmap_now) {
920 xcb_void_cookie_t cookie;
921 if (con->window != NULL) {
922 /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
923 long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
924 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
925 A_WM_STATE, A_WM_STATE, 32, 2, data);
928 cookie = xcb_unmap_window(conn, con->frame.id);
929 DLOG("unmapping container %p / %s (serial %d)\n", con, con->name, cookie.sequence);
930 /* we need to increase ignore_unmap for this container (if it
931 * contains a window) and for every window "under" this one which
932 * contains a window */
933 if (con->window != NULL) {
935 DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame.id, con->ignore_unmap);
937 state->mapped = con->mapped;
940 /* handle all children and floating windows of this node */
941 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
942 x_push_node_unmaps(current);
944 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
945 x_push_node_unmaps(current);
949 * Returns true if the given container is currently attached to its parent.
951 * TODO: Remove once #1185 has been fixed
953 static bool is_con_attached(Con *con) {
954 if (con->parent == NULL)
958 TAILQ_FOREACH(current, &(con->parent->nodes_head), nodes) {
967 * Pushes all changes (state of each node, see x_push_node() and the window
970 * NOTE: We need to push the stack first so that the windows have the correct
971 * stacking order. This is relevant for workspace switching where we map the
972 * windows because mapping may generate EnterNotify events. When they are
973 * generated in the wrong order, this will cause focus problems when switching
977 void x_push_changes(Con *con) {
979 xcb_query_pointer_cookie_t pointercookie;
981 /* If we need to warp later, we request the pointer position as soon as possible */
983 pointercookie = xcb_query_pointer(conn, root);
986 DLOG("-- PUSHING WINDOW STACK --\n");
987 //DLOG("Disabling EnterNotify\n");
988 uint32_t values[1] = {XCB_NONE};
989 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
991 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
993 //DLOG("Done, EnterNotify disabled\n");
994 bool order_changed = false;
995 bool stacking_changed = false;
997 /* count first, necessary to (re)allocate memory for the bottom-to-top
998 * stack afterwards */
1000 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
1001 if (con_has_managed_window(state->con))
1004 /* The bottom-to-top window stack of all windows which are managed by i3.
1005 * Used for x_get_window_stack(). */
1006 static xcb_window_t *client_list_windows = NULL;
1007 static int client_list_count = 0;
1009 if (cnt != client_list_count) {
1010 client_list_windows = srealloc(client_list_windows, sizeof(xcb_window_t) * cnt);
1011 client_list_count = cnt;
1014 xcb_window_t *walk = client_list_windows;
1016 /* X11 correctly represents the stack if we push it from bottom to top */
1017 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1018 if (con_has_managed_window(state->con))
1019 memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
1021 //DLOG("stack: 0x%08x\n", state->id);
1022 con_state *prev = CIRCLEQ_PREV(state, state);
1023 con_state *old_prev = CIRCLEQ_PREV(state, old_state);
1024 if (prev != old_prev)
1025 order_changed = true;
1026 if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
1027 stacking_changed = true;
1028 //DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
1030 mask |= XCB_CONFIG_WINDOW_SIBLING;
1031 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
1032 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
1034 xcb_configure_window(conn, prev->id, mask, values);
1036 state->initial = false;
1039 /* If we re-stacked something (or a new window appeared), we need to update
1040 * the _NET_CLIENT_LIST and _NET_CLIENT_LIST_STACKING hints */
1041 if (stacking_changed) {
1042 DLOG("Client list changed (%i clients)\n", cnt);
1043 ewmh_update_client_list_stacking(client_list_windows, client_list_count);
1045 walk = client_list_windows;
1047 /* reorder by initial mapping */
1048 TAILQ_FOREACH(state, &initial_mapping_head, initial_mapping_order) {
1049 if (con_has_managed_window(state->con))
1050 *walk++ = state->con->window->id;
1053 ewmh_update_client_list(client_list_windows, client_list_count);
1056 DLOG("PUSHING CHANGES\n");
1060 xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
1061 if (!pointerreply) {
1062 ELOG("Could not query pointer position, not warping pointer\n");
1064 int mid_x = warp_to->x + (warp_to->width / 2);
1065 int mid_y = warp_to->y + (warp_to->height / 2);
1067 Output *current = get_output_containing(pointerreply->root_x, pointerreply->root_y);
1068 Output *target = get_output_containing(mid_x, mid_y);
1069 if (current != target) {
1070 /* Ignore MotionNotify events generated by warping */
1071 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT});
1072 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
1073 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){ROOT_EVENT_MASK});
1079 //DLOG("Re-enabling EnterNotify\n");
1080 values[0] = FRAME_EVENT_MASK;
1081 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1083 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1085 //DLOG("Done, EnterNotify re-enabled\n");
1087 x_deco_recurse(con);
1089 xcb_window_t to_focus = focused->frame.id;
1090 if (focused->window != NULL)
1091 to_focus = focused->window->id;
1093 if (focused_id != to_focus) {
1094 if (!focused->mapped) {
1095 DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
1096 /* Invalidate focused_id to correctly focus new windows with the same ID */
1097 focused_id = XCB_NONE;
1099 if (focused->window != NULL &&
1100 focused->window->needs_take_focus &&
1101 focused->window->doesnt_accept_focus) {
1102 DLOG("Updating focus by sending WM_TAKE_FOCUS to window 0x%08x (focused: %p / %s)\n",
1103 to_focus, focused, focused->name);
1104 send_take_focus(to_focus, last_timestamp);
1106 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1108 if (to_focus != last_focused && is_con_attached(focused))
1109 ipc_send_window_event("focus", focused);
1111 DLOG("Updating focus (focused: %p / %s) to X11 window 0x%08x\n", focused, focused->name, to_focus);
1112 /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
1113 * no focus change events for our own focus changes. We only want
1114 * these generated by the clients. */
1115 if (focused->window != NULL) {
1116 values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
1117 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1119 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
1120 if (focused->window != NULL) {
1121 values[0] = CHILD_EVENT_MASK;
1122 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1125 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1127 if (to_focus != XCB_NONE && to_focus != last_focused && focused->window != NULL && is_con_attached(focused))
1128 ipc_send_window_event("focus", focused);
1131 focused_id = last_focused = to_focus;
1135 if (focused_id == XCB_NONE) {
1136 /* If we still have no window to focus, we focus the EWMH window instead. We use this rather than the
1137 * root window in order to avoid an X11 fallback mechanism causing a ghosting effect (see #1378). */
1138 DLOG("Still no window focused, better set focus to the EWMH support window (%d)\n", ewmh_window);
1139 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, ewmh_window, XCB_CURRENT_TIME);
1140 ewmh_update_active_window(XCB_WINDOW_NONE);
1141 focused_id = ewmh_window;
1145 DLOG("ENDING CHANGES\n");
1147 /* Disable EnterWindow events for windows which will be unmapped in
1148 * x_push_node_unmaps() now. Unmapping windows happens when switching
1149 * workspaces. We want to avoid getting EnterNotifies during that phase
1150 * because they would screw up our focus. One of these cases is having a
1151 * stack with two windows. If the first window is focused and gets
1152 * unmapped, the second one appears under the cursor and therefore gets an
1153 * EnterNotify event. */
1154 values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
1155 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1156 if (!state->unmap_now)
1158 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1161 /* Push all pending unmaps */
1162 x_push_node_unmaps(con);
1164 /* save the current stack as old stack */
1165 CIRCLEQ_FOREACH(state, &state_head, state) {
1166 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
1167 CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
1169 //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
1170 // DLOG("old stack: 0x%08x\n", state->id);
1177 * Raises the specified container in the internal stack of X windows. The
1178 * next call to x_push_changes() will make the change visible in X11.
1181 void x_raise_con(Con *con) {
1183 state = state_for_frame(con->frame.id);
1184 //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
1186 CIRCLEQ_REMOVE(&state_head, state, state);
1187 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
1191 * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
1192 * of the given name. Used for properly tagging the windows for easily spotting
1193 * i3 windows in xwininfo -root -all.
1196 void x_set_name(Con *con, const char *name) {
1197 struct con_state *state;
1199 if ((state = state_for_frame(con->frame.id)) == NULL) {
1200 ELOG("window state not found\n");
1205 state->name = sstrdup(name);
1209 * Set up the I3_SHMLOG_PATH atom.
1212 void update_shmlog_atom() {
1213 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1214 A_I3_SHMLOG_PATH, A_UTF8_STRING, 8,
1215 strlen(shmlogname), shmlogname);
1219 * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
1222 void x_set_i3_atoms(void) {
1223 pid_t pid = getpid();
1224 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
1225 (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
1226 current_socketpath);
1227 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_PID, XCB_ATOM_CARDINAL, 32, 1, &pid);
1228 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
1229 strlen(current_configpath), current_configpath);
1230 update_shmlog_atom();
1234 * Set warp_to coordinates. This will trigger on the next call to
1238 void x_set_warp_to(Rect *rect) {
1239 if (config.mouse_warping != POINTER_WARPING_NONE)
1244 * Applies the given mask to the event mask of every i3 window decoration X11
1245 * window. This is useful to disable EnterNotify while resizing so that focus
1249 void x_mask_event_mask(uint32_t mask) {
1250 uint32_t values[] = {FRAME_EVENT_MASK & mask};
1253 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1255 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);