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 /* On which border was the dragging initiated? */
30 typedef enum { BORDER_LEFT, BORDER_RIGHT, BORDER_TOP, BORDER_BOTTOM} border_t;
31 /* Callback for dragging */
32 typedef void(*callback_t)(Rect*, uint32_t, uint32_t);
34 /* Forward definitions */
35 static void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
36 border_t border, callback_t callback);
39 * Toggles floating mode for the given client.
40 * Correctly takes care of the position/size (separately stored for tiling/floating mode)
41 * and repositions/resizes/redecorates the client.
43 * If the automatic flag is set to true, this was an automatic update by a change of the
44 * window class from the application which can be overwritten by the user.
47 void toggle_floating_mode(xcb_connection_t *conn, Client *client, bool automatic) {
48 Container *con = client->container;
49 i3Font *font = load_font(conn, config.font);
52 LOG("This client is already in floating (container == NULL), re-inserting\n");
54 SLIST_FOREACH(next_tiling, &(client->workspace->focus_stack), focus_clients)
55 if (!client_is_floating(next_tiling))
57 /* If there are no tiling clients on this workspace, there can only be one
58 * container: the first one */
59 if (next_tiling == TAILQ_END(&(client->workspace->focus_stack)))
60 con = client->workspace->table[0][0];
61 else con = next_tiling->container;
63 /* Remove the client from the list of floating clients */
64 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
66 LOG("destination container = %p\n", con);
67 Client *old_focused = con->currently_focused;
68 /* Preserve position/size */
69 memcpy(&(client->floating_rect), &(client->rect), sizeof(Rect));
71 client->floating = FLOATING_USER_OFF;
72 client->container = con;
74 if (old_focused != NULL && !old_focused->dock)
75 CIRCLEQ_INSERT_AFTER(&(con->clients), old_focused, client, clients);
76 else CIRCLEQ_INSERT_TAIL(&(con->clients), client, clients);
78 LOG("Re-inserted the client into the matrix.\n");
79 con->currently_focused = client;
81 client_set_below_floating(conn, client);
83 render_container(conn, con);
89 LOG("Entering floating for client %08x\n", client->child);
91 /* Remove the client of its container */
92 client_remove_from_container(conn, client, con, false);
93 client->container = NULL;
95 /* Add the client to the list of floating clients for its workspace */
96 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
98 if (con->currently_focused == client) {
99 LOG("Need to re-adjust currently_focused\n");
100 /* Get the next client in the focus stack for this particular container */
101 con->currently_focused = get_last_focused_client(conn, con, NULL);
105 client->floating = FLOATING_AUTO_ON;
106 else client->floating = FLOATING_USER_ON;
108 /* Initialize the floating position from the position in tiling mode, if this
109 * client never was floating (x == -1) */
110 if (client->floating_rect.x == -1) {
111 /* Copy over the position */
112 client->floating_rect.x = client->rect.x;
113 client->floating_rect.y = client->rect.y;
115 /* Copy size the other direction */
116 client->child_rect.width = client->floating_rect.width;
117 client->child_rect.height = client->floating_rect.height;
119 client->rect.width = client->child_rect.width + 2 + 2;
120 client->rect.height = client->child_rect.height + (font->height + 2 + 2) + 2;
122 LOG("copying size from tiling (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
123 client->floating_rect.width, client->floating_rect.height);
125 /* If the client was already in floating before we restore the old position / size */
126 LOG("using: (%d, %d) size (%d, %d)\n", client->floating_rect.x, client->floating_rect.y,
127 client->floating_rect.width, client->floating_rect.height);
128 memcpy(&(client->rect), &(client->floating_rect), sizeof(Rect));
131 /* Raise the client */
132 xcb_raise_window(conn, client->frame);
133 reposition_client(conn, client);
134 resize_client(conn, client);
135 /* redecorate_window flushes */
136 redecorate_window(conn, client);
138 /* Re-render the tiling layout of this container */
139 render_container(conn, con);
144 * Removes the floating client from its workspace and attaches it to the new workspace.
145 * This is centralized here because it may happen if you move it via keyboard and
146 * if you move it using your mouse.
149 void floating_assign_to_workspace(Client *client, Workspace *new_workspace) {
150 /* Remove from focus stack and list of floating clients */
151 SLIST_REMOVE(&(client->workspace->focus_stack), client, Client, focus_clients);
152 TAILQ_REMOVE(&(client->workspace->floating_clients), client, floating_clients);
154 if (client->workspace->fullscreen_client == client)
155 client->workspace->fullscreen_client = NULL;
157 /* Insert into destination focus stack and list of floating clients */
158 client->workspace = new_workspace;
159 SLIST_INSERT_HEAD(&(client->workspace->focus_stack), client, focus_clients);
160 TAILQ_INSERT_TAIL(&(client->workspace->floating_clients), client, floating_clients);
161 if (client->fullscreen)
162 client->workspace->fullscreen_client = client;
167 * Called whenever the user clicks on a border (not the titlebar!) of a floating window.
168 * Determines on which border the user clicked and launches the drag_pointer function
169 * with the resize_callback.
172 int floating_border_click(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
174 LOG("floating border click\n");
178 void resize_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
181 int new_width = old_rect->width + (new_x - event->root_x);
182 if ((new_width < 0) ||
183 (new_width < 50 && client->rect.width >= new_width))
185 client->rect.width = new_width;
189 case BORDER_BOTTOM: {
190 int new_height = old_rect->height + (new_y - event->root_y);
191 if ((new_height < 0) ||
192 (new_height < 20 && client->rect.height >= new_height))
194 client->rect.height = old_rect->height + (new_y - event->root_y);
199 int new_height = old_rect->height + (event->root_y - new_y);
200 if ((new_height < 0) ||
201 (new_height < 20 && client->rect.height >= new_height))
204 client->rect.y = old_rect->y + (new_y - event->root_y);
205 client->rect.height = new_height;
210 int new_width = old_rect->width + (event->root_x - new_x);
211 if ((new_width < 0) ||
212 (new_width < 50 && client->rect.width >= new_width))
214 client->rect.x = old_rect->x + (new_x - event->root_x);
215 client->rect.width = new_width;
220 /* Push the new position/size to X11 */
221 reposition_client(conn, client);
222 resize_client(conn, client);
226 if (event->event_y < 2)
228 else if (event->event_y >= (client->rect.height - 2))
229 border = BORDER_BOTTOM;
230 else if (event->event_x <= 2)
231 border = BORDER_LEFT;
232 else if (event->event_x > 2)
233 border = BORDER_RIGHT;
235 LOG("Not on any border, not doing anything.\n");
239 LOG("border = %d\n", border);
241 drag_pointer(conn, client, event, border, resize_callback);
248 * Called when the user clicked on the titlebar of a floating window.
249 * Calls the drag_pointer function with the drag_window callback
252 void floating_drag_window(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event) {
253 LOG("floating_drag_window\n");
255 void drag_window_callback(Rect *old_rect, uint32_t new_x, uint32_t new_y) {
256 /* Reposition the client correctly while moving */
257 client->rect.x = old_rect->x + (new_x - event->root_x);
258 client->rect.y = old_rect->y + (new_y - event->root_y);
259 reposition_client(conn, client);
260 /* Because reposition_client does not send a faked configure event (only resize does),
261 * we need to initiate that on our own */
262 fake_absolute_configure_notify(conn, client);
263 /* fake_absolute_configure_notify flushes */
267 drag_pointer(conn, client, event, BORDER_TOP /* irrelevant */, drag_window_callback);
271 * This function grabs your pointer and lets you drag stuff around (borders).
272 * Every time you move your mouse, an XCB_MOTION_NOTIFY event will be received
273 * and the given callback will be called with the parameters specified (client,
274 * border on which the click originally was), the original rect of the client,
275 * the event and the new coordinates (x, y).
278 static void drag_pointer(xcb_connection_t *conn, Client *client, xcb_button_press_event_t *event,
279 border_t border, callback_t callback) {
280 xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
281 uint32_t new_x, new_y;
283 memcpy(&old_rect, &(client->rect), sizeof(Rect));
285 /* Grab the pointer */
286 /* TODO: returncode */
287 xcb_grab_pointer(conn,
288 false, /* get all pointer events specified by the following mask */
289 root, /* grab the root window */
290 XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_POINTER_MOTION, /* which events to let through */
291 XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
292 XCB_GRAB_MODE_ASYNC, /* keyboard mode */
293 XCB_NONE, /* confine_to = in which window should the cursor stay */
294 XCB_NONE, /* don’t display a special cursor */
297 /* Go into our own event loop */
300 xcb_generic_event_t *inside_event, *last_motion_notify = NULL;
301 /* I’ve always wanted to have my own eventhandler… */
302 while ((inside_event = xcb_wait_for_event(conn))) {
303 /* We now handle all events we can get using xcb_poll_for_event */
305 /* Same as get_event_handler in xcb */
306 int nr = inside_event->response_type;
308 /* An error occured */
309 handle_event(NULL, conn, inside_event);
314 nr &= XCB_EVENT_RESPONSE_TYPE_MASK;
318 case XCB_BUTTON_RELEASE:
321 case XCB_MOTION_NOTIFY:
322 /* motion_notify events are saved for later */
323 FREE(last_motion_notify);
324 last_motion_notify = inside_event;
328 LOG("Passing to original handler\n");
329 /* Use original handler */
330 xcb_event_handle(&evenths, inside_event);
333 if (last_motion_notify != inside_event)
335 } while ((inside_event = xcb_poll_for_event(conn)) != NULL);
337 if (last_motion_notify == NULL)
340 new_x = ((xcb_motion_notify_event_t*)last_motion_notify)->root_x;
341 new_y = ((xcb_motion_notify_event_t*)last_motion_notify)->root_y;
343 callback(&old_rect, new_x, new_y);
344 FREE(last_motion_notify);
347 xcb_ungrab_pointer(conn, XCB_CURRENT_TIME);
352 * Changes focus in the given direction for floating clients.
354 * Changing to the left/right means going to the previous/next floating client,
355 * changing to top/bottom means cycling through the Z-index.
358 void floating_focus_direction(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
359 LOG("floating focus\n");
361 if (direction == D_LEFT || direction == D_RIGHT) {
362 /* Go to the next/previous floating client */
365 while ((client = (direction == D_LEFT ? TAILQ_PREV(currently_focused, floating_clients_head, floating_clients) :
366 TAILQ_NEXT(currently_focused, floating_clients))) !=
367 TAILQ_END(&(currently_focused->workspace->floating_clients))) {
368 if (!client->floating)
370 set_focus(conn, client, true);
377 * Moves the client 10px to the specified direction.
380 void floating_move(xcb_connection_t *conn, Client *currently_focused, direction_t direction) {
381 LOG("floating move\n");
385 if (currently_focused->rect.x < 10)
387 currently_focused->rect.x -= 10;
390 currently_focused->rect.x += 10;
393 if (currently_focused->rect.y < 10)
395 currently_focused->rect.y -= 10;
398 currently_focused->rect.y += 10;
400 /* to make static analyzers happy */
405 reposition_client(conn, currently_focused);
407 /* Because reposition_client does not send a faked configure event (only resize does),
408 * we need to initiate that on our own */
409 fake_absolute_configure_notify(conn, currently_focused);
410 /* fake_absolute_configure_notify flushes */
414 * Hides all floating clients (or show them if they are currently hidden) on
415 * the specified workspace.
418 void floating_toggle_hide(xcb_connection_t *conn, Workspace *workspace) {
421 workspace->floating_hidden = !workspace->floating_hidden;
422 LOG("floating_hidden is now: %d\n", workspace->floating_hidden);
423 TAILQ_FOREACH(client, &(workspace->floating_clients), floating_clients) {
424 if (workspace->floating_hidden)
425 xcb_unmap_window(conn, client->frame);
426 else xcb_map_window(conn, client->frame);
429 /* If we just unmapped all floating windows we should ensure that the focus
430 * is set correctly, that ist, to the first non-floating client in stack */
431 if (workspace->floating_hidden)
432 SLIST_FOREACH(client, &(workspace->focus_stack), focus_clients) {
433 if (client_is_floating(client))
435 set_focus(conn, client, true);