2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * floating.c: Floating windows.
13 #define MAX(x, y) ((x) > (y) ? (x) : (y))
17 * Calculates sum of heights and sum of widths of all currently active outputs
20 static Rect total_outputs_dimensions(void) {
21 if (TAILQ_EMPTY(&outputs))
22 return (Rect){0, 0, root_screen->width_in_pixels, root_screen->height_in_pixels};
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;
31 return outputs_dimensions;
35 * Updates I3_FLOATING_WINDOW by either setting or removing it on the con and
39 static void floating_set_hint_atom(Con *con, bool floating) {
40 if (!con_is_leaf(con)) {
42 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
43 floating_set_hint_atom(child, floating);
47 if (con->window == NULL) {
53 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
54 A_I3_FLOATING_WINDOW, XCB_ATOM_CARDINAL, 32, 1, &val);
56 xcb_delete_property(conn, con->window->id, A_I3_FLOATING_WINDOW);
63 * Called when a floating window is created or resized.
64 * This function resizes the window if its size is higher or lower than the
65 * configured maximum/minimum size, respectively.
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);
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();
86 i3Window *window = focused_con->window;
88 if (window->min_width) {
89 floating_con->rect.width -= border_rect.width;
90 floating_con->rect.width = max(floating_con->rect.width, window->min_width);
91 floating_con->rect.width += border_rect.width;
94 if (window->min_height) {
95 floating_con->rect.height -= border_rect.height;
96 floating_con->rect.height = max(floating_con->rect.height, window->min_height);
97 floating_con->rect.height += border_rect.height;
100 if (window->max_width) {
101 floating_con->rect.width -= border_rect.width;
102 floating_con->rect.width = min(floating_con->rect.width, window->max_width);
103 floating_con->rect.width += border_rect.width;
106 if (window->max_height) {
107 floating_con->rect.height -= border_rect.height;
108 floating_con->rect.height = min(floating_con->rect.height, window->max_height);
109 floating_con->rect.height += border_rect.height;
112 if (window->height_increment &&
113 floating_con->rect.height >= window->base_height + border_rect.height) {
114 floating_con->rect.height -= window->base_height + border_rect.height;
115 floating_con->rect.height -= floating_con->rect.height % window->height_increment;
116 floating_con->rect.height += window->base_height + border_rect.height;
119 if (window->width_increment &&
120 floating_con->rect.width >= window->base_width + border_rect.width) {
121 floating_con->rect.width -= window->base_width + border_rect.width;
122 floating_con->rect.width -= floating_con->rect.width % window->width_increment;
123 floating_con->rect.width += window->base_width + border_rect.width;
127 /* Unless user requests otherwise (-1), raise the width/height to
128 * reasonable minimum dimensions */
129 if (config.floating_minimum_height != -1) {
130 floating_con->rect.height -= border_rect.height;
131 if (config.floating_minimum_height == 0) {
132 floating_con->rect.height = max(floating_con->rect.height, floating_sane_min_height);
134 floating_con->rect.height = max(floating_con->rect.height, config.floating_minimum_height);
136 floating_con->rect.height += border_rect.height;
139 if (config.floating_minimum_width != -1) {
140 floating_con->rect.width -= border_rect.width;
141 if (config.floating_minimum_width == 0) {
142 floating_con->rect.width = max(floating_con->rect.width, floating_sane_min_width);
144 floating_con->rect.width = max(floating_con->rect.width, config.floating_minimum_width);
146 floating_con->rect.width += border_rect.width;
149 /* Unless user requests otherwise (-1), ensure width/height do not exceed
150 * configured maxima or, if unconfigured, limit to combined width of all
152 floating_sane_max_dimensions = total_outputs_dimensions();
153 if (config.floating_maximum_height != -1) {
154 floating_con->rect.height -= border_rect.height;
155 if (config.floating_maximum_height == 0) {
156 floating_con->rect.height = min(floating_con->rect.height, floating_sane_max_dimensions.height);
158 floating_con->rect.height = min(floating_con->rect.height, config.floating_maximum_height);
160 floating_con->rect.height += border_rect.height;
163 if (config.floating_maximum_width != -1) {
164 floating_con->rect.width -= border_rect.width;
165 if (config.floating_maximum_width == 0) {
166 floating_con->rect.width = min(floating_con->rect.width, floating_sane_max_dimensions.width);
168 floating_con->rect.width = min(floating_con->rect.width, config.floating_maximum_width);
170 floating_con->rect.width += border_rect.width;
174 void floating_enable(Con *con, bool automatic) {
175 bool set_focus = (con == focused);
177 if (con_is_docked(con)) {
178 LOG("Container is a dock window, not enabling floating mode.\n");
182 if (con_is_floating(con)) {
183 LOG("Container is already in floating mode, not doing anything.\n");
187 if (con->type == CT_WORKSPACE) {
188 LOG("Container is a workspace, not enabling floating mode.\n");
192 Con *focus_head_placeholder = NULL;
193 bool focus_before_parent = true;
195 /* Find recursively the ancestor container which is a child of our workspace.
196 * We need to reuse its focus position later. */
198 while (ancestor->parent->type != CT_WORKSPACE) {
199 focus_before_parent &= TAILQ_FIRST(&(ancestor->parent->focus_head)) == ancestor;
200 ancestor = ancestor->parent;
202 /* Consider the part of the focus stack of our current workspace:
203 * [ ... S_{i-1} S_{i} S_{i+1} ... ]
204 * Where S_{x} is a container tree and the container 'con' that is beeing switched to
205 * floating belongs in S_{i}. The new floating container, 'nc', will have the
206 * workspace as its parent so it needs to be placed in this stack. If C was focused
207 * we just need to call con_focus(). Otherwise, nc must be placed before or after S_{i}.
208 * We should avoid using the S_{i} container for our operations since it might get
209 * killed if it has no other children. So, the two possible positions are after S_{i-1}
212 if (focus_before_parent) {
213 focus_head_placeholder = TAILQ_PREV(ancestor, focus_head, focused);
215 focus_head_placeholder = TAILQ_NEXT(ancestor, focused);
219 /* 1: detach the container from its parent */
220 /* TODO: refactor this with tree_close_internal() */
222 con_fix_percent(con->parent);
224 /* 2: create a new container to render the decoration on, add
225 * it as a floating window to the workspace */
226 Con *nc = con_new(NULL, NULL);
227 /* we need to set the parent afterwards instead of passing it as an
228 * argument to con_new() because nc would be inserted into the tiling layer
230 Con *ws = con_get_workspace(con);
232 nc->type = CT_FLOATING_CON;
233 nc->layout = L_SPLITH;
234 /* We insert nc already, even though its rect is not yet calculated. This
235 * is necessary because otherwise the workspace might be empty (and get
236 * closed in tree_close_internal()) even though it’s not. */
237 TAILQ_INSERT_HEAD(&(ws->floating_head), nc, floating_windows);
239 struct focus_head *fh = &(ws->focus_head);
240 if (focus_before_parent) {
241 if (focus_head_placeholder) {
242 TAILQ_INSERT_AFTER(fh, focus_head_placeholder, nc, focused);
244 TAILQ_INSERT_HEAD(fh, nc, focused);
247 if (focus_head_placeholder) {
248 TAILQ_INSERT_BEFORE(focus_head_placeholder, nc, focused);
250 /* Also used for the set_focus case */
251 TAILQ_INSERT_TAIL(fh, nc, focused);
255 /* check if the parent container is empty and close it if so */
256 if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) &&
257 con_num_children(con->parent) == 0) {
258 DLOG("Old container empty after setting this child to floating, closing\n");
259 Con *parent = con->parent;
260 /* clear the pointer before calling tree_close_internal in which the memory is freed */
262 tree_close_internal(parent, DONT_KILL_WINDOW, false);
266 sasprintf(&name, "[i3 con] floatingcon around %p", con);
267 x_set_name(nc, name);
270 /* find the height for the decorations */
271 int deco_height = render_deco_height();
273 DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
274 DLOG("Geometry = (%d, %d) with %d x %d\n", con->geometry.x, con->geometry.y, con->geometry.width, con->geometry.height);
275 Rect zero = {0, 0, 0, 0};
276 nc->rect = con->geometry;
277 /* If the geometry was not set (split containers), we need to determine a
278 * sensible one by combining the geometry of all children */
279 if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
280 DLOG("Geometry not set, combining children\n");
282 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
283 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
284 nc->rect.width += child->geometry.width;
285 nc->rect.height = max(nc->rect.height, child->geometry.height);
289 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
290 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
292 /* 3: attach the child to the new parent container. We need to do this
293 * because con_border_style_rect() needs to access con->parent. */
296 con->floating = FLOATING_USER_ON;
298 /* 4: set the border style as specified with new_float */
300 con->border_style = config.default_floating_border;
302 /* Add pixels for the decoration. */
303 Rect border_style_rect = con_border_style_rect(con);
305 nc->rect.height -= border_style_rect.height;
306 nc->rect.width -= border_style_rect.width;
308 /* Add some more pixels for the title bar */
309 if (con_border_style(con) == BS_NORMAL) {
310 nc->rect.height += deco_height;
313 /* Honor the X11 border */
314 nc->rect.height += con->border_width * 2;
315 nc->rect.width += con->border_width * 2;
317 floating_check_size(nc);
319 /* Some clients (like GIMP’s color picker window) get mapped
320 * to (0, 0), so we push them to a reasonable position
321 * (centered over their leader) */
322 if (nc->rect.x == 0 && nc->rect.y == 0) {
324 if (con->window && con->window->leader != XCB_NONE &&
325 (leader = con_by_window_id(con->window->leader)) != NULL) {
326 DLOG("Centering above leader\n");
327 floating_center(nc, leader->rect);
329 /* center the window on workspace as fallback */
330 floating_center(nc, ws->rect);
334 /* Sanity check: Are the coordinates on the appropriate output? If not, we
335 * need to change them */
336 Output *current_output = get_output_from_rect(nc->rect);
337 Con *correct_output = con_get_output(ws);
338 if (!current_output || current_output->con != correct_output) {
339 DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
340 nc->rect.x, nc->rect.y);
342 /* If moving from one output to another, keep the relative position
343 * consistent (e.g. a centered dialog will remain centered). */
344 if (current_output) {
345 floating_fix_coordinates(nc, ¤t_output->con->rect, &correct_output->rect);
346 /* Make sure that the result is in the correct output. */
347 current_output = get_output_from_rect(nc->rect);
349 if (!current_output || current_output->con != correct_output) {
350 floating_center(nc, ws->rect);
354 DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
356 /* 5: Subtract the deco_height in order to make the floating window appear
357 * at precisely the position it specified in its original geometry (which
358 * is what applications might remember). */
359 deco_height = (con->border_style == BS_NORMAL ? render_deco_height() : 0);
360 nc->rect.y -= deco_height;
362 DLOG("Corrected y = %d (deco_height = %d)\n", nc->rect.y, deco_height);
364 /* render the cons to get initial window_rect correct */
365 render_con(nc, false);
366 render_con(con, false);
371 floating_set_hint_atom(nc, true);
372 ipc_send_window_event("floating", con);
375 void floating_disable(Con *con, bool automatic) {
376 if (!con_is_floating(con)) {
377 LOG("Container isn't floating, not doing anything.\n");
381 Con *ws = con_get_workspace(con);
382 if (con_is_internal(ws)) {
383 LOG("Can't disable floating for container in internal workspace.\n");
386 Con *tiling_focused = con_descend_tiling_focused(ws);
388 if (tiling_focused->type == CT_WORKSPACE) {
389 Con *parent = con->parent;
392 tree_close_internal(parent, DONT_KILL_WINDOW, true);
393 con_attach(con, tiling_focused, false);
395 con_fix_percent(con->parent);
397 insert_con_into(con, tiling_focused, AFTER);
400 con->floating = FLOATING_USER_OFF;
401 floating_set_hint_atom(con, false);
402 ipc_send_window_event("floating", con);
406 * Toggles floating mode for the given container.
408 * If the automatic flag is set to true, this was an automatic update by a change of the
409 * window class from the application which can be overwritten by the user.
412 void toggle_floating_mode(Con *con, bool automatic) {
413 /* forbid the command to toggle floating on a CT_FLOATING_CON */
414 if (con->type == CT_FLOATING_CON) {
415 ELOG("Cannot toggle floating mode on con = %p because it is of type CT_FLOATING_CON.\n", con);
419 /* see if the client is already floating */
420 if (con_is_floating(con)) {
421 LOG("already floating, re-setting to tiling\n");
423 floating_disable(con, automatic);
427 floating_enable(con, automatic);
431 * Raises the given container in the list of floating containers
434 void floating_raise_con(Con *con) {
435 DLOG("Raising floating con %p / %s\n", con, con->name);
436 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
437 TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
441 * Checks if con’s coordinates are within its workspace and re-assigns it to
442 * the actual workspace if not.
445 bool floating_maybe_reassign_ws(Con *con) {
446 Output *output = get_output_from_rect(con->rect);
449 ELOG("No output found at destination coordinates?\n");
453 if (con_get_output(con) == output->con) {
454 DLOG("still the same ws\n");
458 DLOG("Need to re-assign!\n");
460 Con *content = output_get_content(output->con);
461 Con *ws = TAILQ_FIRST(&(content->focus_head));
462 DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
463 con_move_to_workspace(con, ws, false, true, false);
465 con_activate(con_descend_focused(con));
470 * Centers a floating con above the specified rect.
473 void floating_center(Con *con, Rect rect) {
474 con->rect.x = rect.x + (rect.width / 2) - (con->rect.width / 2);
475 con->rect.y = rect.y + (rect.height / 2) - (con->rect.height / 2);
479 * Moves the given floating con to the current pointer position.
482 void floating_move_to_pointer(Con *con) {
483 assert(con->type == CT_FLOATING_CON);
485 xcb_query_pointer_reply_t *reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
487 ELOG("could not query pointer position, not moving this container\n");
491 Output *output = get_output_containing(reply->root_x, reply->root_y);
492 if (output == NULL) {
493 ELOG("The pointer is not on any output, cannot move the container here.\n");
497 /* Determine where to put the window. */
498 int32_t x = reply->root_x - con->rect.width / 2;
499 int32_t y = reply->root_y - con->rect.height / 2;
502 /* Correct target coordinates to be in-bounds. */
503 x = MAX(x, (int32_t)output->rect.x);
504 y = MAX(y, (int32_t)output->rect.y);
505 if (x + con->rect.width > output->rect.x + output->rect.width)
506 x = output->rect.x + output->rect.width - con->rect.width;
507 if (y + con->rect.height > output->rect.y + output->rect.height)
508 y = output->rect.y + output->rect.height - con->rect.height;
510 /* Update container's coordinates to position it correctly. */
511 floating_reposition(con, (Rect){x, y, con->rect.width, con->rect.height});
514 DRAGGING_CB(drag_window_callback) {
515 const struct xcb_button_press_event_t *event = extra;
517 /* Reposition the client correctly while moving */
518 con->rect.x = old_rect->x + (new_x - event->root_x);
519 con->rect.y = old_rect->y + (new_y - event->root_y);
521 render_con(con, false);
525 /* Check if we cross workspace boundaries while moving */
526 if (!floating_maybe_reassign_ws(con))
528 /* Ensure not to warp the pointer while dragging */
534 * Called when the user clicked on the titlebar of a floating window.
535 * Calls the drag_pointer function with the drag_window callback
538 void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
539 DLOG("floating_drag_window\n");
541 /* Push changes before dragging, so that the window gets raised now and not
542 * after the user releases the mouse button */
545 /* Store the initial rect in case of user revert/cancel */
546 Rect initial_rect = con->rect;
548 /* Drag the window */
549 drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, XCURSOR_CURSOR_MOVE, drag_window_callback, event);
551 if (!con_exists(con)) {
552 DLOG("The container has been closed in the meantime.\n");
556 /* If the user cancelled, undo the changes. */
557 if (drag_result == DRAG_REVERT) {
558 floating_reposition(con, initial_rect);
562 /* If this is a scratchpad window, don't auto center it from now on. */
563 if (con->scratchpad_state == SCRATCHPAD_FRESH)
564 con->scratchpad_state = SCRATCHPAD_CHANGED;
570 * This is an ugly data structure which we need because there is no standard
571 * way of having nested functions (only available as a gcc extension at the
572 * moment, clang doesn’t support it) or blocks (only available as a clang
573 * extension and only on Mac OS X systems at the moment).
576 struct resize_window_callback_params {
577 const border_t corner;
578 const bool proportional;
579 const xcb_button_press_event_t *event;
582 DRAGGING_CB(resize_window_callback) {
583 const struct resize_window_callback_params *params = extra;
584 const xcb_button_press_event_t *event = params->event;
585 border_t corner = params->corner;
587 int32_t dest_x = con->rect.x;
588 int32_t dest_y = con->rect.y;
590 uint32_t dest_height;
592 double ratio = (double)old_rect->width / old_rect->height;
594 /* First guess: We resize by exactly the amount the mouse moved,
595 * taking into account in which corner the client was grabbed */
596 if (corner & BORDER_LEFT)
597 dest_width = old_rect->width - (new_x - event->root_x);
599 dest_width = old_rect->width + (new_x - event->root_x);
601 if (corner & BORDER_TOP)
602 dest_height = old_rect->height - (new_y - event->root_y);
604 dest_height = old_rect->height + (new_y - event->root_y);
606 /* User wants to keep proportions, so we may have to adjust our values */
607 if (params->proportional) {
608 dest_width = max(dest_width, (int)(dest_height * ratio));
609 dest_height = max(dest_height, (int)(dest_width / ratio));
612 con->rect = (Rect){dest_x, dest_y, dest_width, dest_height};
614 /* Obey window size */
615 floating_check_size(con);
617 /* If not the lower right corner is grabbed, we must also reposition
618 * the client by exactly the amount we resized it */
619 if (corner & BORDER_LEFT)
620 dest_x = old_rect->x + (old_rect->width - con->rect.width);
622 if (corner & BORDER_TOP)
623 dest_y = old_rect->y + (old_rect->height - con->rect.height);
625 con->rect.x = dest_x;
626 con->rect.y = dest_y;
628 /* TODO: don’t re-render the whole tree just because we change
629 * coordinates of a floating window */
631 x_push_changes(croot);
635 * Called when the user clicked on a floating window while holding the
636 * floating_modifier and the right mouse button.
637 * Calls the drag_pointer function with the resize_window callback
640 void floating_resize_window(Con *con, const bool proportional,
641 const xcb_button_press_event_t *event) {
642 DLOG("floating_resize_window\n");
644 /* corner saves the nearest corner to the original click. It contains
645 * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
648 if (event->event_x <= (int16_t)(con->rect.width / 2))
649 corner |= BORDER_LEFT;
651 corner |= BORDER_RIGHT;
654 if (event->event_y <= (int16_t)(con->rect.height / 2)) {
655 corner |= BORDER_TOP;
656 cursor = (corner & BORDER_LEFT) ? XCURSOR_CURSOR_TOP_LEFT_CORNER : XCURSOR_CURSOR_TOP_RIGHT_CORNER;
658 corner |= BORDER_BOTTOM;
659 cursor = (corner & BORDER_LEFT) ? XCURSOR_CURSOR_BOTTOM_LEFT_CORNER : XCURSOR_CURSOR_BOTTOM_RIGHT_CORNER;
662 struct resize_window_callback_params params = {corner, proportional, event};
664 /* get the initial rect in case of revert/cancel */
665 Rect initial_rect = con->rect;
667 drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, cursor, resize_window_callback, ¶ms);
669 if (!con_exists(con)) {
670 DLOG("The container has been closed in the meantime.\n");
674 /* If the user cancels, undo the resize */
675 if (drag_result == DRAG_REVERT)
676 floating_reposition(con, initial_rect);
678 /* If this is a scratchpad window, don't auto center it from now on. */
679 if (con->scratchpad_state == SCRATCHPAD_FRESH)
680 con->scratchpad_state = SCRATCHPAD_CHANGED;
683 /* Custom data structure used to track dragging-related events. */
687 /* Whether this modal event loop should be exited and with which result. */
688 drag_result_t result;
690 /* The container that is being dragged or resized, or NULL if this is a
691 * drag of the resize handle. */
694 /* The dimensions of con when the loop was started. */
697 /* The callback to invoke after every pointer movement. */
700 /* User data pointer for callback. */
704 static bool drain_drag_events(EV_P, struct drag_x11_cb *dragloop) {
705 xcb_motion_notify_event_t *last_motion_notify = NULL;
706 xcb_generic_event_t *event;
708 while ((event = xcb_poll_for_event(conn)) != NULL) {
709 if (event->response_type == 0) {
710 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
711 DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
712 error->sequence, error->error_code);
717 /* Strip off the highest bit (set if the event is generated) */
718 int type = (event->response_type & 0x7F);
721 case XCB_BUTTON_RELEASE:
722 dragloop->result = DRAG_SUCCESS;
726 DLOG("A key was pressed during drag, reverting changes.\n");
727 dragloop->result = DRAG_REVERT;
728 handle_event(type, event);
731 case XCB_UNMAP_NOTIFY: {
732 xcb_unmap_notify_event_t *unmap_event = (xcb_unmap_notify_event_t *)event;
733 Con *con = con_by_window_id(unmap_event->window);
736 DLOG("UnmapNotify for window 0x%08x (container %p)\n", unmap_event->window, con);
738 if (con_get_workspace(con) == con_get_workspace(focused)) {
739 DLOG("UnmapNotify for a managed window on the current workspace, aborting\n");
740 dragloop->result = DRAG_ABORT;
744 handle_event(type, event);
748 case XCB_MOTION_NOTIFY:
749 /* motion_notify events are saved for later */
750 FREE(last_motion_notify);
751 last_motion_notify = (xcb_motion_notify_event_t *)event;
755 DLOG("Passing to original handler\n");
756 handle_event(type, event);
760 if (last_motion_notify != (xcb_motion_notify_event_t *)event)
763 if (dragloop->result != DRAGGING) {
764 ev_break(EV_A_ EVBREAK_ONE);
765 if (dragloop->result == DRAG_SUCCESS) {
766 /* Ensure motion notify events are handled. */
769 free(last_motion_notify);
775 if (last_motion_notify == NULL) {
779 /* Ensure that we are either dragging the resize handle (con is NULL) or that the
780 * container still exists. The latter might not be true, e.g., if the window closed
781 * for any reason while the user was dragging it. */
782 if (!dragloop->con || con_exists(dragloop->con)) {
785 &(dragloop->old_rect),
786 last_motion_notify->root_x,
787 last_motion_notify->root_y,
790 FREE(last_motion_notify);
793 return dragloop->result != DRAGGING;
796 static void xcb_drag_prepare_cb(EV_P_ ev_prepare *w, int revents) {
797 struct drag_x11_cb *dragloop = (struct drag_x11_cb *)w->data;
798 while (!drain_drag_events(EV_A, dragloop)) {
799 /* repeatedly drain events: draining might produce additional ones */
804 * This function grabs your pointer and keyboard and lets you drag stuff around
805 * (borders). Every time you move your mouse, an XCB_MOTION_NOTIFY event will
806 * be received and the given callback will be called with the parameters
807 * specified (client, border on which the click originally was), the original
808 * rect of the client, the event and the new coordinates (x, y).
811 drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t confine_to,
812 border_t border, int cursor, callback_t callback, const void *extra) {
813 xcb_cursor_t xcursor = (cursor && xcursor_supported) ? xcursor_get_cursor(cursor) : XCB_NONE;
815 /* Grab the pointer */
816 xcb_grab_pointer_cookie_t cookie;
817 xcb_grab_pointer_reply_t *reply;
818 xcb_generic_error_t *error;
820 cookie = xcb_grab_pointer(conn,
821 false, /* get all pointer events specified by the following mask */
822 root, /* grab the root window */
823 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
824 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
825 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
826 confine_to, /* confine_to = in which window should the cursor stay */
827 xcursor, /* possibly display a special cursor */
830 if ((reply = xcb_grab_pointer_reply(conn, cookie, &error)) == NULL) {
831 ELOG("Could not grab pointer (error_code = %d)\n", error->error_code);
838 /* Grab the keyboard */
839 xcb_grab_keyboard_cookie_t keyb_cookie;
840 xcb_grab_keyboard_reply_t *keyb_reply;
842 keyb_cookie = xcb_grab_keyboard(conn,
843 false, /* get all keyboard events */
844 root, /* grab the root window */
846 XCB_GRAB_MODE_ASYNC, /* continue processing pointer events as normal */
847 XCB_GRAB_MODE_ASYNC /* keyboard mode */
850 if ((keyb_reply = xcb_grab_keyboard_reply(conn, keyb_cookie, &error)) == NULL) {
851 ELOG("Could not grab keyboard (error_code = %d)\n", error->error_code);
853 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
859 /* Go into our own event loop */
860 struct drag_x11_cb loop = {
863 .callback = callback,
866 ev_prepare *prepare = &loop.prepare;
868 loop.old_rect = con->rect;
869 ev_prepare_init(prepare, xcb_drag_prepare_cb);
870 prepare->data = &loop;
871 main_set_x11_cb(false);
872 ev_prepare_start(main_loop, prepare);
874 ev_loop(main_loop, 0);
876 ev_prepare_stop(main_loop, prepare);
877 main_set_x11_cb(true);
879 xcb_ungrab_keyboard(conn, XCB_CURRENT_TIME);
880 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
887 * Repositions the CT_FLOATING_CON to have the coordinates specified by
888 * newrect, but only if the coordinates are not out-of-bounds. Also reassigns
889 * the floating con to a different workspace if this move was across different
893 bool floating_reposition(Con *con, Rect newrect) {
894 /* Sanity check: Are the new coordinates on any output? If not, we
895 * ignore that request. */
896 if (!output_containing_rect(newrect)) {
897 ELOG("No output found at destination coordinates. Not repositioning.\n");
903 bool reassigned = floating_maybe_reassign_ws(con);
905 /* If this is a scratchpad window, don't auto center it from now on. */
906 if (con->scratchpad_state == SCRATCHPAD_FRESH)
907 con->scratchpad_state = SCRATCHPAD_CHANGED;
909 /* Workspace change will already result in a tree_render. */
911 render_con(con, false);
918 * Sets size of the CT_FLOATING_CON to specified dimensions. Might limit the
919 * actual size with regard to size constraints taken from user settings.
920 * Additionally, the dimensions may be upscaled until they're divisible by the
921 * window's size hints.
924 void floating_resize(Con *floating_con, int x, int y) {
925 DLOG("floating resize to %dx%d px\n", x, y);
926 Rect *rect = &floating_con->rect;
927 Con *focused_con = con_descend_focused(floating_con);
928 if (focused_con->window == NULL) {
929 DLOG("No window is focused. Not resizing.\n");
932 int wi = focused_con->window->width_increment;
933 int hi = focused_con->window->height_increment;
937 rect->width += (wi - 1 - rect->width) % wi;
939 rect->height += (hi - 1 - rect->height) % hi;
941 floating_check_size(floating_con);
943 /* If this is a scratchpad window, don't auto center it from now on. */
944 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
945 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
949 * Fixes the coordinates of the floating window whenever the window gets
950 * reassigned to a different output (or when the output’s rect changes).
953 void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect) {
954 DLOG("Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
955 con, con->rect.x, con->rect.y, con->rect.width, con->rect.height);
956 DLOG("old_rect = (%d, %d), %d x %d\n",
957 old_rect->x, old_rect->y, old_rect->width, old_rect->height);
958 DLOG("new_rect = (%d, %d), %d x %d\n",
959 new_rect->x, new_rect->y, new_rect->width, new_rect->height);
960 /* First we get the x/y coordinates relative to the x/y coordinates
961 * of the output on which the window is on */
962 int32_t rel_x = con->rect.x - old_rect->x + (int32_t)(con->rect.width / 2);
963 int32_t rel_y = con->rect.y - old_rect->y + (int32_t)(con->rect.height / 2);
964 /* Then we calculate a fraction, for example 0.63 for a window
965 * which is at y = 1212 of a 1920 px high output */
966 DLOG("rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
967 rel_x, rel_y, (double)rel_x / old_rect->width, (double)rel_y / old_rect->height,
968 old_rect->width, old_rect->height);
969 /* Here we have to multiply at first. Or we will lose precision when not compiled with -msse2 */
970 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);
971 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);
972 DLOG("Resulting coordinates: x = %d, y = %d\n", con->rect.x, con->rect.y);