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