]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Replace the discrete 'split' Con property with a simple function.
[i3/i3] / src / workspace.c
1 #undef I3__FILE__
2 #define I3__FILE__ "workspace.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * workspace.c: Modifying workspaces, accessing them, moving containers to
10  *              workspaces.
11  *
12  */
13 #include "all.h"
14
15 /* Stores a copy of the name of the last used workspace for the workspace
16  * back-and-forth switching. */
17 static char *previous_workspace_name = NULL;
18
19 /*
20  * Sets ws->layout to splith/splitv if default_orientation was specified in the
21  * configfile. Otherwise, it uses splith/splitv depending on whether the output
22  * is higher than wide.
23  *
24  */
25 static void _workspace_apply_default_orientation(Con *ws) {
26     /* If default_orientation is set to NO_ORIENTATION we determine
27      * orientation depending on output resolution. */
28     if (config.default_orientation == NO_ORIENTATION) {
29         Con *output = con_get_output(ws);
30         ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
31         DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
32              output->rect.width, output->rect.height, ws->layout);
33     } else {
34         ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
35     }
36 }
37
38 /*
39  * Returns a pointer to the workspace with the given number (starting at 0),
40  * creating the workspace if necessary (by allocating the necessary amount of
41  * memory and initializing the data structures correctly).
42  *
43  */
44 Con *workspace_get(const char *num, bool *created) {
45     Con *output, *workspace = NULL;
46
47     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
48         GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
49
50     if (workspace == NULL) {
51         LOG("Creating new workspace \"%s\"\n", num);
52         /* unless an assignment is found, we will create this workspace on the current output */
53         output = con_get_output(focused);
54         /* look for assignments */
55         struct Workspace_Assignment *assignment;
56         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
57             if (strcmp(assignment->name, num) != 0)
58                 continue;
59
60             LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
61             GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
62             break;
63         }
64         Con *content = output_get_content(output);
65         LOG("got output %p with content %p\n", output, content);
66         /* We need to attach this container after setting its type. con_attach
67          * will handle CT_WORKSPACEs differently */
68         workspace = con_new(NULL, NULL);
69         char *name;
70         sasprintf(&name, "[i3 con] workspace %s", num);
71         x_set_name(workspace, name);
72         free(name);
73         workspace->type = CT_WORKSPACE;
74         FREE(workspace->name);
75         workspace->name = sstrdup(num);
76         workspace->workspace_layout = config.default_layout;
77         /* We set ->num to the number if this workspace’s name begins with a
78          * positive number. Otherwise it’s a named ws and num will be -1. */
79         char *endptr = NULL;
80         long parsed_num = strtol(num, &endptr, 10);
81         if (parsed_num == LONG_MIN ||
82             parsed_num == LONG_MAX ||
83             parsed_num < 0 ||
84             endptr == num)
85             workspace->num = -1;
86         else workspace->num = parsed_num;
87         LOG("num = %d\n", workspace->num);
88
89         workspace->parent = content;
90         _workspace_apply_default_orientation(workspace);
91
92         con_attach(workspace, content, false);
93
94         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
95         if (created != NULL)
96             *created = true;
97     }
98     else if (created != NULL) {
99         *created = false;
100     }
101
102     return workspace;
103 }
104
105 /*
106  * Returns a pointer to a new workspace in the given output. The workspace
107  * is created attached to the tree hierarchy through the given content
108  * container.
109  *
110  */
111 Con *create_workspace_on_output(Output *output, Con *content) {
112     /* add a workspace to this output */
113     Con *out, *current;
114     char *name;
115     bool exists = true;
116     Con *ws = con_new(NULL, NULL);
117     ws->type = CT_WORKSPACE;
118
119     /* try the configured workspace bindings first to find a free name */
120     Binding *bind;
121     TAILQ_FOREACH(bind, bindings, bindings) {
122         DLOG("binding with command %s\n", bind->command);
123         if (strlen(bind->command) < strlen("workspace ") ||
124             strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
125             continue;
126         DLOG("relevant command = %s\n", bind->command);
127         char *target = bind->command + strlen("workspace ");
128         /* We check if this is the workspace
129          * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
130          * Beware: The workspace names "next", "prev", "next_on_output",
131          * "prev_on_output", "number", "back_and_forth" and "current" are OK,
132          * so we check before stripping the double quotes */
133         if (strncasecmp(target, "next", strlen("next")) == 0 ||
134             strncasecmp(target, "prev", strlen("prev")) == 0 ||
135             strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
136             strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
137             strncasecmp(target, "number", strlen("number")) == 0 ||
138             strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
139             strncasecmp(target, "current", strlen("current")) == 0)
140             continue;
141         if (*target == '"')
142             target++;
143         FREE(ws->name);
144         ws->name = strdup(target);
145         if (ws->name[strlen(ws->name)-1] == '"')
146             ws->name[strlen(ws->name)-1] = '\0';
147         DLOG("trying name *%s*\n", ws->name);
148
149         /* Ensure that this workspace is not assigned to a different output —
150          * otherwise we would create it, then move it over to its output, then
151          * find a new workspace, etc… */
152         bool assigned = false;
153         struct Workspace_Assignment *assignment;
154         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
155             if (strcmp(assignment->name, ws->name) != 0 ||
156                 strcmp(assignment->output, output->name) == 0)
157                 continue;
158
159             assigned = true;
160             break;
161         }
162
163         if (assigned)
164             continue;
165
166         current = NULL;
167         TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
168             GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
169
170         exists = (current != NULL);
171         if (!exists) {
172             /* Set ->num to the number of the workspace, if the name actually
173              * is a number or starts with a number */
174             char *endptr = NULL;
175             long parsed_num = strtol(ws->name, &endptr, 10);
176             if (parsed_num == LONG_MIN ||
177                 parsed_num == LONG_MAX ||
178                 parsed_num < 0 ||
179                 endptr == ws->name)
180                 ws->num = -1;
181             else ws->num = parsed_num;
182             LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
183
184             break;
185         }
186     }
187
188     if (exists) {
189         /* get the next unused workspace number */
190         DLOG("Getting next unused workspace by number\n");
191         int c = 0;
192         while (exists) {
193             c++;
194
195             FREE(ws->name);
196             sasprintf(&(ws->name), "%d", c);
197
198             current = NULL;
199             TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
200                 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
201             exists = (current != NULL);
202
203             DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
204         }
205         ws->num = c;
206     }
207     con_attach(ws, content, false);
208
209     sasprintf(&name, "[i3 con] workspace %s", ws->name);
210     x_set_name(ws, name);
211     free(name);
212
213     ws->fullscreen_mode = CF_OUTPUT;
214
215     ws->workspace_layout = config.default_layout;
216     _workspace_apply_default_orientation(ws);
217
218     return ws;
219 }
220
221
222 /*
223  * Returns true if the workspace is currently visible. Especially important for
224  * multi-monitor environments, as they can have multiple currenlty active
225  * workspaces.
226  *
227  */
228 bool workspace_is_visible(Con *ws) {
229     Con *output = con_get_output(ws);
230     if (output == NULL)
231         return false;
232     Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
233     LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
234     return (fs == ws);
235 }
236
237 /*
238  * XXX: we need to clean up all this recursive walking code.
239  *
240  */
241 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
242     Con *current;
243
244     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
245         if (current != exclude &&
246             current->sticky_group != NULL &&
247             current->window != NULL &&
248             strcmp(current->sticky_group, sticky_group) == 0)
249             return current;
250
251         Con *recurse = _get_sticky(current, sticky_group, exclude);
252         if (recurse != NULL)
253             return recurse;
254     }
255
256     TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
257         if (current != exclude &&
258             current->sticky_group != NULL &&
259             current->window != NULL &&
260             strcmp(current->sticky_group, sticky_group) == 0)
261             return current;
262
263         Con *recurse = _get_sticky(current, sticky_group, exclude);
264         if (recurse != NULL)
265             return recurse;
266     }
267
268     return NULL;
269 }
270
271 /*
272  * Reassigns all child windows in sticky containers. Called when the user
273  * changes workspaces.
274  *
275  * XXX: what about sticky containers which contain containers?
276  *
277  */
278 static void workspace_reassign_sticky(Con *con) {
279     Con *current;
280     /* 1: go through all containers */
281
282     /* handle all children and floating windows of this node */
283     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
284         if (current->sticky_group == NULL) {
285             workspace_reassign_sticky(current);
286             continue;
287         }
288
289         LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
290         /* 2: find a window which we can re-assign */
291         Con *output = con_get_output(current);
292         Con *src = _get_sticky(output, current->sticky_group, current);
293
294         if (src == NULL) {
295             LOG("No window found for this sticky group\n");
296             workspace_reassign_sticky(current);
297             continue;
298         }
299
300         x_move_win(src, current);
301         current->window = src->window;
302         current->mapped = true;
303         src->window = NULL;
304         src->mapped = false;
305
306         x_reparent_child(current, src);
307
308         LOG("re-assigned window from src %p to dest %p\n", src, current);
309     }
310
311     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
312         workspace_reassign_sticky(current);
313 }
314
315 /*
316  * Callback to reset the urgent flag of the given con to false. May be started by
317  * _workspace_show to avoid urgency hints being lost by switching to a workspace
318  * focusing the con.
319  *
320  */
321 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
322     Con *con = w->data;
323
324     DLOG("Resetting urgency flag of con %p by timer\n", con);
325     con->urgent = false;
326     con_update_parents_urgency(con);
327     workspace_update_urgent_flag(con_get_workspace(con));
328     tree_render();
329
330     ev_timer_stop(main_loop, con->urgency_timer);
331     FREE(con->urgency_timer);
332 }
333
334 static void _workspace_show(Con *workspace) {
335     Con *current, *old = NULL;
336
337     /* safe-guard against showing i3-internal workspaces like __i3_scratch */
338     if (con_is_internal(workspace))
339         return;
340
341     /* disable fullscreen for the other workspaces and get the workspace we are
342      * currently on. */
343     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
344         if (current->fullscreen_mode == CF_OUTPUT)
345             old = current;
346         current->fullscreen_mode = CF_NONE;
347     }
348
349     /* enable fullscreen for the target workspace. If it happens to be the
350      * same one we are currently on anyways, we can stop here. */
351     workspace->fullscreen_mode = CF_OUTPUT;
352     current = con_get_workspace(focused);
353     if (workspace == current) {
354         DLOG("Not switching, already there.\n");
355         return;
356     }
357
358     /* Remember currently focused workspace for switching back to it later with
359      * the 'workspace back_and_forth' command.
360      * NOTE: We have to duplicate the name as the original will be freed when
361      * the corresponding workspace is cleaned up. */
362
363     FREE(previous_workspace_name);
364     if (current)
365         previous_workspace_name = sstrdup(current->name);
366
367     workspace_reassign_sticky(workspace);
368
369     DLOG("switching to %p / %s\n", workspace, workspace->name);
370     Con *next = con_descend_focused(workspace);
371
372     /* Memorize current output */
373     Con *old_output = con_get_output(focused);
374
375     /* Display urgency hint for a while if the newly visible workspace would
376      * focus and thereby immediately destroy it */
377     if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
378         /* focus for now… */
379         con_focus(next);
380
381         /* … but immediately reset urgency flags; they will be set to false by
382          * the timer callback in case the container is focused at the time of
383          * its expiration */
384         focused->urgent = true;
385         workspace->urgent = true;
386
387         if (focused->urgency_timer == NULL) {
388             DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
389                     focused, workspace);
390             focused->urgency_timer = scalloc(sizeof(struct ev_timer));
391             /* use a repeating timer to allow for easy resets */
392             ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
393                     config.workspace_urgency_timer, config.workspace_urgency_timer);
394             focused->urgency_timer->data = focused;
395             ev_timer_start(main_loop, focused->urgency_timer);
396         } else {
397             DLOG("Resetting urgency timer of con %p on workspace %p\n",
398                     focused, workspace);
399             ev_timer_again(main_loop, focused->urgency_timer);
400         }
401     } else
402         con_focus(next);
403
404     DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
405     /* Close old workspace if necessary. This must be done *after* doing
406      * urgency handling, because tree_close() will do a con_focus() on the next
407      * client, which will clear the urgency flag too early. Also, there is no
408      * way for con_focus() to know about when to clear urgency immediately and
409      * when to defer it. */
410     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
411         /* check if this workspace is currently visible */
412         if (!workspace_is_visible(old)) {
413             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
414             tree_close(old, DONT_KILL_WINDOW, false, false);
415             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
416         }
417     }
418
419     workspace->fullscreen_mode = CF_OUTPUT;
420     LOG("focused now = %p / %s\n", focused, focused->name);
421
422     /* Set mouse pointer */
423     Con *new_output = con_get_output(focused);
424     if (old_output != new_output) {
425         x_set_warp_to(&next->rect);
426     }
427
428     /* Update the EWMH hints */
429     ewmh_update_current_desktop();
430
431     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
432 }
433
434 /*
435  * Switches to the given workspace
436  *
437  */
438 void workspace_show(Con *workspace) {
439     _workspace_show(workspace);
440 }
441
442 /*
443  * Looks up the workspace by name and switches to it.
444  *
445  */
446 void workspace_show_by_name(const char *num) {
447     Con *workspace;
448     workspace = workspace_get(num, NULL);
449     _workspace_show(workspace);
450 }
451
452 /*
453  * Focuses the next workspace.
454  *
455  */
456 Con* workspace_next(void) {
457     Con *current = con_get_workspace(focused);
458     Con *next = NULL;
459     Con *output;
460
461     if (current->num == -1) {
462         /* If currently a named workspace, find next named workspace. */
463         next = TAILQ_NEXT(current, nodes);
464     } else {
465         /* If currently a numbered workspace, find next numbered workspace. */
466         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
467             /* Skip outputs starting with __, they are internal. */
468             if (con_is_internal(output))
469                 continue;
470             NODES_FOREACH(output_get_content(output)) {
471                 if (child->type != CT_WORKSPACE)
472                     continue;
473                 if (child->num == -1)
474                     break;
475                 /* Need to check child against current and next because we are
476                  * traversing multiple lists and thus are not guaranteed the
477                  * relative order between the list of workspaces. */
478                 if (current->num < child->num && (!next || child->num < next->num))
479                     next = child;
480             }
481         }
482     }
483
484     /* Find next named workspace. */
485     if (!next) {
486         bool found_current = false;
487         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
488             /* Skip outputs starting with __, they are internal. */
489             if (con_is_internal(output))
490                 continue;
491             NODES_FOREACH(output_get_content(output)) {
492                 if (child->type != CT_WORKSPACE)
493                     continue;
494                 if (child == current) {
495                     found_current = 1;
496                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
497                     next = child;
498                     goto workspace_next_end;
499                 }
500             }
501         }
502     }
503
504     /* Find first workspace. */
505     if (!next) {
506         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
507             /* Skip outputs starting with __, they are internal. */
508             if (con_is_internal(output))
509                 continue;
510             NODES_FOREACH(output_get_content(output)) {
511                 if (child->type != CT_WORKSPACE)
512                     continue;
513                 if (!next || (child->num != -1 && child->num < next->num))
514                     next = child;
515             }
516         }
517     }
518 workspace_next_end:
519     return next;
520 }
521
522 /*
523  * Focuses the previous workspace.
524  *
525  */
526 Con* workspace_prev(void) {
527     Con *current = con_get_workspace(focused);
528     Con *prev = NULL;
529     Con *output;
530
531     if (current->num == -1) {
532         /* If named workspace, find previous named workspace. */
533         prev = TAILQ_PREV(current, nodes_head, nodes);
534         if (prev && prev->num != -1)
535             prev = NULL;
536     } else {
537         /* If numbered workspace, find previous numbered workspace. */
538         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
539             /* Skip outputs starting with __, they are internal. */
540             if (con_is_internal(output))
541                 continue;
542             NODES_FOREACH_REVERSE(output_get_content(output)) {
543                 if (child->type != CT_WORKSPACE || child->num == -1)
544                     continue;
545                 /* Need to check child against current and previous because we
546                  * are traversing multiple lists and thus are not guaranteed
547                  * the relative order between the list of workspaces. */
548                 if (current->num > child->num && (!prev || child->num > prev->num))
549                     prev = child;
550             }
551         }
552     }
553
554     /* Find previous named workspace. */
555     if (!prev) {
556         bool found_current = false;
557         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
558             /* Skip outputs starting with __, they are internal. */
559             if (con_is_internal(output))
560                 continue;
561             NODES_FOREACH_REVERSE(output_get_content(output)) {
562                 if (child->type != CT_WORKSPACE)
563                     continue;
564                 if (child == current) {
565                     found_current = true;
566                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
567                     prev = child;
568                     goto workspace_prev_end;
569                 }
570             }
571         }
572     }
573
574     /* Find last workspace. */
575     if (!prev) {
576         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
577             /* Skip outputs starting with __, they are internal. */
578             if (con_is_internal(output))
579                 continue;
580             NODES_FOREACH_REVERSE(output_get_content(output)) {
581                 if (child->type != CT_WORKSPACE)
582                     continue;
583                 if (!prev || child->num > prev->num)
584                     prev = child;
585             }
586         }
587     }
588
589 workspace_prev_end:
590     return prev;
591 }
592
593
594 /*
595  * Focuses the next workspace on the same output.
596  *
597  */
598 Con* workspace_next_on_output(void) {
599     Con *current = con_get_workspace(focused);
600     Con *next = NULL;
601     Con *output  = con_get_output(focused);
602
603     if (current->num == -1) {
604         /* If currently a named workspace, find next named workspace. */
605         next = TAILQ_NEXT(current, nodes);
606     } else {
607         /* If currently a numbered workspace, find next numbered workspace. */
608         NODES_FOREACH(output_get_content(output)) {
609             if (child->type != CT_WORKSPACE)
610                 continue;
611             if (child->num == -1)
612                 break;
613             /* Need to check child against current and next because we are
614              * traversing multiple lists and thus are not guaranteed the
615              * relative order between the list of workspaces. */
616             if (current->num < child->num && (!next || child->num < next->num))
617                 next = child;
618             }
619         }
620
621     /* Find next named workspace. */
622     if (!next) {
623         bool found_current = false;
624         NODES_FOREACH(output_get_content(output)) {
625             if (child->type != CT_WORKSPACE)
626                 continue;
627             if (child == current) {
628                 found_current = 1;
629             } else if (child->num == -1 && (current->num != -1 || found_current)) {
630                 next = child;
631                 goto workspace_next_on_output_end;
632             }
633         }
634     }
635
636     /* Find first workspace. */
637     if (!next) {
638         NODES_FOREACH(output_get_content(output)) {
639             if (child->type != CT_WORKSPACE)
640                 continue;
641             if (!next || (child->num != -1 && child->num < next->num))
642                 next = child;
643         }
644     }
645 workspace_next_on_output_end:
646     return next;
647 }
648
649 /*
650  * Focuses the previous workspace on same output.
651  *
652  */
653 Con* workspace_prev_on_output(void) {
654     Con *current = con_get_workspace(focused);
655     Con *prev = NULL;
656     Con *output  = con_get_output(focused);
657     DLOG("output = %s\n", output->name);
658
659     if (current->num == -1) {
660         /* If named workspace, find previous named workspace. */
661         prev = TAILQ_PREV(current, nodes_head, nodes);
662         if (prev && prev->num != -1)
663             prev = NULL;
664     } else {
665         /* If numbered workspace, find previous numbered workspace. */
666         NODES_FOREACH_REVERSE(output_get_content(output)) {
667             if (child->type != CT_WORKSPACE || child->num == -1)
668                 continue;
669              /* Need to check child against current and previous because we
670              * are traversing multiple lists and thus are not guaranteed
671              * the relative order between the list of workspaces. */
672             if (current->num > child->num && (!prev || child->num > prev->num))
673                 prev = child;
674         }
675     }
676
677     /* Find previous named workspace. */
678     if (!prev) {
679         bool found_current = false;
680         NODES_FOREACH_REVERSE(output_get_content(output)) {
681             if (child->type != CT_WORKSPACE)
682                 continue;
683             if (child == current) {
684                 found_current = true;
685             } else if (child->num == -1 && (current->num != -1 || found_current)) {
686                 prev = child;
687                 goto workspace_prev_on_output_end;
688             }
689         }
690     }
691
692     /* Find last workspace. */
693     if (!prev) {
694         NODES_FOREACH_REVERSE(output_get_content(output)) {
695             if (child->type != CT_WORKSPACE)
696                 continue;
697             if (!prev || child->num > prev->num)
698                 prev = child;
699         }
700     }
701
702 workspace_prev_on_output_end:
703     return prev;
704 }
705
706 /*
707  * Focuses the previously focused workspace.
708  *
709  */
710 void workspace_back_and_forth(void) {
711     if (!previous_workspace_name) {
712         DLOG("No previous workspace name set. Not switching.");
713         return;
714     }
715
716     workspace_show_by_name(previous_workspace_name);
717 }
718
719 /*
720  * Returns the previously focused workspace con, or NULL if unavailable.
721  *
722  */
723 Con *workspace_back_and_forth_get(void) {
724     if (!previous_workspace_name) {
725         DLOG("no previous workspace name set.");
726         return NULL;
727     }
728
729     Con *workspace;
730     workspace = workspace_get(previous_workspace_name, NULL);
731
732     return workspace;
733 }
734
735 static bool get_urgency_flag(Con *con) {
736     Con *child;
737     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
738         if (child->urgent || get_urgency_flag(child))
739             return true;
740
741     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
742         if (child->urgent || get_urgency_flag(child))
743             return true;
744
745     return false;
746 }
747
748 /*
749  * Goes through all clients on the given workspace and updates the workspace’s
750  * urgent flag accordingly.
751  *
752  */
753 void workspace_update_urgent_flag(Con *ws) {
754     bool old_flag = ws->urgent;
755     ws->urgent = get_urgency_flag(ws);
756     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
757
758     if (old_flag != ws->urgent)
759         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
760 }
761
762 /*
763  * 'Forces' workspace orientation by moving all cons into a new split-con with
764  * the same layout as the workspace and then changing the workspace layout.
765  *
766  */
767 void ws_force_orientation(Con *ws, orientation_t orientation) {
768     /* 1: create a new split container */
769     Con *split = con_new(NULL, NULL);
770     split->parent = ws;
771
772     /* 2: copy layout from workspace */
773     split->layout = ws->layout;
774
775     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
776
777     /* 3: move the existing cons of this workspace below the new con */
778     DLOG("Moving cons\n");
779     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
780         Con *child = TAILQ_FIRST(&(ws->nodes_head));
781         con_detach(child);
782         con_attach(child, split, true);
783     }
784
785     /* 4: switch workspace layout */
786     ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
787     DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
788
789     /* 5: attach the new split container to the workspace */
790     DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
791     con_attach(split, ws, false);
792
793     /* 6: fix the percentages */
794     con_fix_percent(ws);
795
796     if (old_focused)
797         con_focus(old_focused);
798 }
799
800 /*
801  * Called when a new con (with a window, not an empty or split con) should be
802  * attached to the workspace (for example when managing a new window or when
803  * moving an existing window to the workspace level).
804  *
805  * Depending on the workspace_layout setting, this function either returns the
806  * workspace itself (default layout) or creates a new stacked/tabbed con and
807  * returns that.
808  *
809  */
810 Con *workspace_attach_to(Con *ws) {
811     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
812
813     if (ws->workspace_layout == L_DEFAULT) {
814         DLOG("Default layout, just attaching it to the workspace itself.\n");
815         return ws;
816     }
817
818     DLOG("Non-default layout, creating a new split container\n");
819     /* 1: create a new split container */
820     Con *new = con_new(NULL, NULL);
821     new->parent = ws;
822
823     /* 2: set the requested layout on the split con */
824     new->layout = ws->workspace_layout;
825
826     /* 4: attach the new split container to the workspace */
827     DLOG("Attaching new split %p to workspace %p\n", new, ws);
828     con_attach(new, ws, false);
829
830     return new;
831 }
832
833 /**
834  * Creates a new container and re-parents all of children from the given
835  * workspace into it.
836  *
837  * The container inherits the layout from the workspace.
838  */
839 Con *workspace_encapsulate(Con *ws) {
840     if (TAILQ_EMPTY(&(ws->nodes_head))) {
841         ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
842         return NULL;
843     }
844
845     Con *new = con_new(NULL, NULL);
846     new->parent = ws;
847     new->layout = ws->layout;
848
849     DLOG("Moving children of workspace %p / %s into container %p\n",
850         ws, ws->name, new);
851
852     Con *child;
853     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
854         child = TAILQ_FIRST(&(ws->nodes_head));
855         con_detach(child);
856         con_attach(child, new, true);
857     }
858
859     con_attach(new, ws, true);
860
861     return new;
862 }