4 * i3 - an improved dynamic tiling window manager
6 * © 2009 Michael Stapelberg and contributors
8 * See file LICENSE for license information.
10 * client.c: holds all client-specific functions
18 #include <xcb/xcb_icccm.h>
28 #include "workspace.h"
33 * Removes the given client from the container, either because it will be inserted into another
34 * one or because it was unmapped
37 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container, bool remove_from_focusstack) {
38 CIRCLEQ_REMOVE(&(container->clients), client, clients);
40 if (remove_from_focusstack)
41 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
43 /* If the container will be empty now and is in stacking mode, we need to
44 unmap the stack_win */
45 if (CIRCLEQ_EMPTY(&(container->clients)) &&
46 (container->mode == MODE_STACK ||
47 container->mode == MODE_TABBED)) {
48 DLOG("Unmapping stack window\n");
49 struct Stack_Window *stack_win = &(container->stack_win);
50 stack_win->rect.height = 0;
51 xcb_unmap_window(conn, stack_win->window);
57 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
61 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
62 int mid_x = client->rect.width / 2,
63 mid_y = client->rect.height / 2;
64 xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
68 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
71 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
72 xcb_get_property_cookie_t cookie;
73 xcb_get_wm_protocols_reply_t protocols;
76 cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
77 if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
80 /* Check if the client’s protocols have the requested atom set */
81 for (uint32_t i = 0; i < protocols.atoms_len; i++)
82 if (protocols.atoms[i] == atom)
85 xcb_get_wm_protocols_reply_wipe(&protocols);
91 * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
94 void client_kill(xcb_connection_t *conn, Client *window) {
95 /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
96 if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
97 LOG("Killing window the hard way\n");
98 xcb_kill_client(conn, window->child);
102 xcb_client_message_event_t ev;
104 memset(&ev, 0, sizeof(xcb_client_message_event_t));
106 ev.response_type = XCB_CLIENT_MESSAGE;
107 ev.window = window->child;
108 ev.type = atoms[WM_PROTOCOLS];
110 ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
111 ev.data.data32[1] = XCB_CURRENT_TIME;
113 LOG("Sending WM_DELETE to the client\n");
114 xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
119 * Checks if the given window class and title match the given client
120 * Window title is passed as "normal" string and as UCS-2 converted string for
121 * matching _NET_WM_NAME capable clients as well as those using legacy hints.
124 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
125 char *to_title_ucs, int to_title_ucs_len) {
126 /* Check if the given class is part of the window class */
127 if ((client->window_class_instance == NULL ||
128 strcasestr(client->window_class_instance, to_class) == NULL) &&
129 (client->window_class_class == NULL ||
130 strcasestr(client->window_class_class, to_class) == NULL))
133 /* If no title was given, we’re done */
134 if (to_title == NULL)
137 if (client->name_len > -1) {
138 /* UCS-2 converted window titles */
139 if (client->name == NULL || memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
143 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
151 * Enters fullscreen mode for the given client. This is called by toggle_fullscreen
152 * and when moving a fullscreen client to another screen.
155 void client_enter_fullscreen(xcb_connection_t *conn, Client *client) {
156 Workspace *workspace = client->workspace;
158 if (workspace->fullscreen_client != NULL) {
159 LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
163 client->fullscreen = true;
164 workspace->fullscreen_client = client;
165 LOG("Entering fullscreen mode...\n");
166 /* We just entered fullscreen mode, let’s configure the window */
167 Rect r = workspace->rect;
169 DLOG("child itself will be at %dx%d with size %dx%d\n",
170 r.x, r.y, r.width, r.height);
172 xcb_set_window_rect(conn, client->frame, r);
174 /* Child’s coordinates are relative to the parent (=frame) */
177 xcb_set_window_rect(conn, client->child, r);
179 /* Raise the window */
180 uint32_t values[] = { XCB_STACK_MODE_ABOVE };
181 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
183 fake_configure_notify(conn, r, client->child);
189 * Toggles fullscreen mode for the given client. It updates the data structures and
190 * reconfigures (= resizes/moves) the client and its frame to the full size of the
191 * screen. When leaving fullscreen, re-rendering the layout is forced.
194 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
195 /* dock clients cannot enter fullscreen mode */
196 assert(!client->dock);
198 Workspace *workspace = client->workspace;
200 if (!client->fullscreen) {
201 client_enter_fullscreen(conn, client);
205 LOG("leaving fullscreen mode\n");
206 client->fullscreen = false;
207 workspace->fullscreen_client = NULL;
208 if (client_is_floating(client)) {
209 /* For floating clients it’s enough if we just reconfigure that window (in fact,
210 * re-rendering the layout will not update the client.) */
211 reposition_client(conn, client);
212 resize_client(conn, client);
213 /* redecorate_window flushes */
214 redecorate_window(conn, client);
216 client_set_below_floating(conn, client);
218 /* Because the coordinates of the window haven’t changed, it would not be
219 re-configured if we don’t set the following flag */
220 client->force_reconfigure = true;
221 /* We left fullscreen mode, redraw the whole layout to ensure enternotify events are disabled */
229 * Sets the position of the given client in the X stack to the highest (tiling layer is always
230 * on the same position, so this doesn’t matter) below the first floating client, so that
231 * floating windows are always on top.
234 void client_set_below_floating(xcb_connection_t *conn, Client *client) {
235 /* Ensure that it is below all floating clients */
236 Workspace *ws = client->workspace;
237 Client *first_floating = TAILQ_FIRST(&(ws->floating_clients));
238 if (first_floating == TAILQ_END(&(ws->floating_clients)))
241 DLOG("Setting below floating\n");
242 uint32_t values[] = { first_floating->frame, XCB_STACK_MODE_BELOW };
243 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
245 if (client->workspace->fullscreen_client == NULL)
248 DLOG("(and below fullscreen)\n");
249 /* Ensure that the window is still below the fullscreen window */
250 values[0] = client->workspace->fullscreen_client->frame;
251 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
255 * Returns true if the client is floating. Makes the code more beatiful, as floating
256 * is not simply a boolean, but also saves whether the user selected the current state
257 * or whether it was automatically set.
260 bool client_is_floating(Client *client) {
261 return (client->floating >= FLOATING_AUTO_ON);
265 * Change the border type for the given client to normal (n), 1px border (p) or
266 * completely borderless (b) without actually re-rendering the layout. Useful
267 * for calling it when initializing a new client.
270 bool client_init_border(xcb_connection_t *conn, Client *client, char border_type) {
271 switch (border_type) {
273 LOG("Changing to normal border\n");
274 client->titlebar_position = TITLEBAR_TOP;
275 client->borderless = false;
278 LOG("Changing to 1px border\n");
279 client->titlebar_position = TITLEBAR_OFF;
280 client->borderless = false;
283 LOG("Changing to borderless\n");
284 client->titlebar_position = TITLEBAR_OFF;
285 client->borderless = true;
288 LOG("Unknown border mode\n");
294 * Change the border type for the given client to normal (n), 1px border (p) or
295 * completely borderless (b).
298 void client_change_border(xcb_connection_t *conn, Client *client, char border_type) {
299 if (!client_init_border(conn, client, border_type))
302 /* Ensure that the child’s position inside our window gets updated */
303 client->force_reconfigure = true;
305 /* For clients inside a container, we can simply render the container */
306 if (client->container != NULL)
307 render_container(conn, client->container);
309 /* If the client is floating, directly push its size */
310 if (client_is_floating(client))
311 resize_client(conn, client);
312 /* Otherwise, it may be a dock client, thus render the whole layout */
313 else render_layout(conn);
316 redecorate_window(conn, client);
320 * Unmap the client, correctly setting any state which is needed.
323 void client_unmap(xcb_connection_t *conn, Client *client) {
324 /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
325 long data[] = { XCB_WM_STATE_WITHDRAWN, XCB_NONE };
326 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, atoms[WM_STATE], atoms[WM_STATE], 32, 2, data);
328 xcb_unmap_window(conn, client->frame);
332 * Map the client, correctly restoring any state needed.
335 void client_map(xcb_connection_t *conn, Client *client) {
336 /* Set WM_STATE_NORMAL because GTK applications don’t want to drag & drop if we don’t.
337 * Also, xprop(1) needs that to work. */
338 long data[] = { XCB_WM_STATE_NORMAL, XCB_NONE };
339 xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, atoms[WM_STATE], atoms[WM_STATE], 32, 2, data);
341 xcb_map_window(conn, client->frame);
345 * Set the given mark for this client. Used for jumping to the client
346 * afterwards (like m<mark> and '<mark> in vim).
349 void client_mark(xcb_connection_t *conn, Client *client, const char *mark) {
350 if (client->mark != NULL)
352 client->mark = sstrdup(mark);
354 /* Make sure no other client has this mark set */
357 TAILQ_FOREACH(ws, workspaces, workspaces)
358 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
359 if (current == client ||
360 current->mark == NULL ||
361 strcmp(current->mark, mark) != 0)
365 current->mark = NULL;
366 /* We can break here since there can only be one other
367 * client with this mark. */
373 * Returns the minimum height of a specific window. The height is calculated
374 * by using 2 pixels (for the client window itself), possibly padding this to
375 * comply with the client’s base_height and then adding the decoration height.
378 uint32_t client_min_height(Client *client) {
379 uint32_t height = max(2, client->base_height);
380 i3Font *font = load_font(global_conn, config.font);
382 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
385 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
388 return height + font->height + 2 + 2;
392 * See client_min_height.
395 uint32_t client_min_width(Client *client) {
396 uint32_t width = max(2, client->base_width);
398 if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
401 if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
404 return width + 2 + 2;