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