4 * i3 - an improved dynamic tiling window manager
6 * © 2009-2010 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * client.c: holds all client-specific functions
18 /* Contains compatibility definitions for old libxcb versions */
20 #include "xcb_compat.h"
24 #include <xcb/xcb_icccm.h>
34 #include "workspace.h"
39 * Removes the given client from the container, either because it will be inserted into another
40 * one or because it was unmapped
43 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container, bool remove_from_focusstack) {
44 CIRCLEQ_REMOVE(&(container->clients), client, clients);
46 if (remove_from_focusstack)
47 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
49 /* If the container will be empty now and is in stacking mode, we need to
50 unmap the stack_win */
51 if (CIRCLEQ_EMPTY(&(container->clients)) &&
52 (container->mode == MODE_STACK ||
53 container->mode == MODE_TABBED)) {
54 DLOG("Unmapping stack window\n");
55 struct Stack_Window *stack_win = &(container->stack_win);
56 stack_win->rect.height = 0;
57 xcb_unmap_window(conn, stack_win->window);
63 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
67 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
68 int mid_x = client->rect.width / 2,
69 mid_y = client->rect.height / 2;
70 xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
74 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
77 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
78 xcb_get_property_cookie_t cookie;
79 xcb_icccm_get_wm_protocols_reply_t protocols;
82 cookie = xcb_icccm_get_wm_protocols_unchecked(conn, client->child, A_WM_PROTOCOLS);
83 if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
86 /* Check if the client’s protocols have the requested atom set */
87 for (uint32_t i = 0; i < protocols.atoms_len; i++)
88 if (protocols.atoms[i] == atom)
91 xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
97 * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
100 void client_kill(xcb_connection_t *conn, Client *window) {
101 /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
102 if (!client_supports_protocol(conn, window, A_WM_DELETE_WINDOW)) {
103 LOG("Killing window the hard way\n");
104 xcb_kill_client(conn, window->child);
108 xcb_client_message_event_t ev;
110 memset(&ev, 0, sizeof(xcb_client_message_event_t));
112 ev.response_type = XCB_CLIENT_MESSAGE;
113 ev.window = window->child;
114 ev.type = A_WM_PROTOCOLS;
116 ev.data.data32[0] = A_WM_DELETE_WINDOW;
117 ev.data.data32[1] = XCB_CURRENT_TIME;
119 LOG("Sending WM_DELETE to the client\n");
120 xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
125 * Checks if the given window class and title match the given client
126 * Window title is passed as "normal" string and as UCS-2 converted string for
127 * matching _NET_WM_NAME capable clients as well as those using legacy hints.
130 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
131 char *to_title_ucs, int to_title_ucs_len) {
132 /* Check if the given class is part of the window class */
133 if ((client->window_class_instance == NULL ||
134 strcasestr(client->window_class_instance, to_class) == NULL) &&
135 (client->window_class_class == NULL ||
136 strcasestr(client->window_class_class, to_class) == NULL))
139 /* If no title was given, we’re done */
140 if (to_title == NULL)
143 if (client->name_len > -1) {
144 /* UCS-2 converted window titles */
145 if (client->name == NULL || memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
149 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
157 * Enters fullscreen mode for the given client. This is called by toggle_fullscreen
158 * and when moving a fullscreen client to another screen.
161 void client_enter_fullscreen(xcb_connection_t *conn, Client *client, bool global) {
162 Workspace *workspace;
167 TAILQ_FOREACH(output, &outputs, outputs) {
171 if (output->current_workspace->fullscreen_client == NULL)
174 LOG("Not entering global fullscreen mode, there already "
175 "is a fullscreen client on output %s.\n", output->name);
179 r = (Rect) { UINT_MAX, UINT_MAX, 0,0 };
182 /* Set fullscreen_client for each active workspace.
183 * Expand the rectangle to contain all outputs. */
184 TAILQ_FOREACH(output, &outputs, outputs) {
188 output->current_workspace->fullscreen_client = client;
190 /* Temporarily abuse width/heigth as coordinates of the lower right corner */
191 if (r.x > output->rect.x)
192 r.x = output->rect.x;
193 if (r.y > output->rect.y)
194 r.y = output->rect.y;
195 if (r.x + r.width < output->rect.x + output->rect.width)
196 r.width = output->rect.x + output->rect.width;
197 if (r.y + r.height < output->rect.y + output->rect.height)
198 r.height = output->rect.y + output->rect.height;
201 /* Putting them back to their original meaning */
205 LOG("Entering global fullscreen mode...\n");
207 workspace = client->workspace;
208 if (workspace->fullscreen_client != NULL && workspace->fullscreen_client != client) {
209 LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
213 workspace->fullscreen_client = client;
216 LOG("Entering fullscreen mode...\n");
219 client->fullscreen = true;
221 /* We just entered fullscreen mode, let’s configure the window */
222 DLOG("child itself will be at %dx%d with size %dx%d\n",
223 r.x, r.y, r.width, r.height);
225 xcb_set_window_rect(conn, client->frame, r);
227 /* Child’s coordinates are relative to the parent (=frame) */
230 xcb_set_window_rect(conn, client->child, r);
232 /* Raise the window */
233 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
234 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
236 /* Update _NET_WM_STATE */
237 values[0] = A__NET_WM_STATE_FULLSCREEN;
238 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, A__NET_WM_STATE, A_ATOM, 32, 1, values);
240 fake_configure_notify(conn, r, client->child);
246 * Leaves fullscreen mode for the current client. This is called by toggle_fullscreen.
249 void client_leave_fullscreen(xcb_connection_t *conn, Client *client) {
250 LOG("leaving fullscreen mode\n");
251 client->fullscreen = false;
253 TAILQ_FOREACH(ws, workspaces, workspaces)
254 if (ws->fullscreen_client == client)
255 ws->fullscreen_client = NULL;
257 if (client_is_floating(client)) {
258 /* For floating clients it’s enough if we just reconfigure that window (in fact,
259 * re-rendering the layout will not update the client.) */
260 reposition_client(conn, client);
261 resize_client(conn, client);
262 /* redecorate_window flushes */
263 redecorate_window(conn, client);
265 client_set_below_floating(conn, client);
267 /* Because the coordinates of the window haven’t changed, it would not be
268 re-configured if we don’t set the following flag */
269 client->force_reconfigure = true;
270 /* We left fullscreen mode, redraw the whole layout to ensure enternotify events are disabled */
274 /* Update _NET_WM_STATE */
275 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, A__NET_WM_STATE, A_ATOM, 32, 0, NULL);
281 * Toggles fullscreen mode for the given client. It updates the data structures and
282 * reconfigures (= resizes/moves) the client and its frame to the full size of the
283 * screen. When leaving fullscreen, re-rendering the layout is forced.
286 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
287 /* dock clients cannot enter fullscreen mode */
288 assert(!client->dock);
290 if (!client->fullscreen) {
291 client_enter_fullscreen(conn, client, false);
293 client_leave_fullscreen(conn, client);
298 * Like client_toggle_fullscreen(), but putting it in global fullscreen-mode.
301 void client_toggle_fullscreen_global(xcb_connection_t *conn, Client *client) {
302 /* dock clients cannot enter fullscreen mode */
303 assert(!client->dock);
305 if (!client->fullscreen) {
306 client_enter_fullscreen(conn, client, true);
308 client_leave_fullscreen(conn, client);
313 * Sets the position of the given client in the X stack to the highest (tiling layer is always
314 * on the same position, so this doesn’t matter) below the first floating client, so that
315 * floating windows are always on top.
318 void client_set_below_floating(xcb_connection_t *conn, Client *client) {
319 /* Ensure that it is below all floating clients */
320 Workspace *ws = client->workspace;
321 Client *first_floating = TAILQ_FIRST(&(ws->floating_clients));
322 if (first_floating == TAILQ_END(&(ws->floating_clients)))
325 DLOG("Setting below floating\n");
326 uint32_t values[] = { first_floating->frame, XCB_STACK_MODE_BELOW };
327 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
329 if (client->workspace->fullscreen_client == NULL)
332 DLOG("(and below fullscreen)\n");
333 /* Ensure that the window is still below the fullscreen window */
334 values[0] = client->workspace->fullscreen_client->frame;
335 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
339 * Returns true if the client is floating. Makes the code more beatiful, as floating
340 * is not simply a boolean, but also saves whether the user selected the current state
341 * or whether it was automatically set.
344 bool client_is_floating(Client *client) {
345 return (client->floating >= FLOATING_AUTO_ON);
349 * Change the border type for the given client to normal (n), 1px border (p) or
350 * completely borderless (b) without actually re-rendering the layout. Useful
351 * for calling it when initializing a new client.
354 bool client_init_border(xcb_connection_t *conn, Client *client, char border_type) {
355 switch (border_type) {
357 LOG("Changing to normal border\n");
358 client->titlebar_position = TITLEBAR_TOP;
359 client->borderless = false;
362 LOG("Changing to 1px border\n");
363 client->titlebar_position = TITLEBAR_OFF;
364 client->borderless = false;
367 LOG("Changing to borderless\n");
368 client->titlebar_position = TITLEBAR_OFF;
369 client->borderless = true;
372 LOG("Unknown border mode\n");
378 * Change the border type for the given client to normal (n), 1px border (p) or
379 * completely borderless (b).
382 void client_change_border(xcb_connection_t *conn, Client *client, char border_type) {
383 if (!client_init_border(conn, client, border_type))
386 /* Ensure that the child’s position inside our window gets updated */
387 client->force_reconfigure = true;
389 /* For clients inside a container, we can simply render the container */
390 if (client->container != NULL)
391 render_container(conn, client->container);
393 /* If the client is floating, directly push its size */
394 if (client_is_floating(client))
395 resize_client(conn, client);
396 /* Otherwise, it may be a dock client, thus render the whole layout */
397 else render_layout(conn);
400 redecorate_window(conn, client);
404 * Unmap the client, correctly setting any state which is needed.
407 void client_unmap(xcb_connection_t *conn, Client *client) {
408 /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
409 long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
410 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, A_WM_STATE, A_WM_STATE, 32, 2, data);
412 xcb_unmap_window(conn, client->frame);
416 * Map the client, correctly restoring any state needed.
419 void client_map(xcb_connection_t *conn, Client *client) {
420 /* Set WM_STATE_NORMAL because GTK applications don’t want to drag & drop if we don’t.
421 * Also, xprop(1) needs that to work. */
422 long data[] = { XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE };
423 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, A_WM_STATE, A_WM_STATE, 32, 2, data);
425 xcb_map_window(conn, client->frame);
429 * Set the given mark for this client. Used for jumping to the client
430 * afterwards (like m<mark> and '<mark> in vim).
433 void client_mark(xcb_connection_t *conn, Client *client, const char *mark) {
434 if (client->mark != NULL)
436 client->mark = sstrdup(mark);
438 /* Make sure no other client has this mark set */
441 TAILQ_FOREACH(ws, workspaces, workspaces)
442 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
443 if (current == client ||
444 current->mark == NULL ||
445 strcmp(current->mark, mark) != 0)
449 current->mark = NULL;
450 /* We can break here since there can only be one other
451 * client with this mark. */
457 * Returns the minimum height of a specific window. The height is calculated
458 * by using 2 pixels (for the client window itself), possibly padding this to
459 * comply with the client’s base_height and then adding the decoration height.
462 uint32_t client_min_height(Client *client) {
463 uint32_t height = max(2, client->base_height);
464 i3Font *font = load_font(global_conn, config.font);
466 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
469 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
472 return height + font->height + 2 + 2;
476 * See client_min_height.
479 uint32_t client_min_width(Client *client) {
480 uint32_t width = max(2, client->base_width);
482 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
485 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
488 return width + 2 + 2;