]> git.sur5r.net Git - i3/i3/blob - src/x.c
Don't create a pixmap for CT_ROOT and CT_OUTPUT containers.
[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     /* The root con and output cons will never require a pixmap. In particular for the
750      * __i3 output, this will likely not work anyway because it might be ridiculously
751      * large, causing an XCB_ALLOC error. */
752     if (con->type == CT_ROOT || con->type == CT_OUTPUT)
753         is_pixmap_needed = false;
754
755     bool fake_notify = false;
756     /* Set new position if rect changed (and if height > 0) or if the pixmap
757      * needs to be recreated */
758     if ((is_pixmap_needed && con->frame_buffer.id == XCB_NONE) || (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
759                                                                    rect.height > 0)) {
760         /* We first create the new pixmap, then render to it, set it as the
761          * background and only afterwards change the window size. This reduces
762          * flickering. */
763
764         /* As the pixmap only depends on the size and not on the position, it
765          * is enough to check if width/height have changed. Also, we don’t
766          * create a pixmap at all when the window is actually not visible
767          * (height == 0) or when it is not needed. */
768         bool has_rect_changed = (state->rect.width != rect.width || state->rect.height != rect.height);
769
770         /* Check if the container has an unneeded pixmap left over from
771          * previously having a border or titlebar. */
772         if (!is_pixmap_needed && con->frame_buffer.id != XCB_NONE) {
773             draw_util_surface_free(conn, &(con->frame_buffer));
774             xcb_free_pixmap(conn, con->frame_buffer.id);
775             con->frame_buffer.id = XCB_NONE;
776         }
777
778         if (is_pixmap_needed && (has_rect_changed || con->frame_buffer.id == XCB_NONE)) {
779             if (con->frame_buffer.id == XCB_NONE) {
780                 con->frame_buffer.id = xcb_generate_id(conn);
781             } else {
782                 draw_util_surface_free(conn, &(con->frame_buffer));
783                 xcb_free_pixmap(conn, con->frame_buffer.id);
784             }
785
786             uint16_t win_depth = root_depth;
787             if (con->window)
788                 win_depth = con->window->depth;
789
790             /* Ensure we have valid dimensions for our surface. */
791             // TODO This is probably a bug in the condition above as we should never enter this path
792             //      for height == 0. Also, we should probably handle width == 0 the same way.
793             int width = MAX(rect.width, 1);
794             int height = MAX(rect.height, 1);
795
796             xcb_create_pixmap_checked(conn, win_depth, con->frame_buffer.id, con->frame.id, width, height);
797             draw_util_surface_init(conn, &(con->frame_buffer), con->frame_buffer.id,
798                                    get_visualtype_by_id(get_visualid_by_depth(win_depth)), width, height);
799
800             /* For the graphics context, we disable GraphicsExposure events.
801              * Those will be sent when a CopyArea request cannot be fulfilled
802              * properly due to parts of the source being unmapped or otherwise
803              * unavailable. Since we always copy from pixmaps to windows, this
804              * is not a concern for us. */
805             xcb_change_gc(conn, con->frame_buffer.gc, XCB_GC_GRAPHICS_EXPOSURES, (uint32_t[]){0});
806
807             draw_util_surface_set_size(&(con->frame), width, height);
808             con->pixmap_recreated = true;
809
810             /* Don’t render the decoration for windows inside a stack which are
811              * not visible right now */
812             // TODO Should this work the same way for L_TABBED?
813             if (!con->parent ||
814                 con->parent->layout != L_STACKED ||
815                 TAILQ_FIRST(&(con->parent->focus_head)) == con)
816                 /* Render the decoration now to make the correct decoration visible
817                  * from the very first moment. Later calls will be cached, so this
818                  * doesn’t hurt performance. */
819                 x_deco_recurse(con);
820         }
821
822         DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
823         /* flush to ensure that the following commands are sent in a single
824          * buffer and will be processed directly afterwards (the contents of a
825          * window get lost when resizing it, therefore we want to provide it as
826          * fast as possible) */
827         xcb_flush(conn);
828         xcb_set_window_rect(conn, con->frame.id, rect);
829         if (con->frame_buffer.id != XCB_NONE) {
830             draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
831         }
832         xcb_flush(conn);
833
834         memcpy(&(state->rect), &rect, sizeof(Rect));
835         fake_notify = true;
836     }
837
838     /* dito, but for child windows */
839     if (con->window != NULL &&
840         memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
841         DLOG("setting window rect (%d, %d, %d, %d)\n",
842              con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
843         xcb_set_window_rect(conn, con->window->id, con->window_rect);
844         memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
845         fake_notify = true;
846     }
847
848     /* Map if map state changed, also ensure that the child window
849      * is changed if we are mapped and there is a new, unmapped child window.
850      * Unmaps are handled in x_push_node_unmaps(). */
851     if ((state->mapped != con->mapped || (con->window != NULL && !state->child_mapped)) &&
852         con->mapped) {
853         xcb_void_cookie_t cookie;
854
855         if (con->window != NULL) {
856             /* Set WM_STATE_NORMAL because GTK applications don’t want to
857              * drag & drop if we don’t. Also, xprop(1) needs it. */
858             long data[] = {XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE};
859             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
860                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
861         }
862
863         uint32_t values[1];
864         if (!state->child_mapped && con->window != NULL) {
865             cookie = xcb_map_window(conn, con->window->id);
866
867             /* We are interested in EnterNotifys as soon as the window is
868              * mapped */
869             values[0] = CHILD_EVENT_MASK;
870             xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
871             DLOG("mapping child window (serial %d)\n", cookie.sequence);
872             state->child_mapped = true;
873         }
874
875         cookie = xcb_map_window(conn, con->frame.id);
876
877         values[0] = FRAME_EVENT_MASK;
878         xcb_change_window_attributes(conn, con->frame.id, XCB_CW_EVENT_MASK, values);
879
880         /* copy the pixmap contents to the frame window immediately after mapping */
881         if (con->frame_buffer.id != XCB_NONE) {
882             draw_util_copy_surface(conn, &(con->frame_buffer), &(con->frame), 0, 0, 0, 0, con->rect.width, con->rect.height);
883         }
884         xcb_flush(conn);
885
886         DLOG("mapping container %08x (serial %d)\n", con->frame.id, cookie.sequence);
887         state->mapped = con->mapped;
888     }
889
890     state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
891
892     if (fake_notify) {
893         DLOG("Sending fake configure notify\n");
894         fake_absolute_configure_notify(con);
895     }
896
897     set_hidden_state(con);
898
899     /* Handle all children and floating windows of this node. We recurse
900      * in focus order to display the focused client in a stack first when
901      * switching workspaces (reduces flickering). */
902     TAILQ_FOREACH(current, &(con->focus_head), focused)
903     x_push_node(current);
904 }
905
906 /*
907  * Same idea as in x_push_node(), but this function only unmaps windows. It is
908  * necessary to split this up to handle new fullscreen clients properly: The
909  * new window needs to be mapped and focus needs to be set *before* the
910  * underlying windows are unmapped. Otherwise, focus will revert to the
911  * PointerRoot and will then be set to the new window, generating unnecessary
912  * FocusIn/FocusOut events.
913  *
914  */
915 static void x_push_node_unmaps(Con *con) {
916     Con *current;
917     con_state *state;
918
919     //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
920     state = state_for_frame(con->frame.id);
921
922     /* map/unmap if map state changed, also ensure that the child window
923      * is changed if we are mapped *and* in initial state (meaning the
924      * container was empty before, but now got a child) */
925     if (state->unmap_now) {
926         xcb_void_cookie_t cookie;
927         if (con->window != NULL) {
928             /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
929             long data[] = {XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE};
930             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
931                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
932         }
933
934         cookie = xcb_unmap_window(conn, con->frame.id);
935         DLOG("unmapping container %p / %s (serial %d)\n", con, con->name, cookie.sequence);
936         /* we need to increase ignore_unmap for this container (if it
937          * contains a window) and for every window "under" this one which
938          * contains a window */
939         if (con->window != NULL) {
940             con->ignore_unmap++;
941             DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame.id, con->ignore_unmap);
942         }
943         state->mapped = con->mapped;
944     }
945
946     /* handle all children and floating windows of this node */
947     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
948     x_push_node_unmaps(current);
949
950     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
951     x_push_node_unmaps(current);
952 }
953
954 /*
955  * Returns true if the given container is currently attached to its parent.
956  *
957  * TODO: Remove once #1185 has been fixed
958  */
959 static bool is_con_attached(Con *con) {
960     if (con->parent == NULL)
961         return false;
962
963     Con *current;
964     TAILQ_FOREACH(current, &(con->parent->nodes_head), nodes) {
965         if (current == con)
966             return true;
967     }
968
969     return false;
970 }
971
972 /*
973  * Pushes all changes (state of each node, see x_push_node() and the window
974  * stack) to X11.
975  *
976  * NOTE: We need to push the stack first so that the windows have the correct
977  * stacking order. This is relevant for workspace switching where we map the
978  * windows because mapping may generate EnterNotify events. When they are
979  * generated in the wrong order, this will cause focus problems when switching
980  * workspaces.
981  *
982  */
983 void x_push_changes(Con *con) {
984     con_state *state;
985     xcb_query_pointer_cookie_t pointercookie;
986
987     /* If we need to warp later, we request the pointer position as soon as possible */
988     if (warp_to) {
989         pointercookie = xcb_query_pointer(conn, root);
990     }
991
992     DLOG("-- PUSHING WINDOW STACK --\n");
993     //DLOG("Disabling EnterNotify\n");
994     uint32_t values[1] = {XCB_NONE};
995     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
996         if (state->mapped)
997             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
998     }
999     //DLOG("Done, EnterNotify disabled\n");
1000     bool order_changed = false;
1001     bool stacking_changed = false;
1002
1003     /* count first, necessary to (re)allocate memory for the bottom-to-top
1004      * stack afterwards */
1005     int cnt = 0;
1006     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
1007     if (con_has_managed_window(state->con))
1008         cnt++;
1009
1010     /* The bottom-to-top window stack of all windows which are managed by i3.
1011      * Used for x_get_window_stack(). */
1012     static xcb_window_t *client_list_windows = NULL;
1013     static int client_list_count = 0;
1014
1015     if (cnt != client_list_count) {
1016         client_list_windows = srealloc(client_list_windows, sizeof(xcb_window_t) * cnt);
1017         client_list_count = cnt;
1018     }
1019
1020     xcb_window_t *walk = client_list_windows;
1021
1022     /* X11 correctly represents the stack if we push it from bottom to top */
1023     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1024         if (con_has_managed_window(state->con))
1025             memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
1026
1027         //DLOG("stack: 0x%08x\n", state->id);
1028         con_state *prev = CIRCLEQ_PREV(state, state);
1029         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
1030         if (prev != old_prev)
1031             order_changed = true;
1032         if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
1033             stacking_changed = true;
1034             //DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
1035             uint32_t mask = 0;
1036             mask |= XCB_CONFIG_WINDOW_SIBLING;
1037             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
1038             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
1039
1040             xcb_configure_window(conn, prev->id, mask, values);
1041         }
1042         state->initial = false;
1043     }
1044
1045     /* If we re-stacked something (or a new window appeared), we need to update
1046      * the _NET_CLIENT_LIST and _NET_CLIENT_LIST_STACKING hints */
1047     if (stacking_changed) {
1048         DLOG("Client list changed (%i clients)\n", cnt);
1049         ewmh_update_client_list_stacking(client_list_windows, client_list_count);
1050
1051         walk = client_list_windows;
1052
1053         /* reorder by initial mapping */
1054         TAILQ_FOREACH(state, &initial_mapping_head, initial_mapping_order) {
1055             if (con_has_managed_window(state->con))
1056                 *walk++ = state->con->window->id;
1057         }
1058
1059         ewmh_update_client_list(client_list_windows, client_list_count);
1060     }
1061
1062     DLOG("PUSHING CHANGES\n");
1063     x_push_node(con);
1064
1065     if (warp_to) {
1066         xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
1067         if (!pointerreply) {
1068             ELOG("Could not query pointer position, not warping pointer\n");
1069         } else {
1070             int mid_x = warp_to->x + (warp_to->width / 2);
1071             int mid_y = warp_to->y + (warp_to->height / 2);
1072
1073             Output *current = get_output_containing(pointerreply->root_x, pointerreply->root_y);
1074             Output *target = get_output_containing(mid_x, mid_y);
1075             if (current != target) {
1076                 /* Ignore MotionNotify events generated by warping */
1077                 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT});
1078                 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
1079                 xcb_change_window_attributes(conn, root, XCB_CW_EVENT_MASK, (uint32_t[]){ROOT_EVENT_MASK});
1080             }
1081         }
1082         warp_to = NULL;
1083     }
1084
1085     //DLOG("Re-enabling EnterNotify\n");
1086     values[0] = FRAME_EVENT_MASK;
1087     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1088         if (state->mapped)
1089             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1090     }
1091     //DLOG("Done, EnterNotify re-enabled\n");
1092
1093     x_deco_recurse(con);
1094
1095     xcb_window_t to_focus = focused->frame.id;
1096     if (focused->window != NULL)
1097         to_focus = focused->window->id;
1098
1099     if (focused_id != to_focus) {
1100         if (!focused->mapped) {
1101             DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
1102             /* Invalidate focused_id to correctly focus new windows with the same ID */
1103             focused_id = XCB_NONE;
1104         } else {
1105             if (focused->window != NULL &&
1106                 focused->window->needs_take_focus &&
1107                 focused->window->doesnt_accept_focus) {
1108                 DLOG("Updating focus by sending WM_TAKE_FOCUS to window 0x%08x (focused: %p / %s)\n",
1109                      to_focus, focused, focused->name);
1110                 send_take_focus(to_focus, last_timestamp);
1111
1112                 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1113
1114                 if (to_focus != last_focused && is_con_attached(focused))
1115                     ipc_send_window_event("focus", focused);
1116             } else {
1117                 DLOG("Updating focus (focused: %p / %s) to X11 window 0x%08x\n", focused, focused->name, to_focus);
1118                 /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
1119                  * no focus change events for our own focus changes. We only want
1120                  * these generated by the clients. */
1121                 if (focused->window != NULL) {
1122                     values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
1123                     xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1124                 }
1125                 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
1126                 if (focused->window != NULL) {
1127                     values[0] = CHILD_EVENT_MASK;
1128                     xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
1129                 }
1130
1131                 ewmh_update_active_window((con_has_managed_window(focused) ? focused->window->id : XCB_WINDOW_NONE));
1132
1133                 if (to_focus != XCB_NONE && to_focus != last_focused && focused->window != NULL && is_con_attached(focused))
1134                     ipc_send_window_event("focus", focused);
1135             }
1136
1137             focused_id = last_focused = to_focus;
1138         }
1139     }
1140
1141     if (focused_id == XCB_NONE) {
1142         /* If we still have no window to focus, we focus the EWMH window instead. We use this rather than the
1143          * root window in order to avoid an X11 fallback mechanism causing a ghosting effect (see #1378). */
1144         DLOG("Still no window focused, better set focus to the EWMH support window (%d)\n", ewmh_window);
1145         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, ewmh_window, XCB_CURRENT_TIME);
1146         ewmh_update_active_window(XCB_WINDOW_NONE);
1147         focused_id = ewmh_window;
1148     }
1149
1150     xcb_flush(conn);
1151     DLOG("ENDING CHANGES\n");
1152
1153     /* Disable EnterWindow events for windows which will be unmapped in
1154      * x_push_node_unmaps() now. Unmapping windows happens when switching
1155      * workspaces. We want to avoid getting EnterNotifies during that phase
1156      * because they would screw up our focus. One of these cases is having a
1157      * stack with two windows. If the first window is focused and gets
1158      * unmapped, the second one appears under the cursor and therefore gets an
1159      * EnterNotify event. */
1160     values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
1161     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1162         if (!state->unmap_now)
1163             continue;
1164         xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1165     }
1166
1167     /* Push all pending unmaps */
1168     x_push_node_unmaps(con);
1169
1170     /* save the current stack as old stack */
1171     CIRCLEQ_FOREACH(state, &state_head, state) {
1172         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
1173         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
1174     }
1175     //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
1176     //    DLOG("old stack: 0x%08x\n", state->id);
1177     //}
1178
1179     xcb_flush(conn);
1180 }
1181
1182 /*
1183  * Raises the specified container in the internal stack of X windows. The
1184  * next call to x_push_changes() will make the change visible in X11.
1185  *
1186  */
1187 void x_raise_con(Con *con) {
1188     con_state *state;
1189     state = state_for_frame(con->frame.id);
1190     //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
1191
1192     CIRCLEQ_REMOVE(&state_head, state, state);
1193     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
1194 }
1195
1196 /*
1197  * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
1198  * of the given name. Used for properly tagging the windows for easily spotting
1199  * i3 windows in xwininfo -root -all.
1200  *
1201  */
1202 void x_set_name(Con *con, const char *name) {
1203     struct con_state *state;
1204
1205     if ((state = state_for_frame(con->frame.id)) == NULL) {
1206         ELOG("window state not found\n");
1207         return;
1208     }
1209
1210     FREE(state->name);
1211     state->name = sstrdup(name);
1212 }
1213
1214 /*
1215  * Set up the I3_SHMLOG_PATH atom.
1216  *
1217  */
1218 void update_shmlog_atom() {
1219     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root,
1220                         A_I3_SHMLOG_PATH, A_UTF8_STRING, 8,
1221                         strlen(shmlogname), shmlogname);
1222 }
1223
1224 /*
1225  * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
1226  *
1227  */
1228 void x_set_i3_atoms(void) {
1229     pid_t pid = getpid();
1230     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
1231                         (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
1232                         current_socketpath);
1233     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_PID, XCB_ATOM_CARDINAL, 32, 1, &pid);
1234     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
1235                         strlen(current_configpath), current_configpath);
1236     update_shmlog_atom();
1237 }
1238
1239 /*
1240  * Set warp_to coordinates.  This will trigger on the next call to
1241  * x_push_changes().
1242  *
1243  */
1244 void x_set_warp_to(Rect *rect) {
1245     if (config.mouse_warping != POINTER_WARPING_NONE)
1246         warp_to = rect;
1247 }
1248
1249 /*
1250  * Applies the given mask to the event mask of every i3 window decoration X11
1251  * window. This is useful to disable EnterNotify while resizing so that focus
1252  * is untouched.
1253  *
1254  */
1255 void x_mask_event_mask(uint32_t mask) {
1256     uint32_t values[] = {FRAME_EVENT_MASK & mask};
1257
1258     con_state *state;
1259     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
1260         if (state->mapped)
1261             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
1262     }
1263 }