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"
33 * Toggles floating mode for the given client.
34 * Correctly takes care of the position/size (separately stored for tiling/floating mode)
35 * and repositions/resizes/redecorates the client.
37 * If the automatic flag is set to true, this was an automatic update by a change of the
38 * window class from the application which can be overwritten by the user.
41 void toggle_floating_mode(xcb_connection_t *conn, Client *client, bool automatic) {
42 Container *con = client->container;
43 i3Font *font = load_font(conn, config.font);
46 DLOG("This client is already in floating (container == NULL), re-inserting\n");
48 Workspace *ws = client->workspace;
49 SLIST_FOREACH(next_tiling, &(ws->focus_stack), focus_clients)
50 if (!client_is_floating(next_tiling))
52 /* If there are no tiling clients on this workspace, there can only be one
53 * container: the first one */
54 if (next_tiling == TAILQ_END(&(ws->focus_stack)))
55 con = ws->table[0][0];
56 else con = next_tiling->container;
58 /* Remove the client from the list of floating clients */
59 TAILQ_REMOVE(&(ws->floating_clients), client, floating_clients);
61 DLOG("destination container = %p\n", con);
62 Client *old_focused = con->currently_focused;
63 /* Preserve position/size */
64 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
66 client->floating = FLOATING_USER_OFF;
67 client->container = con;
69 if (old_focused != NULL && !old_focused->dock)
70 CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
71 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
73 DLOG("Re-inserted the window.\n");
74 con->currently_focused = client;
76 client_set_below_floating(conn, client);
78 render_container(conn, con);
84 DLOG("Entering floating for client %08x\n", client->child);
86 /* Remove the client of its container */
87 client_remove_from_container(conn, client, con, false);
88 client->container = NULL;
90 /* Add the client to the list of floating clients for its workspace */
91 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
93 if (con->currently_focused == client) {
94 DLOG("Need to re-adjust currently_focused\n");
95 /* Get the next client in the focus stack for this particular container */
96 con->currently_focused = get_last_focused_client(conn, con, NULL);
100 client->floating = FLOATING_AUTO_ON;
101 else client->floating = FLOATING_USER_ON;
103 /* Initialize the floating position from the position in tiling mode, if this
104 * client never was floating (x == -1) */
105 if (client->floating_rect.x == -1) {
106 /* Copy over the position */
107 client->floating_rect.x = client->rect.x;
108 client->floating_rect.y = client->rect.y;
110 /* Copy size the other direction */
111 client->child_rect.width = client->floating_rect.width;
112 client->child_rect.height = client->floating_rect.height;
114 client->rect.width = client->child_rect.width + 2 + 2;
115 client->rect.height = client->child_rect.height + (font->height + 2 + 2) + 2;
117 DLOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
118 client->floating_rect.width, client->floating_rect.height);
120 /* If the client was already in floating before we restore the old position / size */
121 DLOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
122 client->floating_rect.width, client->floating_rect.height);
123 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
126 /* Raise the client */
127 xcb_raise_window(conn, client->frame);
128 reposition_client(conn, client);
129 resize_client(conn, client);
130 /* redecorate_window flushes */
131 redecorate_window(conn, client);
133 /* Re-render the tiling layout of this container */
134 render_container(conn, con);
139 * Removes the floating client from its workspace and attaches it to the new workspace.
140 * This is centralized here because it may happen if you move it via keyboard and
141 * if you move it using your mouse.
144 void floating_assign_to_workspace(Client *client, Workspace *new_workspace) {
145 /* Remove from focus stack and list of floating clients */
146 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
147 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
149 if (client->workspace->fullscreen_client == client)
150 client->workspace->fullscreen_client = NULL;
152 /* Insert into destination focus stack and list of floating clients */
153 client->workspace = new_workspace;
154 SLIST_INSERT_HEAD(&(client->workspace->focus_stack), client, focus_clients);
155 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
156 if (client->fullscreen)
157 client->workspace->fullscreen_client = client;
161 * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
162 * Determines on which border the user clicked and launches the drag_pointer function
163 * with the resize_callback.
166 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
167 DLOG("floating border click\n");
171 void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
174 int new_width = old_rect->width + (new_x - event->root_x);
175 if ((new_width < 0) ||
176 (new_width < client_min_width(client) && client->rect.width >= new_width))
178 client->rect.width = new_width;
182 case BORDER_BOTTOM: {
183 int new_height = old_rect->height + (new_y - event->root_y);
184 if ((new_height < 0) ||
185 (new_height < client_min_height(client) && client->rect.height >= new_height))
187 client->rect.height = old_rect->height + (new_y - event->root_y);
192 int new_height = old_rect->height + (event->root_y - new_y);
193 if ((new_height < 0) ||
194 (new_height < client_min_height(client) && client->rect.height >= new_height))
197 client->rect.y = old_rect->y + (new_y - event->root_y);
198 client->rect.height = new_height;
203 int new_width = old_rect->width + (event->root_x - new_x);
204 if ((new_width < 0) ||
205 (new_width < client_min_width(client) && client->rect.width >= new_width))
207 client->rect.x = old_rect->x + (new_x - event->root_x);
208 client->rect.width = new_width;
213 /* Push the new position/size to X11 */
214 reposition_client(conn, client);
215 resize_client(conn, client);
219 if (event->event_y < 2)
221 else if (event->event_y >= (client->rect.height - 2))
222 border = BORDER_BOTTOM;
223 else if (event->event_x <= 2)
224 border = BORDER_LEFT;
225 else if (event->event_x >= (client->rect.width - 2))
226 border = BORDER_RIGHT;
228 DLOG("Not on any border, not doing anything.\n");
232 DLOG("border = %d\n", border);
234 drag_pointer(conn, client, event, XCB_NONE, border, resize_callback);
241 * Called when the user clicked on the titlebar of a floating window.
242 * Calls the drag_pointer function with the drag_window callback
245 void floating_drag_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
246 DLOG("floating_drag_window\n");
248 void drag_window_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
249 /* Reposition the client correctly while moving */
250 client->rect.x = old_rect->x + (new_x - event->root_x);
251 client->rect.y = old_rect->y + (new_y - event->root_y);
252 reposition_client(conn, client);
253 /* Because reposition_client does not send a faked configure event (only resize does),
254 * we need to initiate that on our own */
255 fake_absolute_configure_notify(conn, client);
256 /* fake_absolute_configure_notify flushes */
259 drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback);
263 * Called when the user right-clicked on the titlebar of a floating window to
265 * Calls the drag_pointer function with the resize_window callback
268 void floating_resize_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
269 DLOG("floating_resize_window\n");
271 void resize_window_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
272 int32_t new_width = old_rect->width + (new_x - event->root_x);
273 int32_t new_height = old_rect->height + (new_y - event->root_y);
275 /* Obey minimum window size and reposition the client */
276 if (new_width > 0 && new_width >= client_min_width(client))
277 client->rect.width = new_width;
279 if (new_height > 0 && new_height >= client_min_height(client))
280 client->rect.height = new_height;
282 /* resize_client flushes */
283 resize_client(conn, client);
286 drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback);
291 * This function grabs your pointer and lets you drag stuff around (borders).
292 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
293 * and the given callback will be called with the parameters specified (client,
294 * border on which the click originally was), the original rect of the client,
295 * the event and the new coordinates (x, y).
298 void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
299 xcb_window_t confine_to, border_t border, callback_t callback) {
300 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
301 uint32_t new_x, new_y;
304 memcpy(&old_rect, &(client->rect), sizeof(Rect));
306 /* Grab the pointer */
307 /* TODO: returncode */
308 xcb_grab_pointer(conn,
309 false, /* get all pointer events specified by the following mask */
310 root, /* grab the root window */
311 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
312 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
313 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
314 confine_to, /* confine_to = in which window should the cursor stay */
315 XCB_NONE, /* don’t display a special cursor */
318 /* Go into our own event loop */
321 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
322 /* I’ve always wanted to have my own eventhandler… */
323 while ((inside_event = xcb_wait_for_event(conn))) {
324 /* We now handle all events we can get using xcb_poll_for_event */
326 /* Same as get_event_handler in xcb */
327 int nr = inside_event->response_type;
329 /* An error occured */
330 handle_event(NULL, conn, inside_event);
335 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
339 case XCB_BUTTON_RELEASE:
342 case XCB_MOTION_NOTIFY:
343 /* motion_notify events are saved for later */
344 FREE(last_motion_notify);
345 last_motion_notify = inside_event;
348 case XCB_UNMAP_NOTIFY:
349 DLOG("Unmap-notify, aborting\n");
350 xcb_event_handle(&evenths, inside_event);
354 DLOG("Passing to original handler\n");
355 /* Use original handler */
356 xcb_event_handle(&evenths, inside_event);
359 if (last_motion_notify != inside_event)
361 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
363 if (last_motion_notify == NULL)
366 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
367 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
369 callback(&old_rect, new_x, new_y);
370 FREE(last_motion_notify);
373 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
378 * Changes focus in the given direction for floating clients.
380 * Changing to the left/right means going to the previous/next floating client,
381 * changing to top/bottom means cycling through the Z-index.
384 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
385 DLOG("floating focus\n");
387 if (direction == D_LEFT || direction == D_RIGHT) {
388 /* Go to the next/previous floating client */
391 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
392 TAILQ_NEXT(currently_focused, floating_clients))) !=
393 TAILQ_END(&(currently_focused->workspace->floating_clients))) {
394 if (!client->floating)
396 set_focus(conn, client, true);
403 * Moves the client 10px to the specified direction.
406 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
407 DLOG("floating move\n");
411 if (currently_focused->rect.x < 10)
413 currently_focused->rect.x -= 10;
416 currently_focused->rect.x += 10;
419 if (currently_focused->rect.y < 10)
421 currently_focused->rect.y -= 10;
424 currently_focused->rect.y += 10;
426 /* to make static analyzers happy */
431 reposition_client(conn, currently_focused);
433 /* Because reposition_client does not send a faked configure event (only resize does),
434 * we need to initiate that on our own */
435 fake_absolute_configure_notify(conn, currently_focused);
436 /* fake_absolute_configure_notify flushes */
440 * Hides all floating clients (or show them if they are currently hidden) on
441 * the specified workspace.
444 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
447 workspace->floating_hidden = !workspace->floating_hidden;
448 DLOG("floating_hidden is now: %d\n", workspace->floating_hidden);
449 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
450 if (workspace->floating_hidden)
451 client_unmap(conn, client);
452 else client_map(conn, client);
455 /* If we just unmapped all floating windows we should ensure that the focus
456 * is set correctly, that ist, to the first non-floating client in stack */
457 if (workspace->floating_hidden)
458 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
459 if (client_is_floating(client))
461 set_focus(conn, client, true);