2 * vim:ts=4:sw=4:expandtab
7 /* Stores the X11 window ID of the currently focused window */
8 xcb_window_t focused_id = XCB_NONE;
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;
15 /* Stores coordinates to warp mouse pointer to if set */
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().
26 typedef struct con_state {
32 /** The con for which this state is. */
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). */
39 xcb_window_t old_frame;
48 CIRCLEQ_ENTRY(con_state) state;
49 CIRCLEQ_ENTRY(con_state) old_state;
52 CIRCLEQ_HEAD(state_head, con_state) state_head =
53 CIRCLEQ_HEAD_INITIALIZER(state_head);
55 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
56 CIRCLEQ_HEAD_INITIALIZER(old_state_head);
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
65 static con_state *state_for_frame(xcb_window_t window) {
67 CIRCLEQ_FOREACH(state, &state_head, state)
68 if (state->id == window)
71 /* TODO: better error handling? */
72 ELOG("No state found\n");
78 * Initializes the X11 part for the given container. Called exactly once for
79 * every container from con_new().
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 */
89 /* our own frames should not be managed */
90 mask |= XCB_CW_OVERRIDE_REDIRECT;
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;
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);
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);
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.
115 void x_reinit(Con *con) {
116 struct con_state *state;
118 if ((state = state_for_frame(con->frame)) == NULL) {
119 ELOG("window state not found\n");
123 DLOG("resetting state %p to initial\n", state);
124 state->initial = true;
125 state->child_mapped = false;
127 memset(&(state->window_rect), 0, sizeof(Rect));
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().
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");
142 state->need_reparent = true;
143 state->old_frame = old->frame;
147 * Moves a child window from Container src to Container dest.
150 void x_move_win(Con *src, Con *dest) {
151 struct con_state *state_src, *state_dest;
153 if ((state_src = state_for_frame(src->frame)) == NULL) {
154 ELOG("window state for src not found\n");
158 if ((state_dest = state_for_frame(dest->frame)) == NULL) {
159 ELOG("window state for dest not found\n");
163 state_dest->con = state_src->con;
164 state_src->con = NULL;
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");
174 * Kills the window decoration associated with the given container.
177 void x_con_kill(Con *con) {
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);
189 /* Invalidate focused_id to correctly focus new windows with the same ID */
190 focused_id = XCB_NONE;
194 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
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;
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)
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)
211 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
217 * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
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);
227 LOG("Killing the X11 client which owns window 0x%08x\n", window);
228 xcb_kill_client(conn, window);
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;
239 ev->response_type = XCB_CLIENT_MESSAGE;
241 ev->type = A_WM_PROTOCOLS;
243 ev->data.data32[0] = A_WM_DELETE_WINDOW;
244 ev->data.data32[1] = XCB_CURRENT_TIME;
246 LOG("Sending WM_DELETE to the client\n");
247 xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)ev);
253 * Draws the decoration of the given container onto its parent.
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
263 * It does not need to run for:
264 * • floating containers (they don’t have a decoration)
267 parent->layout != L_STACKED &&
268 parent->layout != L_TABBED) ||
269 con->type == CT_FLOATING_CON)
271 DLOG("decoration should be rendered for con %p\n", con);
273 /* Skip containers whose height is 0 (for example empty dockareas) */
274 if (con->rect.height == 0) {
275 DLOG("height == 0, not rendering\n");
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");
287 /* 1: build deco_params and compare with cache */
288 struct deco_render_params *p = scalloc(sizeof(struct deco_render_params));
290 /* find out which colors to use */
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;
298 p->color = &config.client.unfocused;
300 p->border_style = con_border_style(con);
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;
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");
321 DLOG("CACHE MISS\n");
323 while ((next = TAILQ_NEXT(next, nodes))) {
324 DLOG("Also invalidating cache of %p\n", next);
325 FREE(next->deco_render_params);
328 FREE(con->deco_render_params);
329 con->deco_render_params = p;
331 if (con->window != NULL && con->window->name_x_changed)
332 con->window->name_x_changed = false;
334 parent->pixmap_recreated = false;
335 con->pixmap_recreated = false;
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[] = {
341 { 0, 0, r->width, w->y },
343 { 0, (w->y + w->height), r->width, r->height - (w->y + w->height) },
345 { 0, 0, w->x, r->height },
347 { w->x + w->width, 0, r->width - (w->x + w->width), r->height }
350 for (int i = 0; i < 4; i++)
351 DLOG("rect is (%d, %d) with %d x %d\n",
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);
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);
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);
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 }
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);
390 /* if this is a borderless/1pixel window, we don’t * need to render the
392 if (p->border_style != BS_NORMAL) {
393 DLOG("border style not BS_NORMAL, aborting rendering of decoration\n");
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);
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[] = {
407 dr->x + dr->width - 1, dr->y },
409 { dr->x + 2, dr->y + dr->height - 1,
410 dr->x + dr->width - 3, dr->y + dr->height - 1 }
412 xcb_poly_segment(conn, parent->pixmap, parent->pm_gc, 2, segments);
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;
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"
426 strlen("another container"),
429 con->deco_rect.x + 2,
430 con->deco_rect.y + text_offset_y,
437 int indent_level = 0,
439 Con *il_parent = parent;
440 if (il_parent->layout != L_STACKED) {
442 DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
443 if (il_parent->layout == L_STACKED)
445 if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
447 il_parent = il_parent->parent;
451 DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
452 int indent_px = (indent_level * 5) * indent_mult;
454 if (win->uses_net_wm_name)
460 con->deco_rect.x + 2 + indent_px,
461 con->deco_rect.y + text_offset_y,
462 (xcb_char2b_t*)win->name_x
470 con->deco_rect.x + 2 + indent_px,
471 con->deco_rect.y + text_offset_y,
476 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
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.
485 void x_deco_recurse(Con *con) {
487 bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
488 TAILQ_EMPTY(&(con->floating_head));
489 con_state *state = state_for_frame(con->frame);
492 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
493 x_deco_recurse(current);
495 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
496 x_deco_recurse(current);
499 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
502 if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
503 (!leaf || con->mapped))
504 x_draw_decoration(con);
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.
513 void x_push_node(Con *con) {
516 Rect rect = con->rect;
518 //DLOG("Pushing changes for node %p / %s\n", con, con->name);
519 state = state_for_frame(con->frame);
521 if (state->name != NULL) {
522 DLOG("pushing name %s for con %p\n", state->name, con);
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);
529 if (con->window == NULL) {
530 /* Calculate the height of all window decorations which will be drawn on to
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) {
537 max_height = dr->height;
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);
547 /* reparent the child window (when the window was moved due to a sticky
549 if (state->need_reparent && con->window != NULL) {
550 DLOG("Reparenting child window\n");
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);
559 xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
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);
566 state->old_frame = XCB_NONE;
567 state->need_reparent = false;
570 DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
571 con, con->window->id, con->ignore_unmap);
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 &&
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
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
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);
595 xcb_free_pixmap(conn, con->pixmap);
596 xcb_free_gc(conn, con->pm_gc);
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);
607 con->pixmap_recreated = true;
609 /* Don’t render the decoration for windows inside a stack which are
610 * not visible right now */
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. */
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) */
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);
631 memcpy(&(state->rect), &rect, sizeof(Rect));
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));
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)) &&
651 xcb_void_cookie_t cookie;
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);
662 if (!state->child_mapped && con->window != NULL) {
663 cookie = xcb_map_window(conn, con->window->id);
665 /* We are interested in EnterNotifys as soon as the window is
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;
673 cookie = xcb_map_window(conn, con->frame);
675 values[0] = FRAME_EVENT_MASK;
676 xcb_change_window_attributes(conn, con->frame, XCB_CW_EVENT_MASK, values);
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);
683 DLOG("mapping container %08x (serial %d)\n", con->frame, cookie.sequence);
684 state->mapped = con->mapped;
687 state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
690 DLOG("Sending fake configure notify\n");
691 fake_absolute_configure_notify(con);
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);
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.
710 static void x_push_node_unmaps(Con *con) {
714 //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
715 state = state_for_frame(con->frame);
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);
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) {
736 DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame, con->ignore_unmap);
738 state->mapped = con->mapped;
741 /* handle all children and floating windows of this node */
742 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
743 x_push_node_unmaps(current);
745 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
746 x_push_node_unmaps(current);
750 * Pushes all changes (state of each node, see x_push_node() and the window
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
760 void x_push_changes(Con *con) {
762 xcb_query_pointer_cookie_t pointercookie;
764 /* If we need to warp later, we request the pointer position as soon as possible */
766 pointercookie = xcb_query_pointer(conn, root);
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) {
774 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
776 //DLOG("Done, EnterNotify disabled\n");
777 bool order_changed = false;
778 bool stacking_changed = false;
780 /* count first, necessary to (re)allocate memory for the bottom-to-top
781 * stack afterwards */
783 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
784 if (state->con && state->con->window)
787 if (cnt != btt_stack_num) {
788 btt_stack = srealloc(btt_stack, sizeof(xcb_window_t) * cnt);
792 xcb_window_t *walk = btt_stack;
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));
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);
808 mask |= XCB_CONFIG_WINDOW_SIBLING;
809 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
810 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
812 xcb_configure_window(conn, prev->id, mask, values);
814 state->initial = false;
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);
822 DLOG("\n\n PUSHING CHANGES\n\n");
825 //DLOG("Re-enabling EnterNotify\n");
826 values[0] = FRAME_EVENT_MASK;
827 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
829 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
831 //DLOG("Done, EnterNotify re-enabled\n");
835 xcb_window_t to_focus = focused->frame;
836 if (focused->window != NULL)
837 to_focus = focused->window->id;
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;
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);
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);
860 if (focused->window != NULL &&
861 focused->window->needs_take_focus) {
862 send_take_focus(to_focus);
865 ewmh_update_active_window(to_focus);
866 focused_id = to_focus;
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);
877 xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
879 ELOG("Could not query pointer position, not warping pointer\n");
881 int mid_x = warp_to->x + (warp_to->width / 2);
882 int mid_y = warp_to->y + (warp_to->height / 2);
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);
893 DLOG("\n\n ENDING CHANGES\n\n");
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)
906 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
909 /* Push all pending unmaps */
910 x_push_node_unmaps(con);
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);
917 //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
918 // DLOG("old stack: 0x%08x\n", state->id);
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.
929 void x_raise_con(Con *con) {
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);
934 CIRCLEQ_REMOVE(&state_head, state, state);
935 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
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.
944 void x_set_name(Con *con, const char *name) {
945 struct con_state *state;
947 if ((state = state_for_frame(con->frame)) == NULL) {
948 ELOG("window state not found\n");
953 state->name = sstrdup(name);
957 * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
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)),
964 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
965 strlen(current_configpath), current_configpath);
969 * Set warp_to coordinates. This will trigger on the next call to
973 void x_set_warp_to(Rect *rect)