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