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