]> git.sur5r.net Git - i3/i3/blob - src/x.c
Merge branch 'master' into next
[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     bool leaf = con_is_leaf(con);
259     /* This code needs to run for:
260      *  • leaf containers
261      *  • non-leaf containers which are in a stacked/tabbed container
262      *
263      * It does not need to run for:
264      *  • floating containers (they don’t have a decoration)
265      */
266     if ((!leaf &&
267          parent->layout != L_STACKED &&
268          parent->layout != L_TABBED) ||
269         con->type == CT_FLOATING_CON)
270         return;
271     DLOG("decoration should be rendered for con %p\n", con);
272
273     /* Skip containers whose height is 0 (for example empty dockareas) */
274     if (con->rect.height == 0) {
275         DLOG("height == 0, not rendering\n");
276         return;
277     }
278
279     /* Skip containers whose pixmap has not yet been created (can happen when
280      * decoration rendering happens recursively for a window for which
281      * x_push_node() was not yet called) */
282     if (leaf && con->pixmap == XCB_NONE) {
283         DLOG("pixmap not yet created, not rendering\n");
284         return;
285     }
286
287     /* 1: build deco_params and compare with cache */
288     struct deco_render_params *p = scalloc(sizeof(struct deco_render_params));
289
290     /* find out which colors to use */
291     if (con->urgent)
292         p->color = &config.client.urgent;
293     else if (con == focused || con_inside_focused(con))
294         p->color = &config.client.focused;
295     else if (con == TAILQ_FIRST(&(parent->focus_head)))
296         p->color = &config.client.focused_inactive;
297     else
298         p->color = &config.client.unfocused;
299
300     p->border_style = con_border_style(con);
301
302     Rect *r = &(con->rect);
303     Rect *w = &(con->window_rect);
304     p->con_rect = (struct width_height){ r->width, r->height };
305     p->con_window_rect = (struct width_height){ w->width, w->height };
306     p->con_deco_rect = con->deco_rect;
307     p->background = config.client.background;
308     p->con_is_leaf = con_is_leaf(con);
309     p->font = config.font.id;
310
311     if (con->deco_render_params != NULL &&
312         (con->window == NULL || !con->window->name_x_changed) &&
313         !parent->pixmap_recreated &&
314         !con->pixmap_recreated &&
315         memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
316         DLOG("CACHE HIT, copying existing pixmaps\n");
317         free(p);
318         goto copy_pixmaps;
319     }
320
321     DLOG("CACHE MISS\n");
322     Con *next = con;
323     while ((next = TAILQ_NEXT(next, nodes))) {
324         DLOG("Also invalidating cache of %p\n", next);
325         FREE(next->deco_render_params);
326     }
327
328     FREE(con->deco_render_params);
329     con->deco_render_params = p;
330
331     if (con->window != NULL && con->window->name_x_changed)
332         con->window->name_x_changed = false;
333
334     parent->pixmap_recreated = false;
335     con->pixmap_recreated = false;
336
337     /* 2: draw the client.background, but only for the parts around the client_rect */
338     if (con->window != NULL) {
339         xcb_rectangle_t background[] = {
340             /* top area */
341             { 0, 0, r->width, w->y },
342             /* bottom area */
343             { 0, (w->y + w->height), r->width, r->height - (w->y + w->height) },
344             /* left area */
345             { 0, 0, w->x, r->height },
346             /* right area */
347             { w->x + w->width, 0, r->width - (w->x + w->width), r->height }
348         };
349 #if 0
350         for (int i = 0; i < 4; i++)
351             DLOG("rect is (%d, %d) with %d x %d\n",
352                     background[i].x,
353                     background[i].y,
354                     background[i].width,
355                     background[i].height
356                 );
357 #endif
358
359         xcb_change_gc_single(conn, con->pm_gc, XCB_GC_FOREGROUND, config.client.background);
360         xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, sizeof(background) / sizeof(xcb_rectangle_t), background);
361     }
362
363     /* 3: draw a rectangle in border color around the client */
364     if (p->border_style != BS_NONE && p->con_is_leaf) {
365         Rect br = con_border_style_rect(con);
366 #if 0
367         DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
368         DLOG("border_rect spans (%d, %d) with %d x %d\n", br.x, br.y, br.width, br.height);
369         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);
370 #endif
371
372         /* These rectangles represents the border around the child window
373          * (left, bottom and right part). We don’t just fill the whole
374          * rectangle because some childs are not freely resizable and we want
375          * their background color to "shine through". */
376         xcb_change_gc_single(conn, con->pm_gc, XCB_GC_FOREGROUND, p->color->background);
377         xcb_rectangle_t borders[] = {
378             { 0, 0, br.x, r->height },
379             { 0, r->height + br.height + br.y, r->width, r->height },
380             { r->width + br.width + br.x, 0, r->width, r->height }
381         };
382         xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 3, borders);
383         /* 1pixel border needs an additional line at the top */
384         if (p->border_style == BS_1PIXEL) {
385             xcb_rectangle_t topline = { br.x, 0, con->rect.width + br.width + br.x, br.y };
386             xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, &topline);
387         }
388     }
389
390     /* if this is a borderless/1pixel window, we don’t * need to render the
391      * decoration. */
392     if (p->border_style != BS_NORMAL) {
393         DLOG("border style not BS_NORMAL, aborting rendering of decoration\n");
394         goto copy_pixmaps;
395     }
396
397     /* 4: paint the bar */
398     xcb_change_gc_single(conn, parent->pm_gc, XCB_GC_FOREGROUND, p->color->background);
399     xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
400     xcb_poly_fill_rectangle(conn, parent->pixmap, parent->pm_gc, 1, &drect);
401
402     /* 5: draw two unconnected lines in border color */
403     xcb_change_gc_single(conn, parent->pm_gc, XCB_GC_FOREGROUND, p->color->border);
404     Rect *dr = &(con->deco_rect);
405     xcb_segment_t segments[] = {
406         { dr->x,                 dr->y,
407           dr->x + dr->width - 1, dr->y },
408
409         { dr->x + 2,             dr->y + dr->height - 1,
410           dr->x + dr->width - 3, dr->y + dr->height - 1 }
411     };
412     xcb_poly_segment(conn, parent->pixmap, parent->pm_gc, 2, segments);
413
414     /* 6: draw the title */
415     uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
416     uint32_t values[] = { p->color->text, p->color->background, config.font.id };
417     xcb_change_gc(conn, parent->pm_gc, mask, values);
418     int text_offset_y = config.font.height + (con->deco_rect.height - config.font.height) / 2 - 1;
419
420     struct Window *win = con->window;
421     if (win == NULL || win->name_x == NULL) {
422         /* this is a non-leaf container, we need to make up a good description */
423         // TODO: use a good description instead of just "another container"
424         xcb_image_text_8(
425             conn,
426             strlen("another container"),
427             parent->pixmap,
428             parent->pm_gc,
429             con->deco_rect.x + 2,
430             con->deco_rect.y + text_offset_y,
431             "another container"
432         );
433
434         goto copy_pixmaps;
435     }
436
437     int indent_level = 0,
438         indent_mult = 0;
439     Con *il_parent = parent;
440     if (il_parent->layout != L_STACKED) {
441         while (1) {
442             DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
443             if (il_parent->layout == L_STACKED)
444                 indent_level++;
445             if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
446                 break;
447             il_parent = il_parent->parent;
448             indent_mult++;
449         }
450     }
451     DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
452     int indent_px = (indent_level * 5) * indent_mult;
453
454     if (win->uses_net_wm_name)
455         xcb_image_text_16(
456             conn,
457             win->name_len,
458             parent->pixmap,
459             parent->pm_gc,
460             con->deco_rect.x + 2 + indent_px,
461             con->deco_rect.y + text_offset_y,
462             (xcb_char2b_t*)win->name_x
463         );
464     else
465         xcb_image_text_8(
466             conn,
467             win->name_len,
468             parent->pixmap,
469             parent->pm_gc,
470             con->deco_rect.x + 2 + indent_px,
471             con->deco_rect.y + text_offset_y,
472             win->name_x
473         );
474
475 copy_pixmaps:
476     xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
477 }
478
479 /*
480  * Recursively calls x_draw_decoration. This cannot be done in x_push_node
481  * because x_push_node uses focus order to recurse (see the comment above)
482  * while drawing the decoration needs to happen in the actual order.
483  *
484  */
485 void x_deco_recurse(Con *con) {
486     Con *current;
487     bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
488                 TAILQ_EMPTY(&(con->floating_head));
489     con_state *state = state_for_frame(con->frame);
490
491     if (!leaf) {
492         TAILQ_FOREACH(current, &(con->nodes_head), nodes)
493             x_deco_recurse(current);
494
495         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
496             x_deco_recurse(current);
497
498         if (state->mapped)
499             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
500     }
501
502     if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
503         (!leaf || con->mapped))
504         x_draw_decoration(con);
505 }
506
507 /*
508  * This function pushes the properties of each node of the layout tree to
509  * X11 if they have changed (like the map state, position of the window, …).
510  * It recursively traverses all children of the given node.
511  *
512  */
513 void x_push_node(Con *con) {
514     Con *current;
515     con_state *state;
516     Rect rect = con->rect;
517
518     //DLOG("Pushing changes for node %p / %s\n", con, con->name);
519     state = state_for_frame(con->frame);
520
521     if (state->name != NULL) {
522         DLOG("pushing name %s for con %p\n", state->name, con);
523
524         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame,
525                             XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(state->name), state->name);
526         FREE(state->name);
527     }
528
529     if (con->window == NULL) {
530         /* Calculate the height of all window decorations which will be drawn on to
531          * this frame. */
532         uint32_t max_y = 0, max_height = 0;
533         TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
534             Rect *dr = &(current->deco_rect);
535             if (dr->y >= max_y && dr->height >= max_height) {
536                 max_y = dr->y;
537                 max_height = dr->height;
538             }
539         }
540         rect.height = max_y + max_height;
541         if (rect.height == 0) {
542             DLOG("Unmapping container %p because it does not contain anything.\n", con);
543             con->mapped = false;
544         }
545     }
546
547     /* reparent the child window (when the window was moved due to a sticky
548      * container) */
549     if (state->need_reparent && con->window != NULL) {
550         DLOG("Reparenting child window\n");
551
552         /* Temporarily set the event masks to XCB_NONE so that we won’t get
553          * UnmapNotify events (otherwise the handler would close the container).
554          * These events are generated automatically when reparenting. */
555         uint32_t values[] = { XCB_NONE };
556         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
557         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
558
559         xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
560
561         values[0] = FRAME_EVENT_MASK;
562         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
563         values[0] = CHILD_EVENT_MASK;
564         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
565
566         state->old_frame = XCB_NONE;
567         state->need_reparent = false;
568
569         con->ignore_unmap++;
570         DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
571                 con, con->window->id, con->ignore_unmap);
572     }
573
574     bool fake_notify = false;
575     /* Set new position if rect changed (and if height > 0) */
576     if (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
577         rect.height > 0) {
578         /* We first create the new pixmap, then render to it, set it as the
579          * background and only afterwards change the window size. This reduces
580          * flickering. */
581
582         /* As the pixmap only depends on the size and not on the position, it
583          * is enough to check if width/height have changed. Also, we don’t
584          * create a pixmap at all when the window is actually not visible
585          * (height == 0). */
586         if ((state->rect.width != rect.width ||
587             state->rect.height != rect.height)) {
588             DLOG("CACHE: creating new pixmap for con %p (old: %d x %d, new: %d x %d)\n",
589                     con, state->rect.width, state->rect.height,
590                     rect.width, rect.height);
591             if (con->pixmap == 0) {
592                 con->pixmap = xcb_generate_id(conn);
593                 con->pm_gc = xcb_generate_id(conn);
594             } else {
595                 xcb_free_pixmap(conn, con->pixmap);
596                 xcb_free_gc(conn, con->pm_gc);
597             }
598             xcb_create_pixmap(conn, root_depth, con->pixmap, con->frame, rect.width, rect.height);
599             /* For the graphics context, we disable GraphicsExposure events.
600              * Those will be sent when a CopyArea request cannot be fulfilled
601              * properly due to parts of the source being unmapped or otherwise
602              * unavailable. Since we always copy from pixmaps to windows, this
603              * is not a concern for us. */
604             uint32_t values[] = { 0 };
605             xcb_create_gc(conn, con->pm_gc, con->pixmap, XCB_GC_GRAPHICS_EXPOSURES, values);
606
607             con->pixmap_recreated = true;
608
609             /* Don’t render the decoration for windows inside a stack which are
610              * not visible right now */
611             if (!con->parent ||
612                 con->parent->layout != L_STACKED ||
613                 TAILQ_FIRST(&(con->parent->focus_head)) == con)
614                 /* Render the decoration now to make the correct decoration visible
615                  * from the very first moment. Later calls will be cached, so this
616                  * doesn’t hurt performance. */
617                 x_deco_recurse(con);
618         }
619
620         DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
621         /* flush to ensure that the following commands are sent in a single
622          * buffer and will be processed directly afterwards (the contents of a
623          * window get lost when resizing it, therefore we want to provide it as
624          * fast as possible) */
625         xcb_flush(conn);
626         xcb_set_window_rect(conn, con->frame, rect);
627         if (con->pixmap != XCB_NONE)
628             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
629         xcb_flush(conn);
630
631         memcpy(&(state->rect), &rect, sizeof(Rect));
632         fake_notify = true;
633     }
634
635     /* dito, but for child windows */
636     if (con->window != NULL &&
637         memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
638         DLOG("setting window rect (%d, %d, %d, %d)\n",
639             con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
640         xcb_set_window_rect(conn, con->window->id, con->window_rect);
641         memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
642         fake_notify = true;
643     }
644
645     /* Map if map state changed, also ensure that the child window
646      * is changed if we are mapped *and* in initial state (meaning the
647      * container was empty before, but now got a child). Unmaps are handled in
648      * x_push_node_unmaps(). */
649     if ((state->mapped != con->mapped || (con->mapped && state->initial)) &&
650         con->mapped) {
651         xcb_void_cookie_t cookie;
652
653         if (con->window != NULL) {
654             /* Set WM_STATE_NORMAL because GTK applications don’t want to
655              * drag & drop if we don’t. Also, xprop(1) needs it. */
656             long data[] = { XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE };
657             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
658                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
659         }
660
661         uint32_t values[1];
662         if (!state->child_mapped && con->window != NULL) {
663             cookie = xcb_map_window(conn, con->window->id);
664
665             /* We are interested in EnterNotifys as soon as the window is
666              * mapped */
667             values[0] = CHILD_EVENT_MASK;
668             xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
669             DLOG("mapping child window (serial %d)\n", cookie.sequence);
670             state->child_mapped = true;
671         }
672
673         cookie = xcb_map_window(conn, con->frame);
674
675         values[0] = FRAME_EVENT_MASK;
676         xcb_change_window_attributes(conn, con->frame, XCB_CW_EVENT_MASK, values);
677
678         /* copy the pixmap contents to the frame window immediately after mapping */
679         if (con->pixmap != XCB_NONE)
680             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
681         xcb_flush(conn);
682
683         DLOG("mapping container %08x (serial %d)\n", con->frame, cookie.sequence);
684         state->mapped = con->mapped;
685     }
686
687     state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
688
689     if (fake_notify) {
690         DLOG("Sending fake configure notify\n");
691         fake_absolute_configure_notify(con);
692     }
693
694     /* Handle all children and floating windows of this node. We recurse
695      * in focus order to display the focused client in a stack first when
696      * switching workspaces (reduces flickering). */
697     TAILQ_FOREACH(current, &(con->focus_head), focused)
698         x_push_node(current);
699 }
700
701 /*
702  * Same idea as in x_push_node(), but this function only unmaps windows. It is
703  * necessary to split this up to handle new fullscreen clients properly: The
704  * new window needs to be mapped and focus needs to be set *before* the
705  * underlying windows are unmapped. Otherwise, focus will revert to the
706  * PointerRoot and will then be set to the new window, generating unnecessary
707  * FocusIn/FocusOut events.
708  *
709  */
710 static void x_push_node_unmaps(Con *con) {
711     Con *current;
712     con_state *state;
713
714     //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
715     state = state_for_frame(con->frame);
716
717     /* map/unmap if map state changed, also ensure that the child window
718      * is changed if we are mapped *and* in initial state (meaning the
719      * container was empty before, but now got a child) */
720     if (state->unmap_now) {
721         xcb_void_cookie_t cookie;
722         if (con->window != NULL) {
723             /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
724             long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
725             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
726                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
727         }
728
729         cookie = xcb_unmap_window(conn, con->frame);
730         DLOG("unmapping container (serial %d)\n", cookie.sequence);
731         /* we need to increase ignore_unmap for this container (if it
732          * contains a window) and for every window "under" this one which
733          * contains a window */
734         if (con->window != NULL) {
735             con->ignore_unmap++;
736             DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame, con->ignore_unmap);
737         }
738         state->mapped = con->mapped;
739     }
740
741     /* handle all children and floating windows of this node */
742     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
743         x_push_node_unmaps(current);
744
745     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
746         x_push_node_unmaps(current);
747 }
748
749 /*
750  * Pushes all changes (state of each node, see x_push_node() and the window
751  * stack) to X11.
752  *
753  * NOTE: We need to push the stack first so that the windows have the correct
754  * stacking order. This is relevant for workspace switching where we map the
755  * windows because mapping may generate EnterNotify events. When they are
756  * generated in the wrong order, this will cause focus problems when switching
757  * workspaces.
758  *
759  */
760 void x_push_changes(Con *con) {
761     con_state *state;
762     xcb_query_pointer_cookie_t pointercookie;
763
764     /* If we need to warp later, we request the pointer position as soon as possible */
765     if (warp_to) {
766         pointercookie = xcb_query_pointer(conn, root);
767     }
768
769     DLOG("-- PUSHING WINDOW STACK --\n");
770     //DLOG("Disabling EnterNotify\n");
771     uint32_t values[1] = { XCB_NONE };
772     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
773         if (state->mapped)
774             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
775     }
776     //DLOG("Done, EnterNotify disabled\n");
777     bool order_changed = false;
778     bool stacking_changed = false;
779
780     /* count first, necessary to (re)allocate memory for the bottom-to-top
781      * stack afterwards */
782     int cnt = 0;
783     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
784         if (state->con && state->con->window)
785             cnt++;
786
787     if (cnt != btt_stack_num) {
788         btt_stack = srealloc(btt_stack, sizeof(xcb_window_t) * cnt);
789         btt_stack_num = cnt;
790     }
791
792     xcb_window_t *walk = btt_stack;
793
794     /* X11 correctly represents the stack if we push it from bottom to top */
795     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
796         if (state->con && state->con->window)
797             memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
798
799         //DLOG("stack: 0x%08x\n", state->id);
800         con_state *prev = CIRCLEQ_PREV(state, state);
801         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
802         if (prev != old_prev)
803             order_changed = true;
804         if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
805             stacking_changed = true;
806             DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
807             uint32_t mask = 0;
808             mask |= XCB_CONFIG_WINDOW_SIBLING;
809             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
810             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
811
812             xcb_configure_window(conn, prev->id, mask, values);
813         }
814         state->initial = false;
815     }
816
817     /* If we re-stacked something (or a new window appeared), we need to update
818      * the _NET_CLIENT_LIST_STACKING hint */
819     if (stacking_changed)
820         ewmh_update_client_list_stacking(btt_stack, btt_stack_num);
821
822     DLOG("\n\n PUSHING CHANGES\n\n");
823     x_push_node(con);
824
825     //DLOG("Re-enabling EnterNotify\n");
826     values[0] = FRAME_EVENT_MASK;
827     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
828         if (state->mapped)
829             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
830     }
831     //DLOG("Done, EnterNotify re-enabled\n");
832
833     x_deco_recurse(con);
834
835     xcb_window_t to_focus = focused->frame;
836     if (focused->window != NULL)
837         to_focus = focused->window->id;
838
839     DLOG("focused_id = 0x%08x, to_focus = 0x%08x\n", focused_id, to_focus);
840     if (focused_id != to_focus) {
841         if (!focused->mapped) {
842             DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
843             /* Invalidate focused_id to correctly focus new windows with the same ID */
844             focused_id = XCB_NONE;
845         } else {
846             DLOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
847             /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
848              * no focus change events for our own focus changes. We only want
849              * these generated by the clients. */
850             if (focused->window != NULL) {
851                 values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
852                 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
853             }
854             xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
855             if (focused->window != NULL) {
856                 values[0] = CHILD_EVENT_MASK;
857                 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
858             }
859
860             if (focused->window != NULL &&
861                 focused->window->needs_take_focus) {
862                 send_take_focus(to_focus);
863             }
864
865             ewmh_update_active_window(to_focus);
866             focused_id = to_focus;
867         }
868     }
869
870     if (focused_id == XCB_NONE) {
871         DLOG("Still no window focused, better set focus to the root window\n");
872         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
873         focused_id = root;
874     }
875
876     if (warp_to) {
877         xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
878         if (!pointerreply) {
879             ELOG("Could not query pointer position, not warping pointer\n");
880         } else {
881             int mid_x = warp_to->x + (warp_to->width / 2);
882             int mid_y = warp_to->y + (warp_to->height / 2);
883
884             Output *current = get_output_containing(pointerreply->root_x, pointerreply->root_y);
885             Output *target = get_output_containing(mid_x, mid_y);
886             if (current != target)
887                 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
888         }
889         warp_to = NULL;
890     }
891
892     xcb_flush(conn);
893     DLOG("\n\n ENDING CHANGES\n\n");
894
895     /* Disable EnterWindow events for windows which will be unmapped in
896      * x_push_node_unmaps() now. Unmapping windows happens when switching
897      * workspaces. We want to avoid getting EnterNotifies during that phase
898      * because they would screw up our focus. One of these cases is having a
899      * stack with two windows. If the first window is focused and gets
900      * unmapped, the second one appears under the cursor and therefore gets an
901      * EnterNotify event. */
902     values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
903     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
904         if (!state->unmap_now)
905             continue;
906         xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
907     }
908
909     /* Push all pending unmaps */
910     x_push_node_unmaps(con);
911
912     /* save the current stack as old stack */
913     CIRCLEQ_FOREACH(state, &state_head, state) {
914         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
915         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
916     }
917     //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
918     //    DLOG("old stack: 0x%08x\n", state->id);
919     //}
920
921     xcb_flush(conn);
922 }
923
924 /*
925  * Raises the specified container in the internal stack of X windows. The
926  * next call to x_push_changes() will make the change visible in X11.
927  *
928  */
929 void x_raise_con(Con *con) {
930     con_state *state;
931     state = state_for_frame(con->frame);
932     //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
933
934     CIRCLEQ_REMOVE(&state_head, state, state);
935     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
936 }
937
938 /*
939  * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
940  * of the given name. Used for properly tagging the windows for easily spotting
941  * i3 windows in xwininfo -root -all.
942  *
943  */
944 void x_set_name(Con *con, const char *name) {
945     struct con_state *state;
946
947     if ((state = state_for_frame(con->frame)) == NULL) {
948         ELOG("window state not found\n");
949         return;
950     }
951
952     FREE(state->name);
953     state->name = sstrdup(name);
954 }
955
956 /*
957  * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
958  *
959  */
960 void x_set_i3_atoms() {
961     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
962                         (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
963                         current_socketpath);
964     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
965                         strlen(current_configpath), current_configpath);
966 }
967
968 /*
969  * Set warp_to coordinates.  This will trigger on the next call to
970  * x_push_changes().
971  *
972  */
973 void x_set_warp_to(Rect *rect)
974 {
975     warp_to = rect;
976 }