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