2 #define I3__FILE__ "floating.c"
14 extern xcb_connection_t *
conn;
23 Rect outputs_dimensions = {0, 0, 0, 0};
28 return outputs_dimensions;
32 bool set_focus = (con ==
focused);
35 LOG(
"Container is a dock window, not enabling floating mode.\n");
40 LOG(
"Container is already in floating mode, not doing anything.\n");
48 if (con->
type == CT_WORKSPACE) {
49 LOG(
"This is a workspace, creating new container around content\n");
51 LOG(
"Workspace is empty, aborting\n");
61 memcpy(&(new->rect), &(con->
rect),
sizeof(
Rect));
64 if (old_focused ==
TAILQ_END(&(con->focus_head)))
68 DLOG(
"Moving cons\n");
77 DLOG(
"Attaching new split to ws\n");
103 nc->
type = CT_FLOATING_CON;
114 DLOG(
"Old container empty after setting this child to floating, closing\n");
119 sasprintf(&name,
"[i3 con] floatingcon around %p", con);
128 Rect zero = { 0, 0, 0, 0 };
132 if (memcmp(&(nc->
rect), &zero,
sizeof(
Rect)) == 0) {
133 DLOG(
"Geometry not set, combining children\n");
143 const int floating_sane_min_height = 50;
144 const int floating_sane_min_width = 75;
146 Rect floating_sane_max_dimensions;
211 DLOG(
"Centering above leader\n");
225 if (!current_output || current_output->
con != correct_output) {
226 DLOG(
"This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
230 uint32_t rel_x = (nc->
rect.
x - (current_output ? current_output->
con->
rect.
x : 0));
231 uint32_t rel_y = (nc->
rect.
y - (current_output ? current_output->
con->
rect.
y : 0));
242 nc->
rect.
y -= deco_height;
244 DLOG(
"Corrected y = %d (deco_height = %d)\n", nc->
rect.
y, deco_height);
265 ELOG(
"No output found at destination coordinates, centering floating window on current ws\n");
272 LOG(
"Container isn't floating, not doing anything.\n");
293 if (focused->
type == CT_WORKSPACE)
319 LOG(
"already floating, re-setting to tiling\n");
333 DLOG(
"Raising floating con %p / %s\n", con, con->
name);
349 ELOG(
"No output found at destination coordinates?\n");
354 DLOG(
"still the same ws\n");
358 DLOG(
"Need to re-assign!\n");
362 DLOG(
"Moving con %p / %s to workspace %p / %s\n", con, con->
name, ws, ws->
name);
369 const struct xcb_button_press_event_t *
event = extra;
372 con->rect.x = old_rect->x + (new_x -
event->root_x);
373 con->rect.y = old_rect->y + (new_y -
event->root_y);
391 DLOG(
"floating_drag_window\n");
412 const xcb_button_press_event_t *
event;
417 const xcb_button_press_event_t *
event = params->
event;
420 int32_t dest_x = con->rect.x;
421 int32_t dest_y = con->rect.y;
423 uint32_t dest_height;
425 double ratio = (double) old_rect->width / old_rect->height;
430 dest_width = old_rect->width - (new_x -
event->root_x);
431 else dest_width = old_rect->width + (new_x -
event->root_x);
434 dest_height = old_rect->height - (new_y -
event->root_y);
435 else dest_height = old_rect->height + (new_y -
event->root_y);
439 dest_width =
max(dest_width, minimum.
width);
440 dest_height =
max(dest_height, minimum.
height);
444 dest_width =
max(dest_width, (
int) (dest_height * ratio));
445 dest_height =
max(dest_height, (
int) (dest_width / ratio));
450 if (corner & BORDER_LEFT)
451 dest_x = old_rect->x + (old_rect->width - dest_width);
453 if (corner & BORDER_TOP)
454 dest_y = old_rect->y + (old_rect->height - dest_height);
456 con->rect = (
Rect) { dest_x, dest_y, dest_width, dest_height };
471 const xcb_button_press_event_t *
event) {
472 DLOG(
"floating_resize_window\n");
478 if (event->event_x <= (con->
rect.
width / 2))
502 uint32_t new_x, new_y;
503 Rect old_rect = { 0, 0, 0, 0 };
505 memcpy(&old_rect, &(con->
rect),
sizeof(
Rect));
508 xcb_grab_pointer_cookie_t cookie;
509 xcb_grab_pointer_reply_t *reply;
510 cookie = xcb_grab_pointer(
conn,
513 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION,
520 if ((reply = xcb_grab_pointer_reply(
conn, cookie, NULL)) == NULL) {
521 ELOG(
"Could not grab pointer\n");
530 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
531 bool loop_done =
false;
533 while (!loop_done && (inside_event = xcb_wait_for_event(
conn))) {
537 if (inside_event->response_type == 0) {
542 int type = (inside_event->response_type & 0x7F);
545 case XCB_BUTTON_RELEASE:
549 case XCB_MOTION_NOTIFY:
551 FREE(last_motion_notify);
552 last_motion_notify = inside_event;
555 case XCB_UNMAP_NOTIFY:
557 case XCB_KEY_RELEASE:
558 DLOG(
"Unmap-notify, aborting\n");
564 DLOG(
"Passing to original handler\n");
569 if (last_motion_notify != inside_event)
571 }
while ((inside_event = xcb_poll_for_event(
conn)) != NULL);
573 if (last_motion_notify == NULL)
576 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
577 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
579 callback(con, &old_rect, new_x, new_y, extra);
580 FREE(last_motion_notify);
583 xcb_ungrab_pointer(
conn, XCB_CURRENT_TIME);
598 newrect.
x + (newrect.
width / 2),
599 newrect.
y + (newrect.
height / 2));
602 ELOG(
"No output found at destination coordinates. Not repositioning.\n");
618 DLOG(
"Fixing coordinates of floating window %p (rect (%d, %d), %d x %d)\n",
620 DLOG(
"old_rect = (%d, %d), %d x %d\n",
621 old_rect->
x, old_rect->
y, old_rect->
width, old_rect->
height);
622 DLOG(
"new_rect = (%d, %d), %d x %d\n",
623 new_rect->
x, new_rect->
y, new_rect->
width, new_rect->
height);
626 int32_t rel_x = (con->
rect.
x - old_rect->
x);
627 int32_t rel_y = (con->
rect.
y - old_rect->
y);
630 DLOG(
"rel_x = %d, rel_y = %d, fraction_x = %f, fraction_y = %f, output->w = %d, output->h = %d\n",
631 rel_x, rel_y, (
double)rel_x / old_rect->
width, (
double)rel_y / old_rect->
height,
634 con->
rect.
x = (int32_t)new_rect->
x + (
double)(rel_x * (int32_t)new_rect->
width) / (int32_t)old_rect->
width;
635 con->
rect.
y = (int32_t)new_rect->
y + (double)(rel_y * (int32_t)new_rect->
height) / (int32_t)old_rect->
height;
636 DLOG(
"Resulting coordinates: x = %d, y = %d\n", con->
rect.
x, con->
rect.
y);
644 void floating_move(xcb_connection_t *
conn, Client *currently_focused,
direction_t direction) {
645 DLOG(
"floating move\n");
647 Rect destination = currently_focused->rect;
648 Rect *screen = &(currently_focused->workspace->output->rect);
669 if ((int32_t)(destination.
x + destination.
width - 5) <= (int32_t)screen->
x ||
670 (int32_t)(destination.
x + 5) >= (int32_t)(screen->
x + screen->
width) ||
671 (int32_t)(destination.
y + destination.
height - 5) <= (int32_t)screen->
y ||
672 (int32_t)(destination.
y + 5) >= (int32_t)(screen->
y + screen->
height)) {
673 DLOG(
"boundary check failed, not moving\n");
677 currently_focused->rect = destination;
678 reposition_client(conn, currently_focused);
691 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
694 workspace->floating_hidden = !workspace->floating_hidden;
695 DLOG(
"floating_hidden is now: %d\n", workspace->floating_hidden);
696 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
697 if (workspace->floating_hidden)
698 client_unmap(conn, client);
699 else client_map(conn, client);
704 if (workspace->floating_hidden)
705 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
706 if (client_is_floating(client))
708 set_focus(conn, client,
true);