]> 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         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     workspace_update_urgent_flag(con_get_workspace(con));
326     tree_render();
327
328     ev_timer_stop(main_loop, con->urgency_timer);
329     FREE(con->urgency_timer);
330 }
331
332 static void _workspace_show(Con *workspace) {
333     Con *current, *old = NULL;
334
335     /* safe-guard against showing i3-internal workspaces like __i3_scratch */
336     if (con_is_internal(workspace))
337         return;
338
339     /* disable fullscreen for the other workspaces and get the workspace we are
340      * currently on. */
341     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
342         if (current->fullscreen_mode == CF_OUTPUT)
343             old = current;
344         current->fullscreen_mode = CF_NONE;
345     }
346
347     /* enable fullscreen for the target workspace. If it happens to be the
348      * same one we are currently on anyways, we can stop here. */
349     workspace->fullscreen_mode = CF_OUTPUT;
350     current = con_get_workspace(focused);
351     if (workspace == current) {
352         DLOG("Not switching, already there.\n");
353         return;
354     }
355
356     /* Remember currently focused workspace for switching back to it later with
357      * the 'workspace back_and_forth' command.
358      * NOTE: We have to duplicate the name as the original will be freed when
359      * the corresponding workspace is cleaned up. */
360
361     FREE(previous_workspace_name);
362     if (current)
363         previous_workspace_name = sstrdup(current->name);
364
365     workspace_reassign_sticky(workspace);
366
367     LOG("switching to %p\n", workspace);
368     Con *next = con_descend_focused(workspace);
369
370     /* Memorize current output */
371     Con *old_output = con_get_output(focused);
372
373     /* Display urgency hint for a while if the newly visible workspace would
374      * focus and thereby immediately destroy it */
375     if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
376         /* focus for now… */
377         con_focus(next);
378
379         /* … but immediately reset urgency flags; they will be set to false by
380          * the timer callback in case the container is focused at the time of
381          * its expiration */
382         focused->urgent = true;
383         workspace->urgent = true;
384
385         if (focused->urgency_timer == NULL) {
386             DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
387                     focused, workspace);
388             focused->urgency_timer = scalloc(sizeof(struct ev_timer));
389             /* use a repeating timer to allow for easy resets */
390             ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
391                     config.workspace_urgency_timer, config.workspace_urgency_timer);
392             focused->urgency_timer->data = focused;
393             ev_timer_start(main_loop, focused->urgency_timer);
394         } else {
395             DLOG("Resetting urgency timer of con %p on workspace %p\n",
396                     focused, workspace);
397             ev_timer_again(main_loop, focused->urgency_timer);
398         }
399     } else
400         con_focus(next);
401
402     /* Close old workspace if necessary. This must be done *after* doing
403      * urgency handling, because tree_close() will do a con_focus() on the next
404      * client, which will clear the urgency flag too early. Also, there is no
405      * way for con_focus() to know about when to clear urgency immediately and
406      * when to defer it. */
407     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
408         /* check if this workspace is currently visible */
409         if (!workspace_is_visible(old)) {
410             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
411             tree_close(old, DONT_KILL_WINDOW, false, false);
412             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
413         }
414     }
415
416     workspace->fullscreen_mode = CF_OUTPUT;
417     LOG("focused now = %p / %s\n", focused, focused->name);
418
419     /* Set mouse pointer */
420     Con *new_output = con_get_output(focused);
421     if (old_output != new_output) {
422         x_set_warp_to(&next->rect);
423     }
424
425     /* Update the EWMH hints */
426     ewmh_update_current_desktop();
427
428     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
429 }
430
431 /*
432  * Switches to the given workspace
433  *
434  */
435 void workspace_show(Con *workspace) {
436     _workspace_show(workspace);
437 }
438
439 /*
440  * Looks up the workspace by name and switches to it.
441  *
442  */
443 void workspace_show_by_name(const char *num) {
444     Con *workspace;
445     workspace = workspace_get(num, NULL);
446     _workspace_show(workspace);
447 }
448
449 /*
450  * Focuses the next workspace.
451  *
452  */
453 Con* workspace_next(void) {
454     Con *current = con_get_workspace(focused);
455     Con *next = NULL;
456     Con *output;
457
458     if (current->num == -1) {
459         /* If currently a named workspace, find next named workspace. */
460         next = TAILQ_NEXT(current, nodes);
461     } else {
462         /* If currently a numbered workspace, find next numbered workspace. */
463         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
464             /* Skip outputs starting with __, they are internal. */
465             if (con_is_internal(output))
466                 continue;
467             NODES_FOREACH(output_get_content(output)) {
468                 if (child->type != CT_WORKSPACE)
469                     continue;
470                 if (child->num == -1)
471                     break;
472                 /* Need to check child against current and next because we are
473                  * traversing multiple lists and thus are not guaranteed the
474                  * relative order between the list of workspaces. */
475                 if (current->num < child->num && (!next || child->num < next->num))
476                     next = child;
477             }
478         }
479     }
480
481     /* Find next named workspace. */
482     if (!next) {
483         bool found_current = false;
484         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
485             /* Skip outputs starting with __, they are internal. */
486             if (con_is_internal(output))
487                 continue;
488             NODES_FOREACH(output_get_content(output)) {
489                 if (child->type != CT_WORKSPACE)
490                     continue;
491                 if (child == current) {
492                     found_current = 1;
493                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
494                     next = child;
495                     goto workspace_next_end;
496                 }
497             }
498         }
499     }
500
501     /* Find first workspace. */
502     if (!next) {
503         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
504             /* Skip outputs starting with __, they are internal. */
505             if (con_is_internal(output))
506                 continue;
507             NODES_FOREACH(output_get_content(output)) {
508                 if (child->type != CT_WORKSPACE)
509                     continue;
510                 if (!next || (child->num != -1 && child->num < next->num))
511                     next = child;
512             }
513         }
514     }
515 workspace_next_end:
516     return next;
517 }
518
519 /*
520  * Focuses the previous workspace.
521  *
522  */
523 Con* workspace_prev(void) {
524     Con *current = con_get_workspace(focused);
525     Con *prev = NULL;
526     Con *output;
527
528     if (current->num == -1) {
529         /* If named workspace, find previous named workspace. */
530         prev = TAILQ_PREV(current, nodes_head, nodes);
531         if (prev && prev->num != -1)
532             prev = NULL;
533     } else {
534         /* If numbered workspace, find previous numbered workspace. */
535         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
536             /* Skip outputs starting with __, they are internal. */
537             if (con_is_internal(output))
538                 continue;
539             NODES_FOREACH_REVERSE(output_get_content(output)) {
540                 if (child->type != CT_WORKSPACE || child->num == -1)
541                     continue;
542                 /* Need to check child against current and previous because we
543                  * are traversing multiple lists and thus are not guaranteed
544                  * the relative order between the list of workspaces. */
545                 if (current->num > child->num && (!prev || child->num > prev->num))
546                     prev = child;
547             }
548         }
549     }
550
551     /* Find previous named workspace. */
552     if (!prev) {
553         bool found_current = false;
554         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
555             /* Skip outputs starting with __, they are internal. */
556             if (con_is_internal(output))
557                 continue;
558             NODES_FOREACH_REVERSE(output_get_content(output)) {
559                 if (child->type != CT_WORKSPACE)
560                     continue;
561                 if (child == current) {
562                     found_current = true;
563                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
564                     prev = child;
565                     goto workspace_prev_end;
566                 }
567             }
568         }
569     }
570
571     /* Find last workspace. */
572     if (!prev) {
573         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
574             /* Skip outputs starting with __, they are internal. */
575             if (con_is_internal(output))
576                 continue;
577             NODES_FOREACH_REVERSE(output_get_content(output)) {
578                 if (child->type != CT_WORKSPACE)
579                     continue;
580                 if (!prev || child->num > prev->num)
581                     prev = child;
582             }
583         }
584     }
585
586 workspace_prev_end:
587     return prev;
588 }
589
590
591 /*
592  * Focuses the next workspace on the same output.
593  *
594  */
595 Con* workspace_next_on_output(void) {
596     Con *current = con_get_workspace(focused);
597     Con *next = NULL;
598     Con *output  = con_get_output(focused);
599
600     if (current->num == -1) {
601         /* If currently a named workspace, find next named workspace. */
602         next = TAILQ_NEXT(current, nodes);
603     } else {
604         /* If currently a numbered workspace, find next numbered workspace. */
605         NODES_FOREACH(output_get_content(output)) {
606             if (child->type != CT_WORKSPACE)
607                 continue;
608             if (child->num == -1)
609                 break;
610             /* Need to check child against current and next because we are
611              * traversing multiple lists and thus are not guaranteed the
612              * relative order between the list of workspaces. */
613             if (current->num < child->num && (!next || child->num < next->num))
614                 next = child;
615             }
616         }
617
618     /* Find next named workspace. */
619     if (!next) {
620         bool found_current = false;
621         NODES_FOREACH(output_get_content(output)) {
622             if (child->type != CT_WORKSPACE)
623                 continue;
624             if (child == current) {
625                 found_current = 1;
626             } else if (child->num == -1 && (current->num != -1 || found_current)) {
627                 next = child;
628                 goto workspace_next_on_output_end;
629             }
630         }
631     }
632
633     /* Find first workspace. */
634     if (!next) {
635         NODES_FOREACH(output_get_content(output)) {
636             if (child->type != CT_WORKSPACE)
637                 continue;
638             if (!next || (child->num != -1 && child->num < next->num))
639                 next = child;
640         }
641     }
642 workspace_next_on_output_end:
643     return next;
644 }
645
646 /*
647  * Focuses the previous workspace on same output.
648  *
649  */
650 Con* workspace_prev_on_output(void) {
651     Con *current = con_get_workspace(focused);
652     Con *prev = NULL;
653     Con *output  = con_get_output(focused);
654     DLOG("output = %s\n", output->name);
655
656     if (current->num == -1) {
657         /* If named workspace, find previous named workspace. */
658         prev = TAILQ_PREV(current, nodes_head, nodes);
659         if (prev && prev->num != -1)
660             prev = NULL;
661     } else {
662         /* If numbered workspace, find previous numbered workspace. */
663         NODES_FOREACH_REVERSE(output_get_content(output)) {
664             if (child->type != CT_WORKSPACE || child->num == -1)
665                 continue;
666              /* Need to check child against current and previous because we
667              * are traversing multiple lists and thus are not guaranteed
668              * the relative order between the list of workspaces. */
669             if (current->num > child->num && (!prev || child->num > prev->num))
670                 prev = child;
671         }
672     }
673
674     /* Find previous named workspace. */
675     if (!prev) {
676         bool found_current = false;
677         NODES_FOREACH_REVERSE(output_get_content(output)) {
678             if (child->type != CT_WORKSPACE)
679                 continue;
680             if (child == current) {
681                 found_current = true;
682             } else if (child->num == -1 && (current->num != -1 || found_current)) {
683                 prev = child;
684                 goto workspace_prev_on_output_end;
685             }
686         }
687     }
688
689     /* Find last workspace. */
690     if (!prev) {
691         NODES_FOREACH_REVERSE(output_get_content(output)) {
692             if (child->type != CT_WORKSPACE)
693                 continue;
694             if (!prev || child->num > prev->num)
695                 prev = child;
696         }
697     }
698
699 workspace_prev_on_output_end:
700     return prev;
701 }
702
703 /*
704  * Focuses the previously focused workspace.
705  *
706  */
707 void workspace_back_and_forth(void) {
708     if (!previous_workspace_name) {
709         DLOG("No previous workspace name set. Not switching.");
710         return;
711     }
712
713     workspace_show_by_name(previous_workspace_name);
714 }
715
716 /*
717  * Returns the previously focused workspace con, or NULL if unavailable.
718  *
719  */
720 Con *workspace_back_and_forth_get(void) {
721     if (!previous_workspace_name) {
722         DLOG("no previous workspace name set.");
723         return NULL;
724     }
725
726     Con *workspace;
727     workspace = workspace_get(previous_workspace_name, NULL);
728
729     return workspace;
730 }
731
732 static bool get_urgency_flag(Con *con) {
733     Con *child;
734     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
735         if (child->urgent || get_urgency_flag(child))
736             return true;
737
738     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
739         if (child->urgent || get_urgency_flag(child))
740             return true;
741
742     return false;
743 }
744
745 /*
746  * Goes through all clients on the given workspace and updates the workspace’s
747  * urgent flag accordingly.
748  *
749  */
750 void workspace_update_urgent_flag(Con *ws) {
751     bool old_flag = ws->urgent;
752     ws->urgent = get_urgency_flag(ws);
753     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
754
755     if (old_flag != ws->urgent)
756         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
757 }
758
759 /*
760  * 'Forces' workspace orientation by moving all cons into a new split-con with
761  * the same layout as the workspace and then changing the workspace layout.
762  *
763  */
764 void ws_force_orientation(Con *ws, orientation_t orientation) {
765     /* 1: create a new split container */
766     Con *split = con_new(NULL, NULL);
767     split->parent = ws;
768     split->split = true;
769
770     /* 2: copy layout from workspace */
771     split->layout = ws->layout;
772
773     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
774
775     /* 3: move the existing cons of this workspace below the new con */
776     DLOG("Moving cons\n");
777     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
778         Con *child = TAILQ_FIRST(&(ws->nodes_head));
779         con_detach(child);
780         con_attach(child, split, true);
781     }
782
783     /* 4: switch workspace layout */
784     ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
785     DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
786
787     /* 5: attach the new split container to the workspace */
788     DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
789     con_attach(split, ws, false);
790
791     /* 6: fix the percentages */
792     con_fix_percent(ws);
793
794     if (old_focused)
795         con_focus(old_focused);
796 }
797
798 /*
799  * Called when a new con (with a window, not an empty or split con) should be
800  * attached to the workspace (for example when managing a new window or when
801  * moving an existing window to the workspace level).
802  *
803  * Depending on the workspace_layout setting, this function either returns the
804  * workspace itself (default layout) or creates a new stacked/tabbed con and
805  * returns that.
806  *
807  */
808 Con *workspace_attach_to(Con *ws) {
809     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
810
811     if (ws->workspace_layout == L_DEFAULT) {
812         DLOG("Default layout, just attaching it to the workspace itself.\n");
813         return ws;
814     }
815
816     DLOG("Non-default layout, creating a new split container\n");
817     /* 1: create a new split container */
818     Con *new = con_new(NULL, NULL);
819     new->parent = ws;
820     new->split = true;
821
822     /* 2: set the requested layout on the split con */
823     new->layout = ws->workspace_layout;
824
825     /* 4: attach the new split container to the workspace */
826     DLOG("Attaching new split %p to workspace %p\n", new, ws);
827     con_attach(new, ws, false);
828
829     return new;
830 }