]> git.sur5r.net Git - i3/i3/blob - src/client.c
Make i3 compatible with the very latest xcb
[i3/i3] / src / client.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 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 #include <limits.h>
17
18 /* Contains compatibility definitions for old libxcb versions */
19 #ifdef XCB_COMPAT
20 #include "xcb_compat.h"
21 #endif
22
23 #include <xcb/xcb.h>
24 #include <xcb/xcb_icccm.h>
25
26 #include "data.h"
27 #include "i3.h"
28 #include "xcb.h"
29 #include "util.h"
30 #include "queue.h"
31 #include "layout.h"
32 #include "client.h"
33 #include "table.h"
34 #include "workspace.h"
35 #include "config.h"
36 #include "log.h"
37
38 /*
39  * Removes the given client from the container, either because it will be inserted into another
40  * one or because it was unmapped
41  *
42  */
43 void client_remove_from_container(xcb_connection_t *conn, Client *client, Container *container, bool remove_from_focusstack) {
44         CIRCLEQ_REMOVE(&(container->clients), client, clients);
45
46         if (remove_from_focusstack)
47                 SLIST_REMOVE(&(container->workspace->focus_stack), client, Client, focus_clients);
48
49         /* If the container will be empty now and is in stacking mode, we need to
50            unmap the stack_win */
51         if (CIRCLEQ_EMPTY(&(container->clients)) &&
52             (container->mode == MODE_STACK ||
53              container->mode == MODE_TABBED)) {
54                 DLOG("Unmapping stack window\n");
55                 struct Stack_Window *stack_win = &(container->stack_win);
56                 stack_win->rect.height = 0;
57                 xcb_unmap_window(conn, stack_win->window);
58                 xcb_flush(conn);
59         }
60 }
61
62 /*
63  * Warps the pointer into the given client (in the middle of it, to be specific), therefore
64  * selecting it
65  *
66  */
67 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
68         int mid_x = client->rect.width / 2,
69             mid_y = client->rect.height / 2;
70         xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
71 }
72
73 /*
74  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
75  *
76  */
77 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
78         xcb_get_property_cookie_t cookie;
79         xcb_icccm_get_wm_protocols_reply_t protocols;
80         bool result = false;
81
82         cookie = xcb_icccm_get_wm_protocols_unchecked(conn, client->child, A_WM_PROTOCOLS);
83         if (xcb_icccm_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
84                 return false;
85
86         /* Check if the client’s protocols have the requested atom set */
87         for (uint32_t i = 0; i < protocols.atoms_len; i++)
88                 if (protocols.atoms[i] == atom)
89                         result = true;
90
91         xcb_icccm_get_wm_protocols_reply_wipe(&protocols);
92
93         return result;
94 }
95
96 /*
97  * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
98  *
99  */
100 void client_kill(xcb_connection_t *conn, Client *window) {
101         /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
102         if (!client_supports_protocol(conn, window, A_WM_DELETE_WINDOW)) {
103                 LOG("Killing window the hard way\n");
104                 xcb_kill_client(conn, window->child);
105                 return;
106         }
107
108         xcb_client_message_event_t ev;
109
110         memset(&ev, 0, sizeof(xcb_client_message_event_t));
111
112         ev.response_type = XCB_CLIENT_MESSAGE;
113         ev.window = window->child;
114         ev.type = A_WM_PROTOCOLS;
115         ev.format = 32;
116         ev.data.data32[0] = A_WM_DELETE_WINDOW;
117         ev.data.data32[1] = XCB_CURRENT_TIME;
118
119         LOG("Sending WM_DELETE to the client\n");
120         xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
121         xcb_flush(conn);
122 }
123
124 /*
125  * Checks if the given window class and title match the given client
126  * Window title is passed as "normal" string and as UCS-2 converted string for
127  * matching _NET_WM_NAME capable clients as well as those using legacy hints.
128  *
129  */
130 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
131                                char *to_title_ucs, int to_title_ucs_len) {
132         /* Check if the given class is part of the window class */
133         if ((client->window_class_instance == NULL ||
134              strcasestr(client->window_class_instance, to_class) == NULL) &&
135             (client->window_class_class == NULL ||
136              strcasestr(client->window_class_class, to_class) == NULL))
137                 return false;
138
139         /* If no title was given, we’re done */
140         if (to_title == NULL)
141                 return true;
142
143         if (client->name_len > -1) {
144                 /* UCS-2 converted window titles */
145                 if (client->name == NULL || memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
146                         return false;
147         } else {
148                 /* Legacy hints */
149                 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
150                         return false;
151         }
152
153         return true;
154 }
155
156 /*
157  * Enters fullscreen mode for the given client. This is called by toggle_fullscreen
158  * and when moving a fullscreen client to another screen.
159  *
160  */
161 void client_enter_fullscreen(xcb_connection_t *conn, Client *client, bool global) {
162         Workspace *workspace;
163         Output *output;
164         Rect r;
165
166         if (global) {
167                 TAILQ_FOREACH(output, &outputs, outputs) {
168                         if (!output->active)
169                                 continue;
170
171                         if (output->current_workspace->fullscreen_client == NULL)
172                                 continue;
173
174                         LOG("Not entering global fullscreen mode, there already "
175                             "is a fullscreen client on output %s.\n", output->name);
176                         return;
177                 }
178
179                 r = (Rect) { UINT_MAX, UINT_MAX, 0,0 };
180                 Output *output;
181
182                 /* Set fullscreen_client for each active workspace.
183                  * Expand the rectangle to contain all outputs. */
184                 TAILQ_FOREACH(output, &outputs, outputs) {
185                         if (!output->active)
186                                 continue;
187
188                         output->current_workspace->fullscreen_client = client;
189
190                         /* Temporarily abuse width/heigth as coordinates of the lower right corner */
191                         if (r.x > output->rect.x)
192                                 r.x = output->rect.x;
193                         if (r.y > output->rect.y)
194                                 r.y = output->rect.y;
195                         if (r.x + r.width < output->rect.x + output->rect.width)
196                                 r.width = output->rect.x + output->rect.width;
197                         if (r.y + r.height < output->rect.y + output->rect.height)
198                                 r.height = output->rect.y + output->rect.height;
199                 }
200
201                 /* Putting them back to their original meaning */
202                 r.height -= r.x;
203                 r.width -= r.y;
204
205                 LOG("Entering global fullscreen mode...\n");
206         } else {
207                 workspace = client->workspace;
208                 if (workspace->fullscreen_client != NULL && workspace->fullscreen_client != client) {
209                         LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
210                         return;
211                 }
212
213                 workspace->fullscreen_client = client;
214                 r = workspace->rect;
215
216                 LOG("Entering fullscreen mode...\n");
217         }
218
219         client->fullscreen = true;
220
221         /* We just entered fullscreen mode, let’s configure the window */
222         DLOG("child itself will be at %dx%d with size %dx%d\n",
223                         r.x, r.y, r.width, r.height);
224
225         xcb_set_window_rect(conn, client->frame, r);
226
227         /* Child’s coordinates are relative to the parent (=frame) */
228         r.x = 0;
229         r.y = 0;
230         xcb_set_window_rect(conn, client->child, r);
231
232         /* Raise the window */
233         uint32_t values[] = { XCB_STACK_MODE_ABOVE };
234         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
235
236         /* Update _NET_WM_STATE */
237         values[0] = A__NET_WM_STATE_FULLSCREEN;
238         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, A__NET_WM_STATE, A_ATOM, 32, 1, values);
239
240         fake_configure_notify(conn, r, client->child);
241
242         xcb_flush(conn);
243 }
244
245 /*
246  * Leaves fullscreen mode for the current client. This is called by toggle_fullscreen.
247  *
248  */
249 void client_leave_fullscreen(xcb_connection_t *conn, Client *client) {
250         LOG("leaving fullscreen mode\n");
251         client->fullscreen = false;
252         Workspace *ws;
253         TAILQ_FOREACH(ws, workspaces, workspaces)
254                 if (ws->fullscreen_client == client)
255                         ws->fullscreen_client = NULL;
256
257         if (client_is_floating(client)) {
258                 /* For floating clients it’s enough if we just reconfigure that window (in fact,
259                  * re-rendering the layout will not update the client.) */
260                 reposition_client(conn, client);
261                 resize_client(conn, client);
262                 /* redecorate_window flushes */
263                 redecorate_window(conn, client);
264         } else {
265                 client_set_below_floating(conn, client);
266
267                 /* Because the coordinates of the window haven’t changed, it would not be
268                    re-configured if we don’t set the following flag */
269                 client->force_reconfigure = true;
270                 /* We left fullscreen mode, redraw the whole layout to ensure enternotify events are disabled */
271                 render_layout(conn);
272         }
273
274         /* Update _NET_WM_STATE */
275         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, A__NET_WM_STATE, A_ATOM, 32, 0, NULL);
276
277         xcb_flush(conn);
278 }
279
280 /*
281  * Toggles fullscreen mode for the given client. It updates the data structures and
282  * reconfigures (= resizes/moves) the client and its frame to the full size of the
283  * screen. When leaving fullscreen, re-rendering the layout is forced.
284  *
285  */
286 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
287         /* dock clients cannot enter fullscreen mode */
288         assert(!client->dock);
289
290         if (!client->fullscreen) {
291                 client_enter_fullscreen(conn, client, false);
292         } else {
293                 client_leave_fullscreen(conn, client);
294         }
295 }
296
297 /*
298  * Like client_toggle_fullscreen(), but putting it in global fullscreen-mode.
299  *
300  */
301 void client_toggle_fullscreen_global(xcb_connection_t *conn, Client *client) {
302         /* dock clients cannot enter fullscreen mode */
303         assert(!client->dock);
304
305         if (!client->fullscreen) {
306                 client_enter_fullscreen(conn, client, true);
307         } else {
308                 client_leave_fullscreen(conn, client);
309         }
310 }
311
312 /*
313  * Sets the position of the given client in the X stack to the highest (tiling layer is always
314  * on the same position, so this doesn’t matter) below the first floating client, so that
315  * floating windows are always on top.
316  *
317  */
318 void client_set_below_floating(xcb_connection_t *conn, Client *client) {
319         /* Ensure that it is below all floating clients */
320         Workspace *ws = client->workspace;
321         Client *first_floating = TAILQ_FIRST(&(ws->floating_clients));
322         if (first_floating == TAILQ_END(&(ws->floating_clients)))
323                 return;
324
325         DLOG("Setting below floating\n");
326         uint32_t values[] = { first_floating->frame, XCB_STACK_MODE_BELOW };
327         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
328
329         if (client->workspace->fullscreen_client == NULL)
330                 return;
331
332         DLOG("(and below fullscreen)\n");
333         /* Ensure that the window is still below the fullscreen window */
334         values[0] = client->workspace->fullscreen_client->frame;
335         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
336 }
337
338 /*
339  * Returns true if the client is floating. Makes the code more beatiful, as floating
340  * is not simply a boolean, but also saves whether the user selected the current state
341  * or whether it was automatically set.
342  *
343  */
344 bool client_is_floating(Client *client) {
345         return (client->floating >= FLOATING_AUTO_ON);
346 }
347
348 /*
349  * Change the border type for the given client to normal (n), 1px border (p) or
350  * completely borderless (b) without actually re-rendering the layout. Useful
351  * for calling it when initializing a new client.
352  *
353  */
354 bool client_init_border(xcb_connection_t *conn, Client *client, char border_type) {
355         switch (border_type) {
356                 case 'n':
357                         LOG("Changing to normal border\n");
358                         client->titlebar_position = TITLEBAR_TOP;
359                         client->borderless = false;
360                         return true;
361                 case 'p':
362                         LOG("Changing to 1px border\n");
363                         client->titlebar_position = TITLEBAR_OFF;
364                         client->borderless = false;
365                         return true;
366                 case 'b':
367                         LOG("Changing to borderless\n");
368                         client->titlebar_position = TITLEBAR_OFF;
369                         client->borderless = true;
370                         return true;
371                 default:
372                         LOG("Unknown border mode\n");
373                         return false;
374         }
375 }
376
377 /*
378  * Change the border type for the given client to normal (n), 1px border (p) or
379  * completely borderless (b).
380  *
381  */
382 void client_change_border(xcb_connection_t *conn, Client *client, char border_type) {
383         if (!client_init_border(conn, client, border_type))
384                 return;
385
386         /* Ensure that the child’s position inside our window gets updated */
387         client->force_reconfigure = true;
388
389         /* For clients inside a container, we can simply render the container */
390         if (client->container != NULL)
391                 render_container(conn, client->container);
392         else {
393                 /* If the client is floating, directly push its size */
394                 if (client_is_floating(client))
395                         resize_client(conn, client);
396                 /* Otherwise, it may be a dock client, thus render the whole layout */
397                 else render_layout(conn);
398         }
399
400         redecorate_window(conn, client);
401 }
402
403 /*
404  * Unmap the client, correctly setting any state which is needed.
405  *
406  */
407 void client_unmap(xcb_connection_t *conn, Client *client) {
408         /* Set WM_STATE_WITHDRAWN, it seems like Java apps need it */
409         long data[] = { XCB_ICCCM_WM_STATE_WITHDRAWN, XCB_NONE };
410         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, A_WM_STATE, A_WM_STATE, 32, 2, data);
411
412         xcb_unmap_window(conn, client->frame);
413 }
414
415 /*
416  * Map the client, correctly restoring any state needed.
417  *
418  */
419 void client_map(xcb_connection_t *conn, Client *client) {
420         /* Set WM_STATE_NORMAL because GTK applications don’t want to drag & drop if we don’t.
421          * Also, xprop(1) needs that to work. */
422         long data[] = { XCB_ICCCM_WM_STATE_NORMAL, XCB_NONE };
423         xcb_change_property(conn, XCB_PROP_MODE_REPLACE, client->child, A_WM_STATE, A_WM_STATE, 32, 2, data);
424
425         xcb_map_window(conn, client->frame);
426 }
427
428 /*
429  * Set the given mark for this client. Used for jumping to the client
430  * afterwards (like m<mark> and '<mark> in vim).
431  *
432  */
433 void client_mark(xcb_connection_t *conn, Client *client, const char *mark) {
434         if (client->mark != NULL)
435                 free(client->mark);
436         client->mark = sstrdup(mark);
437
438         /* Make sure no other client has this mark set */
439         Client *current;
440         Workspace *ws;
441         TAILQ_FOREACH(ws, workspaces, workspaces)
442                 SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
443                         if (current == client ||
444                             current->mark == NULL ||
445                             strcmp(current->mark, mark) != 0)
446                                 continue;
447
448                         free(current->mark);
449                         current->mark = NULL;
450                         /* We can break here since there can only be one other
451                          * client with this mark. */
452                         break;
453                 }
454 }
455
456 /*
457  * Returns the minimum height of a specific window. The height is calculated
458  * by using 2 pixels (for the client window itself), possibly padding this to
459  * comply with the client’s base_height and then adding the decoration height.
460  *
461  */
462 uint32_t client_min_height(Client *client) {
463         uint32_t height = max(2, client->base_height);
464         i3Font *font = load_font(global_conn, config.font);
465
466         if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
467                 return height;
468
469         if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
470                 return height + 2;
471
472         return height + font->height + 2 + 2;
473 }
474
475 /*
476  * See client_min_height.
477  *
478  */
479 uint32_t client_min_width(Client *client) {
480         uint32_t width = max(2, client->base_width);
481
482         if (client->titlebar_position == TITLEBAR_OFF && client->borderless)
483                 return width;
484
485         if (client->titlebar_position == TITLEBAR_OFF && !client->borderless)
486                 return width + 2;
487
488         return width + 2 + 2;
489 }