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