i3
floating.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
6 *
7 * floating.c: Floating windows.
8 *
9 */
10#include "all.h"
11
12#include <math.h>
13
14#ifndef MAX
15#define MAX(x, y) ((x) > (y) ? (x) : (y))
16#endif
17
18/*
19 * Calculates sum of heights and sum of widths of all currently active outputs
20 *
21 */
23 if (TAILQ_EMPTY(&outputs)) {
24 return (Rect){0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
25 }
26
27 Output *output;
28 /* Use Rect to encapsulate dimensions, ignoring x/y */
29 Rect outputs_dimensions = {0, 0, 0, 0};
30 TAILQ_FOREACH (output, &outputs, outputs) {
31 outputs_dimensions.height += output->rect.height;
32 outputs_dimensions.width += output->rect.width;
33 }
34 return outputs_dimensions;
35}
36
37/*
38 * Updates I3_FLOATING_WINDOW by either setting or removing it on the con and
39 * all its children.
40 *
41 */
42static void floating_set_hint_atom(Con *con, bool floating) {
43 if (!con_is_leaf(con)) {
44 Con *child;
45 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
46 floating_set_hint_atom(child, floating);
47 }
48 }
49
50 if (con->window == NULL) {
51 return;
52 }
53
54 if (floating) {
55 uint32_t val = 1;
56 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
57 A_I3_FLOATING_WINDOW, XCB_ATOM_CARDINAL, 32, 1, &val);
58 } else {
59 xcb_delete_property(conn, con->window->id, A_I3_FLOATING_WINDOW);
60 }
61
62 xcb_flush(conn);
63}
64
65/*
66 * Called when a floating window is created or resized. This function resizes
67 * the window if its size is higher or lower than the configured maximum/minimum
68 * size, respectively or when adjustments are needed to conform to the
69 * configured size increments or aspect ratio limits.
70 *
71 * When prefer_height is true and the window needs to be resized because of the
72 * configured aspect ratio, the width is adjusted first, preserving the previous
73 * height.
74 *
75 */
76void floating_check_size(Con *floating_con, bool prefer_height) {
77 /* Define reasonable minimal and maximal sizes for floating windows */
78 const int floating_sane_min_height = 50;
79 const int floating_sane_min_width = 75;
80 Rect floating_sane_max_dimensions;
81 Con *focused_con = con_descend_focused(floating_con);
82
83 DLOG("deco_rect.height = %d\n", focused_con->deco_rect.height);
84 Rect border_rect = con_border_style_rect(focused_con);
85 /* We have to do the opposite calculations that render_con() do
86 * to get the exact size we want. */
87 border_rect.width = -border_rect.width;
88 border_rect.height = -border_rect.height;
89
90 /* undo x11 border */
91 border_rect.width += 2 * focused_con->border_width;
92 border_rect.height += 2 * focused_con->border_width;
93
94 DLOG("floating_check_size, want min width %d, min height %d, border extra: w=%d, h=%d\n",
95 floating_sane_min_width,
96 floating_sane_min_height,
97 border_rect.width,
98 border_rect.height);
99
100 i3Window *window = focused_con->window;
101 if (window != NULL) {
102 /* ICCCM says: If a base size is not provided, the minimum size is to be used in its place
103 * and vice versa. */
104 int min_width = (window->min_width ? window->min_width : window->base_width);
105 int min_height = (window->min_height ? window->min_height : window->base_height);
106 int base_width = (window->base_width ? window->base_width : window->min_width);
107 int base_height = (window->base_height ? window->base_height : window->min_height);
108
109 if (min_width) {
110 floating_con->rect.width -= border_rect.width;
111 floating_con->rect.width = max(floating_con->rect.width, min_width);
112 floating_con->rect.width += border_rect.width;
113 }
114
115 if (min_height) {
116 floating_con->rect.height -= border_rect.height;
117 floating_con->rect.height = max(floating_con->rect.height, min_height);
118 floating_con->rect.height += border_rect.height;
119 }
120
121 if (window->max_width) {
122 floating_con->rect.width -= border_rect.width;
123 floating_con->rect.width = min(floating_con->rect.width, window->max_width);
124 floating_con->rect.width += border_rect.width;
125 }
126
127 if (window->max_height) {
128 floating_con->rect.height -= border_rect.height;
129 floating_con->rect.height = min(floating_con->rect.height, window->max_height);
130 floating_con->rect.height += border_rect.height;
131 }
132
133 /* Obey the aspect ratio, if any, unless we are in fullscreen mode.
134 *
135 * The spec isn’t explicit on whether the aspect ratio hints should be
136 * respected during fullscreen mode. Other WMs such as Openbox don’t do
137 * that, and this post suggests that this is the correct way to do it:
138 * https://mail.gnome.org/archives/wm-spec-list/2003-May/msg00007.html
139 *
140 * Ignoring aspect ratio during fullscreen was necessary to fix MPlayer
141 * subtitle rendering, see https://bugs.i3wm.org/594 */
142 const double min_ar = window->min_aspect_ratio;
143 const double max_ar = window->max_aspect_ratio;
144 if (floating_con->fullscreen_mode == CF_NONE && (min_ar > 0 || max_ar > 0)) {
145 /* The ICCCM says to subtract the base size from the window size for
146 * aspect ratio calculations. However, unlike determining the base
147 * size itself we must not fall back to using the minimum size in
148 * this case according to the ICCCM. */
149 double width = floating_con->rect.width - window->base_width - border_rect.width;
150 double height = floating_con->rect.height - window->base_height - border_rect.height;
151 const double ar = (double)width / (double)height;
152 double new_ar = -1;
153 if (min_ar > 0 && ar < min_ar) {
154 new_ar = min_ar;
155 } else if (max_ar > 0 && ar > max_ar) {
156 new_ar = max_ar;
157 }
158 if (new_ar > 0) {
159 if (prefer_height) {
160 width = round(height * new_ar);
161 height = round(width / new_ar);
162 } else {
163 height = round(width / new_ar);
164 width = round(height * new_ar);
165 }
166 floating_con->rect.width = width + window->base_width + border_rect.width;
167 floating_con->rect.height = height + window->base_height + border_rect.height;
168 }
169 }
170
171 if (window->height_increment &&
172 floating_con->rect.height >= base_height + border_rect.height) {
173 floating_con->rect.height -= base_height + border_rect.height;
174 floating_con->rect.height -= floating_con->rect.height % window->height_increment;
175 floating_con->rect.height += base_height + border_rect.height;
176 }
177
178 if (window->width_increment &&
179 floating_con->rect.width >= base_width + border_rect.width) {
180 floating_con->rect.width -= base_width + border_rect.width;
181 floating_con->rect.width -= floating_con->rect.width % window->width_increment;
182 floating_con->rect.width += base_width + border_rect.width;
183 }
184 }
185
186 /* Unless user requests otherwise (-1), raise the width/height to
187 * reasonable minimum dimensions */
189 floating_con->rect.height -= border_rect.height;
191 floating_con->rect.height = max(floating_con->rect.height, floating_sane_min_height);
192 } else {
193 floating_con->rect.height = max(floating_con->rect.height, config.floating_minimum_height);
194 }
195 floating_con->rect.height += border_rect.height;
196 }
197
198 if (config.floating_minimum_width != -1) {
199 floating_con->rect.width -= border_rect.width;
201 floating_con->rect.width = max(floating_con->rect.width, floating_sane_min_width);
202 } else {
203 floating_con->rect.width = max(floating_con->rect.width, config.floating_minimum_width);
204 }
205 floating_con->rect.width += border_rect.width;
206 }
207
208 /* Unless user requests otherwise (-1), ensure width/height do not exceed
209 * configured maxima or, if unconfigured, limit to combined width of all
210 * outputs */
211 floating_sane_max_dimensions = total_outputs_dimensions();
213 floating_con->rect.height -= border_rect.height;
215 floating_con->rect.height = min(floating_con->rect.height, floating_sane_max_dimensions.height);
216 } else {
217 floating_con->rect.height = min(floating_con->rect.height, config.floating_maximum_height);
218 }
219 floating_con->rect.height += border_rect.height;
220 }
221
222 if (config.floating_maximum_width != -1) {
223 floating_con->rect.width -= border_rect.width;
225 floating_con->rect.width = min(floating_con->rect.width, floating_sane_max_dimensions.width);
226 } else {
227 floating_con->rect.width = min(floating_con->rect.width, config.floating_maximum_width);
228 }
229 floating_con->rect.width += border_rect.width;
230 }
231}
232
233bool floating_enable(Con *con, bool automatic) {
234 bool set_focus = (con == focused);
235
236 if (con_is_docked(con)) {
237 LOG("Container is a dock window, not enabling floating mode.\n");
238 return false;
239 }
240
241 if (con_is_floating(con)) {
242 LOG("Container is already in floating mode, not doing anything.\n");
243 return false;
244 }
245
246 if (con->type == CT_WORKSPACE) {
247 LOG("Container is a workspace, not enabling floating mode.\n");
248 return false;
249 }
250
251 Con *focus_head_placeholder = NULL;
252 bool focus_before_parent = true;
253 if (!set_focus) {
254 /* Find recursively the ancestor container which is a child of our workspace.
255 * We need to reuse its focus position later. */
256 Con *ancestor = con;
257 while (ancestor->parent->type != CT_WORKSPACE) {
258 focus_before_parent &= TAILQ_FIRST(&(ancestor->parent->focus_head)) == ancestor;
259 ancestor = ancestor->parent;
260 }
261 /* Consider the part of the focus stack of our current workspace:
262 * [ ... S_{i-1} S_{i} S_{i+1} ... ]
263 * Where S_{x} is a container tree and the container 'con' that is being switched to
264 * floating belongs in S_{i}. The new floating container, 'nc', will have the
265 * workspace as its parent so it needs to be placed in this stack. If C was focused
266 * we just need to call con_focus(). Otherwise, nc must be placed before or after S_{i}.
267 * We should avoid using the S_{i} container for our operations since it might get
268 * killed if it has no other children. So, the two possible positions are after S_{i-1}
269 * or before S_{i+1}.
270 */
271 if (focus_before_parent) {
272 focus_head_placeholder = TAILQ_PREV(ancestor, focus_head, focused);
273 } else {
274 focus_head_placeholder = TAILQ_NEXT(ancestor, focused);
275 }
276 }
277
278 /* 1: detach the container from its parent */
279 /* TODO: refactor this with tree_close_internal() */
280 con_detach(con);
282
283 /* 2: create a new container to render the decoration on, add
284 * it as a floating window to the workspace */
285 Con *nc = con_new(NULL, NULL);
286 /* we need to set the parent afterwards instead of passing it as an
287 * argument to con_new() because nc would be inserted into the tiling layer
288 * otherwise. */
289 Con *ws = con_get_workspace(con);
290 nc->parent = ws;
291 nc->type = CT_FLOATING_CON;
292 nc->layout = L_SPLITH;
293 /* We insert nc already, even though its rect is not yet calculated. This
294 * is necessary because otherwise the workspace might be empty (and get
295 * closed in tree_close_internal()) even though it’s not. */
296 TAILQ_INSERT_HEAD(&(ws->floating_head), nc, floating_windows);
297
298 struct focus_head *fh = &(ws->focus_head);
299 if (focus_before_parent) {
300 if (focus_head_placeholder) {
301 TAILQ_INSERT_AFTER(fh, focus_head_placeholder, nc, focused);
302 } else {
303 TAILQ_INSERT_HEAD(fh, nc, focused);
304 }
305 } else {
306 if (focus_head_placeholder) {
307 TAILQ_INSERT_BEFORE(focus_head_placeholder, nc, focused);
308 } else {
309 /* Also used for the set_focus case */
310 TAILQ_INSERT_TAIL(fh, nc, focused);
311 }
312 }
313
314 /* check if the parent container is empty and close it if so */
315 if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) &&
316 con_num_children(con->parent) == 0) {
317 DLOG("Old container empty after setting this child to floating, closing\n");
318 Con *parent = con->parent;
319 /* clear the pointer before calling tree_close_internal in which the memory is freed */
320 con->parent = NULL;
322 }
323
324 char *name;
325 sasprintf(&name, "[i3 con] floatingcon around %p", con);
326 x_set_name(nc, name);
327 free(name);
328
329 DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
330 DLOG("Geometry = (%d, %d) with %d x %d\n", con->geometry.x, con->geometry.y, con->geometry.width, con->geometry.height);
331 nc->rect = con->geometry;
332 /* If the geometry was not set (split containers), we need to determine a
333 * sensible one by combining the geometry of all children */
334 if (rect_equals(nc->rect, (Rect){0, 0, 0, 0})) {
335 DLOG("Geometry not set, combining children\n");
336 Con *child;
337 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
338 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
339 nc->rect.width += child->geometry.width;
340 nc->rect.height = max(nc->rect.height, child->geometry.height);
341 }
342 }
343
344 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
345 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
346
347 /* 3: attach the child to the new parent container. We need to do this
348 * because con_border_style_rect() needs to access con->parent. */
349 con->parent = nc;
350 con->percent = 1.0;
351 con->floating = FLOATING_USER_ON;
352
353 /* 4: set the border style as specified with new_float */
354 if (automatic) {
356 }
357
358 /* Add pixels for the decoration. */
359 Rect bsr = con_border_style_rect(con);
360
361 nc->rect.height -= bsr.height;
362 nc->rect.width -= bsr.width;
363
364 /* Honor the X11 border */
365 nc->rect.height += con->border_width * 2;
366 nc->rect.width += con->border_width * 2;
367
368 floating_check_size(nc, false);
369
370 /* Some clients (like GIMP’s color picker window) get mapped
371 * to (0, 0), so we push them to a reasonable position
372 * (centered over their leader) */
373 if (nc->rect.x == 0 && nc->rect.y == 0) {
374 Con *leader;
375 if (con->window && con->window->leader != XCB_NONE &&
376 con->window->id != con->window->leader &&
377 (leader = con_by_window_id(con->window->leader)) != NULL) {
378 DLOG("Centering above leader\n");
379 floating_center(nc, leader->rect);
380 } else {
381 /* center the window on workspace as fallback */
382 floating_center(nc, ws->rect);
383 }
384 }
385
386 /* Sanity check: Are the coordinates on the appropriate output? If not, we
387 * need to change them */
388 Output *current_output = get_output_from_rect(nc->rect);
389 Con *correct_output = con_get_output(ws);
390 if (!current_output || current_output->con != correct_output) {
391 DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
392 nc->rect.x, nc->rect.y);
393
394 /* If moving from one output to another, keep the relative position
395 * consistent (e.g. a centered dialog will remain centered). */
396 if (current_output) {
397 floating_fix_coordinates(nc, &current_output->con->rect, &correct_output->rect);
398 /* Make sure that the result is in the correct output. */
399 current_output = get_output_from_rect(nc->rect);
400 }
401 if (!current_output || current_output->con != correct_output) {
402 floating_center(nc, ws->rect);
403 }
404 }
405
406 DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
407
408 /* render the cons to get initial window_rect correct */
409 render_con(nc);
410
411 if (set_focus) {
412 con_activate(con);
413 }
414
415 floating_set_hint_atom(nc, true);
416 ipc_send_window_event("floating", con);
417 return true;
418}
419
421 if (!con_is_floating(con)) {
422 LOG("Container isn't floating, not doing anything.\n");
423 return;
424 }
425
426 Con *ws = con_get_workspace(con);
427 if (con_is_internal(ws)) {
428 LOG("Can't disable floating for container in internal workspace.\n");
429 return;
430 }
431 Con *tiling_focused = con_descend_tiling_focused(ws);
432
433 if (tiling_focused->type == CT_WORKSPACE) {
434 Con *parent = con->parent;
435 con_detach(con);
436 con->parent = NULL;
438 con_attach(con, tiling_focused, false);
439 con->percent = 0.0;
441 } else {
442 insert_con_into(con, tiling_focused, AFTER);
443 }
444
445 con->floating = FLOATING_USER_OFF;
446 floating_set_hint_atom(con, false);
447 ipc_send_window_event("floating", con);
448}
449
450/*
451 * Toggles floating mode for the given container.
452 *
453 * If the automatic flag is set to true, this was an automatic update by a change of the
454 * window class from the application which can be overwritten by the user.
455 *
456 */
457void toggle_floating_mode(Con *con, bool automatic) {
458 /* forbid the command to toggle floating on a CT_FLOATING_CON */
459 if (con->type == CT_FLOATING_CON) {
460 ELOG("Cannot toggle floating mode on con = %p because it is of type CT_FLOATING_CON.\n", con);
461 return;
462 }
463
464 /* see if the client is already floating */
465 if (con_is_floating(con)) {
466 LOG("already floating, re-setting to tiling\n");
467
468 floating_disable(con);
469 return;
470 }
471
472 floating_enable(con, automatic);
473}
474
475/*
476 * Raises the given container in the list of floating containers
477 *
478 */
480 DLOG("Raising floating con %p / %s\n", con, con->name);
481 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
482 TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
483}
484
485/*
486 * Checks if con’s coordinates are within its workspace and re-assigns it to
487 * the actual workspace if not.
488 *
489 */
492 DLOG("Con in an internal workspace\n");
493 return false;
494 }
495
496 Output *output = get_output_from_rect(con->rect);
497
498 if (!output) {
499 ELOG("No output found at destination coordinates?\n");
500 return false;
501 }
502
503 if (con_get_output(con) == output->con) {
504 DLOG("still the same ws\n");
505 return false;
506 }
507
508 DLOG("Need to re-assign!\n");
509
510 Con *content = output_get_content(output->con);
511 Con *ws = TAILQ_FIRST(&(content->focus_head));
512 DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
513 Con *needs_focus = con_descend_focused(con);
514 if (!con_inside_focused(needs_focus)) {
515 needs_focus = NULL;
516 }
517 con_move_to_workspace(con, ws, false, true, false);
518 if (needs_focus) {
519 workspace_show(ws);
520 con_activate(needs_focus);
521 }
522 return true;
523}
524
525/*
526 * Centers a floating con above the specified rect.
527 *
528 */
529void floating_center(Con *con, Rect rect) {
530 con->rect.x = rect.x + (rect.width / 2) - (con->rect.width / 2);
531 con->rect.y = rect.y + (rect.height / 2) - (con->rect.height / 2);
532}
533
534/*
535 * Moves the given floating con to the current pointer position.
536 *
537 */
539 assert(con->type == CT_FLOATING_CON);
540
541 xcb_query_pointer_reply_t *reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
542 if (reply == NULL) {
543 ELOG("could not query pointer position, not moving this container\n");
544 return;
545 }
546
547 Output *output = get_output_containing(reply->root_x, reply->root_y);
548 if (output == NULL) {
549 ELOG("The pointer is not on any output, cannot move the container here.\n");
550 return;
551 }
552
553 /* Determine where to put the window. */
554 int32_t x = reply->root_x - con->rect.width / 2;
555 int32_t y = reply->root_y - con->rect.height / 2;
556 FREE(reply);
557
558 /* Correct target coordinates to be in-bounds. */
559 x = MAX(x, (int32_t)output->rect.x);
560 y = MAX(y, (int32_t)output->rect.y);
561 if (x + con->rect.width > output->rect.x + output->rect.width) {
562 x = output->rect.x + output->rect.width - con->rect.width;
563 }
564 if (y + con->rect.height > output->rect.y + output->rect.height) {
565 y = output->rect.y + output->rect.height - con->rect.height;
566 }
567
568 /* Update container's coordinates to position it correctly. */
569 floating_reposition(con, (Rect){x, y, con->rect.width, con->rect.height});
570}
571
572DRAGGING_CB(drag_window_callback) {
573 /* Reposition the client correctly while moving */
574 con->rect.x = old_rect->x + (new_x - event->root_x);
575 con->rect.y = old_rect->y + (new_y - event->root_y);
576
577 render_con(con);
578 x_push_node(con);
579 xcb_flush(conn);
580
581 /* Check if we cross workspace boundaries while moving */
582 if (!floating_maybe_reassign_ws(con)) {
583 return;
584 }
585 /* Ensure not to warp the pointer while dragging */
586 x_set_warp_to(NULL);
587 tree_render();
588}
589
590/*
591 * Called when the user clicked on the titlebar of a floating window.
592 * Calls the drag_pointer function with the drag_window callback
593 *
594 */
595void floating_drag_window(Con *con, const xcb_button_press_event_t *event, bool use_threshold) {
596 DLOG("floating_drag_window\n");
597
598 /* Push changes before dragging, so that the window gets raised now and not
599 * after the user releases the mouse button */
600 tree_render();
601
602 /* Store the initial rect in case of user revert/cancel */
603 Rect initial_rect = con->rect;
604
605 /* Drag the window */
606 drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, XCURSOR_CURSOR_MOVE, use_threshold, drag_window_callback, NULL);
607
608 if (!con_exists(con)) {
609 DLOG("The container has been closed in the meantime.\n");
610 return;
611 }
612
613 /* If the user cancelled, undo the changes. */
614 if (drag_result == DRAG_REVERT) {
615 floating_reposition(con, initial_rect);
616 return;
617 }
618
619 /* If this is a scratchpad window, don't auto center it from now on. */
620 if (con->scratchpad_state == SCRATCHPAD_FRESH) {
621 con->scratchpad_state = SCRATCHPAD_CHANGED;
622 }
623
624 tree_render();
625}
626
627/*
628 * This is an ugly data structure which we need because there is no standard
629 * way of having nested functions (only available as a gcc extension at the
630 * moment, clang doesn’t support it) or blocks (only available as a clang
631 * extension and only on Mac OS X systems at the moment).
632 *
633 */
638
639DRAGGING_CB(resize_window_callback) {
640 const struct resize_window_callback_params *params = extra;
641 border_t corner = params->corner;
642
643 int32_t dest_x = con->rect.x;
644 int32_t dest_y = con->rect.y;
645 uint32_t dest_width;
646 uint32_t dest_height;
647
648 double ratio = (double)old_rect->width / old_rect->height;
649
650 /* First guess: We resize by exactly the amount the mouse moved,
651 * taking into account in which corner the client was grabbed */
652 if (corner & BORDER_LEFT) {
653 dest_width = old_rect->width - (new_x - event->root_x);
654 } else {
655 dest_width = old_rect->width + (new_x - event->root_x);
656 }
657
658 if (corner & BORDER_TOP) {
659 dest_height = old_rect->height - (new_y - event->root_y);
660 } else {
661 dest_height = old_rect->height + (new_y - event->root_y);
662 }
663
664 /* User wants to keep proportions, so we may have to adjust our values */
665 if (params->proportional) {
666 dest_width = max(dest_width, (int)(dest_height * ratio));
667 dest_height = max(dest_height, (int)(dest_width / ratio));
668 }
669
670 con->rect = (Rect){dest_x, dest_y, dest_width, dest_height};
671
672 /* Obey window size */
673 floating_check_size(con, false);
674
675 /* If not the lower right corner is grabbed, we must also reposition
676 * the client by exactly the amount we resized it */
677 if (corner & BORDER_LEFT) {
678 dest_x = old_rect->x + (old_rect->width - con->rect.width);
679 }
680
681 if (corner & BORDER_TOP) {
682 dest_y = old_rect->y + (old_rect->height - con->rect.height);
683 }
684
685 con->rect.x = dest_x;
686 con->rect.y = dest_y;
687
688 render_con(con);
690}
691
692/*
693 * Called when the user clicked on a floating window while holding the
694 * floating_modifier and the right mouse button.
695 * Calls the drag_pointer function with the resize_window callback
696 *
697 */
699 const xcb_button_press_event_t *event) {
700 DLOG("floating_resize_window\n");
701
702 /* Push changes before resizing, so that the window gets raised now and not
703 * after the user releases the mouse button */
704 tree_render();
705
706 /* corner saves the nearest corner to the original click. It contains
707 * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
708 border_t corner = 0;
709
710 if (event->event_x <= (int16_t)(con->rect.width / 2)) {
712 } else {
714 }
715
716 int cursor = 0;
717 if (event->event_y <= (int16_t)(con->rect.height / 2)) {
720 } else {
723 }
724
726
727 /* get the initial rect in case of revert/cancel */
728 Rect initial_rect = con->rect;
729
730 drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, cursor, false, resize_window_callback, &params);
731
732 if (!con_exists(con)) {
733 DLOG("The container has been closed in the meantime.\n");
734 return;
735 }
736
737 /* If the user cancels, undo the resize */
738 if (drag_result == DRAG_REVERT) {
739 floating_reposition(con, initial_rect);
740 }
741
742 /* If this is a scratchpad window, don't auto center it from now on. */
743 if (con->scratchpad_state == SCRATCHPAD_FRESH) {
744 con->scratchpad_state = SCRATCHPAD_CHANGED;
745 }
746}
747
748/*
749 * Repositions the CT_FLOATING_CON to have the coordinates specified by
750 * newrect, but only if the coordinates are not out-of-bounds. Also reassigns
751 * the floating con to a different workspace if this move was across different
752 * outputs.
753 *
754 */
755bool floating_reposition(Con *con, Rect newrect) {
756 /* Sanity check: Are the new coordinates on any output? If not, we
757 * ignore that request. */
758 if (!output_containing_rect(newrect)) {
759 ELOG("No output found at destination coordinates. Not repositioning.\n");
760 return false;
761 }
762
763 con->rect = newrect;
764
766
767 /* If this is a scratchpad window, don't auto center it from now on. */
768 if (con->scratchpad_state == SCRATCHPAD_FRESH) {
769 con->scratchpad_state = SCRATCHPAD_CHANGED;
770 }
771
772 tree_render();
773 return true;
774}
775
776/*
777 * Sets size of the CT_FLOATING_CON to specified dimensions. Might limit the
778 * actual size with regard to size constraints taken from user settings.
779 * Additionally, the dimensions may be upscaled until they're divisible by the
780 * window's size hints.
781 *
782 */
783void floating_resize(Con *floating_con, uint32_t x, uint32_t y) {
784 DLOG("floating resize to %dx%d px\n", x, y);
785 Rect *rect = &floating_con->rect;
786 Con *focused_con = con_descend_focused(floating_con);
787 if (focused_con->window == NULL) {
788 DLOG("No window is focused. Not resizing.\n");
789 return;
790 }
791 int wi = focused_con->window->width_increment;
792 int hi = focused_con->window->height_increment;
793 bool prefer_height = (rect->width == x);
794 rect->width = x;
795 rect->height = y;
796 if (wi) {
797 rect->width += (wi - 1 - rect->width) % wi;
798 }
799 if (hi) {
800 rect->height += (hi - 1 - rect->height) % hi;
801 }
802
803 floating_check_size(floating_con, prefer_height);
804
805 /* If this is a scratchpad window, don't auto center it from now on. */
806 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH) {
807 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
808 }
809}
810
811/*
812 * Fixes the coordinates of the floating window whenever the window gets
813 * reassigned to a different output (or when the output’s rect changes).
814 *
815 */
816void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect) {
817 DLOG("Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
818 con, con->rect.x, con->rect.y, con->rect.width, con->rect.height);
819 DLOG("old_rect = (%d, %d), %d x %d\n",
820 old_rect->x, old_rect->y, old_rect->width, old_rect->height);
821 DLOG("new_rect = (%d, %d), %d x %d\n",
822 new_rect->x, new_rect->y, new_rect->width, new_rect->height);
823 /* First we get the x/y coordinates relative to the x/y coordinates
824 * of the output on which the window is on */
825 int32_t rel_x = con->rect.x - old_rect->x + (int32_t)(con->rect.width / 2);
826 int32_t rel_y = con->rect.y - old_rect->y + (int32_t)(con->rect.height / 2);
827 /* Then we calculate a fraction, for example 0.63 for a window
828 * which is at y = 1212 of a 1920 px high output */
829 DLOG("rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
830 rel_x, rel_y, (double)rel_x / old_rect->width, (double)rel_y / old_rect->height,
831 old_rect->width, old_rect->height);
832 /* Here we have to multiply at first. Or we will lose precision when not compiled with -msse2 */
833 con->rect.x = (int32_t)new_rect->x + (double)(rel_x * (int32_t)new_rect->width) / (int32_t)old_rect->width - (int32_t)(con->rect.width / 2);
834 con->rect.y = (int32_t)new_rect->y + (double)(rel_y * (int32_t)new_rect->height) / (int32_t)old_rect->height - (int32_t)(con->rect.height / 2);
835 DLOG("Resulting coordinates: x = %d, y = %d\n", con->rect.x, con->rect.y);
836}
#define y(x,...)
Definition commands.c:18
void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp, bool ignore_focus)
Moves the given container to the currently focused container on the given workspace.
Definition con.c:1574
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition con.c:670
Con * con_new(Con *parent, i3Window *window)
A wrapper for con_new_skeleton, to retain the old con_new behaviour.
Definition con.c:70
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition con.c:547
Con * con_descend_tiling_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1714
bool con_is_docked(Con *con)
Returns true if the container is a docked container.
Definition con.c:679
Con * con_by_window_id(xcb_window_t window)
Returns the container with the given client window ID or NULL if no such container exists.
Definition con.c:752
Rect con_border_style_rect(Con *con)
Returns a "relative" Rect which contains the amount of pixels that need to be added to the original R...
Definition con.c:1887
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition con.c:662
bool con_exists(Con *con)
Returns true if the given container (still) exists.
Definition con.c:783
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition con.c:234
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition con.c:1144
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition con.c:226
bool con_inside_focused(Con *con)
Checks if the given container is inside a focused container.
Definition con.c:720
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition con.c:366
int con_num_children(Con *con)
Returns the number of children of this container.
Definition con.c:1075
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition con.c:292
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition con.c:532
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1698
Config config
Definition config.c:19
drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t confine_to, int cursor, bool use_threshold, callback_t callback, const void *extra)
This function grabs your pointer and keyboard and lets you drag stuff around (borders).
Definition drag.c:176
void floating_resize_window(Con *con, const bool proportional, const xcb_button_press_event_t *event)
Called when the user clicked on a floating window while holding the floating_modifier and the right m...
Definition floating.c:698
void floating_check_size(Con *floating_con, bool prefer_height)
Called when a floating window is created or resized.
Definition floating.c:76
void floating_disable(Con *con)
Disables floating mode for the given container by re-attaching the container to its old parent.
Definition floating.c:420
void floating_center(Con *con, Rect rect)
Centers a floating con above the specified rect.
Definition floating.c:529
static Rect total_outputs_dimensions(void)
Definition floating.c:22
static void floating_set_hint_atom(Con *con, bool floating)
Definition floating.c:42
void floating_drag_window(Con *con, const xcb_button_press_event_t *event, bool use_threshold)
Called when the user clicked on the titlebar of a floating window.
Definition floating.c:595
void floating_raise_con(Con *con)
Raises the given container in the list of floating containers.
Definition floating.c:479
void toggle_floating_mode(Con *con, bool automatic)
Calls floating_enable() for tiling containers and floating_disable() for floating containers.
Definition floating.c:457
#define MAX(x, y)
Definition floating.c:15
void floating_resize(Con *floating_con, uint32_t x, uint32_t y)
Sets size of the CT_FLOATING_CON to specified dimensions.
Definition floating.c:783
bool floating_maybe_reassign_ws(Con *con)
Checks if con’s coordinates are within its workspace and re-assigns it to the actual workspace if not...
Definition floating.c:490
void floating_move_to_pointer(Con *con)
Moves the given floating con to the current pointer position.
Definition floating.c:538
void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect)
Fixes the coordinates of the floating window whenever the window gets reassigned to a different outpu...
Definition floating.c:816
bool floating_enable(Con *con, bool automatic)
Enables floating mode for the given container by detaching it from its parent, creating a new contain...
Definition floating.c:233
bool floating_reposition(Con *con, Rect newrect)
Repositions the CT_FLOATING_CON to have the coordinates specified by newrect, but only if the coordin...
Definition floating.c:755
void insert_con_into(Con *con, Con *target, position_t position)
This function detaches 'con' from its parent and inserts it either before or after 'target'.
Definition move.c:65
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition output.c:16
Output * get_output_from_rect(Rect rect)
Returns the active output which contains the midpoint of the given rect.
Definition randr.c:145
Output * output_containing_rect(Rect rect)
In output_containing_rect, we check if any active output contains part of the container.
Definition randr.c:183
Output * get_output_containing(unsigned int x, unsigned int y)
Returns the active (!) output which contains the coordinates x, y or NULL if there is no output which...
Definition randr.c:122
struct outputs_head outputs
Definition randr.c:22
void render_con(Con *con)
"Renders" the given container (and its children), meaning that all rects are updated correctly.
Definition render.c:43
struct Con * focused
Definition tree.c:13
struct Con * croot
Definition tree.c:12
bool tree_close_internal(Con *con, kill_window_t kill_window, bool dont_kill_parent)
Closes the given container including all children.
Definition tree.c:192
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition tree.c:455
bool rect_equals(Rect a, Rect b)
Definition util.c:59
int min(int a, int b)
Definition util.c:24
int max(int a, int b)
Definition util.c:28
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition workspace.c:438
void x_set_warp_to(Rect *rect)
Set warp_to coordinates.
Definition x.c:1545
void x_set_name(Con *con, const char *name)
Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways) of the given name.
Definition x.c:1497
void x_push_node(Con *con)
This function pushes the properties of each node of the layout tree to X11 if they have changed (like...
Definition x.c:942
void x_push_changes(Con *con)
Pushes all changes (state of each node, see x_push_node() and the window stack) to X11.
Definition x.c:1270
void ipc_send_window_event(const char *property, Con *con)
For the window events we send, along the usual "change" field, also the window container,...
Definition ipc.c:1650
xcb_connection_t * conn
XCB connection and root screen.
Definition main.c:54
xcb_window_t root
Definition main.c:67
xcb_screen_t * root_screen
Definition main.c:66
@ AFTER
Definition data.h:64
struct Rect Rect
Definition data.h:44
@ L_SPLITH
Definition data.h:108
@ CF_NONE
Definition data.h:629
@ DONT_KILL_WINDOW
Definition data.h:73
#define DRAGGING_CB(name)
Macro to create a callback function for dragging.
Definition drag.h:19
drag_result_t
This is the return value of a drag operation like drag_pointer.
Definition drag.h:39
@ DRAG_REVERT
Definition drag.h:42
border_t
On which border was the dragging initiated?
Definition floating.h:17
@ BORDER_BOTTOM
Definition floating.h:20
@ BORDER_TOP
Definition floating.h:19
@ BORDER_RIGHT
Definition floating.h:18
@ BORDER_LEFT
Definition floating.h:17
#define DLOG(fmt,...)
Definition libi3.h:105
#define LOG(fmt,...)
Definition libi3.h:95
#define ELOG(fmt,...)
Definition libi3.h:100
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:376
#define TAILQ_PREV(elm, headname, field)
Definition queue.h:342
#define TAILQ_FIRST(head)
Definition queue.h:336
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:402
#define TAILQ_NEXT(elm, field)
Definition queue.h:338
#define TAILQ_EMPTY(head)
Definition queue.h:344
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition queue.h:394
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition queue.h:366
#define TAILQ_INSERT_AFTER(head, listelm, elm, field)
Definition queue.h:384
#define FREE(pointer)
Definition util.h:47
@ XCURSOR_CURSOR_TOP_LEFT_CORNER
Definition xcursor.h:20
@ XCURSOR_CURSOR_BOTTOM_RIGHT_CORNER
Definition xcursor.h:23
@ XCURSOR_CURSOR_MOVE
Definition xcursor.h:25
@ XCURSOR_CURSOR_TOP_RIGHT_CORNER
Definition xcursor.h:21
@ XCURSOR_CURSOR_BOTTOM_LEFT_CORNER
Definition xcursor.h:22
int32_t floating_minimum_width
int32_t floating_minimum_height
int32_t floating_maximum_width
Maximum and minimum dimensions of a floating window.
int32_t floating_maximum_height
border_style_t default_floating_border
The default border style for new floating windows.
Stores a rectangle, for example the size of a window, the child window etc.
Definition data.h:185
uint32_t height
Definition data.h:189
uint32_t x
Definition data.h:186
uint32_t y
Definition data.h:187
uint32_t width
Definition data.h:188
An Output is a physical output on your graphics driver.
Definition data.h:391
Con * con
Pointer to the Con which represents this output.
Definition data.h:411
Rect rect
x, y, width, height
Definition data.h:414
A 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition data.h:424
double max_aspect_ratio
Definition data.h:503
int base_height
Definition data.h:487
int height_increment
Definition data.h:491
int max_height
Definition data.h:499
double min_aspect_ratio
Definition data.h:502
int max_width
Definition data.h:498
xcb_window_t id
Definition data.h:425
int min_height
Definition data.h:495
int width_increment
Definition data.h:490
int base_width
Definition data.h:486
xcb_window_t leader
Holds the xcb_window_t (just an ID) for the leader window (logical parent for toolwindows and similar...
Definition data.h:429
int min_width
Definition data.h:494
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:643
struct Con * parent
Definition data.h:678
enum Con::@20 scratchpad_state
struct Rect deco_rect
Definition data.h:688
enum Con::@18 type
int border_width
Definition data.h:715
double percent
Definition data.h:712
struct Rect rect
Definition data.h:682
border_style_t max_user_border_style
Definition data.h:764
layout_t layout
Definition data.h:755
struct Window * window
Definition data.h:718
border_style_t border_style
Definition data.h:757
char * name
Definition data.h:692
struct Rect geometry
the geometry this window requested when getting mapped
Definition data.h:690
enum Con::@19 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...
fullscreen_mode_t fullscreen_mode
Definition data.h:734