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 
68 void floating_check_size(Con *floating_con) {
69  /* Define reasonable minimal and maximal sizes for floating windows */
70  const int floating_sane_min_height = 50;
71  const int floating_sane_min_width = 75;
72  Rect floating_sane_max_dimensions;
73  Con *focused_con = con_descend_focused(floating_con);
74 
75  Rect border_rect = con_border_style_rect(focused_con);
76  /* We have to do the opposite calculations that render_con() do
77  * to get the exact size we want. */
78  border_rect.width = -border_rect.width;
79  border_rect.width += 2 * focused_con->border_width;
80  border_rect.height = -border_rect.height;
81  border_rect.height += 2 * focused_con->border_width;
82  if (con_border_style(focused_con) == BS_NORMAL) {
83  border_rect.height += render_deco_height();
84  }
85 
86  if (focused_con->window != NULL) {
87  if (focused_con->window->min_width) {
88  floating_con->rect.width -= border_rect.width;
89  floating_con->rect.width = max(floating_con->rect.width, focused_con->window->min_width);
90  floating_con->rect.width += border_rect.width;
91  }
92 
93  if (focused_con->window->min_height) {
94  floating_con->rect.height -= border_rect.height;
95  floating_con->rect.height = max(floating_con->rect.height, focused_con->window->min_height);
96  floating_con->rect.height += border_rect.height;
97  }
98 
99  if (focused_con->window->height_increment &&
100  floating_con->rect.height >= focused_con->window->base_height + border_rect.height) {
101  floating_con->rect.height -= focused_con->window->base_height + border_rect.height;
102  floating_con->rect.height -= floating_con->rect.height % focused_con->window->height_increment;
103  floating_con->rect.height += focused_con->window->base_height + border_rect.height;
104  }
105 
106  if (focused_con->window->width_increment &&
107  floating_con->rect.width >= focused_con->window->base_width + border_rect.width) {
108  floating_con->rect.width -= focused_con->window->base_width + border_rect.width;
109  floating_con->rect.width -= floating_con->rect.width % focused_con->window->width_increment;
110  floating_con->rect.width += focused_con->window->base_width + border_rect.width;
111  }
112  }
113 
114  /* Unless user requests otherwise (-1), raise the width/height to
115  * reasonable minimum dimensions */
116  if (config.floating_minimum_height != -1) {
117  floating_con->rect.height -= border_rect.height;
118  if (config.floating_minimum_height == 0) {
119  floating_con->rect.height = max(floating_con->rect.height, floating_sane_min_height);
120  } else {
121  floating_con->rect.height = max(floating_con->rect.height, config.floating_minimum_height);
122  }
123  floating_con->rect.height += border_rect.height;
124  }
125 
126  if (config.floating_minimum_width != -1) {
127  floating_con->rect.width -= border_rect.width;
128  if (config.floating_minimum_width == 0) {
129  floating_con->rect.width = max(floating_con->rect.width, floating_sane_min_width);
130  } else {
131  floating_con->rect.width = max(floating_con->rect.width, config.floating_minimum_width);
132  }
133  floating_con->rect.width += border_rect.width;
134  }
135 
136  /* Unless user requests otherwise (-1), ensure width/height do not exceed
137  * configured maxima or, if unconfigured, limit to combined width of all
138  * outputs */
139  floating_sane_max_dimensions = total_outputs_dimensions();
140  if (config.floating_maximum_height != -1) {
141  floating_con->rect.height -= border_rect.height;
142  if (config.floating_maximum_height == 0) {
143  floating_con->rect.height = min(floating_con->rect.height, floating_sane_max_dimensions.height);
144  } else {
145  floating_con->rect.height = min(floating_con->rect.height, config.floating_maximum_height);
146  }
147  floating_con->rect.height += border_rect.height;
148  }
149 
150  if (config.floating_maximum_width != -1) {
151  floating_con->rect.width -= border_rect.width;
152  if (config.floating_maximum_width == 0) {
153  floating_con->rect.width = min(floating_con->rect.width, floating_sane_max_dimensions.width);
154  } else {
155  floating_con->rect.width = min(floating_con->rect.width, config.floating_maximum_width);
156  }
157  floating_con->rect.width += border_rect.width;
158  }
159 }
160 
161 void floating_enable(Con *con, bool automatic) {
162  bool set_focus = (con == focused);
163 
164  if (con_is_docked(con)) {
165  LOG("Container is a dock window, not enabling floating mode.\n");
166  return;
167  }
168 
169  if (con_is_floating(con)) {
170  LOG("Container is already in floating mode, not doing anything.\n");
171  return;
172  }
173 
174  if (con->type == CT_WORKSPACE) {
175  LOG("Container is a workspace, not enabling floating mode.\n");
176  return;
177  }
178 
179  /* 1: detach the container from its parent */
180  /* TODO: refactor this with tree_close_internal() */
181  TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
182  TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
183 
184  con_fix_percent(con->parent);
185 
186  /* 2: create a new container to render the decoration on, add
187  * it as a floating window to the workspace */
188  Con *nc = con_new(NULL, NULL);
189  /* we need to set the parent afterwards instead of passing it as an
190  * argument to con_new() because nc would be inserted into the tiling layer
191  * otherwise. */
192  Con *ws = con_get_workspace(con);
193  nc->parent = ws;
194  nc->type = CT_FLOATING_CON;
195  nc->layout = L_SPLITH;
196  /* We insert nc already, even though its rect is not yet calculated. This
197  * is necessary because otherwise the workspace might be empty (and get
198  * closed in tree_close_internal()) even though it’s not. */
199  if (set_focus) {
200  TAILQ_INSERT_TAIL(&(ws->floating_head), nc, floating_windows);
201  } else {
202  TAILQ_INSERT_HEAD(&(ws->floating_head), nc, floating_windows);
203  }
204  TAILQ_INSERT_TAIL(&(ws->focus_head), nc, focused);
205 
206  /* check if the parent container is empty and close it if so */
207  if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) &&
208  con_num_children(con->parent) == 0) {
209  DLOG("Old container empty after setting this child to floating, closing\n");
210  Con *parent = con->parent;
211  /* clear the pointer before calling tree_close_internal in which the memory is freed */
212  con->parent = NULL;
213  tree_close_internal(parent, DONT_KILL_WINDOW, false, false);
214  }
215 
216  char *name;
217  sasprintf(&name, "[i3 con] floatingcon around %p", con);
218  x_set_name(nc, name);
219  free(name);
220 
221  /* find the height for the decorations */
222  int deco_height = render_deco_height();
223 
224  DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
225  DLOG("Geometry = (%d, %d) with %d x %d\n", con->geometry.x, con->geometry.y, con->geometry.width, con->geometry.height);
226  Rect zero = {0, 0, 0, 0};
227  nc->rect = con->geometry;
228  /* If the geometry was not set (split containers), we need to determine a
229  * sensible one by combining the geometry of all children */
230  if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
231  DLOG("Geometry not set, combining children\n");
232  Con *child;
233  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
234  DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
235  nc->rect.width += child->geometry.width;
236  nc->rect.height = max(nc->rect.height, child->geometry.height);
237  }
238  }
239 
240  TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
241  TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
242 
243  /* 3: attach the child to the new parent container. We need to do this
244  * because con_border_style_rect() needs to access con->parent. */
245  con->parent = nc;
246  con->percent = 1.0;
247  con->floating = FLOATING_USER_ON;
248 
249  /* 4: set the border style as specified with new_float */
250  if (automatic)
252 
253  /* Add pixels for the decoration. */
254  Rect border_style_rect = con_border_style_rect(con);
255 
256  nc->rect.height -= border_style_rect.height;
257  nc->rect.width -= border_style_rect.width;
258 
259  /* Add some more pixels for the title bar */
260  if (con_border_style(con) == BS_NORMAL) {
261  nc->rect.height += deco_height;
262  }
263 
264  /* Honor the X11 border */
265  nc->rect.height += con->border_width * 2;
266  nc->rect.width += con->border_width * 2;
267 
269 
270  /* Some clients (like GIMP’s color picker window) get mapped
271  * to (0, 0), so we push them to a reasonable position
272  * (centered over their leader) */
273  if (nc->rect.x == 0 && nc->rect.y == 0) {
274  Con *leader;
275  if (con->window && con->window->leader != XCB_NONE &&
276  (leader = con_by_window_id(con->window->leader)) != NULL) {
277  DLOG("Centering above leader\n");
278  floating_center(nc, leader->rect);
279  } else {
280  /* center the window on workspace as fallback */
281  floating_center(nc, ws->rect);
282  }
283  }
284 
285  /* Sanity check: Are the coordinates on the appropriate output? If not, we
286  * need to change them */
287  Output *current_output = get_output_containing(nc->rect.x +
288  (nc->rect.width / 2),
289  nc->rect.y + (nc->rect.height / 2));
290 
291  Con *correct_output = con_get_output(ws);
292  if (!current_output || current_output->con != correct_output) {
293  DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
294  nc->rect.x, nc->rect.y);
295 
296  /* If moving from one output to another, keep the relative position
297  * consistent (e.g. a centered dialog will remain centered). */
298  if (current_output)
299  floating_fix_coordinates(nc, &current_output->con->rect, &correct_output->rect);
300  else {
301  nc->rect.x = correct_output->rect.x;
302  nc->rect.y = correct_output->rect.y;
303  }
304  }
305 
306  DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
307 
308  /* 5: Subtract the deco_height in order to make the floating window appear
309  * at precisely the position it specified in its original geometry (which
310  * is what applications might remember). */
311  deco_height = (con->border_style == BS_NORMAL ? render_deco_height() : 0);
312  nc->rect.y -= deco_height;
313 
314  DLOG("Corrected y = %d (deco_height = %d)\n", nc->rect.y, deco_height);
315 
316  /* render the cons to get initial window_rect correct */
317  render_con(nc, false);
318  render_con(con, false);
319 
320  if (set_focus)
321  con_focus(con);
322 
323  /* Check if we need to re-assign it to a different workspace because of its
324  * coordinates and exit if that was done successfully. */
325  if (floating_maybe_reassign_ws(nc)) {
326  goto done;
327  }
328 
329  /* Sanitize coordinates: Check if they are on any output */
330  if (get_output_containing(nc->rect.x, nc->rect.y) != NULL) {
331  goto done;
332  }
333 
334  ELOG("No output found at destination coordinates, centering floating window on current ws\n");
335  floating_center(nc, ws->rect);
336 
337 done:
338  floating_set_hint_atom(nc, true);
339  ipc_send_window_event("floating", con);
340 }
341 
342 void floating_disable(Con *con, bool automatic) {
343  if (!con_is_floating(con)) {
344  LOG("Container isn't floating, not doing anything.\n");
345  return;
346  }
347 
348  const bool set_focus = (con == focused);
349 
350  Con *ws = con_get_workspace(con);
351  Con *parent = con->parent;
352 
353  /* 1: detach from parent container */
354  TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
355  TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
356 
357  /* 2: kill parent container */
358  TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
360  /* clear the pointer before calling tree_close_internal in which the memory is freed */
361  con->parent = NULL;
362  tree_close_internal(parent, DONT_KILL_WINDOW, true, false);
363 
364  /* 3: re-attach to the parent of the currently focused con on the workspace
365  * this floating con was on */
367 
368  /* if there is no other container on this workspace, focused will be the
369  * workspace itself */
370  if (focused->type == CT_WORKSPACE)
371  con->parent = focused;
372  else
373  con->parent = focused->parent;
374 
375  /* con_fix_percent will adjust the percent value */
376  con->percent = 0.0;
377 
378  con->floating = FLOATING_USER_OFF;
379 
380  con_attach(con, con->parent, false);
381 
382  con_fix_percent(con->parent);
383 
384  if (set_focus)
385  con_focus(con);
386 
387  floating_set_hint_atom(con, false);
388  ipc_send_window_event("floating", con);
389 }
390 
391 /*
392  * Toggles floating mode for the given container.
393  *
394  * If the automatic flag is set to true, this was an automatic update by a change of the
395  * window class from the application which can be overwritten by the user.
396  *
397  */
398 void toggle_floating_mode(Con *con, bool automatic) {
399  /* forbid the command to toggle floating on a CT_FLOATING_CON */
400  if (con->type == CT_FLOATING_CON) {
401  ELOG("Cannot toggle floating mode on con = %p because it is of type CT_FLOATING_CON.\n", con);
402  return;
403  }
404 
405  /* see if the client is already floating */
406  if (con_is_floating(con)) {
407  LOG("already floating, re-setting to tiling\n");
408 
409  floating_disable(con, automatic);
410  return;
411  }
412 
413  floating_enable(con, automatic);
414 }
415 
416 /*
417  * Raises the given container in the list of floating containers
418  *
419  */
421  DLOG("Raising floating con %p / %s\n", con, con->name);
422  TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
423  TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
424 }
425 
426 /*
427  * Checks if con’s coordinates are within its workspace and re-assigns it to
428  * the actual workspace if not.
429  *
430  */
432  Output *output = get_output_containing(
433  con->rect.x + (con->rect.width / 2),
434  con->rect.y + (con->rect.height / 2));
435 
436  if (!output) {
437  ELOG("No output found at destination coordinates?\n");
438  return false;
439  }
440 
441  if (con_get_output(con) == output->con) {
442  DLOG("still the same ws\n");
443  return false;
444  }
445 
446  DLOG("Need to re-assign!\n");
447 
448  Con *content = output_get_content(output->con);
449  Con *ws = TAILQ_FIRST(&(content->focus_head));
450  DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
451  con_move_to_workspace(con, ws, false, true, false);
453  return true;
454 }
455 
456 /*
457  * Centers a floating con above the specified rect.
458  *
459  */
460 void floating_center(Con *con, Rect rect) {
461  con->rect.x = rect.x + (rect.width / 2) - (con->rect.width / 2);
462  con->rect.y = rect.y + (rect.height / 2) - (con->rect.height / 2);
463 }
464 
465 /*
466  * Moves the given floating con to the current pointer position.
467  *
468  */
470  assert(con->type == CT_FLOATING_CON);
471 
472  xcb_query_pointer_reply_t *reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
473  if (reply == NULL) {
474  ELOG("could not query pointer position, not moving this container\n");
475  return;
476  }
477 
478  Output *output = get_output_containing(reply->root_x, reply->root_y);
479  if (output == NULL) {
480  ELOG("The pointer is not on any output, cannot move the container here.\n");
481  return;
482  }
483 
484  /* Determine where to put the window. */
485  int32_t x = reply->root_x - con->rect.width / 2;
486  int32_t y = reply->root_y - con->rect.height / 2;
487  FREE(reply);
488 
489  /* Correct target coordinates to be in-bounds. */
490  x = MAX(x, (int32_t)output->rect.x);
491  y = MAX(y, (int32_t)output->rect.y);
492  if (x + con->rect.width > output->rect.x + output->rect.width)
493  x = output->rect.x + output->rect.width - con->rect.width;
494  if (y + con->rect.height > output->rect.y + output->rect.height)
495  y = output->rect.y + output->rect.height - con->rect.height;
496 
497  /* Update container's coordinates to position it correctly. */
498  floating_reposition(con, (Rect){x, y, con->rect.width, con->rect.height});
499 }
500 
501 DRAGGING_CB(drag_window_callback) {
502  const struct xcb_button_press_event_t *event = extra;
503 
504  /* Reposition the client correctly while moving */
505  con->rect.x = old_rect->x + (new_x - event->root_x);
506  con->rect.y = old_rect->y + (new_y - event->root_y);
507 
508  render_con(con, false);
509  x_push_node(con);
510  xcb_flush(conn);
511 
512  /* Check if we cross workspace boundaries while moving */
513  if (!floating_maybe_reassign_ws(con))
514  return;
515  /* Ensure not to warp the pointer while dragging */
516  x_set_warp_to(NULL);
517  tree_render();
518 }
519 
520 /*
521  * Called when the user clicked on the titlebar of a floating window.
522  * Calls the drag_pointer function with the drag_window callback
523  *
524  */
525 void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
526  DLOG("floating_drag_window\n");
527 
528  /* Push changes before dragging, so that the window gets raised now and not
529  * after the user releases the mouse button */
530  tree_render();
531 
532  /* Store the initial rect in case of user revert/cancel */
533  Rect initial_rect = con->rect;
534 
535  /* Drag the window */
536  drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, XCURSOR_CURSOR_MOVE, drag_window_callback, event);
537 
538  if (!con_exists(con)) {
539  DLOG("The container has been closed in the meantime.\n");
540  return;
541  }
542 
543  /* If the user cancelled, undo the changes. */
544  if (drag_result == DRAG_REVERT)
545  floating_reposition(con, initial_rect);
546 
547  /* If this is a scratchpad window, don't auto center it from now on. */
548  if (con->scratchpad_state == SCRATCHPAD_FRESH)
549  con->scratchpad_state = SCRATCHPAD_CHANGED;
550 
551  tree_render();
552 }
553 
554 /*
555  * This is an ugly data structure which we need because there is no standard
556  * way of having nested functions (only available as a gcc extension at the
557  * moment, clang doesn’t support it) or blocks (only available as a clang
558  * extension and only on Mac OS X systems at the moment).
559  *
560  */
563  const bool proportional;
564  const xcb_button_press_event_t *event;
565 };
566 
567 DRAGGING_CB(resize_window_callback) {
568  const struct resize_window_callback_params *params = extra;
569  const xcb_button_press_event_t *event = params->event;
570  border_t corner = params->corner;
571 
572  int32_t dest_x = con->rect.x;
573  int32_t dest_y = con->rect.y;
574  uint32_t dest_width;
575  uint32_t dest_height;
576 
577  double ratio = (double)old_rect->width / old_rect->height;
578 
579  /* First guess: We resize by exactly the amount the mouse moved,
580  * taking into account in which corner the client was grabbed */
581  if (corner & BORDER_LEFT)
582  dest_width = old_rect->width - (new_x - event->root_x);
583  else
584  dest_width = old_rect->width + (new_x - event->root_x);
585 
586  if (corner & BORDER_TOP)
587  dest_height = old_rect->height - (new_y - event->root_y);
588  else
589  dest_height = old_rect->height + (new_y - event->root_y);
590 
591  /* User wants to keep proportions, so we may have to adjust our values */
592  if (params->proportional) {
593  dest_width = max(dest_width, (int)(dest_height * ratio));
594  dest_height = max(dest_height, (int)(dest_width / ratio));
595  }
596 
597  con->rect = (Rect){dest_x, dest_y, dest_width, dest_height};
598 
599  /* Obey window size */
600  floating_check_size(con);
601 
602  /* If not the lower right corner is grabbed, we must also reposition
603  * the client by exactly the amount we resized it */
604  if (corner & BORDER_LEFT)
605  dest_x = old_rect->x + (old_rect->width - con->rect.width);
606 
607  if (corner & BORDER_TOP)
608  dest_y = old_rect->y + (old_rect->height - con->rect.height);
609 
610  con->rect.x = dest_x;
611  con->rect.y = dest_y;
612 
613  /* TODO: don’t re-render the whole tree just because we change
614  * coordinates of a floating window */
615  tree_render();
617 }
618 
619 /*
620  * Called when the user clicked on a floating window while holding the
621  * floating_modifier and the right mouse button.
622  * Calls the drag_pointer function with the resize_window callback
623  *
624  */
626  const xcb_button_press_event_t *event) {
627  DLOG("floating_resize_window\n");
628 
629  /* corner saves the nearest corner to the original click. It contains
630  * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
631  border_t corner = 0;
632 
633  if (event->event_x <= (int16_t)(con->rect.width / 2))
634  corner |= BORDER_LEFT;
635  else
636  corner |= BORDER_RIGHT;
637 
638  int cursor = 0;
639  if (event->event_y <= (int16_t)(con->rect.height / 2)) {
640  corner |= BORDER_TOP;
642  } else {
643  corner |= BORDER_BOTTOM;
645  }
646 
647  struct resize_window_callback_params params = {corner, proportional, event};
648 
649  /* get the initial rect in case of revert/cancel */
650  Rect initial_rect = con->rect;
651 
652  drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, cursor, resize_window_callback, &params);
653 
654  if (!con_exists(con)) {
655  DLOG("The container has been closed in the meantime.\n");
656  return;
657  }
658 
659  /* If the user cancels, undo the resize */
660  if (drag_result == DRAG_REVERT)
661  floating_reposition(con, initial_rect);
662 
663  /* If this is a scratchpad window, don't auto center it from now on. */
664  if (con->scratchpad_state == SCRATCHPAD_FRESH)
665  con->scratchpad_state = SCRATCHPAD_CHANGED;
666 }
667 
668 /* Custom data structure used to track dragging-related events. */
669 struct drag_x11_cb {
670  ev_check check;
671 
672  /* Whether this modal event loop should be exited and with which result. */
674 
675  /* The container that is being dragged or resized, or NULL if this is a
676  * drag of the resize handle. */
678 
679  /* The dimensions of con when the loop was started. */
681 
682  /* The callback to invoke after every pointer movement. */
684 
685  /* User data pointer for callback. */
686  const void *extra;
687 };
688 
689 static void xcb_drag_check_cb(EV_P_ ev_check *w, int revents) {
690  struct drag_x11_cb *dragloop = (struct drag_x11_cb *)w->data;
691  xcb_motion_notify_event_t *last_motion_notify = NULL;
692  xcb_generic_event_t *event;
693 
694  while ((event = xcb_poll_for_event(conn)) != NULL) {
695  if (event->response_type == 0) {
696  xcb_generic_error_t *error = (xcb_generic_error_t *)event;
697  DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
698  error->sequence, error->error_code);
699  free(event);
700  continue;
701  }
702 
703  /* Strip off the highest bit (set if the event is generated) */
704  int type = (event->response_type & 0x7F);
705 
706  switch (type) {
707  case XCB_BUTTON_RELEASE:
708  dragloop->result = DRAG_SUCCESS;
709  break;
710 
711  case XCB_KEY_PRESS:
712  DLOG("A key was pressed during drag, reverting changes.\n");
713  dragloop->result = DRAG_REVERT;
714  handle_event(type, event);
715  break;
716 
717  case XCB_UNMAP_NOTIFY: {
718  xcb_unmap_notify_event_t *unmap_event = (xcb_unmap_notify_event_t *)event;
719  Con *con = con_by_window_id(unmap_event->window);
720 
721  if (con != NULL) {
722  DLOG("UnmapNotify for window 0x%08x (container %p)\n", unmap_event->window, con);
723 
725  DLOG("UnmapNotify for a managed window on the current workspace, aborting\n");
726  dragloop->result = DRAG_ABORT;
727  }
728  }
729 
730  handle_event(type, event);
731  break;
732  }
733 
734  case XCB_MOTION_NOTIFY:
735  /* motion_notify events are saved for later */
736  FREE(last_motion_notify);
737  last_motion_notify = (xcb_motion_notify_event_t *)event;
738  break;
739 
740  default:
741  DLOG("Passing to original handler\n");
742  handle_event(type, event);
743  break;
744  }
745 
746  if (last_motion_notify != (xcb_motion_notify_event_t *)event)
747  free(event);
748 
749  if (dragloop->result != DRAGGING)
750  return;
751  }
752 
753  if (last_motion_notify == NULL)
754  return;
755 
756  /* Ensure that we are either dragging the resize handle (con is NULL) or that the
757  * container still exists. The latter might not be true, e.g., if the window closed
758  * for any reason while the user was dragging it. */
759  if (!dragloop->con || con_exists(dragloop->con)) {
760  dragloop->callback(
761  dragloop->con,
762  &(dragloop->old_rect),
763  last_motion_notify->root_x,
764  last_motion_notify->root_y,
765  dragloop->extra);
766  }
767  free(last_motion_notify);
768 }
769 
770 /*
771  * This function grabs your pointer and keyboard and lets you drag stuff around
772  * (borders). Every time you move your mouse, an XCB_MOTION_NOTIFY event will
773  * be received and the given callback will be called with the parameters
774  * specified (client, border on which the click originally was), the original
775  * rect of the client, the event and the new coordinates (x, y).
776  *
777  */
778 drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t
779  confine_to,
780  border_t border, int cursor, callback_t callback, const void *extra) {
781  xcb_cursor_t xcursor = (cursor && xcursor_supported) ? xcursor_get_cursor(cursor) : XCB_NONE;
782 
783  /* Grab the pointer */
784  xcb_grab_pointer_cookie_t cookie;
785  xcb_grab_pointer_reply_t *reply;
786  xcb_generic_error_t *error;
787 
788  cookie = xcb_grab_pointer(conn,
789  false, /* get all pointer events specified by the following mask */
790  root, /* grab the root window */
791  XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
792  XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
793  XCB_GRAB_MODE_ASYNC, /* keyboard mode */
794  confine_to, /* confine_to = in which window should the cursor stay */
795  xcursor, /* possibly display a special cursor */
796  XCB_CURRENT_TIME);
797 
798  if ((reply = xcb_grab_pointer_reply(conn, cookie, &error)) == NULL) {
799  ELOG("Could not grab pointer (error_code = %d)\n", error->error_code);
800  free(error);
801  return DRAG_ABORT;
802  }
803 
804  free(reply);
805 
806  /* Grab the keyboard */
807  xcb_grab_keyboard_cookie_t keyb_cookie;
808  xcb_grab_keyboard_reply_t *keyb_reply;
809 
810  keyb_cookie = xcb_grab_keyboard(conn,
811  false, /* get all keyboard events */
812  root, /* grab the root window */
813  XCB_CURRENT_TIME,
814  XCB_GRAB_MODE_ASYNC, /* continue processing pointer events as normal */
815  XCB_GRAB_MODE_ASYNC /* keyboard mode */
816  );
817 
818  if ((keyb_reply = xcb_grab_keyboard_reply(conn, keyb_cookie, &error)) == NULL) {
819  ELOG("Could not grab keyboard (error_code = %d)\n", error->error_code);
820  free(error);
821  xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
822  return DRAG_ABORT;
823  }
824 
825  free(keyb_reply);
826 
827  /* Go into our own event loop */
828  struct drag_x11_cb loop = {
829  .result = DRAGGING,
830  .con = con,
831  .callback = callback,
832  .extra = extra,
833  };
834  ev_check *check = &loop.check;
835  if (con)
836  loop.old_rect = con->rect;
837  ev_check_init(check, xcb_drag_check_cb);
838  check->data = &loop;
839  main_set_x11_cb(false);
840  ev_check_start(main_loop, check);
841 
842  while (loop.result == DRAGGING)
843  ev_run(main_loop, EVRUN_ONCE);
844 
845  ev_check_stop(main_loop, check);
846  main_set_x11_cb(true);
847 
848  xcb_ungrab_keyboard(conn, XCB_CURRENT_TIME);
849  xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
850  xcb_flush(conn);
851 
852  return loop.result;
853 }
854 
855 /*
856  * Repositions the CT_FLOATING_CON to have the coordinates specified by
857  * newrect, but only if the coordinates are not out-of-bounds. Also reassigns
858  * the floating con to a different workspace if this move was across different
859  * outputs.
860  *
861  */
862 void floating_reposition(Con *con, Rect newrect) {
863  /* Sanity check: Are the new coordinates on any output? If not, we
864  * ignore that request. */
865  if (!contained_by_output(newrect)) {
866  ELOG("No output found at destination coordinates. Not repositioning.\n");
867  return;
868  }
869 
870  con->rect = newrect;
871 
873 
874  /* If this is a scratchpad window, don't auto center it from now on. */
875  if (con->scratchpad_state == SCRATCHPAD_FRESH)
876  con->scratchpad_state = SCRATCHPAD_CHANGED;
877 
878  tree_render();
879 }
880 
881 /*
882  * Sets size of the CT_FLOATING_CON to specified dimensions. Might limit the
883  * actual size with regard to size constraints taken from user settings.
884  * Additionally, the dimensions may be upscaled until they're divisible by the
885  * window's size hints.
886  *
887  */
888 void floating_resize(Con *floating_con, int x, int y) {
889  DLOG("floating resize to %dx%d px\n", x, y);
890  Rect *rect = &floating_con->rect;
891  Con *focused_con = con_descend_focused(floating_con);
892  if (focused_con->window == NULL) {
893  DLOG("No window is focused. Not resizing.\n");
894  return;
895  }
896  int wi = focused_con->window->width_increment;
897  int hi = focused_con->window->height_increment;
898  rect->width = x;
899  rect->height = y;
900  if (wi)
901  rect->width += (wi - 1 - rect->width) % wi;
902  if (hi)
903  rect->height += (hi - 1 - rect->height) % hi;
904 
905  floating_check_size(floating_con);
906 
907  /* If this is a scratchpad window, don't auto center it from now on. */
908  if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
909  floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
910 }
911 
912 /*
913  * Fixes the coordinates of the floating window whenever the window gets
914  * reassigned to a different output (or when the output’s rect changes).
915  *
916  */
918  DLOG("Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
919  con, con->rect.x, con->rect.y, con->rect.width, con->rect.height);
920  DLOG("old_rect = (%d, %d), %d x %d\n",
921  old_rect->x, old_rect->y, old_rect->width, old_rect->height);
922  DLOG("new_rect = (%d, %d), %d x %d\n",
923  new_rect->x, new_rect->y, new_rect->width, new_rect->height);
924  /* First we get the x/y coordinates relative to the x/y coordinates
925  * of the output on which the window is on */
926  int32_t rel_x = con->rect.x - old_rect->x + (int32_t)(con->rect.width / 2);
927  int32_t rel_y = con->rect.y - old_rect->y + (int32_t)(con->rect.height / 2);
928  /* Then we calculate a fraction, for example 0.63 for a window
929  * which is at y = 1212 of a 1920 px high output */
930  DLOG("rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
931  rel_x, rel_y, (double)rel_x / old_rect->width, (double)rel_y / old_rect->height,
932  old_rect->width, old_rect->height);
933  /* Here we have to multiply at first. Or we will lose precision when not compiled with -msse2 */
934  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);
935  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);
936  DLOG("Resulting coordinates: x = %d, y = %d\n", con->rect.x, con->rect.y);
937 }
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:1565
void main_set_x11_cb(bool enable)
Enable or disable the main X11 event handling function.
Definition: main.c:148
void floating_raise_con(Con *con)
Raises the given container in the list of floating containers.
Definition: floating.c:420
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition: con.c:861
int32_t floating_minimum_height
xcb_window_t root
Definition: main.c:59
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...
Con * focused
Definition: tree.c:13
Rect old_rect
Definition: floating.c:680
const void * extra
Definition: floating.c:686
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:366
struct outputs_head outputs
Definition: randr.c:21
drag_result_t
This is the return value of a drag operation like drag_pointer.
Definition: floating.h:120
xcb_cursor_t xcursor_get_cursor(enum xcursor_cursor_t c)
Definition: xcursor.c:62
callback_t callback
Definition: floating.c:683
void toggle_floating_mode(Con *con, bool automatic)
Calls floating_enable() for tiling containers and floating_disable() for floating containers...
Definition: floating.c:398
bool con_exists(Con *con)
Returns true if the given container (still) exists.
Definition: con.c:612
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:161
int min_height
Definition: data.h:469
void 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:862
int height_increment
Definition: data.h:465
int32_t floating_minimum_width
#define TAILQ_EMPTY(head)
Definition: queue.h:344
#define ELOG(fmt,...)
Definition: libi3.h:99
A &#39;Con&#39; represents everything from the X11 root window down to a single X11 window.
Definition: data.h:591
int base_height
Definition: data.h:461
#define LOG(fmt,...)
Definition: libi3.h:94
void floating_drag_window(Con *con, const xcb_button_press_event_t *event)
Called when the user clicked on the titlebar of a floating window.
Definition: floating.c:525
struct Rect rect
Definition: data.h:627
xcb_connection_t * conn
XCB connection and root screen.
Definition: main.c:46
const xcb_button_press_event_t * event
Definition: floating.c:564
bool tree_close_internal(Con *con, kill_window_t kill_window, bool dont_kill_parent, bool force_set_focus)
Closes the given container including all children.
Definition: tree.c:201
bool xcursor_supported
Definition: main.c:92
bool contained_by_output(Rect rect)
Definition: randr.c:146
void con_focus(Con *con)
Sets input focus to the given container.
Definition: con.c:223
struct Window * window
Definition: data.h:659
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:376
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition: con.c:289
struct Rect Rect
Definition: data.h:44
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:688
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:1342
enum Con::@20 type
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:406
xcb_screen_t * root_screen
Definition: main.c:58
Definition: data.h:62
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:583
int min(int a, int b)
Definition: util.c:27
void floating_center(Con *con, Rect rect)
Centers a floating con above the specified rect.
Definition: floating.c:460
#define FREE(pointer)
Definition: util.h:50
struct Rect geometry
the geometry this window requested when getting mapped
Definition: data.h:635
nodes_head
Definition: data.h:672
drag_result_t result
Definition: floating.c:673
enum Con::@21 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...
uint32_t x
Definition: data.h:149
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:1213
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:431
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:917
uint32_t x
Definition: data.h:127
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition: con.c:405
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on...
Definition: con.c:391
int con_num_children(Con *con)
Returns the number of children of this container.
Definition: con.c:802
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:347
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition: con.c:510
#define TAILQ_FIRST(head)
Definition: queue.h:336
int width_increment
Definition: data.h:464
bool con_is_docked(Con *con)
Returns true if the container is a docked container.
Definition: con.c:520
drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t confine_to, border_t border, int cursor, callback_t callback, const void *extra)
This function grabs your pointer and keyboard and lets you drag stuff around (borders).
Definition: floating.c:778
floating_head
Definition: data.h:669
void floating_disable(Con *con, bool automatic)
Disables floating mode for the given container by re-attaching the container to its old parent...
Definition: floating.c:342
char * name
Definition: data.h:637
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:1285
xcb_window_t id
Definition: data.h:402
layout_t layout
Definition: data.h:701
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:402
border_t
On which border was the dragging initiated?
Definition: floating.h:25
void render_con(Con *con, bool render_fullscreen)
"Renders" the given container (and its children), meaning that all rects are updated correctly...
Definition: render.c:40
Con * con
Definition: floating.c:677
static void floating_set_hint_atom(Con *con, bool floating)
Definition: floating.c:39
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition: output.c:16
static void xcb_drag_check_cb(EV_P_ ev_check *w, int revents)
Definition: floating.c:689
void x_set_warp_to(Rect *rect)
Set warp_to coordinates.
Definition: x.c:1255
void floating_check_size(Con *floating_con)
Called when a floating window is created or resized.
Definition: floating.c:68
char * name
Definition: data.h:353
uint32_t y
Definition: data.h:150
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:625
double percent
Definition: data.h:653
int32_t floating_maximum_width
Maximum and minimum dimensions of a floating window.
An Output is a physical output on your graphics driver.
Definition: data.h:366
ev_check check
Definition: floating.c:670
int min_width
Definition: data.h:468
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1458
uint32_t height
Definition: data.h:152
int32_t floating_maximum_height
uint32_t width
Definition: data.h:151
int con_border_style(Con *con)
Use this function to get a container’s border style.
Definition: con.c:1645
void(* callback_t)(Con *, Rect *, uint32_t, uint32_t, const void *)
Callback for dragging.
Definition: floating.h:17
uint32_t y
Definition: data.h:128
Con * con_new(Con *parent, i3Window *window)
Definition: con.c:70
#define DLOG(fmt,...)
Definition: libi3.h:104
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:989
border_style_t default_floating_border
The default border style for new floating windows.
Con * con
Pointer to the Con which represents this output.
Definition: data.h:387
int border_width
Definition: data.h:656
border_style_t border_style
Definition: data.h:702
struct ev_loop * main_loop
Definition: main.c:68
Definition: data.h:98
static Rect total_outputs_dimensions(void)
Definition: floating.c:20
int max(int a, int b)
Definition: util.c:31
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition: con.c:199
Con * con_descend_tiling_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition: con.c:1473
#define MAX(x, y)
Definition: floating.c:13
enum Con::@22 scratchpad_state
Config config
Definition: config.c:17
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:102
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:495
Rect rect
x, y, width, height
Definition: data.h:390
int render_deco_height(void)
Definition: render.c:25
void floating_resize(Con *floating_con, int x, int y)
Sets size of the CT_FLOATING_CON to specified dimensions.
Definition: floating.c:888
DRAGGING_CB(drag_window_callback)
Definition: floating.c:501
void floating_move_to_pointer(Con *con)
Moves the given floating con to the current pointer position.
Definition: floating.c:469
Stores a rectangle, for example the size of a window, the child window etc.
Definition: data.h:148
int base_width
Definition: data.h:460
struct Con * parent
Definition: data.h:623
struct Con * croot
Definition: tree.c:12
focus_head
Definition: data.h:675
void handle_event(int type, xcb_generic_event_t *event)
Takes an xcb_generic_event_t and calls the appropriate handler, based on the event type...
Definition: handlers.c:1482