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