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 */
259 drag_pointer(conn, client, event, XCB_NONE, BORDER_TOP /* irrelevant */, drag_window_callback);
263 * This function grabs your pointer and lets you drag stuff around (borders).
264 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
265 * and the given callback will be called with the parameters specified (client,
266 * border on which the click originally was), the original rect of the client,
267 * the event and the new coordinates (x, y).
270 void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
271 xcb_window_t confine_to, border_t border, callback_t callback) {
272 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
273 uint32_t new_x, new_y;
276 memcpy(&old_rect, &(client->rect), sizeof(Rect));
278 /* Grab the pointer */
279 /* TODO: returncode */
280 xcb_grab_pointer(conn,
281 false, /* get all pointer events specified by the following mask */
282 root, /* grab the root window */
283 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
284 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
285 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
286 confine_to, /* confine_to = in which window should the cursor stay */
287 XCB_NONE, /* don’t display a special cursor */
290 /* Go into our own event loop */
293 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
294 /* I’ve always wanted to have my own eventhandler… */
295 while ((inside_event = xcb_wait_for_event(conn))) {
296 /* We now handle all events we can get using xcb_poll_for_event */
298 /* Same as get_event_handler in xcb */
299 int nr = inside_event->response_type;
301 /* An error occured */
302 handle_event(NULL, conn, inside_event);
307 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
311 case XCB_BUTTON_RELEASE:
314 case XCB_MOTION_NOTIFY:
315 /* motion_notify events are saved for later */
316 FREE(last_motion_notify);
317 last_motion_notify = inside_event;
320 case XCB_UNMAP_NOTIFY:
321 LOG("Unmap-notify, aborting\n");
322 xcb_event_handle(&evenths, inside_event);
326 LOG("Passing to original handler\n");
327 /* Use original handler */
328 xcb_event_handle(&evenths, inside_event);
331 if (last_motion_notify != inside_event)
333 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
335 if (last_motion_notify == NULL)
338 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
339 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
341 callback(&old_rect, new_x, new_y);
342 FREE(last_motion_notify);
345 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
350 * Changes focus in the given direction for floating clients.
352 * Changing to the left/right means going to the previous/next floating client,
353 * changing to top/bottom means cycling through the Z-index.
356 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
357 LOG("floating focus\n");
359 if (direction == D_LEFT || direction == D_RIGHT) {
360 /* Go to the next/previous floating client */
363 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
364 TAILQ_NEXT(currently_focused, floating_clients))) !=
365 TAILQ_END(&(currently_focused->workspace->floating_clients))) {
366 if (!client->floating)
368 set_focus(conn, client, true);
375 * Moves the client 10px to the specified direction.
378 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
379 LOG("floating move\n");
383 if (currently_focused->rect.x < 10)
385 currently_focused->rect.x -= 10;
388 currently_focused->rect.x += 10;
391 if (currently_focused->rect.y < 10)
393 currently_focused->rect.y -= 10;
396 currently_focused->rect.y += 10;
398 /* to make static analyzers happy */
403 reposition_client(conn, currently_focused);
405 /* Because reposition_client does not send a faked configure event (only resize does),
406 * we need to initiate that on our own */
407 fake_absolute_configure_notify(conn, currently_focused);
408 /* fake_absolute_configure_notify flushes */
412 * Hides all floating clients (or show them if they are currently hidden) on
413 * the specified workspace.
416 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
419 workspace->floating_hidden = !workspace->floating_hidden;
420 LOG("floating_hidden is now: %d\n", workspace->floating_hidden);
421 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
422 if (workspace->floating_hidden)
423 client_unmap(conn, client);
424 else client_map(conn, client);
427 /* If we just unmapped all floating windows we should ensure that the focus
428 * is set correctly, that ist, to the first non-floating client in stack */
429 if (workspace->floating_hidden)
430 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
431 if (client_is_floating(client))
433 set_focus(conn, client, true);