]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Merge branch 'fix-static-analysis'
[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 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) {
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         }
351     }
352
353     /* Memorize current output */
354     Con *old_output = con_get_output(focused);
355
356     con_focus(next);
357     workspace->fullscreen_mode = CF_OUTPUT;
358     LOG("focused now = %p / %s\n", focused, focused->name);
359
360     /* Set mouse pointer */
361     Con *new_output = con_get_output(focused);
362     if (old_output != new_output) {
363         x_set_warp_to(&next->rect);
364     }
365
366     /* Update the EWMH hints */
367     ewmh_update_current_desktop();
368
369     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
370 }
371
372 /*
373  * Switches to the given workspace
374  *
375  */
376 void workspace_show(Con *workspace) {
377     _workspace_show(workspace);
378 }
379
380 /*
381  * Looks up the workspace by name and switches to it.
382  *
383  */
384 void workspace_show_by_name(const char *num) {
385     Con *workspace;
386     bool changed_num_workspaces;
387     workspace = workspace_get(num, &changed_num_workspaces);
388     _workspace_show(workspace);
389 }
390
391 /*
392  * Focuses the next workspace.
393  *
394  */
395 Con* workspace_next(void) {
396     Con *current = con_get_workspace(focused);
397     Con *next = NULL;
398     Con *output;
399
400     if (current->num == -1) {
401         /* If currently a named workspace, find next named workspace. */
402         next = TAILQ_NEXT(current, nodes);
403     } else {
404         /* If currently a numbered workspace, find next numbered workspace. */
405         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
406             /* Skip outputs starting with __, they are internal. */
407             if (output->name[0] == '_' && output->name[1] == '_')
408                 continue;
409             NODES_FOREACH(output_get_content(output)) {
410                 if (child->type != CT_WORKSPACE)
411                     continue;
412                 if (child->num == -1)
413                     break;
414                 /* Need to check child against current and next because we are
415                  * traversing multiple lists and thus are not guaranteed the
416                  * relative order between the list of workspaces. */
417                 if (current->num < child->num && (!next || child->num < next->num))
418                     next = child;
419             }
420         }
421     }
422
423     /* Find next named workspace. */
424     if (!next) {
425         bool found_current = false;
426         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
427             /* Skip outputs starting with __, they are internal. */
428             if (output->name[0] == '_' && output->name[1] == '_')
429                 continue;
430             NODES_FOREACH(output_get_content(output)) {
431                 if (child->type != CT_WORKSPACE)
432                     continue;
433                 if (child == current) {
434                     found_current = 1;
435                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
436                     next = child;
437                     goto workspace_next_end;
438                 }
439             }
440         }
441     }
442
443     /* Find first workspace. */
444     if (!next) {
445         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
446             /* Skip outputs starting with __, they are internal. */
447             if (output->name[0] == '_' && output->name[1] == '_')
448                 continue;
449             NODES_FOREACH(output_get_content(output)) {
450                 if (child->type != CT_WORKSPACE)
451                     continue;
452                 if (!next || (child->num != -1 && child->num < next->num))
453                     next = child;
454             }
455         }
456     }
457 workspace_next_end:
458     return next;
459 }
460
461 /*
462  * Focuses the previous workspace.
463  *
464  */
465 Con* workspace_prev(void) {
466     Con *current = con_get_workspace(focused);
467     Con *prev = NULL;
468     Con *output;
469
470     if (current->num == -1) {
471         /* If named workspace, find previous named workspace. */
472         prev = TAILQ_PREV(current, nodes_head, nodes);
473         if (prev && prev->num != -1)
474             prev = NULL;
475     } else {
476         /* If numbered workspace, find previous numbered workspace. */
477         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
478             /* Skip outputs starting with __, they are internal. */
479             if (output->name[0] == '_' && output->name[1] == '_')
480                 continue;
481             NODES_FOREACH_REVERSE(output_get_content(output)) {
482                 if (child->type != CT_WORKSPACE || child->num == -1)
483                     continue;
484                 /* Need to check child against current and previous because we
485                  * are traversing multiple lists and thus are not guaranteed
486                  * the relative order between the list of workspaces. */
487                 if (current->num > child->num && (!prev || child->num > prev->num))
488                     prev = child;
489             }
490         }
491     }
492
493     /* Find previous named workspace. */
494     if (!prev) {
495         bool found_current = false;
496         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
497             /* Skip outputs starting with __, they are internal. */
498             if (output->name[0] == '_' && output->name[1] == '_')
499                 continue;
500             NODES_FOREACH_REVERSE(output_get_content(output)) {
501                 if (child->type != CT_WORKSPACE)
502                     continue;
503                 if (child == current) {
504                     found_current = true;
505                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
506                     prev = child;
507                     goto workspace_prev_end;
508                 }
509             }
510         }
511     }
512
513     /* Find last workspace. */
514     if (!prev) {
515         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
516             /* Skip outputs starting with __, they are internal. */
517             if (output->name[0] == '_' && output->name[1] == '_')
518                 continue;
519             NODES_FOREACH_REVERSE(output_get_content(output)) {
520                 if (child->type != CT_WORKSPACE)
521                     continue;
522                 if (!prev || child->num > prev->num)
523                     prev = child;
524             }
525         }
526     }
527
528 workspace_prev_end:
529     return prev;
530 }
531
532
533 /*
534  * Focuses the next workspace on the same output.
535  *
536  */
537 Con* workspace_next_on_output(void) {
538     Con *current = con_get_workspace(focused);
539     Con *next = NULL;
540     Con *output  = con_get_output(focused);
541
542     if (current->num == -1) {
543         /* If currently a named workspace, find next named workspace. */
544         next = TAILQ_NEXT(current, nodes);
545     } else {
546         /* If currently a numbered workspace, find next numbered workspace. */
547         NODES_FOREACH(output_get_content(output)) {
548             if (child->type != CT_WORKSPACE)
549                 continue;
550             if (child->num == -1)
551                 break;
552             /* Need to check child against current and next because we are
553              * traversing multiple lists and thus are not guaranteed the
554              * relative order between the list of workspaces. */
555             if (current->num < child->num && (!next || child->num < next->num))
556                 next = child;
557             }
558         }
559
560     /* Find next named workspace. */
561     if (!next) {
562         bool found_current = false;
563         NODES_FOREACH(output_get_content(output)) {
564             if (child->type != CT_WORKSPACE)
565                 continue;
566             if (child == current) {
567                 found_current = 1;
568             } else if (child->num == -1 && (current->num != -1 || found_current)) {
569                 next = child;
570                 goto workspace_next_on_output_end;
571             }
572         }
573     }
574
575     /* Find first workspace. */
576     if (!next) {
577         NODES_FOREACH(output_get_content(output)) {
578             if (child->type != CT_WORKSPACE)
579                 continue;
580             if (!next || (child->num != -1 && child->num < next->num))
581                 next = child;
582         }
583     }
584 workspace_next_on_output_end:
585     return next;
586 }
587
588 /*
589  * Focuses the previous workspace on same output.
590  *
591  */
592 Con* workspace_prev_on_output(void) {
593     Con *current = con_get_workspace(focused);
594     Con *prev = NULL;
595     Con *output  = con_get_output(focused);
596     DLOG("output = %s\n", output->name);
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(void) {
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 }