]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
bugfix: don’t remap stack windows errnously when changing workspaces
[i3/i3] / src / workspace.c
1 /*
2  * vim:ts=8:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  *
6  * © 2009-2010 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 "randr.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         FREE(ws->utf8_name);
88
89         ws->name = convert_utf8_to_ucs2(label, &(ws->name_len));
90         if (config.font != NULL)
91                 ws->text_width = predict_text_width(global_conn, config.font, ws->name, ws->name_len);
92         else ws->text_width = 0;
93         ws->utf8_name = 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->output->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->output, false);
124
125         if (c_ws->output != t_ws->output) {
126                 /* We need to switch to the other output first */
127                 DLOG("moving over to other output.\n");
128
129                 /* Store the old client */
130                 Client *old_client = CUR_CELL->currently_focused;
131
132                 c_ws = t_ws->output->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->output->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                 /* We need to check if a global fullscreen-client is blocking
149                  * the t_ws and if necessary switch that to local fullscreen */
150                 Client* client = c_ws->fullscreen_client;
151                 if (client != NULL && client->workspace != c_ws) {
152                         if (c_ws->fullscreen_client->workspace != c_ws)
153                                 c_ws->fullscreen_client = NULL;
154                         client_enter_fullscreen(conn, client, false);
155                 }
156         }
157
158         /* Check if we need to change something or if we’re already there */
159         if (c_ws->output->current_workspace->num == (workspace-1)) {
160                 Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
161                 if (last_focused != SLIST_END(&(c_ws->focus_stack)))
162                         set_focus(conn, last_focused, true);
163                 if (need_warp) {
164                         client_warp_pointer_into(conn, last_focused);
165                         xcb_flush(conn);
166                 }
167
168                 return;
169         }
170
171         Workspace *old_workspace = c_ws;
172         c_ws = t_ws->output->current_workspace = workspace_get(workspace-1);
173
174         /* Unmap all clients of the old workspace */
175         workspace_unmap_clients(conn, old_workspace);
176
177         current_row = c_ws->current_row;
178         current_col = c_ws->current_col;
179         DLOG("new current row = %d, current col = %d\n", current_row, current_col);
180
181         workspace_map_clients(conn, c_ws);
182
183         /* POTENTIAL TO IMPROVE HERE: due to the call to _map_clients first and
184          * render_layout afterwards, there is a short flickering on the source
185          * workspace (assign ws 3 to output 0, ws 4 to output 1, create single
186          * client on ws 4, move it to ws 3, switch to ws 3, you’ll see the
187          * flickering). */
188
189         /* Restore focus on the new workspace */
190         Client *last_focused = SLIST_FIRST(&(c_ws->focus_stack));
191         if (last_focused != SLIST_END(&(c_ws->focus_stack)))
192                 set_focus(conn, last_focused, true);
193         else xcb_set_input_focus(conn, XCB_INPUT_FOCUS_POINTER_ROOT, root, XCB_CURRENT_TIME);
194
195         render_layout(conn);
196
197         /* We can warp the pointer only after the window has been
198          * reconfigured in render_layout, otherwise the pointer will
199          * be warped to the old position, which will not work when we
200          * moved it to another output. */
201         if (last_focused != SLIST_END(&(c_ws->focus_stack)) && need_warp) {
202                 client_warp_pointer_into(conn, last_focused);
203                 xcb_flush(conn);
204         }
205 }
206
207 /*
208  * Assigns the given workspace to the given output by correctly updating its
209  * state and reconfiguring all the clients on this workspace.
210  *
211  * This is called when initializing a output and when re-assigning it to a
212  * different output which just got available (if you configured it to be on
213  * output 1 and you just plugged in output 1).
214  *
215  */
216 void workspace_assign_to(Workspace *ws, Output *output, bool hide_it) {
217         Client *client;
218         bool empty = true;
219         bool visible = workspace_is_visible(ws);
220
221         ws->output = output;
222
223         /* Copy the dimensions from the virtual output */
224         memcpy(&(ws->rect), &(ws->output->rect), sizeof(Rect));
225
226         ewmh_update_workarea();
227
228         /* Force reconfiguration for each client on that workspace */
229         SLIST_FOREACH(client, &(ws->focus_stack), focus_clients) {
230                 client->force_reconfigure = true;
231                 empty = false;
232         }
233
234         if (empty)
235                 return;
236
237         /* Render the workspace to reconfigure the clients. However, they will be visible now, so… */
238         render_workspace(global_conn, output, ws);
239
240         /* …unless we want to see them at the moment, we should hide that workspace */
241         if (visible && !hide_it)
242                 return;
243
244         workspace_unmap_clients(global_conn, ws);
245
246         if (c_ws == ws) {
247                 DLOG("Need to adjust output->current_workspace...\n");
248                 output->current_workspace = c_ws;
249         }
250 }
251
252 /*
253  * Initializes the given workspace if it is not already initialized. The given
254  * screen is to be understood as a fallback, if the workspace itself either
255  * was not assigned to a particular screen or cannot be placed there because
256  * the screen is not attached at the moment.
257  *
258  */
259 void workspace_initialize(Workspace *ws, Output *output, bool recheck) {
260         Output *old_output;
261
262         if (ws->output != NULL && !recheck) {
263                 DLOG("Workspace already initialized\n");
264                 return;
265         }
266
267         old_output = ws->output;
268
269         /* If this workspace has no preferred output or if the output it wants
270          * to be on is not available at the moment, we initialize it with
271          * the output which was given */
272         if (ws->preferred_output == NULL ||
273             (ws->output = get_output_by_name(ws->preferred_output)) == NULL)
274                 ws->output = output;
275
276         DLOG("old_output = %p, ws->output = %p\n", old_output, ws->output);
277         /* If the assignment did not change, we do not need to update anything */
278         if (old_output != NULL && ws->output == old_output)
279                 return;
280
281         workspace_assign_to(ws, ws->output, false);
282 }
283
284 /*
285  * Gets the first unused workspace for the given screen, taking into account
286  * the preferred_output setting of every workspace (workspace assignments).
287  *
288  */
289 Workspace *get_first_workspace_for_output(Output *output) {
290         Workspace *result = NULL;
291
292         Workspace *ws;
293         TAILQ_FOREACH(ws, workspaces, workspaces) {
294                 if (ws->preferred_output == NULL ||
295                     get_output_by_name(ws->preferred_output) != output)
296                         continue;
297
298                 result = ws;
299                 break;
300         }
301
302         if (result == NULL) {
303                 /* No assignment found, returning first unused workspace */
304                 TAILQ_FOREACH(ws, workspaces, workspaces) {
305                         if (ws->output != NULL)
306                                 continue;
307
308                         result = ws;
309                         break;
310                 }
311         }
312
313         if (result == NULL) {
314                 DLOG("No existing free workspace found to assign, creating a new one\n");
315
316                 int last_ws = 0;
317                 TAILQ_FOREACH(ws, workspaces, workspaces)
318                         last_ws = ws->num;
319                 result = workspace_get(last_ws + 1);
320         }
321
322         workspace_initialize(result, output, false);
323         return result;
324 }
325
326 /*
327  * Maps all clients (and stack windows) of the given workspace.
328  *
329  */
330 void workspace_map_clients(xcb_connection_t *conn, Workspace *ws) {
331         Client *client;
332
333         ignore_enter_notify_forall(conn, ws, true);
334
335         /* Map all clients on the new workspace */
336         FOR_TABLE(ws)
337                 CIRCLEQ_FOREACH(client, &(ws->table[cols][rows]->clients), clients)
338                         client_map(conn, client);
339
340         /* Map all floating clients */
341         if (!ws->floating_hidden)
342                 TAILQ_FOREACH(client, &(ws->floating_clients), floating_clients)
343                         client_map(conn, client);
344
345         /* Map all stack windows, if any */
346         struct Stack_Window *stack_win;
347         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
348                 if (stack_win->container->workspace == ws && stack_win->rect.height > 0)
349                         xcb_map_window(conn, stack_win->window);
350
351         ignore_enter_notify_forall(conn, ws, false);
352 }
353
354 /*
355  * Unmaps all clients (and stack windows) of the given workspace.
356  *
357  * This needs to be called separately when temporarily rendering
358  * a workspace which is not the active workspace to force
359  * reconfiguration of all clients, like in src/xinerama.c when
360  * re-assigning a workspace to another screen.
361  *
362  */
363 void workspace_unmap_clients(xcb_connection_t *conn, Workspace *u_ws) {
364         Client *client;
365         struct Stack_Window *stack_win;
366
367         /* Ignore notify events because they would cause focus to be changed */
368         ignore_enter_notify_forall(conn, u_ws, true);
369
370         /* Unmap all clients of the given workspace */
371         int unmapped_clients = 0;
372         FOR_TABLE(u_ws)
373                 CIRCLEQ_FOREACH(client, &(u_ws->table[cols][rows]->clients), clients) {
374                         DLOG("unmapping normal client %p / %p / %p\n", client, client->frame, client->child);
375                         client_unmap(conn, client);
376                         unmapped_clients++;
377                 }
378
379         /* To find floating clients, we traverse the focus stack */
380         SLIST_FOREACH(client, &(u_ws->focus_stack), focus_clients) {
381                 if (!client_is_floating(client))
382                         continue;
383
384                 DLOG("unmapping floating client %p / %p / %p\n", client, client->frame, client->child);
385
386                 client_unmap(conn, client);
387                 unmapped_clients++;
388         }
389
390         /* If we did not unmap any clients, the workspace is empty and we can destroy it, at least
391          * if it is not the current workspace. */
392         if (unmapped_clients == 0 && u_ws != c_ws) {
393                 /* Re-assign the workspace of all dock clients which use this workspace */
394                 Client *dock;
395                 DLOG("workspace %p is empty\n", u_ws);
396                 SLIST_FOREACH(dock, &(u_ws->output->dock_clients), dock_clients) {
397                         if (dock->workspace != u_ws)
398                                 continue;
399
400                         DLOG("Re-assigning dock client to c_ws (%p)\n", c_ws);
401                         dock->workspace = c_ws;
402                 }
403                 u_ws->output = NULL;
404         }
405
406         /* Unmap the stack windows on the given workspace, if any */
407         SLIST_FOREACH(stack_win, &stack_wins, stack_windows)
408                 if (stack_win->container->workspace == u_ws)
409                         xcb_unmap_window(conn, stack_win->window);
410
411         ignore_enter_notify_forall(conn, u_ws, false);
412 }
413
414 /*
415  * Goes through all clients on the given workspace and updates the workspace’s
416  * urgent flag accordingly.
417  *
418  */
419 void workspace_update_urgent_flag(Workspace *ws) {
420         Client *current;
421
422         SLIST_FOREACH(current, &(ws->focus_stack), focus_clients) {
423                 if (!current->urgent)
424                         continue;
425
426                 ws->urgent = true;
427                 return;
428         }
429
430         ws->urgent = false;
431 }
432
433 /*
434  * Returns the width of the workspace.
435  *
436  */
437 int workspace_width(Workspace *ws) {
438         return ws->rect.width;
439 }
440
441 /*
442  * Returns the effective height of the workspace (without the internal bar and
443  * without dock clients).
444  *
445  */
446 int workspace_height(Workspace *ws) {
447         int height = ws->rect.height;
448         i3Font *font = load_font(global_conn, config.font);
449
450         /* Reserve space for dock clients */
451         Client *client;
452         SLIST_FOREACH(client, &(ws->output->dock_clients), dock_clients)
453                 height -= client->desired_height;
454
455         /* Space for the internal bar */
456         height -= (font->height + 6);
457
458         return height;
459 }