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