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 /* This code needs to run for:
260 * • non-leaf containers which are in a stacked/tabbed container
262 * It does not need to run for:
263 * • floating containers (they don’t have a decoration)
265 if ((!con_is_leaf(con) &&
266 parent->layout != L_STACKED &&
267 parent->layout != L_TABBED) ||
268 con->type == CT_FLOATING_CON)
270 DLOG("decoration should be rendered for con %p\n", con);
272 /* Skip containers whose height is 0 (for example empty dockareas) */
273 if (con->rect.height == 0) {
274 DLOG("height == 0, not rendering\n");
278 /* Skip containers whose pixmap has not yet been created (can happen when
279 * decoration rendering happens recursively for a window for which
280 * x_push_node() was not yet called) */
281 if (con->pixmap == XCB_NONE) {
282 DLOG("pixmap not yet created, not rendering\n");
286 /* 1: build deco_params and compare with cache */
287 struct deco_render_params *p = scalloc(sizeof(struct deco_render_params));
289 /* find out which colors to use */
291 p->color = &config.client.urgent;
292 else if (con == focused)
293 p->color = &config.client.focused;
294 else if (con == TAILQ_FIRST(&(parent->focus_head)))
295 p->color = &config.client.focused_inactive;
297 p->color = &config.client.unfocused;
299 p->border_style = con_border_style(con);
301 Rect *r = &(con->rect);
302 Rect *w = &(con->window_rect);
303 p->con_rect = (struct width_height){ r->width, r->height };
304 p->con_window_rect = (struct width_height){ w->width, w->height };
305 p->con_deco_rect = con->deco_rect;
306 p->background = config.client.background;
307 p->con_is_leaf = con_is_leaf(con);
308 p->font = config.font.id;
310 if (con->deco_render_params != NULL &&
311 (con->window == NULL || !con->window->name_x_changed) &&
312 !parent->pixmap_recreated &&
313 !con->pixmap_recreated &&
314 memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
315 DLOG("CACHE HIT, copying existing pixmaps\n");
320 DLOG("CACHE MISS\n");
322 while ((next = TAILQ_NEXT(next, nodes))) {
323 DLOG("Also invalidating cache of %p\n", next);
324 FREE(next->deco_render_params);
327 FREE(con->deco_render_params);
328 con->deco_render_params = p;
330 if (con->window != NULL && con->window->name_x_changed)
331 con->window->name_x_changed = false;
333 parent->pixmap_recreated = false;
334 con->pixmap_recreated = false;
336 /* 2: draw the client.background, but only for the parts around the client_rect */
337 if (con->window != NULL) {
338 xcb_rectangle_t background[] = {
340 { 0, 0, r->width, w->y },
342 { 0, (w->y + w->height), r->width, r->height - (w->y + w->height) },
344 { 0, 0, w->x, r->height },
346 { w->x + w->width, 0, r->width - (w->x + w->width), r->height }
349 for (int i = 0; i < 4; i++)
350 DLOG("rect is (%d, %d) with %d x %d\n",
358 xcb_change_gc_single(conn, con->pm_gc, XCB_GC_FOREGROUND, config.client.background);
359 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, sizeof(background) / sizeof(xcb_rectangle_t), background);
362 /* 3: draw a rectangle in border color around the client */
363 if (p->border_style != BS_NONE && p->con_is_leaf) {
364 Rect br = con_border_style_rect(con);
366 DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
367 DLOG("border_rect spans (%d, %d) with %d x %d\n", br.x, br.y, br.width, br.height);
368 DLOG("window_rect spans (%d, %d) with %d x %d\n", con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
371 /* These rectangles represents the border around the child window
372 * (left, bottom and right part). We don’t just fill the whole
373 * rectangle because some childs are not freely resizable and we want
374 * their background color to "shine through". */
375 xcb_change_gc_single(conn, con->pm_gc, XCB_GC_FOREGROUND, p->color->background);
376 xcb_rectangle_t borders[] = {
377 { 0, 0, br.x, r->height },
378 { 0, r->height + br.height + br.y, r->width, r->height },
379 { r->width + br.width + br.x, 0, r->width, r->height }
381 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 3, borders);
382 /* 1pixel border needs an additional line at the top */
383 if (p->border_style == BS_1PIXEL) {
384 xcb_rectangle_t topline = { br.x, 0, con->rect.width + br.width + br.x, br.y };
385 xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, &topline);
389 /* if this is a borderless/1pixel window, we don’t * need to render the
391 if (p->border_style != BS_NORMAL) {
392 DLOG("border style not BS_NORMAL, aborting rendering of decoration\n");
396 /* 4: paint the bar */
397 xcb_change_gc_single(conn, parent->pm_gc, XCB_GC_FOREGROUND, p->color->background);
398 xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
399 xcb_poly_fill_rectangle(conn, parent->pixmap, parent->pm_gc, 1, &drect);
401 /* 5: draw two unconnected lines in border color */
402 xcb_change_gc_single(conn, parent->pm_gc, XCB_GC_FOREGROUND, p->color->border);
403 Rect *dr = &(con->deco_rect);
404 xcb_segment_t segments[] = {
406 dr->x + dr->width - 1, dr->y },
408 { dr->x + 2, dr->y + dr->height - 1,
409 dr->x + dr->width - 3, dr->y + dr->height - 1 }
411 xcb_poly_segment(conn, parent->pixmap, parent->pm_gc, 2, segments);
413 /* 6: draw the title */
414 uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
415 uint32_t values[] = { p->color->text, p->color->background, config.font.id };
416 xcb_change_gc(conn, parent->pm_gc, mask, values);
417 int text_offset_y = config.font.height + (con->deco_rect.height - config.font.height) / 2 - 1;
419 struct Window *win = con->window;
420 if (win == NULL || win->name_x == NULL) {
421 /* this is a non-leaf container, we need to make up a good description */
422 // TODO: use a good description instead of just "another container"
425 strlen("another container"),
428 con->deco_rect.x + 2,
429 con->deco_rect.y + text_offset_y,
436 int indent_level = 0,
438 Con *il_parent = parent;
439 if (il_parent->layout != L_STACKED) {
441 DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
442 if (il_parent->layout == L_STACKED)
444 if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
446 il_parent = il_parent->parent;
450 DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
451 int indent_px = (indent_level * 5) * indent_mult;
453 if (win->uses_net_wm_name)
459 con->deco_rect.x + 2 + indent_px,
460 con->deco_rect.y + text_offset_y,
461 (xcb_char2b_t*)win->name_x
469 con->deco_rect.x + 2 + indent_px,
470 con->deco_rect.y + text_offset_y,
475 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
479 * Recursively calls x_draw_decoration. This cannot be done in x_push_node
480 * because x_push_node uses focus order to recurse (see the comment above)
481 * while drawing the decoration needs to happen in the actual order.
484 void x_deco_recurse(Con *con) {
486 bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
487 TAILQ_EMPTY(&(con->floating_head));
488 con_state *state = state_for_frame(con->frame);
491 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
492 x_deco_recurse(current);
494 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
495 x_deco_recurse(current);
498 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
501 if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
503 x_draw_decoration(con);
507 * This function pushes the properties of each node of the layout tree to
508 * X11 if they have changed (like the map state, position of the window, …).
509 * It recursively traverses all children of the given node.
512 void x_push_node(Con *con) {
515 Rect rect = con->rect;
517 //DLOG("Pushing changes for node %p / %s\n", con, con->name);
518 state = state_for_frame(con->frame);
520 if (state->name != NULL) {
521 DLOG("pushing name %s for con %p\n", state->name, con);
523 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame,
524 XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(state->name), state->name);
528 if (con->window == NULL) {
529 /* Calculate the height of all window decorations which will be drawn on to
531 uint32_t max_y = 0, max_height = 0;
532 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
533 Rect *dr = &(current->deco_rect);
534 if (dr->y >= max_y && dr->height >= max_height) {
536 max_height = dr->height;
539 rect.height = max_y + max_height;
540 if (rect.height == 0) {
541 DLOG("Unmapping container %p because it does not contain anything.\n", con);
546 /* reparent the child window (when the window was moved due to a sticky
548 if (state->need_reparent && con->window != NULL) {
549 DLOG("Reparenting child window\n");
551 /* Temporarily set the event masks to XCB_NONE so that we won’t get
552 * UnmapNotify events (otherwise the handler would close the container).
553 * These events are generated automatically when reparenting. */
554 uint32_t values[] = { XCB_NONE };
555 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
556 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
558 xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
560 values[0] = FRAME_EVENT_MASK;
561 xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
562 values[0] = CHILD_EVENT_MASK;
563 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
565 state->old_frame = XCB_NONE;
566 state->need_reparent = false;
569 DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
570 con, con->window->id, con->ignore_unmap);
573 bool fake_notify = false;
574 /* Set new position if rect changed (and if height > 0) */
575 if (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
577 /* We first create the new pixmap, then render to it, set it as the
578 * background and only afterwards change the window size. This reduces
581 /* As the pixmap only depends on the size and not on the position, it
582 * is enough to check if width/height have changed. Also, we don’t
583 * create a pixmap at all when the window is actually not visible
585 if ((state->rect.width != rect.width ||
586 state->rect.height != rect.height)) {
587 DLOG("CACHE: creating new pixmap for con %p (old: %d x %d, new: %d x %d)\n",
588 con, state->rect.width, state->rect.height,
589 rect.width, rect.height);
590 if (con->pixmap == 0) {
591 con->pixmap = xcb_generate_id(conn);
592 con->pm_gc = xcb_generate_id(conn);
594 xcb_free_pixmap(conn, con->pixmap);
595 xcb_free_gc(conn, con->pm_gc);
597 xcb_create_pixmap(conn, root_depth, con->pixmap, con->frame, rect.width, rect.height);
598 /* For the graphics context, we disable GraphicsExposure events.
599 * Those will be sent when a CopyArea request cannot be fulfilled
600 * properly due to parts of the source being unmapped or otherwise
601 * unavailable. Since we always copy from pixmaps to windows, this
602 * is not a concern for us. */
603 uint32_t values[] = { 0 };
604 xcb_create_gc(conn, con->pm_gc, con->pixmap, XCB_GC_GRAPHICS_EXPOSURES, values);
606 con->pixmap_recreated = true;
608 /* Don’t render the decoration for windows inside a stack which are
609 * not visible right now */
611 con->parent->layout != L_STACKED ||
612 TAILQ_FIRST(&(con->parent->focus_head)) == con)
613 /* Render the decoration now to make the correct decoration visible
614 * from the very first moment. Later calls will be cached, so this
615 * doesn’t hurt performance. */
619 DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
620 /* flush to ensure that the following commands are sent in a single
621 * buffer and will be processed directly afterwards (the contents of a
622 * window get lost when resizing it, therefore we want to provide it as
623 * fast as possible) */
625 xcb_set_window_rect(conn, con->frame, rect);
626 if (con->pixmap != XCB_NONE)
627 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
630 memcpy(&(state->rect), &rect, sizeof(Rect));
634 /* dito, but for child windows */
635 if (con->window != NULL &&
636 memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
637 DLOG("setting window rect (%d, %d, %d, %d)\n",
638 con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
639 xcb_set_window_rect(conn, con->window->id, con->window_rect);
640 memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
644 /* Map if map state changed, also ensure that the child window
645 * is changed if we are mapped *and* in initial state (meaning the
646 * container was empty before, but now got a child). Unmaps are handled in
647 * x_push_node_unmaps(). */
648 if ((state->mapped != con->mapped || (con->mapped && state->initial)) &&
650 xcb_void_cookie_t cookie;
652 if (con->window != NULL) {
653 /* Set WM_STATE_NORMAL because GTK applications don’t want to
654 * drag & drop if we don’t. Also, xprop(1) needs it. */
655 long data[] = { XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE };
656 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
657 A_WM_STATE, A_WM_STATE, 32, 2, data);
661 if (!state->child_mapped && con->window != NULL) {
662 cookie = xcb_map_window(conn, con->window->id);
664 /* We are interested in EnterNotifys as soon as the window is
666 values[0] = CHILD_EVENT_MASK;
667 xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
668 DLOG("mapping child window (serial %d)\n", cookie.sequence);
669 state->child_mapped = true;
672 cookie = xcb_map_window(conn, con->frame);
674 values[0] = FRAME_EVENT_MASK;
675 xcb_change_window_attributes(conn, con->frame, XCB_CW_EVENT_MASK, values);
677 /* copy the pixmap contents to the frame window immediately after mapping */
678 if (con->pixmap != XCB_NONE)
679 xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
682 DLOG("mapping container %08x (serial %d)\n", con->frame, cookie.sequence);
683 state->mapped = con->mapped;
686 state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
689 DLOG("Sending fake configure notify\n");
690 fake_absolute_configure_notify(con);
693 /* Handle all children and floating windows of this node. We recurse
694 * in focus order to display the focused client in a stack first when
695 * switching workspaces (reduces flickering). */
696 TAILQ_FOREACH(current, &(con->focus_head), focused)
697 x_push_node(current);
701 * Same idea as in x_push_node(), but this function only unmaps windows. It is
702 * necessary to split this up to handle new fullscreen clients properly: The
703 * new window needs to be mapped and focus needs to be set *before* the
704 * underlying windows are unmapped. Otherwise, focus will revert to the
705 * PointerRoot and will then be set to the new window, generating unnecessary
706 * FocusIn/FocusOut events.
709 static void x_push_node_unmaps(Con *con) {
713 //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
714 state = state_for_frame(con->frame);
716 /* map/unmap if map state changed, also ensure that the child window
717 * is changed if we are mapped *and* in initial state (meaning the
718 * container was empty before, but now got a child) */
719 if (state->unmap_now) {
720 xcb_void_cookie_t cookie;
721 if (con->window != NULL) {
722 /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
723 long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
724 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
725 A_WM_STATE, A_WM_STATE, 32, 2, data);
728 cookie = xcb_unmap_window(conn, con->frame);
729 DLOG("unmapping container (serial %d)\n", cookie.sequence);
730 /* we need to increase ignore_unmap for this container (if it
731 * contains a window) and for every window "under" this one which
732 * contains a window */
733 if (con->window != NULL) {
735 DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame, con->ignore_unmap);
737 state->mapped = con->mapped;
740 /* handle all children and floating windows of this node */
741 TAILQ_FOREACH(current, &(con->nodes_head), nodes)
742 x_push_node_unmaps(current);
744 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
745 x_push_node_unmaps(current);
749 * Pushes all changes (state of each node, see x_push_node() and the window
752 * NOTE: We need to push the stack first so that the windows have the correct
753 * stacking order. This is relevant for workspace switching where we map the
754 * windows because mapping may generate EnterNotify events. When they are
755 * generated in the wrong order, this will cause focus problems when switching
759 void x_push_changes(Con *con) {
762 DLOG("-- PUSHING WINDOW STACK --\n");
763 //DLOG("Disabling EnterNotify\n");
764 uint32_t values[1] = { XCB_NONE };
765 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
767 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
769 //DLOG("Done, EnterNotify disabled\n");
770 bool order_changed = false;
771 bool stacking_changed = false;
773 /* count first, necessary to (re)allocate memory for the bottom-to-top
774 * stack afterwards */
776 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
777 if (state->con && state->con->window)
780 if (cnt != btt_stack_num) {
781 btt_stack = srealloc(btt_stack, sizeof(xcb_window_t) * cnt);
785 xcb_window_t *walk = btt_stack;
787 /* X11 correctly represents the stack if we push it from bottom to top */
788 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
789 if (state->con && state->con->window)
790 memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
792 //DLOG("stack: 0x%08x\n", state->id);
793 con_state *prev = CIRCLEQ_PREV(state, state);
794 con_state *old_prev = CIRCLEQ_PREV(state, old_state);
795 if (prev != old_prev)
796 order_changed = true;
797 if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
798 stacking_changed = true;
799 DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
801 mask |= XCB_CONFIG_WINDOW_SIBLING;
802 mask |= XCB_CONFIG_WINDOW_STACK_MODE;
803 uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
805 xcb_configure_window(conn, prev->id, mask, values);
807 state->initial = false;
810 /* If we re-stacked something (or a new window appeared), we need to update
811 * the _NET_CLIENT_LIST_STACKING hint */
812 if (stacking_changed)
813 ewmh_update_client_list_stacking(btt_stack, btt_stack_num);
815 //DLOG("Re-enabling EnterNotify\n");
816 values[0] = FRAME_EVENT_MASK;
817 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
819 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
821 //DLOG("Done, EnterNotify re-enabled\n");
823 DLOG("\n\n PUSHING CHANGES\n\n");
827 xcb_window_t to_focus = focused->frame;
828 if (focused->window != NULL)
829 to_focus = focused->window->id;
831 DLOG("focused_id = 0x%08x, to_focus = 0x%08x\n", focused_id, to_focus);
832 if (focused_id != to_focus) {
833 if (!focused->mapped) {
834 DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
835 /* Invalidate focused_id to correctly focus new windows with the same ID */
836 focused_id = XCB_NONE;
838 DLOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
839 /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
840 * no focus change events for our own focus changes. We only want
841 * these generated by the clients. */
842 if (focused->window != NULL) {
843 values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
844 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
846 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
847 if (focused->window != NULL) {
848 values[0] = CHILD_EVENT_MASK;
849 xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
852 if (focused->window != NULL &&
853 focused->window->needs_take_focus) {
854 send_take_focus(to_focus);
857 ewmh_update_active_window(to_focus);
858 focused_id = to_focus;
862 if (focused_id == XCB_NONE) {
863 DLOG("Still no window focused, better set focus to the root window\n");
864 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
869 xcb_warp_pointer_rect(conn, warp_to);
874 DLOG("\n\n ENDING CHANGES\n\n");
876 /* Disable EnterWindow events for windows which will be unmapped in
877 * x_push_node_unmaps() now. Unmapping windows happens when switching
878 * workspaces. We want to avoid getting EnterNotifies during that phase
879 * because they would screw up our focus. One of these cases is having a
880 * stack with two windows. If the first window is focused and gets
881 * unmapped, the second one appears under the cursor and therefore gets an
882 * EnterNotify event. */
883 values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
884 CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
885 if (!state->unmap_now)
887 xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
890 /* Push all pending unmaps */
891 x_push_node_unmaps(con);
893 /* save the current stack as old stack */
894 CIRCLEQ_FOREACH(state, &state_head, state) {
895 CIRCLEQ_REMOVE(&old_state_head, state, old_state);
896 CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
898 //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
899 // DLOG("old stack: 0x%08x\n", state->id);
906 * Raises the specified container in the internal stack of X windows. The
907 * next call to x_push_changes() will make the change visible in X11.
910 void x_raise_con(Con *con) {
912 state = state_for_frame(con->frame);
913 //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
915 CIRCLEQ_REMOVE(&state_head, state, state);
916 CIRCLEQ_INSERT_HEAD(&state_head, state, state);
920 * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
921 * of the given name. Used for properly tagging the windows for easily spotting
922 * i3 windows in xwininfo -root -all.
925 void x_set_name(Con *con, const char *name) {
926 struct con_state *state;
928 if ((state = state_for_frame(con->frame)) == NULL) {
929 ELOG("window state not found\n");
934 state->name = sstrdup(name);
938 * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
941 void x_set_i3_atoms() {
942 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
943 (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
945 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
946 strlen(current_configpath), current_configpath);
950 * Set warp_to coordinates. This will trigger on the next call to
954 void x_set_warp_to(Rect *rect)