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