]> git.sur5r.net Git - i3/i3/blob - src/client.c
floating: Don’t let clients become hidden under stack windows or fulscreen clients...
[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                 struct Stack_Window *stack_win = &(container->stack_win);
43                 stack_win->rect.height = 0;
44                 xcb_unmap_window(conn, stack_win->window);
45         }
46 }
47
48 /*
49  * Warps the pointer into the given client (in the middle of it, to be specific), therefore
50  * selecting it
51  *
52  */
53 void client_warp_pointer_into(xcb_connection_t *conn, Client *client) {
54         int mid_x = client->rect.width / 2,
55             mid_y = client->rect.height / 2;
56         xcb_warp_pointer(conn, XCB_NONE, client->child, 0, 0, 0, 0, mid_x, mid_y);
57 }
58
59 /*
60  * Returns true if the client supports the given protocol atom (like WM_DELETE_WINDOW)
61  *
62  */
63 static bool client_supports_protocol(xcb_connection_t *conn, Client *client, xcb_atom_t atom) {
64         xcb_get_property_cookie_t cookie;
65         xcb_get_wm_protocols_reply_t protocols;
66         bool result = false;
67
68         cookie = xcb_get_wm_protocols_unchecked(conn, client->child, atoms[WM_PROTOCOLS]);
69         if (xcb_get_wm_protocols_reply(conn, cookie, &protocols, NULL) != 1)
70                 return false;
71
72         /* Check if the client’s protocols have the requested atom set */
73         for (uint32_t i = 0; i < protocols.atoms_len; i++)
74                 if (protocols.atoms[i] == atom)
75                         result = true;
76
77         xcb_get_wm_protocols_reply_wipe(&protocols);
78
79         return result;
80 }
81
82 /*
83  * Kills the given window using WM_DELETE_WINDOW or xcb_kill_window
84  *
85  */
86 void client_kill(xcb_connection_t *conn, Client *window) {
87         /* If the client does not support WM_DELETE_WINDOW, we kill it the hard way */
88         if (!client_supports_protocol(conn, window, atoms[WM_DELETE_WINDOW])) {
89                 LOG("Killing window the hard way\n");
90                 xcb_kill_client(conn, window->child);
91                 return;
92         }
93
94         xcb_client_message_event_t ev;
95
96         memset(&ev, 0, sizeof(xcb_client_message_event_t));
97
98         ev.response_type = XCB_CLIENT_MESSAGE;
99         ev.window = window->child;
100         ev.type = atoms[WM_PROTOCOLS];
101         ev.format = 32;
102         ev.data.data32[0] = atoms[WM_DELETE_WINDOW];
103         ev.data.data32[1] = XCB_CURRENT_TIME;
104
105         LOG("Sending WM_DELETE to the client\n");
106         xcb_send_event(conn, false, window->child, XCB_EVENT_MASK_NO_EVENT, (char*)&ev);
107         xcb_flush(conn);
108 }
109
110 /*
111  * Checks if the given window class and title match the given client
112  * Window title is passed as "normal" string and as UCS-2 converted string for
113  * matching _NET_WM_NAME capable clients as well as those using legacy hints.
114  *
115  */
116 bool client_matches_class_name(Client *client, char *to_class, char *to_title,
117                                char *to_title_ucs, int to_title_ucs_len) {
118         /* Check if the given class is part of the window class */
119         if (client->window_class == NULL || strcasestr(client->window_class, to_class) == NULL)
120                 return false;
121
122         /* If no title was given, we’re done */
123         if (to_title == NULL)
124                 return true;
125
126         if (client->name_len > -1) {
127                 /* UCS-2 converted window titles */
128                 if (client->name == NULL || memmem(client->name, (client->name_len * 2), to_title_ucs, (to_title_ucs_len * 2)) == NULL)
129                         return false;
130         } else {
131                 /* Legacy hints */
132                 if (client->name == NULL || strcasestr(client->name, to_title) == NULL)
133                         return false;
134         }
135
136         return true;
137 }
138
139 /*
140  * Toggles fullscreen mode for the given client. It updates the data structures and
141  * reconfigures (= resizes/moves) the client and its frame to the full size of the
142  * screen. When leaving fullscreen, re-rendering the layout is forced.
143  *
144  */
145 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
146         /* dock clients cannot enter fullscreen mode */
147         assert(!client->dock);
148
149         Workspace *workspace = client->workspace;
150
151         if (!client->fullscreen) {
152                 if (workspace->fullscreen_client != NULL) {
153                         LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
154                         return;
155                 }
156                 client->fullscreen = true;
157                 workspace->fullscreen_client = client;
158                 LOG("Entering fullscreen mode...\n");
159                 /* We just entered fullscreen mode, let’s configure the window */
160                 uint32_t mask = XCB_CONFIG_WINDOW_X |
161                                 XCB_CONFIG_WINDOW_Y |
162                                 XCB_CONFIG_WINDOW_WIDTH |
163                                 XCB_CONFIG_WINDOW_HEIGHT;
164                 uint32_t values[4] = {workspace->rect.x,
165                                       workspace->rect.y,
166                                       workspace->rect.width,
167                                       workspace->rect.height};
168
169                 LOG("child itself will be at %dx%d with size %dx%d\n",
170                                 values[0], values[1], values[2], values[3]);
171
172                 xcb_configure_window(conn, client->frame, mask, values);
173
174                 /* Child’s coordinates are relative to the parent (=frame) */
175                 values[0] = 0;
176                 values[1] = 0;
177                 xcb_configure_window(conn, client->child, mask, values);
178
179                 /* Raise the window */
180                 values[0] = XCB_STACK_MODE_ABOVE;
181                 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
182
183                 Rect child_rect = workspace->rect;
184                 child_rect.x = child_rect.y = 0;
185                 fake_configure_notify(conn, child_rect, client->child);
186         } else {
187                 LOG("leaving fullscreen mode\n");
188                 client->fullscreen = false;
189                 workspace->fullscreen_client = NULL;
190                 if (client->floating >= FLOATING_AUTO_ON) {
191                         /* For floating clients it’s enough if we just reconfigure that window (in fact,
192                          * re-rendering the layout will not update the client.) */
193                         reposition_client(conn, client);
194                         resize_client(conn, client);
195                         /* redecorate_window flushes */
196                         redecorate_window(conn, client);
197                 } else {
198                         client_set_below_floating(conn, client);
199
200                         /* Because the coordinates of the window haven’t changed, it would not be
201                            re-configured if we don’t set the following flag */
202                         client->force_reconfigure = true;
203                         /* We left fullscreen mode, redraw the whole layout to ensure enternotify events are disabled */
204                         render_layout(conn);
205                 }
206         }
207
208         xcb_flush(conn);
209 }
210
211 /*
212  * Sets the position of the given client in the X stack to the highest (tiling layer is always
213  * on the same position, so this doesn’t matter) below the first floating client, so that
214  * floating windows are always on top.
215  *
216  */
217 void client_set_below_floating(xcb_connection_t *conn, Client *client) {
218         /* Ensure that it is below all floating clients */
219         Client *first_floating = TAILQ_FIRST(&(client->workspace->floating_clients));
220         if (first_floating != TAILQ_END(&(client->workspace->floating_clients))) {
221                 LOG("Setting below floating\n");
222                 uint32_t values[] = { first_floating->frame, XCB_STACK_MODE_BELOW };
223                 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
224         }
225 }