]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Bugfix: Re-distribute free space when closing rows which were horizontally resized
[i3/i3] / src / workspace.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  * workspace.c: Functions for modifying workspaces
11  *
12  */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <limits.h>
17 #include <err.h>
18
19 #include "util.h"
20 #include "data.h"
21 #include "i3.h"
22 #include "config.h"
23 #include "xcb.h"
24 #include "table.h"
25 #include "xinerama.h"
26 #include "layout.h"
27 #include "workspace.h"
28 #include "client.h"
29
30 /*
31  * Sets the name (or just its number) for the given workspace. This has to
32  * be called for every workspace as the rendering function
33  * (render_internal_bar) relies on workspace->name and workspace->name_len
34  * being ready-to-use.
35  *
36  */
37 void workspace_set_name(Workspace *ws, const char *name) {
38         char *label;
39         int ret;
40
41         if (name != NULL)
42                 ret = asprintf(&label, "%d: %s", ws->num + 1, name);
43         else ret = asprintf(&label, "%d", ws->num + 1);
44
45         if (ret == -1)
46                 errx(1, "asprintf() failed");
47
48         FREE(ws->name);
49
50         ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
51         if (config.font != NULL)
52                 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
53         else ws->text_width = 0;
54
55         free(label);
56 }
57
58 /*
59  * Returns true if the workspace is currently visible. Especially important for
60  * multi-monitor environments, as they can have multiple currenlty active
61  * workspaces.
62  *
63  */
64 bool workspace_is_visible(Workspace *ws) {
65         return (ws->screen->current_workspace == ws->num);
66 }
67
68 /*
69  * Switches to the given workspace
70  *
71  */
72 void workspace_show(xcb_connection_t *conn, int workspace) {
73         bool need_warp = false;
74         xcb_window_t root = xcb_setup_roots_iterator(xcb_get_setup(conn)).data->root;
75         /* t_ws (to workspace) is just a convenience pointer to the workspace we’re switching to */
76         Workspace *t_ws = &(workspaces[workspace-1]);
77
78         LOG("show_workspace(%d)\n", workspace);
79
80         /* Store current_row/current_col */
81         c_ws->current_row = current_row;
82         c_ws->current_col = current_col;
83
84         /* Check if the workspace has not been used yet */
85         workspace_initialize(t_ws, c_ws->screen);
86
87         if (c_ws->screen != t_ws->screen) {
88                 /* We need to switch to the other screen first */
89                 LOG("moving over to other screen.\n");
90
91                 /* Store the old client */
92                 Client *old_client = CUR_CELL->currently_focused;
93
94                 c_ws = &(workspaces[t_ws->screen->current_workspace]);
95                 current_col = c_ws->current_col;
96                 current_row = c_ws->current_row;
97                 if (CUR_CELL->currently_focused != NULL)
98                         need_warp = true;
99                 else {
100                         Rect *dims = &(c_ws->screen->rect);
101                         xcb_warp_pointer(conn, XCB_NONE, root, 0, 0, 0, 0,
102                                          dims->x + (dims->width / 2), dims->y + (dims->height / 2));
103                 }
104
105                 /* Re-decorate the old client, it’s not focused anymore */
106                 if ((old_client != NULL) && !old_client->dock)
107                         redecorate_window(conn, old_client);
108                 else xcb_flush(conn);
109         }
110
111         /* Check if we need to change something or if we’re already there */
112         if (c_ws->screen->current_workspace == (workspace-1)) {
113                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
114                 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
115                         set_focus(conn, last_focused, true);
116                 if (need_warp) {
117                         client_warp_pointer_into(conn, last_focused);
118                         xcb_flush(conn);
119                 }
120
121                 return;
122         }
123
124         t_ws->screen->current_workspace = workspace-1;
125         Workspace *old_workspace = c_ws;
126         c_ws = &workspaces[workspace-1];
127
128         /* Unmap all clients of the old workspace */
129         workspace_unmap_clients(conn, old_workspace);
130
131         current_row = c_ws->current_row;
132         current_col = c_ws->current_col;
133         LOG("new current row = %d, current col = %d\n", current_row, current_col);
134
135         workspace_map_clients(conn, c_ws);
136
137         /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
138          * render_layout afterwards, there is a short flickering on the source
139          * workspace (assign ws 3 to screen 0, ws 4 to screen 1, create single
140          * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
141          * flickering). */
142
143         /* Restore focus on the new workspace */
144         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
145         if (last_focused != SLIST_END(&(c_ws->focus_stack)))
146                 set_focus(conn, last_focused, true);
147         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
148
149         render_layout(conn);
150
151         /* We can warp the pointer only after the window has been
152          * reconfigured in render_layout, otherwise the pointer will
153          * be warped to the old position, which will not work when we
154          * moved it to another screen. */
155         if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
156                 client_warp_pointer_into(conn, last_focused);
157                 xcb_flush(conn);
158         }
159 }
160
161
162 /*
163  * Parses the preferred_screen property of a workspace. You can either specify
164  * the screen number (it is not given that the screen numbering always stays
165  * the same) or the screen coordinates (exact coordinates, e.g. 1280 will match
166  * the screen starting at x=1280, but 1281 will not). For coordinates, you can
167  * either specify an x coordinate ("1280") or an y coordinate ("x800") or both
168  * ("1280x800").
169  *
170  */
171 static i3Screen *get_screen_from_preference(struct screens_head *slist, char *preference) {
172         i3Screen *screen;
173         char *rest;
174         int preferred_screen = strtol(preference, &rest, 10);
175
176         LOG("Getting screen for preference \"%s\" (%d)\n", preference, preferred_screen);
177
178         if ((rest == preference) || (preferred_screen >= num_screens)) {
179                 int x = INT_MAX, y = INT_MAX;
180                 if (strchr(preference, 'x') != NULL) {
181                         /* Check if only the y coordinate was specified */
182                         if (*preference == 'x')
183                                 y = atoi(preference+1);
184                         else {
185                                 x = atoi(preference);
186                                 y = atoi(strchr(preference, 'x') + 1);
187                         }
188                 } else {
189                         x = atoi(preference);
190                 }
191
192                 LOG("Looking for screen at %d x %d\n", x, y);
193
194                 TAILQ_FOREACH(screen, slist, screens)
195                         if ((x == INT_MAX || screen->rect.x == x) &&
196                             (y == INT_MAX || screen->rect.y == y)) {
197                                 LOG("found %p\n", screen);
198                                 return screen;
199                         }
200
201                 LOG("none found\n");
202                 return NULL;
203         } else {
204                 int c = 0;
205                 TAILQ_FOREACH(screen, slist, screens)
206                         if (c++ == preferred_screen)
207                                 return screen;
208         }
209
210         return NULL;
211 }
212
213 /*
214  * Initializes the given workspace if it is not already initialized. The given
215  * screen is to be understood as a fallback, if the workspace itself either
216  * was not assigned to a particular screen or cannot be placed there because
217  * the screen is not attached at the moment.
218  *
219  */
220 void workspace_initialize(Workspace *ws, i3Screen *screen) {
221         if (ws->screen != NULL) {
222                 LOG("Workspace already initialized\n");
223                 return;
224         }
225
226         /* If this workspace has no preferred screen or if the screen it wants
227          * to be on is not available at the moment, we initialize it with
228          * the screen which was given */
229         if (ws->preferred_screen == NULL ||
230             (ws->screen = get_screen_from_preference(virtual_screens, ws->preferred_screen)) == NULL)
231                 ws->screen = screen;
232         else { LOG("yay, found assignment\n"); }
233
234         /* Copy the dimensions from the virtual screen */
235         memcpy(&(ws->rect), &(ws->screen->rect), sizeof(Rect));
236 }
237
238 /*
239  * Gets the first unused workspace for the given screen, taking into account
240  * the preferred_screen setting of every workspace (workspace assignments).
241  *
242  */
243 Workspace *get_first_workspace_for_screen(struct screens_head *slist, i3Screen *screen) {
244         Workspace *result = NULL;
245
246         for (int c = 0; c < 10; c++) {
247                 Workspace *ws = &(workspaces[c]);
248                 if (ws->preferred_screen == NULL ||
249                     !screens_are_equal(get_screen_from_preference(slist, ws->preferred_screen), screen))
250                         continue;
251
252                 result = ws;
253                 break;
254         }
255
256         if (result == NULL) {
257                 /* No assignment found, returning first unused workspace */
258                 for (int c = 0; c < 10; c++) {
259                         if (workspaces[c].screen != NULL)
260                                 continue;
261
262                         result = &(workspaces[c]);
263                         break;
264                 }
265         }
266
267         if (result != NULL) {
268                 workspace_initialize(result, screen);
269                 return result;
270         }
271
272         LOG("WARNING: No free workspace found to assign!\n");
273         return NULL;
274 }
275
276 /*
277  * Maps all clients (and stack windows) of the given workspace.
278  *
279  */
280 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
281         Client *client;
282
283         ignore_enter_notify_forall(conn, ws, true);
284
285         /* Map all clients on the new workspace */
286         FOR_TABLE(ws)
287                 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
288                         client_map(conn, client);
289
290         /* Map all floating clients */
291         if (!ws->floating_hidden)
292                 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
293                         client_map(conn, client);
294
295         /* Map all stack windows, if any */
296         struct Stack_Window *stack_win;
297         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
298                 if (stack_win->container->workspace == ws)
299                         xcb_map_window(conn, stack_win->window);
300
301         ignore_enter_notify_forall(conn, ws, false);
302 }
303
304 /*
305  * Unmaps all clients (and stack windows) of the given workspace.
306  *
307  * This needs to be called separately when temporarily rendering
308  * a workspace which is not the active workspace to force
309  * reconfiguration of all clients, like in src/xinerama.c when
310  * re-assigning a workspace to another screen.
311  *
312  */
313 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
314         Client *client;
315         struct Stack_Window *stack_win;
316
317         /* Ignore notify events because they would cause focus to be changed */
318         ignore_enter_notify_forall(conn, u_ws, true);
319
320         /* Unmap all clients of the given workspace */
321         int unmapped_clients = 0;
322         FOR_TABLE(u_ws)
323                 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
324                         LOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
325                         client_unmap(conn, client);
326                         unmapped_clients++;
327                 }
328
329         /* To find floating clients, we traverse the focus stack */
330         SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
331                 if (!client_is_floating(client))
332                         continue;
333
334                 LOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
335
336                 client_unmap(conn, client);
337                 unmapped_clients++;
338         }
339
340         /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
341          * if it is not the current workspace. */
342         if (unmapped_clients == 0 && u_ws != c_ws) {
343                 /* Re-assign the workspace of all dock clients which use this workspace */
344                 Client *dock;
345                 LOG("workspace %p is empty\n", u_ws);
346                 SLIST_FOREACH(dock, &(u_ws->screen->dock_clients), dock_clients) {
347                         if (dock->workspace != u_ws)
348                                 continue;
349
350                         LOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
351                         dock->workspace = c_ws;
352                 }
353                 u_ws->screen = NULL;
354         }
355
356         /* Unmap the stack windows on the given workspace, if any */
357         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
358                 if (stack_win->container->workspace == u_ws)
359                         xcb_unmap_window(conn, stack_win->window);
360
361         ignore_enter_notify_forall(conn, u_ws, false);
362 }
363
364 /*
365  * Goes through all clients on the given workspace and updates the workspace’s
366  * urgent flag accordingly.
367  *
368  */
369 void workspace_update_urgent_flag(Workspace *ws) {
370         Client *current;
371
372         SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
373                 if (!current->urgent)
374                         continue;
375
376                 ws->urgent = true;
377                 return;
378         }
379
380         ws->urgent = false;
381 }