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
19 #include <xcb/xcb_icccm.h>
29 #include "workspace.h"
34 * Removes the given client from the container, either because it will be inserted into another
35 * one or because it was unmapped
38 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container, bool remove_from_focusstack) {
39 CIRCLEQ_REMOVE(&(container->clients), client, clients);
41 if (remove_from_focusstack)
42 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
44 /* If the container will be empty now and is in stacking mode, we need to
45 unmap the stack_win */
46 if (CIRCLEQ_EMPTY(&(container->clients)) &&
47 (container->mode == MODE_STACK ||
48 container->mode == MODE_TABBED)) {
49 DLOG("Unmapping stack window\n");
50 struct Stack_Window *stack_win = &(container->stack_win);
51 stack_win->rect.height = 0;
52 xcb_unmap_window(conn, stack_win->window);
58 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
62 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
63 int mid_x = client->rect.width / 2,
64 mid_y = client->rect.height / 2;
65 xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
69 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
72 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
73 xcb_get_property_cookie_t cookie;
74 xcb_get_wm_protocols_reply_t protocols;
77 cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
78 if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
81 /* Check if the client’s protocols have the requested atom set */
82 for (uint32_t i = 0; i < protocols.atoms_len; i++)
83 if (protocols.atoms[i] == atom)
86 xcb_get_wm_protocols_reply_wipe(&protocols);
92 * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
95 void client_kill(xcb_connection_t *conn, Client *window) {
96 /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
97 if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
98 LOG("Killing window the hard way\n");
99 xcb_kill_client(conn, window->child);
103 xcb_client_message_event_t ev;
105 memset(&ev, 0, sizeof(xcb_client_message_event_t));
107 ev.response_type = XCB_CLIENT_MESSAGE;
108 ev.window = window->child;
109 ev.type = atoms[WM_PROTOCOLS];
111 ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
112 ev.data.data32[1] = XCB_CURRENT_TIME;
114 LOG("Sending WM_DELETE to the client\n");
115 xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
120 * Checks if the given window class and title match the given client
121 * Window title is passed as "normal" string and as UCS-2 converted string for
122 * matching _NET_WM_NAME capable clients as well as those using legacy hints.
125 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
126 char *to_title_ucs, int to_title_ucs_len) {
127 /* Check if the given class is part of the window class */
128 if ((client->window_class_instance == NULL ||
129 strcasestr(client->window_class_instance, to_class) == NULL) &&
130 (client->window_class_class == NULL ||
131 strcasestr(client->window_class_class, to_class) == NULL))
134 /* If no title was given, we’re done */
135 if (to_title == NULL)
138 if (client->name_len > -1) {
139 /* UCS-2 converted window titles */
140 if (client->name == NULL || memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
144 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
152 * Sets the position of the given client in the X stack to the highest (tiling layer is always
153 * on the same position, so this doesn’t matter) below the first floating client, so that
154 * floating windows are always on top.
157 void client_set_below_floating(xcb_connection_t *conn, Client *client) {
158 /* Ensure that it is below all floating clients */
159 Workspace *ws = client->workspace;
160 Client *first_floating = TAILQ_FIRST(&(ws->floating_clients));
161 if (first_floating == TAILQ_END(&(ws->floating_clients)))
164 DLOG("Setting below floating\n");
165 uint32_t values[] = { first_floating->frame, XCB_STACK_MODE_BELOW };
166 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
168 if (client->workspace->fullscreen_client == NULL)
171 DLOG("(and below fullscreen)\n");
172 /* Ensure that the window is still below the fullscreen window */
173 values[0] = client->workspace->fullscreen_client->frame;
174 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
178 * Returns true if the client is floating. Makes the code more beatiful, as floating
179 * is not simply a boolean, but also saves whether the user selected the current state
180 * or whether it was automatically set.
183 bool client_is_floating(Client *client) {
184 return (client->floating >= FLOATING_AUTO_ON);
188 * Change the border type for the given client to normal (n), 1px border (p) or
189 * completely borderless (b) without actually re-rendering the layout. Useful
190 * for calling it when initializing a new client.
193 bool client_init_border(xcb_connection_t *conn, Client *client, char border_type) {
194 switch (border_type) {
196 LOG("Changing to normal border\n");
197 client->titlebar_position = TITLEBAR_TOP;
198 client->borderless = false;
201 LOG("Changing to 1px border\n");
202 client->titlebar_position = TITLEBAR_OFF;
203 client->borderless = false;
206 LOG("Changing to borderless\n");
207 client->titlebar_position = TITLEBAR_OFF;
208 client->borderless = true;
211 LOG("Unknown border mode\n");
217 * Change the border type for the given client to normal (n), 1px border (p) or
218 * completely borderless (b).
221 void client_change_border(xcb_connection_t *conn, Client *client, char border_type) {
222 if (!client_init_border(conn, client, border_type))
225 /* Ensure that the child’s position inside our window gets updated */
226 client->force_reconfigure = true;
228 /* For clients inside a container, we can simply render the container */
229 if (client->container != NULL)
230 render_container(conn, client->container);
232 /* If the client is floating, directly push its size */
233 if (client_is_floating(client))
234 resize_client(conn, client);
235 /* Otherwise, it may be a dock client, thus render the whole layout */
236 else render_layout(conn);
239 redecorate_window(conn, client);
243 * Unmap the client, correctly setting any state which is needed.
246 void client_unmap(xcb_connection_t *conn, Client *client) {
247 /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
248 long data[] = { XCB_WM_STATE_WITHDRAWN, XCB_NONE };
249 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, atoms[WM_STATE], atoms[WM_STATE], 32, 2, data);
251 xcb_unmap_window(conn, client->frame);
255 * Map the client, correctly restoring any state needed.
258 void client_map(xcb_connection_t *conn, Client *client) {
259 /* Set WM_STATE_NORMAL because GTK applications don’t want to drag & drop if we don’t.
260 * Also, xprop(1) needs that to work. */
261 long data[] = { XCB_WM_STATE_NORMAL, XCB_NONE };
262 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, atoms[WM_STATE], atoms[WM_STATE], 32, 2, data);
264 xcb_map_window(conn, client->frame);
268 * Set the given mark for this client. Used for jumping to the client
269 * afterwards (like m<mark> and '<mark> in vim).
272 void client_mark(xcb_connection_t *conn, Client *client, const char *mark) {
273 if (client->mark != NULL)
275 client->mark = sstrdup(mark);
277 /* Make sure no other client has this mark set */
280 TAILQ_FOREACH(ws, workspaces, workspaces)
281 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
282 if (current == client ||
283 current->mark == NULL ||
284 strcmp(current->mark, mark) != 0)
288 current->mark = NULL;
289 /* We can break here since there can only be one other
290 * client with this mark. */
296 * Returns the minimum height of a specific window. The height is calculated
297 * by using 2 pixels (for the client window itself), possibly padding this to
298 * comply with the client’s base_height and then adding the decoration height.
301 uint32_t client_min_height(Client *client) {
302 uint32_t height = max(2, client->base_height);
303 i3Font *font = load_font(global_conn, config.font);
305 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
308 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
311 return height + font->height + 2 + 2;
315 * See client_min_height.
318 uint32_t client_min_width(Client *client) {
319 uint32_t width = max(2, client->base_width);
321 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
324 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
327 return width + 2 + 2;