2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * floating.c: Floating windows.
12 extern xcb_connection_t *conn;
14 void floating_enable(Con *con, bool automatic) {
15 bool set_focus = (con == focused);
17 if (con->parent && con->parent->type == CT_DOCKAREA) {
18 LOG("Container is a dock window, not enabling floating mode.\n");
22 if (con_is_floating(con)) {
23 LOG("Container is already in floating mode, not doing anything.\n");
27 /* 1: If the container is a workspace container, we need to create a new
28 * split-container with the same orientation and make that one floating. We
29 * cannot touch the workspace container itself because floating containers
30 * are children of the workspace. */
31 if (con->type == CT_WORKSPACE) {
32 LOG("This is a workspace, creating new container around content\n");
33 if (con_num_children(con) == 0) {
34 LOG("Workspace is empty, aborting\n");
37 /* TODO: refactor this with src/con.c:con_set_layout */
38 Con *new = con_new(NULL, NULL);
40 new->orientation = con->orientation;
42 /* since the new container will be set into floating mode directly
43 * afterwards, we need to copy the workspace rect. */
44 memcpy(&(new->rect), &(con->rect), sizeof(Rect));
46 Con *old_focused = TAILQ_FIRST(&(con->focus_head));
47 if (old_focused == TAILQ_END(&(con->focus_head)))
50 /* 4: move the existing cons of this workspace below the new con */
51 DLOG("Moving cons\n");
53 while (!TAILQ_EMPTY(&(con->nodes_head))) {
54 child = TAILQ_FIRST(&(con->nodes_head));
56 con_attach(child, new, true);
59 /* 4: attach the new split container to the workspace */
60 DLOG("Attaching new split to ws\n");
61 con_attach(new, con, false);
64 con_focus(old_focused);
70 /* 1: detach the container from its parent */
71 /* TODO: refactor this with tree_close() */
72 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
73 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
75 con_fix_percent(con->parent);
77 /* 2: create a new container to render the decoration on, add
78 * it as a floating window to the workspace */
79 Con *nc = con_new(NULL, NULL);
80 /* we need to set the parent afterwards instead of passing it as an
81 * argument to con_new() because nc would be inserted into the tiling layer
83 Con *ws = con_get_workspace(con);
86 /* check if the parent container is empty and close it if so */
87 if ((con->parent->type == CT_CON || con->parent->type == CT_FLOATING_CON) && con_num_children(con->parent) == 0) {
88 DLOG("Old container empty after setting this child to floating, closing\n");
89 tree_close(con->parent, DONT_KILL_WINDOW, false, false);
93 sasprintf(&name, "[i3 con] floatingcon around %p", con);
97 /* find the height for the decorations */
98 int deco_height = config.font.height + 5;
100 DLOG("Original rect: (%d, %d) with %d x %d\n", con->rect.x, con->rect.y, con->rect.width, con->rect.height);
101 DLOG("Geometry = (%d, %d) with %d x %d\n", con->geometry.x, con->geometry.y, con->geometry.width, con->geometry.height);
102 Rect zero = { 0, 0, 0, 0 };
103 nc->rect = con->geometry;
104 /* If the geometry was not set (split containers), we need to determine a
105 * sensible one by combining the geometry of all children */
106 if (memcmp(&(nc->rect), &zero, sizeof(Rect)) == 0) {
107 DLOG("Geometry not set, combining children\n");
109 TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
110 DLOG("child geometry: %d x %d\n", child->geometry.width, child->geometry.height);
111 nc->rect.width += child->geometry.width;
112 nc->rect.height = max(nc->rect.height, child->geometry.height);
115 /* Raise the width/height to at least 75x50 (minimum size for windows) */
116 nc->rect.width = max(nc->rect.width, 75);
117 nc->rect.height = max(nc->rect.height, 50);
118 /* add pixels for the decoration */
119 /* TODO: don’t add them when the user automatically puts new windows into
120 * 1pixel/borderless mode */
121 nc->rect.height += deco_height + 2;
124 /* Honor the X11 border */
125 nc->rect.height += con->border_width * 2;
126 nc->rect.width += con->border_width * 2;
128 /* Some clients (like GIMP’s color picker window) get mapped
129 * to (0, 0), so we push them to a reasonable position
130 * (centered over their leader) */
131 if (nc->rect.x == 0 && nc->rect.y == 0) {
133 if (con->window && con->window->leader != XCB_NONE &&
134 (leader = con_by_window_id(con->window->leader)) != NULL) {
135 DLOG("Centering above leader\n");
136 nc->rect.x = leader->rect.x + (leader->rect.width / 2) - (nc->rect.width / 2);
137 nc->rect.y = leader->rect.y + (leader->rect.height / 2) - (nc->rect.height / 2);
139 /* center the window on workspace as fallback */
140 nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
141 nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
145 /* Sanity check: Are the coordinates on the appropriate output? If not, we
146 * need to change them */
147 Output *current_output = get_output_containing(nc->rect.x, nc->rect.y);
148 Con *correct_output = con_get_output(ws);
149 if (!current_output || current_output->con != correct_output) {
150 DLOG("This floating window is on the wrong output, fixing coordinates (currently (%d, %d))\n",
151 nc->rect.x, nc->rect.y);
152 /* Take the relative coordinates of the current output, then add them
153 * to the coordinate space of the correct output */
154 uint32_t rel_x = (nc->rect.x - (current_output ? current_output->con->rect.x : 0));
155 uint32_t rel_y = (nc->rect.y - (current_output ? current_output->con->rect.y : 0));
156 nc->rect.x = correct_output->rect.x + rel_x;
157 nc->rect.y = correct_output->rect.y + rel_y;
160 DLOG("Floating rect: (%d, %d) with %d x %d\n", nc->rect.x, nc->rect.y, nc->rect.width, nc->rect.height);
161 nc->orientation = NO_ORIENTATION;
162 nc->type = CT_FLOATING_CON;
163 TAILQ_INSERT_TAIL(&(ws->floating_head), nc, floating_windows);
164 TAILQ_INSERT_TAIL(&(ws->focus_head), nc, focused);
166 /* 3: attach the child to the new parent container */
169 con->floating = FLOATING_USER_ON;
171 /* 4: set the border style as specified with new_float */
173 con->border_style = config.default_floating_border;
175 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
176 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
178 /* render the cons to get initial window_rect correct */
179 render_con(nc, false);
180 render_con(con, false);
185 /* Check if we need to re-assign it to a different workspace because of its
186 * coordinates and exit if that was done successfully. */
187 if (floating_maybe_reassign_ws(nc))
190 /* Sanitize coordinates: Check if they are on any output */
191 if (get_output_containing(nc->rect.x, nc->rect.y) != NULL)
194 ELOG("No output found at destination coordinates, centering floating window on current ws\n");
195 nc->rect.x = ws->rect.x + (ws->rect.width / 2) - (nc->rect.width / 2);
196 nc->rect.y = ws->rect.y + (ws->rect.height / 2) - (nc->rect.height / 2);
199 void floating_disable(Con *con, bool automatic) {
200 if (!con_is_floating(con)) {
201 LOG("Container isn't floating, not doing anything.\n");
205 Con *ws = con_get_workspace(con);
207 /* 1: detach from parent container */
208 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
209 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
211 /* 2: kill parent container */
212 TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
213 TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
214 tree_close(con->parent, DONT_KILL_WINDOW, false, false);
216 /* 3: re-attach to the parent of the currently focused con on the workspace
217 * this floating con was on */
218 Con *focused = con_descend_tiling_focused(ws);
220 /* if there is no other container on this workspace, focused will be the
221 * workspace itself */
222 if (focused->type == CT_WORKSPACE)
223 con->parent = focused;
224 else con->parent = focused->parent;
226 /* con_fix_percent will adjust the percent value */
229 TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes);
230 TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused);
232 con->floating = FLOATING_USER_OFF;
234 con_fix_percent(con->parent);
235 // TODO: don’t influence focus handling when Con was not focused before.
240 * Toggles floating mode for the given container.
242 * If the automatic flag is set to true, this was an automatic update by a change of the
243 * window class from the application which can be overwritten by the user.
246 void toggle_floating_mode(Con *con, bool automatic) {
247 /* see if the client is already floating */
248 if (con_is_floating(con)) {
249 LOG("already floating, re-setting to tiling\n");
251 floating_disable(con, automatic);
255 floating_enable(con, automatic);
259 * Raises the given container in the list of floating containers
262 void floating_raise_con(Con *con) {
263 DLOG("Raising floating con %p / %s\n", con, con->name);
264 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
265 TAILQ_INSERT_TAIL(&(con->parent->floating_head), con, floating_windows);
269 * Checks if con’s coordinates are within its workspace and re-assigns it to
270 * the actual workspace if not.
273 bool floating_maybe_reassign_ws(Con *con) {
274 Output *output = get_output_containing(
275 con->rect.x + (con->rect.width / 2),
276 con->rect.y + (con->rect.height / 2));
279 ELOG("No output found at destination coordinates?\n");
283 if (con_get_output(con) == output->con) {
284 DLOG("still the same ws\n");
288 DLOG("Need to re-assign!\n");
290 Con *content = output_get_content(output->con);
291 Con *ws = TAILQ_FIRST(&(content->focus_head));
292 DLOG("Moving con %p / %s to workspace %p / %s\n", con, con->name, ws, ws->name);
293 con_move_to_workspace(con, ws, false, true);
294 con_focus(con_descend_focused(con));
298 DRAGGING_CB(drag_window_callback) {
299 const struct xcb_button_press_event_t *event = extra;
301 /* Reposition the client correctly while moving */
302 con->rect.x = old_rect->x + (new_x - event->root_x);
303 con->rect.y = old_rect->y + (new_y - event->root_y);
305 render_con(con, false);
309 /* Check if we cross workspace boundaries while moving */
310 if (!floating_maybe_reassign_ws(con))
316 * Called when the user clicked on the titlebar of a floating window.
317 * Calls the drag_pointer function with the drag_window callback
320 void floating_drag_window(Con *con, const xcb_button_press_event_t *event) {
321 DLOG("floating_drag_window\n");
323 /* Push changes before dragging, so that the window gets raised now and not
324 * after the user releases the mouse button */
327 /* Drag the window */
328 drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback, event);
333 * This is an ugly data structure which we need because there is no standard
334 * way of having nested functions (only available as a gcc extension at the
335 * moment, clang doesn’t support it) or blocks (only available as a clang
336 * extension and only on Mac OS X systems at the moment).
339 struct resize_window_callback_params {
340 const border_t corner;
341 const bool proportional;
342 const xcb_button_press_event_t *event;
345 DRAGGING_CB(resize_window_callback) {
346 const struct resize_window_callback_params *params = extra;
347 const xcb_button_press_event_t *event = params->event;
348 border_t corner = params->corner;
350 int32_t dest_x = con->rect.x;
351 int32_t dest_y = con->rect.y;
353 uint32_t dest_height;
355 double ratio = (double) old_rect->width / old_rect->height;
357 /* First guess: We resize by exactly the amount the mouse moved,
358 * taking into account in which corner the client was grabbed */
359 if (corner & BORDER_LEFT)
360 dest_width = old_rect->width - (new_x - event->root_x);
361 else dest_width = old_rect->width + (new_x - event->root_x);
363 if (corner & BORDER_TOP)
364 dest_height = old_rect->height - (new_y - event->root_y);
365 else dest_height = old_rect->height + (new_y - event->root_y);
367 /* Obey minimum window size */
368 Rect minimum = con_minimum_size(con);
369 dest_width = max(dest_width, minimum.width);
370 dest_height = max(dest_height, minimum.height);
372 /* User wants to keep proportions, so we may have to adjust our values */
373 if (params->proportional) {
374 dest_width = max(dest_width, (int) (dest_height * ratio));
375 dest_height = max(dest_height, (int) (dest_width / ratio));
378 /* If not the lower right corner is grabbed, we must also reposition
379 * the client by exactly the amount we resized it */
380 if (corner & BORDER_LEFT)
381 dest_x = old_rect->x + (old_rect->width - dest_width);
383 if (corner & BORDER_TOP)
384 dest_y = old_rect->y + (old_rect->height - dest_height);
386 con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
388 /* TODO: don’t re-render the whole tree just because we change
389 * coordinates of a floating window */
391 x_push_changes(croot);
395 * Called when the user clicked on a floating window while holding the
396 * floating_modifier and the right mouse button.
397 * Calls the drag_pointer function with the resize_window callback
400 void floating_resize_window(Con *con, const bool proportional,
401 const xcb_button_press_event_t *event) {
402 DLOG("floating_resize_window\n");
404 /* corner saves the nearest corner to the original click. It contains
405 * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
408 if (event->event_x <= (con->rect.width / 2))
409 corner |= BORDER_LEFT;
410 else corner |= BORDER_RIGHT;
412 if (event->event_y <= (con->rect.height / 2))
413 corner |= BORDER_TOP;
414 else corner |= BORDER_BOTTOM;
416 struct resize_window_callback_params params = { corner, proportional, event };
418 drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, ¶ms);
422 * This function grabs your pointer and lets you drag stuff around (borders).
423 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
424 * and the given callback will be called with the parameters specified (client,
425 * border on which the click originally was), the original rect of the client,
426 * the event and the new coordinates (x, y).
429 void drag_pointer(Con *con, const xcb_button_press_event_t *event, xcb_window_t
430 confine_to, border_t border, callback_t callback, const void *extra)
432 uint32_t new_x, new_y;
435 memcpy(&old_rect, &(con->rect), sizeof(Rect));
437 /* Grab the pointer */
438 xcb_grab_pointer_cookie_t cookie;
439 xcb_grab_pointer_reply_t *reply;
440 cookie = xcb_grab_pointer(conn,
441 false, /* get all pointer events specified by the following mask */
442 root, /* grab the root window */
443 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
444 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
445 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
446 confine_to, /* confine_to = in which window should the cursor stay */
447 XCB_NONE, /* don’t display a special cursor */
450 if ((reply = xcb_grab_pointer_reply(conn, cookie, NULL)) == NULL) {
451 ELOG("Could not grab pointer\n");
457 /* Go into our own event loop */
460 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
461 bool loop_done = false;
462 /* I’ve always wanted to have my own eventhandler… */
463 while (!loop_done && (inside_event = xcb_wait_for_event(conn))) {
464 /* We now handle all events we can get using xcb_poll_for_event */
466 /* skip x11 errors */
467 if (inside_event->response_type == 0) {
471 /* Strip off the highest bit (set if the event is generated) */
472 int type = (inside_event->response_type & 0x7F);
475 case XCB_BUTTON_RELEASE:
479 case XCB_MOTION_NOTIFY:
480 /* motion_notify events are saved for later */
481 FREE(last_motion_notify);
482 last_motion_notify = inside_event;
485 case XCB_UNMAP_NOTIFY:
487 case XCB_KEY_RELEASE:
488 DLOG("Unmap-notify, aborting\n");
489 handle_event(type, inside_event);
494 DLOG("Passing to original handler\n");
495 /* Use original handler */
496 handle_event(type, inside_event);
499 if (last_motion_notify != inside_event)
501 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
503 if (last_motion_notify == NULL)
506 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
507 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
509 callback(con, &old_rect, new_x, new_y, extra);
510 FREE(last_motion_notify);
513 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
519 * Moves the client 10px to the specified direction.
522 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
523 DLOG("floating move\n");
525 Rect destination = currently_focused->rect;
526 Rect *screen = &(currently_focused->workspace->output->rect);
541 /* to make static analyzers happy */
546 /* Prevent windows from vanishing completely */
547 if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
548 (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
549 (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
550 (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
551 DLOG("boundary check failed, not moving\n");
555 currently_focused->rect = destination;
556 reposition_client(conn, currently_focused);
558 /* Because reposition_client does not send a faked configure event (only resize does),
559 * we need to initiate that on our own */
560 fake_absolute_configure_notify(conn, currently_focused);
561 /* fake_absolute_configure_notify flushes */
565 * Hides all floating clients (or show them if they are currently hidden) on
566 * the specified workspace.
569 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
572 workspace->floating_hidden = !workspace->floating_hidden;
573 DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
574 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
575 if (workspace->floating_hidden)
576 client_unmap(conn, client);
577 else client_map(conn, client);
580 /* If we just unmapped all floating windows we should ensure that the focus
581 * is set correctly, that ist, to the first non-floating client in stack */
582 if (workspace->floating_hidden)
583 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
584 if (client_is_floating(client))
586 set_focus(conn, client, true);