i3
src/x.c
Go to the documentation of this file.
00001 /*
00002  * vim:ts=4:sw=4:expandtab
00003  *
00004  * i3 - an improved dynamic tiling window manager
00005  * © 2009-2012 Michael Stapelberg and contributors (see also: LICENSE)
00006  *
00007  * x.c: Interface to X11, transfers our in-memory state to X11 (see also
00008  *      render.c). Basically a big state machine.
00009  *
00010  */
00011 #include "all.h"
00012 
00013 /* Stores the X11 window ID of the currently focused window */
00014 xcb_window_t focused_id = XCB_NONE;
00015 
00016 /* The bottom-to-top window stack of all windows which are managed by i3.
00017  * Used for x_get_window_stack(). */
00018 static xcb_window_t *btt_stack;
00019 static int btt_stack_num;
00020 
00021 /* Stores coordinates to warp mouse pointer to if set */
00022 static Rect *warp_to;
00023 
00024 /*
00025  * Describes the X11 state we may modify (map state, position, window stack).
00026  * There is one entry per container. The state represents the current situation
00027  * as X11 sees it (with the exception of the order in the state_head CIRCLEQ,
00028  * which represents the order that will be pushed to X11, while old_state_head
00029  * represents the current order). It will be updated in x_push_changes().
00030  *
00031  */
00032 typedef struct con_state {
00033     xcb_window_t id;
00034     bool mapped;
00035     bool unmap_now;
00036     bool child_mapped;
00037 
00039     Con *con;
00040 
00041     /* For reparenting, we have a flag (need_reparent) and the X ID of the old
00042      * frame this window was in. The latter is necessary because we need to
00043      * ignore UnmapNotify events (by changing the window event mask). */
00044     bool need_reparent;
00045     xcb_window_t old_frame;
00046 
00047     Rect rect;
00048     Rect window_rect;
00049 
00050     bool initial;
00051 
00052     char *name;
00053 
00054     CIRCLEQ_ENTRY(con_state) state;
00055     CIRCLEQ_ENTRY(con_state) old_state;
00056 } con_state;
00057 
00058 CIRCLEQ_HEAD(state_head, con_state) state_head =
00059     CIRCLEQ_HEAD_INITIALIZER(state_head);
00060 
00061 CIRCLEQ_HEAD(old_state_head, con_state) old_state_head =
00062     CIRCLEQ_HEAD_INITIALIZER(old_state_head);
00063 
00064 /*
00065  * Returns the container state for the given frame. This function always
00066  * returns a container state (otherwise, there is a bug in the code and the
00067  * container state of a container for which x_con_init() was not called was
00068  * requested).
00069  *
00070  */
00071 static con_state *state_for_frame(xcb_window_t window) {
00072     con_state *state;
00073     CIRCLEQ_FOREACH(state, &state_head, state)
00074         if (state->id == window)
00075             return state;
00076 
00077     /* TODO: better error handling? */
00078     ELOG("No state found\n");
00079     assert(false);
00080     return NULL;
00081 }
00082 
00083 /*
00084  * Initializes the X11 part for the given container. Called exactly once for
00085  * every container from con_new().
00086  *
00087  */
00088 void x_con_init(Con *con) {
00089     /* TODO: maybe create the window when rendering first? we could then even
00090      * get the initial geometry right */
00091 
00092     uint32_t mask = 0;
00093     uint32_t values[2];
00094 
00095     /* our own frames should not be managed */
00096     mask |= XCB_CW_OVERRIDE_REDIRECT;
00097     values[0] = 1;
00098 
00099     /* see include/xcb.h for the FRAME_EVENT_MASK */
00100     mask |= XCB_CW_EVENT_MASK;
00101     values[1] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
00102 
00103     Rect dims = { -15, -15, 10, 10 };
00104     con->frame = create_window(conn, dims, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCURSOR_CURSOR_POINTER, false, mask, values);
00105 
00106     struct con_state *state = scalloc(sizeof(struct con_state));
00107     state->id = con->frame;
00108     state->mapped = false;
00109     state->initial = true;
00110     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
00111     CIRCLEQ_INSERT_HEAD(&old_state_head, state, old_state);
00112     DLOG("adding new state for window id 0x%08x\n", state->id);
00113 }
00114 
00115 /*
00116  * Re-initializes the associated X window state for this container. You have
00117  * to call this when you assign a client to an empty container to ensure that
00118  * its state gets updated correctly.
00119  *
00120  */
00121 void x_reinit(Con *con) {
00122     struct con_state *state;
00123 
00124     if ((state = state_for_frame(con->frame)) == NULL) {
00125         ELOG("window state not found\n");
00126         return;
00127     }
00128 
00129     DLOG("resetting state %p to initial\n", state);
00130     state->initial = true;
00131     state->child_mapped = false;
00132     state->con = con;
00133     memset(&(state->window_rect), 0, sizeof(Rect));
00134 }
00135 
00136 /*
00137  * Reparents the child window of the given container (necessary for sticky
00138  * containers). The reparenting happens in the next call of x_push_changes().
00139  *
00140  */
00141 void x_reparent_child(Con *con, Con *old) {
00142     struct con_state *state;
00143     if ((state = state_for_frame(con->frame)) == NULL) {
00144         ELOG("window state for con not found\n");
00145         return;
00146     }
00147 
00148     state->need_reparent = true;
00149     state->old_frame = old->frame;
00150 }
00151 
00152 /*
00153  * Moves a child window from Container src to Container dest.
00154  *
00155  */
00156 void x_move_win(Con *src, Con *dest) {
00157     struct con_state *state_src, *state_dest;
00158 
00159     if ((state_src = state_for_frame(src->frame)) == NULL) {
00160         ELOG("window state for src not found\n");
00161         return;
00162     }
00163 
00164     if ((state_dest = state_for_frame(dest->frame)) == NULL) {
00165         ELOG("window state for dest not found\n");
00166         return;
00167     }
00168 
00169     state_dest->con = state_src->con;
00170     state_src->con = NULL;
00171 
00172     Rect zero = { 0, 0, 0, 0 };
00173     if (memcmp(&(state_dest->window_rect), &(zero), sizeof(Rect)) == 0) {
00174         memcpy(&(state_dest->window_rect), &(state_src->window_rect), sizeof(Rect));
00175         DLOG("COPYING RECT\n");
00176     }
00177 }
00178 
00179 /*
00180  * Kills the window decoration associated with the given container.
00181  *
00182  */
00183 void x_con_kill(Con *con) {
00184     con_state *state;
00185 
00186     xcb_destroy_window(conn, con->frame);
00187     xcb_free_pixmap(conn, con->pixmap);
00188     xcb_free_gc(conn, con->pm_gc);
00189     state = state_for_frame(con->frame);
00190     CIRCLEQ_REMOVE(&state_head, state, state);
00191     CIRCLEQ_REMOVE(&old_state_head, state, old_state);
00192     FREE(state->name);
00193     free(state);
00194 
00195     /* Invalidate focused_id to correctly focus new windows with the same ID */
00196     focused_id = XCB_NONE;
00197 }
00198 
00199 /*
00200  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
00201  *
00202  */
00203 bool window_supports_protocol(xcb_window_t window, xcb_atom_t atom) {
00204     xcb_get_property_cookie_t cookie;
00205     xcb_icccm_get_wm_protocols_reply_t protocols;
00206     bool result = false;
00207 
00208     cookie = xcb_icccm_get_wm_protocols(conn, window, A_WM_PROTOCOLS);
00209     if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
00210         return false;
00211 
00212     /* Check if the client’s protocols have the requested atom set */
00213     for (uint32_t i = 0; i < protocols.atoms_len; i++)
00214         if (protocols.atoms[i] == atom)
00215             result = true;
00216 
00217     xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
00218 
00219     return result;
00220 }
00221 
00222 /*
00223  * Kills the given X11 window using WM_DELETE_WINDOW (if supported).
00224  *
00225  */
00226 void x_window_kill(xcb_window_t window, kill_window_t kill_window) {
00227     /* if this window does not support WM_DELETE_WINDOW, we kill it the hard way */
00228     if (!window_supports_protocol(window, A_WM_DELETE_WINDOW)) {
00229         if (kill_window == KILL_WINDOW) {
00230             LOG("Killing specific window 0x%08x\n", window);
00231             xcb_destroy_window(conn, window);
00232         } else {
00233             LOG("Killing the X11 client which owns window 0x%08x\n", window);
00234             xcb_kill_client(conn, window);
00235         }
00236         return;
00237     }
00238 
00239     /* Every X11 event is 32 bytes long. Therefore, XCB will copy 32 bytes.
00240      * In order to properly initialize these bytes, we allocate 32 bytes even
00241      * though we only need less for an xcb_configure_notify_event_t */
00242     void *event = scalloc(32);
00243     xcb_client_message_event_t *ev = event;
00244 
00245     ev->response_type = XCB_CLIENT_MESSAGE;
00246     ev->window = window;
00247     ev->type = A_WM_PROTOCOLS;
00248     ev->format = 32;
00249     ev->data.data32[0] = A_WM_DELETE_WINDOW;
00250     ev->data.data32[1] = XCB_CURRENT_TIME;
00251 
00252     LOG("Sending WM_DELETE to the client\n");
00253     xcb_send_event(conn, false, window, XCB_EVENT_MASK_NO_EVENT, (char*)ev);
00254     xcb_flush(conn);
00255     free(event);
00256 }
00257 
00258 /*
00259  * Draws the decoration of the given container onto its parent.
00260  *
00261  */
00262 void x_draw_decoration(Con *con) {
00263     Con *parent = con->parent;
00264     bool leaf = con_is_leaf(con);
00265     /* This code needs to run for:
00266      *  • leaf containers
00267      *  • non-leaf containers which are in a stacked/tabbed container
00268      *
00269      * It does not need to run for:
00270      *  • floating containers (they don’t have a decoration)
00271      */
00272     if ((!leaf &&
00273          parent->layout != L_STACKED &&
00274          parent->layout != L_TABBED) ||
00275         con->type == CT_FLOATING_CON)
00276         return;
00277 
00278     /* Skip containers whose height is 0 (for example empty dockareas) */
00279     if (con->rect.height == 0)
00280         return;
00281 
00282     /* Skip containers whose pixmap has not yet been created (can happen when
00283      * decoration rendering happens recursively for a window for which
00284      * x_push_node() was not yet called) */
00285     if (leaf && con->pixmap == XCB_NONE)
00286         return;
00287 
00288     /* 1: build deco_params and compare with cache */
00289     struct deco_render_params *p = scalloc(sizeof(struct deco_render_params));
00290 
00291     /* find out which colors to use */
00292     if (con->urgent)
00293         p->color = &config.client.urgent;
00294     else if (con == focused || con_inside_focused(con))
00295         p->color = &config.client.focused;
00296     else if (con == TAILQ_FIRST(&(parent->focus_head)))
00297         p->color = &config.client.focused_inactive;
00298     else
00299         p->color = &config.client.unfocused;
00300 
00301     p->border_style = con_border_style(con);
00302 
00303     Rect *r = &(con->rect);
00304     Rect *w = &(con->window_rect);
00305     p->con_rect = (struct width_height){ r->width, r->height };
00306     p->con_window_rect = (struct width_height){ w->width, w->height };
00307     p->con_deco_rect = con->deco_rect;
00308     p->background = config.client.background;
00309     p->con_is_leaf = con_is_leaf(con);
00310     p->font = config.font.id;
00311 
00312     if (con->deco_render_params != NULL &&
00313         (con->window == NULL || !con->window->name_x_changed) &&
00314         !parent->pixmap_recreated &&
00315         !con->pixmap_recreated &&
00316         memcmp(p, con->deco_render_params, sizeof(struct deco_render_params)) == 0) {
00317         free(p);
00318         goto copy_pixmaps;
00319     }
00320 
00321     Con *next = con;
00322     while ((next = TAILQ_NEXT(next, nodes))) {
00323         FREE(next->deco_render_params);
00324     }
00325 
00326     FREE(con->deco_render_params);
00327     con->deco_render_params = p;
00328 
00329     if (con->window != NULL && con->window->name_x_changed)
00330         con->window->name_x_changed = false;
00331 
00332     parent->pixmap_recreated = false;
00333     con->pixmap_recreated = false;
00334 
00335     /* 2: draw the client.background, but only for the parts around the client_rect */
00336     if (con->window != NULL) {
00337         xcb_rectangle_t background[] = {
00338             /* top area */
00339             { 0, 0, r->width, w->y },
00340             /* bottom area */
00341             { 0, (w->y + w->height), r->width, r->height - (w->y + w->height) },
00342             /* left area */
00343             { 0, 0, w->x, r->height },
00344             /* right area */
00345             { w->x + w->width, 0, r->width - (w->x + w->width), r->height }
00346         };
00347 #if 0
00348         for (int i = 0; i < 4; i++)
00349             DLOG("rect is (%d, %d) with %d x %d\n",
00350                     background[i].x,
00351                     background[i].y,
00352                     background[i].width,
00353                     background[i].height
00354                 );
00355 #endif
00356 
00357         xcb_change_gc(conn, con->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]) { config.client.background });
00358         xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, sizeof(background) / sizeof(xcb_rectangle_t), background);
00359     }
00360 
00361     /* 3: draw a rectangle in border color around the client */
00362     if (p->border_style != BS_NONE && p->con_is_leaf) {
00363         Rect br = con_border_style_rect(con);
00364 #if 0
00365         DLOG("con->rect spans %d x %d\n", con->rect.width, con->rect.height);
00366         DLOG("border_rect spans (%d, %d) with %d x %d\n", br.x, br.y, br.width, br.height);
00367         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);
00368 #endif
00369 
00370         /* These rectangles represents the border around the child window
00371          * (left, bottom and right part). We don’t just fill the whole
00372          * rectangle because some childs are not freely resizable and we want
00373          * their background color to "shine through". */
00374         xcb_change_gc(conn, con->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){ p->color->background });
00375         xcb_rectangle_t borders[] = {
00376             { 0, 0, br.x, r->height },
00377             { 0, r->height + br.height + br.y, r->width, r->height },
00378             { r->width + br.width + br.x, 0, r->width, r->height }
00379         };
00380         xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 3, borders);
00381         /* 1pixel border needs an additional line at the top */
00382         if (p->border_style == BS_1PIXEL) {
00383             xcb_rectangle_t topline = { br.x, 0, con->rect.width + br.width + br.x, br.y };
00384             xcb_poly_fill_rectangle(conn, con->pixmap, con->pm_gc, 1, &topline);
00385         }
00386     }
00387 
00388     /* if this is a borderless/1pixel window, we don’t * need to render the
00389      * decoration. */
00390     if (p->border_style != BS_NORMAL)
00391         goto copy_pixmaps;
00392 
00393     /* 4: paint the bar */
00394     xcb_change_gc(conn, parent->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){ p->color->background });
00395     xcb_rectangle_t drect = { con->deco_rect.x, con->deco_rect.y, con->deco_rect.width, con->deco_rect.height };
00396     xcb_poly_fill_rectangle(conn, parent->pixmap, parent->pm_gc, 1, &drect);
00397 
00398     /* 5: draw two unconnected lines in border color */
00399     xcb_change_gc(conn, parent->pm_gc, XCB_GC_FOREGROUND, (uint32_t[]){ p->color->border });
00400     Rect *dr = &(con->deco_rect);
00401     xcb_segment_t segments[] = {
00402         { dr->x,                 dr->y,
00403           dr->x + dr->width - 1, dr->y },
00404 
00405         { dr->x + 2,             dr->y + dr->height - 1,
00406           dr->x + dr->width - 3, dr->y + dr->height - 1 }
00407     };
00408     xcb_poly_segment(conn, parent->pixmap, parent->pm_gc, 2, segments);
00409 
00410     /* 6: draw the title */
00411     uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
00412     uint32_t values[] = { p->color->text, p->color->background, config.font.id };
00413     xcb_change_gc(conn, parent->pm_gc, mask, values);
00414     int text_offset_y = config.font.height + (con->deco_rect.height - config.font.height) / 2 - 1;
00415 
00416     struct Window *win = con->window;
00417     if (win == NULL || win->name_x == NULL) {
00418         /* this is a non-leaf container, we need to make up a good description */
00419         // TODO: use a good description instead of just "another container"
00420         xcb_image_text_8(
00421             conn,
00422             strlen("another container"),
00423             parent->pixmap,
00424             parent->pm_gc,
00425             con->deco_rect.x + 2,
00426             con->deco_rect.y + text_offset_y,
00427             "another container"
00428         );
00429 
00430         goto copy_pixmaps;
00431     }
00432 
00433     int indent_level = 0,
00434         indent_mult = 0;
00435     Con *il_parent = parent;
00436     if (il_parent->layout != L_STACKED) {
00437         while (1) {
00438             //DLOG("il_parent = %p, layout = %d\n", il_parent, il_parent->layout);
00439             if (il_parent->layout == L_STACKED)
00440                 indent_level++;
00441             if (il_parent->type == CT_WORKSPACE || il_parent->type == CT_DOCKAREA || il_parent->type == CT_OUTPUT)
00442                 break;
00443             il_parent = il_parent->parent;
00444             indent_mult++;
00445         }
00446     }
00447     //DLOG("indent_level = %d, indent_mult = %d\n", indent_level, indent_mult);
00448     int indent_px = (indent_level * 5) * indent_mult;
00449 
00450     if (win->uses_net_wm_name)
00451         xcb_image_text_16(
00452             conn,
00453             win->name_len,
00454             parent->pixmap,
00455             parent->pm_gc,
00456             con->deco_rect.x + 2 + indent_px,
00457             con->deco_rect.y + text_offset_y,
00458             (xcb_char2b_t*)win->name_x
00459         );
00460     else
00461         xcb_image_text_8(
00462             conn,
00463             win->name_len,
00464             parent->pixmap,
00465             parent->pm_gc,
00466             con->deco_rect.x + 2 + indent_px,
00467             con->deco_rect.y + text_offset_y,
00468             win->name_x
00469         );
00470 
00471 copy_pixmaps:
00472     xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
00473 }
00474 
00475 /*
00476  * Recursively calls x_draw_decoration. This cannot be done in x_push_node
00477  * because x_push_node uses focus order to recurse (see the comment above)
00478  * while drawing the decoration needs to happen in the actual order.
00479  *
00480  */
00481 void x_deco_recurse(Con *con) {
00482     Con *current;
00483     bool leaf = TAILQ_EMPTY(&(con->nodes_head)) &&
00484                 TAILQ_EMPTY(&(con->floating_head));
00485     con_state *state = state_for_frame(con->frame);
00486 
00487     if (!leaf) {
00488         TAILQ_FOREACH(current, &(con->nodes_head), nodes)
00489             x_deco_recurse(current);
00490 
00491         TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
00492             x_deco_recurse(current);
00493 
00494         if (state->mapped)
00495             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
00496     }
00497 
00498     if ((con->type != CT_ROOT && con->type != CT_OUTPUT) &&
00499         (!leaf || con->mapped))
00500         x_draw_decoration(con);
00501 }
00502 
00503 /*
00504  * This function pushes the properties of each node of the layout tree to
00505  * X11 if they have changed (like the map state, position of the window, …).
00506  * It recursively traverses all children of the given node.
00507  *
00508  */
00509 void x_push_node(Con *con) {
00510     Con *current;
00511     con_state *state;
00512     Rect rect = con->rect;
00513 
00514     //DLOG("Pushing changes for node %p / %s\n", con, con->name);
00515     state = state_for_frame(con->frame);
00516 
00517     if (state->name != NULL) {
00518         DLOG("pushing name %s for con %p\n", state->name, con);
00519 
00520         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->frame,
00521                             XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, strlen(state->name), state->name);
00522         FREE(state->name);
00523     }
00524 
00525     if (con->window == NULL) {
00526         /* Calculate the height of all window decorations which will be drawn on to
00527          * this frame. */
00528         uint32_t max_y = 0, max_height = 0;
00529         TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
00530             Rect *dr = &(current->deco_rect);
00531             if (dr->y >= max_y && dr->height >= max_height) {
00532                 max_y = dr->y;
00533                 max_height = dr->height;
00534             }
00535         }
00536         rect.height = max_y + max_height;
00537         if (rect.height == 0)
00538             con->mapped = false;
00539     }
00540 
00541     /* reparent the child window (when the window was moved due to a sticky
00542      * container) */
00543     if (state->need_reparent && con->window != NULL) {
00544         DLOG("Reparenting child window\n");
00545 
00546         /* Temporarily set the event masks to XCB_NONE so that we won’t get
00547          * UnmapNotify events (otherwise the handler would close the container).
00548          * These events are generated automatically when reparenting. */
00549         uint32_t values[] = { XCB_NONE };
00550         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
00551         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
00552 
00553         xcb_reparent_window(conn, con->window->id, con->frame, 0, 0);
00554 
00555         values[0] = FRAME_EVENT_MASK;
00556         xcb_change_window_attributes(conn, state->old_frame, XCB_CW_EVENT_MASK, values);
00557         values[0] = CHILD_EVENT_MASK;
00558         xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
00559 
00560         state->old_frame = XCB_NONE;
00561         state->need_reparent = false;
00562 
00563         con->ignore_unmap++;
00564         DLOG("ignore_unmap for reparenting of con %p (win 0x%08x) is now %d\n",
00565                 con, con->window->id, con->ignore_unmap);
00566     }
00567 
00568     bool fake_notify = false;
00569     /* Set new position if rect changed (and if height > 0) */
00570     if (memcmp(&(state->rect), &rect, sizeof(Rect)) != 0 &&
00571         rect.height > 0) {
00572         /* We first create the new pixmap, then render to it, set it as the
00573          * background and only afterwards change the window size. This reduces
00574          * flickering. */
00575 
00576         /* As the pixmap only depends on the size and not on the position, it
00577          * is enough to check if width/height have changed. Also, we don’t
00578          * create a pixmap at all when the window is actually not visible
00579          * (height == 0). */
00580         if ((state->rect.width != rect.width ||
00581             state->rect.height != rect.height)) {
00582             if (con->pixmap == 0) {
00583                 con->pixmap = xcb_generate_id(conn);
00584                 con->pm_gc = xcb_generate_id(conn);
00585             } else {
00586                 xcb_free_pixmap(conn, con->pixmap);
00587                 xcb_free_gc(conn, con->pm_gc);
00588             }
00589             xcb_create_pixmap(conn, root_depth, con->pixmap, con->frame, rect.width, rect.height);
00590             /* For the graphics context, we disable GraphicsExposure events.
00591              * Those will be sent when a CopyArea request cannot be fulfilled
00592              * properly due to parts of the source being unmapped or otherwise
00593              * unavailable. Since we always copy from pixmaps to windows, this
00594              * is not a concern for us. */
00595             uint32_t values[] = { 0 };
00596             xcb_create_gc(conn, con->pm_gc, con->pixmap, XCB_GC_GRAPHICS_EXPOSURES, values);
00597 
00598             con->pixmap_recreated = true;
00599 
00600             /* Don’t render the decoration for windows inside a stack which are
00601              * not visible right now */
00602             if (!con->parent ||
00603                 con->parent->layout != L_STACKED ||
00604                 TAILQ_FIRST(&(con->parent->focus_head)) == con)
00605                 /* Render the decoration now to make the correct decoration visible
00606                  * from the very first moment. Later calls will be cached, so this
00607                  * doesn’t hurt performance. */
00608                 x_deco_recurse(con);
00609         }
00610 
00611         DLOG("setting rect (%d, %d, %d, %d)\n", rect.x, rect.y, rect.width, rect.height);
00612         /* flush to ensure that the following commands are sent in a single
00613          * buffer and will be processed directly afterwards (the contents of a
00614          * window get lost when resizing it, therefore we want to provide it as
00615          * fast as possible) */
00616         xcb_flush(conn);
00617         xcb_set_window_rect(conn, con->frame, rect);
00618         if (con->pixmap != XCB_NONE)
00619             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
00620         xcb_flush(conn);
00621 
00622         memcpy(&(state->rect), &rect, sizeof(Rect));
00623         fake_notify = true;
00624     }
00625 
00626     /* dito, but for child windows */
00627     if (con->window != NULL &&
00628         memcmp(&(state->window_rect), &(con->window_rect), sizeof(Rect)) != 0) {
00629         DLOG("setting window rect (%d, %d, %d, %d)\n",
00630             con->window_rect.x, con->window_rect.y, con->window_rect.width, con->window_rect.height);
00631         xcb_set_window_rect(conn, con->window->id, con->window_rect);
00632         memcpy(&(state->window_rect), &(con->window_rect), sizeof(Rect));
00633         fake_notify = true;
00634     }
00635 
00636     /* Map if map state changed, also ensure that the child window
00637      * is changed if we are mapped *and* in initial state (meaning the
00638      * container was empty before, but now got a child). Unmaps are handled in
00639      * x_push_node_unmaps(). */
00640     if ((state->mapped != con->mapped || (con->mapped && state->initial)) &&
00641         con->mapped) {
00642         xcb_void_cookie_t cookie;
00643 
00644         if (con->window != NULL) {
00645             /* Set WM_STATE_NORMAL because GTK applications don’t want to
00646              * drag & drop if we don’t. Also, xprop(1) needs it. */
00647             long data[] = { XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE };
00648             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
00649                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
00650         }
00651 
00652         uint32_t values[1];
00653         if (!state->child_mapped && con->window != NULL) {
00654             cookie = xcb_map_window(conn, con->window->id);
00655 
00656             /* We are interested in EnterNotifys as soon as the window is
00657              * mapped */
00658             values[0] = CHILD_EVENT_MASK;
00659             xcb_change_window_attributes(conn, con->window->id, XCB_CW_EVENT_MASK, values);
00660             DLOG("mapping child window (serial %d)\n", cookie.sequence);
00661             state->child_mapped = true;
00662         }
00663 
00664         cookie = xcb_map_window(conn, con->frame);
00665 
00666         values[0] = FRAME_EVENT_MASK;
00667         xcb_change_window_attributes(conn, con->frame, XCB_CW_EVENT_MASK, values);
00668 
00669         /* copy the pixmap contents to the frame window immediately after mapping */
00670         if (con->pixmap != XCB_NONE)
00671             xcb_copy_area(conn, con->pixmap, con->frame, con->pm_gc, 0, 0, 0, 0, con->rect.width, con->rect.height);
00672         xcb_flush(conn);
00673 
00674         DLOG("mapping container %08x (serial %d)\n", con->frame, cookie.sequence);
00675         state->mapped = con->mapped;
00676     }
00677 
00678     state->unmap_now = (state->mapped != con->mapped) && !con->mapped;
00679 
00680     if (fake_notify) {
00681         DLOG("Sending fake configure notify\n");
00682         fake_absolute_configure_notify(con);
00683     }
00684 
00685     /* Handle all children and floating windows of this node. We recurse
00686      * in focus order to display the focused client in a stack first when
00687      * switching workspaces (reduces flickering). */
00688     TAILQ_FOREACH(current, &(con->focus_head), focused)
00689         x_push_node(current);
00690 }
00691 
00692 /*
00693  * Same idea as in x_push_node(), but this function only unmaps windows. It is
00694  * necessary to split this up to handle new fullscreen clients properly: The
00695  * new window needs to be mapped and focus needs to be set *before* the
00696  * underlying windows are unmapped. Otherwise, focus will revert to the
00697  * PointerRoot and will then be set to the new window, generating unnecessary
00698  * FocusIn/FocusOut events.
00699  *
00700  */
00701 static void x_push_node_unmaps(Con *con) {
00702     Con *current;
00703     con_state *state;
00704 
00705     //DLOG("Pushing changes (with unmaps) for node %p / %s\n", con, con->name);
00706     state = state_for_frame(con->frame);
00707 
00708     /* map/unmap if map state changed, also ensure that the child window
00709      * is changed if we are mapped *and* in initial state (meaning the
00710      * container was empty before, but now got a child) */
00711     if (state->unmap_now) {
00712         xcb_void_cookie_t cookie;
00713         if (con->window != NULL) {
00714             /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
00715             long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
00716             xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
00717                                 A_WM_STATE, A_WM_STATE, 32, 2, data);
00718         }
00719 
00720         cookie = xcb_unmap_window(conn, con->frame);
00721         DLOG("unmapping container (serial %d)\n", cookie.sequence);
00722         /* we need to increase ignore_unmap for this container (if it
00723          * contains a window) and for every window "under" this one which
00724          * contains a window */
00725         if (con->window != NULL) {
00726             con->ignore_unmap++;
00727             DLOG("ignore_unmap for con %p (frame 0x%08x) now %d\n", con, con->frame, con->ignore_unmap);
00728         }
00729         state->mapped = con->mapped;
00730     }
00731 
00732     /* handle all children and floating windows of this node */
00733     TAILQ_FOREACH(current, &(con->nodes_head), nodes)
00734         x_push_node_unmaps(current);
00735 
00736     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
00737         x_push_node_unmaps(current);
00738 }
00739 
00740 /*
00741  * Pushes all changes (state of each node, see x_push_node() and the window
00742  * stack) to X11.
00743  *
00744  * NOTE: We need to push the stack first so that the windows have the correct
00745  * stacking order. This is relevant for workspace switching where we map the
00746  * windows because mapping may generate EnterNotify events. When they are
00747  * generated in the wrong order, this will cause focus problems when switching
00748  * workspaces.
00749  *
00750  */
00751 void x_push_changes(Con *con) {
00752     con_state *state;
00753     xcb_query_pointer_cookie_t pointercookie;
00754 
00755     /* If we need to warp later, we request the pointer position as soon as possible */
00756     if (warp_to) {
00757         pointercookie = xcb_query_pointer(conn, root);
00758     }
00759 
00760     DLOG("-- PUSHING WINDOW STACK --\n");
00761     //DLOG("Disabling EnterNotify\n");
00762     uint32_t values[1] = { XCB_NONE };
00763     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
00764         if (state->mapped)
00765             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
00766     }
00767     //DLOG("Done, EnterNotify disabled\n");
00768     bool order_changed = false;
00769     bool stacking_changed = false;
00770 
00771     /* count first, necessary to (re)allocate memory for the bottom-to-top
00772      * stack afterwards */
00773     int cnt = 0;
00774     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state)
00775         if (state->con && state->con->window)
00776             cnt++;
00777 
00778     if (cnt != btt_stack_num) {
00779         btt_stack = srealloc(btt_stack, sizeof(xcb_window_t) * cnt);
00780         btt_stack_num = cnt;
00781     }
00782 
00783     xcb_window_t *walk = btt_stack;
00784 
00785     /* X11 correctly represents the stack if we push it from bottom to top */
00786     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
00787         if (state->con && state->con->window)
00788             memcpy(walk++, &(state->con->window->id), sizeof(xcb_window_t));
00789 
00790         //DLOG("stack: 0x%08x\n", state->id);
00791         con_state *prev = CIRCLEQ_PREV(state, state);
00792         con_state *old_prev = CIRCLEQ_PREV(state, old_state);
00793         if (prev != old_prev)
00794             order_changed = true;
00795         if ((state->initial || order_changed) && prev != CIRCLEQ_END(&state_head)) {
00796             stacking_changed = true;
00797             //DLOG("Stacking 0x%08x above 0x%08x\n", prev->id, state->id);
00798             uint32_t mask = 0;
00799             mask |= XCB_CONFIG_WINDOW_SIBLING;
00800             mask |= XCB_CONFIG_WINDOW_STACK_MODE;
00801             uint32_t values[] = {state->id, XCB_STACK_MODE_ABOVE};
00802 
00803             xcb_configure_window(conn, prev->id, mask, values);
00804         }
00805         state->initial = false;
00806     }
00807 
00808     /* If we re-stacked something (or a new window appeared), we need to update
00809      * the _NET_CLIENT_LIST_STACKING hint */
00810     if (stacking_changed)
00811         ewmh_update_client_list_stacking(btt_stack, btt_stack_num);
00812 
00813     DLOG("PUSHING CHANGES\n");
00814     x_push_node(con);
00815 
00816     if (warp_to) {
00817         xcb_query_pointer_reply_t *pointerreply = xcb_query_pointer_reply(conn, pointercookie, NULL);
00818         if (!pointerreply) {
00819             ELOG("Could not query pointer position, not warping pointer\n");
00820         } else {
00821             int mid_x = warp_to->x + (warp_to->width / 2);
00822             int mid_y = warp_to->y + (warp_to->height / 2);
00823 
00824             Output *current = get_output_containing(pointerreply->root_x, pointerreply->root_y);
00825             Output *target = get_output_containing(mid_x, mid_y);
00826             if (current != target)
00827                 xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0, mid_x, mid_y);
00828         }
00829         warp_to = NULL;
00830     }
00831 
00832     //DLOG("Re-enabling EnterNotify\n");
00833     values[0] = FRAME_EVENT_MASK;
00834     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
00835         if (state->mapped)
00836             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
00837     }
00838     //DLOG("Done, EnterNotify re-enabled\n");
00839 
00840     x_deco_recurse(con);
00841 
00842     xcb_window_t to_focus = focused->frame;
00843     if (focused->window != NULL)
00844         to_focus = focused->window->id;
00845 
00846     if (focused_id != to_focus) {
00847         if (!focused->mapped) {
00848             DLOG("Not updating focus (to %p / %s), focused window is not mapped.\n", focused, focused->name);
00849             /* Invalidate focused_id to correctly focus new windows with the same ID */
00850             focused_id = XCB_NONE;
00851         } else {
00852             bool set_focus = true;
00853             if (focused->window != NULL &&
00854                 focused->window->needs_take_focus) {
00855                 DLOG("Updating focus by sending WM_TAKE_FOCUS to window 0x%08x (focused: %p / %s)\n",
00856                      to_focus, focused, focused->name);
00857                 send_take_focus(to_focus);
00858                 set_focus = !focused->window->doesnt_accept_focus;
00859                 DLOG("set_focus = %d\n", set_focus);
00860             }
00861 
00862             if (set_focus) {
00863                 DLOG("Updating focus (focused: %p / %s)\n", focused, focused->name);
00864                 /* We remove XCB_EVENT_MASK_FOCUS_CHANGE from the event mask to get
00865                  * no focus change events for our own focus changes. We only want
00866                  * these generated by the clients. */
00867                 if (focused->window != NULL) {
00868                     values[0] = CHILD_EVENT_MASK & ~(XCB_EVENT_MASK_FOCUS_CHANGE);
00869                     xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
00870                 }
00871                 xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, to_focus, XCB_CURRENT_TIME);
00872                 if (focused->window != NULL) {
00873                     values[0] = CHILD_EVENT_MASK;
00874                     xcb_change_window_attributes(conn, focused->window->id, XCB_CW_EVENT_MASK, values);
00875                 }
00876 
00877                 ewmh_update_active_window(to_focus);
00878             }
00879 
00880             focused_id = to_focus;
00881         }
00882     }
00883 
00884     if (focused_id == XCB_NONE) {
00885         DLOG("Still no window focused, better set focus to the root window\n");
00886         xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
00887         focused_id = root;
00888     }
00889 
00890     xcb_flush(conn);
00891     DLOG("ENDING CHANGES\n");
00892 
00893     /* Disable EnterWindow events for windows which will be unmapped in
00894      * x_push_node_unmaps() now. Unmapping windows happens when switching
00895      * workspaces. We want to avoid getting EnterNotifies during that phase
00896      * because they would screw up our focus. One of these cases is having a
00897      * stack with two windows. If the first window is focused and gets
00898      * unmapped, the second one appears under the cursor and therefore gets an
00899      * EnterNotify event. */
00900     values[0] = FRAME_EVENT_MASK & ~XCB_EVENT_MASK_ENTER_WINDOW;
00901     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
00902         if (!state->unmap_now)
00903             continue;
00904         xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
00905     }
00906 
00907     /* Push all pending unmaps */
00908     x_push_node_unmaps(con);
00909 
00910     /* save the current stack as old stack */
00911     CIRCLEQ_FOREACH(state, &state_head, state) {
00912         CIRCLEQ_REMOVE(&old_state_head, state, old_state);
00913         CIRCLEQ_INSERT_TAIL(&old_state_head, state, old_state);
00914     }
00915     //CIRCLEQ_FOREACH(state, &old_state_head, old_state) {
00916     //    DLOG("old stack: 0x%08x\n", state->id);
00917     //}
00918 
00919     xcb_flush(conn);
00920 }
00921 
00922 /*
00923  * Raises the specified container in the internal stack of X windows. The
00924  * next call to x_push_changes() will make the change visible in X11.
00925  *
00926  */
00927 void x_raise_con(Con *con) {
00928     con_state *state;
00929     state = state_for_frame(con->frame);
00930     //DLOG("raising in new stack: %p / %s / %s / xid %08x\n", con, con->name, con->window ? con->window->name_json : "", state->id);
00931 
00932     CIRCLEQ_REMOVE(&state_head, state, state);
00933     CIRCLEQ_INSERT_HEAD(&state_head, state, state);
00934 }
00935 
00936 /*
00937  * Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways)
00938  * of the given name. Used for properly tagging the windows for easily spotting
00939  * i3 windows in xwininfo -root -all.
00940  *
00941  */
00942 void x_set_name(Con *con, const char *name) {
00943     struct con_state *state;
00944 
00945     if ((state = state_for_frame(con->frame)) == NULL) {
00946         ELOG("window state not found\n");
00947         return;
00948     }
00949 
00950     FREE(state->name);
00951     state->name = sstrdup(name);
00952 }
00953 
00954 /*
00955  * Sets up i3 specific atoms (I3_SOCKET_PATH and I3_CONFIG_PATH)
00956  *
00957  */
00958 void x_set_i3_atoms() {
00959     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_SOCKET_PATH, A_UTF8_STRING, 8,
00960                         (current_socketpath == NULL ? 0 : strlen(current_socketpath)),
00961                         current_socketpath);
00962     xcb_change_property(conn, XCB_PROP_MODE_REPLACE, root, A_I3_CONFIG_PATH, A_UTF8_STRING, 8,
00963                         strlen(current_configpath), current_configpath);
00964 }
00965 
00966 /*
00967  * Set warp_to coordinates.  This will trigger on the next call to
00968  * x_push_changes().
00969  *
00970  */
00971 void x_set_warp_to(Rect *rect)
00972 {
00973     warp_to = rect;
00974 }
00975 
00976 /*
00977  * Applies the given mask to the event mask of every i3 window decoration X11
00978  * window. This is useful to disable EnterNotify while resizing so that focus
00979  * is untouched.
00980  *
00981  */
00982 void x_mask_event_mask(uint32_t mask) {
00983     uint32_t values[] = { FRAME_EVENT_MASK & mask };
00984 
00985     con_state *state;
00986     CIRCLEQ_FOREACH_REVERSE(state, &state_head, state) {
00987         if (state->mapped)
00988             xcb_change_window_attributes(conn, state->id, XCB_CW_EVENT_MASK, values);
00989     }
00990 }