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>
29 * Removes the given client from the container, either because it will be inserted into another
30 * one or because it was unmapped
33 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container, bool remove_from_focusstack) {
34 CIRCLEQ_REMOVE(&(container->clients), client, clients);
36 if (remove_from_focusstack)
37 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
39 /* If the container will be empty now and is in stacking mode, we need to
40 unmap the stack_win */
41 if (CIRCLEQ_EMPTY(&(container->clients)) && container->mode == MODE_STACK) {
42 LOG("Unmapping stack window\n");
43 struct Stack_Window *stack_win = &(container->stack_win);
44 stack_win->rect.height = 0;
45 xcb_unmap_window(conn, stack_win->window);
51 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
55 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
56 int mid_x = client->rect.width / 2,
57 mid_y = client->rect.height / 2;
58 xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
62 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
65 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
66 xcb_get_property_cookie_t cookie;
67 xcb_get_wm_protocols_reply_t protocols;
70 cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
71 if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
74 /* Check if the client’s protocols have the requested atom set */
75 for (uint32_t i = 0; i < protocols.atoms_len; i++)
76 if (protocols.atoms[i] == atom)
79 xcb_get_wm_protocols_reply_wipe(&protocols);
85 * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
88 void client_kill(xcb_connection_t *conn, Client *window) {
89 /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
90 if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
91 LOG("Killing window the hard way\n");
92 xcb_kill_client(conn, window->child);
96 xcb_client_message_event_t ev;
98 memset(&ev, 0, sizeof(xcb_client_message_event_t));
100 ev.response_type = XCB_CLIENT_MESSAGE;
101 ev.window = window->child;
102 ev.type = atoms[WM_PROTOCOLS];
104 ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
105 ev.data.data32[1] = XCB_CURRENT_TIME;
107 LOG("Sending WM_DELETE to the client\n");
108 xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
113 * Checks if the given window class and title match the given client
114 * Window title is passed as "normal" string and as UCS-2 converted string for
115 * matching _NET_WM_NAME capable clients as well as those using legacy hints.
118 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
119 char *to_title_ucs, int to_title_ucs_len) {
120 /* Check if the given class is part of the window class */
121 if (client->window_class == NULL || strcasestr(client->window_class, to_class) == NULL)
124 /* If no title was given, we’re done */
125 if (to_title == NULL)
128 if (client->name_len > -1) {
129 /* UCS-2 converted window titles */
130 if (client->name == NULL || memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
134 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
142 * Enters fullscreen mode for the given client. This is called by toggle_fullscreen
143 * and when moving a fullscreen client to another screen.
146 void client_enter_fullscreen(xcb_connection_t *conn, Client *client) {
147 Workspace *workspace = client->workspace;
149 if (workspace->fullscreen_client != NULL) {
150 LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
154 client->fullscreen = true;
155 workspace->fullscreen_client = client;
156 LOG("Entering fullscreen mode...\n");
157 /* We just entered fullscreen mode, let’s configure the window */
158 uint32_t mask = XCB_CONFIG_WINDOW_X |
159 XCB_CONFIG_WINDOW_Y |
160 XCB_CONFIG_WINDOW_WIDTH |
161 XCB_CONFIG_WINDOW_HEIGHT;
162 uint32_t values[4] = {workspace->rect.x,
164 workspace->rect.width,
165 workspace->rect.height};
167 LOG("child itself will be at %dx%d with size %dx%d\n",
168 values[0], values[1], values[2], values[3]);
170 xcb_configure_window(conn, client->frame, mask, values);
172 /* Child’s coordinates are relative to the parent (=frame) */
175 xcb_configure_window(conn, client->child, mask, values);
177 /* Raise the window */
178 values[0] = XCB_STACK_MODE_ABOVE;
179 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
181 Rect child_rect = workspace->rect;
182 child_rect.x = child_rect.y = 0;
183 fake_configure_notify(conn, child_rect, 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 Client *first_floating = TAILQ_FIRST(&(client->workspace->floating_clients));
237 if (first_floating != TAILQ_END(&(client->workspace->floating_clients))) {
238 LOG("Setting below floating\n");
239 uint32_t values[] = { first_floating->frame, XCB_STACK_MODE_BELOW };
240 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
245 * Returns true if the client is floating. Makes the code more beatiful, as floating
246 * is not simply a boolean, but also saves whether the user selected the current state
247 * or whether it was automatically set.
250 bool client_is_floating(Client *client) {
251 return (client->floating >= FLOATING_AUTO_ON);
255 * Change the border type for the given client to normal (n), 1px border (p) or
256 * completely borderless (b).
259 void client_change_border(xcb_connection_t *conn, Client *client, char border_type) {
260 switch (border_type) {
262 LOG("Changing to normal border\n");
263 client->titlebar_position = TITLEBAR_TOP;
264 client->borderless = false;
267 LOG("Changing to 1px border\n");
268 client->titlebar_position = TITLEBAR_OFF;
269 client->borderless = false;
272 LOG("Changing to borderless\n");
273 client->titlebar_position = TITLEBAR_OFF;
274 client->borderless = true;
277 LOG("Unknown border mode\n");
281 /* Ensure that the child’s position inside our window gets updated */
282 client->force_reconfigure = true;
284 /* For clients inside a container, we can simply render the container.
285 * If the client is floating, we need to render the whole layout */
286 if (client->container != NULL)
287 render_container(conn, client->container);
288 else render_layout(conn);
290 redecorate_window(conn, client);