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