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