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 /* Stores the X11 window ID of the currently focused window */
16 xcb_window_t focused_id = XCB_NONE;
18 /* Because 'focused_id' might be reset to force input focus, we separately keep
19 * track of the X11 window ID to be able to always tell whether the focused
20 * window actually changed. */
21 static xcb_window_t last_focused = XCB_NONE;
23 /* Stores coordinates to warp mouse pointer to if set */
27 * Describes the X11 state we may modify (map state, position, window stack).
28 * There is one entry per container. The state represents the current situation
29 * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
30 * which represents the order that will be pushed to X11, while old_state_head
31 * represents the current order). It will be updated in x_push_changes().
34 typedef struct con_state {
41 /** The con for which this state is. */
44 /* For reparenting, we have a flag (need_reparent) and the X ID of the old
45 * frame this window was in. The latter is necessary because we need to
46 * ignore UnmapNotify events (by changing the window event mask). */
48 xcb_window_t old_frame;
57 CIRCLEQ_ENTRY(con_state) state;
58 CIRCLEQ_ENTRY(con_state) old_state;
59 TAILQ_ENTRY(con_state) initial_mapping_order;
62 CIRCLEQ_HEAD(state_head, con_state) state_head =
63 CIRCLEQ_HEAD_INITIALIZER(state_head);
65 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
66 CIRCLEQ_HEAD_INITIALIZER(old_state_head);
68 TAILQ_HEAD(initial_mapping_head, con_state) initial_mapping_head =
69 TAILQ_HEAD_INITIALIZER(initial_mapping_head);
72 * Returns the container state for the given frame. This function always
73 * returns a container state (otherwise, there is a bug in the code and the
74 * container state of a container for which x_con_init() was not called was
78 static con_state *state_for_frame(xcb_window_t window) {
80 CIRCLEQ_FOREACH(state, &state_head, state)
81 if (state->id == window)
84 /* TODO: better error handling? */
85 ELOG("No state found\n");
91 * Initializes the X11 part for the given container. Called exactly once for
92 * every container from con_new().
95 void x_con_init(Con *con, uint16_t depth) {
96 /* TODO: maybe create the window when rendering first? we could then even
97 * get the initial geometry right */
102 xcb_visualid_t visual = XCB_COPY_FROM_PARENT;
103 xcb_colormap_t win_colormap = XCB_NONE;
104 if (depth != root_depth && depth != XCB_COPY_FROM_PARENT) {
105 /* For custom visuals, we need to create a colormap before creating
106 * this window. It will be freed directly after creating the window. */
107 visual = get_visualid_by_depth(depth);
108 win_colormap = xcb_generate_id(conn);
109 xcb_create_colormap_checked(conn, XCB_COLORMAP_ALLOC_NONE, win_colormap, root, visual);
111 /* We explicitly set a background color and border color (even though we
112 * don’t even have a border) because the X11 server requires us to when
113 * using 32 bit color depths, see
114 * http://stackoverflow.com/questions/3645632 */
115 mask |= XCB_CW_BACK_PIXEL;
116 values[0] = root_screen->black_pixel;
118 mask |= XCB_CW_BORDER_PIXEL;
119 values[1] = root_screen->black_pixel;
121 /* our own frames should not be managed */
122 mask |= XCB_CW_OVERRIDE_REDIRECT;
125 /* see include/xcb.h for the FRAME_EVENT_MASK */
126 mask |= XCB_CW_EVENT_MASK;
127 values[3] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
129 mask |= XCB_CW_COLORMAP;
130 values[4] = win_colormap;
132 /* our own frames should not be managed */
133 mask = XCB_CW_OVERRIDE_REDIRECT;
136 /* see include/xcb.h for the FRAME_EVENT_MASK */
137 mask |= XCB_CW_EVENT_MASK;
138 values[1] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
140 mask |= XCB_CW_COLORMAP;
141 values[2] = colormap;
144 Rect dims = {-15, -15, 10, 10};
145 con->frame = create_window(conn, dims, depth, visual, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER, false, mask, values);
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;
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)) == 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)) == NULL) {
190 ELOG("window state for con not found\n");
194 state->need_reparent = true;
195 state->old_frame = old->frame;
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)) == NULL) {
206 ELOG("window state for src not found\n");
210 if ((state_dest = state_for_frame(dest->frame)) == 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 xcb_destroy_window(conn, con->frame);
233 xcb_free_pixmap(conn, con->pixmap);
234 xcb_free_gc(conn, con->pm_gc);
235 state = state_for_frame(con->frame);
236 CIRCLEQ_REMOVE(&state_head, state, state);
237 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
238 TAILQ_REMOVE(&initial_mapping_head, state, initial_mapping_order);
242 /* Invalidate focused_id to correctly focus new windows with the same ID */
243 focused_id = last_focused = XCB_NONE;
247 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
250 bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
251 xcb_get_property_cookie_t cookie;
252 xcb_icccm_get_wm_protocols_reply_t protocols;
255 cookie = xcb_icccm_get_wm_protocols(conn, window, A_WM_PROTOCOLS);
256 if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
259 /* Check if the client’s protocols have the requested atom set */
260 for (uint32_t i = 0; i < protocols.atoms_len; i++)
261 if (protocols.atoms[i] == atom)
264 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
270 * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
273 void x_window_kill(xcb_window_t window, kill_window_t kill_window) {
274 /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
275 if (!window_supports_protocol(window, A_WM_DELETE_WINDOW)) {
276 if (kill_window == KILL_WINDOW) {
277 LOG("Killing specific window 0x%08x\n", window);
278 xcb_destroy_window(conn, window);
280 LOG("Killing the X11 client which owns window 0x%08x\n", window);
281 xcb_kill_client(conn, window);
286 /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
287 * In order to properly initialize these bytes, we allocate 32 bytes even
288 * though we only need less for an xcb_configure_notify_event_t */
289 void *event = scalloc(32, 1);
290 xcb_client_message_event_t *ev = event;
292 ev->response_type = XCB_CLIENT_MESSAGE;
294 ev->type = A_WM_PROTOCOLS;
296 ev->data.data32[0] = A_WM_DELETE_WINDOW;
297 ev->data.data32[1] = XCB_CURRENT_TIME;
299 LOG("Sending WM_DELETE to the client\n");
300 xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)ev);
305 static i3String *parse_title_format(struct Window *win) {
306 /* We need to ensure that we only escape the window title if pango
307 * is used by the current font. */
308 const bool is_markup = font_is_pango();
310 char *format = win->title_format;
311 /* We initialize these lazily so we only escape them if really necessary. */
312 const char *escaped_title = NULL;
313 const char *escaped_class = NULL;
314 const char *escaped_instance = NULL;
316 /* We have to first iterate over the string to see how much buffer space
317 * we need to allocate. */
318 int buffer_len = strlen(format) + 1;
319 for (char *walk = format; *walk != '\0'; walk++) {
320 if (STARTS_WITH(walk, "%title")) {
321 if (escaped_title == NULL)
322 escaped_title = i3string_as_utf8(is_markup ? i3string_escape_markup(win->name) : win->name);
324 buffer_len = buffer_len - strlen("%title") + strlen(escaped_title);
325 walk += strlen("%title") - 1;
326 } else if (STARTS_WITH(walk, "%class")) {
327 if (escaped_class == NULL)
328 escaped_class = is_markup ? g_markup_escape_text(win->class_class, -1) : win->class_class;
330 buffer_len = buffer_len - strlen("%class") + strlen(escaped_class);
331 walk += strlen("%class") - 1;
332 } else if (STARTS_WITH(walk, "%instance")) {
333 if (escaped_instance == NULL)
334 escaped_instance = is_markup ? g_markup_escape_text(win->class_instance, -1) : win->class_instance;
336 buffer_len = buffer_len - strlen("%instance") + strlen(escaped_instance);
337 walk += strlen("%instance") - 1;
341 /* Now we can parse the format string. */
342 char buffer[buffer_len];
343 char *outwalk = buffer;
344 for (char *walk = format; *walk != '\0'; walk++) {
346 *(outwalk++) = *walk;
350 if (STARTS_WITH(walk + 1, "title")) {
351 outwalk += sprintf(outwalk, "%s", escaped_title);
352 walk += strlen("title");
353 } else if (STARTS_WITH(walk + 1, "class")) {
354 outwalk += sprintf(outwalk, "%s", escaped_class);
355 walk += strlen("class");
356 } else if (STARTS_WITH(walk + 1, "instance")) {
357 outwalk += sprintf(outwalk, "%s", escaped_instance);
358 walk += strlen("instance");
363 i3String *formatted = i3string_from_utf8(buffer);
364 i3string_set_markup(formatted, is_markup);
369 * Draws the decoration of the given container onto its parent.
372 void x_draw_decoration(Con *con) {
373 Con *parent = con->parent;
374 bool leaf = con_is_leaf(con);
376 /* This code needs to run for:
378 * • non-leaf containers which are in a stacked/tabbed container
380 * It does not need to run for:
381 * • direct children of outputs or dockareas
382 * • floating containers (they don’t have a decoration)
385 parent->layout != L_STACKED &&
386 parent->layout != L_TABBED) ||
387 parent->type == CT_OUTPUT ||
388 parent->type == CT_DOCKAREA ||
389 con->type == CT_FLOATING_CON)
392 /* Skip containers whose height is 0 (for example empty dockareas) */
393 if (con->rect.height == 0)
396 /* Skip containers whose pixmap has not yet been created (can happen when
397 * decoration rendering happens recursively for a window for which
398 * x_push_node() was not yet called) */
399 if (leaf && con->pixmap == XCB_NONE)
402 /* 1: build deco_params and compare with cache */
403 struct deco_render_params *p = scalloc(1, sizeof(struct deco_render_params));
405 /* find out which colors to use */
407 p->color = &config.client.urgent;
408 else if (con == focused || con_inside_focused(con))
409 p->color = &config.client.focused;
410 else if (con == TAILQ_FIRST(&(parent->focus_head)))
411 p->color = &config.client.focused_inactive;
413 p->color = &config.client.unfocused;
415 p->border_style = con_border_style(con);
417 Rect *r = &(con->rect);
418 Rect *w = &(con->window_rect);
419 p->con_rect = (struct width_height){r->width, r->height};
420 p->con_window_rect = (struct width_height){w->width, w->height};
421 p->con_deco_rect = con->deco_rect;
422 p->background = config.client.background;
423 p->con_is_leaf = con_is_leaf(con);
424 p->parent_layout = con->parent->layout;
426 if (con->deco_render_params != NULL &&
427 (con->window == NULL || !con->window->name_x_changed) &&
428 !parent->pixmap_recreated &&
429 !con->pixmap_recreated &&
430 !con->mark_changed &&
431 memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
437 while ((next = TAILQ_NEXT(next, nodes))) {
438 FREE(next->deco_render_params);
441 FREE(con->deco_render_params);
442 con->deco_render_params = p;
444 if (con->window != NULL && con->window->name_x_changed)
445 con->window->name_x_changed = false;
447 parent->pixmap_recreated = false;
448 con->pixmap_recreated = false;
449 con->mark_changed = false;
451 /* 2: draw the client.background, but only for the parts around the client_rect */
452 if (con->window != NULL) {
453 xcb_rectangle_t background[] = {
455 {0, 0, r->width, w->y},
457 {0, (w->y + w->height), r->width, r->height - (w->y + w->height)},
459 {0, 0, w->x, r->height},
461 {w->x + w->width, 0, r->width - (w->x + w->width), r->height}};
463 for (int i = 0; i < 4; i++)
464 DLOG("rect is (%d, %d) with %d x %d\n",
472 xcb_change_gc(conn, con->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){config.client.background});
473 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, sizeof(background) / sizeof(xcb_rectangle_t), background);
476 /* 3: draw a rectangle in border color around the client */
477 if (p->border_style != BS_NONE && p->con_is_leaf) {
478 /* We might hide some borders adjacent to the screen-edge */
479 adjacent_t borders_to_hide = ADJ_NONE;
480 borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
482 Rect br = con_border_style_rect(con);
484 DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
485 DLOG("border_rect spans (%d, %d) with %d x %d\n", br.x, br.y, br.width, br.height);
486 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);
489 /* These rectangles represents the border around the child window
490 * (left, bottom and right part). We don’t just fill the whole
491 * rectangle because some childs are not freely resizable and we want
492 * their background color to "shine through". */
493 xcb_change_gc(conn, con->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){p->color->background});
494 if (!(borders_to_hide & ADJ_LEFT_SCREEN_EDGE)) {
495 xcb_rectangle_t leftline = {0, 0, br.x, r->height};
496 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, &leftline);
498 if (!(borders_to_hide & ADJ_RIGHT_SCREEN_EDGE)) {
499 xcb_rectangle_t rightline = {r->width + (br.width + br.x), 0, -(br.width + br.x), r->height};
500 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, &rightline);
502 if (!(borders_to_hide & ADJ_LOWER_SCREEN_EDGE)) {
503 xcb_rectangle_t bottomline = {br.x, r->height + (br.height + br.y), r->width + br.width, -(br.height + br.y)};
504 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, &bottomline);
506 /* 1pixel border needs an additional line at the top */
507 if (p->border_style == BS_PIXEL && !(borders_to_hide & ADJ_UPPER_SCREEN_EDGE)) {
508 xcb_rectangle_t topline = {br.x, 0, r->width + br.width, br.y};
509 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, &topline);
512 /* Highlight the side of the border at which the next window will be
513 * opened if we are rendering a single window within a split container
514 * (which is undistinguishable from a single window outside a split
515 * container otherwise. */
516 if (TAILQ_NEXT(con, nodes) == NULL &&
517 TAILQ_PREV(con, nodes_head, nodes) == NULL &&
518 con->parent->type != CT_FLOATING_CON) {
519 xcb_change_gc(conn, con->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){p->color->indicator});
520 if (p->parent_layout == L_SPLITH)
521 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, (xcb_rectangle_t[]){
522 {r->width + (br.width + br.x), br.y, -(br.width + br.x), r->height + br.height}});
523 else if (p->parent_layout == L_SPLITV)
524 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, (xcb_rectangle_t[]){
525 {br.x, r->height + (br.height + br.y), r->width + br.width, -(br.height + br.y)}});
529 /* if this is a borderless/1pixel window, we don’t need to render the
531 if (p->border_style != BS_NORMAL)
534 /* 4: paint the bar */
535 xcb_change_gc(conn, parent->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){p->color->background});
536 xcb_rectangle_t drect = {con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height};
537 xcb_poly_fill_rectangle(conn, parent->pixmap, parent->pm_gc, 1, &drect);
539 /* 5: draw two unconnected horizontal lines in border color */
540 xcb_change_gc(conn, parent->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){p->color->border});
541 Rect *dr = &(con->deco_rect);
542 adjacent_t borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
543 int deco_diff_l = borders_to_hide & ADJ_LEFT_SCREEN_EDGE ? 0 : con->current_border_width;
544 int deco_diff_r = borders_to_hide & ADJ_RIGHT_SCREEN_EDGE ? 0 : con->current_border_width;
545 if (parent->layout == L_TABBED ||
546 (parent->layout == L_STACKED && TAILQ_NEXT(con, nodes) != NULL)) {
550 xcb_segment_t segments[] = {
552 dr->x + dr->width - 1, dr->y},
553 {dr->x + deco_diff_l, dr->y + dr->height - 1,
554 dr->x - deco_diff_r + dr->width - 1, dr->y + dr->height - 1}};
555 xcb_poly_segment(conn, parent->pixmap, parent->pm_gc, 2, segments);
557 /* 6: draw the title */
558 set_font_colors(parent->pm_gc, p->color->text, p->color->background);
559 int text_offset_y = (con->deco_rect.height - config.font.height) / 2;
561 struct Window *win = con->window;
563 /* we have a split container which gets a representation
564 * of its children as title
567 char *tree = con_get_tree_representation(con);
568 sasprintf(&title, "i3: %s", tree);
571 draw_text_ascii(title,
572 parent->pixmap, parent->pm_gc,
573 con->deco_rect.x + 2, con->deco_rect.y + text_offset_y,
574 con->deco_rect.width - 2);
580 if (win->name == NULL)
583 int indent_level = 0,
585 Con *il_parent = parent;
586 if (il_parent->layout != L_STACKED) {
588 //DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
589 if (il_parent->layout == L_STACKED)
591 if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
593 il_parent = il_parent->parent;
597 //DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
598 int indent_px = (indent_level * 5) * indent_mult;
601 if (config.show_marks && con->mark != NULL && (con->mark)[0] != '_') {
602 char *formatted_mark;
603 sasprintf(&formatted_mark, "[%s]", con->mark);
604 i3String *mark = i3string_from_utf8(formatted_mark);
605 FREE(formatted_mark);
606 mark_width = predict_text_width(mark);
608 draw_text(mark, parent->pixmap, parent->pm_gc,
609 con->deco_rect.x + con->deco_rect.width - mark_width - logical_px(2),
610 con->deco_rect.y + text_offset_y, mark_width);
615 i3String *title = win->title_format == NULL ? win->name : parse_title_format(win);
617 parent->pixmap, parent->pm_gc,
618 con->deco_rect.x + logical_px(2) + indent_px, con->deco_rect.y + text_offset_y,
619 con->deco_rect.width - logical_px(2) - indent_px - mark_width - logical_px(2));
620 if (win->title_format != NULL)
621 I3STRING_FREE(title);
624 /* Since we don’t clip the text at all, it might in some cases be painted
625 * on the border pixels on the right side of a window. Therefore, we draw
626 * the right border again after rendering the text (and the unconnected
627 * lines in border color). */
629 /* Draw a 1px separator line before and after every tab, so that tabs can
630 * be easily distinguished. */
631 if (parent->layout == L_TABBED) {
632 xcb_change_gc(conn, parent->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){p->color->border});
634 xcb_change_gc(conn, parent->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){p->color->background});
636 xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, parent->pixmap, parent->pm_gc, 6,
638 {dr->x + dr->width, dr->y},
639 {dr->x + dr->width, dr->y + dr->height},
640 {dr->x + dr->width - 1, dr->y},
641 {dr->x + dr->width - 1, dr->y + dr->height},
642 {dr->x, dr->y + dr->height},
646 xcb_change_gc(conn, parent->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){p->color->border});
647 xcb_poly_segment(conn, parent->pixmap, parent->pm_gc, 2, segments);
650 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
654 * Recursively calls x_draw_decoration. This cannot be done in x_push_node
655 * because x_push_node uses focus order to recurse (see the comment above)
656 * while drawing the decoration needs to happen in the actual order.
659 void x_deco_recurse(Con *con) {
661 bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
662 TAILQ_EMPTY(&(con->floating_head));
663 con_state *state = state_for_frame(con->frame);
666 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
667 x_deco_recurse(current);
669 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
670 x_deco_recurse(current);
673 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
676 if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
677 (!leaf || con->mapped))
678 x_draw_decoration(con);
682 * Sets or removes the _NET_WM_STATE_HIDDEN property on con if necessary.
685 static void set_hidden_state(Con *con) {
686 if (con->window == NULL) {
690 con_state *state = state_for_frame(con->frame);
691 bool should_be_hidden = con_is_hidden(con);
692 if (should_be_hidden == state->is_hidden)
695 unsigned int num = 0;
697 if (should_be_hidden) {
698 DLOG("setting _NET_WM_STATE_HIDDEN for con = %p\n", con);
699 values[num++] = A__NET_WM_STATE_HIDDEN;
701 DLOG("removing _NET_WM_STATE_HIDDEN for con = %p\n", con);
704 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id, A__NET_WM_STATE, XCB_ATOM_ATOM, 32, num, values);
705 state->is_hidden = should_be_hidden;
709 * This function pushes the properties of each node of the layout tree to
710 * X11 if they have changed (like the map state, position of the window, …).
711 * It recursively traverses all children of the given node.
714 void x_push_node(Con *con) {
717 Rect rect = con->rect;
719 //DLOG("Pushing changes for node %p / %s\n", con, con->name);
720 state = state_for_frame(con->frame);
722 if (state->name != NULL) {
723 DLOG("pushing name %s for con %p\n", state->name, con);
725 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame,
726 XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(state->name), state->name);
730 if (con->window == NULL) {
731 /* Calculate the height of all window decorations which will be drawn on to
733 uint32_t max_y = 0, max_height = 0;
734 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
735 Rect *dr = &(current->deco_rect);
736 if (dr->y >= max_y && dr->height >= max_height) {
738 max_height = dr->height;
741 rect.height = max_y + max_height;
742 if (rect.height == 0)
746 /* reparent the child window (when the window was moved due to a sticky
748 if (state->need_reparent && con->window != NULL) {
749 DLOG("Reparenting child window\n");
751 /* Temporarily set the event masks to XCB_NONE so that we won’t get
752 * UnmapNotify events (otherwise the handler would close the container).
753 * These events are generated automatically when reparenting. */
754 uint32_t values[] = {XCB_NONE};
755 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
756 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
758 xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
760 values[0] = FRAME_EVENT_MASK;
761 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
762 values[0] = CHILD_EVENT_MASK;
763 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
765 state->old_frame = XCB_NONE;
766 state->need_reparent = false;
769 DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
770 con, con->window->id, con->ignore_unmap);
773 /* The pixmap of a borderless leaf container will not be used except
774 * for the titlebar in a stack or tabs (issue #1013). */
775 bool is_pixmap_needed = (con->border_style != BS_NONE ||
777 con->parent->layout == L_STACKED ||
778 con->parent->layout == L_TABBED);
780 bool fake_notify = false;
781 /* Set new position if rect changed (and if height > 0) or if the pixmap
782 * needs to be recreated */
783 if ((is_pixmap_needed && con->pixmap == XCB_NONE) || (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
785 /* We first create the new pixmap, then render to it, set it as the
786 * background and only afterwards change the window size. This reduces
789 /* As the pixmap only depends on the size and not on the position, it
790 * is enough to check if width/height have changed. Also, we don’t
791 * create a pixmap at all when the window is actually not visible
792 * (height == 0) or when it is not needed. */
793 bool has_rect_changed = (state->rect.width != rect.width || state->rect.height != rect.height);
795 /* Check if the container has an unneeded pixmap left over from
796 * previously having a border or titlebar. */
797 if (!is_pixmap_needed && con->pixmap != XCB_NONE) {
798 xcb_free_pixmap(conn, con->pixmap);
799 con->pixmap = XCB_NONE;
802 if (is_pixmap_needed && (has_rect_changed || con->pixmap == XCB_NONE)) {
803 if (con->pixmap == 0) {
804 con->pixmap = xcb_generate_id(conn);
805 con->pm_gc = xcb_generate_id(conn);
807 xcb_free_pixmap(conn, con->pixmap);
808 xcb_free_gc(conn, con->pm_gc);
811 uint16_t win_depth = root_depth;
813 win_depth = con->window->depth;
815 xcb_create_pixmap(conn, win_depth, con->pixmap, con->frame, rect.width, rect.height);
817 /* For the graphics context, we disable GraphicsExposure events.
818 * Those will be sent when a CopyArea request cannot be fulfilled
819 * properly due to parts of the source being unmapped or otherwise
820 * unavailable. Since we always copy from pixmaps to windows, this
821 * is not a concern for us. */
822 uint32_t values[] = {0};
823 xcb_create_gc(conn, con->pm_gc, con->pixmap, XCB_GC_GRAPHICS_EXPOSURES, values);
825 con->pixmap_recreated = true;
827 /* Don’t render the decoration for windows inside a stack which are
828 * not visible right now */
830 con->parent->layout != L_STACKED ||
831 TAILQ_FIRST(&(con->parent->focus_head)) == con)
832 /* Render the decoration now to make the correct decoration visible
833 * from the very first moment. Later calls will be cached, so this
834 * doesn’t hurt performance. */
838 DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
839 /* flush to ensure that the following commands are sent in a single
840 * buffer and will be processed directly afterwards (the contents of a
841 * window get lost when resizing it, therefore we want to provide it as
842 * fast as possible) */
844 xcb_set_window_rect(conn, con->frame, rect);
845 if (con->pixmap != XCB_NONE)
846 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
849 memcpy(&(state->rect), &rect, sizeof(Rect));
853 /* dito, but for child windows */
854 if (con->window != NULL &&
855 memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
856 DLOG("setting window rect (%d, %d, %d, %d)\n",
857 con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
858 xcb_set_window_rect(conn, con->window->id, con->window_rect);
859 memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
863 /* Map if map state changed, also ensure that the child window
864 * is changed if we are mapped and there is a new, unmapped child window.
865 * Unmaps are handled in x_push_node_unmaps(). */
866 if ((state->mapped != con->mapped || (con->window != NULL && !state->child_mapped)) &&
868 xcb_void_cookie_t cookie;
870 if (con->window != NULL) {
871 /* Set WM_STATE_NORMAL because GTK applications don’t want to
872 * drag & drop if we don’t. Also, xprop(1) needs it. */
873 long data[] = {XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE};
874 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
875 A_WM_STATE, A_WM_STATE, 32, 2, data);
879 if (!state->child_mapped && con->window != NULL) {
880 cookie = xcb_map_window(conn, con->window->id);
882 /* We are interested in EnterNotifys as soon as the window is
884 values[0] = CHILD_EVENT_MASK;
885 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
886 DLOG("mapping child window (serial %d)\n", cookie.sequence);
887 state->child_mapped = true;
890 cookie = xcb_map_window(conn, con->frame);
892 values[0] = FRAME_EVENT_MASK;
893 xcb_change_window_attributes(conn, con->frame, XCB_CW_EVENT_MASK, values);
895 /* copy the pixmap contents to the frame window immediately after mapping */
896 if (con->pixmap != XCB_NONE)
897 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
900 DLOG("mapping container %08x (serial %d)\n", con->frame, cookie.sequence);
901 state->mapped = con->mapped;
904 state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
907 DLOG("Sending fake configure notify\n");
908 fake_absolute_configure_notify(con);
911 set_hidden_state(con);
913 /* Handle all children and floating windows of this node. We recurse
914 * in focus order to display the focused client in a stack first when
915 * switching workspaces (reduces flickering). */
916 TAILQ_FOREACH(current, &(con->focus_head), focused)
917 x_push_node(current);
921 * Same idea as in x_push_node(), but this function only unmaps windows. It is
922 * necessary to split this up to handle new fullscreen clients properly: The
923 * new window needs to be mapped and focus needs to be set *before* the
924 * underlying windows are unmapped. Otherwise, focus will revert to the
925 * PointerRoot and will then be set to the new window, generating unnecessary
926 * FocusIn/FocusOut events.
929 static void x_push_node_unmaps(Con *con) {
933 //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
934 state = state_for_frame(con->frame);
936 /* map/unmap if map state changed, also ensure that the child window
937 * is changed if we are mapped *and* in initial state (meaning the
938 * container was empty before, but now got a child) */
939 if (state->unmap_now) {
940 xcb_void_cookie_t cookie;
941 if (con->window != NULL) {
942 /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
943 long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
944 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
945 A_WM_STATE, A_WM_STATE, 32, 2, data);
948 cookie = xcb_unmap_window(conn, con->frame);
949 DLOG("unmapping container %p / %s (serial %d)\n", con, con->name, cookie.sequence);
950 /* we need to increase ignore_unmap for this container (if it
951 * contains a window) and for every window "under" this one which
952 * contains a window */
953 if (con->window != NULL) {
955 DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame, con->ignore_unmap);
957 state->mapped = con->mapped;
960 /* handle all children and floating windows of this node */
961 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
962 x_push_node_unmaps(current);
964 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
965 x_push_node_unmaps(current);
969 * Returns true if the given container is currently attached to its parent.
971 * TODO: Remove once #1185 has been fixed
973 static bool is_con_attached(Con *con) {
974 if (con->parent == NULL)
978 TAILQ_FOREACH(current, &(con->parent->nodes_head), nodes) {
987 * Pushes all changes (state of each node, see x_push_node() and the window
990 * NOTE: We need to push the stack first so that the windows have the correct
991 * stacking order. This is relevant for workspace switching where we map the
992 * windows because mapping may generate EnterNotify events. When they are
993 * generated in the wrong order, this will cause focus problems when switching
997 void x_push_changes(Con *con) {
999 xcb_query_pointer_cookie_t pointercookie;
1001 /* If we need to warp later, we request the pointer position as soon as possible */
1003 pointercookie = xcb_query_pointer(conn, root);
1006 DLOG("-- PUSHING WINDOW STACK --\n");
1007 //DLOG("Disabling EnterNotify\n");
1008 uint32_t values[1] = {XCB_NONE};
1009 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1011 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1013 //DLOG("Done, EnterNotify disabled\n");
1014 bool order_changed = false;
1015 bool stacking_changed = false;
1017 /* count first, necessary to (re)allocate memory for the bottom-to-top
1018 * stack afterwards */
1020 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
1021 if (con_has_managed_window(state->con))
1024 /* The bottom-to-top window stack of all windows which are managed by i3.
1025 * Used for x_get_window_stack(). */
1026 static xcb_window_t *client_list_windows = NULL;
1027 static int client_list_count = 0;
1029 if (cnt != client_list_count) {
1030 client_list_windows = srealloc(client_list_windows, sizeof(xcb_window_t) * cnt);
1031 client_list_count = cnt;
1034 xcb_window_t *walk = client_list_windows;
1036 /* X11 correctly represents the stack if we push it from bottom to top */
1037 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1038 if (con_has_managed_window(state->con))
1039 memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
1041 //DLOG("stack: 0x%08x\n", state->id);
1042 con_state *prev = CIRCLEQ_PREV(state, state);
1043 con_state *old_prev = CIRCLEQ_PREV(state, old_state);
1044 if (prev != old_prev)
1045 order_changed = true;
1046 if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
1047 stacking_changed = true;
1048 //DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
1050 mask |= XCB_CONFIG_WINDOW_SIBLING;
1051 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
1052 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
1054 xcb_configure_window(conn, prev->id, mask, values);
1056 state->initial = false;
1059 /* If we re-stacked something (or a new window appeared), we need to update
1060 * the _NET_CLIENT_LIST and _NET_CLIENT_LIST_STACKING hints */
1061 if (stacking_changed) {
1062 DLOG("Client list changed (%i clients)\n", cnt);
1063 ewmh_update_client_list_stacking(client_list_windows, client_list_count);
1065 walk = client_list_windows;
1067 /* reorder by initial mapping */
1068 TAILQ_FOREACH(state, &initial_mapping_head, initial_mapping_order) {
1069 if (con_has_managed_window(state->con))
1070 *walk++ = state->con->window->id;
1073 ewmh_update_client_list(client_list_windows, client_list_count);
1076 DLOG("PUSHING CHANGES\n");
1080 xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
1081 if (!pointerreply) {
1082 ELOG("Could not query pointer position, not warping pointer\n");
1084 int mid_x = warp_to->x + (warp_to->width / 2);
1085 int mid_y = warp_to->y + (warp_to->height / 2);
1087 Output *current = get_output_containing(pointerreply->root_x, pointerreply->root_y);
1088 Output *target = get_output_containing(mid_x, mid_y);
1089 if (current != target) {
1090 /* Ignore MotionNotify events generated by warping */
1091 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT});
1092 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
1093 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){ROOT_EVENT_MASK});
1099 //DLOG("Re-enabling EnterNotify\n");
1100 values[0] = FRAME_EVENT_MASK;
1101 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1103 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1105 //DLOG("Done, EnterNotify re-enabled\n");
1107 x_deco_recurse(con);
1109 xcb_window_t to_focus = focused->frame;
1110 if (focused->window != NULL)
1111 to_focus = focused->window->id;
1113 if (focused_id != to_focus) {
1114 if (!focused->mapped) {
1115 DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
1116 /* Invalidate focused_id to correctly focus new windows with the same ID */
1117 focused_id = XCB_NONE;
1119 if (focused->window != NULL &&
1120 focused->window->needs_take_focus &&
1121 focused->window->doesnt_accept_focus) {
1122 DLOG("Updating focus by sending WM_TAKE_FOCUS to window 0x%08x (focused: %p / %s)\n",
1123 to_focus, focused, focused->name);
1124 send_take_focus(to_focus, last_timestamp);
1126 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1128 if (to_focus != last_focused && is_con_attached(focused))
1129 ipc_send_window_event("focus", focused);
1131 DLOG("Updating focus (focused: %p / %s) to X11 window 0x%08x\n", focused, focused->name, to_focus);
1132 /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
1133 * no focus change events for our own focus changes. We only want
1134 * these generated by the clients. */
1135 if (focused->window != NULL) {
1136 values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
1137 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1139 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
1140 if (focused->window != NULL) {
1141 values[0] = CHILD_EVENT_MASK;
1142 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1145 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1147 if (to_focus != XCB_NONE && to_focus != last_focused && focused->window != NULL && is_con_attached(focused))
1148 ipc_send_window_event("focus", focused);
1151 focused_id = last_focused = to_focus;
1155 if (focused_id == XCB_NONE) {
1156 DLOG("Still no window focused, better set focus to the root window\n");
1157 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
1158 ewmh_update_active_window(XCB_WINDOW_NONE);
1163 DLOG("ENDING CHANGES\n");
1165 /* Disable EnterWindow events for windows which will be unmapped in
1166 * x_push_node_unmaps() now. Unmapping windows happens when switching
1167 * workspaces. We want to avoid getting EnterNotifies during that phase
1168 * because they would screw up our focus. One of these cases is having a
1169 * stack with two windows. If the first window is focused and gets
1170 * unmapped, the second one appears under the cursor and therefore gets an
1171 * EnterNotify event. */
1172 values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
1173 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1174 if (!state->unmap_now)
1176 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1179 /* Push all pending unmaps */
1180 x_push_node_unmaps(con);
1182 /* save the current stack as old stack */
1183 CIRCLEQ_FOREACH(state, &state_head, state) {
1184 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
1185 CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
1187 //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
1188 // DLOG("old stack: 0x%08x\n", state->id);
1195 * Raises the specified container in the internal stack of X windows. The
1196 * next call to x_push_changes() will make the change visible in X11.
1199 void x_raise_con(Con *con) {
1201 state = state_for_frame(con->frame);
1202 //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
1204 CIRCLEQ_REMOVE(&state_head, state, state);
1205 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
1209 * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
1210 * of the given name. Used for properly tagging the windows for easily spotting
1211 * i3 windows in xwininfo -root -all.
1214 void x_set_name(Con *con, const char *name) {
1215 struct con_state *state;
1217 if ((state = state_for_frame(con->frame)) == NULL) {
1218 ELOG("window state not found\n");
1223 state->name = sstrdup(name);
1227 * Set up the I3_SHMLOG_PATH atom.
1230 void update_shmlog_atom() {
1231 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1232 A_I3_SHMLOG_PATH, A_UTF8_STRING, 8,
1233 strlen(shmlogname), shmlogname);
1237 * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
1240 void x_set_i3_atoms(void) {
1241 pid_t pid = getpid();
1242 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
1243 (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
1244 current_socketpath);
1245 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_PID, XCB_ATOM_CARDINAL, 32, 1, &pid);
1246 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
1247 strlen(current_configpath), current_configpath);
1248 update_shmlog_atom();
1252 * Set warp_to coordinates. This will trigger on the next call to
1256 void x_set_warp_to(Rect *rect) {
1257 if (config.mouse_warping != POINTER_WARPING_NONE)
1262 * Applies the given mask to the event mask of every i3 window decoration X11
1263 * window. This is useful to disable EnterNotify while resizing so that focus
1267 void x_mask_event_mask(uint32_t mask) {
1268 uint32_t values[] = {FRAME_EVENT_MASK & mask};
1271 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1273 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);