]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Merge branch 'master' into next
[i3/i3] / src / workspace.c
1 #undef I3__FILE__
2 #define I3__FILE__ "workspace.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * workspace.c: Modifying workspaces, accessing them, moving containers to
10  *              workspaces.
11  *
12  */
13 #include "all.h"
14
15 /* Stores a copy of the name of the last used workspace for the workspace
16  * back-and-forth switching. */
17 static char *previous_workspace_name = NULL;
18
19 /*
20  * Sets ws->layout to splith/splitv if default_orientation was specified in the
21  * configfile. Otherwise, it uses splith/splitv depending on whether the output
22  * is higher than wide.
23  *
24  */
25 static void _workspace_apply_default_orientation(Con *ws) {
26     /* If default_orientation is set to NO_ORIENTATION we determine
27      * orientation depending on output resolution. */
28     if (config.default_orientation == NO_ORIENTATION) {
29         Con *output = con_get_output(ws);
30         ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
31         DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
32              output->rect.width, output->rect.height, ws->layout);
33     } else {
34         ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
35     }
36 }
37
38 /*
39  * Returns a pointer to the workspace with the given number (starting at 0),
40  * creating the workspace if necessary (by allocating the necessary amount of
41  * memory and initializing the data structures correctly).
42  *
43  */
44 Con *workspace_get(const char *num, bool *created) {
45     Con *output, *workspace = NULL;
46
47     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
48     GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
49
50     if (workspace == NULL) {
51         LOG("Creating new workspace \"%s\"\n", num);
52         /* unless an assignment is found, we will create this workspace on the current output */
53         output = con_get_output(focused);
54         /* look for assignments */
55         struct Workspace_Assignment *assignment;
56
57         /* We set workspace->num to the number if this workspace’s name begins
58          * with a positive number. Otherwise it’s a named ws and num will be
59          * -1. */
60         long parsed_num = ws_name_to_number(num);
61
62         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
63             if (strcmp(assignment->name, num) == 0) {
64                 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
65                 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
66                 break;
67             } else if (parsed_num != -1 && name_is_digits(assignment->name) && ws_name_to_number(assignment->name) == parsed_num) {
68                 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
69                 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
70             }
71         }
72
73         Con *content = output_get_content(output);
74         LOG("got output %p with content %p\n", output, content);
75         /* We need to attach this container after setting its type. con_attach
76          * will handle CT_WORKSPACEs differently */
77         workspace = con_new(NULL, NULL);
78         char *name;
79         sasprintf(&name, "[i3 con] workspace %s", num);
80         x_set_name(workspace, name);
81         free(name);
82         workspace->type = CT_WORKSPACE;
83         FREE(workspace->name);
84         workspace->name = sstrdup(num);
85         workspace->workspace_layout = config.default_layout;
86         workspace->num = parsed_num;
87         LOG("num = %d\n", workspace->num);
88
89         workspace->parent = content;
90         _workspace_apply_default_orientation(workspace);
91
92         con_attach(workspace, content, false);
93
94         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
95         ewmh_update_number_of_desktops();
96         ewmh_update_desktop_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     if (con->urgent) {
324         DLOG("Resetting urgency flag of con %p by timer\n", con);
325         con->urgent = false;
326         con_update_parents_urgency(con);
327         workspace_update_urgent_flag(con_get_workspace(con));
328         ipc_send_window_event("urgent", con);
329         tree_render();
330     }
331
332     ev_timer_stop(main_loop, con->urgency_timer);
333     FREE(con->urgency_timer);
334 }
335
336 static void _workspace_show(Con *workspace) {
337     Con *current, *old = NULL;
338
339     /* safe-guard against showing i3-internal workspaces like __i3_scratch */
340     if (con_is_internal(workspace))
341         return;
342
343     /* disable fullscreen for the other workspaces and get the workspace we are
344      * currently on. */
345     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
346         if (current->fullscreen_mode == CF_OUTPUT)
347             old = current;
348         current->fullscreen_mode = CF_NONE;
349     }
350
351     /* enable fullscreen for the target workspace. If it happens to be the
352      * same one we are currently on anyways, we can stop here. */
353     workspace->fullscreen_mode = CF_OUTPUT;
354     current = con_get_workspace(focused);
355     if (workspace == current) {
356         DLOG("Not switching, already there.\n");
357         return;
358     }
359
360     /* Remember currently focused workspace for switching back to it later with
361      * the 'workspace back_and_forth' command.
362      * NOTE: We have to duplicate the name as the original will be freed when
363      * the corresponding workspace is cleaned up.
364      * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
365      * focused) are skipped, see bug #868. */
366     if (current && !con_is_internal(current)) {
367         FREE(previous_workspace_name);
368         if (current) {
369             previous_workspace_name = sstrdup(current->name);
370             DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
371         }
372     }
373
374     workspace_reassign_sticky(workspace);
375
376     DLOG("switching to %p / %s\n", workspace, workspace->name);
377     Con *next = con_descend_focused(workspace);
378
379     /* Memorize current output */
380     Con *old_output = con_get_output(focused);
381
382     /* Display urgency hint for a while if the newly visible workspace would
383      * focus and thereby immediately destroy it */
384     if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
385         /* focus for now… */
386         next->urgent = false;
387         con_focus(next);
388
389         /* … but immediately reset urgency flags; they will be set to false by
390          * the timer callback in case the container is focused at the time of
391          * its expiration */
392         focused->urgent = true;
393         workspace->urgent = true;
394
395         if (focused->urgency_timer == NULL) {
396             DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
397                  focused, workspace);
398             focused->urgency_timer = scalloc(sizeof(struct ev_timer));
399             /* use a repeating timer to allow for easy resets */
400             ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
401                           config.workspace_urgency_timer, config.workspace_urgency_timer);
402             focused->urgency_timer->data = focused;
403             ev_timer_start(main_loop, focused->urgency_timer);
404         } else {
405             DLOG("Resetting urgency timer of con %p on workspace %p\n",
406                  focused, workspace);
407             ev_timer_again(main_loop, focused->urgency_timer);
408         }
409     } else
410         con_focus(next);
411
412     ipc_send_workspace_focus_event(workspace, current);
413
414     DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
415     /* Close old workspace if necessary. This must be done *after* doing
416      * urgency handling, because tree_close() will do a con_focus() on the next
417      * client, which will clear the urgency flag too early. Also, there is no
418      * way for con_focus() to know about when to clear urgency immediately and
419      * when to defer it. */
420     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
421         /* check if this workspace is currently visible */
422         if (!workspace_is_visible(old)) {
423             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
424             tree_close(old, DONT_KILL_WINDOW, false, false);
425             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
426             ewmh_update_number_of_desktops();
427             ewmh_update_desktop_names();
428             ewmh_update_desktop_viewport();
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 }