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