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, bool remove_from_focusstack) {
33 CIRCLEQ_REMOVE(&(container->clients), client, clients);
35 if (remove_from_focusstack)
36 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
38 /* If the container will be empty now and is in stacking mode, we need to
39 unmap the stack_win */
40 if (CIRCLEQ_EMPTY(&(container->clients)) && container->mode == MODE_STACK) {
41 struct Stack_Window *stack_win = &(container->stack_win);
42 stack_win->rect.height = 0;
43 xcb_unmap_window(conn, stack_win->window);
48 * Warps the pointer into the given client (in the middle of it, to be specific), therefore
52 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
53 int mid_x = client->rect.width / 2,
54 mid_y = client->rect.height / 2;
55 xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
59 * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
62 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
63 xcb_get_property_cookie_t cookie;
64 xcb_get_wm_protocols_reply_t protocols;
67 cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
68 if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
71 /* Check if the client’s protocols have the requested atom set */
72 for (uint32_t i = 0; i < protocols.atoms_len; i++)
73 if (protocols.atoms[i] == atom)
76 xcb_get_wm_protocols_reply_wipe(&protocols);
82 * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
85 void client_kill(xcb_connection_t *conn, Client *window) {
86 /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
87 if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
88 LOG("Killing window the hard way\n");
89 xcb_kill_client(conn, window->child);
93 xcb_client_message_event_t ev;
95 memset(&ev, 0, sizeof(xcb_client_message_event_t));
97 ev.response_type = XCB_CLIENT_MESSAGE;
98 ev.window = window->child;
99 ev.type = atoms[WM_PROTOCOLS];
101 ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
102 ev.data.data32[1] = XCB_CURRENT_TIME;
104 LOG("Sending WM_DELETE to the client\n");
105 xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
110 * Checks if the given window class and title match the given client
111 * Window title is passed as "normal" string and as UCS-2 converted string for
112 * matching _NET_WM_NAME capable clients as well as those using legacy hints.
115 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
116 char *to_title_ucs, int to_title_ucs_len) {
117 /* Check if the given class is part of the window class */
118 if (client->window_class == NULL || strcasestr(client->window_class, to_class) == NULL)
121 /* If no title was given, we’re done */
122 if (to_title == NULL)
125 if (client->name_len > -1) {
126 /* UCS-2 converted window titles */
127 if (client->name == NULL || memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
131 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
139 * Toggles fullscreen mode for the given client. It updates the data structures and
140 * reconfigures (= resizes/moves) the client and its frame to the full size of the
141 * screen. When leaving fullscreen, re-rendering the layout is forced.
144 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
145 /* dock clients cannot enter fullscreen mode */
146 assert(!client->dock);
148 Workspace *workspace = client->workspace;
150 if (!client->fullscreen) {
151 if (workspace->fullscreen_client != NULL) {
152 LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
155 client->fullscreen = true;
156 workspace->fullscreen_client = client;
157 LOG("Entering fullscreen mode...\n");
158 /* We just entered fullscreen mode, let’s configure the window */
159 uint32_t mask = XCB_CONFIG_WINDOW_X |
160 XCB_CONFIG_WINDOW_Y |
161 XCB_CONFIG_WINDOW_WIDTH |
162 XCB_CONFIG_WINDOW_HEIGHT;
163 uint32_t values[4] = {workspace->rect.x,
165 workspace->rect.width,
166 workspace->rect.height};
168 LOG("child itself will be at %dx%d with size %dx%d\n",
169 values[0], values[1], values[2], values[3]);
171 xcb_configure_window(conn, client->frame, mask, values);
173 /* Child’s coordinates are relative to the parent (=frame) */
176 xcb_configure_window(conn, client->child, mask, values);
178 /* Raise the window */
179 values[0] = XCB_STACK_MODE_ABOVE;
180 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
182 Rect child_rect = workspace->rect;
183 child_rect.x = child_rect.y = 0;
184 fake_configure_notify(conn, child_rect, client->child);
186 LOG("leaving fullscreen mode\n");
187 client->fullscreen = false;
188 workspace->fullscreen_client = NULL;
189 if (client->floating >= FLOATING_AUTO_ON) {
190 /* For floating clients it’s enough if we just reconfigure that window (in fact,
191 * re-rendering the layout will not update the client.) */
192 reposition_client(conn, client);
193 resize_client(conn, client);
194 /* redecorate_window flushes */
195 redecorate_window(conn, client);
197 /* Because the coordinates of the window haven’t changed, it would not be
198 re-configured if we don’t set the following flag */
199 client->force_reconfigure = true;
200 /* We left fullscreen mode, redraw the whole layout to ensure enternotify events are disabled */