]> git.sur5r.net Git - i3/i3/blob - src/client.c
Bugfix: Correctly unmap stack windows and don’t re-map them too early
[i3/i3] / src / client.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009 Michael Stapelberg and contributors
7  *
8  * See file LICENSE for license information.
9  *
10  * client.c: holds all client-specific functions
11  *
12  */
13 #include <stdlib.h>
14 #include <string.h>
15 #include <assert.h>
16
17 #include <xcb/xcb.h>
18 #include <xcb/xcb_icccm.h>
19
20 #include "data.h"
21 #include "i3.h"
22 #include "xcb.h"
23 #include "util.h"
24 #include "queue.h"
25 #include "layout.h"
26 #include "client.h"
27
28 /*
29  * Removes the given client from the container, either because it will be inserted into another
30  * one or because it was unmapped
31  *
32  */
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);
35
36         if (remove_from_focusstack)
37                 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
38
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);
46                 xcb_flush(conn);
47         }
48 }
49
50 /*
51  * Warps the pointer into the given client (in the middle of it, to be specific), therefore
52  * selecting it
53  *
54  */
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);
59 }
60
61 /*
62  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
63  *
64  */
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;
68         bool result = false;
69
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)
72                 return false;
73
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)
77                         result = true;
78
79         xcb_get_wm_protocols_reply_wipe(&protocols);
80
81         return result;
82 }
83
84 /*
85  * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
86  *
87  */
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);
93                 return;
94         }
95
96         xcb_client_message_event_t ev;
97
98         memset(&ev, 0, sizeof(xcb_client_message_event_t));
99
100         ev.response_type = XCB_CLIENT_MESSAGE;
101         ev.window = window->child;
102         ev.type = atoms[WM_PROTOCOLS];
103         ev.format = 32;
104         ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
105         ev.data.data32[1] = XCB_CURRENT_TIME;
106
107         LOG("Sending WM_DELETE to the client\n");
108         xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
109         xcb_flush(conn);
110 }
111
112 /*
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.
116  *
117  */
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)
122                 return false;
123
124         /* If no title was given, we’re done */
125         if (to_title == NULL)
126                 return true;
127
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)
131                         return false;
132         } else {
133                 /* Legacy hints */
134                 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
135                         return false;
136         }
137
138         return true;
139 }
140
141 /*
142  * Enters fullscreen mode for the given client. This is called by toggle_fullscreen
143  * and when moving a fullscreen client to another screen.
144  *
145  */
146 void client_enter_fullscreen(xcb_connection_t *conn, Client *client) {
147         Workspace *workspace = client->workspace;
148
149         if (workspace->fullscreen_client != NULL) {
150                 LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
151                 return;
152         }
153
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,
163                               workspace->rect.y,
164                               workspace->rect.width,
165                               workspace->rect.height};
166
167         LOG("child itself will be at %dx%d with size %dx%d\n",
168                         values[0], values[1], values[2], values[3]);
169
170         xcb_configure_window(conn, client->frame, mask, values);
171
172         /* Child’s coordinates are relative to the parent (=frame) */
173         values[0] = 0;
174         values[1] = 0;
175         xcb_configure_window(conn, client->child, mask, values);
176
177         /* Raise the window */
178         values[0] = XCB_STACK_MODE_ABOVE;
179         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
180
181         Rect child_rect = workspace->rect;
182         child_rect.x = child_rect.y = 0;
183         fake_configure_notify(conn, child_rect, client->child);
184
185         xcb_flush(conn);
186 }
187
188 /*
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.
192  *
193  */
194 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
195         /* dock clients cannot enter fullscreen mode */
196         assert(!client->dock);
197
198         Workspace *workspace = client->workspace;
199
200         if (!client->fullscreen) {
201                 client_enter_fullscreen(conn, client);
202                 return;
203         }
204
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);
215         } else {
216                 client_set_below_floating(conn, client);
217
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 */
222                 render_layout(conn);
223         }
224
225         xcb_flush(conn);
226 }
227
228 /*
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.
232  *
233  */
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);
241         }
242 }
243
244 /*
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.
248  *
249  */
250 bool client_is_floating(Client *client) {
251         return (client->floating >= FLOATING_AUTO_ON);
252 }
253
254 /*
255  * Change the border type for the given client to normal (n), 1px border (p) or
256  * completely borderless (b).
257  *
258  */
259 void client_change_border(xcb_connection_t *conn, Client *client, char border_type) {
260         switch (border_type) {
261                 case 'n':
262                         LOG("Changing to normal border\n");
263                         client->titlebar_position = TITLEBAR_TOP;
264                         client->borderless = false;
265                         break;
266                 case 'p':
267                         LOG("Changing to 1px border\n");
268                         client->titlebar_position = TITLEBAR_OFF;
269                         client->borderless = false;
270                         break;
271                 case 'b':
272                         LOG("Changing to borderless\n");
273                         client->titlebar_position = TITLEBAR_OFF;
274                         client->borderless = true;
275                         break;
276                 default:
277                         LOG("Unknown border mode\n");
278                         return;
279         }
280
281         /* Ensure that the child’s position inside our window gets updated */
282         client->force_reconfigure = true;
283
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);
289
290         redecorate_window(conn, client);
291 }