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