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;
77 bool set_focus = (con ==
focused);
80 LOG(
"Container is a dock window, not enabling floating mode.\n");
85 LOG(
"Container is already in floating mode, not doing anything.\n");
93 if (con->
type == CT_WORKSPACE) {
94 LOG(
"This is a workspace, creating new container around content\n");
96 LOG(
"Workspace is empty, aborting\n");
106 memcpy(&(new->rect), &(con->
rect),
sizeof(
Rect));
109 if (old_focused ==
TAILQ_END(&(con->focus_head)))
113 DLOG(
"Moving cons\n");
122 DLOG(
"Attaching new split to ws\n");
147 nc->
type = CT_FLOATING_CON;
158 DLOG(
"Old container empty after setting this child to floating, closing\n");
163 sasprintf(&name,
"[i3 con] floatingcon around %p", con);
172 Rect zero = { 0, 0, 0, 0 };
176 if (memcmp(&(nc->
rect), &zero,
sizeof(
Rect)) == 0) {
177 DLOG(
"Geometry not set, combining children\n");
219 DLOG(
"Centering above leader\n");
235 if (!current_output || current_output->
con != correct_output) {
236 DLOG(
"This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
255 nc->
rect.
y -= deco_height;
257 DLOG(
"Corrected y = %d (deco_height = %d)\n", nc->
rect.
y, deco_height);
278 ELOG(
"No output found at destination coordinates, centering floating window on current ws\n");
285 LOG(
"Container isn't floating, not doing anything.\n");
306 if (focused->
type == CT_WORKSPACE)
332 LOG(
"already floating, re-setting to tiling\n");
346 DLOG(
"Raising floating con %p / %s\n", con, con->
name);
362 ELOG(
"No output found at destination coordinates?\n");
367 DLOG(
"still the same ws\n");
371 DLOG(
"Need to re-assign!\n");
375 DLOG(
"Moving con %p / %s to workspace %p / %s\n", con, con->
name, ws, ws->
name);
382 const struct xcb_button_press_event_t *
event = extra;
385 con->rect.x = old_rect->x + (new_x -
event->root_x);
386 con->rect.y = old_rect->y + (new_y -
event->root_y);
404 DLOG(
"floating_drag_window\n");
425 const xcb_button_press_event_t *
event;
430 const xcb_button_press_event_t *
event = params->
event;
433 int32_t dest_x = con->rect.x;
434 int32_t dest_y = con->rect.y;
436 uint32_t dest_height;
438 double ratio = (double) old_rect->width / old_rect->height;
443 dest_width = old_rect->width - (new_x -
event->root_x);
444 else dest_width = old_rect->width + (new_x -
event->root_x);
447 dest_height = old_rect->height - (new_y -
event->root_y);
448 else dest_height = old_rect->height + (new_y -
event->root_y);
452 dest_width =
max(dest_width, minimum.
width);
453 dest_height =
max(dest_height, minimum.
height);
457 dest_width =
max(dest_width, (
int) (dest_height * ratio));
458 dest_height =
max(dest_height, (
int) (dest_width / ratio));
463 if (corner & BORDER_LEFT)
464 dest_x = old_rect->x + (old_rect->width - dest_width);
466 if (corner & BORDER_TOP)
467 dest_y = old_rect->y + (old_rect->height - dest_height);
469 con->rect = (
Rect) { dest_x, dest_y, dest_width, dest_height };
484 const xcb_button_press_event_t *
event) {
485 DLOG(
"floating_resize_window\n");
491 if (event->event_x <= (con->
rect.
width / 2))
496 if (event->event_y <= (con->
rect.
height / 2)) {
523 uint32_t new_x, new_y;
524 Rect old_rect = { 0, 0, 0, 0 };
526 memcpy(&old_rect, &(con->
rect),
sizeof(
Rect));
532 xcb_grab_pointer_cookie_t cookie;
533 xcb_grab_pointer_reply_t *reply;
534 cookie = xcb_grab_pointer(
conn,
537 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
544 if ((reply = xcb_grab_pointer_reply(
conn, cookie, NULL)) == NULL) {
545 ELOG(
"Could not grab pointer\n");
554 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
555 bool loop_done =
false;
557 while (!loop_done && (inside_event = xcb_wait_for_event(
conn))) {
561 if (inside_event->response_type == 0) {
566 int type = (inside_event->response_type & 0x7F);
569 case XCB_BUTTON_RELEASE:
573 case XCB_MOTION_NOTIFY:
575 FREE(last_motion_notify);
576 last_motion_notify = inside_event;
579 case XCB_UNMAP_NOTIFY:
581 case XCB_KEY_RELEASE:
582 DLOG(
"Unmap-notify, aborting\n");
588 DLOG(
"Passing to original handler\n");
593 if (last_motion_notify != inside_event)
595 }
while ((inside_event = xcb_poll_for_event(
conn)) != NULL);
597 if (last_motion_notify == NULL || loop_done)
600 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
601 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
603 callback(con, &old_rect, new_x, new_y, extra);
604 FREE(last_motion_notify);
607 xcb_ungrab_pointer(
conn, XCB_CURRENT_TIME);
622 newrect.
x + (newrect.
width / 2),
623 newrect.
y + (newrect.
height / 2));
626 ELOG(
"No output found at destination coordinates. Not repositioning.\n");
642 DLOG(
"Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
644 DLOG(
"old_rect = (%d, %d), %d x %d\n",
645 old_rect->
x, old_rect->
y, old_rect->
width, old_rect->
height);
646 DLOG(
"new_rect = (%d, %d), %d x %d\n",
647 new_rect->
x, new_rect->
y, new_rect->
width, new_rect->
height);
650 int32_t rel_x = con->
rect.
x - old_rect->
x + (int32_t)(con->
rect.
width / 2);
651 int32_t rel_y = con->
rect.
y - old_rect->
y + (int32_t)(con->
rect.
height / 2);
654 DLOG(
"rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
655 rel_x, rel_y, (
double)rel_x / old_rect->
width, (
double)rel_y / old_rect->
height,
658 con->
rect.
x = (int32_t)new_rect->
x + (
double)(rel_x * (int32_t)new_rect->
width)
660 con->
rect.
y = (int32_t)new_rect->
y + (
double)(rel_y * (int32_t)new_rect->
height)
662 DLOG(
"Resulting coordinates: x = %d, y = %d\n", con->
rect.
x, con->
rect.
y);
670 void floating_move(xcb_connection_t *
conn, Client *currently_focused,
direction_t direction) {
671 DLOG(
"floating move\n");
673 Rect destination = currently_focused->rect;
674 Rect *screen = &(currently_focused->workspace->output->rect);
695 if ((int32_t)(destination.
x + destination.
width - 5) <= (int32_t)screen->
x ||
696 (int32_t)(destination.
x + 5) >= (int32_t)(screen->
x + screen->
width) ||
697 (int32_t)(destination.
y + destination.
height - 5) <= (int32_t)screen->
y ||
698 (int32_t)(destination.
y + 5) >= (int32_t)(screen->
y + screen->
height)) {
699 DLOG(
"boundary check failed, not moving\n");
703 currently_focused->rect = destination;
704 reposition_client(conn, currently_focused);
717 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
720 workspace->floating_hidden = !workspace->floating_hidden;
721 DLOG(
"floating_hidden is now: %d\n", workspace->floating_hidden);
722 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
723 if (workspace->floating_hidden)
724 client_unmap(conn, client);
725 else client_map(conn, client);
730 if (workspace->floating_hidden)
731 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
732 if (client_is_floating(client))
734 set_focus(conn, client,
true);