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