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