4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * src/floating.c: contains all functions for handling floating clients
18 #include <xcb/xcb_event.h>
29 #include "workspace.h"
32 * Toggles floating mode for the given client.
33 * Correctly takes care of the position/size (separately stored for tiling/floating mode)
34 * and repositions/resizes/redecorates the client.
36 * If the automatic flag is set to true, this was an automatic update by a change of the
37 * window class from the application which can be overwritten by the user.
40 void toggle_floating_mode(xcb_connection_t *conn, Client *client, bool automatic) {
41 Container *con = client->container;
42 i3Font *font = load_font(conn, config.font);
45 LOG("This client is already in floating (container == NULL), re-inserting\n");
47 Workspace *ws = client->workspace;
48 SLIST_FOREACH(next_tiling, &(ws->focus_stack), focus_clients)
49 if (!client_is_floating(next_tiling))
51 /* If there are no tiling clients on this workspace, there can only be one
52 * container: the first one */
53 if (next_tiling == TAILQ_END(&(ws->focus_stack)))
54 con = ws->table[0][0];
55 else con = next_tiling->container;
57 /* Remove the client from the list of floating clients */
58 TAILQ_REMOVE(&(ws->floating_clients), client, floating_clients);
60 LOG("destination container = %p\n", con);
61 Client *old_focused = con->currently_focused;
62 /* Preserve position/size */
63 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
65 client->floating = FLOATING_USER_OFF;
66 client->container = con;
68 if (old_focused != NULL && !old_focused->dock)
69 CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
70 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
72 LOG("Re-inserted the client into the matrix.\n");
73 con->currently_focused = client;
75 client_set_below_floating(conn, client);
77 render_container(conn, con);
83 LOG("Entering floating for client %08x\n", client->child);
85 /* Remove the client of its container */
86 client_remove_from_container(conn, client, con, false);
87 client->container = NULL;
89 /* Add the client to the list of floating clients for its workspace */
90 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
92 if (con->currently_focused == client) {
93 LOG("Need to re-adjust currently_focused\n");
94 /* Get the next client in the focus stack for this particular container */
95 con->currently_focused = get_last_focused_client(conn, con, NULL);
99 client->floating = FLOATING_AUTO_ON;
100 else client->floating = FLOATING_USER_ON;
102 /* Initialize the floating position from the position in tiling mode, if this
103 * client never was floating (x == -1) */
104 if (client->floating_rect.x == -1) {
105 /* Copy over the position */
106 client->floating_rect.x = client->rect.x;
107 client->floating_rect.y = client->rect.y;
109 /* Copy size the other direction */
110 client->child_rect.width = client->floating_rect.width;
111 client->child_rect.height = client->floating_rect.height;
113 client->rect.width = client->child_rect.width + 2 + 2;
114 client->rect.height = client->child_rect.height + (font->height + 2 + 2) + 2;
116 LOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
117 client->floating_rect.width, client->floating_rect.height);
119 /* If the client was already in floating before we restore the old position / size */
120 LOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
121 client->floating_rect.width, client->floating_rect.height);
122 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
125 /* Raise the client */
126 xcb_raise_window(conn, client->frame);
127 reposition_client(conn, client);
128 resize_client(conn, client);
129 /* redecorate_window flushes */
130 redecorate_window(conn, client);
132 /* Re-render the tiling layout of this container */
133 render_container(conn, con);
138 * Removes the floating client from its workspace and attaches it to the new workspace.
139 * This is centralized here because it may happen if you move it via keyboard and
140 * if you move it using your mouse.
143 void floating_assign_to_workspace(Client *client, Workspace *new_workspace) {
144 Workspace *ws = client->workspace;
146 /* Remove from focus stack and list of floating clients */
147 SLIST_REMOVE(&(ws->focus_stack), client, Client, focus_clients);
148 TAILQ_REMOVE(&(ws->floating_clients), client, floating_clients);
150 if (ws->fullscreen_client == client)
151 ws->fullscreen_client = NULL;
153 /* Insert into destination focus stack and list of floating clients */
155 SLIST_INSERT_HEAD(&(ws->focus_stack), client, focus_clients);
156 TAILQ_INSERT_TAIL(&(ws->floating_clients), client, floating_clients);
157 if (client->fullscreen)
158 ws->fullscreen_client = client;
162 * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
163 * Determines on which border the user clicked and launches the drag_pointer function
164 * with the resize_callback.
167 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
169 LOG("floating border click\n");
173 void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
176 int new_width = old_rect->width + (new_x - event->root_x);
177 if ((new_width < 0) ||
178 (new_width < 50 && client->rect.width >= new_width))
180 client->rect.width = new_width;
184 case BORDER_BOTTOM: {
185 int new_height = old_rect->height + (new_y - event->root_y);
186 if ((new_height < 0) ||
187 (new_height < 20 && client->rect.height >= new_height))
189 client->rect.height = old_rect->height + (new_y - event->root_y);
194 int new_height = old_rect->height + (event->root_y - new_y);
195 if ((new_height < 0) ||
196 (new_height < 20 && client->rect.height >= new_height))
199 client->rect.y = old_rect->y + (new_y - event->root_y);
200 client->rect.height = new_height;
205 int new_width = old_rect->width + (event->root_x - new_x);
206 if ((new_width < 0) ||
207 (new_width < 50 && client->rect.width >= new_width))
209 client->rect.x = old_rect->x + (new_x - event->root_x);
210 client->rect.width = new_width;
215 /* Push the new position/size to X11 */
216 reposition_client(conn, client);
217 resize_client(conn, client);
221 if (event->event_y < 2)
223 else if (event->event_y >= (client->rect.height - 2))
224 border = BORDER_BOTTOM;
225 else if (event->event_x <= 2)
226 border = BORDER_LEFT;
227 else if (event->event_x >= (client->rect.width - 2))
228 border = BORDER_RIGHT;
230 LOG("Not on any border, not doing anything.\n");
234 LOG("border = %d\n", border);
236 drag_pointer(conn, client, event, XCB_NONE, border, resize_callback);
243 * Called when the user clicked on the titlebar of a floating window.
244 * Calls the drag_pointer function with the drag_window callback
247 void floating_drag_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
248 LOG("floating_drag_window\n");
250 void drag_window_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
251 /* Reposition the client correctly while moving */
252 client->rect.x = old_rect->x + (new_x - event->root_x);
253 client->rect.y = old_rect->y + (new_y - event->root_y);
254 reposition_client(conn, client);
255 /* Because reposition_client does not send a faked configure event (only resize does),
256 * we need to initiate that on our own */
257 fake_absolute_configure_notify(conn, client);
258 /* fake_absolute_configure_notify flushes */
261 drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback);
265 * Called when the user right-clicked on the titlebar of a floating window to
267 * Calls the drag_pointer function with the resize_window callback
270 void floating_resize_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
271 LOG("floating_resize_window\n");
273 void resize_window_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
274 int32_t new_width = old_rect->width + (new_x - event->root_x);
275 int32_t new_height = old_rect->height + (new_y - event->root_y);
276 /* Obey minimum window size */
277 if (new_width < 75 || new_height < 50)
280 /* Reposition the client correctly while moving */
281 client->rect.width = new_width;
282 client->rect.height = new_height;
284 /* resize_client flushes */
285 resize_client(conn, client);
288 drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback);
293 * This function grabs your pointer and lets you drag stuff around (borders).
294 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
295 * and the given callback will be called with the parameters specified (client,
296 * border on which the click originally was), the original rect of the client,
297 * the event and the new coordinates (x, y).
300 void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
301 xcb_window_t confine_to, border_t border, callback_t callback) {
302 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
303 uint32_t new_x, new_y;
306 memcpy(&old_rect, &(client->rect), sizeof(Rect));
308 /* Grab the pointer */
309 /* TODO: returncode */
310 xcb_grab_pointer(conn,
311 false, /* get all pointer events specified by the following mask */
312 root, /* grab the root window */
313 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
314 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
315 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
316 confine_to, /* confine_to = in which window should the cursor stay */
317 XCB_NONE, /* don’t display a special cursor */
320 /* Go into our own event loop */
323 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
324 /* I’ve always wanted to have my own eventhandler… */
325 while ((inside_event = xcb_wait_for_event(conn))) {
326 /* We now handle all events we can get using xcb_poll_for_event */
328 /* Same as get_event_handler in xcb */
329 int nr = inside_event->response_type;
331 /* An error occured */
332 handle_event(NULL, conn, inside_event);
337 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
341 case XCB_BUTTON_RELEASE:
344 case XCB_MOTION_NOTIFY:
345 /* motion_notify events are saved for later */
346 FREE(last_motion_notify);
347 last_motion_notify = inside_event;
350 case XCB_UNMAP_NOTIFY:
351 LOG("Unmap-notify, aborting\n");
352 xcb_event_handle(&evenths, inside_event);
356 LOG("Passing to original handler\n");
357 /* Use original handler */
358 xcb_event_handle(&evenths, inside_event);
361 if (last_motion_notify != inside_event)
363 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
365 if (last_motion_notify == NULL)
368 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
369 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
371 callback(&old_rect, new_x, new_y);
372 FREE(last_motion_notify);
375 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
380 * Changes focus in the given direction for floating clients.
382 * Changing to the left/right means going to the previous/next floating client,
383 * changing to top/bottom means cycling through the Z-index.
386 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
387 LOG("floating focus\n");
389 if (direction == D_LEFT || direction == D_RIGHT) {
390 /* Go to the next/previous floating client */
393 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
394 TAILQ_NEXT(currently_focused, floating_clients))) !=
395 TAILQ_END(&(currently_focused->workspace->floating_clients))) {
396 if (!client->floating)
398 set_focus(conn, client, true);
405 * Moves the client 10px to the specified direction.
408 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
409 LOG("floating move\n");
413 if (currently_focused->rect.x < 10)
415 currently_focused->rect.x -= 10;
418 currently_focused->rect.x += 10;
421 if (currently_focused->rect.y < 10)
423 currently_focused->rect.y -= 10;
426 currently_focused->rect.y += 10;
428 /* to make static analyzers happy */
433 reposition_client(conn, currently_focused);
435 /* Because reposition_client does not send a faked configure event (only resize does),
436 * we need to initiate that on our own */
437 fake_absolute_configure_notify(conn, currently_focused);
438 /* fake_absolute_configure_notify flushes */
442 * Hides all floating clients (or show them if they are currently hidden) on
443 * the specified workspace.
446 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
449 workspace->floating_hidden = !workspace->floating_hidden;
450 LOG("floating_hidden is now: %d\n", workspace->floating_hidden);
451 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
452 if (workspace->floating_hidden)
453 client_unmap(conn, client);
454 else client_map(conn, client);
457 /* If we just unmapped all floating windows we should ensure that the focus
458 * is set correctly, that ist, to the first non-floating client in stack */
459 if (workspace->floating_hidden)
460 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
461 if (client_is_floating(client))
463 set_focus(conn, client, true);