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>
31 * Toggles floating mode for the given client.
32 * Correctly takes care of the position/size (separately stored for tiling/floating mode)
33 * and repositions/resizes/redecorates the client.
35 * If the automatic flag is set to true, this was an automatic update by a change of the
36 * window class from the application which can be overwritten by the user.
39 void toggle_floating_mode(xcb_connection_t *conn, Client *client, bool automatic) {
40 Container *con = client->container;
41 i3Font *font = load_font(conn, config.font);
44 LOG("This client is already in floating (container == NULL), re-inserting\n");
46 SLIST_FOREACH(next_tiling, &(client->workspace->focus_stack), focus_clients)
47 if (!client_is_floating(next_tiling))
49 /* If there are no tiling clients on this workspace, there can only be one
50 * container: the first one */
51 if (next_tiling == TAILQ_END(&(client->workspace->focus_stack)))
52 con = client->workspace->table[0][0];
53 else con = next_tiling->container;
55 /* Remove the client from the list of floating clients */
56 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
58 LOG("destination container = %p\n", con);
59 Client *old_focused = con->currently_focused;
60 /* Preserve position/size */
61 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
63 client->floating = FLOATING_USER_OFF;
64 client->container = con;
66 if (old_focused != NULL && !old_focused->dock)
67 CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
68 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
70 LOG("Re-inserted the client into the matrix.\n");
71 con->currently_focused = client;
73 client_set_below_floating(conn, client);
75 render_container(conn, con);
81 LOG("Entering floating for client %08x\n", client->child);
83 /* Remove the client of its container */
84 client_remove_from_container(conn, client, con, false);
85 client->container = NULL;
87 /* Add the client to the list of floating clients for its workspace */
88 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
90 if (con->currently_focused == client) {
91 LOG("Need to re-adjust currently_focused\n");
92 /* Get the next client in the focus stack for this particular container */
93 con->currently_focused = get_last_focused_client(conn, con, NULL);
97 client->floating = FLOATING_AUTO_ON;
98 else client->floating = FLOATING_USER_ON;
100 /* Initialize the floating position from the position in tiling mode, if this
101 * client never was floating (x == -1) */
102 if (client->floating_rect.x == -1) {
103 /* Copy over the position */
104 client->floating_rect.x = client->rect.x;
105 client->floating_rect.y = client->rect.y;
107 /* Copy size the other direction */
108 client->child_rect.width = client->floating_rect.width;
109 client->child_rect.height = client->floating_rect.height;
111 client->rect.width = client->child_rect.width + 2 + 2;
112 client->rect.height = client->child_rect.height + (font->height + 2 + 2) + 2;
114 LOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
115 client->floating_rect.width, client->floating_rect.height);
117 /* If the client was already in floating before we restore the old position / size */
118 LOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
119 client->floating_rect.width, client->floating_rect.height);
120 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
123 /* Raise the client */
124 xcb_raise_window(conn, client->frame);
125 reposition_client(conn, client);
126 resize_client(conn, client);
127 /* redecorate_window flushes */
128 redecorate_window(conn, client);
130 /* Re-render the tiling layout of this container */
131 render_container(conn, con);
136 * Removes the floating client from its workspace and attaches it to the new workspace.
137 * This is centralized here because it may happen if you move it via keyboard and
138 * if you move it using your mouse.
141 void floating_assign_to_workspace(Client *client, Workspace *new_workspace) {
142 /* Remove from focus stack and list of floating clients */
143 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
144 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
146 if (client->workspace->fullscreen_client == client)
147 client->workspace->fullscreen_client = NULL;
149 /* Insert into destination focus stack and list of floating clients */
150 client->workspace = new_workspace;
151 SLIST_INSERT_HEAD(&(client->workspace->focus_stack), client, focus_clients);
152 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
153 if (client->fullscreen)
154 client->workspace->fullscreen_client = client;
159 * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
160 * Determines on which border the user clicked and launches the drag_pointer function
161 * with the resize_callback.
164 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
166 LOG("floating border click\n");
170 void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
173 int new_width = old_rect->width + (new_x - event->root_x);
174 if ((new_width < 0) ||
175 (new_width < 50 && client->rect.width >= new_width))
177 client->rect.width = new_width;
181 case BORDER_BOTTOM: {
182 int new_height = old_rect->height + (new_y - event->root_y);
183 if ((new_height < 0) ||
184 (new_height < 20 && client->rect.height >= new_height))
186 client->rect.height = old_rect->height + (new_y - event->root_y);
191 int new_height = old_rect->height + (event->root_y - new_y);
192 if ((new_height < 0) ||
193 (new_height < 20 && client->rect.height >= new_height))
196 client->rect.y = old_rect->y + (new_y - event->root_y);
197 client->rect.height = new_height;
202 int new_width = old_rect->width + (event->root_x - new_x);
203 if ((new_width < 0) ||
204 (new_width < 50 && client->rect.width >= new_width))
206 client->rect.x = old_rect->x + (new_x - event->root_x);
207 client->rect.width = new_width;
212 /* Push the new position/size to X11 */
213 reposition_client(conn, client);
214 resize_client(conn, client);
218 if (event->event_y < 2)
220 else if (event->event_y >= (client->rect.height - 2))
221 border = BORDER_BOTTOM;
222 else if (event->event_x <= 2)
223 border = BORDER_LEFT;
224 else if (event->event_x >= (client->rect.width - 2))
225 border = BORDER_RIGHT;
227 LOG("Not on any border, not doing anything.\n");
231 LOG("border = %d\n", border);
233 drag_pointer(conn, client, event, XCB_NONE, border, resize_callback);
240 * Called when the user clicked on the titlebar of a floating window.
241 * Calls the drag_pointer function with the drag_window callback
244 void floating_drag_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
245 LOG("floating_drag_window\n");
247 void drag_window_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
248 /* Reposition the client correctly while moving */
249 client->rect.x = old_rect->x + (new_x - event->root_x);
250 client->rect.y = old_rect->y + (new_y - event->root_y);
251 reposition_client(conn, client);
252 /* Because reposition_client does not send a faked configure event (only resize does),
253 * we need to initiate that on our own */
254 fake_absolute_configure_notify(conn, client);
255 /* fake_absolute_configure_notify flushes */
258 drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback);
262 * Called when the user right-clicked on the titlebar of a floating window to
264 * Calls the drag_pointer function with the resize_window callback
267 void floating_resize_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
268 LOG("floating_resize_window\n");
270 void resize_window_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
271 int32_t new_width = old_rect->width + (new_x - event->root_x);
272 int32_t new_height = old_rect->height + (new_y - event->root_y);
273 /* Obey minimum window size */
274 if (new_width < 75 || new_height < 50)
277 /* Reposition the client correctly while moving */
278 client->rect.width = new_width;
279 client->rect.height = new_height;
281 /* resize_client flushes */
282 resize_client(conn, client);
285 drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, resize_window_callback);
290 * This function grabs your pointer and lets you drag stuff around (borders).
291 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
292 * and the given callback will be called with the parameters specified (client,
293 * border on which the click originally was), the original rect of the client,
294 * the event and the new coordinates (x, y).
297 void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
298 xcb_window_t confine_to, border_t border, callback_t callback) {
299 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
300 uint32_t new_x, new_y;
303 memcpy(&old_rect, &(client->rect), sizeof(Rect));
305 /* Grab the pointer */
306 /* TODO: returncode */
307 xcb_grab_pointer(conn,
308 false, /* get all pointer events specified by the following mask */
309 root, /* grab the root window */
310 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
311 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
312 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
313 confine_to, /* confine_to = in which window should the cursor stay */
314 XCB_NONE, /* don’t display a special cursor */
317 /* Go into our own event loop */
320 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
321 /* I’ve always wanted to have my own eventhandler… */
322 while ((inside_event = xcb_wait_for_event(conn))) {
323 /* We now handle all events we can get using xcb_poll_for_event */
325 /* Same as get_event_handler in xcb */
326 int nr = inside_event->response_type;
328 /* An error occured */
329 handle_event(NULL, conn, inside_event);
334 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
338 case XCB_BUTTON_RELEASE:
341 case XCB_MOTION_NOTIFY:
342 /* motion_notify events are saved for later */
343 FREE(last_motion_notify);
344 last_motion_notify = inside_event;
347 case XCB_UNMAP_NOTIFY:
348 LOG("Unmap-notify, aborting\n");
349 xcb_event_handle(&evenths, inside_event);
353 LOG("Passing to original handler\n");
354 /* Use original handler */
355 xcb_event_handle(&evenths, inside_event);
358 if (last_motion_notify != inside_event)
360 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
362 if (last_motion_notify == NULL)
365 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
366 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
368 callback(&old_rect, new_x, new_y);
369 FREE(last_motion_notify);
372 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
377 * Changes focus in the given direction for floating clients.
379 * Changing to the left/right means going to the previous/next floating client,
380 * changing to top/bottom means cycling through the Z-index.
383 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
384 LOG("floating focus\n");
386 if (direction == D_LEFT || direction == D_RIGHT) {
387 /* Go to the next/previous floating client */
390 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
391 TAILQ_NEXT(currently_focused, floating_clients))) !=
392 TAILQ_END(&(currently_focused->workspace->floating_clients))) {
393 if (!client->floating)
395 set_focus(conn, client, true);
402 * Moves the client 10px to the specified direction.
405 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
406 LOG("floating move\n");
410 if (currently_focused->rect.x < 10)
412 currently_focused->rect.x -= 10;
415 currently_focused->rect.x += 10;
418 if (currently_focused->rect.y < 10)
420 currently_focused->rect.y -= 10;
423 currently_focused->rect.y += 10;
425 /* to make static analyzers happy */
430 reposition_client(conn, currently_focused);
432 /* Because reposition_client does not send a faked configure event (only resize does),
433 * we need to initiate that on our own */
434 fake_absolute_configure_notify(conn, currently_focused);
435 /* fake_absolute_configure_notify flushes */
439 * Hides all floating clients (or show them if they are currently hidden) on
440 * the specified workspace.
443 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
446 workspace->floating_hidden = !workspace->floating_hidden;
447 LOG("floating_hidden is now: %d\n", workspace->floating_hidden);
448 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
449 if (workspace->floating_hidden)
450 client_unmap(conn, client);
451 else client_map(conn, client);
454 /* If we just unmapped all floating windows we should ensure that the focus
455 * is set correctly, that ist, to the first non-floating client in stack */
456 if (workspace->floating_hidden)
457 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
458 if (client_is_floating(client))
460 set_focus(conn, client, true);