]> git.sur5r.net Git - i3/i3/blob - src/x.c
Merge pull request #1864 from Airblader/feature-1861-class-instance
[i3/i3] / src / x.c
1 #undef I3__FILE__
2 #define I3__FILE__ "x.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * x.c: Interface to X11, transfers our in-memory state to X11 (see also
10  *      render.c). Basically a big state machine.
11  *
12  */
13 #include "all.h"
14
15 /* Stores the X11 window ID of the currently focused window */
16 xcb_window_t focused_id = XCB_NONE;
17
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;
22
23 /* Stores coordinates to warp mouse pointer to if set */
24 static Rect *warp_to;
25
26 /*
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().
32  *
33  */
34 typedef struct con_state {
35     xcb_window_t id;
36     bool mapped;
37     bool unmap_now;
38     bool child_mapped;
39     bool is_hidden;
40
41     /** The con for which this state is. */
42     Con *con;
43
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). */
47     bool need_reparent;
48     xcb_window_t old_frame;
49
50     Rect rect;
51     Rect window_rect;
52
53     bool initial;
54
55     char *name;
56
57     CIRCLEQ_ENTRY(con_state) state;
58     CIRCLEQ_ENTRY(con_state) old_state;
59     TAILQ_ENTRY(con_state) initial_mapping_order;
60 } con_state;
61
62 CIRCLEQ_HEAD(state_head, con_state) state_head =
63     CIRCLEQ_HEAD_INITIALIZER(state_head);
64
65 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
66     CIRCLEQ_HEAD_INITIALIZER(old_state_head);
67
68 TAILQ_HEAD(initial_mapping_head, con_state) initial_mapping_head =
69     TAILQ_HEAD_INITIALIZER(initial_mapping_head);
70
71 /*
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
75  * requested).
76  *
77  */
78 static con_state *state_for_frame(xcb_window_t window) {
79     con_state *state;
80     CIRCLEQ_FOREACH(state, &state_head, state)
81     if (state->id == window)
82         return state;
83
84     /* TODO: better error handling? */
85     ELOG("No state found\n");
86     assert(false);
87     return NULL;
88 }
89
90 /*
91  * Initializes the X11 part for the given container. Called exactly once for
92  * every container from con_new().
93  *
94  */
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 */
98
99     uint32_t mask = 0;
100     uint32_t values[5];
101
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);
110
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;
117
118         mask |= XCB_CW_BORDER_PIXEL;
119         values[1] = root_screen->black_pixel;
120
121         /* our own frames should not be managed */
122         mask |= XCB_CW_OVERRIDE_REDIRECT;
123         values[2] = 1;
124
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;
128
129         mask |= XCB_CW_COLORMAP;
130         values[4] = win_colormap;
131     } else {
132         /* our own frames should not be managed */
133         mask = XCB_CW_OVERRIDE_REDIRECT;
134         values[0] = 1;
135
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;
139
140         mask |= XCB_CW_COLORMAP;
141         values[2] = colormap;
142     }
143
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);
146
147     if (win_colormap != XCB_NONE)
148         xcb_free_colormap(conn, win_colormap);
149
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);
159 }
160
161 /*
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.
165  *
166  */
167 void x_reinit(Con *con) {
168     struct con_state *state;
169
170     if ((state = state_for_frame(con->frame)) == NULL) {
171         ELOG("window state not found\n");
172         return;
173     }
174
175     DLOG("resetting state %p to initial\n", state);
176     state->initial = true;
177     state->child_mapped = false;
178     state->con = con;
179     memset(&(state->window_rect), 0, sizeof(Rect));
180 }
181
182 /*
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().
185  *
186  */
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");
191         return;
192     }
193
194     state->need_reparent = true;
195     state->old_frame = old->frame;
196 }
197
198 /*
199  * Moves a child window from Container src to Container dest.
200  *
201  */
202 void x_move_win(Con *src, Con *dest) {
203     struct con_state *state_src, *state_dest;
204
205     if ((state_src = state_for_frame(src->frame)) == NULL) {
206         ELOG("window state for src not found\n");
207         return;
208     }
209
210     if ((state_dest = state_for_frame(dest->frame)) == NULL) {
211         ELOG("window state for dest not found\n");
212         return;
213     }
214
215     state_dest->con = state_src->con;
216     state_src->con = NULL;
217
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");
222     }
223 }
224
225 /*
226  * Kills the window decoration associated with the given container.
227  *
228  */
229 void x_con_kill(Con *con) {
230     con_state *state;
231
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);
239     FREE(state->name);
240     free(state);
241
242     /* Invalidate focused_id to correctly focus new windows with the same ID */
243     focused_id = last_focused = XCB_NONE;
244 }
245
246 /*
247  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
248  *
249  */
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;
253     bool result = false;
254
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)
257         return false;
258
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)
262             result = true;
263
264     xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
265
266     return result;
267 }
268
269 /*
270  * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
271  *
272  */
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);
279         } else {
280             LOG("Killing the X11 client which owns window 0x%08x\n", window);
281             xcb_kill_client(conn, window);
282         }
283         return;
284     }
285
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;
291
292     ev->response_type = XCB_CLIENT_MESSAGE;
293     ev->window = window;
294     ev->type = A_WM_PROTOCOLS;
295     ev->format = 32;
296     ev->data.data32[0] = A_WM_DELETE_WINDOW;
297     ev->data.data32[1] = XCB_CURRENT_TIME;
298
299     LOG("Sending WM_DELETE to the client\n");
300     xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)ev);
301     xcb_flush(conn);
302     free(event);
303 }
304
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();
309
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;
315
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);
323
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;
329
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;
335
336             buffer_len = buffer_len - strlen("%instance") + strlen(escaped_instance);
337             walk += strlen("%instance") - 1;
338         }
339     }
340
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++) {
345         if (*walk != '%') {
346             *(outwalk++) = *walk;
347             continue;
348         }
349
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");
359         }
360     }
361     *outwalk = '\0';
362
363     i3String *formatted = i3string_from_utf8(buffer);
364     i3string_set_markup(formatted, is_markup);
365     return formatted;
366 }
367
368 /*
369  * Draws the decoration of the given container onto its parent.
370  *
371  */
372 void x_draw_decoration(Con *con) {
373     Con *parent = con->parent;
374     bool leaf = con_is_leaf(con);
375
376     /* This code needs to run for:
377      *  • leaf containers
378      *  • non-leaf containers which are in a stacked/tabbed container
379      *
380      * It does not need to run for:
381      *  • direct children of outputs or dockareas
382      *  • floating containers (they don’t have a decoration)
383      */
384     if ((!leaf &&
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)
390         return;
391
392     /* Skip containers whose height is 0 (for example empty dockareas) */
393     if (con->rect.height == 0)
394         return;
395
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)
400         return;
401
402     /* 1: build deco_params and compare with cache */
403     struct deco_render_params *p = scalloc(1, sizeof(struct deco_render_params));
404
405     /* find out which colors to use */
406     if (con->urgent)
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;
412     else
413         p->color = &config.client.unfocused;
414
415     p->border_style = con_border_style(con);
416
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;
425
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) {
432         free(p);
433         goto copy_pixmaps;
434     }
435
436     Con *next = con;
437     while ((next = TAILQ_NEXT(next, nodes))) {
438         FREE(next->deco_render_params);
439     }
440
441     FREE(con->deco_render_params);
442     con->deco_render_params = p;
443
444     if (con->window != NULL && con->window->name_x_changed)
445         con->window->name_x_changed = false;
446
447     parent->pixmap_recreated = false;
448     con->pixmap_recreated = false;
449     con->mark_changed = false;
450
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[] = {
454             /* top area */
455             {0, 0, r->width, w->y},
456             /* bottom area */
457             {0, (w->y + w->height), r->width, r->height - (w->y + w->height)},
458             /* left area */
459             {0, 0, w->x, r->height},
460             /* right area */
461             {w->x + w->width, 0, r->width - (w->x + w->width), r->height}};
462 #if 0
463         for (int i = 0; i < 4; i++)
464             DLOG("rect is (%d, %d) with %d x %d\n",
465                     background[i].x,
466                     background[i].y,
467                     background[i].width,
468                     background[i].height
469                 );
470 #endif
471
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);
474     }
475
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;
481
482         Rect br = con_border_style_rect(con);
483 #if 0
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);
487 #endif
488
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);
497         }
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);
501         }
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);
505         }
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);
510         }
511
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)}});
526         }
527     }
528
529     /* if this is a borderless/1pixel window, we don’t need to render the
530      * decoration. */
531     if (p->border_style != BS_NORMAL)
532         goto copy_pixmaps;
533
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);
538
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)) {
547         deco_diff_l = 0;
548         deco_diff_r = 0;
549     }
550     xcb_segment_t segments[] = {
551         {dr->x, dr->y,
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);
556
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;
560
561     struct Window *win = con->window;
562     if (win == NULL) {
563         /* we have a split container which gets a representation
564          * of its children as title
565          */
566         char *title;
567         char *tree = con_get_tree_representation(con);
568         sasprintf(&title, "i3: %s", tree);
569         free(tree);
570
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);
575         free(title);
576
577         goto after_title;
578     }
579
580     if (win->name == NULL)
581         goto copy_pixmaps;
582
583     int indent_level = 0,
584         indent_mult = 0;
585     Con *il_parent = parent;
586     if (il_parent->layout != L_STACKED) {
587         while (1) {
588             //DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
589             if (il_parent->layout == L_STACKED)
590                 indent_level++;
591             if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
592                 break;
593             il_parent = il_parent->parent;
594             indent_mult++;
595         }
596     }
597     //DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
598     int indent_px = (indent_level * 5) * indent_mult;
599
600     int mark_width = 0;
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);
607
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);
611
612         I3STRING_FREE(mark);
613     }
614
615     i3String *title = win->title_format == NULL ? win->name : parse_title_format(win);
616     draw_text(title,
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);
622
623 after_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). */
628
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});
633     } else {
634         xcb_change_gc(conn, parent->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){p->color->background});
635     }
636     xcb_poly_line(conn, XCB_COORD_MODE_ORIGIN, parent->pixmap, parent->pm_gc, 6,
637                   (xcb_point_t[]){
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},
643                       {dr->x, dr->y},
644                   });
645
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);
648
649 copy_pixmaps:
650     xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
651 }
652
653 /*
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.
657  *
658  */
659 void x_deco_recurse(Con *con) {
660     Con *current;
661     bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
662                 TAILQ_EMPTY(&(con->floating_head));
663     con_state *state = state_for_frame(con->frame);
664
665     if (!leaf) {
666         TAILQ_FOREACH(current, &(con->nodes_head), nodes)
667         x_deco_recurse(current);
668
669         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
670         x_deco_recurse(current);
671
672         if (state->mapped)
673             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
674     }
675
676     if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
677         (!leaf || con->mapped))
678         x_draw_decoration(con);
679 }
680
681 /*
682  * Sets or removes the _NET_WM_STATE_HIDDEN property on con if necessary.
683  *
684  */
685 static void set_hidden_state(Con *con) {
686     if (con->window == NULL) {
687         return;
688     }
689
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)
693         return;
694
695     unsigned int num = 0;
696     uint32_t values[1];
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;
700     } else {
701         DLOG("removing _NET_WM_STATE_HIDDEN for con = %p\n", con);
702     }
703
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;
706 }
707
708 /*
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.
712  *
713  */
714 void x_push_node(Con *con) {
715     Con *current;
716     con_state *state;
717     Rect rect = con->rect;
718
719     //DLOG("Pushing changes for node %p / %s\n", con, con->name);
720     state = state_for_frame(con->frame);
721
722     if (state->name != NULL) {
723         DLOG("pushing name %s for con %p\n", state->name, con);
724
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);
727         FREE(state->name);
728     }
729
730     if (con->window == NULL) {
731         /* Calculate the height of all window decorations which will be drawn on to
732          * this frame. */
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) {
737                 max_y = dr->y;
738                 max_height = dr->height;
739             }
740         }
741         rect.height = max_y + max_height;
742         if (rect.height == 0)
743             con->mapped = false;
744     }
745
746     /* reparent the child window (when the window was moved due to a sticky
747      * container) */
748     if (state->need_reparent && con->window != NULL) {
749         DLOG("Reparenting child window\n");
750
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);
757
758         xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
759
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);
764
765         state->old_frame = XCB_NONE;
766         state->need_reparent = false;
767
768         con->ignore_unmap++;
769         DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
770              con, con->window->id, con->ignore_unmap);
771     }
772
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 ||
776                              !con_is_leaf(con) ||
777                              con->parent->layout == L_STACKED ||
778                              con->parent->layout == L_TABBED);
779
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 &&
784                                                           rect.height > 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
787          * flickering. */
788
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);
794
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;
800         }
801
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);
806             } else {
807                 xcb_free_pixmap(conn, con->pixmap);
808                 xcb_free_gc(conn, con->pm_gc);
809             }
810
811             uint16_t win_depth = root_depth;
812             if (con->window)
813                 win_depth = con->window->depth;
814
815             xcb_create_pixmap(conn, win_depth, con->pixmap, con->frame, rect.width, rect.height);
816
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);
824
825             con->pixmap_recreated = true;
826
827             /* Don’t render the decoration for windows inside a stack which are
828              * not visible right now */
829             if (!con->parent ||
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. */
835                 x_deco_recurse(con);
836         }
837
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) */
843         xcb_flush(conn);
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);
847         xcb_flush(conn);
848
849         memcpy(&(state->rect), &rect, sizeof(Rect));
850         fake_notify = true;
851     }
852
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));
860         fake_notify = true;
861     }
862
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)) &&
867         con->mapped) {
868         xcb_void_cookie_t cookie;
869
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);
876         }
877
878         uint32_t values[1];
879         if (!state->child_mapped && con->window != NULL) {
880             cookie = xcb_map_window(conn, con->window->id);
881
882             /* We are interested in EnterNotifys as soon as the window is
883              * mapped */
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;
888         }
889
890         cookie = xcb_map_window(conn, con->frame);
891
892         values[0] = FRAME_EVENT_MASK;
893         xcb_change_window_attributes(conn, con->frame, XCB_CW_EVENT_MASK, values);
894
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);
898         xcb_flush(conn);
899
900         DLOG("mapping container %08x (serial %d)\n", con->frame, cookie.sequence);
901         state->mapped = con->mapped;
902     }
903
904     state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
905
906     if (fake_notify) {
907         DLOG("Sending fake configure notify\n");
908         fake_absolute_configure_notify(con);
909     }
910
911     set_hidden_state(con);
912
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);
918 }
919
920 /*
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.
927  *
928  */
929 static void x_push_node_unmaps(Con *con) {
930     Con *current;
931     con_state *state;
932
933     //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
934     state = state_for_frame(con->frame);
935
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);
946         }
947
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) {
954             con->ignore_unmap++;
955             DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame, con->ignore_unmap);
956         }
957         state->mapped = con->mapped;
958     }
959
960     /* handle all children and floating windows of this node */
961     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
962     x_push_node_unmaps(current);
963
964     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
965     x_push_node_unmaps(current);
966 }
967
968 /*
969  * Returns true if the given container is currently attached to its parent.
970  *
971  * TODO: Remove once #1185 has been fixed
972  */
973 static bool is_con_attached(Con *con) {
974     if (con->parent == NULL)
975         return false;
976
977     Con *current;
978     TAILQ_FOREACH(current, &(con->parent->nodes_head), nodes) {
979         if (current == con)
980             return true;
981     }
982
983     return false;
984 }
985
986 /*
987  * Pushes all changes (state of each node, see x_push_node() and the window
988  * stack) to X11.
989  *
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
994  * workspaces.
995  *
996  */
997 void x_push_changes(Con *con) {
998     con_state *state;
999     xcb_query_pointer_cookie_t pointercookie;
1000
1001     /* If we need to warp later, we request the pointer position as soon as possible */
1002     if (warp_to) {
1003         pointercookie = xcb_query_pointer(conn, root);
1004     }
1005
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) {
1010         if (state->mapped)
1011             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1012     }
1013     //DLOG("Done, EnterNotify disabled\n");
1014     bool order_changed = false;
1015     bool stacking_changed = false;
1016
1017     /* count first, necessary to (re)allocate memory for the bottom-to-top
1018      * stack afterwards */
1019     int cnt = 0;
1020     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
1021     if (con_has_managed_window(state->con))
1022         cnt++;
1023
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;
1028
1029     if (cnt != client_list_count) {
1030         client_list_windows = srealloc(client_list_windows, sizeof(xcb_window_t) * cnt);
1031         client_list_count = cnt;
1032     }
1033
1034     xcb_window_t *walk = client_list_windows;
1035
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));
1040
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);
1049             uint32_t mask = 0;
1050             mask |= XCB_CONFIG_WINDOW_SIBLING;
1051             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
1052             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
1053
1054             xcb_configure_window(conn, prev->id, mask, values);
1055         }
1056         state->initial = false;
1057     }
1058
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);
1064
1065         walk = client_list_windows;
1066
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;
1071         }
1072
1073         ewmh_update_client_list(client_list_windows, client_list_count);
1074     }
1075
1076     DLOG("PUSHING CHANGES\n");
1077     x_push_node(con);
1078
1079     if (warp_to) {
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");
1083         } else {
1084             int mid_x = warp_to->x + (warp_to->width / 2);
1085             int mid_y = warp_to->y + (warp_to->height / 2);
1086
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});
1094             }
1095         }
1096         warp_to = NULL;
1097     }
1098
1099     //DLOG("Re-enabling EnterNotify\n");
1100     values[0] = FRAME_EVENT_MASK;
1101     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1102         if (state->mapped)
1103             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1104     }
1105     //DLOG("Done, EnterNotify re-enabled\n");
1106
1107     x_deco_recurse(con);
1108
1109     xcb_window_t to_focus = focused->frame;
1110     if (focused->window != NULL)
1111         to_focus = focused->window->id;
1112
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;
1118         } else {
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);
1125
1126                 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1127
1128                 if (to_focus != last_focused && is_con_attached(focused))
1129                     ipc_send_window_event("focus", focused);
1130             } else {
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);
1138                 }
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);
1143                 }
1144
1145                 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1146
1147                 if (to_focus != XCB_NONE && to_focus != last_focused && focused->window != NULL && is_con_attached(focused))
1148                     ipc_send_window_event("focus", focused);
1149             }
1150
1151             focused_id = last_focused = to_focus;
1152         }
1153     }
1154
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);
1159         focused_id = root;
1160     }
1161
1162     xcb_flush(conn);
1163     DLOG("ENDING CHANGES\n");
1164
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)
1175             continue;
1176         xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1177     }
1178
1179     /* Push all pending unmaps */
1180     x_push_node_unmaps(con);
1181
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);
1186     }
1187     //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
1188     //    DLOG("old stack: 0x%08x\n", state->id);
1189     //}
1190
1191     xcb_flush(conn);
1192 }
1193
1194 /*
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.
1197  *
1198  */
1199 void x_raise_con(Con *con) {
1200     con_state *state;
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);
1203
1204     CIRCLEQ_REMOVE(&state_head, state, state);
1205     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
1206 }
1207
1208 /*
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.
1212  *
1213  */
1214 void x_set_name(Con *con, const char *name) {
1215     struct con_state *state;
1216
1217     if ((state = state_for_frame(con->frame)) == NULL) {
1218         ELOG("window state not found\n");
1219         return;
1220     }
1221
1222     FREE(state->name);
1223     state->name = sstrdup(name);
1224 }
1225
1226 /*
1227  * Set up the I3_SHMLOG_PATH atom.
1228  *
1229  */
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);
1234 }
1235
1236 /*
1237  * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
1238  *
1239  */
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();
1249 }
1250
1251 /*
1252  * Set warp_to coordinates.  This will trigger on the next call to
1253  * x_push_changes().
1254  *
1255  */
1256 void x_set_warp_to(Rect *rect) {
1257     if (config.mouse_warping != POINTER_WARPING_NONE)
1258         warp_to = rect;
1259 }
1260
1261 /*
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
1264  * is untouched.
1265  *
1266  */
1267 void x_mask_event_mask(uint32_t mask) {
1268     uint32_t values[] = {FRAME_EVENT_MASK & mask};
1269
1270     con_state *state;
1271     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1272         if (state->mapped)
1273             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1274     }
1275 }