]> git.sur5r.net Git - i3/i3/blob - src/x.c
Redraw right-hand side border correctly.
[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 #ifndef MAX
16 #define MAX(x, y) ((x) > (y) ? (x) : (y))
17 #endif
18
19 xcb_window_t ewmh_window;
20
21 /* Stores the X11 window ID of the currently focused window */
22 xcb_window_t focused_id = XCB_NONE;
23
24 /* Because 'focused_id' might be reset to force input focus, we separately keep
25  * track of the X11 window ID to be able to always tell whether the focused
26  * window actually changed. */
27 static xcb_window_t last_focused = XCB_NONE;
28
29 /* Stores coordinates to warp mouse pointer to if set */
30 static Rect *warp_to;
31
32 /*
33  * Describes the X11 state we may modify (map state, position, window stack).
34  * There is one entry per container. The state represents the current situation
35  * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
36  * which represents the order that will be pushed to X11, while old_state_head
37  * represents the current order). It will be updated in x_push_changes().
38  *
39  */
40 typedef struct con_state {
41     xcb_window_t id;
42     bool mapped;
43     bool unmap_now;
44     bool child_mapped;
45     bool is_hidden;
46
47     /** The con for which this state is. */
48     Con *con;
49
50     /* For reparenting, we have a flag (need_reparent) and the X ID of the old
51      * frame this window was in. The latter is necessary because we need to
52      * ignore UnmapNotify events (by changing the window event mask). */
53     bool need_reparent;
54     xcb_window_t old_frame;
55
56     Rect rect;
57     Rect window_rect;
58
59     bool initial;
60
61     char *name;
62
63     CIRCLEQ_ENTRY(con_state) state;
64     CIRCLEQ_ENTRY(con_state) old_state;
65     TAILQ_ENTRY(con_state) initial_mapping_order;
66 } con_state;
67
68 CIRCLEQ_HEAD(state_head, con_state) state_head =
69     CIRCLEQ_HEAD_INITIALIZER(state_head);
70
71 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
72     CIRCLEQ_HEAD_INITIALIZER(old_state_head);
73
74 TAILQ_HEAD(initial_mapping_head, con_state) initial_mapping_head =
75     TAILQ_HEAD_INITIALIZER(initial_mapping_head);
76
77 /*
78  * Returns the container state for the given frame. This function always
79  * returns a container state (otherwise, there is a bug in the code and the
80  * container state of a container for which x_con_init() was not called was
81  * requested).
82  *
83  */
84 static con_state *state_for_frame(xcb_window_t window) {
85     con_state *state;
86     CIRCLEQ_FOREACH(state, &state_head, state)
87     if (state->id == window)
88         return state;
89
90     /* TODO: better error handling? */
91     ELOG("No state found\n");
92     assert(false);
93     return NULL;
94 }
95
96 /*
97  * Initializes the X11 part for the given container. Called exactly once for
98  * every container from con_new().
99  *
100  */
101 void x_con_init(Con *con, uint16_t depth) {
102     /* TODO: maybe create the window when rendering first? we could then even
103      * get the initial geometry right */
104
105     uint32_t mask = 0;
106     uint32_t values[5];
107
108     /* For custom visuals, we need to create a colormap before creating
109      * this window. It will be freed directly after creating the window. */
110     xcb_visualid_t visual = get_visualid_by_depth(depth);
111     xcb_colormap_t win_colormap = xcb_generate_id(conn);
112     xcb_create_colormap_checked(conn, XCB_COLORMAP_ALLOC_NONE, win_colormap, root, visual);
113
114     /* We explicitly set a background color and border color (even though we
115      * don’t even have a border) because the X11 server requires us to when
116      * using 32 bit color depths, see
117      * http://stackoverflow.com/questions/3645632 */
118     mask |= XCB_CW_BACK_PIXEL;
119     values[0] = root_screen->black_pixel;
120
121     mask |= XCB_CW_BORDER_PIXEL;
122     values[1] = root_screen->black_pixel;
123
124     /* our own frames should not be managed */
125     mask |= XCB_CW_OVERRIDE_REDIRECT;
126     values[2] = 1;
127
128     /* see include/xcb.h for the FRAME_EVENT_MASK */
129     mask |= XCB_CW_EVENT_MASK;
130     values[3] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
131
132     mask |= XCB_CW_COLORMAP;
133     values[4] = win_colormap;
134
135     Rect dims = {-15, -15, 10, 10};
136     xcb_window_t frame_id = create_window(conn, dims, depth, visual, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER, false, mask, values);
137     draw_util_surface_init(conn, &(con->frame), frame_id, get_visualtype_by_id(visual), dims.width, dims.height);
138     xcb_change_property(conn,
139                         XCB_PROP_MODE_REPLACE,
140                         con->frame.id,
141                         XCB_ATOM_WM_CLASS,
142                         XCB_ATOM_STRING,
143                         8,
144                         (strlen("i3-frame") + 1) * 2,
145                         "i3-frame\0i3-frame\0");
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.id;
152     state->mapped = false;
153     state->initial = true;
154     DLOG("Adding window 0x%08x to lists\n", state->id);
155     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
156     CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
157     TAILQ_INSERT_TAIL(&initial_mapping_head, state, initial_mapping_order);
158     DLOG("adding new state for window id 0x%08x\n", state->id);
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.id)) == 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.id)) == 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.id;
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.id)) == NULL) {
206         ELOG("window state for src not found\n");
207         return;
208     }
209
210     if ((state_dest = state_for_frame(dest->frame.id)) == 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     draw_util_surface_free(conn, &(con->frame));
233     draw_util_surface_free(conn, &(con->frame_buffer));
234     xcb_destroy_window(conn, con->frame.id);
235     xcb_free_pixmap(conn, con->frame_buffer.id);
236     state = state_for_frame(con->frame.id);
237     CIRCLEQ_REMOVE(&state_head, state, state);
238     CIRCLEQ_REMOVE(&old_state_head, state, old_state);
239     TAILQ_REMOVE(&initial_mapping_head, state, initial_mapping_order);
240     FREE(state->name);
241     free(state);
242
243     /* Invalidate focused_id to correctly focus new windows with the same ID */
244     focused_id = last_focused = XCB_NONE;
245 }
246
247 /*
248  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
249  *
250  */
251 bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
252     xcb_get_property_cookie_t cookie;
253     xcb_icccm_get_wm_protocols_reply_t protocols;
254     bool result = false;
255
256     cookie = xcb_icccm_get_wm_protocols(conn, window, A_WM_PROTOCOLS);
257     if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
258         return false;
259
260     /* Check if the client’s protocols have the requested atom set */
261     for (uint32_t i = 0; i < protocols.atoms_len; i++)
262         if (protocols.atoms[i] == atom)
263             result = true;
264
265     xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
266
267     return result;
268 }
269
270 /*
271  * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
272  *
273  */
274 void x_window_kill(xcb_window_t window, kill_window_t kill_window) {
275     /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
276     if (!window_supports_protocol(window, A_WM_DELETE_WINDOW)) {
277         if (kill_window == KILL_WINDOW) {
278             LOG("Killing specific window 0x%08x\n", window);
279             xcb_destroy_window(conn, window);
280         } else {
281             LOG("Killing the X11 client which owns window 0x%08x\n", window);
282             xcb_kill_client(conn, window);
283         }
284         return;
285     }
286
287     /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
288      * In order to properly initialize these bytes, we allocate 32 bytes even
289      * though we only need less for an xcb_configure_notify_event_t */
290     void *event = scalloc(32, 1);
291     xcb_client_message_event_t *ev = event;
292
293     ev->response_type = XCB_CLIENT_MESSAGE;
294     ev->window = window;
295     ev->type = A_WM_PROTOCOLS;
296     ev->format = 32;
297     ev->data.data32[0] = A_WM_DELETE_WINDOW;
298     ev->data.data32[1] = XCB_CURRENT_TIME;
299
300     LOG("Sending WM_DELETE to the client\n");
301     xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char *)ev);
302     xcb_flush(conn);
303     free(event);
304 }
305
306 static void x_draw_title_border(Con *con, struct deco_render_params *p) {
307     assert(con->parent != NULL);
308
309     Rect *dr = &(con->deco_rect);
310     adjacent_t borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
311     int deco_diff_l = borders_to_hide & ADJ_LEFT_SCREEN_EDGE ? 0 : con->current_border_width;
312     int deco_diff_r = borders_to_hide & ADJ_RIGHT_SCREEN_EDGE ? 0 : con->current_border_width;
313     if (con->parent->layout == L_TABBED ||
314         (con->parent->layout == L_STACKED && TAILQ_NEXT(con, nodes) != NULL)) {
315         deco_diff_l = 0;
316         deco_diff_r = 0;
317     }
318
319     draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
320                         dr->x, dr->y, dr->width, 1);
321
322     draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
323                         dr->x + deco_diff_l, dr->y + dr->height - 1, dr->width - (deco_diff_l + deco_diff_r), 1);
324 }
325
326 static void x_draw_decoration_after_title(Con *con, struct deco_render_params *p) {
327     assert(con->parent != NULL);
328
329     Rect *dr = &(con->deco_rect);
330
331     /* Redraw the right border to cut off any text that went past it.
332      * This is necessary when the text was drawn using XCB since cutting text off
333      * automatically does not work there. For pango rendering, this isn't necessary. */
334     if (!font_is_pango()) {
335         /* We actually only redraw the far right two pixels as that is the
336          * distance we keep from the edge (not the entire border width).
337          * Redrawing the entire border would cause text to be cut off. */
338         draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->background,
339                             dr->x + dr->width - 2 * logical_px(1),
340                             dr->y,
341                             2 * logical_px(1),
342                             dr->height);
343     }
344
345     /* Draw a 1px separator line before and after every tab, so that tabs can
346      * be easily distinguished. */
347     if (con->parent->layout == L_TABBED) {
348         /* Left side */
349         draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
350                             dr->x, dr->y, 1, dr->height);
351
352         /* Right side */
353         draw_util_rectangle(conn, &(con->parent->frame_buffer), p->color->border,
354                             dr->x + dr->width - 1, dr->y, 1, dr->height);
355     }
356
357     /* Redraw the border. */
358     x_draw_title_border(con, p);
359 }
360
361 /*
362  * Draws the decoration of the given container onto its parent.
363  *
364  */
365 void x_draw_decoration(Con *con) {
366     Con *parent = con->parent;
367     bool leaf = con_is_leaf(con);
368
369     /* This code needs to run for:
370      *  • leaf containers
371      *  • non-leaf containers which are in a stacked/tabbed container
372      *
373      * It does not need to run for:
374      *  • direct children of outputs or dockareas
375      *  • floating containers (they don’t have a decoration)
376      */
377     if ((!leaf &&
378          parent->layout != L_STACKED &&
379          parent->layout != L_TABBED) ||
380         parent->type == CT_OUTPUT ||
381         parent->type == CT_DOCKAREA ||
382         con->type == CT_FLOATING_CON)
383         return;
384
385     /* Skip containers whose height is 0 (for example empty dockareas) */
386     if (con->rect.height == 0)
387         return;
388
389     /* Skip containers whose pixmap has not yet been created (can happen when
390      * decoration rendering happens recursively for a window for which
391      * x_push_node() was not yet called) */
392     if (leaf && con->frame_buffer.id == XCB_NONE)
393         return;
394
395     /* 1: build deco_params and compare with cache */
396     struct deco_render_params *p = scalloc(1, sizeof(struct deco_render_params));
397
398     /* find out which colors to use */
399     if (con->urgent)
400         p->color = &config.client.urgent;
401     else if (con == focused || con_inside_focused(con))
402         p->color = &config.client.focused;
403     else if (con == TAILQ_FIRST(&(parent->focus_head)))
404         p->color = &config.client.focused_inactive;
405     else
406         p->color = &config.client.unfocused;
407
408     p->border_style = con_border_style(con);
409
410     Rect *r = &(con->rect);
411     Rect *w = &(con->window_rect);
412     p->con_rect = (struct width_height){r->width, r->height};
413     p->con_window_rect = (struct width_height){w->width, w->height};
414     p->con_deco_rect = con->deco_rect;
415     p->background = config.client.background;
416     p->con_is_leaf = con_is_leaf(con);
417     p->parent_layout = con->parent->layout;
418
419     if (con->deco_render_params != NULL &&
420         (con->window == NULL || !con->window->name_x_changed) &&
421         !parent->pixmap_recreated &&
422         !con->pixmap_recreated &&
423         !con->mark_changed &&
424         memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
425         free(p);
426         goto copy_pixmaps;
427     }
428
429     Con *next = con;
430     while ((next = TAILQ_NEXT(next, nodes))) {
431         FREE(next->deco_render_params);
432     }
433
434     FREE(con->deco_render_params);
435     con->deco_render_params = p;
436
437     if (con->window != NULL && con->window->name_x_changed)
438         con->window->name_x_changed = false;
439
440     parent->pixmap_recreated = false;
441     con->pixmap_recreated = false;
442     con->mark_changed = false;
443
444     /* 2: draw the client.background, but only for the parts around the window_rect */
445     if (con->window != NULL) {
446         /* top area */
447         draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
448                             0, 0, r->width, w->y);
449         /* bottom area */
450         draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
451                             0, w->y + w->height, r->width, r->height - (w->y + w->height));
452         /* left area */
453         draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
454                             0, 0, w->x, r->height);
455         /* right area */
456         draw_util_rectangle(conn, &(con->frame_buffer), config.client.background,
457                             w->x + w->width, 0, r->width - (w->x + w->width), r->height);
458     }
459
460     /* 3: draw a rectangle in border color around the client */
461     if (p->border_style != BS_NONE && p->con_is_leaf) {
462         /* We might hide some borders adjacent to the screen-edge */
463         adjacent_t borders_to_hide = ADJ_NONE;
464         borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
465
466         Rect br = con_border_style_rect(con);
467 #if 0
468         DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
469         DLOG("border_rect spans (%d, %d) with %d x %d\n", br.x, br.y, br.width, br.height);
470         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);
471 #endif
472
473         /* These rectangles represent the border around the child window
474          * (left, bottom and right part). We don’t just fill the whole
475          * rectangle because some childs are not freely resizable and we want
476          * their background color to "shine through". */
477         if (!(borders_to_hide & ADJ_LEFT_SCREEN_EDGE)) {
478             draw_util_rectangle(conn, &(con->frame_buffer), p->color->child_border, 0, 0, br.x, r->height);
479         }
480         if (!(borders_to_hide & ADJ_RIGHT_SCREEN_EDGE)) {
481             draw_util_rectangle(conn, &(con->frame_buffer),
482                                 p->color->child_border, r->width + (br.width + br.x), 0,
483                                 -(br.width + br.x), r->height);
484         }
485         if (!(borders_to_hide & ADJ_LOWER_SCREEN_EDGE)) {
486             draw_util_rectangle(conn, &(con->frame_buffer),
487                                 p->color->child_border, br.x, r->height + (br.height + br.y),
488                                 r->width + br.width, -(br.height + br.y));
489         }
490         /* pixel border needs an additional line at the top */
491         if (p->border_style == BS_PIXEL && !(borders_to_hide & ADJ_UPPER_SCREEN_EDGE)) {
492             draw_util_rectangle(conn, &(con->frame_buffer),
493                                 p->color->child_border, br.x, 0, r->width + br.width, br.y);
494         }
495
496         /* Highlight the side of the border at which the next window will be
497          * opened if we are rendering a single window within a split container
498          * (which is undistinguishable from a single window outside a split
499          * container otherwise. */
500         if (TAILQ_NEXT(con, nodes) == NULL &&
501             TAILQ_PREV(con, nodes_head, nodes) == NULL &&
502             con->parent->type != CT_FLOATING_CON) {
503             if (p->parent_layout == L_SPLITH) {
504                 draw_util_rectangle(conn, &(con->frame_buffer), p->color->indicator,
505                                     r->width + (br.width + br.x), br.y, -(br.width + br.x), r->height + br.height);
506             } else if (p->parent_layout == L_SPLITV) {
507                 draw_util_rectangle(conn, &(con->frame_buffer), p->color->indicator,
508                                     br.x, r->height + (br.height + br.y), r->width + br.width, -(br.height + br.y));
509             }
510         }
511     }
512
513     /* if this is a borderless/1pixel window, we don’t need to render the
514      * decoration. */
515     if (p->border_style != BS_NORMAL)
516         goto copy_pixmaps;
517
518     /* If the parent hasn't been set up yet, skip the decoration rendering
519      * for now. */
520     if (parent->frame_buffer.id == XCB_NONE)
521         goto copy_pixmaps;
522
523     /* For the first child, we clear the parent pixmap to ensure there's no
524      * garbage left on there. This is important to avoid tearing when using
525      * transparency. */
526     if (con == TAILQ_FIRST(&(con->parent->nodes_head))) {
527         draw_util_clear_surface(conn, &(con->parent->frame_buffer), COLOR_TRANSPARENT);
528         FREE(con->parent->deco_render_params);
529     }
530
531     /* 4: paint the bar */
532     draw_util_rectangle(conn, &(parent->frame_buffer), p->color->background,
533                         con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height);
534
535     /* 5: draw two unconnected horizontal lines in border color */
536     x_draw_title_border(con, p);
537
538     /* 6: draw the title */
539     int text_offset_y = (con->deco_rect.height - config.font.height) / 2;
540
541     struct Window *win = con->window;
542     if (win == NULL) {
543         i3String *title;
544         if (con->title_format == NULL) {
545             char *_title;
546             char *tree = con_get_tree_representation(con);
547             sasprintf(&_title, "i3: %s", tree);
548             free(tree);
549
550             title = i3string_from_utf8(_title);
551             FREE(_title);
552         } else {
553             title = con_parse_title_format(con);
554         }
555
556         draw_util_text(title, &(parent->frame_buffer),
557                        p->color->text, p->color->background,
558                        con->deco_rect.x + 2, con->deco_rect.y + text_offset_y,
559                        con->deco_rect.width - 2);
560         I3STRING_FREE(title);
561
562         goto after_title;
563     }
564
565     if (win->name == NULL)
566         goto copy_pixmaps;
567
568     int indent_level = 0,
569         indent_mult = 0;
570     Con *il_parent = parent;
571     if (il_parent->layout != L_STACKED) {
572         while (1) {
573             //DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
574             if (il_parent->layout == L_STACKED)
575                 indent_level++;
576             if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
577                 break;
578             il_parent = il_parent->parent;
579             indent_mult++;
580         }
581     }
582     //DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
583     int indent_px = (indent_level * 5) * indent_mult;
584
585     int mark_width = 0;
586     if (config.show_marks && !TAILQ_EMPTY(&(con->marks_head))) {
587         char *formatted_mark = sstrdup("");
588         bool had_visible_mark = false;
589
590         mark_t *mark;
591         TAILQ_FOREACH(mark, &(con->marks_head), marks) {
592             if (mark->name[0] == '_')
593                 continue;
594             had_visible_mark = true;
595
596             char *buf;
597             sasprintf(&buf, "%s[%s]", formatted_mark, mark->name);
598             free(formatted_mark);
599             formatted_mark = buf;
600         }
601
602         if (had_visible_mark) {
603             i3String *mark = i3string_from_utf8(formatted_mark);
604             mark_width = predict_text_width(mark);
605
606             draw_util_text(mark, &(parent->frame_buffer),
607                            p->color->text, p->color->background,
608                            con->deco_rect.x + con->deco_rect.width - mark_width - logical_px(2),
609                            con->deco_rect.y + text_offset_y, mark_width);
610
611             I3STRING_FREE(mark);
612         }
613
614         FREE(formatted_mark);
615     }
616
617     i3String *title = con->title_format == NULL ? win->name : con_parse_title_format(con);
618     draw_util_text(title, &(parent->frame_buffer),
619                    p->color->text, p->color->background,
620                    con->deco_rect.x + logical_px(2) + indent_px, con->deco_rect.y + text_offset_y,
621                    con->deco_rect.width - logical_px(2) - indent_px - mark_width - logical_px(2));
622     if (con->title_format != NULL)
623         I3STRING_FREE(title);
624
625 after_title:
626     x_draw_decoration_after_title(con, p);
627 copy_pixmaps:
628     draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
629 }
630
631 /*
632  * Recursively calls x_draw_decoration. This cannot be done in x_push_node
633  * because x_push_node uses focus order to recurse (see the comment above)
634  * while drawing the decoration needs to happen in the actual order.
635  *
636  */
637 void x_deco_recurse(Con *con) {
638     Con *current;
639     bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
640                 TAILQ_EMPTY(&(con->floating_head));
641     con_state *state = state_for_frame(con->frame.id);
642
643     if (!leaf) {
644         TAILQ_FOREACH(current, &(con->nodes_head), nodes)
645         x_deco_recurse(current);
646
647         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
648         x_deco_recurse(current);
649
650         if (state->mapped) {
651             draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
652         }
653     }
654
655     if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
656         (!leaf || con->mapped))
657         x_draw_decoration(con);
658 }
659
660 /*
661  * Sets or removes the _NET_WM_STATE_HIDDEN property on con if necessary.
662  *
663  */
664 static void set_hidden_state(Con *con) {
665     if (con->window == NULL) {
666         return;
667     }
668
669     con_state *state = state_for_frame(con->frame.id);
670     bool should_be_hidden = con_is_hidden(con);
671     if (should_be_hidden == state->is_hidden)
672         return;
673
674     if (should_be_hidden) {
675         DLOG("setting _NET_WM_STATE_HIDDEN for con = %p\n", con);
676         xcb_add_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
677     } else {
678         DLOG("removing _NET_WM_STATE_HIDDEN for con = %p\n", con);
679         xcb_remove_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_HIDDEN);
680     }
681
682     state->is_hidden = should_be_hidden;
683 }
684
685 /*
686  * This function pushes the properties of each node of the layout tree to
687  * X11 if they have changed (like the map state, position of the window, …).
688  * It recursively traverses all children of the given node.
689  *
690  */
691 void x_push_node(Con *con) {
692     Con *current;
693     con_state *state;
694     Rect rect = con->rect;
695
696     //DLOG("Pushing changes for node %p / %s\n", con, con->name);
697     state = state_for_frame(con->frame.id);
698
699     if (state->name != NULL) {
700         DLOG("pushing name %s for con %p\n", state->name, con);
701
702         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame.id,
703                             XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(state->name), state->name);
704         FREE(state->name);
705     }
706
707     if (con->window == NULL) {
708         /* Calculate the height of all window decorations which will be drawn on to
709          * this frame. */
710         uint32_t max_y = 0, max_height = 0;
711         TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
712             Rect *dr = &(current->deco_rect);
713             if (dr->y >= max_y && dr->height >= max_height) {
714                 max_y = dr->y;
715                 max_height = dr->height;
716             }
717         }
718         rect.height = max_y + max_height;
719         if (rect.height == 0)
720             con->mapped = false;
721     }
722
723     /* reparent the child window (when the window was moved due to a sticky
724      * container) */
725     if (state->need_reparent && con->window != NULL) {
726         DLOG("Reparenting child window\n");
727
728         /* Temporarily set the event masks to XCB_NONE so that we won’t get
729          * UnmapNotify events (otherwise the handler would close the container).
730          * These events are generated automatically when reparenting. */
731         uint32_t values[] = {XCB_NONE};
732         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
733         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
734
735         xcb_reparent_window(conn, con->window->id, con->frame.id, 0, 0);
736
737         values[0] = FRAME_EVENT_MASK;
738         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
739         values[0] = CHILD_EVENT_MASK;
740         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
741
742         state->old_frame = XCB_NONE;
743         state->need_reparent = false;
744
745         con->ignore_unmap++;
746         DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
747              con, con->window->id, con->ignore_unmap);
748     }
749
750     /* The pixmap of a borderless leaf container will not be used except
751      * for the titlebar in a stack or tabs (issue #1013). */
752     bool is_pixmap_needed = (con->border_style != BS_NONE ||
753                              !con_is_leaf(con) ||
754                              con->parent->layout == L_STACKED ||
755                              con->parent->layout == L_TABBED);
756
757     /* The root con and output cons will never require a pixmap. In particular for the
758      * __i3 output, this will likely not work anyway because it might be ridiculously
759      * large, causing an XCB_ALLOC error. */
760     if (con->type == CT_ROOT || con->type == CT_OUTPUT)
761         is_pixmap_needed = false;
762
763     bool fake_notify = false;
764     /* Set new position if rect changed (and if height > 0) or if the pixmap
765      * needs to be recreated */
766     if ((is_pixmap_needed && con->frame_buffer.id == XCB_NONE) || (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
767                                                                    rect.height > 0)) {
768         /* We first create the new pixmap, then render to it, set it as the
769          * background and only afterwards change the window size. This reduces
770          * flickering. */
771
772         /* As the pixmap only depends on the size and not on the position, it
773          * is enough to check if width/height have changed. Also, we don’t
774          * create a pixmap at all when the window is actually not visible
775          * (height == 0) or when it is not needed. */
776         bool has_rect_changed = (state->rect.width != rect.width || state->rect.height != rect.height);
777
778         /* Check if the container has an unneeded pixmap left over from
779          * previously having a border or titlebar. */
780         if (!is_pixmap_needed && con->frame_buffer.id != XCB_NONE) {
781             draw_util_surface_free(conn, &(con->frame_buffer));
782             xcb_free_pixmap(conn, con->frame_buffer.id);
783             con->frame_buffer.id = XCB_NONE;
784         }
785
786         if (is_pixmap_needed && (has_rect_changed || con->frame_buffer.id == XCB_NONE)) {
787             if (con->frame_buffer.id == XCB_NONE) {
788                 con->frame_buffer.id = xcb_generate_id(conn);
789             } else {
790                 draw_util_surface_free(conn, &(con->frame_buffer));
791                 xcb_free_pixmap(conn, con->frame_buffer.id);
792             }
793
794             uint16_t win_depth = root_depth;
795             if (con->window)
796                 win_depth = con->window->depth;
797
798             /* Ensure we have valid dimensions for our surface. */
799             // TODO This is probably a bug in the condition above as we should never enter this path
800             //      for height == 0. Also, we should probably handle width == 0 the same way.
801             int width = MAX((int32_t)rect.width, 1);
802             int height = MAX((int32_t)rect.height, 1);
803
804             xcb_create_pixmap_checked(conn, win_depth, con->frame_buffer.id, con->frame.id, width, height);
805             draw_util_surface_init(conn, &(con->frame_buffer), con->frame_buffer.id,
806                                    get_visualtype_by_id(get_visualid_by_depth(win_depth)), width, height);
807
808             /* For the graphics context, we disable GraphicsExposure events.
809              * Those will be sent when a CopyArea request cannot be fulfilled
810              * properly due to parts of the source being unmapped or otherwise
811              * unavailable. Since we always copy from pixmaps to windows, this
812              * is not a concern for us. */
813             xcb_change_gc(conn, con->frame_buffer.gc, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
814
815             draw_util_surface_set_size(&(con->frame), width, height);
816             con->pixmap_recreated = true;
817
818             /* Don’t render the decoration for windows inside a stack which are
819              * not visible right now */
820             // TODO Should this work the same way for L_TABBED?
821             if (!con->parent ||
822                 con->parent->layout != L_STACKED ||
823                 TAILQ_FIRST(&(con->parent->focus_head)) == con)
824                 /* Render the decoration now to make the correct decoration visible
825                  * from the very first moment. Later calls will be cached, so this
826                  * doesn’t hurt performance. */
827                 x_deco_recurse(con);
828         }
829
830         DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
831         /* flush to ensure that the following commands are sent in a single
832          * buffer and will be processed directly afterwards (the contents of a
833          * window get lost when resizing it, therefore we want to provide it as
834          * fast as possible) */
835         xcb_flush(conn);
836         xcb_set_window_rect(conn, con->frame.id, rect);
837         if (con->frame_buffer.id != XCB_NONE) {
838             draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
839         }
840         xcb_flush(conn);
841
842         memcpy(&(state->rect), &rect, sizeof(Rect));
843         fake_notify = true;
844     }
845
846     /* dito, but for child windows */
847     if (con->window != NULL &&
848         memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
849         DLOG("setting window rect (%d, %d, %d, %d)\n",
850              con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
851         xcb_set_window_rect(conn, con->window->id, con->window_rect);
852         memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
853         fake_notify = true;
854     }
855
856     /* Map if map state changed, also ensure that the child window
857      * is changed if we are mapped and there is a new, unmapped child window.
858      * Unmaps are handled in x_push_node_unmaps(). */
859     if ((state->mapped != con->mapped || (con->window != NULL && !state->child_mapped)) &&
860         con->mapped) {
861         xcb_void_cookie_t cookie;
862
863         if (con->window != NULL) {
864             /* Set WM_STATE_NORMAL because GTK applications don’t want to
865              * drag & drop if we don’t. Also, xprop(1) needs it. */
866             long data[] = {XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE};
867             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
868                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
869         }
870
871         uint32_t values[1];
872         if (!state->child_mapped && con->window != NULL) {
873             cookie = xcb_map_window(conn, con->window->id);
874
875             /* We are interested in EnterNotifys as soon as the window is
876              * mapped */
877             values[0] = CHILD_EVENT_MASK;
878             xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
879             DLOG("mapping child window (serial %d)\n", cookie.sequence);
880             state->child_mapped = true;
881         }
882
883         cookie = xcb_map_window(conn, con->frame.id);
884
885         values[0] = FRAME_EVENT_MASK;
886         xcb_change_window_attributes(conn, con->frame.id, XCB_CW_EVENT_MASK, values);
887
888         /* copy the pixmap contents to the frame window immediately after mapping */
889         if (con->frame_buffer.id != XCB_NONE) {
890             draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
891         }
892         xcb_flush(conn);
893
894         DLOG("mapping container %08x (serial %d)\n", con->frame.id, cookie.sequence);
895         state->mapped = con->mapped;
896     }
897
898     state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
899
900     if (fake_notify) {
901         DLOG("Sending fake configure notify\n");
902         fake_absolute_configure_notify(con);
903     }
904
905     set_hidden_state(con);
906
907     /* Handle all children and floating windows of this node. We recurse
908      * in focus order to display the focused client in a stack first when
909      * switching workspaces (reduces flickering). */
910     TAILQ_FOREACH(current, &(con->focus_head), focused)
911     x_push_node(current);
912 }
913
914 /*
915  * Same idea as in x_push_node(), but this function only unmaps windows. It is
916  * necessary to split this up to handle new fullscreen clients properly: The
917  * new window needs to be mapped and focus needs to be set *before* the
918  * underlying windows are unmapped. Otherwise, focus will revert to the
919  * PointerRoot and will then be set to the new window, generating unnecessary
920  * FocusIn/FocusOut events.
921  *
922  */
923 static void x_push_node_unmaps(Con *con) {
924     Con *current;
925     con_state *state;
926
927     //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
928     state = state_for_frame(con->frame.id);
929
930     /* map/unmap if map state changed, also ensure that the child window
931      * is changed if we are mapped *and* in initial state (meaning the
932      * container was empty before, but now got a child) */
933     if (state->unmap_now) {
934         xcb_void_cookie_t cookie;
935         if (con->window != NULL) {
936             /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
937             long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
938             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
939                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
940         }
941
942         cookie = xcb_unmap_window(conn, con->frame.id);
943         DLOG("unmapping container %p / %s (serial %d)\n", con, con->name, cookie.sequence);
944         /* we need to increase ignore_unmap for this container (if it
945          * contains a window) and for every window "under" this one which
946          * contains a window */
947         if (con->window != NULL) {
948             con->ignore_unmap++;
949             DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame.id, con->ignore_unmap);
950         }
951         state->mapped = con->mapped;
952     }
953
954     /* handle all children and floating windows of this node */
955     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
956     x_push_node_unmaps(current);
957
958     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
959     x_push_node_unmaps(current);
960 }
961
962 /*
963  * Returns true if the given container is currently attached to its parent.
964  *
965  * TODO: Remove once #1185 has been fixed
966  */
967 static bool is_con_attached(Con *con) {
968     if (con->parent == NULL)
969         return false;
970
971     Con *current;
972     TAILQ_FOREACH(current, &(con->parent->nodes_head), nodes) {
973         if (current == con)
974             return true;
975     }
976
977     return false;
978 }
979
980 /*
981  * Pushes all changes (state of each node, see x_push_node() and the window
982  * stack) to X11.
983  *
984  * NOTE: We need to push the stack first so that the windows have the correct
985  * stacking order. This is relevant for workspace switching where we map the
986  * windows because mapping may generate EnterNotify events. When they are
987  * generated in the wrong order, this will cause focus problems when switching
988  * workspaces.
989  *
990  */
991 void x_push_changes(Con *con) {
992     con_state *state;
993     xcb_query_pointer_cookie_t pointercookie;
994
995     /* If we need to warp later, we request the pointer position as soon as possible */
996     if (warp_to) {
997         pointercookie = xcb_query_pointer(conn, root);
998     }
999
1000     DLOG("-- PUSHING WINDOW STACK --\n");
1001     //DLOG("Disabling EnterNotify\n");
1002     /* We need to keep SubstructureRedirect around, otherwise clients can send
1003      * ConfigureWindow requests and get them applied directly instead of having
1004      * them become ConfigureRequests that i3 handles. */
1005     uint32_t values[1] = {XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT};
1006     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1007         if (state->mapped)
1008             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1009     }
1010     //DLOG("Done, EnterNotify disabled\n");
1011     bool order_changed = false;
1012     bool stacking_changed = false;
1013
1014     /* count first, necessary to (re)allocate memory for the bottom-to-top
1015      * stack afterwards */
1016     int cnt = 0;
1017     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
1018     if (con_has_managed_window(state->con))
1019         cnt++;
1020
1021     /* The bottom-to-top window stack of all windows which are managed by i3.
1022      * Used for x_get_window_stack(). */
1023     static xcb_window_t *client_list_windows = NULL;
1024     static int client_list_count = 0;
1025
1026     if (cnt != client_list_count) {
1027         client_list_windows = srealloc(client_list_windows, sizeof(xcb_window_t) * cnt);
1028         client_list_count = cnt;
1029     }
1030
1031     xcb_window_t *walk = client_list_windows;
1032
1033     /* X11 correctly represents the stack if we push it from bottom to top */
1034     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1035         if (con_has_managed_window(state->con))
1036             memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
1037
1038         //DLOG("stack: 0x%08x\n", state->id);
1039         con_state *prev = CIRCLEQ_PREV(state, state);
1040         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
1041         if (prev != old_prev)
1042             order_changed = true;
1043         if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
1044             stacking_changed = true;
1045             //DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
1046             uint32_t mask = 0;
1047             mask |= XCB_CONFIG_WINDOW_SIBLING;
1048             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
1049             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
1050
1051             xcb_configure_window(conn, prev->id, mask, values);
1052         }
1053         state->initial = false;
1054     }
1055
1056     /* If we re-stacked something (or a new window appeared), we need to update
1057      * the _NET_CLIENT_LIST and _NET_CLIENT_LIST_STACKING hints */
1058     if (stacking_changed) {
1059         DLOG("Client list changed (%i clients)\n", cnt);
1060         ewmh_update_client_list_stacking(client_list_windows, client_list_count);
1061
1062         walk = client_list_windows;
1063
1064         /* reorder by initial mapping */
1065         TAILQ_FOREACH(state, &initial_mapping_head, initial_mapping_order) {
1066             if (con_has_managed_window(state->con))
1067                 *walk++ = state->con->window->id;
1068         }
1069
1070         ewmh_update_client_list(client_list_windows, client_list_count);
1071     }
1072
1073     DLOG("PUSHING CHANGES\n");
1074     x_push_node(con);
1075
1076     if (warp_to) {
1077         xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
1078         if (!pointerreply) {
1079             ELOG("Could not query pointer position, not warping pointer\n");
1080         } else {
1081             int mid_x = warp_to->x + (warp_to->width / 2);
1082             int mid_y = warp_to->y + (warp_to->height / 2);
1083
1084             Output *current = get_output_containing(pointerreply->root_x, pointerreply->root_y);
1085             Output *target = get_output_containing(mid_x, mid_y);
1086             if (current != target) {
1087                 /* Ignore MotionNotify events generated by warping */
1088                 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT});
1089                 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
1090                 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){ROOT_EVENT_MASK});
1091             }
1092
1093             free(pointerreply);
1094         }
1095         warp_to = NULL;
1096     }
1097
1098     //DLOG("Re-enabling EnterNotify\n");
1099     values[0] = FRAME_EVENT_MASK;
1100     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1101         if (state->mapped)
1102             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1103     }
1104     //DLOG("Done, EnterNotify re-enabled\n");
1105
1106     x_deco_recurse(con);
1107
1108     xcb_window_t to_focus = focused->frame.id;
1109     if (focused->window != NULL)
1110         to_focus = focused->window->id;
1111
1112     if (focused_id != to_focus) {
1113         if (!focused->mapped) {
1114             DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
1115             /* Invalidate focused_id to correctly focus new windows with the same ID */
1116             focused_id = XCB_NONE;
1117         } else {
1118             if (focused->window != NULL &&
1119                 focused->window->needs_take_focus &&
1120                 focused->window->doesnt_accept_focus) {
1121                 DLOG("Updating focus by sending WM_TAKE_FOCUS to window 0x%08x (focused: %p / %s)\n",
1122                      to_focus, focused, focused->name);
1123                 send_take_focus(to_focus, last_timestamp);
1124
1125                 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1126
1127                 if (to_focus != last_focused && is_con_attached(focused))
1128                     ipc_send_window_event("focus", focused);
1129             } else {
1130                 DLOG("Updating focus (focused: %p / %s) to X11 window 0x%08x\n", focused, focused->name, to_focus);
1131                 /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
1132                  * no focus change events for our own focus changes. We only want
1133                  * these generated by the clients. */
1134                 if (focused->window != NULL) {
1135                     values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
1136                     xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1137                 }
1138                 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
1139                 if (focused->window != NULL) {
1140                     values[0] = CHILD_EVENT_MASK;
1141                     xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1142                 }
1143
1144                 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1145
1146                 if (to_focus != XCB_NONE && to_focus != last_focused && focused->window != NULL && is_con_attached(focused))
1147                     ipc_send_window_event("focus", focused);
1148             }
1149
1150             focused_id = last_focused = to_focus;
1151         }
1152     }
1153
1154     if (focused_id == XCB_NONE) {
1155         /* If we still have no window to focus, we focus the EWMH window instead. We use this rather than the
1156          * root window in order to avoid an X11 fallback mechanism causing a ghosting effect (see #1378). */
1157         DLOG("Still no window focused, better set focus to the EWMH support window (%d)\n", ewmh_window);
1158         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, ewmh_window, XCB_CURRENT_TIME);
1159         ewmh_update_active_window(XCB_WINDOW_NONE);
1160         focused_id = ewmh_window;
1161     }
1162
1163     xcb_flush(conn);
1164     DLOG("ENDING CHANGES\n");
1165
1166     /* Disable EnterWindow events for windows which will be unmapped in
1167      * x_push_node_unmaps() now. Unmapping windows happens when switching
1168      * workspaces. We want to avoid getting EnterNotifies during that phase
1169      * because they would screw up our focus. One of these cases is having a
1170      * stack with two windows. If the first window is focused and gets
1171      * unmapped, the second one appears under the cursor and therefore gets an
1172      * EnterNotify event. */
1173     values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
1174     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1175         if (!state->unmap_now)
1176             continue;
1177         xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1178     }
1179
1180     /* Push all pending unmaps */
1181     x_push_node_unmaps(con);
1182
1183     /* save the current stack as old stack */
1184     CIRCLEQ_FOREACH(state, &state_head, state) {
1185         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
1186         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
1187     }
1188     //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
1189     //    DLOG("old stack: 0x%08x\n", state->id);
1190     //}
1191
1192     xcb_flush(conn);
1193 }
1194
1195 /*
1196  * Raises the specified container in the internal stack of X windows. The
1197  * next call to x_push_changes() will make the change visible in X11.
1198  *
1199  */
1200 void x_raise_con(Con *con) {
1201     con_state *state;
1202     state = state_for_frame(con->frame.id);
1203     //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
1204
1205     CIRCLEQ_REMOVE(&state_head, state, state);
1206     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
1207 }
1208
1209 /*
1210  * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
1211  * of the given name. Used for properly tagging the windows for easily spotting
1212  * i3 windows in xwininfo -root -all.
1213  *
1214  */
1215 void x_set_name(Con *con, const char *name) {
1216     struct con_state *state;
1217
1218     if ((state = state_for_frame(con->frame.id)) == NULL) {
1219         ELOG("window state not found\n");
1220         return;
1221     }
1222
1223     FREE(state->name);
1224     state->name = sstrdup(name);
1225 }
1226
1227 /*
1228  * Set up the I3_SHMLOG_PATH atom.
1229  *
1230  */
1231 void update_shmlog_atom() {
1232     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1233                         A_I3_SHMLOG_PATH, A_UTF8_STRING, 8,
1234                         strlen(shmlogname), shmlogname);
1235 }
1236
1237 /*
1238  * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
1239  *
1240  */
1241 void x_set_i3_atoms(void) {
1242     pid_t pid = getpid();
1243     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
1244                         (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
1245                         current_socketpath);
1246     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_PID, XCB_ATOM_CARDINAL, 32, 1, &pid);
1247     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
1248                         strlen(current_configpath), current_configpath);
1249     update_shmlog_atom();
1250 }
1251
1252 /*
1253  * Set warp_to coordinates.  This will trigger on the next call to
1254  * x_push_changes().
1255  *
1256  */
1257 void x_set_warp_to(Rect *rect) {
1258     if (config.mouse_warping != POINTER_WARPING_NONE)
1259         warp_to = rect;
1260 }
1261
1262 /*
1263  * Applies the given mask to the event mask of every i3 window decoration X11
1264  * window. This is useful to disable EnterNotify while resizing so that focus
1265  * is untouched.
1266  *
1267  */
1268 void x_mask_event_mask(uint32_t mask) {
1269     uint32_t values[] = {FRAME_EVENT_MASK & mask};
1270
1271     con_state *state;
1272     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1273         if (state->mapped)
1274             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1275     }
1276 }