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