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