2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * floating.c: Floating windows.
12 extern xcb_connection_t *conn;
15 * Calculates sum of heights and sum of widths of all currently active outputs
18 static Rect total_outputs_dimensions(void) {
20 /* Use Rect to encapsulate dimensions, ignoring x/y */
21 Rect outputs_dimensions = {0, 0, 0, 0};
22 TAILQ_FOREACH(output, &outputs, outputs) {
23 outputs_dimensions.height += output->rect.height;
24 outputs_dimensions.width += output->rect.width;
26 return outputs_dimensions;
29 void floating_enable(Con *con, bool automatic) {
30 bool set_focus = (con == focused);
32 if (con->parent && con->parent->type == CT_DOCKAREA) {
33 LOG("Container is a dock window, not enabling floating mode.\n");
37 if (con_is_floating(con)) {
38 LOG("Container is already in floating mode, not doing anything.\n");
42 /* 1: If the container is a workspace container, we need to create a new
43 * split-container with the same layout and make that one floating. We
44 * cannot touch the workspace container itself because floating containers
45 * are children of the workspace. */
46 if (con->type == CT_WORKSPACE) {
47 LOG("This is a workspace, creating new container around content\n");
48 if (con_num_children(con) == 0) {
49 LOG("Workspace is empty, aborting\n");
52 /* TODO: refactor this with src/con.c:con_set_layout */
53 Con *new = con_new(NULL, NULL);
55 new->layout = con->layout;
57 /* since the new container will be set into floating mode directly
58 * afterwards, we need to copy the workspace rect. */
59 memcpy(&(new->rect), &(con->rect), sizeof(Rect));
61 Con *old_focused = TAILQ_FIRST(&(con->focus_head));
62 if (old_focused == TAILQ_END(&(con->focus_head)))
65 /* 4: move the existing cons of this workspace below the new con */
66 DLOG("Moving cons\n");
68 while (!TAILQ_EMPTY(&(con->nodes_head))) {
69 child = TAILQ_FIRST(&(con->nodes_head));
71 con_attach(child, new, true);
74 /* 4: attach the new split container to the workspace */
75 DLOG("Attaching new split to ws\n");
76 con_attach(new, con, false);
79 con_focus(old_focused);
85 /* 1: detach the container from its parent */
86 /* TODO: refactor this with tree_close() */
87 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
88 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
90 con_fix_percent(con->parent);
92 /* 2: create a new container to render the decoration on, add
93 * it as a floating window to the workspace */
94 Con *nc = con_new(NULL, NULL);
95 /* we need to set the parent afterwards instead of passing it as an
96 * argument to con_new() because nc would be inserted into the tiling layer
98 Con *ws = con_get_workspace(con);
101 nc->type = CT_FLOATING_CON;
102 nc->layout = L_SPLITH;
103 /* We insert nc already, even though its rect is not yet calculated. This
104 * is necessary because otherwise the workspace might be empty (and get
105 * closed in tree_close()) even though it’s not. */
106 TAILQ_INSERT_TAIL(&(ws->floating_head), nc, floating_windows);
107 TAILQ_INSERT_TAIL(&(ws->focus_head), nc, focused);
109 /* check if the parent container is empty and close it if so */
110 if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) &&
111 con_num_children(con->parent) == 0) {
112 DLOG("Old container empty after setting this child to floating, closing\n");
113 tree_close(con->parent, DONT_KILL_WINDOW, false, false);
117 sasprintf(&name, "[i3 con] floatingcon around %p", con);
118 x_set_name(nc, name);
121 /* find the height for the decorations */
122 int deco_height = config.font.height + 5;
124 DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
125 DLOG("Geometry = (%d, %d) with %d x %d\n", con->geometry.x, con->geometry.y, con->geometry.width, con->geometry.height);
126 Rect zero = { 0, 0, 0, 0 };
127 nc->rect = con->geometry;
128 /* If the geometry was not set (split containers), we need to determine a
129 * sensible one by combining the geometry of all children */
130 if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
131 DLOG("Geometry not set, combining children\n");
133 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
134 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
135 nc->rect.width += child->geometry.width;
136 nc->rect.height = max(nc->rect.height, child->geometry.height);
140 /* Define reasonable minimal and maximal sizes for floating windows */
141 const int floating_sane_min_height = 50;
142 const int floating_sane_min_width = 75;
144 Rect floating_sane_max_dimensions;
145 floating_sane_max_dimensions = total_outputs_dimensions();
147 /* Unless user requests otherwise (-1), ensure width/height do not exceed
148 * configured maxima or, if unconfigured, limit to combined width of all
150 if (config.floating_maximum_height != -1) {
151 if (config.floating_maximum_height == 0)
152 nc->rect.height = min(nc->rect.height, floating_sane_max_dimensions.height);
154 nc->rect.height = min(nc->rect.height, config.floating_maximum_height);
156 if (config.floating_maximum_width != -1) {
157 if (config.floating_maximum_width == 0)
158 nc->rect.width = min(nc->rect.width, floating_sane_max_dimensions.width);
160 nc->rect.width = min(nc->rect.width, config.floating_maximum_width);
163 /* Unless user requests otherwise (-1), raise the width/height to
164 * reasonable minimum dimensions */
165 if (config.floating_minimum_height != -1) {
166 if (config.floating_minimum_height == 0)
167 nc->rect.height = max(nc->rect.height, floating_sane_min_height);
169 nc->rect.height = max(nc->rect.height, config.floating_minimum_height);
171 if (config.floating_minimum_width != -1) {
172 if (config.floating_minimum_width == 0)
173 nc->rect.width = max(nc->rect.width, floating_sane_min_width);
175 nc->rect.width = max(nc->rect.width, config.floating_minimum_width);
178 /* Add pixels for the decoration. */
179 Rect border_style_rect = con_border_style_rect(con);
181 nc->rect.height -= border_style_rect.height;
182 nc->rect.width -= border_style_rect.width;
184 /* Add some more pixels for the title bar */
185 if(con_border_style(con) == BS_NORMAL)
186 nc->rect.height += deco_height;
188 /* Honor the X11 border */
189 nc->rect.height += con->border_width * 2;
190 nc->rect.width += con->border_width * 2;
192 /* Some clients (like GIMP’s color picker window) get mapped
193 * to (0, 0), so we push them to a reasonable position
194 * (centered over their leader) */
195 if (nc->rect.x == 0 && nc->rect.y == 0) {
197 if (con->window && con->window->leader != XCB_NONE &&
198 (leader = con_by_window_id(con->window->leader)) != NULL) {
199 DLOG("Centering above leader\n");
200 nc->rect.x = leader->rect.x + (leader->rect.width / 2) - (nc->rect.width / 2);
201 nc->rect.y = leader->rect.y + (leader->rect.height / 2) - (nc->rect.height / 2);
203 /* center the window on workspace as fallback */
204 nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
205 nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
209 /* Sanity check: Are the coordinates on the appropriate output? If not, we
210 * need to change them */
211 Output *current_output = get_output_containing(nc->rect.x, nc->rect.y);
212 Con *correct_output = con_get_output(ws);
213 if (!current_output || current_output->con != correct_output) {
214 DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
215 nc->rect.x, nc->rect.y);
216 /* Take the relative coordinates of the current output, then add them
217 * to the coordinate space of the correct output */
218 uint32_t rel_x = (nc->rect.x - (current_output ? current_output->con->rect.x : 0));
219 uint32_t rel_y = (nc->rect.y - (current_output ? current_output->con->rect.y : 0));
220 nc->rect.x = correct_output->rect.x + rel_x;
221 nc->rect.y = correct_output->rect.y + rel_y;
224 DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
226 /* 3: attach the child to the new parent container */
229 con->floating = FLOATING_USER_ON;
231 /* 4: set the border style as specified with new_float */
233 con->border_style = config.default_floating_border;
235 /* 5: Subtract the deco_height in order to make the floating window appear
236 * at precisely the position it specified in its original geometry (which
237 * is what applications might remember). */
238 deco_height = (con->border_style == BS_NORMAL ? config.font.height + 5 : 0);
239 nc->rect.y -= deco_height;
241 DLOG("Corrected y = %d (deco_height = %d)\n", nc->rect.y, deco_height);
243 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
244 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
246 /* render the cons to get initial window_rect correct */
247 render_con(nc, false);
248 render_con(con, false);
253 /* Check if we need to re-assign it to a different workspace because of its
254 * coordinates and exit if that was done successfully. */
255 if (floating_maybe_reassign_ws(nc))
258 /* Sanitize coordinates: Check if they are on any output */
259 if (get_output_containing(nc->rect.x, nc->rect.y) != NULL)
262 ELOG("No output found at destination coordinates, centering floating window on current ws\n");
263 nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
264 nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
267 void floating_disable(Con *con, bool automatic) {
268 if (!con_is_floating(con)) {
269 LOG("Container isn't floating, not doing anything.\n");
273 Con *ws = con_get_workspace(con);
275 /* 1: detach from parent container */
276 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
277 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
279 /* 2: kill parent container */
280 TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
281 TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
282 tree_close(con->parent, DONT_KILL_WINDOW, true, false);
284 /* 3: re-attach to the parent of the currently focused con on the workspace
285 * this floating con was on */
286 Con *focused = con_descend_tiling_focused(ws);
288 /* if there is no other container on this workspace, focused will be the
289 * workspace itself */
290 if (focused->type == CT_WORKSPACE)
291 con->parent = focused;
292 else con->parent = focused->parent;
294 /* con_fix_percent will adjust the percent value */
297 con->floating = FLOATING_USER_OFF;
299 con_attach(con, con->parent, false);
301 con_fix_percent(con->parent);
302 // TODO: don’t influence focus handling when Con was not focused before.
307 * Toggles floating mode for the given container.
309 * If the automatic flag is set to true, this was an automatic update by a change of the
310 * window class from the application which can be overwritten by the user.
313 void toggle_floating_mode(Con *con, bool automatic) {
314 /* see if the client is already floating */
315 if (con_is_floating(con)) {
316 LOG("already floating, re-setting to tiling\n");
318 floating_disable(con, automatic);
322 floating_enable(con, automatic);
326 * Raises the given container in the list of floating containers
329 void floating_raise_con(Con *con) {
330 DLOG("Raising floating con %p / %s\n", con, con->name);
331 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
332 TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
336 * Checks if con’s coordinates are within its workspace and re-assigns it to
337 * the actual workspace if not.
340 bool floating_maybe_reassign_ws(Con *con) {
341 Output *output = get_output_containing(
342 con->rect.x + (con->rect.width / 2),
343 con->rect.y + (con->rect.height / 2));
346 ELOG("No output found at destination coordinates?\n");
350 if (con_get_output(con) == output->con) {
351 DLOG("still the same ws\n");
355 DLOG("Need to re-assign!\n");
357 Con *content = output_get_content(output->con);
358 Con *ws = TAILQ_FIRST(&(content->focus_head));
359 DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
360 con_move_to_workspace(con, ws, false, true);
361 con_focus(con_descend_focused(con));
365 DRAGGING_CB(drag_window_callback) {
366 const struct xcb_button_press_event_t *event = extra;
368 /* Reposition the client correctly while moving */
369 con->rect.x = old_rect->x + (new_x - event->root_x);
370 con->rect.y = old_rect->y + (new_y - event->root_y);
372 render_con(con, false);
376 /* Check if we cross workspace boundaries while moving */
377 if (!floating_maybe_reassign_ws(con))
383 * Called when the user clicked on the titlebar of a floating window.
384 * Calls the drag_pointer function with the drag_window callback
387 void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
388 DLOG("floating_drag_window\n");
390 /* Push changes before dragging, so that the window gets raised now and not
391 * after the user releases the mouse button */
394 /* Drag the window */
395 drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback, event);
400 * This is an ugly data structure which we need because there is no standard
401 * way of having nested functions (only available as a gcc extension at the
402 * moment, clang doesn’t support it) or blocks (only available as a clang
403 * extension and only on Mac OS X systems at the moment).
406 struct resize_window_callback_params {
407 const border_t corner;
408 const bool proportional;
409 const xcb_button_press_event_t *event;
412 DRAGGING_CB(resize_window_callback) {
413 const struct resize_window_callback_params *params = extra;
414 const xcb_button_press_event_t *event = params->event;
415 border_t corner = params->corner;
417 int32_t dest_x = con->rect.x;
418 int32_t dest_y = con->rect.y;
420 uint32_t dest_height;
422 double ratio = (double) old_rect->width / old_rect->height;
424 /* First guess: We resize by exactly the amount the mouse moved,
425 * taking into account in which corner the client was grabbed */
426 if (corner & BORDER_LEFT)
427 dest_width = old_rect->width - (new_x - event->root_x);
428 else dest_width = old_rect->width + (new_x - event->root_x);
430 if (corner & BORDER_TOP)
431 dest_height = old_rect->height - (new_y - event->root_y);
432 else dest_height = old_rect->height + (new_y - event->root_y);
434 /* Obey minimum window size */
435 Rect minimum = con_minimum_size(con);
436 dest_width = max(dest_width, minimum.width);
437 dest_height = max(dest_height, minimum.height);
439 /* User wants to keep proportions, so we may have to adjust our values */
440 if (params->proportional) {
441 dest_width = max(dest_width, (int) (dest_height * ratio));
442 dest_height = max(dest_height, (int) (dest_width / ratio));
445 /* If not the lower right corner is grabbed, we must also reposition
446 * the client by exactly the amount we resized it */
447 if (corner & BORDER_LEFT)
448 dest_x = old_rect->x + (old_rect->width - dest_width);
450 if (corner & BORDER_TOP)
451 dest_y = old_rect->y + (old_rect->height - dest_height);
453 con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
455 /* TODO: don’t re-render the whole tree just because we change
456 * coordinates of a floating window */
458 x_push_changes(croot);
462 * Called when the user clicked on a floating window while holding the
463 * floating_modifier and the right mouse button.
464 * Calls the drag_pointer function with the resize_window callback
467 void floating_resize_window(Con *con, const bool proportional,
468 const xcb_button_press_event_t *event) {
469 DLOG("floating_resize_window\n");
471 /* corner saves the nearest corner to the original click. It contains
472 * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
475 if (event->event_x <= (con->rect.width / 2))
476 corner |= BORDER_LEFT;
477 else corner |= BORDER_RIGHT;
479 if (event->event_y <= (con->rect.height / 2))
480 corner |= BORDER_TOP;
481 else corner |= BORDER_BOTTOM;
483 struct resize_window_callback_params params = { corner, proportional, event };
485 drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, ¶ms);
489 * This function grabs your pointer and lets you drag stuff around (borders).
490 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
491 * and the given callback will be called with the parameters specified (client,
492 * border on which the click originally was), the original rect of the client,
493 * the event and the new coordinates (x, y).
496 void drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t
497 confine_to, border_t border, callback_t callback, const void *extra)
499 uint32_t new_x, new_y;
500 Rect old_rect = { 0, 0, 0, 0 };
502 memcpy(&old_rect, &(con->rect), sizeof(Rect));
504 /* Grab the pointer */
505 xcb_grab_pointer_cookie_t cookie;
506 xcb_grab_pointer_reply_t *reply;
507 cookie = xcb_grab_pointer(conn,
508 false, /* get all pointer events specified by the following mask */
509 root, /* grab the root window */
510 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
511 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
512 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
513 confine_to, /* confine_to = in which window should the cursor stay */
514 XCB_NONE, /* don’t display a special cursor */
517 if ((reply = xcb_grab_pointer_reply(conn, cookie, NULL)) == NULL) {
518 ELOG("Could not grab pointer\n");
524 /* Go into our own event loop */
527 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
528 bool loop_done = false;
529 /* I’ve always wanted to have my own eventhandler… */
530 while (!loop_done && (inside_event = xcb_wait_for_event(conn))) {
531 /* We now handle all events we can get using xcb_poll_for_event */
533 /* skip x11 errors */
534 if (inside_event->response_type == 0) {
538 /* Strip off the highest bit (set if the event is generated) */
539 int type = (inside_event->response_type & 0x7F);
542 case XCB_BUTTON_RELEASE:
546 case XCB_MOTION_NOTIFY:
547 /* motion_notify events are saved for later */
548 FREE(last_motion_notify);
549 last_motion_notify = inside_event;
552 case XCB_UNMAP_NOTIFY:
554 case XCB_KEY_RELEASE:
555 DLOG("Unmap-notify, aborting\n");
556 handle_event(type, inside_event);
561 DLOG("Passing to original handler\n");
562 /* Use original handler */
563 handle_event(type, inside_event);
566 if (last_motion_notify != inside_event)
568 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
570 if (last_motion_notify == NULL)
573 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
574 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
576 callback(con, &old_rect, new_x, new_y, extra);
577 FREE(last_motion_notify);
580 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
585 * Repositions the CT_FLOATING_CON to have the coordinates specified by
586 * newrect, but only if the coordinates are not out-of-bounds. Also reassigns
587 * the floating con to a different workspace if this move was across different
591 void floating_reposition(Con *con, Rect newrect) {
592 /* Sanity check: Are the new coordinates on any output? If not, we
593 * ignore that request. */
594 Output *output = get_output_containing(
595 newrect.x + (newrect.width / 2),
596 newrect.y + (newrect.height / 2));
599 ELOG("No output found at destination coordinates. Not repositioning.\n");
605 floating_maybe_reassign_ws(con);
610 * Fixes the coordinates of the floating window whenever the window gets
611 * reassigned to a different output (or when the output’s rect changes).
614 void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect) {
615 DLOG("Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
616 con, con->rect.x, con->rect.y, con->rect.width, con->rect.height);
617 DLOG("old_rect = (%d, %d), %d x %d\n",
618 old_rect->x, old_rect->y, old_rect->width, old_rect->height);
619 DLOG("new_rect = (%d, %d), %d x %d\n",
620 new_rect->x, new_rect->y, new_rect->width, new_rect->height);
621 /* First we get the x/y coordinates relative to the x/y coordinates
622 * of the output on which the window is on */
623 int32_t rel_x = (con->rect.x - old_rect->x);
624 int32_t rel_y = (con->rect.y - old_rect->y);
625 /* Then we calculate a fraction, for example 0.63 for a window
626 * which is at y = 1212 of a 1920 px high output */
627 DLOG("rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
628 rel_x, rel_y, (double)rel_x / old_rect->width, (double)rel_y / old_rect->height,
629 old_rect->width, old_rect->height);
630 /* Here we have to multiply at first. Or we will lose precision when not compiled with -msse2 */
631 con->rect.x = (int32_t)new_rect->x + (double)(rel_x * (int32_t)new_rect->width) / (int32_t)old_rect->width;
632 con->rect.y = (int32_t)new_rect->y + (double)(rel_y * (int32_t)new_rect->height) / (int32_t)old_rect->height;
633 DLOG("Resulting coordinates: x = %d, y = %d\n", con->rect.x, con->rect.y);
638 * Moves the client 10px to the specified direction.
641 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
642 DLOG("floating move\n");
644 Rect destination = currently_focused->rect;
645 Rect *screen = &(currently_focused->workspace->output->rect);
660 /* to make static analyzers happy */
665 /* Prevent windows from vanishing completely */
666 if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
667 (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
668 (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
669 (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
670 DLOG("boundary check failed, not moving\n");
674 currently_focused->rect = destination;
675 reposition_client(conn, currently_focused);
677 /* Because reposition_client does not send a faked configure event (only resize does),
678 * we need to initiate that on our own */
679 fake_absolute_configure_notify(conn, currently_focused);
680 /* fake_absolute_configure_notify flushes */
684 * Hides all floating clients (or show them if they are currently hidden) on
685 * the specified workspace.
688 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
691 workspace->floating_hidden = !workspace->floating_hidden;
692 DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
693 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
694 if (workspace->floating_hidden)
695 client_unmap(conn, client);
696 else client_map(conn, client);
699 /* If we just unmapped all floating windows we should ensure that the focus
700 * is set correctly, that ist, to the first non-floating client in stack */
701 if (workspace->floating_hidden)
702 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
703 if (client_is_floating(client))
705 set_focus(conn, client, true);