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