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