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