2 #define I3__FILE__ "floating.c"
14 extern xcb_connection_t *
conn;
23 Rect outputs_dimensions = {0, 0, 0, 0};
28 return outputs_dimensions;
39 const int floating_sane_min_height = 50;
40 const int floating_sane_min_width = 75;
41 Rect floating_sane_max_dimensions;
106 bool set_focus = (con ==
focused);
109 LOG(
"Container is a dock window, not enabling floating mode.\n");
114 LOG(
"Container is already in floating mode, not doing anything.\n");
122 if (con->
type == CT_WORKSPACE) {
123 LOG(
"This is a workspace, creating new container around content\n");
125 LOG(
"Workspace is empty, aborting\n");
135 memcpy(&(new->rect), &(con->
rect),
sizeof(
Rect));
138 if (old_focused ==
TAILQ_END(&(con->focus_head)))
142 DLOG(
"Moving cons\n");
151 DLOG(
"Attaching new split to ws\n");
176 nc->
type = CT_FLOATING_CON;
187 DLOG(
"Old container empty after setting this child to floating, closing\n");
192 sasprintf(&name,
"[i3 con] floatingcon around %p", con);
201 Rect zero = { 0, 0, 0, 0 };
205 if (memcmp(&(nc->
rect), &zero,
sizeof(
Rect)) == 0) {
206 DLOG(
"Geometry not set, combining children\n");
248 DLOG(
"Centering above leader\n");
264 if (!current_output || current_output->
con != correct_output) {
265 DLOG(
"This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
284 nc->
rect.
y -= deco_height;
286 DLOG(
"Corrected y = %d (deco_height = %d)\n", nc->
rect.
y, deco_height);
307 ELOG(
"No output found at destination coordinates, centering floating window on current ws\n");
314 LOG(
"Container isn't floating, not doing anything.\n");
318 const bool set_focus = (con ==
focused);
337 if (focused->
type == CT_WORKSPACE)
364 LOG(
"already floating, re-setting to tiling\n");
378 DLOG(
"Raising floating con %p / %s\n", con, con->
name);
394 ELOG(
"No output found at destination coordinates?\n");
399 DLOG(
"still the same ws\n");
403 DLOG(
"Need to re-assign!\n");
407 DLOG(
"Moving con %p / %s to workspace %p / %s\n", con, con->
name, ws, ws->
name);
414 const struct xcb_button_press_event_t *
event = extra;
417 con->rect.x = old_rect->x + (new_x -
event->root_x);
418 con->rect.y = old_rect->y + (new_y -
event->root_y);
438 DLOG(
"floating_drag_window\n");
471 const xcb_button_press_event_t *
event;
476 const xcb_button_press_event_t *
event = params->
event;
479 int32_t dest_x = con->rect.x;
480 int32_t dest_y = con->rect.y;
482 uint32_t dest_height;
484 double ratio = (double) old_rect->width / old_rect->height;
489 dest_width = old_rect->width - (new_x -
event->root_x);
490 else dest_width = old_rect->width + (new_x -
event->root_x);
493 dest_height = old_rect->height - (new_y -
event->root_y);
494 else dest_height = old_rect->height + (new_y -
event->root_y);
498 dest_width =
max(dest_width, (
int) (dest_height * ratio));
499 dest_height =
max(dest_height, (
int) (dest_width / ratio));
502 con->rect = (
Rect) { dest_x, dest_y, dest_width, dest_height };
509 if (corner & BORDER_LEFT)
510 dest_x = old_rect->x + (old_rect->width - con->rect.width);
512 if (corner & BORDER_TOP)
513 dest_y = old_rect->y + (old_rect->height - con->rect.height);
515 con->rect.x = dest_x;
516 con->rect.y = dest_y;
531 const xcb_button_press_event_t *
event) {
532 DLOG(
"floating_resize_window\n");
538 if (event->event_x <= (con->
rect.
width / 2))
543 if (event->event_y <= (con->
rect.
height / 2)) {
581 uint32_t new_x, new_y;
582 Rect old_rect = { 0, 0, 0, 0 };
584 memcpy(&old_rect, &(con->
rect),
sizeof(
Rect));
590 xcb_grab_pointer_cookie_t cookie;
591 xcb_grab_pointer_reply_t *reply;
592 cookie = xcb_grab_pointer(
conn,
595 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
602 if ((reply = xcb_grab_pointer_reply(
conn, cookie, NULL)) == NULL) {
603 ELOG(
"Could not grab pointer\n");
610 xcb_grab_keyboard_cookie_t keyb_cookie;
611 xcb_grab_keyboard_reply_t *keyb_reply;
613 keyb_cookie = xcb_grab_keyboard(
conn,
621 if ((keyb_reply = xcb_grab_keyboard_reply(
conn, keyb_cookie, NULL)) == NULL) {
622 ELOG(
"Could not grab keyboard\n");
631 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
632 Con *inside_con = NULL;
636 while (drag_result ==
DRAGGING && (inside_event = xcb_wait_for_event(
conn))) {
640 if (inside_event->response_type == 0) {
645 int type = (inside_event->response_type & 0x7F);
648 case XCB_BUTTON_RELEASE:
652 case XCB_MOTION_NOTIFY:
654 FREE(last_motion_notify);
655 last_motion_notify = inside_event;
658 case XCB_UNMAP_NOTIFY:
659 inside_con =
con_by_window_id(((xcb_unmap_notify_event_t*)inside_event)->window);
661 if (inside_con != NULL) {
662 DLOG(
"UnmapNotify for window 0x%08x (container %p)\n", ((xcb_unmap_notify_event_t*)inside_event)->window, inside_con);
665 DLOG(
"UnmapNotify for a managed window on the current workspace, aborting\n");
675 DLOG(
"A key was pressed during drag, reverting changes.");
682 DLOG(
"Passing to original handler\n");
687 if (last_motion_notify != inside_event)
689 }
while ((inside_event = xcb_poll_for_event(
conn)) != NULL);
691 if (last_motion_notify == NULL || drag_result !=
DRAGGING)
694 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
695 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
697 callback(con, &old_rect, new_x, new_y, extra);
698 FREE(last_motion_notify);
701 xcb_ungrab_keyboard(
conn, XCB_CURRENT_TIME);
702 xcb_ungrab_pointer(
conn, XCB_CURRENT_TIME);
720 ELOG(
"No output found at destination coordinates. Not repositioning.\n");
741 DLOG(
"Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
743 DLOG(
"old_rect = (%d, %d), %d x %d\n",
744 old_rect->
x, old_rect->
y, old_rect->
width, old_rect->
height);
745 DLOG(
"new_rect = (%d, %d), %d x %d\n",
746 new_rect->
x, new_rect->
y, new_rect->
width, new_rect->
height);
749 int32_t rel_x = con->
rect.
x - old_rect->
x + (int32_t)(con->
rect.
width / 2);
750 int32_t rel_y = con->
rect.
y - old_rect->
y + (int32_t)(con->
rect.
height / 2);
753 DLOG(
"rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
754 rel_x, rel_y, (
double)rel_x / old_rect->
width, (
double)rel_y / old_rect->
height,
757 con->
rect.
x = (int32_t)new_rect->
x + (
double)(rel_x * (int32_t)new_rect->
width)
759 con->
rect.
y = (int32_t)new_rect->
y + (
double)(rel_y * (int32_t)new_rect->
height)
761 DLOG(
"Resulting coordinates: x = %d, y = %d\n", con->
rect.
x, con->
rect.
y);
769 void floating_move(xcb_connection_t *
conn, Client *currently_focused,
direction_t direction) {
770 DLOG(
"floating move\n");
772 Rect destination = currently_focused->rect;
773 Rect *screen = &(currently_focused->workspace->output->rect);
794 if ((int32_t)(destination.
x + destination.
width - 5) <= (int32_t)screen->
x ||
795 (int32_t)(destination.
x + 5) >= (int32_t)(screen->
x + screen->
width) ||
796 (int32_t)(destination.
y + destination.
height - 5) <= (int32_t)screen->
y ||
797 (int32_t)(destination.
y + 5) >= (int32_t)(screen->
y + screen->
height)) {
798 DLOG(
"boundary check failed, not moving\n");
802 currently_focused->rect = destination;
803 reposition_client(conn, currently_focused);
816 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
819 workspace->floating_hidden = !workspace->floating_hidden;
820 DLOG(
"floating_hidden is now: %d\n", workspace->floating_hidden);
821 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
822 if (workspace->floating_hidden)
823 client_unmap(conn, client);
824 else client_map(conn, client);
829 if (workspace->floating_hidden)
830 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
831 if (client_is_floating(client))
833 set_focus(conn, client,
true);