4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * src/floating.c: contains all functions for handling floating clients
17 extern xcb_connection_t *conn;
20 * Toggles floating mode for the given container.
22 * If the automatic flag is set to true, this was an automatic update by a change of the
23 * window class from the application which can be overwritten by the user.
26 void toggle_floating_mode(Con *con, bool automatic) {
27 //i3Font *font = load_font(conn, config.font);
29 /* see if the client is already floating */
30 if (con_is_floating(con)) {
31 LOG("already floating, re-setting to tiling\n");
32 assert(con->old_parent != NULL);
34 /* 1: detach from parent container */
35 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
36 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
38 /* 2: kill parent container */
39 TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
40 TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
41 tree_close(con->parent, false);
43 /* 3: re-attach to previous parent */
44 con->parent = con->old_parent;
45 TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes);
46 TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused);
48 con->floating = FLOATING_USER_OFF;
53 /* 1: detach the container from its parent */
54 /* TODO: refactor this with tree_close() */
55 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
56 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
60 TAILQ_FOREACH(child, &(con->parent->nodes_head), nodes)
62 /* TODO: better document why this math works */
63 double fix = 1.0 / (1.0 - (1.0 / (children+1)));
64 TAILQ_FOREACH(child, &(con->parent->nodes_head), nodes) {
65 if (child->percent <= 0.0)
67 child->percent *= fix;
70 /* 2: create a new container to render the decoration on, add
71 * it as a floating window to the workspace */
72 Con *nc = con_new(NULL);
73 nc->parent = con_get_workspace(con);
75 nc->orientation = NO_ORIENTATION;
76 nc->type = CT_FLOATING_CON;
77 TAILQ_INSERT_TAIL(&(nc->parent->floating_head), nc, floating_windows);
78 TAILQ_INSERT_TAIL(&(nc->parent->focus_head), nc, focused);
80 /* 3: attach the child to the new parent container */
81 con->old_parent = con->parent;
83 con->floating = FLOATING_USER_ON;
86 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
87 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
93 DLOG("Not putting dock client into floating mode\n");
98 DLOG("This client is already in floating (container == NULL), re-inserting\n");
100 Workspace *ws = client->workspace;
101 SLIST_FOREACH(next_tiling, &(ws->focus_stack), focus_clients)
102 if (!client_is_floating(next_tiling))
104 /* If there are no tiling clients on this workspace, there can only be one
105 * container: the first one */
106 if (next_tiling == TAILQ_END(&(ws->focus_stack)))
107 con = ws->table[0][0];
108 else con = next_tiling->container;
110 /* Remove the client from the list of floating clients */
111 TAILQ_REMOVE(&(ws->floating_clients), client, floating_clients);
113 DLOG("destination container = %p\n", con);
114 Client *old_focused = con->currently_focused;
115 /* Preserve position/size */
116 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
118 client->floating = FLOATING_USER_OFF;
119 client->container = con;
121 if (old_focused != NULL && !old_focused->dock)
122 CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
123 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
125 DLOG("Re-inserted the window.\n");
126 con->currently_focused = client;
128 client_set_below_floating(conn, client);
130 render_container(conn, con);
136 DLOG("Entering floating for client %08x\n", client->child);
138 /* Remove the client of its container */
139 client_remove_from_container(conn, client, con, false);
140 client->container = NULL;
142 /* Add the client to the list of floating clients for its workspace */
143 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
145 if (con->currently_focused == client) {
146 DLOG("Need to re-adjust currently_focused\n");
147 /* Get the next client in the focus stack for this particular container */
148 con->currently_focused = get_last_focused_client(conn, con, NULL);
152 client->floating = FLOATING_AUTO_ON;
153 else client->floating = FLOATING_USER_ON;
155 /* Initialize the floating position from the position in tiling mode, if this
156 * client never was floating (x == -1) */
157 if (client->floating_rect.x == -1) {
158 /* Copy over the position */
159 client->floating_rect.x = client->rect.x;
160 client->floating_rect.y = client->rect.y;
162 /* Copy size the other direction */
163 client->child_rect.width = client->floating_rect.width;
164 client->child_rect.height = client->floating_rect.height;
166 client->rect.width = client->child_rect.width + 2 + 2;
167 client->rect.height = client->child_rect.height + (font->height + 2 + 2) + 2;
169 DLOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
170 client->floating_rect.width, client->floating_rect.height);
172 /* If the client was already in floating before we restore the old position / size */
173 DLOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
174 client->floating_rect.width, client->floating_rect.height);
175 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
178 /* Raise the client */
179 xcb_raise_window(conn, client->frame);
180 reposition_client(conn, client);
181 resize_client(conn, client);
182 /* redecorate_window flushes */
183 redecorate_window(conn, client);
185 /* Re-render the tiling layout of this container */
186 render_container(conn, con);
193 * Removes the floating client from its workspace and attaches it to the new workspace.
194 * This is centralized here because it may happen if you move it via keyboard and
195 * if you move it using your mouse.
198 void floating_assign_to_workspace(Client *client, Workspace *new_workspace) {
199 /* Remove from focus stack and list of floating clients */
200 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
201 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
203 if (client->workspace->fullscreen_client == client)
204 client->workspace->fullscreen_client = NULL;
206 /* Insert into destination focus stack and list of floating clients */
207 client->workspace = new_workspace;
208 SLIST_INSERT_HEAD(&(client->workspace->focus_stack), client, focus_clients);
209 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
210 if (client->fullscreen)
211 client->workspace->fullscreen_client = client;
215 * This is an ugly data structure which we need because there is no standard
216 * way of having nested functions (only available as a gcc extension at the
217 * moment, clang doesn’t support it) or blocks (only available as a clang
218 * extension and only on Mac OS X systems at the moment).
221 struct resize_callback_params {
223 xcb_button_press_event_t *event;
226 DRAGGING_CB(resize_callback) {
227 struct resize_callback_params *params = extra;
228 xcb_button_press_event_t *event = params->event;
229 switch (params->border) {
231 int new_width = old_rect->width + (new_x - event->root_x);
232 if ((new_width < 0) ||
233 (new_width < client_min_width(client) && client->rect.width >= new_width))
235 client->rect.width = new_width;
239 case BORDER_BOTTOM: {
240 int new_height = old_rect->height + (new_y - event->root_y);
241 if ((new_height < 0) ||
242 (new_height < client_min_height(client) && client->rect.height >= new_height))
244 client->rect.height = old_rect->height + (new_y - event->root_y);
249 int new_height = old_rect->height + (event->root_y - new_y);
250 if ((new_height < 0) ||
251 (new_height < client_min_height(client) && client->rect.height >= new_height))
254 client->rect.y = old_rect->y + (new_y - event->root_y);
255 client->rect.height = new_height;
260 int new_width = old_rect->width + (event->root_x - new_x);
261 if ((new_width < 0) ||
262 (new_width < client_min_width(client) && client->rect.width >= new_width))
264 client->rect.x = old_rect->x + (new_x - event->root_x);
265 client->rect.width = new_width;
270 /* Push the new position/size to X11 */
271 reposition_client(conn, client);
272 resize_client(conn, client);
278 * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
279 * Determines on which border the user clicked and launches the drag_pointer function
280 * with the resize_callback.
283 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
284 DLOG("floating border click\n");
288 if (event->event_y < 2)
290 else if (event->event_y >= (client->rect.height - 2))
291 border = BORDER_BOTTOM;
292 else if (event->event_x <= 2)
293 border = BORDER_LEFT;
294 else if (event->event_x >= (client->rect.width - 2))
295 border = BORDER_RIGHT;
297 DLOG("Not on any border, not doing anything.\n");
301 DLOG("border = %d\n", border);
303 struct resize_callback_params params = { border, event };
305 drag_pointer(conn, client, event, XCB_NONE, border, resize_callback, ¶ms);
311 DRAGGING_CB(drag_window_callback) {
312 struct xcb_button_press_event_t *event = extra;
314 /* Reposition the client correctly while moving */
315 con->rect.x = old_rect->x + (new_x - event->root_x);
316 con->rect.y = old_rect->y + (new_y - event->root_y);
317 //reposition_client(conn, con);
318 /* Because reposition_client does not send a faked configure event (only resize does),
319 * we need to initiate that on our own */
320 //fake_absolute_configure_notify(conn, client);
321 /* fake_absolute_configure_notify flushes */
325 * Called when the user clicked on the titlebar of a floating window.
326 * Calls the drag_pointer function with the drag_window callback
329 void floating_drag_window(Con *con, xcb_button_press_event_t *event) {
330 DLOG("floating_drag_window\n");
332 drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback, event);
338 * This is an ugly data structure which we need because there is no standard
339 * way of having nested functions (only available as a gcc extension at the
340 * moment, clang doesn’t support it) or blocks (only available as a clang
341 * extension and only on Mac OS X systems at the moment).
344 struct resize_window_callback_params {
347 xcb_button_press_event_t *event;
350 DRAGGING_CB(resize_window_callback) {
351 struct resize_window_callback_params *params = extra;
352 xcb_button_press_event_t *event = params->event;
353 border_t corner = params->corner;
355 int32_t dest_x = client->rect.x;
356 int32_t dest_y = client->rect.y;
358 uint32_t dest_height;
360 double ratio = (double) old_rect->width / old_rect->height;
362 /* First guess: We resize by exactly the amount the mouse moved,
363 * taking into account in which corner the client was grabbed */
364 if (corner & BORDER_LEFT)
365 dest_width = old_rect->width - (new_x - event->root_x);
366 else dest_width = old_rect->width + (new_x - event->root_x);
368 if (corner & BORDER_TOP)
369 dest_height = old_rect->height - (new_y - event->root_y);
370 else dest_height = old_rect->height + (new_y - event->root_y);
372 /* Obey minimum window size */
373 dest_width = max(dest_width, client_min_width(client));
374 dest_height = max(dest_height, client_min_height(client));
376 /* User wants to keep proportions, so we may have to adjust our values */
377 if (params->proportional) {
378 dest_width = max(dest_width, (int) (dest_height * ratio));
379 dest_height = max(dest_height, (int) (dest_width / ratio));
382 /* If not the lower right corner is grabbed, we must also reposition
383 * the client by exactly the amount we resized it */
384 if (corner & BORDER_LEFT)
385 dest_x = old_rect->x + (old_rect->width - dest_width);
387 if (corner & BORDER_TOP)
388 dest_y = old_rect->y + (old_rect->height - dest_height);
390 client->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
392 /* resize_client flushes */
393 resize_client(conn, client);
397 * Called when the user clicked on a floating window while holding the
398 * floating_modifier and the right mouse button.
399 * Calls the drag_pointer function with the resize_window callback
402 void floating_resize_window(xcb_connection_t *conn, Client *client,
403 bool proportional, xcb_button_press_event_t *event) {
404 DLOG("floating_resize_window\n");
406 /* corner saves the nearest corner to the original click. It contains
407 * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
410 if (event->event_x <= (client->rect.width / 2))
411 corner |= BORDER_LEFT;
412 else corner |= BORDER_RIGHT;
414 if (event->event_y <= (client->rect.height / 2))
415 corner |= BORDER_TOP;
416 else corner |= BORDER_RIGHT;
418 struct resize_window_callback_params params = { corner, proportional, event };
420 drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, ¶ms);
425 * This function grabs your pointer and lets you drag stuff around (borders).
426 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
427 * and the given callback will be called with the parameters specified (client,
428 * border on which the click originally was), the original rect of the client,
429 * the event and the new coordinates (x, y).
432 void drag_pointer(Con *con, xcb_button_press_event_t *event, xcb_window_t
433 confine_to, border_t border, callback_t callback, void *extra)
435 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
436 uint32_t new_x, new_y;
439 memcpy(&old_rect, &(con->rect), sizeof(Rect));
441 /* Grab the pointer */
442 /* TODO: returncode */
443 xcb_grab_pointer(conn,
444 false, /* get all pointer events specified by the following mask */
445 root, /* grab the root window */
446 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
447 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
448 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
449 confine_to, /* confine_to = in which window should the cursor stay */
450 XCB_NONE, /* don’t display a special cursor */
453 /* Go into our own event loop */
456 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
457 /* I’ve always wanted to have my own eventhandler… */
458 while ((inside_event = xcb_wait_for_event(conn))) {
459 /* We now handle all events we can get using xcb_poll_for_event */
461 /* Same as get_event_handler in xcb */
462 int nr = inside_event->response_type;
464 /* An error occured */
465 //handle_event(NULL, conn, inside_event);
470 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
474 case XCB_BUTTON_RELEASE:
477 case XCB_MOTION_NOTIFY:
478 /* motion_notify events are saved for later */
479 FREE(last_motion_notify);
480 last_motion_notify = inside_event;
483 case XCB_UNMAP_NOTIFY:
484 DLOG("Unmap-notify, aborting\n");
485 xcb_event_handle(&evenths, inside_event);
489 DLOG("Passing to original handler\n");
490 /* Use original handler */
491 xcb_event_handle(&evenths, inside_event);
494 if (last_motion_notify != inside_event)
496 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
498 if (last_motion_notify == NULL)
501 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
502 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
504 callback(con, &old_rect, new_x, new_y, extra);
505 FREE(last_motion_notify);
508 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
514 * Changes focus in the given direction for floating clients.
516 * Changing to the left/right means going to the previous/next floating client,
517 * changing to top/bottom means cycling through the Z-index.
520 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
521 DLOG("floating focus\n");
523 if (direction == D_LEFT || direction == D_RIGHT) {
524 /* Go to the next/previous floating client */
527 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
528 TAILQ_NEXT(currently_focused, floating_clients))) !=
529 TAILQ_END(&(currently_focused->workspace->floating_clients))) {
530 if (!client->floating)
532 set_focus(conn, client, true);
539 * Moves the client 10px to the specified direction.
542 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
543 DLOG("floating move\n");
545 Rect destination = currently_focused->rect;
546 Rect *screen = &(currently_focused->workspace->output->rect);
561 /* to make static analyzers happy */
566 /* Prevent windows from vanishing completely */
567 if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
568 (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
569 (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
570 (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
571 DLOG("boundary check failed, not moving\n");
575 currently_focused->rect = destination;
576 reposition_client(conn, currently_focused);
578 /* Because reposition_client does not send a faked configure event (only resize does),
579 * we need to initiate that on our own */
580 fake_absolute_configure_notify(conn, currently_focused);
581 /* fake_absolute_configure_notify flushes */
585 * Hides all floating clients (or show them if they are currently hidden) on
586 * the specified workspace.
589 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
592 workspace->floating_hidden = !workspace->floating_hidden;
593 DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
594 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
595 if (workspace->floating_hidden)
596 client_unmap(conn, client);
597 else client_map(conn, client);
600 /* If we just unmapped all floating windows we should ensure that the focus
601 * is set correctly, that ist, to the first non-floating client in stack */
602 if (workspace->floating_hidden)
603 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
604 if (client_is_floating(client))
606 set_focus(conn, client, true);