i3
|
00001 /* 00002 * vim:ts=4:sw=4:expandtab 00003 * 00004 * i3 - an improved dynamic tiling window manager 00005 * 00006 * © 2009-2010 Michael Stapelberg and contributors 00007 * 00008 * See file LICENSE for license information. 00009 * 00010 * src/floating.c: contains all functions for handling floating clients 00011 * 00012 */ 00013 00014 00015 #include "all.h" 00016 00017 extern xcb_connection_t *conn; 00018 00019 void floating_enable(Con *con, bool automatic) { 00020 bool set_focus = true; 00021 00022 if (con_is_floating(con)) { 00023 LOG("Container is already in floating mode, not doing anything.\n"); 00024 return; 00025 } 00026 00027 /* 1: If the container is a workspace container, we need to create a new 00028 * split-container with the same orientation and make that one floating. We 00029 * cannot touch the workspace container itself because floating containers 00030 * are children of the workspace. */ 00031 if (con->type == CT_WORKSPACE) { 00032 LOG("This is a workspace, creating new container around content\n"); 00033 if (con_num_children(con) == 0) { 00034 LOG("Workspace is empty, aborting\n"); 00035 return; 00036 } 00037 /* TODO: refactor this with src/con.c:con_set_layout */ 00038 Con *new = con_new(NULL, NULL); 00039 new->parent = con; 00040 new->orientation = con->orientation; 00041 00042 /* since the new container will be set into floating mode directly 00043 * afterwards, we need to copy the workspace rect. */ 00044 memcpy(&(new->rect), &(con->rect), sizeof(Rect)); 00045 00046 Con *old_focused = TAILQ_FIRST(&(con->focus_head)); 00047 if (old_focused == TAILQ_END(&(con->focus_head))) 00048 old_focused = NULL; 00049 00050 /* 4: move the existing cons of this workspace below the new con */ 00051 DLOG("Moving cons\n"); 00052 Con *child; 00053 while (!TAILQ_EMPTY(&(con->nodes_head))) { 00054 child = TAILQ_FIRST(&(con->nodes_head)); 00055 con_detach(child); 00056 con_attach(child, new, true); 00057 } 00058 00059 /* 4: attach the new split container to the workspace */ 00060 DLOG("Attaching new split to ws\n"); 00061 con_attach(new, con, false); 00062 00063 if (old_focused) 00064 con_focus(old_focused); 00065 00066 con = new; 00067 set_focus = false; 00068 } 00069 00070 /* 1: detach the container from its parent */ 00071 /* TODO: refactor this with tree_close() */ 00072 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes); 00073 TAILQ_REMOVE(&(con->parent->focus_head), con, focused); 00074 00075 con_fix_percent(con->parent); 00076 00077 /* 2: create a new container to render the decoration on, add 00078 * it as a floating window to the workspace */ 00079 Con *nc = con_new(NULL, NULL); 00080 /* we need to set the parent afterwards instead of passing it as an 00081 * argument to con_new() because nc would be inserted into the tiling layer 00082 * otherwise. */ 00083 nc->parent = con_get_workspace(con); 00084 00085 /* check if the parent container is empty and close it if so */ 00086 if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) && con_num_children(con->parent) == 0) { 00087 DLOG("Old container empty after setting this child to floating, closing\n"); 00088 tree_close(con->parent, DONT_KILL_WINDOW, false); 00089 } 00090 00091 char *name; 00092 asprintf(&name, "[i3 con] floatingcon around %p", con); 00093 x_set_name(nc, name); 00094 free(name); 00095 00096 /* find the height for the decorations */ 00097 int deco_height = config.font.height + 5; 00098 00099 DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height); 00100 Rect zero = { 0, 0, 0, 0 }; 00101 nc->rect = con->geometry; 00102 /* If the geometry was not set (split containers), we need to determine a 00103 * sensible one by combining the geometry of all children */ 00104 if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) { 00105 DLOG("Geometry not set, combining children\n"); 00106 Con *child; 00107 TAILQ_FOREACH(child, &(con->nodes_head), nodes) { 00108 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height); 00109 nc->rect.width += child->geometry.width; 00110 nc->rect.height = max(nc->rect.height, child->geometry.height); 00111 } 00112 } 00113 /* Raise the width/height to at least 75x50 (minimum size for windows) */ 00114 nc->rect.width = max(nc->rect.width, 75); 00115 nc->rect.height = max(nc->rect.height, 50); 00116 /* add pixels for the decoration */ 00117 /* TODO: don’t add them when the user automatically puts new windows into 00118 * 1pixel/borderless mode */ 00119 nc->rect.height += deco_height + 4; 00120 nc->rect.width += 4; 00121 DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height); 00122 nc->orientation = NO_ORIENTATION; 00123 nc->type = CT_FLOATING_CON; 00124 TAILQ_INSERT_TAIL(&(nc->parent->floating_head), nc, floating_windows); 00125 TAILQ_INSERT_TAIL(&(nc->parent->focus_head), nc, focused); 00126 00127 /* 3: attach the child to the new parent container */ 00128 con->parent = nc; 00129 con->percent = 1.0; 00130 con->floating = FLOATING_USER_ON; 00131 00132 /* Some clients (like GIMP’s color picker window) get mapped 00133 * to (0, 0), so we push them to a reasonable position 00134 * (centered over their leader) */ 00135 if (nc->rect.x == 0 && nc->rect.y == 0) { 00136 Con *leader; 00137 if (con->window && con->window->leader != XCB_NONE && 00138 (leader = con_by_window_id(con->window->leader)) != NULL) { 00139 DLOG("Centering above leader\n"); 00140 nc->rect.x = leader->rect.x + (leader->rect.width / 2) - (nc->rect.width / 2); 00141 nc->rect.y = leader->rect.y + (leader->rect.height / 2) - (nc->rect.height / 2); 00142 } else { 00143 /* center the window on workspace as fallback */ 00144 Con *ws = nc->parent; 00145 nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2); 00146 nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2); 00147 } 00148 } 00149 00150 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes); 00151 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused); 00152 00153 /* render the cons to get initial window_rect correct */ 00154 render_con(nc, false); 00155 render_con(con, false); 00156 00157 // TODO: don’t influence focus handling when Con was not focused before. 00158 if (set_focus) 00159 con_focus(con); 00160 00161 /* Check if we need to re-assign it to a different workspace because of its 00162 * coordinates and exit if that was done successfully. */ 00163 if (floating_maybe_reassign_ws(nc)) 00164 return; 00165 00166 /* Sanitize coordinates: Check if they are on any output */ 00167 if (get_output_containing(nc->rect.x, nc->rect.y) != NULL) 00168 return; 00169 00170 ELOG("No output found at destination coordinates, centering floating window on current ws\n"); 00171 Con *ws = nc->parent; 00172 nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2); 00173 nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2); 00174 } 00175 00176 void floating_disable(Con *con, bool automatic) { 00177 if (!con_is_floating(con)) { 00178 LOG("Container isn't floating, not doing anything.\n"); 00179 return; 00180 } 00181 00182 Con *ws = con_get_workspace(con); 00183 00184 /* 1: detach from parent container */ 00185 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes); 00186 TAILQ_REMOVE(&(con->parent->focus_head), con, focused); 00187 00188 /* 2: kill parent container */ 00189 TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows); 00190 TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused); 00191 tree_close(con->parent, DONT_KILL_WINDOW, false); 00192 00193 /* 3: re-attach to the parent of the currently focused con on the workspace 00194 * this floating con was on */ 00195 Con *focused = con_descend_tiling_focused(ws); 00196 00197 /* if there is no other container on this workspace, focused will be the 00198 * workspace itself */ 00199 if (focused->type == CT_WORKSPACE) 00200 con->parent = focused; 00201 else con->parent = focused->parent; 00202 00203 /* con_fix_percent will adjust the percent value */ 00204 con->percent = 0.0; 00205 00206 TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes); 00207 TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused); 00208 00209 con->floating = FLOATING_USER_OFF; 00210 00211 con_fix_percent(con->parent); 00212 // TODO: don’t influence focus handling when Con was not focused before. 00213 con_focus(con); 00214 } 00215 00216 /* 00217 * Toggles floating mode for the given container. 00218 * 00219 * If the automatic flag is set to true, this was an automatic update by a change of the 00220 * window class from the application which can be overwritten by the user. 00221 * 00222 */ 00223 void toggle_floating_mode(Con *con, bool automatic) { 00224 /* see if the client is already floating */ 00225 if (con_is_floating(con)) { 00226 LOG("already floating, re-setting to tiling\n"); 00227 00228 floating_disable(con, automatic); 00229 return; 00230 } 00231 00232 floating_enable(con, automatic); 00233 } 00234 00235 /* 00236 * Raises the given container in the list of floating containers 00237 * 00238 */ 00239 void floating_raise_con(Con *con) { 00240 DLOG("Raising floating con %p / %s\n", con, con->name); 00241 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows); 00242 TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows); 00243 } 00244 00245 /* 00246 * Checks if con’s coordinates are within its workspace and re-assigns it to 00247 * the actual workspace if not. 00248 * 00249 */ 00250 bool floating_maybe_reassign_ws(Con *con) { 00251 Output *output = get_output_containing( 00252 con->rect.x + (con->rect.width / 2), 00253 con->rect.y + (con->rect.height / 2)); 00254 00255 if (!output) { 00256 ELOG("No output found at destination coordinates?\n"); 00257 return false; 00258 } 00259 00260 if (con_get_output(con) == output->con) { 00261 DLOG("still the same ws\n"); 00262 return false; 00263 } 00264 00265 DLOG("Need to re-assign!\n"); 00266 00267 Con *content = output_get_content(output->con); 00268 Con *ws = TAILQ_FIRST(&(content->focus_head)); 00269 DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name); 00270 con_move_to_workspace(con, ws); 00271 con_focus(con_descend_focused(con)); 00272 return true; 00273 } 00274 00275 DRAGGING_CB(drag_window_callback) { 00276 struct xcb_button_press_event_t *event = extra; 00277 00278 /* Reposition the client correctly while moving */ 00279 con->rect.x = old_rect->x + (new_x - event->root_x); 00280 con->rect.y = old_rect->y + (new_y - event->root_y); 00281 00282 render_con(con, false); 00283 x_push_node(con); 00284 xcb_flush(conn); 00285 00286 /* Check if we cross workspace boundaries while moving */ 00287 if (!floating_maybe_reassign_ws(con)) 00288 return; 00289 tree_render(); 00290 } 00291 00292 /* 00293 * Called when the user clicked on the titlebar of a floating window. 00294 * Calls the drag_pointer function with the drag_window callback 00295 * 00296 */ 00297 void floating_drag_window(Con *con, xcb_button_press_event_t *event) { 00298 DLOG("floating_drag_window\n"); 00299 00300 drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback, event); 00301 tree_render(); 00302 } 00303 00304 /* 00305 * This is an ugly data structure which we need because there is no standard 00306 * way of having nested functions (only available as a gcc extension at the 00307 * moment, clang doesn’t support it) or blocks (only available as a clang 00308 * extension and only on Mac OS X systems at the moment). 00309 * 00310 */ 00311 struct resize_window_callback_params { 00312 border_t corner; 00313 bool proportional; 00314 xcb_button_press_event_t *event; 00315 }; 00316 00317 DRAGGING_CB(resize_window_callback) { 00318 struct resize_window_callback_params *params = extra; 00319 xcb_button_press_event_t *event = params->event; 00320 border_t corner = params->corner; 00321 00322 int32_t dest_x = con->rect.x; 00323 int32_t dest_y = con->rect.y; 00324 uint32_t dest_width; 00325 uint32_t dest_height; 00326 00327 double ratio = (double) old_rect->width / old_rect->height; 00328 00329 /* First guess: We resize by exactly the amount the mouse moved, 00330 * taking into account in which corner the client was grabbed */ 00331 if (corner & BORDER_LEFT) 00332 dest_width = old_rect->width - (new_x - event->root_x); 00333 else dest_width = old_rect->width + (new_x - event->root_x); 00334 00335 if (corner & BORDER_TOP) 00336 dest_height = old_rect->height - (new_y - event->root_y); 00337 else dest_height = old_rect->height + (new_y - event->root_y); 00338 00339 /* Obey minimum window size */ 00340 Rect minimum = con_minimum_size(con); 00341 dest_width = max(dest_width, minimum.width); 00342 dest_height = max(dest_height, minimum.height); 00343 00344 /* User wants to keep proportions, so we may have to adjust our values */ 00345 if (params->proportional) { 00346 dest_width = max(dest_width, (int) (dest_height * ratio)); 00347 dest_height = max(dest_height, (int) (dest_width / ratio)); 00348 } 00349 00350 /* If not the lower right corner is grabbed, we must also reposition 00351 * the client by exactly the amount we resized it */ 00352 if (corner & BORDER_LEFT) 00353 dest_x = old_rect->x + (old_rect->width - dest_width); 00354 00355 if (corner & BORDER_TOP) 00356 dest_y = old_rect->y + (old_rect->height - dest_height); 00357 00358 con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height }; 00359 00360 /* TODO: don’t re-render the whole tree just because we change 00361 * coordinates of a floating window */ 00362 tree_render(); 00363 x_push_changes(croot); 00364 } 00365 00366 /* 00367 * Called when the user clicked on a floating window while holding the 00368 * floating_modifier and the right mouse button. 00369 * Calls the drag_pointer function with the resize_window callback 00370 * 00371 */ 00372 void floating_resize_window(Con *con, bool proportional, 00373 xcb_button_press_event_t *event) { 00374 DLOG("floating_resize_window\n"); 00375 00376 /* corner saves the nearest corner to the original click. It contains 00377 * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */ 00378 border_t corner = 0; 00379 00380 if (event->event_x <= (con->rect.width / 2)) 00381 corner |= BORDER_LEFT; 00382 else corner |= BORDER_RIGHT; 00383 00384 if (event->event_y <= (con->rect.height / 2)) 00385 corner |= BORDER_TOP; 00386 else corner |= BORDER_BOTTOM; 00387 00388 struct resize_window_callback_params params = { corner, proportional, event }; 00389 00390 drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, ¶ms); 00391 } 00392 00393 /* 00394 * This function grabs your pointer and lets you drag stuff around (borders). 00395 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received 00396 * and the given callback will be called with the parameters specified (client, 00397 * border on which the click originally was), the original rect of the client, 00398 * the event and the new coordinates (x, y). 00399 * 00400 */ 00401 void drag_pointer(Con *con, xcb_button_press_event_t *event, xcb_window_t 00402 confine_to, border_t border, callback_t callback, void *extra) 00403 { 00404 uint32_t new_x, new_y; 00405 Rect old_rect; 00406 if (con != NULL) 00407 memcpy(&old_rect, &(con->rect), sizeof(Rect)); 00408 00409 /* Grab the pointer */ 00410 xcb_grab_pointer_cookie_t cookie; 00411 xcb_grab_pointer_reply_t *reply; 00412 cookie = xcb_grab_pointer(conn, 00413 false, /* get all pointer events specified by the following mask */ 00414 root, /* grab the root window */ 00415 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */ 00416 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */ 00417 XCB_GRAB_MODE_ASYNC, /* keyboard mode */ 00418 confine_to, /* confine_to = in which window should the cursor stay */ 00419 XCB_NONE, /* don’t display a special cursor */ 00420 XCB_CURRENT_TIME); 00421 00422 if ((reply = xcb_grab_pointer_reply(conn, cookie, NULL)) == NULL) { 00423 ELOG("Could not grab pointer\n"); 00424 return; 00425 } 00426 00427 free(reply); 00428 00429 /* Go into our own event loop */ 00430 xcb_flush(conn); 00431 00432 xcb_generic_event_t *inside_event, *last_motion_notify = NULL; 00433 bool loop_done = false; 00434 /* I’ve always wanted to have my own eventhandler… */ 00435 while (!loop_done && (inside_event = xcb_wait_for_event(conn))) { 00436 /* We now handle all events we can get using xcb_poll_for_event */ 00437 do { 00438 /* skip x11 errors */ 00439 if (inside_event->response_type == 0) { 00440 free(inside_event); 00441 continue; 00442 } 00443 /* Strip off the highest bit (set if the event is generated) */ 00444 int type = (inside_event->response_type & 0x7F); 00445 00446 switch (type) { 00447 case XCB_BUTTON_RELEASE: 00448 loop_done = true; 00449 break; 00450 00451 case XCB_MOTION_NOTIFY: 00452 /* motion_notify events are saved for later */ 00453 FREE(last_motion_notify); 00454 last_motion_notify = inside_event; 00455 break; 00456 00457 case XCB_UNMAP_NOTIFY: 00458 case XCB_KEY_PRESS: 00459 case XCB_KEY_RELEASE: 00460 DLOG("Unmap-notify, aborting\n"); 00461 handle_event(type, inside_event); 00462 loop_done = true; 00463 break; 00464 00465 default: 00466 DLOG("Passing to original handler\n"); 00467 /* Use original handler */ 00468 handle_event(type, inside_event); 00469 break; 00470 } 00471 if (last_motion_notify != inside_event) 00472 free(inside_event); 00473 } while ((inside_event = xcb_poll_for_event(conn)) != NULL); 00474 00475 if (last_motion_notify == NULL) 00476 continue; 00477 00478 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x; 00479 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y; 00480 00481 callback(con, &old_rect, new_x, new_y, extra); 00482 FREE(last_motion_notify); 00483 } 00484 00485 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME); 00486 xcb_flush(conn); 00487 } 00488 00489 #if 0 00490 /* 00491 * Changes focus in the given direction for floating clients. 00492 * 00493 * Changing to the left/right means going to the previous/next floating client, 00494 * changing to top/bottom means cycling through the Z-index. 00495 * 00496 */ 00497 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) { 00498 DLOG("floating focus\n"); 00499 00500 if (direction == D_LEFT || direction == D_RIGHT) { 00501 /* Go to the next/previous floating client */ 00502 Client *client; 00503 00504 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) : 00505 TAILQ_NEXT(currently_focused, floating_clients))) != 00506 TAILQ_END(&(currently_focused->workspace->floating_clients))) { 00507 if (!client->floating) 00508 continue; 00509 set_focus(conn, client, true); 00510 return; 00511 } 00512 } 00513 } 00514 00515 /* 00516 * Moves the client 10px to the specified direction. 00517 * 00518 */ 00519 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) { 00520 DLOG("floating move\n"); 00521 00522 Rect destination = currently_focused->rect; 00523 Rect *screen = &(currently_focused->workspace->output->rect); 00524 00525 switch (direction) { 00526 case D_LEFT: 00527 destination.x -= 10; 00528 break; 00529 case D_RIGHT: 00530 destination.x += 10; 00531 break; 00532 case D_UP: 00533 destination.y -= 10; 00534 break; 00535 case D_DOWN: 00536 destination.y += 10; 00537 break; 00538 /* to make static analyzers happy */ 00539 default: 00540 break; 00541 } 00542 00543 /* Prevent windows from vanishing completely */ 00544 if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x || 00545 (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) || 00546 (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y || 00547 (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) { 00548 DLOG("boundary check failed, not moving\n"); 00549 return; 00550 } 00551 00552 currently_focused->rect = destination; 00553 reposition_client(conn, currently_focused); 00554 00555 /* Because reposition_client does not send a faked configure event (only resize does), 00556 * we need to initiate that on our own */ 00557 fake_absolute_configure_notify(conn, currently_focused); 00558 /* fake_absolute_configure_notify flushes */ 00559 } 00560 00561 /* 00562 * Hides all floating clients (or show them if they are currently hidden) on 00563 * the specified workspace. 00564 * 00565 */ 00566 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) { 00567 Client *client; 00568 00569 workspace->floating_hidden = !workspace->floating_hidden; 00570 DLOG("floating_hidden is now: %d\n", workspace->floating_hidden); 00571 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) { 00572 if (workspace->floating_hidden) 00573 client_unmap(conn, client); 00574 else client_map(conn, client); 00575 } 00576 00577 /* If we just unmapped all floating windows we should ensure that the focus 00578 * is set correctly, that ist, to the first non-floating client in stack */ 00579 if (workspace->floating_hidden) 00580 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) { 00581 if (client_is_floating(client)) 00582 continue; 00583 set_focus(conn, client, true); 00584 return; 00585 } 00586 00587 xcb_flush(conn); 00588 } 00589 #endif