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 * Removes the given client from the container, either because it will be inserted into another
29 * one or because it was unmapped
32 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container) {
33 CIRCLEQ_REMOVE(&(container->clients), client, clients);
35 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
37 /* If the container will be empty now and is in stacking mode, we need to
38 unmap the stack_win */
39 if (CIRCLEQ_EMPTY(&(container->clients)) && container->mode == MODE_STACK) {
40 struct Stack_Window *stack_win = &(container->stack_win);
41 stack_win->rect.height = 0;
42 xcb_unmap_window(conn, stack_win->window);
47 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
51 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
52 int mid_x = client->rect.width / 2,
53 mid_y = client->rect.height / 2;
54 xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
58 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
61 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
62 xcb_get_property_cookie_t cookie;
63 xcb_get_wm_protocols_reply_t protocols;
66 cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
67 if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
70 /* Check if the client’s protocols have the requested atom set */
71 for (uint32_t i = 0; i < protocols.atoms_len; i++)
72 if (protocols.atoms[i] == atom)
75 xcb_get_wm_protocols_reply_wipe(&protocols);
81 * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
84 void client_kill(xcb_connection_t *conn, Client *window) {
85 /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
86 if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
87 LOG("Killing window the hard way\n");
88 xcb_kill_client(conn, window->child);
92 xcb_client_message_event_t ev;
94 memset(&ev, 0, sizeof(xcb_client_message_event_t));
96 ev.response_type = XCB_CLIENT_MESSAGE;
97 ev.window = window->child;
98 ev.type = atoms[WM_PROTOCOLS];
100 ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
101 ev.data.data32[1] = XCB_CURRENT_TIME;
103 LOG("Sending WM_DELETE to the client\n");
104 xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
109 * Checks if the given window class and title match the given client
110 * Window title is passed as "normal" string and as UCS-2 converted string for
111 * matching _NET_WM_NAME capable clients as well as those using legacy hints.
114 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
115 char *to_title_ucs, int to_title_ucs_len) {
116 /* Check if the given class is part of the window class */
117 if (strcasestr(client->window_class, to_class) == NULL)
120 /* If no title was given, we’re done */
121 if (to_title == NULL)
124 if (client->name_len > -1) {
125 /* UCS-2 converted window titles */
126 if (memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
130 if (strcasestr(client->name, to_title) == NULL)
138 * Toggles fullscreen mode for the given client. It updates the data structures and
139 * reconfigures (= resizes/moves) the client and its frame to the full size of the
140 * screen. When leaving fullscreen, re-rendering the layout is forced.
143 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
144 /* dock clients cannot enter fullscreen mode */
145 assert(!client->dock);
147 Workspace *workspace = client->workspace;
149 if (!client->fullscreen) {
150 if (workspace->fullscreen_client != NULL) {
151 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);
185 LOG("leaving fullscreen mode\n");
186 client->fullscreen = false;
187 workspace->fullscreen_client = NULL;
188 if (client->floating) {
189 /* For floating clients it’s enough if we just reconfigure that window (in fact,
190 * re-rendering the layout will not update the client.) */
191 reposition_client(conn, client);
192 resize_client(conn, client);
193 /* redecorate_window flushes */
194 redecorate_window(conn, client);
196 /* Because the coordinates of the window haven’t changed, it would not be
197 re-configured if we don’t set the following flag */
198 client->force_reconfigure = true;
199 /* We left fullscreen mode, redraw the whole layout to ensure enternotify events are disabled */