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