2 * vim:ts=4:sw=4:expandtab
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;
19 void floating_enable(Con *con, bool automatic) {
20 if (con_is_floating(con)) {
21 LOG("Container is already in floating mode, not doing anything.\n");
25 /* 1: detach the container from its parent */
26 /* TODO: refactor this with tree_close() */
27 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
28 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
30 con_fix_percent(con->parent, WINDOW_REMOVE);
32 /* 2: create a new container to render the decoration on, add
33 * it as a floating window to the workspace */
34 Con *nc = con_new(NULL);
35 nc->parent = con_get_workspace(con);
37 nc->orientation = NO_ORIENTATION;
38 nc->type = CT_FLOATING_CON;
39 TAILQ_INSERT_TAIL(&(nc->parent->floating_head), nc, floating_windows);
40 TAILQ_INSERT_TAIL(&(nc->parent->focus_head), nc, focused);
42 /* 3: attach the child to the new parent container */
43 con->old_parent = con->parent;
45 con->floating = FLOATING_USER_ON;
48 TAILQ_INSERT_TAIL(&(nc->nodes_head), con, nodes);
49 TAILQ_INSERT_TAIL(&(nc->focus_head), con, focused);
52 void floating_disable(Con *con, bool automatic) {
53 if (!con_is_floating(con)) {
54 LOG("Container isn't floating, not doing anything.\n");
58 assert(con->old_parent != NULL);
60 /* 1: detach from parent container */
61 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
62 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
64 /* 2: kill parent container */
65 TAILQ_REMOVE(&(con->parent->parent->floating_head), con->parent, floating_windows);
66 TAILQ_REMOVE(&(con->parent->parent->focus_head), con->parent, focused);
67 tree_close(con->parent, false);
69 /* 3: re-attach to previous parent */
70 con->parent = con->old_parent;
71 TAILQ_INSERT_TAIL(&(con->parent->nodes_head), con, nodes);
72 TAILQ_INSERT_TAIL(&(con->parent->focus_head), con, focused);
74 con->floating = FLOATING_USER_OFF;
76 con_fix_percent(con->parent, WINDOW_ADD);
81 * Toggles floating mode for the given container.
83 * If the automatic flag is set to true, this was an automatic update by a change of the
84 * window class from the application which can be overwritten by the user.
87 void toggle_floating_mode(Con *con, bool automatic) {
88 //i3Font *font = load_font(conn, config.font);
90 /* see if the client is already floating */
91 if (con_is_floating(con)) {
92 LOG("already floating, re-setting to tiling\n");
94 floating_disable(con, automatic);
98 floating_enable(con, automatic);
103 DLOG("Not putting dock client into floating mode\n");
108 DLOG("This client is already in floating (container == NULL), re-inserting\n");
110 Workspace *ws = client->workspace;
111 SLIST_FOREACH(next_tiling, &(ws->focus_stack), focus_clients)
112 if (!client_is_floating(next_tiling))
114 /* If there are no tiling clients on this workspace, there can only be one
115 * container: the first one */
116 if (next_tiling == TAILQ_END(&(ws->focus_stack)))
117 con = ws->table[0][0];
118 else con = next_tiling->container;
120 /* Remove the client from the list of floating clients */
121 TAILQ_REMOVE(&(ws->floating_clients), client, floating_clients);
123 DLOG("destination container = %p\n", con);
124 Client *old_focused = con->currently_focused;
125 /* Preserve position/size */
126 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
128 client->floating = FLOATING_USER_OFF;
129 client->container = con;
131 if (old_focused != NULL && !old_focused->dock)
132 CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
133 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
135 DLOG("Re-inserted the window.\n");
136 con->currently_focused = client;
138 client_set_below_floating(conn, client);
140 render_container(conn, con);
146 DLOG("Entering floating for client %08x\n", client->child);
148 /* Remove the client of its container */
149 client_remove_from_container(conn, client, con, false);
150 client->container = NULL;
152 /* Add the client to the list of floating clients for its workspace */
153 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
155 if (con->currently_focused == client) {
156 DLOG("Need to re-adjust currently_focused\n");
157 /* Get the next client in the focus stack for this particular container */
158 con->currently_focused = get_last_focused_client(conn, con, NULL);
162 client->floating = FLOATING_AUTO_ON;
163 else client->floating = FLOATING_USER_ON;
165 /* Initialize the floating position from the position in tiling mode, if this
166 * client never was floating (x == -1) */
167 if (client->floating_rect.x == -1) {
168 /* Copy over the position */
169 client->floating_rect.x = client->rect.x;
170 client->floating_rect.y = client->rect.y;
172 /* Copy size the other direction */
173 client->child_rect.width = client->floating_rect.width;
174 client->child_rect.height = client->floating_rect.height;
176 client->rect.width = client->child_rect.width + 2 + 2;
177 client->rect.height = client->child_rect.height + (font->height + 2 + 2) + 2;
179 DLOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
180 client->floating_rect.width, client->floating_rect.height);
182 /* If the client was already in floating before we restore the old position / size */
183 DLOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
184 client->floating_rect.width, client->floating_rect.height);
185 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
188 /* Raise the client */
189 xcb_raise_window(conn, client->frame);
190 reposition_client(conn, client);
191 resize_client(conn, client);
192 /* redecorate_window flushes */
193 redecorate_window(conn, client);
195 /* Re-render the tiling layout of this container */
196 render_container(conn, con);
203 * Removes the floating client from its workspace and attaches it to the new workspace.
204 * This is centralized here because it may happen if you move it via keyboard and
205 * if you move it using your mouse.
208 void floating_assign_to_workspace(Client *client, Workspace *new_workspace) {
209 /* Remove from focus stack and list of floating clients */
210 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
211 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
213 if (client->workspace->fullscreen_client == client)
214 client->workspace->fullscreen_client = NULL;
216 /* Insert into destination focus stack and list of floating clients */
217 client->workspace = new_workspace;
218 SLIST_INSERT_HEAD(&(client->workspace->focus_stack), client, focus_clients);
219 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
220 if (client->fullscreen)
221 client->workspace->fullscreen_client = client;
225 * This is an ugly data structure which we need because there is no standard
226 * way of having nested functions (only available as a gcc extension at the
227 * moment, clang doesn’t support it) or blocks (only available as a clang
228 * extension and only on Mac OS X systems at the moment).
231 struct resize_callback_params {
233 xcb_button_press_event_t *event;
236 DRAGGING_CB(resize_callback) {
237 struct resize_callback_params *params = extra;
238 xcb_button_press_event_t *event = params->event;
239 switch (params->border) {
241 int new_width = old_rect->width + (new_x - event->root_x);
242 if ((new_width < 0) ||
243 (new_width < client_min_width(client) && client->rect.width >= new_width))
245 client->rect.width = new_width;
249 case BORDER_BOTTOM: {
250 int new_height = old_rect->height + (new_y - event->root_y);
251 if ((new_height < 0) ||
252 (new_height < client_min_height(client) && client->rect.height >= new_height))
254 client->rect.height = old_rect->height + (new_y - event->root_y);
259 int new_height = old_rect->height + (event->root_y - new_y);
260 if ((new_height < 0) ||
261 (new_height < client_min_height(client) && client->rect.height >= new_height))
264 client->rect.y = old_rect->y + (new_y - event->root_y);
265 client->rect.height = new_height;
270 int new_width = old_rect->width + (event->root_x - new_x);
271 if ((new_width < 0) ||
272 (new_width < client_min_width(client) && client->rect.width >= new_width))
274 client->rect.x = old_rect->x + (new_x - event->root_x);
275 client->rect.width = new_width;
280 /* Push the new position/size to X11 */
281 reposition_client(conn, client);
282 resize_client(conn, client);
288 * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
289 * Determines on which border the user clicked and launches the drag_pointer function
290 * with the resize_callback.
293 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
294 DLOG("floating border click\n");
298 if (event->event_y < 2)
300 else if (event->event_y >= (client->rect.height - 2))
301 border = BORDER_BOTTOM;
302 else if (event->event_x <= 2)
303 border = BORDER_LEFT;
304 else if (event->event_x >= (client->rect.width - 2))
305 border = BORDER_RIGHT;
307 DLOG("Not on any border, not doing anything.\n");
311 DLOG("border = %d\n", border);
313 struct resize_callback_params params = { border, event };
315 drag_pointer(conn, client, event, XCB_NONE, border, resize_callback, ¶ms);
321 DRAGGING_CB(drag_window_callback) {
322 struct xcb_button_press_event_t *event = extra;
324 /* Reposition the client correctly while moving */
325 con->rect.x = old_rect->x + (new_x - event->root_x);
326 con->rect.y = old_rect->y + (new_y - event->root_y);
327 /* TODO: don’t re-render the whole tree just because we change
328 * coordinates of a floating window */
330 x_push_changes(croot);
334 * Called when the user clicked on the titlebar of a floating window.
335 * Calls the drag_pointer function with the drag_window callback
338 void floating_drag_window(Con *con, xcb_button_press_event_t *event) {
339 DLOG("floating_drag_window\n");
341 drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback, event);
346 * This is an ugly data structure which we need because there is no standard
347 * way of having nested functions (only available as a gcc extension at the
348 * moment, clang doesn’t support it) or blocks (only available as a clang
349 * extension and only on Mac OS X systems at the moment).
352 struct resize_window_callback_params {
355 xcb_button_press_event_t *event;
358 DRAGGING_CB(resize_window_callback) {
359 struct resize_window_callback_params *params = extra;
360 xcb_button_press_event_t *event = params->event;
361 border_t corner = params->corner;
363 int32_t dest_x = con->rect.x;
364 int32_t dest_y = con->rect.y;
366 uint32_t dest_height;
368 double ratio = (double) old_rect->width / old_rect->height;
370 /* First guess: We resize by exactly the amount the mouse moved,
371 * taking into account in which corner the client was grabbed */
372 if (corner & BORDER_LEFT)
373 dest_width = old_rect->width - (new_x - event->root_x);
374 else dest_width = old_rect->width + (new_x - event->root_x);
376 if (corner & BORDER_TOP)
377 dest_height = old_rect->height - (new_y - event->root_y);
378 else dest_height = old_rect->height + (new_y - event->root_y);
380 /* TODO: minimum window size */
382 /* Obey minimum window size */
383 dest_width = max(dest_width, client_min_width(client));
384 dest_height = max(dest_height, client_min_height(client));
387 /* User wants to keep proportions, so we may have to adjust our values */
388 if (params->proportional) {
389 dest_width = max(dest_width, (int) (dest_height * ratio));
390 dest_height = max(dest_height, (int) (dest_width / ratio));
393 /* If not the lower right corner is grabbed, we must also reposition
394 * the client by exactly the amount we resized it */
395 if (corner & BORDER_LEFT)
396 dest_x = old_rect->x + (old_rect->width - dest_width);
398 if (corner & BORDER_TOP)
399 dest_y = old_rect->y + (old_rect->height - dest_height);
401 con->rect = (Rect) { dest_x, dest_y, dest_width, dest_height };
403 /* TODO: don’t re-render the whole tree just because we change
404 * coordinates of a floating window */
406 x_push_changes(croot);
410 * Called when the user clicked on a floating window while holding the
411 * floating_modifier and the right mouse button.
412 * Calls the drag_pointer function with the resize_window callback
415 void floating_resize_window(Con *con, bool proportional,
416 xcb_button_press_event_t *event) {
417 DLOG("floating_resize_window\n");
419 /* corner saves the nearest corner to the original click. It contains
420 * a bitmask of the nearest borders (BORDER_LEFT, BORDER_RIGHT, …) */
423 if (event->event_x <= (con->rect.width / 2))
424 corner |= BORDER_LEFT;
425 else corner |= BORDER_RIGHT;
427 if (event->event_y <= (con->rect.height / 2))
428 corner |= BORDER_TOP;
429 else corner |= BORDER_RIGHT;
431 struct resize_window_callback_params params = { corner, proportional, event };
433 drag_pointer(con, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback, ¶ms);
437 * This function grabs your pointer and lets you drag stuff around (borders).
438 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
439 * and the given callback will be called with the parameters specified (client,
440 * border on which the click originally was), the original rect of the client,
441 * the event and the new coordinates (x, y).
444 void drag_pointer(Con *con, xcb_button_press_event_t *event, xcb_window_t
445 confine_to, border_t border, callback_t callback, void *extra)
447 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
448 uint32_t new_x, new_y;
451 memcpy(&old_rect, &(con->rect), sizeof(Rect));
453 /* Grab the pointer */
454 /* TODO: returncode */
455 xcb_grab_pointer(conn,
456 false, /* get all pointer events specified by the following mask */
457 root, /* grab the root window */
458 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
459 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
460 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
461 confine_to, /* confine_to = in which window should the cursor stay */
462 XCB_NONE, /* don’t display a special cursor */
465 /* Go into our own event loop */
468 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
469 /* I’ve always wanted to have my own eventhandler… */
470 while ((inside_event = xcb_wait_for_event(conn))) {
471 /* We now handle all events we can get using xcb_poll_for_event */
473 /* Same as get_event_handler in xcb */
474 int nr = inside_event->response_type;
476 /* An error occured */
477 //handle_event(NULL, conn, inside_event);
482 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
486 case XCB_BUTTON_RELEASE:
489 case XCB_MOTION_NOTIFY:
490 /* motion_notify events are saved for later */
491 FREE(last_motion_notify);
492 last_motion_notify = inside_event;
495 case XCB_UNMAP_NOTIFY:
496 DLOG("Unmap-notify, aborting\n");
497 xcb_event_handle(&evenths, inside_event);
501 DLOG("Passing to original handler\n");
502 /* Use original handler */
503 xcb_event_handle(&evenths, inside_event);
506 if (last_motion_notify != inside_event)
508 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
510 if (last_motion_notify == NULL)
513 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
514 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
516 callback(con, &old_rect, new_x, new_y, extra);
517 FREE(last_motion_notify);
520 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
526 * Changes focus in the given direction for floating clients.
528 * Changing to the left/right means going to the previous/next floating client,
529 * changing to top/bottom means cycling through the Z-index.
532 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
533 DLOG("floating focus\n");
535 if (direction == D_LEFT || direction == D_RIGHT) {
536 /* Go to the next/previous floating client */
539 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
540 TAILQ_NEXT(currently_focused, floating_clients))) !=
541 TAILQ_END(&(currently_focused->workspace->floating_clients))) {
542 if (!client->floating)
544 set_focus(conn, client, true);
551 * Moves the client 10px to the specified direction.
554 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
555 DLOG("floating move\n");
557 Rect destination = currently_focused->rect;
558 Rect *screen = &(currently_focused->workspace->output->rect);
573 /* to make static analyzers happy */
578 /* Prevent windows from vanishing completely */
579 if ((int32_t)(destination.x + destination.width - 5) <= (int32_t)screen->x ||
580 (int32_t)(destination.x + 5) >= (int32_t)(screen->x + screen->width) ||
581 (int32_t)(destination.y + destination.height - 5) <= (int32_t)screen->y ||
582 (int32_t)(destination.y + 5) >= (int32_t)(screen->y + screen->height)) {
583 DLOG("boundary check failed, not moving\n");
587 currently_focused->rect = destination;
588 reposition_client(conn, currently_focused);
590 /* Because reposition_client does not send a faked configure event (only resize does),
591 * we need to initiate that on our own */
592 fake_absolute_configure_notify(conn, currently_focused);
593 /* fake_absolute_configure_notify flushes */
597 * Hides all floating clients (or show them if they are currently hidden) on
598 * the specified workspace.
601 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
604 workspace->floating_hidden = !workspace->floating_hidden;
605 DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
606 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
607 if (workspace->floating_hidden)
608 client_unmap(conn, client);
609 else client_map(conn, client);
612 /* If we just unmapped all floating windows we should ensure that the focus
613 * is set correctly, that ist, to the first non-floating client in stack */
614 if (workspace->floating_hidden)
615 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
616 if (client_is_floating(client))
618 set_focus(conn, client, true);