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