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 if (focused_con->window != NULL) {
87 if (focused_con->window->min_width) {
88 floating_con->rect.width -= border_rect.width;
89 floating_con->rect.width = max(floating_con->rect.width, focused_con->window->min_width);
90 floating_con->rect.width += border_rect.width;
93 if (focused_con->window->min_height) {
94 floating_con->rect.height -= border_rect.height;
95 floating_con->rect.height = max(floating_con->rect.height, focused_con->window->min_height);
96 floating_con->rect.height += border_rect.height;
99 if (focused_con->window->height_increment &&
100 floating_con->rect.height >= focused_con->window->base_height + border_rect.height) {
101 floating_con->rect.height -= focused_con->window->base_height + border_rect.height;
102 floating_con->rect.height -= floating_con->rect.height % focused_con->window->height_increment;
103 floating_con->rect.height += focused_con->window->base_height + border_rect.height;
106 if (focused_con->window->width_increment &&
107 floating_con->rect.width >= focused_con->window->base_width + border_rect.width) {
108 floating_con->rect.width -= focused_con->window->base_width + border_rect.width;
109 floating_con->rect.width -= floating_con->rect.width % focused_con->window->width_increment;
110 floating_con->rect.width += focused_con->window->base_width + border_rect.width;
114 /* Unless user requests otherwise (-1), raise the width/height to
115 * reasonable minimum dimensions */
116 if (config.floating_minimum_height != -1) {
117 floating_con->rect.height -= border_rect.height;
118 if (config.floating_minimum_height == 0) {
119 floating_con->rect.height = max(floating_con->rect.height, floating_sane_min_height);
121 floating_con->rect.height = max(floating_con->rect.height, config.floating_minimum_height);
123 floating_con->rect.height += border_rect.height;
126 if (config.floating_minimum_width != -1) {
127 floating_con->rect.width -= border_rect.width;
128 if (config.floating_minimum_width == 0) {
129 floating_con->rect.width = max(floating_con->rect.width, floating_sane_min_width);
131 floating_con->rect.width = max(floating_con->rect.width, config.floating_minimum_width);
133 floating_con->rect.width += border_rect.width;
136 /* Unless user requests otherwise (-1), ensure width/height do not exceed
137 * configured maxima or, if unconfigured, limit to combined width of all
139 floating_sane_max_dimensions = total_outputs_dimensions();
140 if (config.floating_maximum_height != -1) {
141 floating_con->rect.height -= border_rect.height;
142 if (config.floating_maximum_height == 0) {
143 floating_con->rect.height = min(floating_con->rect.height, floating_sane_max_dimensions.height);
145 floating_con->rect.height = min(floating_con->rect.height, config.floating_maximum_height);
147 floating_con->rect.height += border_rect.height;
150 if (config.floating_maximum_width != -1) {
151 floating_con->rect.width -= border_rect.width;
152 if (config.floating_maximum_width == 0) {
153 floating_con->rect.width = min(floating_con->rect.width, floating_sane_max_dimensions.width);
155 floating_con->rect.width = min(floating_con->rect.width, config.floating_maximum_width);
157 floating_con->rect.width += border_rect.width;
161 void floating_enable(Con *con, bool automatic) {
162 bool set_focus = (con == focused);
164 if (con_is_docked(con)) {
165 LOG("Container is a dock window, not enabling floating mode.\n");
169 if (con_is_floating(con)) {
170 LOG("Container is already in floating mode, not doing anything.\n");
174 if (con->type == CT_WORKSPACE) {
175 LOG("Container is a workspace, not enabling floating mode.\n");
179 /* 1: detach the container from its parent */
180 /* TODO: refactor this with tree_close_internal() */
181 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
182 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
184 con_fix_percent(con->parent);
186 /* 2: create a new container to render the decoration on, add
187 * it as a floating window to the workspace */
188 Con *nc = con_new(NULL, NULL);
189 /* we need to set the parent afterwards instead of passing it as an
190 * argument to con_new() because nc would be inserted into the tiling layer
192 Con *ws = con_get_workspace(con);
194 nc->type = CT_FLOATING_CON;
195 nc->layout = L_SPLITH;
196 /* We insert nc already, even though its rect is not yet calculated. This
197 * is necessary because otherwise the workspace might be empty (and get
198 * closed in tree_close_internal()) even though it’s not. */
200 TAILQ_INSERT_TAIL(&(ws->floating_head), nc, floating_windows);
202 TAILQ_INSERT_HEAD(&(ws->floating_head), nc, floating_windows);
204 TAILQ_INSERT_TAIL(&(ws->focus_head), nc, focused);
206 /* check if the parent container is empty and close it if so */
207 if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) &&
208 con_num_children(con->parent) == 0) {
209 DLOG("Old container empty after setting this child to floating, closing\n");
210 Con *parent = con->parent;
211 /* clear the pointer before calling tree_close_internal in which the memory is freed */
213 tree_close_internal(parent, DONT_KILL_WINDOW, false, false);
217 sasprintf(&name, "[i3 con] floatingcon around %p", con);
218 x_set_name(nc, name);
221 /* find the height for the decorations */
222 int deco_height = render_deco_height();
224 DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
225 DLOG("Geometry = (%d, %d) with %d x %d\n", con->geometry.x, con->geometry.y, con->geometry.width, con->geometry.height);
226 Rect zero = {0, 0, 0, 0};
227 nc->rect = con->geometry;
228 /* If the geometry was not set (split containers), we need to determine a
229 * sensible one by combining the geometry of all children */
230 if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
231 DLOG("Geometry not set, combining children\n");
233 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
234 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
235 nc->rect.width += child->geometry.width;
236 nc->rect.height = max(nc->rect.height, child->geometry.height);
240 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
241 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
243 /* 3: attach the child to the new parent container. We need to do this
244 * because con_border_style_rect() needs to access con->parent. */
247 con->floating = FLOATING_USER_ON;
249 /* 4: set the border style as specified with new_float */
251 con->border_style = config.default_floating_border;
253 /* Add pixels for the decoration. */
254 Rect border_style_rect = con_border_style_rect(con);
256 nc->rect.height -= border_style_rect.height;
257 nc->rect.width -= border_style_rect.width;
259 /* Add some more pixels for the title bar */
260 if (con_border_style(con) == BS_NORMAL) {
261 nc->rect.height += deco_height;
264 /* Honor the X11 border */
265 nc->rect.height += con->border_width * 2;
266 nc->rect.width += con->border_width * 2;
268 floating_check_size(nc);
270 /* Some clients (like GIMP’s color picker window) get mapped
271 * to (0, 0), so we push them to a reasonable position
272 * (centered over their leader) */
273 if (nc->rect.x == 0 && nc->rect.y == 0) {
275 if (con->window && con->window->leader != XCB_NONE &&
276 (leader = con_by_window_id(con->window->leader)) != NULL) {
277 DLOG("Centering above leader\n");
278 floating_center(nc, leader->rect);
280 /* center the window on workspace as fallback */
281 floating_center(nc, ws->rect);
285 /* Sanity check: Are the coordinates on the appropriate output? If not, we
286 * need to change them */
287 Output *current_output = get_output_from_rect(nc->rect);
288 Con *correct_output = con_get_output(ws);
289 if (!current_output || current_output->con != correct_output) {
290 DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
291 nc->rect.x, nc->rect.y);
293 /* If moving from one output to another, keep the relative position
294 * consistent (e.g. a centered dialog will remain centered). */
295 if (current_output) {
296 floating_fix_coordinates(nc, ¤t_output->con->rect, &correct_output->rect);
297 /* Make sure that the result is in the correct output. */
298 current_output = get_output_from_rect(nc->rect);
300 if (!current_output || current_output->con != correct_output) {
301 floating_center(nc, ws->rect);
305 DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
307 /* 5: Subtract the deco_height in order to make the floating window appear
308 * at precisely the position it specified in its original geometry (which
309 * is what applications might remember). */
310 deco_height = (con->border_style == BS_NORMAL ? render_deco_height() : 0);
311 nc->rect.y -= deco_height;
313 DLOG("Corrected y = %d (deco_height = %d)\n", nc->rect.y, deco_height);
315 /* render the cons to get initial window_rect correct */
316 render_con(nc, false);
317 render_con(con, false);
322 floating_set_hint_atom(nc, true);
323 ipc_send_window_event("floating", con);
326 void floating_disable(Con *con, bool automatic) {
327 if (!con_is_floating(con)) {
328 LOG("Container isn't floating, not doing anything.\n");
332 const bool set_focus = (con == focused);
334 Con *ws = con_get_workspace(con);
335 Con *parent = con->parent;
337 /* 1: detach from parent container */
338 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
339 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
341 /* 2: kill parent container */
342 TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
343 TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
344 /* clear the pointer before calling tree_close_internal in which the memory is freed */
346 tree_close_internal(parent, DONT_KILL_WINDOW, true, false);
348 /* 3: re-attach to the parent of the currently focused con on the workspace
349 * this floating con was on */
350 Con *focused = con_descend_tiling_focused(ws);
352 /* if there is no other container on this workspace, focused will be the
353 * workspace itself */
354 if (focused->type == CT_WORKSPACE)
355 con->parent = focused;
357 con->parent = focused->parent;
359 /* con_fix_percent will adjust the percent value */
362 con->floating = FLOATING_USER_OFF;
364 con_attach(con, con->parent, false);
366 con_fix_percent(con->parent);
371 floating_set_hint_atom(con, false);
372 ipc_send_window_event("floating", con);
376 * Toggles floating mode for the given container.
378 * If the automatic flag is set to true, this was an automatic update by a change of the
379 * window class from the application which can be overwritten by the user.
382 void toggle_floating_mode(Con *con, bool automatic) {
383 /* forbid the command to toggle floating on a CT_FLOATING_CON */
384 if (con->type == CT_FLOATING_CON) {
385 ELOG("Cannot toggle floating mode on con = %p because it is of type CT_FLOATING_CON.\n", con);
389 /* see if the client is already floating */
390 if (con_is_floating(con)) {
391 LOG("already floating, re-setting to tiling\n");
393 floating_disable(con, automatic);
397 floating_enable(con, automatic);
401 * Raises the given container in the list of floating containers
404 void floating_raise_con(Con *con) {
405 DLOG("Raising floating con %p / %s\n", con, con->name);
406 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
407 TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
411 * Checks if con’s coordinates are within its workspace and re-assigns it to
412 * the actual workspace if not.
415 bool floating_maybe_reassign_ws(Con *con) {
416 Output *output = get_output_from_rect(con->rect);
419 ELOG("No output found at destination coordinates?\n");
423 if (con_get_output(con) == output->con) {
424 DLOG("still the same ws\n");
428 DLOG("Need to re-assign!\n");
430 Con *content = output_get_content(output->con);
431 Con *ws = TAILQ_FIRST(&(content->focus_head));
432 DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
433 con_move_to_workspace(con, ws, false, true, false);
435 con_activate(con_descend_focused(con));
440 * Centers a floating con above the specified rect.
443 void floating_center(Con *con, Rect rect) {
444 con->rect.x = rect.x + (rect.width / 2) - (con->rect.width / 2);
445 con->rect.y = rect.y + (rect.height / 2) - (con->rect.height / 2);
449 * Moves the given floating con to the current pointer position.
452 void floating_move_to_pointer(Con *con) {
453 assert(con->type == CT_FLOATING_CON);
455 xcb_query_pointer_reply_t *reply = xcb_query_pointer_reply(conn, xcb_query_pointer(conn, root), NULL);
457 ELOG("could not query pointer position, not moving this container\n");
461 Output *output = get_output_containing(reply->root_x, reply->root_y);
462 if (output == NULL) {
463 ELOG("The pointer is not on any output, cannot move the container here.\n");
467 /* Determine where to put the window. */
468 int32_t x = reply->root_x - con->rect.width / 2;
469 int32_t y = reply->root_y - con->rect.height / 2;
472 /* Correct target coordinates to be in-bounds. */
473 x = MAX(x, (int32_t)output->rect.x);
474 y = MAX(y, (int32_t)output->rect.y);
475 if (x + con->rect.width > output->rect.x + output->rect.width)
476 x = output->rect.x + output->rect.width - con->rect.width;
477 if (y + con->rect.height > output->rect.y + output->rect.height)
478 y = output->rect.y + output->rect.height - con->rect.height;
480 /* Update container's coordinates to position it correctly. */
481 floating_reposition(con, (Rect){x, y, con->rect.width, con->rect.height});
484 DRAGGING_CB(drag_window_callback) {
485 const struct xcb_button_press_event_t *event = extra;
487 /* Reposition the client correctly while moving */
488 con->rect.x = old_rect->x + (new_x - event->root_x);
489 con->rect.y = old_rect->y + (new_y - event->root_y);
491 render_con(con, false);
495 /* Check if we cross workspace boundaries while moving */
496 if (!floating_maybe_reassign_ws(con))
498 /* Ensure not to warp the pointer while dragging */
504 * Called when the user clicked on the titlebar of a floating window.
505 * Calls the drag_pointer function with the drag_window callback
508 void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
509 DLOG("floating_drag_window\n");
511 /* Push changes before dragging, so that the window gets raised now and not
512 * after the user releases the mouse button */
515 /* Store the initial rect in case of user revert/cancel */
516 Rect initial_rect = con->rect;
518 /* Drag the window */
519 drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, XCURSOR_CURSOR_MOVE, drag_window_callback, event);
521 if (!con_exists(con)) {
522 DLOG("The container has been closed in the meantime.\n");
526 /* If the user cancelled, undo the changes. */
527 if (drag_result == DRAG_REVERT)
528 floating_reposition(con, initial_rect);
530 /* If this is a scratchpad window, don't auto center it from now on. */
531 if (con->scratchpad_state == SCRATCHPAD_FRESH)
532 con->scratchpad_state = SCRATCHPAD_CHANGED;
538 * This is an ugly data structure which we need because there is no standard
539 * way of having nested functions (only available as a gcc extension at the
540 * moment, clang doesn’t support it) or blocks (only available as a clang
541 * extension and only on Mac OS X systems at the moment).
544 struct resize_window_callback_params {
545 const border_t corner;
546 const bool proportional;
547 const xcb_button_press_event_t *event;
550 DRAGGING_CB(resize_window_callback) {
551 const struct resize_window_callback_params *params = extra;
552 const xcb_button_press_event_t *event = params->event;
553 border_t corner = params->corner;
555 int32_t dest_x = con->rect.x;
556 int32_t dest_y = con->rect.y;
558 uint32_t dest_height;
560 double ratio = (double)old_rect->width / old_rect->height;
562 /* First guess: We resize by exactly the amount the mouse moved,
563 * taking into account in which corner the client was grabbed */
564 if (corner & BORDER_LEFT)
565 dest_width = old_rect->width - (new_x - event->root_x);
567 dest_width = old_rect->width + (new_x - event->root_x);
569 if (corner & BORDER_TOP)
570 dest_height = old_rect->height - (new_y - event->root_y);
572 dest_height = old_rect->height + (new_y - event->root_y);
574 /* User wants to keep proportions, so we may have to adjust our values */
575 if (params->proportional) {
576 dest_width = max(dest_width, (int)(dest_height * ratio));
577 dest_height = max(dest_height, (int)(dest_width / ratio));
580 con->rect = (Rect){dest_x, dest_y, dest_width, dest_height};
582 /* Obey window size */
583 floating_check_size(con);
585 /* If not the lower right corner is grabbed, we must also reposition
586 * the client by exactly the amount we resized it */
587 if (corner & BORDER_LEFT)
588 dest_x = old_rect->x + (old_rect->width - con->rect.width);
590 if (corner & BORDER_TOP)
591 dest_y = old_rect->y + (old_rect->height - con->rect.height);
593 con->rect.x = dest_x;
594 con->rect.y = dest_y;
596 /* TODO: don’t re-render the whole tree just because we change
597 * coordinates of a floating window */
599 x_push_changes(croot);
603 * Called when the user clicked on a floating window while holding the
604 * floating_modifier and the right mouse button.
605 * Calls the drag_pointer function with the resize_window callback
608 void floating_resize_window(Con *con, const bool proportional,
609 const xcb_button_press_event_t *event) {
610 DLOG("floating_resize_window\n");
612 /* corner saves the nearest corner to the original click. It contains
613 * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
616 if (event->event_x <= (int16_t)(con->rect.width / 2))
617 corner |= BORDER_LEFT;
619 corner |= BORDER_RIGHT;
622 if (event->event_y <= (int16_t)(con->rect.height / 2)) {
623 corner |= BORDER_TOP;
624 cursor = (corner & BORDER_LEFT) ? XCURSOR_CURSOR_TOP_LEFT_CORNER : XCURSOR_CURSOR_TOP_RIGHT_CORNER;
626 corner |= BORDER_BOTTOM;
627 cursor = (corner & BORDER_LEFT) ? XCURSOR_CURSOR_BOTTOM_LEFT_CORNER : XCURSOR_CURSOR_BOTTOM_RIGHT_CORNER;
630 struct resize_window_callback_params params = {corner, proportional, event};
632 /* get the initial rect in case of revert/cancel */
633 Rect initial_rect = con->rect;
635 drag_result_t drag_result = drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, cursor, resize_window_callback, ¶ms);
637 if (!con_exists(con)) {
638 DLOG("The container has been closed in the meantime.\n");
642 /* If the user cancels, undo the resize */
643 if (drag_result == DRAG_REVERT)
644 floating_reposition(con, initial_rect);
646 /* If this is a scratchpad window, don't auto center it from now on. */
647 if (con->scratchpad_state == SCRATCHPAD_FRESH)
648 con->scratchpad_state = SCRATCHPAD_CHANGED;
651 /* Custom data structure used to track dragging-related events. */
655 /* Whether this modal event loop should be exited and with which result. */
656 drag_result_t result;
658 /* The container that is being dragged or resized, or NULL if this is a
659 * drag of the resize handle. */
662 /* The dimensions of con when the loop was started. */
665 /* The callback to invoke after every pointer movement. */
668 /* User data pointer for callback. */
672 static bool drain_drag_events(EV_P, struct drag_x11_cb *dragloop) {
673 xcb_motion_notify_event_t *last_motion_notify = NULL;
674 xcb_generic_event_t *event;
676 while ((event = xcb_poll_for_event(conn)) != NULL) {
677 if (event->response_type == 0) {
678 xcb_generic_error_t *error = (xcb_generic_error_t *)event;
679 DLOG("X11 Error received (probably harmless)! sequence 0x%x, error_code = %d\n",
680 error->sequence, error->error_code);
685 /* Strip off the highest bit (set if the event is generated) */
686 int type = (event->response_type & 0x7F);
689 case XCB_BUTTON_RELEASE:
690 dragloop->result = DRAG_SUCCESS;
694 DLOG("A key was pressed during drag, reverting changes.\n");
695 dragloop->result = DRAG_REVERT;
696 handle_event(type, event);
699 case XCB_UNMAP_NOTIFY: {
700 xcb_unmap_notify_event_t *unmap_event = (xcb_unmap_notify_event_t *)event;
701 Con *con = con_by_window_id(unmap_event->window);
704 DLOG("UnmapNotify for window 0x%08x (container %p)\n", unmap_event->window, con);
706 if (con_get_workspace(con) == con_get_workspace(focused)) {
707 DLOG("UnmapNotify for a managed window on the current workspace, aborting\n");
708 dragloop->result = DRAG_ABORT;
712 handle_event(type, event);
716 case XCB_MOTION_NOTIFY:
717 /* motion_notify events are saved for later */
718 FREE(last_motion_notify);
719 last_motion_notify = (xcb_motion_notify_event_t *)event;
723 DLOG("Passing to original handler\n");
724 handle_event(type, event);
728 if (last_motion_notify != (xcb_motion_notify_event_t *)event)
731 if (dragloop->result != DRAGGING) {
732 free(last_motion_notify);
733 ev_break(EV_A_ EVBREAK_ONE);
738 if (last_motion_notify == NULL) {
742 /* Ensure that we are either dragging the resize handle (con is NULL) or that the
743 * container still exists. The latter might not be true, e.g., if the window closed
744 * for any reason while the user was dragging it. */
745 if (!dragloop->con || con_exists(dragloop->con)) {
748 &(dragloop->old_rect),
749 last_motion_notify->root_x,
750 last_motion_notify->root_y,
753 FREE(last_motion_notify);
759 static void xcb_drag_prepare_cb(EV_P_ ev_prepare *w, int revents) {
760 struct drag_x11_cb *dragloop = (struct drag_x11_cb *)w->data;
761 while (!drain_drag_events(EV_A, dragloop)) {
762 /* repeatedly drain events: draining might produce additional ones */
767 * This function grabs your pointer and keyboard and lets you drag stuff around
768 * (borders). Every time you move your mouse, an XCB_MOTION_NOTIFY event will
769 * be received and the given callback will be called with the parameters
770 * specified (client, border on which the click originally was), the original
771 * rect of the client, the event and the new coordinates (x, y).
774 drag_result_t drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t
776 border_t border, int cursor, callback_t callback, const void *extra) {
777 xcb_cursor_t xcursor = (cursor && xcursor_supported) ? xcursor_get_cursor(cursor) : XCB_NONE;
779 /* Grab the pointer */
780 xcb_grab_pointer_cookie_t cookie;
781 xcb_grab_pointer_reply_t *reply;
782 xcb_generic_error_t *error;
784 cookie = xcb_grab_pointer(conn,
785 false, /* get all pointer events specified by the following mask */
786 root, /* grab the root window */
787 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
788 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
789 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
790 confine_to, /* confine_to = in which window should the cursor stay */
791 xcursor, /* possibly display a special cursor */
794 if ((reply = xcb_grab_pointer_reply(conn, cookie, &error)) == NULL) {
795 ELOG("Could not grab pointer (error_code = %d)\n", error->error_code);
802 /* Grab the keyboard */
803 xcb_grab_keyboard_cookie_t keyb_cookie;
804 xcb_grab_keyboard_reply_t *keyb_reply;
806 keyb_cookie = xcb_grab_keyboard(conn,
807 false, /* get all keyboard events */
808 root, /* grab the root window */
810 XCB_GRAB_MODE_ASYNC, /* continue processing pointer events as normal */
811 XCB_GRAB_MODE_ASYNC /* keyboard mode */
814 if ((keyb_reply = xcb_grab_keyboard_reply(conn, keyb_cookie, &error)) == NULL) {
815 ELOG("Could not grab keyboard (error_code = %d)\n", error->error_code);
817 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
823 /* Go into our own event loop */
824 struct drag_x11_cb loop = {
827 .callback = callback,
830 ev_prepare *prepare = &loop.prepare;
832 loop.old_rect = con->rect;
833 ev_prepare_init(prepare, xcb_drag_prepare_cb);
834 prepare->data = &loop;
835 main_set_x11_cb(false);
836 ev_prepare_start(main_loop, prepare);
838 ev_loop(main_loop, 0);
840 ev_prepare_stop(main_loop, prepare);
841 main_set_x11_cb(true);
843 xcb_ungrab_keyboard(conn, XCB_CURRENT_TIME);
844 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
851 * Repositions the CT_FLOATING_CON to have the coordinates specified by
852 * newrect, but only if the coordinates are not out-of-bounds. Also reassigns
853 * the floating con to a different workspace if this move was across different
857 bool floating_reposition(Con *con, Rect newrect) {
858 /* Sanity check: Are the new coordinates on any output? If not, we
859 * ignore that request. */
860 if (!output_containing_rect(newrect)) {
861 ELOG("No output found at destination coordinates. Not repositioning.\n");
867 floating_maybe_reassign_ws(con);
869 /* If this is a scratchpad window, don't auto center it from now on. */
870 if (con->scratchpad_state == SCRATCHPAD_FRESH)
871 con->scratchpad_state = SCRATCHPAD_CHANGED;
878 * Sets size of the CT_FLOATING_CON to specified dimensions. Might limit the
879 * actual size with regard to size constraints taken from user settings.
880 * Additionally, the dimensions may be upscaled until they're divisible by the
881 * window's size hints.
884 void floating_resize(Con *floating_con, int x, int y) {
885 DLOG("floating resize to %dx%d px\n", x, y);
886 Rect *rect = &floating_con->rect;
887 Con *focused_con = con_descend_focused(floating_con);
888 if (focused_con->window == NULL) {
889 DLOG("No window is focused. Not resizing.\n");
892 int wi = focused_con->window->width_increment;
893 int hi = focused_con->window->height_increment;
897 rect->width += (wi - 1 - rect->width) % wi;
899 rect->height += (hi - 1 - rect->height) % hi;
901 floating_check_size(floating_con);
903 /* If this is a scratchpad window, don't auto center it from now on. */
904 if (floating_con->scratchpad_state == SCRATCHPAD_FRESH)
905 floating_con->scratchpad_state = SCRATCHPAD_CHANGED;
909 * Fixes the coordinates of the floating window whenever the window gets
910 * reassigned to a different output (or when the output’s rect changes).
913 void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect) {
914 DLOG("Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
915 con, con->rect.x, con->rect.y, con->rect.width, con->rect.height);
916 DLOG("old_rect = (%d, %d), %d x %d\n",
917 old_rect->x, old_rect->y, old_rect->width, old_rect->height);
918 DLOG("new_rect = (%d, %d), %d x %d\n",
919 new_rect->x, new_rect->y, new_rect->width, new_rect->height);
920 /* First we get the x/y coordinates relative to the x/y coordinates
921 * of the output on which the window is on */
922 int32_t rel_x = con->rect.x - old_rect->x + (int32_t)(con->rect.width / 2);
923 int32_t rel_y = con->rect.y - old_rect->y + (int32_t)(con->rect.height / 2);
924 /* Then we calculate a fraction, for example 0.63 for a window
925 * which is at y = 1212 of a 1920 px high output */
926 DLOG("rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
927 rel_x, rel_y, (double)rel_x / old_rect->width, (double)rel_y / old_rect->height,
928 old_rect->width, old_rect->height);
929 /* Here we have to multiply at first. Or we will lose precision when not compiled with -msse2 */
930 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);
931 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);
932 DLOG("Resulting coordinates: x = %d, y = %d\n", con->rect.x, con->rect.y);