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 /* obey size increments */
76 if (focused_con->window != NULL && (focused_con->window->height_increment || focused_con->window->width_increment)) {
77 Rect border_rect = con_border_style_rect(focused_con);
79 /* We have to do the opposite calculations that render_con() do
80 * to get the exact size we want. */
81 border_rect.width = -border_rect.width;
82 border_rect.width += 2 * focused_con->border_width;
83 border_rect.height = -border_rect.height;
84 border_rect.height += 2 * focused_con->border_width;
85 if (con_border_style(focused_con) == BS_NORMAL)
86 border_rect.height += render_deco_height();
88 if (focused_con->window->height_increment &&
89 floating_con->rect.height >= focused_con->window->base_height + border_rect.height) {
90 floating_con->rect.height -= focused_con->window->base_height + border_rect.height;
91 floating_con->rect.height -= floating_con->rect.height % focused_con->window->height_increment;
92 floating_con->rect.height += focused_con->window->base_height + border_rect.height;
95 if (focused_con->window->width_increment &&
96 floating_con->rect.width >= focused_con->window->base_width + border_rect.width) {
97 floating_con->rect.width -= focused_con->window->base_width + border_rect.width;
98 floating_con->rect.width -= floating_con->rect.width % focused_con->window->width_increment;
99 floating_con->rect.width += focused_con->window->base_width + border_rect.width;
103 /* Unless user requests otherwise (-1), ensure width/height do not exceed
104 * configured maxima or, if unconfigured, limit to combined width of all
106 if (config.floating_minimum_height != -1) {
107 if (config.floating_minimum_height == 0)
108 floating_con->rect.height = max(floating_con->rect.height, floating_sane_min_height);
110 floating_con->rect.height = max(floating_con->rect.height, config.floating_minimum_height);
112 if (config.floating_minimum_width != -1) {
113 if (config.floating_minimum_width == 0)
114 floating_con->rect.width = max(floating_con->rect.width, floating_sane_min_width);
116 floating_con->rect.width = max(floating_con->rect.width, config.floating_minimum_width);
119 /* Unless user requests otherwise (-1), raise the width/height to
120 * reasonable minimum dimensions */
121 floating_sane_max_dimensions = total_outputs_dimensions();
122 if (config.floating_maximum_height != -1) {
123 if (config.floating_maximum_height == 0)
124 floating_con->rect.height = min(floating_con->rect.height, floating_sane_max_dimensions.height);
126 floating_con->rect.height = min(floating_con->rect.height, config.floating_maximum_height);
128 if (config.floating_maximum_width != -1) {
129 if (config.floating_maximum_width == 0)
130 floating_con->rect.width = min(floating_con->rect.width, floating_sane_max_dimensions.width);
132 floating_con->rect.width = min(floating_con->rect.width, config.floating_maximum_width);
136 void floating_enable(Con *con, bool automatic) {
137 bool set_focus = (con == focused);
139 if (con_is_docked(con)) {
140 LOG("Container is a dock window, not enabling floating mode.\n");
144 if (con_is_floating(con)) {
145 LOG("Container is already in floating mode, not doing anything.\n");
149 if (con->type == CT_WORKSPACE) {
150 LOG("Container is a workspace, not enabling floating mode.\n");
154 /* 1: detach the container from its parent */
155 /* TODO: refactor this with tree_close_internal() */
156 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
157 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
159 con_fix_percent(con->parent);
161 /* 2: create a new container to render the decoration on, add
162 * it as a floating window to the workspace */
163 Con *nc = con_new(NULL, NULL);
164 /* we need to set the parent afterwards instead of passing it as an
165 * argument to con_new() because nc would be inserted into the tiling layer
167 Con *ws = con_get_workspace(con);
169 nc->type = CT_FLOATING_CON;
170 nc->layout = L_SPLITH;
171 /* We insert nc already, even though its rect is not yet calculated. This
172 * is necessary because otherwise the workspace might be empty (and get
173 * closed in tree_close_internal()) even though it’s not. */
174 TAILQ_INSERT_TAIL(&(ws->floating_head), nc, floating_windows);
175 TAILQ_INSERT_TAIL(&(ws->focus_head), nc, focused);
177 /* check if the parent container is empty and close it if so */
178 if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) &&
179 con_num_children(con->parent) == 0) {
180 DLOG("Old container empty after setting this child to floating, closing\n");
181 tree_close_internal(con->parent, DONT_KILL_WINDOW, false, false);
185 sasprintf(&name, "[i3 con] floatingcon around %p", con);
186 x_set_name(nc, name);
189 /* find the height for the decorations */
190 int deco_height = render_deco_height();
192 DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
193 DLOG("Geometry = (%d, %d) with %d x %d\n", con->geometry.x, con->geometry.y, con->geometry.width, con->geometry.height);
194 Rect zero = {0, 0, 0, 0};
195 nc->rect = con->geometry;
196 /* If the geometry was not set (split containers), we need to determine a
197 * sensible one by combining the geometry of all children */
198 if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
199 DLOG("Geometry not set, combining children\n");
201 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
202 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
203 nc->rect.width += child->geometry.width;
204 nc->rect.height = max(nc->rect.height, child->geometry.height);
208 floating_check_size(nc);
210 /* 3: attach the child to the new parent container. We need to do this
211 * because con_border_style_rect() needs to access con->parent. */
214 con->floating = FLOATING_USER_ON;
216 /* 4: set the border style as specified with new_float */
218 con->border_style = config.default_floating_border;
220 /* Add pixels for the decoration. */
221 Rect border_style_rect = con_border_style_rect(con);
223 nc->rect.height -= border_style_rect.height;
224 nc->rect.width -= border_style_rect.width;
226 /* Add some more pixels for the title bar */
227 if (con_border_style(con) == BS_NORMAL)
228 nc->rect.height += deco_height;
230 /* Honor the X11 border */
231 nc->rect.height += con->border_width * 2;
232 nc->rect.width += con->border_width * 2;
234 /* Some clients (like GIMP’s color picker window) get mapped
235 * to (0, 0), so we push them to a reasonable position
236 * (centered over their leader) */
237 if (nc->rect.x == 0 && nc->rect.y == 0) {
239 if (con->window && con->window->leader != XCB_NONE &&
240 (leader = con_by_window_id(con->window->leader)) != NULL) {
241 DLOG("Centering above leader\n");
242 floating_center(nc, leader->rect);
244 /* center the window on workspace as fallback */
245 floating_center(nc, ws->rect);
249 /* Sanity check: Are the coordinates on the appropriate output? If not, we
250 * need to change them */
251 Output *current_output = get_output_containing(nc->rect.x +
252 (nc->rect.width / 2),
253 nc->rect.y + (nc->rect.height / 2));
255 Con *correct_output = con_get_output(ws);
256 if (!current_output || current_output->con != correct_output) {
257 DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
258 nc->rect.x, nc->rect.y);
260 /* If moving from one output to another, keep the relative position
261 * consistent (e.g. a centered dialog will remain centered). */
263 floating_fix_coordinates(nc, ¤t_output->con->rect, &correct_output->rect);
265 nc->rect.x = correct_output->rect.x;
266 nc->rect.y = correct_output->rect.y;
270 DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
272 /* 5: Subtract the deco_height in order to make the floating window appear
273 * at precisely the position it specified in its original geometry (which
274 * is what applications might remember). */
275 deco_height = (con->border_style == BS_NORMAL ? render_deco_height() : 0);
276 nc->rect.y -= deco_height;
278 DLOG("Corrected y = %d (deco_height = %d)\n", nc->rect.y, deco_height);
280 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
281 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
283 /* render the cons to get initial window_rect correct */
284 render_con(nc, false);
285 render_con(con, false);
290 /* Check if we need to re-assign it to a different workspace because of its
291 * coordinates and exit if that was done successfully. */
292 if (floating_maybe_reassign_ws(nc)) {
296 /* Sanitize coordinates: Check if they are on any output */
297 if (get_output_containing(nc->rect.x, nc->rect.y) != NULL) {
301 ELOG("No output found at destination coordinates, centering floating window on current ws\n");
302 floating_center(nc, ws->rect);
305 floating_set_hint_atom(nc, true);
306 ipc_send_window_event("floating", con);
309 void floating_disable(Con *con, bool automatic) {
310 if (!con_is_floating(con)) {
311 LOG("Container isn't floating, not doing anything.\n");
315 const bool set_focus = (con == focused);
317 Con *ws = con_get_workspace(con);
319 /* 1: detach from parent container */
320 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
321 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
323 /* 2: kill parent container */
324 TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
325 TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
326 tree_close_internal(con->parent, DONT_KILL_WINDOW, true, false);
328 /* 3: re-attach to the parent of the currently focused con on the workspace
329 * this floating con was on */
330 Con *focused = con_descend_tiling_focused(ws);
332 /* if there is no other container on this workspace, focused will be the
333 * workspace itself */
334 if (focused->type == CT_WORKSPACE)
335 con->parent = focused;
337 con->parent = focused->parent;
339 /* con_fix_percent will adjust the percent value */
342 con->floating = FLOATING_USER_OFF;
344 con_attach(con, con->parent, false);
346 con_fix_percent(con->parent);
351 floating_set_hint_atom(con, false);
352 ipc_send_window_event("floating", con);
356 * Toggles floating mode for the given container.
358 * If the automatic flag is set to true, this was an automatic update by a change of the
359 * window class from the application which can be overwritten by the user.
362 void toggle_floating_mode(Con *con, bool automatic) {
363 /* forbid the command to toggle floating on a CT_FLOATING_CON */
364 if (con->type == CT_FLOATING_CON) {
365 ELOG("Cannot toggle floating mode on con = %p because it is of type CT_FLOATING_CON.\n", con);
369 /* see if the client is already floating */
370 if (con_is_floating(con)) {
371 LOG("already floating, re-setting to tiling\n");
373 floating_disable(con, automatic);
377 floating_enable(con, automatic);
381 * Raises the given container in the list of floating containers
384 void floating_raise_con(Con *con) {
385 DLOG("Raising floating con %p / %s\n", con, con->name);
386 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
387 TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
391 * Checks if con’s coordinates are within its workspace and re-assigns it to
392 * the actual workspace if not.
395 bool floating_maybe_reassign_ws(Con *con) {
396 Output *output = get_output_containing(
397 con->rect.x + (con->rect.width / 2),
398 con->rect.y + (con->rect.height / 2));
401 ELOG("No output found at destination coordinates?\n");
405 if (con_get_output(con) == output->con) {
406 DLOG("still the same ws\n");
410 DLOG("Need to re-assign!\n");
412 Con *content = output_get_content(output->con);
413 Con *ws = TAILQ_FIRST(&(content->focus_head));
414 DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
415 con_move_to_workspace(con, ws, false, true, false);
416 con_focus(con_descend_focused(con));
421 * Centers a floating con above the specified rect.
424 void floating_center(Con *con, Rect rect) {
425 con->rect.x = rect.x + (rect.width / 2) - (con->rect.width / 2);
426 con->rect.y = rect.y + (rect.height / 2) - (con->rect.height / 2);
430 * Moves the given floating con to the current pointer position.
433 void floating_move_to_pointer(Con *con) {
434 assert(con->type == CT_FLOATING_CON);
436 xcb_query_pointer_reply_t *reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
438 ELOG("could not query pointer position, not moving this container\n");
442 Output *output = get_output_containing(reply->root_x, reply->root_y);
443 if (output == NULL) {
444 ELOG("The pointer is not on any output, cannot move the container here.\n");
448 /* Determine where to put the window. */
449 int32_t x = reply->root_x - con->rect.width / 2;
450 int32_t y = reply->root_y - con->rect.height / 2;
453 /* Correct target coordinates to be in-bounds. */
454 x = MAX(x, (int32_t)output->rect.x);
455 y = MAX(y, (int32_t)output->rect.y);
456 if (x + con->rect.width > output->rect.x + output->rect.width)
457 x = output->rect.x + output->rect.width - con->rect.width;
458 if (y + con->rect.height > output->rect.y + output->rect.height)
459 y = output->rect.y + output->rect.height - con->rect.height;
461 /* Update container's coordinates to position it correctly. */
462 floating_reposition(con, (Rect){x, y, con->rect.width, con->rect.height});
465 DRAGGING_CB(drag_window_callback) {
466 const struct xcb_button_press_event_t *event = extra;
468 /* Reposition the client correctly while moving */
469 con->rect.x = old_rect->x + (new_x - event->root_x);
470 con->rect.y = old_rect->y + (new_y - event->root_y);
472 render_con(con, false);
476 /* Check if we cross workspace boundaries while moving */
477 if (!floating_maybe_reassign_ws(con))
479 /* Ensure not to warp the pointer while dragging */
485 * Called when the user clicked on the titlebar of a floating window.
486 * Calls the drag_pointer function with the drag_window callback
489 void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
490 DLOG("floating_drag_window\n");
492 /* Push changes before dragging, so that the window gets raised now and not
493 * after the user releases the mouse button */
496 /* Store the initial rect in case of user revert/cancel */
497 Rect initial_rect = con->rect;
499 /* Drag the window */
500 drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, XCURSOR_CURSOR_MOVE, drag_window_callback, event);
502 /* If the user cancelled, undo the changes. */
503 if (drag_result == DRAG_REVERT)
504 floating_reposition(con, initial_rect);
506 /* If this is a scratchpad window, don't auto center it from now on. */
507 if (con->scratchpad_state == SCRATCHPAD_FRESH)
508 con->scratchpad_state = SCRATCHPAD_CHANGED;
514 * This is an ugly data structure which we need because there is no standard
515 * way of having nested functions (only available as a gcc extension at the
516 * moment, clang doesn’t support it) or blocks (only available as a clang
517 * extension and only on Mac OS X systems at the moment).
520 struct resize_window_callback_params {
521 const border_t corner;
522 const bool proportional;
523 const xcb_button_press_event_t *event;
526 DRAGGING_CB(resize_window_callback) {
527 const struct resize_window_callback_params *params = extra;
528 const xcb_button_press_event_t *event = params->event;
529 border_t corner = params->corner;
531 int32_t dest_x = con->rect.x;
532 int32_t dest_y = con->rect.y;
534 uint32_t dest_height;
536 double ratio = (double)old_rect->width / old_rect->height;
538 /* First guess: We resize by exactly the amount the mouse moved,
539 * taking into account in which corner the client was grabbed */
540 if (corner & BORDER_LEFT)
541 dest_width = old_rect->width - (new_x - event->root_x);
543 dest_width = old_rect->width + (new_x - event->root_x);
545 if (corner & BORDER_TOP)
546 dest_height = old_rect->height - (new_y - event->root_y);
548 dest_height = old_rect->height + (new_y - event->root_y);
550 /* User wants to keep proportions, so we may have to adjust our values */
551 if (params->proportional) {
552 dest_width = max(dest_width, (int)(dest_height * ratio));
553 dest_height = max(dest_height, (int)(dest_width / ratio));
556 con->rect = (Rect){dest_x, dest_y, dest_width, dest_height};
558 /* Obey window size */
559 floating_check_size(con);
561 /* If not the lower right corner is grabbed, we must also reposition
562 * the client by exactly the amount we resized it */
563 if (corner & BORDER_LEFT)
564 dest_x = old_rect->x + (old_rect->width - con->rect.width);
566 if (corner & BORDER_TOP)
567 dest_y = old_rect->y + (old_rect->height - con->rect.height);
569 con->rect.x = dest_x;
570 con->rect.y = dest_y;
572 /* TODO: don’t re-render the whole tree just because we change
573 * coordinates of a floating window */
575 x_push_changes(croot);
579 * Called when the user clicked on a floating window while holding the
580 * floating_modifier and the right mouse button.
581 * Calls the drag_pointer function with the resize_window callback
584 void floating_resize_window(Con *con, const bool proportional,
585 const xcb_button_press_event_t *event) {
586 DLOG("floating_resize_window\n");
588 /* corner saves the nearest corner to the original click. It contains
589 * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
592 if (event->event_x <= (int16_t)(con->rect.width / 2))
593 corner |= BORDER_LEFT;
595 corner |= BORDER_RIGHT;
598 if (event->event_y <= (int16_t)(con->rect.height / 2)) {
599 corner |= BORDER_TOP;
600 cursor = (corner & BORDER_LEFT) ? XCURSOR_CURSOR_TOP_LEFT_CORNER : XCURSOR_CURSOR_TOP_RIGHT_CORNER;
602 corner |= BORDER_BOTTOM;
603 cursor = (corner & BORDER_LEFT) ? XCURSOR_CURSOR_BOTTOM_LEFT_CORNER : XCURSOR_CURSOR_BOTTOM_RIGHT_CORNER;
606 struct resize_window_callback_params params = {corner, proportional, event};
608 /* get the initial rect in case of revert/cancel */
609 Rect initial_rect = con->rect;
611 drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, cursor, resize_window_callback, ¶ms);
613 /* If the user cancels, undo the resize */
614 if (drag_result == DRAG_REVERT)
615 floating_reposition(con, initial_rect);
617 /* If this is a scratchpad window, don't auto center it from now on. */
618 if (con->scratchpad_state == SCRATCHPAD_FRESH)
619 con->scratchpad_state = SCRATCHPAD_CHANGED;
622 /* Custom data structure used to track dragging-related events. */
626 /* Whether this modal event loop should be exited and with which result. */
627 drag_result_t result;
629 /* The container that is being dragged or resized, or NULL if this is a
630 * drag of the resize handle. */
633 /* The dimensions of con when the loop was started. */
636 /* The callback to invoke after every pointer movement. */
639 /* User data pointer for callback. */
643 static void xcb_drag_check_cb(EV_P_ ev_check *w, int revents) {
644 struct drag_x11_cb *dragloop = (struct drag_x11_cb *)w->data;
645 xcb_motion_notify_event_t *last_motion_notify = NULL;
646 xcb_generic_event_t *event;
648 while ((event = xcb_poll_for_event(conn)) != NULL) {
649 if (event->response_type == 0) {
650 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
651 DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
652 error->sequence, error->error_code);
657 /* Strip off the highest bit (set if the event is generated) */
658 int type = (event->response_type & 0x7F);
661 case XCB_BUTTON_RELEASE:
662 dragloop->result = DRAG_SUCCESS;
666 DLOG("A key was pressed during drag, reverting changes.\n");
667 dragloop->result = DRAG_REVERT;
668 handle_event(type, event);
671 case XCB_UNMAP_NOTIFY: {
672 xcb_unmap_notify_event_t *unmap_event = (xcb_unmap_notify_event_t *)event;
673 Con *con = con_by_window_id(unmap_event->window);
676 DLOG("UnmapNotify for window 0x%08x (container %p)\n", unmap_event->window, con);
678 if (con_get_workspace(con) == con_get_workspace(focused)) {
679 DLOG("UnmapNotify for a managed window on the current workspace, aborting\n");
680 dragloop->result = DRAG_ABORT;
684 handle_event(type, event);
688 case XCB_MOTION_NOTIFY:
689 /* motion_notify events are saved for later */
690 FREE(last_motion_notify);
691 last_motion_notify = (xcb_motion_notify_event_t *)event;
695 DLOG("Passing to original handler\n");
696 handle_event(type, event);
700 if (last_motion_notify != (xcb_motion_notify_event_t *)event)
703 if (dragloop->result != DRAGGING)
707 if (last_motion_notify == NULL)
712 &(dragloop->old_rect),
713 last_motion_notify->root_x,
714 last_motion_notify->root_y,
716 free(last_motion_notify);
720 * This function grabs your pointer and keyboard and lets you drag stuff around
721 * (borders). Every time you move your mouse, an XCB_MOTION_NOTIFY event will
722 * be received and the given callback will be called with the parameters
723 * specified (client, border on which the click originally was), the original
724 * rect of the client, the event and the new coordinates (x, y).
727 drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t
729 border_t border, int cursor, callback_t callback, const void *extra) {
730 xcb_cursor_t xcursor = (cursor && xcursor_supported) ? xcursor_get_cursor(cursor) : XCB_NONE;
732 /* Grab the pointer */
733 xcb_grab_pointer_cookie_t cookie;
734 xcb_grab_pointer_reply_t *reply;
735 xcb_generic_error_t *error;
737 cookie = xcb_grab_pointer(conn,
738 false, /* get all pointer events specified by the following mask */
739 root, /* grab the root window */
740 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
741 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
742 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
743 confine_to, /* confine_to = in which window should the cursor stay */
744 xcursor, /* possibly display a special cursor */
747 if ((reply = xcb_grab_pointer_reply(conn, cookie, &error)) == NULL) {
748 ELOG("Could not grab pointer (error_code = %d)\n", error->error_code);
755 /* Grab the keyboard */
756 xcb_grab_keyboard_cookie_t keyb_cookie;
757 xcb_grab_keyboard_reply_t *keyb_reply;
759 keyb_cookie = xcb_grab_keyboard(conn,
760 false, /* get all keyboard events */
761 root, /* grab the root window */
763 XCB_GRAB_MODE_ASYNC, /* continue processing pointer events as normal */
764 XCB_GRAB_MODE_ASYNC /* keyboard mode */
767 if ((keyb_reply = xcb_grab_keyboard_reply(conn, keyb_cookie, &error)) == NULL) {
768 ELOG("Could not grab keyboard (error_code = %d)\n", error->error_code);
770 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
776 /* Go into our own event loop */
777 struct drag_x11_cb loop = {
780 .callback = callback,
783 ev_check *check = &loop.check;
785 loop.old_rect = con->rect;
786 ev_check_init(check, xcb_drag_check_cb);
788 main_set_x11_cb(false);
789 ev_check_start(main_loop, check);
791 while (loop.result == DRAGGING)
792 ev_run(main_loop, EVRUN_ONCE);
794 ev_check_stop(main_loop, check);
795 main_set_x11_cb(true);
797 xcb_ungrab_keyboard(conn, XCB_CURRENT_TIME);
798 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
805 * Repositions the CT_FLOATING_CON to have the coordinates specified by
806 * newrect, but only if the coordinates are not out-of-bounds. Also reassigns
807 * the floating con to a different workspace if this move was across different
811 void floating_reposition(Con *con, Rect newrect) {
812 /* Sanity check: Are the new coordinates on any output? If not, we
813 * ignore that request. */
814 if (!contained_by_output(newrect)) {
815 ELOG("No output found at destination coordinates. Not repositioning.\n");
821 floating_maybe_reassign_ws(con);
823 /* If this is a scratchpad window, don't auto center it from now on. */
824 if (con->scratchpad_state == SCRATCHPAD_FRESH)
825 con->scratchpad_state = SCRATCHPAD_CHANGED;
831 * Sets size of the CT_FLOATING_CON to specified dimensions. Might limit the
832 * actual size with regard to size constraints taken from user settings.
833 * Additionally, the dimensions may be upscaled until they're divisible by the
834 * window's size hints.
837 void floating_resize(Con *floating_con, int x, int y) {
838 DLOG("floating resize to %dx%d px\n", x, y);
839 Rect *rect = &floating_con->rect;
840 Con *focused_con = con_descend_focused(floating_con);
841 if (focused_con->window == NULL) {
842 DLOG("No window is focused. Not resizing.\n");
845 int wi = focused_con->window->width_increment;
846 int hi = focused_con->window->height_increment;
850 rect->width += (wi - 1 - rect->width) % wi;
852 rect->height += (hi - 1 - rect->height) % hi;
854 floating_check_size(floating_con);
856 /* If this is a scratchpad window, don't auto center it from now on. */
857 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
858 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
862 * Fixes the coordinates of the floating window whenever the window gets
863 * reassigned to a different output (or when the output’s rect changes).
866 void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect) {
867 DLOG("Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
868 con, con->rect.x, con->rect.y, con->rect.width, con->rect.height);
869 DLOG("old_rect = (%d, %d), %d x %d\n",
870 old_rect->x, old_rect->y, old_rect->width, old_rect->height);
871 DLOG("new_rect = (%d, %d), %d x %d\n",
872 new_rect->x, new_rect->y, new_rect->width, new_rect->height);
873 /* First we get the x/y coordinates relative to the x/y coordinates
874 * of the output on which the window is on */
875 int32_t rel_x = con->rect.x - old_rect->x + (int32_t)(con->rect.width / 2);
876 int32_t rel_y = con->rect.y - old_rect->y + (int32_t)(con->rect.height / 2);
877 /* Then we calculate a fraction, for example 0.63 for a window
878 * which is at y = 1212 of a 1920 px high output */
879 DLOG("rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
880 rel_x, rel_y, (double)rel_x / old_rect->width, (double)rel_y / old_rect->height,
881 old_rect->width, old_rect->height);
882 /* Here we have to multiply at first. Or we will lose precision when not compiled with -msse2 */
883 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);
884 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);
885 DLOG("Resulting coordinates: x = %d, y = %d\n", con->rect.x, con->rect.y);