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