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