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