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