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