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 struct Stack_Window *stack_win = &(container->stack_win);
43 stack_win->rect.height = 0;
44 xcb_unmap_window(conn, stack_win->window);
49 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
53 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
54 int mid_x = client->rect.width / 2,
55 mid_y = client->rect.height / 2;
56 xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
60 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
63 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
64 xcb_get_property_cookie_t cookie;
65 xcb_get_wm_protocols_reply_t protocols;
68 cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
69 if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
72 /* Check if the client’s protocols have the requested atom set */
73 for (uint32_t i = 0; i < protocols.atoms_len; i++)
74 if (protocols.atoms[i] == atom)
77 xcb_get_wm_protocols_reply_wipe(&protocols);
83 * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
86 void client_kill(xcb_connection_t *conn, Client *window) {
87 /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
88 if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
89 LOG("Killing window the hard way\n");
90 xcb_kill_client(conn, window->child);
94 xcb_client_message_event_t ev;
96 memset(&ev, 0, sizeof(xcb_client_message_event_t));
98 ev.response_type = XCB_CLIENT_MESSAGE;
99 ev.window = window->child;
100 ev.type = atoms[WM_PROTOCOLS];
102 ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
103 ev.data.data32[1] = XCB_CURRENT_TIME;
105 LOG("Sending WM_DELETE to the client\n");
106 xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
111 * Checks if the given window class and title match the given client
112 * Window title is passed as "normal" string and as UCS-2 converted string for
113 * matching _NET_WM_NAME capable clients as well as those using legacy hints.
116 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
117 char *to_title_ucs, int to_title_ucs_len) {
118 /* Check if the given class is part of the window class */
119 if (client->window_class == NULL || strcasestr(client->window_class, to_class) == NULL)
122 /* If no title was given, we’re done */
123 if (to_title == NULL)
126 if (client->name_len > -1) {
127 /* UCS-2 converted window titles */
128 if (client->name == NULL || memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
132 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
140 * Enters fullscreen mode for the given client. This is called by toggle_fullscreen
141 * and when moving a fullscreen client to another screen.
144 void client_enter_fullscreen(xcb_connection_t *conn, Client *client) {
145 Workspace *workspace = client->workspace;
147 if (workspace->fullscreen_client != NULL) {
148 LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
152 client->fullscreen = true;
153 workspace->fullscreen_client = client;
154 LOG("Entering fullscreen mode...\n");
155 /* We just entered fullscreen mode, let’s configure the window */
156 uint32_t mask = XCB_CONFIG_WINDOW_X |
157 XCB_CONFIG_WINDOW_Y |
158 XCB_CONFIG_WINDOW_WIDTH |
159 XCB_CONFIG_WINDOW_HEIGHT;
160 uint32_t values[4] = {workspace->rect.x,
162 workspace->rect.width,
163 workspace->rect.height};
165 LOG("child itself will be at %dx%d with size %dx%d\n",
166 values[0], values[1], values[2], values[3]);
168 xcb_configure_window(conn, client->frame, mask, values);
170 /* Child’s coordinates are relative to the parent (=frame) */
173 xcb_configure_window(conn, client->child, mask, values);
175 /* Raise the window */
176 values[0] = XCB_STACK_MODE_ABOVE;
177 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
179 Rect child_rect = workspace->rect;
180 child_rect.x = child_rect.y = 0;
181 fake_configure_notify(conn, child_rect, client->child);
187 * Toggles fullscreen mode for the given client. It updates the data structures and
188 * reconfigures (= resizes/moves) the client and its frame to the full size of the
189 * screen. When leaving fullscreen, re-rendering the layout is forced.
192 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
193 /* dock clients cannot enter fullscreen mode */
194 assert(!client->dock);
196 Workspace *workspace = client->workspace;
198 if (!client->fullscreen) {
199 client_enter_fullscreen(conn, client);
203 LOG("leaving fullscreen mode\n");
204 client->fullscreen = false;
205 workspace->fullscreen_client = NULL;
206 if (client_is_floating(client)) {
207 /* For floating clients it’s enough if we just reconfigure that window (in fact,
208 * re-rendering the layout will not update the client.) */
209 reposition_client(conn, client);
210 resize_client(conn, client);
211 /* redecorate_window flushes */
212 redecorate_window(conn, client);
214 client_set_below_floating(conn, client);
216 /* Because the coordinates of the window haven’t changed, it would not be
217 re-configured if we don’t set the following flag */
218 client->force_reconfigure = true;
219 /* We left fullscreen mode, redraw the whole layout to ensure enternotify events are disabled */
227 * Sets the position of the given client in the X stack to the highest (tiling layer is always
228 * on the same position, so this doesn’t matter) below the first floating client, so that
229 * floating windows are always on top.
232 void client_set_below_floating(xcb_connection_t *conn, Client *client) {
233 /* Ensure that it is below all floating clients */
234 Client *first_floating = TAILQ_FIRST(&(client->workspace->floating_clients));
235 if (first_floating != TAILQ_END(&(client->workspace->floating_clients))) {
236 LOG("Setting below floating\n");
237 uint32_t values[] = { first_floating->frame, XCB_STACK_MODE_BELOW };
238 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
243 * Returns true if the client is floating. Makes the code more beatiful, as floating
244 * is not simply a boolean, but also saves whether the user selected the current state
245 * or whether it was automatically set.
248 bool client_is_floating(Client *client) {
249 return (client->floating >= FLOATING_AUTO_ON);