]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Merge branch 'master' into next
[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 begins with a
56          * 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/number command.
116          * Beware: The workspace names "next", "prev", "next_on_output",
117          * "prev_on_output", "number" and "back_and_forth" are OK, so we check
118          * before 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, "number", strlen("number")) == 0 ||
124             strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0)
125             continue;
126         if (*target == '"')
127             target++;
128         FREE(ws->name);
129         ws->name = strdup(target);
130         if (ws->name[strlen(ws->name)-1] == '"')
131             ws->name[strlen(ws->name)-1] = '\0';
132         DLOG("trying name *%s*\n", ws->name);
133
134         /* Ensure that this workspace is not assigned to a different output —
135          * otherwise we would create it, then move it over to its output, then
136          * find a new workspace, etc… */
137         bool assigned = false;
138         struct Workspace_Assignment *assignment;
139         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
140             if (strcmp(assignment->name, ws->name) != 0 ||
141                 strcmp(assignment->output, output->name) == 0)
142                 continue;
143
144             assigned = true;
145             break;
146         }
147
148         if (assigned)
149             continue;
150
151         current = NULL;
152         TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
153             GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
154
155         exists = (current != NULL);
156         if (!exists) {
157             /* Set ->num to the number of the workspace, if the name actually
158              * is a number or starts with a number */
159             char *endptr = NULL;
160             long parsed_num = strtol(ws->name, &endptr, 10);
161             if (parsed_num == LONG_MIN ||
162                 parsed_num == LONG_MAX ||
163                 parsed_num < 0 ||
164                 endptr == ws->name)
165                 ws->num = -1;
166             else ws->num = parsed_num;
167             LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
168
169             break;
170         }
171     }
172
173     if (exists) {
174         /* get the next unused workspace number */
175         DLOG("Getting next unused workspace by number\n");
176         int c = 0;
177         while (exists) {
178             c++;
179
180             FREE(ws->name);
181             sasprintf(&(ws->name), "%d", c);
182
183             current = NULL;
184             TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
185                 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
186             exists = (current != NULL);
187
188             DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
189         }
190         ws->num = c;
191     }
192     con_attach(ws, content, false);
193
194     sasprintf(&name, "[i3 con] workspace %s", ws->name);
195     x_set_name(ws, name);
196     free(name);
197
198     ws->fullscreen_mode = CF_OUTPUT;
199
200     /* If default_orientation is set to NO_ORIENTATION we determine
201      * orientation depending on output resolution. */
202     if (config.default_orientation == NO_ORIENTATION) {
203         ws->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
204         DLOG("Auto orientation. Workspace size set to (%d,%d), setting orientation to %d.\n",
205              output->rect.width, output->rect.height, ws->orientation);
206     } else {
207         ws->orientation = config.default_orientation;
208     }
209
210     return ws;
211 }
212
213 /*
214  * Returns true if the workspace is currently visible. Especially important for
215  * multi-monitor environments, as they can have multiple currenlty active
216  * workspaces.
217  *
218  */
219 bool workspace_is_visible(Con *ws) {
220     Con *output = con_get_output(ws);
221     if (output == NULL)
222         return false;
223     Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
224     LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
225     return (fs == ws);
226 }
227
228 /*
229  * XXX: we need to clean up all this recursive walking code.
230  *
231  */
232 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
233     Con *current;
234
235     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
236         if (current != exclude &&
237             current->sticky_group != NULL &&
238             current->window != NULL &&
239             strcmp(current->sticky_group, sticky_group) == 0)
240             return current;
241
242         Con *recurse = _get_sticky(current, sticky_group, exclude);
243         if (recurse != NULL)
244             return recurse;
245     }
246
247     TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
248         if (current != exclude &&
249             current->sticky_group != NULL &&
250             current->window != NULL &&
251             strcmp(current->sticky_group, sticky_group) == 0)
252             return current;
253
254         Con *recurse = _get_sticky(current, sticky_group, exclude);
255         if (recurse != NULL)
256             return recurse;
257     }
258
259     return NULL;
260 }
261
262 /*
263  * Reassigns all child windows in sticky containers. Called when the user
264  * changes workspaces.
265  *
266  * XXX: what about sticky containers which contain containers?
267  *
268  */
269 static void workspace_reassign_sticky(Con *con) {
270     Con *current;
271     /* 1: go through all containers */
272
273     /* handle all children and floating windows of this node */
274     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
275         if (current->sticky_group == NULL) {
276             workspace_reassign_sticky(current);
277             continue;
278         }
279
280         LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
281         /* 2: find a window which we can re-assign */
282         Con *output = con_get_output(current);
283         Con *src = _get_sticky(output, current->sticky_group, current);
284
285         if (src == NULL) {
286             LOG("No window found for this sticky group\n");
287             workspace_reassign_sticky(current);
288             continue;
289         }
290
291         x_move_win(src, current);
292         current->window = src->window;
293         current->mapped = true;
294         src->window = NULL;
295         src->mapped = false;
296
297         x_reparent_child(current, src);
298
299         LOG("re-assigned window from src %p to dest %p\n", src, current);
300     }
301
302     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
303         workspace_reassign_sticky(current);
304 }
305
306
307 static void _workspace_show(Con *workspace) {
308     Con *current, *old = NULL;
309
310     /* safe-guard against showing i3-internal workspaces like __i3_scratch */
311     if (workspace->name[0] == '_' && workspace->name[1] == '_')
312         return;
313
314     /* disable fullscreen for the other workspaces and get the workspace we are
315      * currently on. */
316     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
317         if (current->fullscreen_mode == CF_OUTPUT)
318             old = current;
319         current->fullscreen_mode = CF_NONE;
320     }
321
322     /* enable fullscreen for the target workspace. If it happens to be the
323      * same one we are currently on anyways, we can stop here. */
324     workspace->fullscreen_mode = CF_OUTPUT;
325     current = con_get_workspace(focused);
326     if (workspace == current) {
327         DLOG("Not switching, already there.\n");
328         return;
329     }
330
331     /* Remember currently focused workspace for switching back to it later with
332      * the 'workspace back_and_forth' command.
333      * NOTE: We have to duplicate the name as the original will be freed when
334      * the corresponding workspace is cleaned up. */
335
336     FREE(previous_workspace_name);
337     if (current)
338         previous_workspace_name = sstrdup(current->name);
339
340     workspace_reassign_sticky(workspace);
341
342     LOG("switching to %p\n", workspace);
343     Con *next = con_descend_focused(workspace);
344
345     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
346         /* check if this workspace is currently visible */
347         if (!workspace_is_visible(old)) {
348             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
349             tree_close(old, DONT_KILL_WINDOW, false, false);
350             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
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);
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);
390 }
391
392 /*
393  * Focuses the next workspace.
394  *
395  */
396 Con* workspace_next(void) {
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(void) {
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(void) {
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(void) {
594     Con *current = con_get_workspace(focused);
595     Con *prev = NULL;
596     Con *output  = con_get_output(focused);
597     DLOG("output = %s\n", output->name);
598
599     if (current->num == -1) {
600         /* If named workspace, find previous named workspace. */
601         prev = TAILQ_PREV(current, nodes_head, nodes);
602         if (prev && prev->num != -1)
603             prev = NULL;
604     } else {
605         /* If numbered workspace, find previous numbered workspace. */
606         NODES_FOREACH_REVERSE(output_get_content(output)) {
607             if (child->type != CT_WORKSPACE || child->num == -1)
608                 continue;
609              /* Need to check child against current and previous because we
610              * are traversing multiple lists and thus are not guaranteed
611              * the relative order between the list of workspaces. */
612             if (current->num > child->num && (!prev || child->num > prev->num))
613                 prev = child;
614         }
615     }
616
617     /* Find previous named workspace. */
618     if (!prev) {
619         bool found_current = false;
620         NODES_FOREACH_REVERSE(output_get_content(output)) {
621             if (child->type != CT_WORKSPACE)
622                 continue;
623             if (child == current) {
624                 found_current = true;
625             } else if (child->num == -1 && (current->num != -1 || found_current)) {
626                 prev = child;
627                 goto workspace_prev_on_output_end;
628             }
629         }
630     }
631
632     /* Find last workspace. */
633     if (!prev) {
634         NODES_FOREACH_REVERSE(output_get_content(output)) {
635             if (child->type != CT_WORKSPACE)
636                 continue;
637             if (!prev || child->num > prev->num)
638                 prev = child;
639         }
640     }
641
642 workspace_prev_on_output_end:
643     return prev;
644 }
645
646 /*
647  * Focuses the previously focused workspace.
648  *
649  */
650 void workspace_back_and_forth(void) {
651     if (!previous_workspace_name) {
652         DLOG("No previous workspace name set. Not switching.");
653         return;
654     }
655
656     workspace_show_by_name(previous_workspace_name);
657 }
658
659 static bool get_urgency_flag(Con *con) {
660     Con *child;
661     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
662         if (child->urgent || get_urgency_flag(child))
663             return true;
664
665     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
666         if (child->urgent || get_urgency_flag(child))
667             return true;
668
669     return false;
670 }
671
672 /*
673  * Goes through all clients on the given workspace and updates the workspace’s
674  * urgent flag accordingly.
675  *
676  */
677 void workspace_update_urgent_flag(Con *ws) {
678     bool old_flag = ws->urgent;
679     ws->urgent = get_urgency_flag(ws);
680     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
681
682     if (old_flag != ws->urgent)
683         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
684 }
685
686 /*
687  * 'Forces' workspace orientation by moving all cons into a new split-con with
688  * the same orientation as the workspace and then changing the workspace
689  * orientation.
690  *
691  */
692 void ws_force_orientation(Con *ws, orientation_t orientation) {
693     /* 1: create a new split container */
694     Con *split = con_new(NULL, NULL);
695     split->parent = ws;
696
697     /* 2: copy layout and orientation from workspace */
698     split->layout = ws->layout;
699     split->orientation = ws->orientation;
700
701     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
702
703     /* 3: move the existing cons of this workspace below the new con */
704     DLOG("Moving cons\n");
705     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
706         Con *child = TAILQ_FIRST(&(ws->nodes_head));
707         con_detach(child);
708         con_attach(child, split, true);
709     }
710
711     /* 4: switch workspace orientation */
712     ws->orientation = orientation;
713
714     /* 5: attach the new split container to the workspace */
715     DLOG("Attaching new split to ws\n");
716     con_attach(split, ws, false);
717
718     /* 6: fix the percentages */
719     con_fix_percent(ws);
720
721     if (old_focused)
722         con_focus(old_focused);
723 }
724
725 /*
726  * Called when a new con (with a window, not an empty or split con) should be
727  * attached to the workspace (for example when managing a new window or when
728  * moving an existing window to the workspace level).
729  *
730  * Depending on the workspace_layout setting, this function either returns the
731  * workspace itself (default layout) or creates a new stacked/tabbed con and
732  * returns that.
733  *
734  */
735 Con *workspace_attach_to(Con *ws) {
736     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
737
738     if (config.default_layout == L_DEFAULT) {
739         DLOG("Default layout, just attaching it to the workspace itself.\n");
740         return ws;
741     }
742
743     DLOG("Non-default layout, creating a new split container\n");
744     /* 1: create a new split container */
745     Con *new = con_new(NULL, NULL);
746     new->parent = ws;
747
748     /* 2: set the requested layout on the split con */
749     new->layout = config.default_layout;
750
751     /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
752      * to be set. Otherwise, this con will not be interpreted as a split
753      * container. */
754     if (config.default_orientation == NO_ORIENTATION) {
755         new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
756     } else {
757         new->orientation = config.default_orientation;
758     }
759
760     /* 4: attach the new split container to the workspace */
761     DLOG("Attaching new split %p to workspace %p\n", new, ws);
762     con_attach(new, ws, false);
763
764     return new;
765 }