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