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