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