]> git.sur5r.net Git - i3/i3/blob - src/client.c
81430b2f8b94d9cc5da438f5a9bbad793b51e431
[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  * Enters fullscreen mode for the given client. This is called by toggle_fullscreen
141  * and when moving a fullscreen client to another screen.
142  *
143  */
144 void client_enter_fullscreen(xcb_connection_t *conn, Client *client) {
145         Workspace *workspace = client->workspace;
146
147         if (workspace->fullscreen_client != NULL) {
148                 LOG("Not entering fullscreen mode, there already is a fullscreen client.\n");
149                 return;
150         }
151
152         client->fullscreen = true;
153         workspace->fullscreen_client = client;
154         LOG("Entering fullscreen mode...\n");
155         /* We just entered fullscreen mode, let’s configure the window */
156         uint32_t mask = XCB_CONFIG_WINDOW_X |
157                         XCB_CONFIG_WINDOW_Y |
158                         XCB_CONFIG_WINDOW_WIDTH |
159                         XCB_CONFIG_WINDOW_HEIGHT;
160         uint32_t values[4] = {workspace->rect.x,
161                               workspace->rect.y,
162                               workspace->rect.width,
163                               workspace->rect.height};
164
165         LOG("child itself will be at %dx%d with size %dx%d\n",
166                         values[0], values[1], values[2], values[3]);
167
168         xcb_configure_window(conn, client->frame, mask, values);
169
170         /* Child’s coordinates are relative to the parent (=frame) */
171         values[0] = 0;
172         values[1] = 0;
173         xcb_configure_window(conn, client->child, mask, values);
174
175         /* Raise the window */
176         values[0] = XCB_STACK_MODE_ABOVE;
177         xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_STACK_MODE, values);
178
179         Rect child_rect = workspace->rect;
180         child_rect.x = child_rect.y = 0;
181         fake_configure_notify(conn, child_rect, client->child);
182
183         xcb_flush(conn);
184 }
185
186 /*
187  * Toggles fullscreen mode for the given client. It updates the data structures and
188  * reconfigures (= resizes/moves) the client and its frame to the full size of the
189  * screen. When leaving fullscreen, re-rendering the layout is forced.
190  *
191  */
192 void client_toggle_fullscreen(xcb_connection_t *conn, Client *client) {
193         /* dock clients cannot enter fullscreen mode */
194         assert(!client->dock);
195
196         Workspace *workspace = client->workspace;
197
198         if (!client->fullscreen) {
199                 client_enter_fullscreen(conn, client);
200                 return;
201         }
202
203         LOG("leaving fullscreen mode\n");
204         client->fullscreen = false;
205         workspace->fullscreen_client = NULL;
206         if (client_is_floating(client)) {
207                 /* For floating clients it’s enough if we just reconfigure that window (in fact,
208                  * re-rendering the layout will not update the client.) */
209                 reposition_client(conn, client);
210                 resize_client(conn, client);
211                 /* redecorate_window flushes */
212                 redecorate_window(conn, client);
213         } else {
214                 client_set_below_floating(conn, client);
215
216                 /* Because the coordinates of the window haven’t changed, it would not be
217                    re-configured if we don’t set the following flag */
218                 client->force_reconfigure = true;
219                 /* We left fullscreen mode, redraw the whole layout to ensure enternotify events are disabled */
220                 render_layout(conn);
221         }
222
223         xcb_flush(conn);
224 }
225
226 /*
227  * Sets the position of the given client in the X stack to the highest (tiling layer is always
228  * on the same position, so this doesn’t matter) below the first floating client, so that
229  * floating windows are always on top.
230  *
231  */
232 void client_set_below_floating(xcb_connection_t *conn, Client *client) {
233         /* Ensure that it is below all floating clients */
234         Client *first_floating = TAILQ_FIRST(&(client->workspace->floating_clients));
235         if (first_floating != TAILQ_END(&(client->workspace->floating_clients))) {
236                 LOG("Setting below floating\n");
237                 uint32_t values[] = { first_floating->frame, XCB_STACK_MODE_BELOW };
238                 xcb_configure_window(conn, client->frame, XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE, values);
239         }
240 }
241
242 /*
243  * Returns true if the client is floating. Makes the code more beatiful, as floating
244  * is not simply a boolean, but also saves whether the user selected the current state
245  * or whether it was automatically set.
246  *
247  */
248 bool client_is_floating(Client *client) {
249         return (client->floating >= FLOATING_AUTO_ON);
250 }
251
252 /*
253  * Change the border type for the given client to normal (n), 1px border (p) or
254  * completely borderless (b).
255  *
256  */
257 void client_change_border(xcb_connection_t *conn, Client *client, char border_type) {
258         switch (border_type) {
259                 case 'n':
260                         LOG("Changing to normal border\n");
261                         client->titlebar_position = TITLEBAR_TOP;
262                         client->borderless = false;
263                         break;
264                 case 'p':
265                         LOG("Changing to 1px border\n");
266                         client->titlebar_position = TITLEBAR_OFF;
267                         client->borderless = false;
268                         break;
269                 case 'b':
270                         LOG("Changing to borderless\n");
271                         client->titlebar_position = TITLEBAR_OFF;
272                         client->borderless = true;
273                         break;
274                 default:
275                         LOG("Unknown border mode\n");
276                         return;
277         }
278
279         /* Ensure that the child’s position inside our window gets updated */
280         client->force_reconfigure = true;
281
282         /* For clients inside a container, we can simply render the container.
283          * If the client is floating, we need to render the whole layout */
284         if (client->container != NULL)
285                 render_container(conn, client->container);
286         else render_layout(conn);
287
288         redecorate_window(conn, client);
289 }