]> git.sur5r.net Git - i3/i3/blob - src/client.c
add xcb_set_window_rect which configures a window according to a Rect
[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 #include "table.h"
28 #include "workspace.h"
29 #include "config.h"
30 #include "log.h"
31
32 /*
33  * Removes the given client from the container, either because it will be inserted into another
34  * one or because it was unmapped
35  *
36  */
37 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container, bool remove_from_focusstack) {
38         CIRCLEQ_REMOVE(&(container->clients), client, clients);
39
40         if (remove_from_focusstack)
41                 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
42
43         /* If the container will be empty now and is in stacking mode, we need to
44            unmap the stack_win */
45         if (CIRCLEQ_EMPTY(&(container->clients)) &&
46             (container->mode == MODE_STACK ||
47              container->mode == MODE_TABBED)) {
48                 DLOG("Unmapping stack window\n");
49                 struct Stack_Window *stack_win = &(container->stack_win);
50                 stack_win->rect.height = 0;
51                 xcb_unmap_window(conn, stack_win->window);
52                 xcb_flush(conn);
53         }
54 }
55
56 /*
57  * Warps the pointer into the given client (in the middle of it, to be specific), therefore
58  * selecting it
59  *
60  */
61 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
62         int mid_x = client->rect.width / 2,
63             mid_y = client->rect.height / 2;
64         xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
65 }
66
67 /*
68  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
69  *
70  */
71 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
72         xcb_get_property_cookie_t cookie;
73         xcb_get_wm_protocols_reply_t protocols;
74         bool result = false;
75
76         cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
77         if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
78                 return false;
79
80         /* Check if the client’s protocols have the requested atom set */
81         for (uint32_t i = 0; i < protocols.atoms_len; i++)
82                 if (protocols.atoms[i] == atom)
83                         result = true;
84
85         xcb_get_wm_protocols_reply_wipe(&protocols);
86
87         return result;
88 }
89
90 /*
91  * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
92  *
93  */
94 void client_kill(xcb_connection_t *conn, Client *window) {
95         /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
96         if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
97                 LOG("Killing window the hard way\n");
98                 xcb_kill_client(conn, window->child);
99                 return;
100         }
101
102         xcb_client_message_event_t ev;
103
104         memset(&ev, 0, sizeof(xcb_client_message_event_t));
105
106         ev.response_type = XCB_CLIENT_MESSAGE;
107         ev.window = window->child;
108         ev.type = atoms[WM_PROTOCOLS];
109         ev.format = 32;
110         ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
111         ev.data.data32[1] = XCB_CURRENT_TIME;
112
113         LOG("Sending WM_DELETE to the client\n");
114         xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
115         xcb_flush(conn);
116 }
117
118 /*
119  * Checks if the given window class and title match the given client
120  * Window title is passed as "normal" string and as UCS-2 converted string for
121  * matching _NET_WM_NAME capable clients as well as those using legacy hints.
122  *
123  */
124 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
125                                char *to_title_ucs, int to_title_ucs_len) {
126         /* Check if the given class is part of the window class */
127         if ((client->window_class_instance == NULL ||
128              strcasestr(client->window_class_instance, to_class) == NULL) &&
129             (client->window_class_class == NULL ||
130              strcasestr(client->window_class_class, to_class) == NULL))
131                 return false;
132
133         /* If no title was given, we’re done */
134         if (to_title == NULL)
135                 return true;
136
137         if (client->name_len > -1) {
138                 /* UCS-2 converted window titles */
139                 if (client->name == NULL || memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
140                         return false;
141         } else {
142                 /* Legacy hints */
143                 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
144                         return false;
145         }
146
147         return true;
148 }
149
150 /*
151  * Enters fullscreen mode for the given client. This is called by toggle_fullscreen
152  * and when moving a fullscreen client to another screen.
153  *
154  */
155 void client_enter_fullscreen(xcb_connection_t *conn, Client *client) {
156         Workspace *workspace = client->workspace;
157
158         if (workspace->fullscreen_client != NULL) {
159                 LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
160                 return;
161         }
162
163         client->fullscreen = true;
164         workspace->fullscreen_client = client;
165         LOG("Entering fullscreen mode...\n");
166         /* We just entered fullscreen mode, let’s configure the window */
167         Rect r = workspace->rect;
168
169         DLOG("child itself will be at %dx%d with size %dx%d\n",
170                         r.x, r.y, r.width, r.height);
171
172         xcb_set_window_rect(conn, client->frame, r);
173
174         /* Child’s coordinates are relative to the parent (=frame) */
175         r.x = 0;
176         r.y = 0;
177         xcb_set_window_rect(conn, client->child, r);
178
179         /* Raise the window */
180         uint32_t values[] = { XCB_STACK_MODE_ABOVE };
181         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
182
183         fake_configure_notify(conn, r, 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         Workspace *ws = client->workspace;
237         Client *first_floating = TAILQ_FIRST(&(ws->floating_clients));
238         if (first_floating == TAILQ_END(&(ws->floating_clients)))
239                 return;
240
241         DLOG("Setting below floating\n");
242         uint32_t values[] = { first_floating->frame, XCB_STACK_MODE_BELOW };
243         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
244
245         if (client->workspace->fullscreen_client == NULL)
246                 return;
247
248         DLOG("(and below fullscreen)\n");
249         /* Ensure that the window is still below the fullscreen window */
250         values[0] = client->workspace->fullscreen_client->frame;
251         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
252 }
253
254 /*
255  * Returns true if the client is floating. Makes the code more beatiful, as floating
256  * is not simply a boolean, but also saves whether the user selected the current state
257  * or whether it was automatically set.
258  *
259  */
260 bool client_is_floating(Client *client) {
261         return (client->floating >= FLOATING_AUTO_ON);
262 }
263
264 /*
265  * Change the border type for the given client to normal (n), 1px border (p) or
266  * completely borderless (b) without actually re-rendering the layout. Useful
267  * for calling it when initializing a new client.
268  *
269  */
270 bool client_init_border(xcb_connection_t *conn, Client *client, char border_type) {
271         switch (border_type) {
272                 case 'n':
273                         LOG("Changing to normal border\n");
274                         client->titlebar_position = TITLEBAR_TOP;
275                         client->borderless = false;
276                         return true;
277                 case 'p':
278                         LOG("Changing to 1px border\n");
279                         client->titlebar_position = TITLEBAR_OFF;
280                         client->borderless = false;
281                         return true;
282                 case 'b':
283                         LOG("Changing to borderless\n");
284                         client->titlebar_position = TITLEBAR_OFF;
285                         client->borderless = true;
286                         return true;
287                 default:
288                         LOG("Unknown border mode\n");
289                         return false;
290         }
291 }
292
293 /*
294  * Change the border type for the given client to normal (n), 1px border (p) or
295  * completely borderless (b).
296  *
297  */
298 void client_change_border(xcb_connection_t *conn, Client *client, char border_type) {
299         if (!client_init_border(conn, client, border_type))
300                 return;
301
302         /* Ensure that the child’s position inside our window gets updated */
303         client->force_reconfigure = true;
304
305         /* For clients inside a container, we can simply render the container */
306         if (client->container != NULL)
307                 render_container(conn, client->container);
308         else {
309                 /* If the client is floating, directly push its size */
310                 if (client_is_floating(client))
311                         resize_client(conn, client);
312                 /* Otherwise, it may be a dock client, thus render the whole layout */
313                 else render_layout(conn);
314         }
315
316         redecorate_window(conn, client);
317 }
318
319 /*
320  * Unmap the client, correctly setting any state which is needed.
321  *
322  */
323 void client_unmap(xcb_connection_t *conn, Client *client) {
324         /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
325         long data[] = { XCB_WM_STATE_WITHDRAWN, XCB_NONE };
326         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, atoms[WM_STATE], atoms[WM_STATE], 32, 2, data);
327
328         xcb_unmap_window(conn, client->frame);
329 }
330
331 /*
332  * Map the client, correctly restoring any state needed.
333  *
334  */
335 void client_map(xcb_connection_t *conn, Client *client) {
336         /* Set WM_STATE_NORMAL because GTK applications don’t want to drag & drop if we don’t.
337          * Also, xprop(1) needs that to work. */
338         long data[] = { XCB_WM_STATE_NORMAL, XCB_NONE };
339         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, atoms[WM_STATE], atoms[WM_STATE], 32, 2, data);
340
341         xcb_map_window(conn, client->frame);
342 }
343
344 /*
345  * Set the given mark for this client. Used for jumping to the client
346  * afterwards (like m<mark> and '<mark> in vim).
347  *
348  */
349 void client_mark(xcb_connection_t *conn, Client *client, const char *mark) {
350         if (client->mark != NULL)
351                 free(client->mark);
352         client->mark = sstrdup(mark);
353
354         /* Make sure no other client has this mark set */
355         Client *current;
356         Workspace *ws;
357         TAILQ_FOREACH(ws, workspaces, workspaces)
358                 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
359                         if (current == client ||
360                             current->mark == NULL ||
361                             strcmp(current->mark, mark) != 0)
362                                 continue;
363
364                         free(current->mark);
365                         current->mark = NULL;
366                         /* We can break here since there can only be one other
367                          * client with this mark. */
368                         break;
369                 }
370 }
371
372 /*
373  * Returns the minimum height of a specific window. The height is calculated
374  * by using 2 pixels (for the client window itself), possibly padding this to
375  * comply with the client’s base_height and then adding the decoration height.
376  *
377  */
378 uint32_t client_min_height(Client *client) {
379         uint32_t height = max(2, client->base_height);
380         i3Font *font = load_font(global_conn, config.font);
381
382         if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
383                 return height;
384
385         if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
386                 return height + 2;
387
388         return height + font->height + 2 + 2;
389 }
390
391 /*
392  * See client_min_height.
393  *
394  */
395 uint32_t client_min_width(Client *client) {
396         uint32_t width = max(2, client->base_width);
397
398         if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
399                 return width;
400
401         if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
402                 return width + 2;
403
404         return width + 2 + 2;
405 }