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