]> git.sur5r.net Git - i3/i3/blob - src/x.c
Bugfix: Correctly set the _NET_CLIENT_LIST_STACKING hint (fixes chromium tabbar)
[i3/i3] / src / x.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  */
4
5 #include "all.h"
6
7 /* Stores the X11 window ID of the currently focused window */
8 xcb_window_t focused_id = XCB_NONE;
9
10 /* The bottom-to-top window stack of all windows which are managed by i3.
11  * Used for x_get_window_stack(). */
12 static xcb_window_t *btt_stack;
13 static int btt_stack_num;
14
15 /*
16  * Describes the X11 state we may modify (map state, position, window stack).
17  * There is one entry per container. The state represents the current situation
18  * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
19  * which represents the order that will be pushed to X11, while old_state_head
20  * represents the current order). It will be updated in x_push_changes().
21  *
22  */
23 typedef struct con_state {
24     xcb_window_t id;
25     bool mapped;
26     bool unmap_now;
27     bool child_mapped;
28
29     /** The con for which this state is. */
30     Con *con;
31
32     /* For reparenting, we have a flag (need_reparent) and the X ID of the old
33      * frame this window was in. The latter is necessary because we need to
34      * ignore UnmapNotify events (by changing the window event mask). */
35     bool need_reparent;
36     xcb_window_t old_frame;
37
38     Rect rect;
39     Rect window_rect;
40
41     bool initial;
42
43     char *name;
44
45     CIRCLEQ_ENTRY(con_state) state;
46     CIRCLEQ_ENTRY(con_state) old_state;
47 } con_state;
48
49 CIRCLEQ_HEAD(state_head, con_state) state_head =
50     CIRCLEQ_HEAD_INITIALIZER(state_head);
51
52 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
53     CIRCLEQ_HEAD_INITIALIZER(old_state_head);
54
55 /*
56  * Returns the container state for the given frame. This function always
57  * returns a container state (otherwise, there is a bug in the code and the
58  * container state of a container for which x_con_init() was not called was
59  * requested).
60  *
61  */
62 static con_state *state_for_frame(xcb_window_t window) {
63     con_state *state;
64     CIRCLEQ_FOREACH(state, &state_head, state)
65         if (state->id == window)
66             return state;
67
68     /* TODO: better error handling? */
69     ELOG("No state found\n");
70     assert(false);
71     return NULL;
72 }
73
74 /*
75  * Initializes the X11 part for the given container. Called exactly once for
76  * every container from con_new().
77  *
78  */
79 void x_con_init(Con *con) {
80     /* TODO: maybe create the window when rendering first? we could then even
81      * get the initial geometry right */
82
83     uint32_t mask = 0;
84     uint32_t values[2];
85
86     /* our own frames should not be managed */
87     mask |= XCB_CW_OVERRIDE_REDIRECT;
88     values[0] = 1;
89
90     /* see include/xcb.h for the FRAME_EVENT_MASK */
91     mask |= XCB_CW_EVENT_MASK;
92     values[1] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
93
94     Rect dims = { -15, -15, 10, 10 };
95     con->frame = create_window(conn, dims, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER, false, mask, values);
96
97     struct con_state *state = scalloc(sizeof(struct con_state));
98     state->id = con->frame;
99     state->mapped = false;
100     state->initial = true;
101     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
102     CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
103     DLOG("adding new state for window id 0x%08x\n", state->id);
104 }
105
106 /*
107  * Re-initializes the associated X window state for this container. You have
108  * to call this when you assign a client to an empty container to ensure that
109  * its state gets updated correctly.
110  *
111  */
112 void x_reinit(Con *con) {
113     struct con_state *state;
114
115     if ((state = state_for_frame(con->frame)) == NULL) {
116         ELOG("window state not found\n");
117         return;
118     }
119
120     DLOG("resetting state %p to initial\n", state);
121     state->initial = true;
122     state->child_mapped = false;
123     state->con = con;
124     memset(&(state->window_rect), 0, sizeof(Rect));
125 }
126
127 /*
128  * Reparents the child window of the given container (necessary for sticky
129  * containers). The reparenting happens in the next call of x_push_changes().
130  *
131  */
132 void x_reparent_child(Con *con, Con *old) {
133     struct con_state *state;
134     if ((state = state_for_frame(con->frame)) == NULL) {
135         ELOG("window state for con not found\n");
136         return;
137     }
138
139     state->need_reparent = true;
140     state->old_frame = old->frame;
141 }
142
143 /*
144  * Moves a child window from Container src to Container dest.
145  *
146  */
147 void x_move_win(Con *src, Con *dest) {
148     struct con_state *state_src, *state_dest;
149
150     if ((state_src = state_for_frame(src->frame)) == NULL) {
151         ELOG("window state for src not found\n");
152         return;
153     }
154
155     if ((state_dest = state_for_frame(dest->frame)) == NULL) {
156         ELOG("window state for dest not found\n");
157         return;
158     }
159
160     state_dest->con = state_src->con;
161     state_src->con = NULL;
162
163     Rect zero = { 0, 0, 0, 0 };
164     if (memcmp(&(state_dest->window_rect), &(zero), sizeof(Rect)) == 0) {
165         memcpy(&(state_dest->window_rect), &(state_src->window_rect), sizeof(Rect));
166         DLOG("COPYING RECT\n");
167     }
168 }
169
170 /*
171  * Kills the window decoration associated with the given container.
172  *
173  */
174 void x_con_kill(Con *con) {
175     con_state *state;
176
177     xcb_destroy_window(conn, con->frame);
178     xcb_free_pixmap(conn, con->pixmap);
179     xcb_free_gc(conn, con->pm_gc);
180     state = state_for_frame(con->frame);
181     CIRCLEQ_REMOVE(&state_head, state, state);
182     CIRCLEQ_REMOVE(&old_state_head, state, old_state);
183     FREE(state->name);
184     free(state);
185
186     /* Invalidate focused_id to correctly focus new windows with the same ID */
187     focused_id = XCB_NONE;
188 }
189
190 /*
191  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
192  *
193  */
194 bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
195     xcb_get_property_cookie_t cookie;
196     xcb_icccm_get_wm_protocols_reply_t protocols;
197     bool result = false;
198
199     cookie = xcb_icccm_get_wm_protocols(conn, window, A_WM_PROTOCOLS);
200     if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
201         return false;
202
203     /* Check if the client’s protocols have the requested atom set */
204     for (uint32_t i = 0; i < protocols.atoms_len; i++)
205         if (protocols.atoms[i] == atom)
206             result = true;
207
208     xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
209
210     return result;
211 }
212
213 /*
214  * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
215  *
216  */
217 void x_window_kill(xcb_window_t window, kill_window_t kill_window) {
218     /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
219     if (!window_supports_protocol(window, A_WM_DELETE_WINDOW)) {
220         if (kill_window == KILL_WINDOW) {
221             LOG("Killing specific window 0x%08x\n", window);
222             xcb_destroy_window(conn, window);
223         } else {
224             LOG("Killing the X11 client which owns window 0x%08x\n", window);
225             xcb_kill_client(conn, window);
226         }
227         return;
228     }
229
230     /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
231      * In order to properly initialize these bytes, we allocate 32 bytes even
232      * though we only need less for an xcb_configure_notify_event_t */
233     void *event = scalloc(32);
234     xcb_client_message_event_t *ev = event;
235
236     ev->response_type = XCB_CLIENT_MESSAGE;
237     ev->window = window;
238     ev->type = A_WM_PROTOCOLS;
239     ev->format = 32;
240     ev->data.data32[0] = A_WM_DELETE_WINDOW;
241     ev->data.data32[1] = XCB_CURRENT_TIME;
242
243     LOG("Sending WM_DELETE to the client\n");
244     xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)ev);
245     xcb_flush(conn);
246     free(event);
247 }
248
249 /*
250  * Draws the decoration of the given container onto its parent.
251  *
252  */
253 void x_draw_decoration(Con *con) {
254     Con *parent = con->parent;
255     /* This code needs to run for:
256      *  â€¢ leaf containers
257      *  â€¢ non-leaf containers which are in a stacked/tabbed container
258      *
259      * It does not need to run for:
260      *  â€¢ floating containers (they don’t have a decoration)
261      */
262     if ((!con_is_leaf(con) &&
263          parent->layout != L_STACKED &&
264          parent->layout != L_TABBED) ||
265         con->type == CT_FLOATING_CON)
266         return;
267     DLOG("decoration should be rendered for con %p\n", con);
268
269     /* Skip containers whose height is 0 (for example empty dockareas) */
270     if (con->rect.height == 0) {
271         DLOG("height == 0, not rendering\n");
272         return;
273     }
274
275     /* Skip containers whose pixmap has not yet been created (can happen when
276      * decoration rendering happens recursively for a window for which
277      * x_push_node() was not yet called) */
278     if (con->pixmap == XCB_NONE) {
279         DLOG("pixmap not yet created, not rendering\n");
280         return;
281     }
282
283     /* 1: build deco_params and compare with cache */
284     struct deco_render_params *p = scalloc(sizeof(struct deco_render_params));
285
286     /* find out which colors to use */
287     if (con->urgent)
288         p->color = &config.client.urgent;
289     else if (con == focused)
290         p->color = &config.client.focused;
291     else if (con == TAILQ_FIRST(&(parent->focus_head)))
292         p->color = &config.client.focused_inactive;
293     else
294         p->color = &config.client.unfocused;
295
296     p->border_style = con_border_style(con);
297
298     Rect *r = &(con->rect);
299     Rect *w = &(con->window_rect);
300     p->con_rect = (struct width_height){ r->width, r->height };
301     p->con_window_rect = (struct width_height){ w->width, w->height };
302     p->con_deco_rect = con->deco_rect;
303     p->background = config.client.background;
304     p->con_is_leaf = con_is_leaf(con);
305     p->font = config.font.id;
306
307     if (con->deco_render_params != NULL &&
308         (con->window == NULL || !con->window->name_x_changed) &&
309         !parent->pixmap_recreated &&
310         !con->pixmap_recreated &&
311         memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
312         DLOG("CACHE HIT, copying existing pixmaps\n");
313         free(p);
314         goto copy_pixmaps;
315     }
316
317     DLOG("CACHE MISS\n");
318     Con *next = con;
319     while ((next = TAILQ_NEXT(next, nodes))) {
320         DLOG("Also invalidating cache of %p\n", next);
321         FREE(next->deco_render_params);
322     }
323
324     FREE(con->deco_render_params);
325     con->deco_render_params = p;
326
327     if (con->window != NULL && con->window->name_x_changed)
328         con->window->name_x_changed = false;
329
330     parent->pixmap_recreated = false;
331     con->pixmap_recreated = false;
332
333     /* 2: draw the client.background, but only for the parts around the client_rect */
334     if (con->window != NULL) {
335         xcb_rectangle_t background[] = {
336             /* top area */
337             { 0, 0, r->width, w->y },
338             /* bottom area */
339             { 0, (w->y + w->height), r->width, r->height - (w->y + w->height) },
340             /* left area */
341             { 0, 0, w->x, r->height },
342             /* right area */
343             { w->x + w->width, 0, r->width - (w->x + w->width), r->height }
344         };
345 #if 0
346         for (int i = 0; i < 4; i++)
347             DLOG("rect is (%d, %d) with %d x %d\n",
348                     background[i].x,
349                     background[i].y,
350                     background[i].width,
351                     background[i].height
352                 );
353 #endif
354
355         xcb_change_gc_single(conn, con->pm_gc, XCB_GC_FOREGROUND, config.client.background);
356         xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, sizeof(background) / sizeof(xcb_rectangle_t), background);
357     }
358
359     /* 3: draw a rectangle in border color around the client */
360     if (p->border_style != BS_NONE && p->con_is_leaf) {
361         Rect br = con_border_style_rect(con);
362 #if 0
363         DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
364         DLOG("border_rect spans (%d, %d) with %d x %d\n", br.x, br.y, br.width, br.height);
365         DLOG("window_rect spans (%d, %d) with %d x %d\n", con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
366 #endif
367
368         /* These rectangles represents the border around the child window
369          * (left, bottom and right part). We don’t just fill the whole
370          * rectangle because some childs are not freely resizable and we want
371          * their background color to "shine through". */
372         xcb_change_gc_single(conn, con->pm_gc, XCB_GC_FOREGROUND, p->color->background);
373         xcb_rectangle_t borders[] = {
374             { 0, 0, br.x, r->height },
375             { 0, r->height + br.height + br.y, r->width, r->height },
376             { r->width + br.width + br.x, 0, r->width, r->height }
377         };
378         xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 3, borders);
379         /* 1pixel border needs an additional line at the top */
380         if (p->border_style == BS_1PIXEL) {
381             xcb_rectangle_t topline = { br.x, 0, con->rect.width + br.width + br.x, br.y };
382             xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, &topline);
383         }
384     }
385
386     /* if this is a borderless/1pixel window, we don’t * need to render the
387      * decoration. */
388     if (p->border_style != BS_NORMAL) {
389         DLOG("border style not BS_NORMAL, aborting rendering of decoration\n");
390         goto copy_pixmaps;
391     }
392
393     /* 4: paint the bar */
394     xcb_change_gc_single(conn, parent->pm_gc, XCB_GC_FOREGROUND, p->color->background);
395     xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
396     xcb_poly_fill_rectangle(conn, parent->pixmap, parent->pm_gc, 1, &drect);
397
398     /* 5: draw two unconnected lines in border color */
399     xcb_change_gc_single(conn, parent->pm_gc, XCB_GC_FOREGROUND, p->color->border);
400     Rect *dr = &(con->deco_rect);
401     xcb_segment_t segments[] = {
402         { dr->x,             dr->y,
403           dr->x + dr->width, dr->y },
404
405         { dr->x,             dr->y + dr->height - 1,
406           dr->x + dr->width, dr->y + dr->height - 1 }
407     };
408     xcb_poly_segment(conn, parent->pixmap, parent->pm_gc, 2, segments);
409
410     /* 6: draw the title */
411     uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
412     uint32_t values[] = { p->color->text, p->color->background, config.font.id };
413     xcb_change_gc(conn, parent->pm_gc, mask, values);
414     int text_offset_y = config.font.height + (con->deco_rect.height - config.font.height) / 2 - 1;
415
416     struct Window *win = con->window;
417     if (win == NULL || win->name_x == NULL) {
418         /* this is a non-leaf container, we need to make up a good description */
419         // TODO: use a good description instead of just "another container"
420         xcb_image_text_8(
421             conn,
422             strlen("another container"),
423             parent->pixmap,
424             parent->pm_gc,
425             con->deco_rect.x + 2,
426             con->deco_rect.y + text_offset_y,
427             "another container"
428         );
429
430         goto copy_pixmaps;
431     }
432
433     int indent_level = 0,
434         indent_mult = 0;
435     Con *il_parent = parent;
436     if (il_parent->layout != L_STACKED) {
437         while (1) {
438             DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
439             if (il_parent->layout == L_STACKED)
440                 indent_level++;
441             if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
442                 break;
443             il_parent = il_parent->parent;
444             indent_mult++;
445         }
446     }
447     DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
448     int indent_px = (indent_level * 5) * indent_mult;
449
450     if (win->uses_net_wm_name)
451         xcb_image_text_16(
452             conn,
453             win->name_len,
454             parent->pixmap,
455             parent->pm_gc,
456             con->deco_rect.x + 2 + indent_px,
457             con->deco_rect.y + text_offset_y,
458             (xcb_char2b_t*)win->name_x
459         );
460     else
461         xcb_image_text_8(
462             conn,
463             win->name_len,
464             parent->pixmap,
465             parent->pm_gc,
466             con->deco_rect.x + 2 + indent_px,
467             con->deco_rect.y + text_offset_y,
468             win->name_x
469         );
470
471 copy_pixmaps:
472     xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
473 }
474
475 /*
476  * Recursively calls x_draw_decoration. This cannot be done in x_push_node
477  * because x_push_node uses focus order to recurse (see the comment above)
478  * while drawing the decoration needs to happen in the actual order.
479  *
480  */
481 void x_deco_recurse(Con *con) {
482     Con *current;
483     bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
484                 TAILQ_EMPTY(&(con->floating_head));
485     con_state *state = state_for_frame(con->frame);
486
487     if (!leaf) {
488         TAILQ_FOREACH(current, &(con->nodes_head), nodes)
489             x_deco_recurse(current);
490
491         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
492             x_deco_recurse(current);
493
494         if (state->mapped)
495             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
496     }
497
498     if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
499         con->mapped)
500         x_draw_decoration(con);
501 }
502
503 /*
504  * This function pushes the properties of each node of the layout tree to
505  * X11 if they have changed (like the map state, position of the window, â€¦).
506  * It recursively traverses all children of the given node.
507  *
508  */
509 void x_push_node(Con *con) {
510     Con *current;
511     con_state *state;
512     Rect rect = con->rect;
513
514     //DLOG("Pushing changes for node %p / %s\n", con, con->name);
515     state = state_for_frame(con->frame);
516
517     if (state->name != NULL) {
518         DLOG("pushing name %s for con %p\n", state->name, con);
519
520         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame,
521                             A_WM_NAME, A_STRING, 8, strlen(state->name), state->name);
522         FREE(state->name);
523     }
524
525     if (con->window == NULL) {
526         /* Calculate the height of all window decorations which will be drawn on to
527          * this frame. */
528         uint32_t max_y = 0, max_height = 0;
529         TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
530             Rect *dr = &(current->deco_rect);
531             if (dr->y >= max_y && dr->height >= max_height) {
532                 max_y = dr->y;
533                 max_height = dr->height;
534             }
535         }
536         rect.height = max_y + max_height;
537         if (rect.height == 0) {
538             DLOG("Unmapping container %p because it does not contain anything.\n", con);
539             con->mapped = false;
540         }
541     }
542
543     /* reparent the child window (when the window was moved due to a sticky
544      * container) */
545     if (state->need_reparent && con->window != NULL) {
546         DLOG("Reparenting child window\n");
547
548         /* Temporarily set the event masks to XCB_NONE so that we won’t get
549          * UnmapNotify events (otherwise the handler would close the container).
550          * These events are generated automatically when reparenting. */
551         uint32_t values[] = { XCB_NONE };
552         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
553         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
554
555         xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
556
557         values[0] = FRAME_EVENT_MASK;
558         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
559         values[0] = CHILD_EVENT_MASK;
560         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
561
562         state->old_frame = XCB_NONE;
563         state->need_reparent = false;
564
565         con->ignore_unmap++;
566         DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
567                 con, con->window->id, con->ignore_unmap);
568     }
569
570     bool fake_notify = false;
571     /* Set new position if rect changed (and if height > 0) */
572     if (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
573         rect.height > 0) {
574         /* We first create the new pixmap, then render to it, set it as the
575          * background and only afterwards change the window size. This reduces
576          * flickering. */
577
578         /* As the pixmap only depends on the size and not on the position, it
579          * is enough to check if width/height have changed. Also, we don’t
580          * create a pixmap at all when the window is actually not visible
581          * (height == 0). */
582         if ((state->rect.width != rect.width ||
583             state->rect.height != rect.height)) {
584             DLOG("CACHE: creating new pixmap for con %p (old: %d x %d, new: %d x %d)\n",
585                     con, state->rect.width, state->rect.height,
586                     rect.width, rect.height);
587             if (con->pixmap == 0) {
588                 con->pixmap = xcb_generate_id(conn);
589                 con->pm_gc = xcb_generate_id(conn);
590             } else {
591                 xcb_free_pixmap(conn, con->pixmap);
592                 xcb_free_gc(conn, con->pm_gc);
593             }
594             xcb_create_pixmap(conn, root_depth, con->pixmap, con->frame, rect.width, rect.height);
595             /* For the graphics context, we disable GraphicsExposure events.
596              * Those will be sent when a CopyArea request cannot be fulfilled
597              * properly due to parts of the source being unmapped or otherwise
598              * unavailable. Since we always copy from pixmaps to windows, this
599              * is not a concern for us. */
600             uint32_t values[] = { 0 };
601             xcb_create_gc(conn, con->pm_gc, con->pixmap, XCB_GC_GRAPHICS_EXPOSURES, values);
602
603             con->pixmap_recreated = true;
604
605             /* Don’t render the decoration for windows inside a stack which are
606              * not visible right now */
607             if (!con->parent ||
608                 con->parent->layout != L_STACKED ||
609                 TAILQ_FIRST(&(con->parent->focus_head)) == con)
610                 /* Render the decoration now to make the correct decoration visible
611                  * from the very first moment. Later calls will be cached, so this
612                  * doesn’t hurt performance. */
613                 x_deco_recurse(con);
614         }
615
616         DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
617         /* flush to ensure that the following commands are sent in a single
618          * buffer and will be processed directly afterwards (the contents of a
619          * window get lost when resizing it, therefore we want to provide it as
620          * fast as possible) */
621         xcb_flush(conn);
622         xcb_set_window_rect(conn, con->frame, rect);
623         if (con->pixmap != XCB_NONE)
624             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
625         xcb_flush(conn);
626
627         memcpy(&(state->rect), &rect, sizeof(Rect));
628         fake_notify = true;
629     }
630
631     /* dito, but for child windows */
632     if (con->window != NULL &&
633         memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
634         DLOG("setting window rect (%d, %d, %d, %d)\n",
635             con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
636         xcb_set_window_rect(conn, con->window->id, con->window_rect);
637         memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
638         fake_notify = true;
639     }
640
641     /* Map if map state changed, also ensure that the child window
642      * is changed if we are mapped *and* in initial state (meaning the
643      * container was empty before, but now got a child). Unmaps are handled in
644      * x_push_node_unmaps(). */
645     if ((state->mapped != con->mapped || (con->mapped && state->initial)) &&
646         con->mapped) {
647         xcb_void_cookie_t cookie;
648
649         if (con->window != NULL) {
650             /* Set WM_STATE_NORMAL because GTK applications don’t want to
651              * drag & drop if we don’t. Also, xprop(1) needs it. */
652             long data[] = { XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE };
653             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
654                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
655         }
656
657         uint32_t values[1];
658         if (!state->child_mapped && con->window != NULL) {
659             cookie = xcb_map_window(conn, con->window->id);
660
661             /* We are interested in EnterNotifys as soon as the window is
662              * mapped */
663             values[0] = CHILD_EVENT_MASK;
664             xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
665             DLOG("mapping child window (serial %d)\n", cookie.sequence);
666             state->child_mapped = true;
667         }
668
669         cookie = xcb_map_window(conn, con->frame);
670
671         values[0] = FRAME_EVENT_MASK;
672         xcb_change_window_attributes(conn, con->frame, XCB_CW_EVENT_MASK, values);
673
674         /* copy the pixmap contents to the frame window immediately after mapping */
675         if (con->pixmap != XCB_NONE)
676             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
677         xcb_flush(conn);
678
679         DLOG("mapping container %08x (serial %d)\n", con->frame, cookie.sequence);
680         state->mapped = con->mapped;
681     }
682
683     state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
684
685     if (fake_notify) {
686         DLOG("Sending fake configure notify\n");
687         fake_absolute_configure_notify(con);
688     }
689
690     /* Handle all children and floating windows of this node. We recurse
691      * in focus order to display the focused client in a stack first when
692      * switching workspaces (reduces flickering). */
693     TAILQ_FOREACH(current, &(con->focus_head), focused)
694         x_push_node(current);
695 }
696
697 /*
698  * Same idea as in x_push_node(), but this function only unmaps windows. It is
699  * necessary to split this up to handle new fullscreen clients properly: The
700  * new window needs to be mapped and focus needs to be set *before* the
701  * underlying windows are unmapped. Otherwise, focus will revert to the
702  * PointerRoot and will then be set to the new window, generating unnecessary
703  * FocusIn/FocusOut events.
704  *
705  */
706 static void x_push_node_unmaps(Con *con) {
707     Con *current;
708     con_state *state;
709
710     //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
711     state = state_for_frame(con->frame);
712
713     /* map/unmap if map state changed, also ensure that the child window
714      * is changed if we are mapped *and* in initial state (meaning the
715      * container was empty before, but now got a child) */
716     if (state->unmap_now) {
717         xcb_void_cookie_t cookie;
718         if (con->window != NULL) {
719             /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
720             long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
721             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
722                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
723         }
724
725         cookie = xcb_unmap_window(conn, con->frame);
726         DLOG("unmapping container (serial %d)\n", cookie.sequence);
727         /* we need to increase ignore_unmap for this container (if it
728          * contains a window) and for every window "under" this one which
729          * contains a window */
730         if (con->window != NULL) {
731             con->ignore_unmap++;
732             DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame, con->ignore_unmap);
733         }
734         state->mapped = con->mapped;
735     }
736
737     /* handle all children and floating windows of this node */
738     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
739         x_push_node_unmaps(current);
740
741     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
742         x_push_node_unmaps(current);
743 }
744
745 /*
746  * Pushes all changes (state of each node, see x_push_node() and the window
747  * stack) to X11.
748  *
749  * NOTE: We need to push the stack first so that the windows have the correct
750  * stacking order. This is relevant for workspace switching where we map the
751  * windows because mapping may generate EnterNotify events. When they are
752  * generated in the wrong order, this will cause focus problems when switching
753  * workspaces.
754  *
755  */
756 void x_push_changes(Con *con) {
757     con_state *state;
758
759     DLOG("-- PUSHING WINDOW STACK --\n");
760     //DLOG("Disabling EnterNotify\n");
761     uint32_t values[1] = { XCB_NONE };
762     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
763         if (state->mapped)
764             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
765     }
766     //DLOG("Done, EnterNotify disabled\n");
767     bool order_changed = false;
768
769     /* count first, necessary to (re)allocate memory for the bottom-to-top
770      * stack afterwards */
771     int cnt = 0;
772     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
773         if (state->con && state->con->window)
774             cnt++;
775
776     if (cnt != btt_stack_num) {
777         btt_stack = srealloc(btt_stack, sizeof(xcb_window_t) * cnt);
778         btt_stack_num = cnt;
779     }
780
781     xcb_window_t *walk = btt_stack;
782
783     /* X11 correctly represents the stack if we push it from bottom to top */
784     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
785         if (state->con && state->con->window)
786             memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
787
788         //DLOG("stack: 0x%08x\n", state->id);
789         con_state *prev = CIRCLEQ_PREV(state, state);
790         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
791         if ((prev != old_prev || state->initial) && prev != CIRCLEQ_END(&state_head)) {
792             order_changed = true;
793             DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
794             uint32_t mask = 0;
795             mask |= XCB_CONFIG_WINDOW_SIBLING;
796             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
797             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
798
799             xcb_configure_window(conn, prev->id, mask, values);
800         }
801         state->initial = false;
802     }
803
804     /* If we re-stacked something (or a new window appeared), we need to update
805      * the _NET_CLIENT_LIST_STACKING hint */
806     if (order_changed)
807         ewmh_update_client_list_stacking(btt_stack, btt_stack_num);
808
809     //DLOG("Re-enabling EnterNotify\n");
810     values[0] = FRAME_EVENT_MASK;
811     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
812         if (state->mapped)
813             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
814     }
815     //DLOG("Done, EnterNotify re-enabled\n");
816
817     DLOG("\n\n PUSHING CHANGES\n\n");
818     x_push_node(con);
819     x_deco_recurse(con);
820
821     xcb_window_t to_focus = focused->frame;
822     if (focused->window != NULL)
823         to_focus = focused->window->id;
824
825     DLOG("focused_id = 0x%08x, to_focus = 0x%08x\n", focused_id, to_focus);
826     if (focused_id != to_focus) {
827         if (!focused->mapped) {
828             DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
829             /* Invalidate focused_id to correctly focus new windows with the same ID */
830             focused_id = XCB_NONE;
831         } else {
832             DLOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
833             /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
834              * no focus change events for our own focus changes. We only want
835              * these generated by the clients. */
836             if (focused->window != NULL) {
837                 values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
838                 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
839             }
840             xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
841             if (focused->window != NULL) {
842                 values[0] = CHILD_EVENT_MASK;
843                 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
844             }
845
846             if (focused->window != NULL &&
847                 focused->window->needs_take_focus) {
848                 send_take_focus(to_focus);
849             }
850
851             ewmh_update_active_window(to_focus);
852             focused_id = to_focus;
853         }
854     }
855
856     if (focused_id == XCB_NONE) {
857         DLOG("Still no window focused, better set focus to the root window\n");
858         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
859         focused_id = root;
860     }
861
862     xcb_flush(conn);
863     DLOG("\n\n ENDING CHANGES\n\n");
864
865     /* Disable EnterWindow events for windows which will be unmapped in
866      * x_push_node_unmaps() now. Unmapping windows happens when switching
867      * workspaces. We want to avoid getting EnterNotifies during that phase
868      * because they would screw up our focus. One of these cases is having a
869      * stack with two windows. If the first window is focused and gets
870      * unmapped, the second one appears under the cursor and therefore gets an
871      * EnterNotify event. */
872     values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
873     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
874         if (!state->unmap_now)
875             continue;
876         xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
877     }
878
879     /* Push all pending unmaps */
880     x_push_node_unmaps(con);
881
882     /* save the current stack as old stack */
883     CIRCLEQ_FOREACH(state, &state_head, state) {
884         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
885         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
886     }
887     //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
888     //    DLOG("old stack: 0x%08x\n", state->id);
889     //}
890
891     xcb_flush(conn);
892 }
893
894 /*
895  * Raises the specified container in the internal stack of X windows. The
896  * next call to x_push_changes() will make the change visible in X11.
897  *
898  */
899 void x_raise_con(Con *con) {
900     con_state *state;
901     state = state_for_frame(con->frame);
902     //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
903
904     CIRCLEQ_REMOVE(&state_head, state, state);
905     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
906 }
907
908 /*
909  * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
910  * of the given name. Used for properly tagging the windows for easily spotting
911  * i3 windows in xwininfo -root -all.
912  *
913  */
914 void x_set_name(Con *con, const char *name) {
915     struct con_state *state;
916
917     if ((state = state_for_frame(con->frame)) == NULL) {
918         ELOG("window state not found\n");
919         return;
920     }
921
922     FREE(state->name);
923     state->name = sstrdup(name);
924 }
925
926 /*
927  * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
928  *
929  */
930 void x_set_i3_atoms() {
931     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
932                         (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
933                         current_socketpath);
934     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
935                         strlen(current_configpath), current_configpath);
936 }