]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
use I3__FILE__ for DLOG, leave __FILE__ as is
[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         /* We set ->num to the number if this workspace’s name begins with a
77          * positive number. Otherwise it’s a named ws and num will be -1. */
78         char *endptr = NULL;
79         long parsed_num = strtol(num, &endptr, 10);
80         if (parsed_num == LONG_MIN ||
81             parsed_num == LONG_MAX ||
82             parsed_num < 0 ||
83             endptr == num)
84             workspace->num = -1;
85         else workspace->num = parsed_num;
86         LOG("num = %d\n", workspace->num);
87
88         workspace->parent = content;
89         _workspace_apply_default_orientation(workspace);
90
91         con_attach(workspace, content, false);
92
93         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
94         if (created != NULL)
95             *created = true;
96     }
97     else if (created != NULL) {
98         *created = false;
99     }
100
101     return workspace;
102 }
103
104 /*
105  * Returns a pointer to a new workspace in the given output. The workspace
106  * is created attached to the tree hierarchy through the given content
107  * container.
108  *
109  */
110 Con *create_workspace_on_output(Output *output, Con *content) {
111     /* add a workspace to this output */
112     Con *out, *current;
113     char *name;
114     bool exists = true;
115     Con *ws = con_new(NULL, NULL);
116     ws->type = CT_WORKSPACE;
117
118     /* try the configured workspace bindings first to find a free name */
119     Binding *bind;
120     TAILQ_FOREACH(bind, bindings, bindings) {
121         DLOG("binding with command %s\n", bind->command);
122         if (strlen(bind->command) < strlen("workspace ") ||
123             strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
124             continue;
125         DLOG("relevant command = %s\n", bind->command);
126         char *target = bind->command + strlen("workspace ");
127         /* We check if this is the workspace
128          * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
129          * Beware: The workspace names "next", "prev", "next_on_output",
130          * "prev_on_output", "number", "back_and_forth" and "current" are OK,
131          * so we check before stripping the double quotes */
132         if (strncasecmp(target, "next", strlen("next")) == 0 ||
133             strncasecmp(target, "prev", strlen("prev")) == 0 ||
134             strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
135             strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
136             strncasecmp(target, "number", strlen("number")) == 0 ||
137             strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
138             strncasecmp(target, "current", strlen("current")) == 0)
139             continue;
140         if (*target == '"')
141             target++;
142         FREE(ws->name);
143         ws->name = strdup(target);
144         if (ws->name[strlen(ws->name)-1] == '"')
145             ws->name[strlen(ws->name)-1] = '\0';
146         DLOG("trying name *%s*\n", ws->name);
147
148         /* Ensure that this workspace is not assigned to a different output —
149          * otherwise we would create it, then move it over to its output, then
150          * find a new workspace, etc… */
151         bool assigned = false;
152         struct Workspace_Assignment *assignment;
153         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
154             if (strcmp(assignment->name, ws->name) != 0 ||
155                 strcmp(assignment->output, output->name) == 0)
156                 continue;
157
158             assigned = true;
159             break;
160         }
161
162         if (assigned)
163             continue;
164
165         current = NULL;
166         TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
167             GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
168
169         exists = (current != NULL);
170         if (!exists) {
171             /* Set ->num to the number of the workspace, if the name actually
172              * is a number or starts with a number */
173             char *endptr = NULL;
174             long parsed_num = strtol(ws->name, &endptr, 10);
175             if (parsed_num == LONG_MIN ||
176                 parsed_num == LONG_MAX ||
177                 parsed_num < 0 ||
178                 endptr == ws->name)
179                 ws->num = -1;
180             else ws->num = parsed_num;
181             LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
182
183             break;
184         }
185     }
186
187     if (exists) {
188         /* get the next unused workspace number */
189         DLOG("Getting next unused workspace by number\n");
190         int c = 0;
191         while (exists) {
192             c++;
193
194             FREE(ws->name);
195             sasprintf(&(ws->name), "%d", c);
196
197             current = NULL;
198             TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
199                 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
200             exists = (current != NULL);
201
202             DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
203         }
204         ws->num = c;
205     }
206     con_attach(ws, content, false);
207
208     sasprintf(&name, "[i3 con] workspace %s", ws->name);
209     x_set_name(ws, name);
210     free(name);
211
212     ws->fullscreen_mode = CF_OUTPUT;
213
214     _workspace_apply_default_orientation(ws);
215
216     return ws;
217 }
218
219
220 /*
221  * Returns true if the workspace is currently visible. Especially important for
222  * multi-monitor environments, as they can have multiple currenlty active
223  * workspaces.
224  *
225  */
226 bool workspace_is_visible(Con *ws) {
227     Con *output = con_get_output(ws);
228     if (output == NULL)
229         return false;
230     Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
231     LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
232     return (fs == ws);
233 }
234
235 /*
236  * XXX: we need to clean up all this recursive walking code.
237  *
238  */
239 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
240     Con *current;
241
242     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
243         if (current != exclude &&
244             current->sticky_group != NULL &&
245             current->window != NULL &&
246             strcmp(current->sticky_group, sticky_group) == 0)
247             return current;
248
249         Con *recurse = _get_sticky(current, sticky_group, exclude);
250         if (recurse != NULL)
251             return recurse;
252     }
253
254     TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
255         if (current != exclude &&
256             current->sticky_group != NULL &&
257             current->window != NULL &&
258             strcmp(current->sticky_group, sticky_group) == 0)
259             return current;
260
261         Con *recurse = _get_sticky(current, sticky_group, exclude);
262         if (recurse != NULL)
263             return recurse;
264     }
265
266     return NULL;
267 }
268
269 /*
270  * Reassigns all child windows in sticky containers. Called when the user
271  * changes workspaces.
272  *
273  * XXX: what about sticky containers which contain containers?
274  *
275  */
276 static void workspace_reassign_sticky(Con *con) {
277     Con *current;
278     /* 1: go through all containers */
279
280     /* handle all children and floating windows of this node */
281     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
282         if (current->sticky_group == NULL) {
283             workspace_reassign_sticky(current);
284             continue;
285         }
286
287         LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
288         /* 2: find a window which we can re-assign */
289         Con *output = con_get_output(current);
290         Con *src = _get_sticky(output, current->sticky_group, current);
291
292         if (src == NULL) {
293             LOG("No window found for this sticky group\n");
294             workspace_reassign_sticky(current);
295             continue;
296         }
297
298         x_move_win(src, current);
299         current->window = src->window;
300         current->mapped = true;
301         src->window = NULL;
302         src->mapped = false;
303
304         x_reparent_child(current, src);
305
306         LOG("re-assigned window from src %p to dest %p\n", src, current);
307     }
308
309     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
310         workspace_reassign_sticky(current);
311 }
312
313
314 static void _workspace_show(Con *workspace) {
315     Con *current, *old = NULL;
316
317     /* safe-guard against showing i3-internal workspaces like __i3_scratch */
318     if (workspace->name[0] == '_' && workspace->name[1] == '_')
319         return;
320
321     /* disable fullscreen for the other workspaces and get the workspace we are
322      * currently on. */
323     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
324         if (current->fullscreen_mode == CF_OUTPUT)
325             old = current;
326         current->fullscreen_mode = CF_NONE;
327     }
328
329     /* enable fullscreen for the target workspace. If it happens to be the
330      * same one we are currently on anyways, we can stop here. */
331     workspace->fullscreen_mode = CF_OUTPUT;
332     current = con_get_workspace(focused);
333     if (workspace == current) {
334         DLOG("Not switching, already there.\n");
335         return;
336     }
337
338     /* Remember currently focused workspace for switching back to it later with
339      * the 'workspace back_and_forth' command.
340      * NOTE: We have to duplicate the name as the original will be freed when
341      * the corresponding workspace is cleaned up. */
342
343     FREE(previous_workspace_name);
344     if (current)
345         previous_workspace_name = sstrdup(current->name);
346
347     workspace_reassign_sticky(workspace);
348
349     LOG("switching to %p\n", workspace);
350     Con *next = con_descend_focused(workspace);
351
352     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
353         /* check if this workspace is currently visible */
354         if (!workspace_is_visible(old)) {
355             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
356             tree_close(old, DONT_KILL_WINDOW, false, false);
357             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
358         }
359     }
360
361     /* Memorize current output */
362     Con *old_output = con_get_output(focused);
363
364     con_focus(next);
365     workspace->fullscreen_mode = CF_OUTPUT;
366     LOG("focused now = %p / %s\n", focused, focused->name);
367
368     /* Set mouse pointer */
369     Con *new_output = con_get_output(focused);
370     if (old_output != new_output) {
371         x_set_warp_to(&next->rect);
372     }
373
374     /* Update the EWMH hints */
375     ewmh_update_current_desktop();
376
377     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
378 }
379
380 /*
381  * Switches to the given workspace
382  *
383  */
384 void workspace_show(Con *workspace) {
385     _workspace_show(workspace);
386 }
387
388 /*
389  * Looks up the workspace by name and switches to it.
390  *
391  */
392 void workspace_show_by_name(const char *num) {
393     Con *workspace;
394     bool changed_num_workspaces;
395     workspace = workspace_get(num, &changed_num_workspaces);
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 static bool get_urgency_flag(Con *con) {
667     Con *child;
668     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
669         if (child->urgent || get_urgency_flag(child))
670             return true;
671
672     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
673         if (child->urgent || get_urgency_flag(child))
674             return true;
675
676     return false;
677 }
678
679 /*
680  * Goes through all clients on the given workspace and updates the workspace’s
681  * urgent flag accordingly.
682  *
683  */
684 void workspace_update_urgent_flag(Con *ws) {
685     bool old_flag = ws->urgent;
686     ws->urgent = get_urgency_flag(ws);
687     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
688
689     if (old_flag != ws->urgent)
690         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
691 }
692
693 /*
694  * 'Forces' workspace orientation by moving all cons into a new split-con with
695  * the same layout as the workspace and then changing the workspace layout.
696  *
697  */
698 void ws_force_orientation(Con *ws, orientation_t orientation) {
699     /* 1: create a new split container */
700     Con *split = con_new(NULL, NULL);
701     split->parent = ws;
702
703     /* 2: copy layout from workspace */
704     split->layout = ws->layout;
705
706     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
707
708     /* 3: move the existing cons of this workspace below the new con */
709     DLOG("Moving cons\n");
710     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
711         Con *child = TAILQ_FIRST(&(ws->nodes_head));
712         con_detach(child);
713         con_attach(child, split, true);
714     }
715
716     /* 4: switch workspace layout */
717     ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
718
719     /* 5: attach the new split container to the workspace */
720     DLOG("Attaching new split to ws\n");
721     con_attach(split, ws, false);
722
723     /* 6: fix the percentages */
724     con_fix_percent(ws);
725
726     if (old_focused)
727         con_focus(old_focused);
728 }
729
730 /*
731  * Called when a new con (with a window, not an empty or split con) should be
732  * attached to the workspace (for example when managing a new window or when
733  * moving an existing window to the workspace level).
734  *
735  * Depending on the workspace_layout setting, this function either returns the
736  * workspace itself (default layout) or creates a new stacked/tabbed con and
737  * returns that.
738  *
739  */
740 Con *workspace_attach_to(Con *ws) {
741     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
742
743     if (config.default_layout == L_DEFAULT) {
744         DLOG("Default layout, just attaching it to the workspace itself.\n");
745         return ws;
746     }
747
748     DLOG("Non-default layout, creating a new split container\n");
749     /* 1: create a new split container */
750     Con *new = con_new(NULL, NULL);
751     new->parent = ws;
752     new->split = true;
753
754     /* 2: set the requested layout on the split con */
755     new->layout = config.default_layout;
756
757     /* 4: attach the new split container to the workspace */
758     DLOG("Attaching new split %p to workspace %p\n", new, ws);
759     con_attach(new, ws, false);
760
761     return new;
762 }