]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Bugfix: with one ws per output, don’t crash on cross-output moves (Thanks moju)
[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         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
57             if (strcmp(assignment->name, num) != 0)
58                 continue;
59
60             LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
61             GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
62             break;
63         }
64         Con *content = output_get_content(output);
65         LOG("got output %p with content %p\n", output, content);
66         /* We need to attach this container after setting its type. con_attach
67          * will handle CT_WORKSPACEs differently */
68         workspace = con_new(NULL, NULL);
69         char *name;
70         sasprintf(&name, "[i3 con] workspace %s", num);
71         x_set_name(workspace, name);
72         free(name);
73         workspace->type = CT_WORKSPACE;
74         FREE(workspace->name);
75         workspace->name = sstrdup(num);
76         workspace->workspace_layout = config.default_layout;
77         /* We set ->num to the number if this workspace’s name begins with a
78          * positive number. Otherwise it’s a named ws and num will be -1. */
79         char *endptr = NULL;
80         long parsed_num = strtol(num, &endptr, 10);
81         if (parsed_num == LONG_MIN ||
82             parsed_num == LONG_MAX ||
83             parsed_num < 0 ||
84             endptr == num)
85             workspace->num = -1;
86         else 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         if (created != NULL)
96             *created = true;
97     }
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         /* We check if this is the workspace
129          * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
130          * Beware: The workspace names "next", "prev", "next_on_output",
131          * "prev_on_output", "number", "back_and_forth" and "current" are OK,
132          * so we check before stripping the double quotes */
133         if (strncasecmp(target, "next", strlen("next")) == 0 ||
134             strncasecmp(target, "prev", strlen("prev")) == 0 ||
135             strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
136             strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
137             strncasecmp(target, "number", strlen("number")) == 0 ||
138             strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
139             strncasecmp(target, "current", strlen("current")) == 0)
140             continue;
141         if (*target == '"')
142             target++;
143         FREE(ws->name);
144         ws->name = strdup(target);
145         if (ws->name[strlen(ws->name)-1] == '"')
146             ws->name[strlen(ws->name)-1] = '\0';
147         DLOG("trying name *%s*\n", ws->name);
148
149         /* Ensure that this workspace is not assigned to a different output —
150          * otherwise we would create it, then move it over to its output, then
151          * find a new workspace, etc… */
152         bool assigned = false;
153         struct Workspace_Assignment *assignment;
154         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
155             if (strcmp(assignment->name, ws->name) != 0 ||
156                 strcmp(assignment->output, output->name) == 0)
157                 continue;
158
159             assigned = true;
160             break;
161         }
162
163         if (assigned)
164             continue;
165
166         current = NULL;
167         TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
168             GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
169
170         exists = (current != NULL);
171         if (!exists) {
172             /* Set ->num to the number of the workspace, if the name actually
173              * is a number or starts with a number */
174             char *endptr = NULL;
175             long parsed_num = strtol(ws->name, &endptr, 10);
176             if (parsed_num == LONG_MIN ||
177                 parsed_num == LONG_MAX ||
178                 parsed_num < 0 ||
179                 endptr == ws->name)
180                 ws->num = -1;
181             else ws->num = parsed_num;
182             LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
183
184             break;
185         }
186     }
187
188     if (exists) {
189         /* get the next unused workspace number */
190         DLOG("Getting next unused workspace by number\n");
191         int c = 0;
192         while (exists) {
193             c++;
194
195             FREE(ws->name);
196             sasprintf(&(ws->name), "%d", c);
197
198             current = NULL;
199             TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
200                 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
201             exists = (current != NULL);
202
203             DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
204         }
205         ws->num = 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     _workspace_apply_default_orientation(ws);
216
217     return ws;
218 }
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
362     FREE(previous_workspace_name);
363     if (current)
364         previous_workspace_name = sstrdup(current->name);
365
366     workspace_reassign_sticky(workspace);
367
368     DLOG("switching to %p / %s\n", workspace, workspace->name);
369     Con *next = con_descend_focused(workspace);
370
371     /* Memorize current output */
372     Con *old_output = con_get_output(focused);
373
374     /* Display urgency hint for a while if the newly visible workspace would
375      * focus and thereby immediately destroy it */
376     if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
377         /* focus for now… */
378         con_focus(next);
379
380         /* … but immediately reset urgency flags; they will be set to false by
381          * the timer callback in case the container is focused at the time of
382          * its expiration */
383         focused->urgent = true;
384         workspace->urgent = true;
385
386         if (focused->urgency_timer == NULL) {
387             DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
388                     focused, workspace);
389             focused->urgency_timer = scalloc(sizeof(struct ev_timer));
390             /* use a repeating timer to allow for easy resets */
391             ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
392                     config.workspace_urgency_timer, config.workspace_urgency_timer);
393             focused->urgency_timer->data = focused;
394             ev_timer_start(main_loop, focused->urgency_timer);
395         } else {
396             DLOG("Resetting urgency timer of con %p on workspace %p\n",
397                     focused, workspace);
398             ev_timer_again(main_loop, focused->urgency_timer);
399         }
400     } else
401         con_focus(next);
402
403     DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
404     /* Close old workspace if necessary. This must be done *after* doing
405      * urgency handling, because tree_close() will do a con_focus() on the next
406      * client, which will clear the urgency flag too early. Also, there is no
407      * way for con_focus() to know about when to clear urgency immediately and
408      * when to defer it. */
409     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
410         /* check if this workspace is currently visible */
411         if (!workspace_is_visible(old)) {
412             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
413             tree_close(old, DONT_KILL_WINDOW, false, false);
414             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
415         }
416     }
417
418     workspace->fullscreen_mode = CF_OUTPUT;
419     LOG("focused now = %p / %s\n", focused, focused->name);
420
421     /* Set mouse pointer */
422     Con *new_output = con_get_output(focused);
423     if (old_output != new_output) {
424         x_set_warp_to(&next->rect);
425     }
426
427     /* Update the EWMH hints */
428     ewmh_update_current_desktop();
429
430     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
431 }
432
433 /*
434  * Switches to the given workspace
435  *
436  */
437 void workspace_show(Con *workspace) {
438     _workspace_show(workspace);
439 }
440
441 /*
442  * Looks up the workspace by name and switches to it.
443  *
444  */
445 void workspace_show_by_name(const char *num) {
446     Con *workspace;
447     workspace = workspace_get(num, NULL);
448     _workspace_show(workspace);
449 }
450
451 /*
452  * Focuses the next workspace.
453  *
454  */
455 Con* workspace_next(void) {
456     Con *current = con_get_workspace(focused);
457     Con *next = NULL;
458     Con *output;
459
460     if (current->num == -1) {
461         /* If currently a named workspace, find next named workspace. */
462         next = TAILQ_NEXT(current, nodes);
463     } else {
464         /* If currently a numbered workspace, find next numbered workspace. */
465         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
466             /* Skip outputs starting with __, they are internal. */
467             if (con_is_internal(output))
468                 continue;
469             NODES_FOREACH(output_get_content(output)) {
470                 if (child->type != CT_WORKSPACE)
471                     continue;
472                 if (child->num == -1)
473                     break;
474                 /* Need to check child against current and next because we are
475                  * traversing multiple lists and thus are not guaranteed the
476                  * relative order between the list of workspaces. */
477                 if (current->num < child->num && (!next || child->num < next->num))
478                     next = child;
479             }
480         }
481     }
482
483     /* Find next named workspace. */
484     if (!next) {
485         bool found_current = false;
486         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
487             /* Skip outputs starting with __, they are internal. */
488             if (con_is_internal(output))
489                 continue;
490             NODES_FOREACH(output_get_content(output)) {
491                 if (child->type != CT_WORKSPACE)
492                     continue;
493                 if (child == current) {
494                     found_current = 1;
495                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
496                     next = child;
497                     goto workspace_next_end;
498                 }
499             }
500         }
501     }
502
503     /* Find first workspace. */
504     if (!next) {
505         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
506             /* Skip outputs starting with __, they are internal. */
507             if (con_is_internal(output))
508                 continue;
509             NODES_FOREACH(output_get_content(output)) {
510                 if (child->type != CT_WORKSPACE)
511                     continue;
512                 if (!next || (child->num != -1 && child->num < next->num))
513                     next = child;
514             }
515         }
516     }
517 workspace_next_end:
518     return next;
519 }
520
521 /*
522  * Focuses the previous workspace.
523  *
524  */
525 Con* workspace_prev(void) {
526     Con *current = con_get_workspace(focused);
527     Con *prev = NULL;
528     Con *output;
529
530     if (current->num == -1) {
531         /* If named workspace, find previous named workspace. */
532         prev = TAILQ_PREV(current, nodes_head, nodes);
533         if (prev && prev->num != -1)
534             prev = NULL;
535     } else {
536         /* If numbered workspace, find previous numbered workspace. */
537         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
538             /* Skip outputs starting with __, they are internal. */
539             if (con_is_internal(output))
540                 continue;
541             NODES_FOREACH_REVERSE(output_get_content(output)) {
542                 if (child->type != CT_WORKSPACE || child->num == -1)
543                     continue;
544                 /* Need to check child against current and previous because we
545                  * are traversing multiple lists and thus are not guaranteed
546                  * the relative order between the list of workspaces. */
547                 if (current->num > child->num && (!prev || child->num > prev->num))
548                     prev = child;
549             }
550         }
551     }
552
553     /* Find previous named workspace. */
554     if (!prev) {
555         bool found_current = false;
556         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
557             /* Skip outputs starting with __, they are internal. */
558             if (con_is_internal(output))
559                 continue;
560             NODES_FOREACH_REVERSE(output_get_content(output)) {
561                 if (child->type != CT_WORKSPACE)
562                     continue;
563                 if (child == current) {
564                     found_current = true;
565                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
566                     prev = child;
567                     goto workspace_prev_end;
568                 }
569             }
570         }
571     }
572
573     /* Find last workspace. */
574     if (!prev) {
575         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
576             /* Skip outputs starting with __, they are internal. */
577             if (con_is_internal(output))
578                 continue;
579             NODES_FOREACH_REVERSE(output_get_content(output)) {
580                 if (child->type != CT_WORKSPACE)
581                     continue;
582                 if (!prev || child->num > prev->num)
583                     prev = child;
584             }
585         }
586     }
587
588 workspace_prev_end:
589     return prev;
590 }
591
592
593 /*
594  * Focuses the next workspace on the same output.
595  *
596  */
597 Con* workspace_next_on_output(void) {
598     Con *current = con_get_workspace(focused);
599     Con *next = NULL;
600     Con *output  = con_get_output(focused);
601
602     if (current->num == -1) {
603         /* If currently a named workspace, find next named workspace. */
604         next = TAILQ_NEXT(current, nodes);
605     } else {
606         /* If currently a numbered workspace, find next numbered workspace. */
607         NODES_FOREACH(output_get_content(output)) {
608             if (child->type != CT_WORKSPACE)
609                 continue;
610             if (child->num == -1)
611                 break;
612             /* Need to check child against current and next because we are
613              * traversing multiple lists and thus are not guaranteed the
614              * relative order between the list of workspaces. */
615             if (current->num < child->num && (!next || child->num < next->num))
616                 next = child;
617             }
618         }
619
620     /* Find next named workspace. */
621     if (!next) {
622         bool found_current = false;
623         NODES_FOREACH(output_get_content(output)) {
624             if (child->type != CT_WORKSPACE)
625                 continue;
626             if (child == current) {
627                 found_current = 1;
628             } else if (child->num == -1 && (current->num != -1 || found_current)) {
629                 next = child;
630                 goto workspace_next_on_output_end;
631             }
632         }
633     }
634
635     /* Find first workspace. */
636     if (!next) {
637         NODES_FOREACH(output_get_content(output)) {
638             if (child->type != CT_WORKSPACE)
639                 continue;
640             if (!next || (child->num != -1 && child->num < next->num))
641                 next = child;
642         }
643     }
644 workspace_next_on_output_end:
645     return next;
646 }
647
648 /*
649  * Focuses the previous workspace on same output.
650  *
651  */
652 Con* workspace_prev_on_output(void) {
653     Con *current = con_get_workspace(focused);
654     Con *prev = NULL;
655     Con *output  = con_get_output(focused);
656     DLOG("output = %s\n", output->name);
657
658     if (current->num == -1) {
659         /* If named workspace, find previous named workspace. */
660         prev = TAILQ_PREV(current, nodes_head, nodes);
661         if (prev && prev->num != -1)
662             prev = NULL;
663     } else {
664         /* If numbered workspace, find previous numbered workspace. */
665         NODES_FOREACH_REVERSE(output_get_content(output)) {
666             if (child->type != CT_WORKSPACE || child->num == -1)
667                 continue;
668              /* Need to check child against current and previous because we
669              * are traversing multiple lists and thus are not guaranteed
670              * the relative order between the list of workspaces. */
671             if (current->num > child->num && (!prev || child->num > prev->num))
672                 prev = child;
673         }
674     }
675
676     /* Find previous named workspace. */
677     if (!prev) {
678         bool found_current = false;
679         NODES_FOREACH_REVERSE(output_get_content(output)) {
680             if (child->type != CT_WORKSPACE)
681                 continue;
682             if (child == current) {
683                 found_current = true;
684             } else if (child->num == -1 && (current->num != -1 || found_current)) {
685                 prev = child;
686                 goto workspace_prev_on_output_end;
687             }
688         }
689     }
690
691     /* Find last workspace. */
692     if (!prev) {
693         NODES_FOREACH_REVERSE(output_get_content(output)) {
694             if (child->type != CT_WORKSPACE)
695                 continue;
696             if (!prev || child->num > prev->num)
697                 prev = child;
698         }
699     }
700
701 workspace_prev_on_output_end:
702     return prev;
703 }
704
705 /*
706  * Focuses the previously focused workspace.
707  *
708  */
709 void workspace_back_and_forth(void) {
710     if (!previous_workspace_name) {
711         DLOG("No previous workspace name set. Not switching.");
712         return;
713     }
714
715     workspace_show_by_name(previous_workspace_name);
716 }
717
718 /*
719  * Returns the previously focused workspace con, or NULL if unavailable.
720  *
721  */
722 Con *workspace_back_and_forth_get(void) {
723     if (!previous_workspace_name) {
724         DLOG("no previous workspace name set.");
725         return NULL;
726     }
727
728     Con *workspace;
729     workspace = workspace_get(previous_workspace_name, NULL);
730
731     return workspace;
732 }
733
734 static bool get_urgency_flag(Con *con) {
735     Con *child;
736     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
737         if (child->urgent || get_urgency_flag(child))
738             return true;
739
740     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
741         if (child->urgent || get_urgency_flag(child))
742             return true;
743
744     return false;
745 }
746
747 /*
748  * Goes through all clients on the given workspace and updates the workspace’s
749  * urgent flag accordingly.
750  *
751  */
752 void workspace_update_urgent_flag(Con *ws) {
753     bool old_flag = ws->urgent;
754     ws->urgent = get_urgency_flag(ws);
755     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
756
757     if (old_flag != ws->urgent)
758         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
759 }
760
761 /*
762  * 'Forces' workspace orientation by moving all cons into a new split-con with
763  * the same layout as the workspace and then changing the workspace layout.
764  *
765  */
766 void ws_force_orientation(Con *ws, orientation_t orientation) {
767     /* 1: create a new split container */
768     Con *split = con_new(NULL, NULL);
769     split->parent = ws;
770     split->split = true;
771
772     /* 2: copy layout from workspace */
773     split->layout = ws->layout;
774
775     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
776
777     /* 3: move the existing cons of this workspace below the new con */
778     DLOG("Moving cons\n");
779     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
780         Con *child = TAILQ_FIRST(&(ws->nodes_head));
781         con_detach(child);
782         con_attach(child, split, true);
783     }
784
785     /* 4: switch workspace layout */
786     ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
787     DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
788
789     /* 5: attach the new split container to the workspace */
790     DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
791     con_attach(split, ws, false);
792
793     /* 6: fix the percentages */
794     con_fix_percent(ws);
795
796     if (old_focused)
797         con_focus(old_focused);
798 }
799
800 /*
801  * Called when a new con (with a window, not an empty or split con) should be
802  * attached to the workspace (for example when managing a new window or when
803  * moving an existing window to the workspace level).
804  *
805  * Depending on the workspace_layout setting, this function either returns the
806  * workspace itself (default layout) or creates a new stacked/tabbed con and
807  * returns that.
808  *
809  */
810 Con *workspace_attach_to(Con *ws) {
811     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
812
813     if (ws->workspace_layout == L_DEFAULT) {
814         DLOG("Default layout, just attaching it to the workspace itself.\n");
815         return ws;
816     }
817
818     DLOG("Non-default layout, creating a new split container\n");
819     /* 1: create a new split container */
820     Con *new = con_new(NULL, NULL);
821     new->parent = ws;
822     new->split = true;
823
824     /* 2: set the requested layout on the split con */
825     new->layout = ws->workspace_layout;
826
827     /* 4: attach the new split container to the workspace */
828     DLOG("Attaching new split %p to workspace %p\n", new, ws);
829     con_attach(new, ws, false);
830
831     return new;
832 }