2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * workspace.c: Modifying workspaces, accessing them, moving containers to
12 #include "yajl_utils.h"
14 /* Stores a copy of the name of the last used workspace for the workspace
15 * back-and-forth switching. */
16 static char *previous_workspace_name = NULL;
18 /* NULL-terminated list of workspace names (in order) extracted from
20 static char **binding_workspace_names = NULL;
23 * Returns the workspace with the given name or NULL if such a workspace does
27 Con *get_existing_workspace_by_name(const char *name) {
28 Con *output, *workspace = NULL;
29 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
30 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, name));
37 * Returns the workspace with the given number or NULL if such a workspace does
41 Con *get_existing_workspace_by_num(int num) {
42 Con *output, *workspace = NULL;
43 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
44 GREP_FIRST(workspace, output_get_content(output), child->num == num);
51 * Sets ws->layout to splith/splitv if default_orientation was specified in the
52 * configfile. Otherwise, it uses splith/splitv depending on whether the output
53 * is higher than wide.
56 static void _workspace_apply_default_orientation(Con *ws) {
57 /* If default_orientation is set to NO_ORIENTATION we determine
58 * orientation depending on output resolution. */
59 if (config.default_orientation == NO_ORIENTATION) {
60 Con *output = con_get_output(ws);
61 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
62 ws->rect = output->rect;
63 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
64 output->rect.width, output->rect.height, ws->layout);
66 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
71 * Returns a pointer to the workspace with the given number (starting at 0),
72 * creating the workspace if necessary (by allocating the necessary amount of
73 * memory and initializing the data structures correctly).
76 Con *workspace_get(const char *num, bool *created) {
77 Con *workspace = get_existing_workspace_by_name(num);
79 if (workspace == NULL) {
80 LOG("Creating new workspace \"%s\"\n", num);
81 /* unless an assignment is found, we will create this workspace on the current output */
82 Con *output = con_get_output(focused);
83 /* look for assignments */
84 struct Workspace_Assignment *assignment;
86 /* We set workspace->num to the number if this workspace’s name begins
87 * with a positive number. Otherwise it’s a named ws and num will be
89 long parsed_num = ws_name_to_number(num);
91 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
92 if (strcmp(assignment->name, num) == 0) {
93 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
94 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
96 } else if (parsed_num != -1 && name_is_digits(assignment->name) && ws_name_to_number(assignment->name) == parsed_num) {
97 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
98 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
102 Con *content = output_get_content(output);
103 LOG("got output %p with content %p\n", output, content);
104 /* We need to attach this container after setting its type. con_attach
105 * will handle CT_WORKSPACEs differently */
106 workspace = con_new(NULL, NULL);
108 sasprintf(&name, "[i3 con] workspace %s", num);
109 x_set_name(workspace, name);
111 workspace->type = CT_WORKSPACE;
112 FREE(workspace->name);
113 workspace->name = sstrdup(num);
114 workspace->workspace_layout = config.default_layout;
115 workspace->num = parsed_num;
116 LOG("num = %d\n", workspace->num);
118 workspace->parent = content;
119 _workspace_apply_default_orientation(workspace);
121 con_attach(workspace, content, false);
123 ipc_send_workspace_event("init", workspace, NULL);
124 ewmh_update_number_of_desktops();
125 ewmh_update_desktop_names();
126 ewmh_update_desktop_viewport();
127 ewmh_update_wm_desktop();
130 } else if (created != NULL) {
138 * Extracts workspace names from keybindings (e.g. “web” from “bindsym $mod+1
139 * workspace web”), so that when an output needs a workspace, i3 can start with
140 * the first configured one. Needs to be called before reorder_bindings() so
141 * that the config-file order is used, not the i3-internal order.
144 void extract_workspace_names_from_bindings(void) {
147 if (binding_workspace_names != NULL) {
148 for (int i = 0; binding_workspace_names[i] != NULL; i++) {
149 free(binding_workspace_names[i]);
151 FREE(binding_workspace_names);
153 TAILQ_FOREACH(bind, bindings, bindings) {
154 DLOG("binding with command %s\n", bind->command);
155 if (strlen(bind->command) < strlen("workspace ") ||
156 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
158 DLOG("relevant command = %s\n", bind->command);
159 const char *target = bind->command + strlen("workspace ");
160 while (*target == ' ' || *target == '\t')
162 /* We check if this is the workspace
163 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
164 * Beware: The workspace names "next", "prev", "next_on_output",
165 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
166 * so we check before stripping the double quotes */
167 if (strncasecmp(target, "next", strlen("next")) == 0 ||
168 strncasecmp(target, "prev", strlen("prev")) == 0 ||
169 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
170 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
171 strncasecmp(target, "number", strlen("number")) == 0 ||
172 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
173 strncasecmp(target, "current", strlen("current")) == 0)
175 char *target_name = parse_string(&target, false);
176 if (target_name == NULL)
178 if (strncasecmp(target_name, "__", strlen("__")) == 0) {
179 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
183 DLOG("Saving workspace name \"%s\"\n", target_name);
185 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
186 binding_workspace_names[n - 1] = target_name;
188 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
189 binding_workspace_names[n - 1] = NULL;
193 * Returns a pointer to a new workspace in the given output. The workspace
194 * is created attached to the tree hierarchy through the given content
198 Con *create_workspace_on_output(Output *output, Con *content) {
199 /* add a workspace to this output */
202 Con *ws = con_new(NULL, NULL);
203 ws->type = CT_WORKSPACE;
205 /* try the configured workspace bindings first to find a free name */
206 for (int n = 0; binding_workspace_names[n] != NULL; n++) {
207 char *target_name = binding_workspace_names[n];
208 /* Ensure that this workspace is not assigned to a different output —
209 * otherwise we would create it, then move it over to its output, then
210 * find a new workspace, etc… */
211 bool assigned = false;
212 struct Workspace_Assignment *assignment;
213 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
214 if (strcmp(assignment->name, target_name) != 0 ||
215 strcmp(assignment->output, output_primary_name(output)) == 0)
225 exists = (get_existing_workspace_by_name(target_name) != NULL);
227 ws->name = sstrdup(target_name);
228 /* Set ->num to the number of the workspace, if the name actually
229 * is a number or starts with a number */
230 ws->num = ws_name_to_number(ws->name);
231 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
238 /* get the next unused workspace number */
239 DLOG("Getting next unused workspace by number\n");
242 exists = (get_existing_workspace_by_num(++c) != NULL);
243 DLOG("result for ws %d: exists = %d\n", c, exists);
246 sasprintf(&(ws->name), "%d", c);
248 con_attach(ws, content, false);
250 sasprintf(&name, "[i3 con] workspace %s", ws->name);
251 x_set_name(ws, name);
254 ws->fullscreen_mode = CF_OUTPUT;
256 ws->workspace_layout = config.default_layout;
257 _workspace_apply_default_orientation(ws);
263 * Returns true if the workspace is currently visible. Especially important for
264 * multi-monitor environments, as they can have multiple currenlty active
268 bool workspace_is_visible(Con *ws) {
269 Con *output = con_get_output(ws);
272 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
273 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
278 * XXX: we need to clean up all this recursive walking code.
281 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
284 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
285 if (current != exclude &&
286 current->sticky_group != NULL &&
287 current->window != NULL &&
288 strcmp(current->sticky_group, sticky_group) == 0)
291 Con *recurse = _get_sticky(current, sticky_group, exclude);
296 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
297 if (current != exclude &&
298 current->sticky_group != NULL &&
299 current->window != NULL &&
300 strcmp(current->sticky_group, sticky_group) == 0)
303 Con *recurse = _get_sticky(current, sticky_group, exclude);
312 * Reassigns all child windows in sticky containers. Called when the user
313 * changes workspaces.
315 * XXX: what about sticky containers which contain containers?
318 static void workspace_reassign_sticky(Con *con) {
320 /* 1: go through all containers */
322 /* handle all children and floating windows of this node */
323 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
324 if (current->sticky_group == NULL) {
325 workspace_reassign_sticky(current);
329 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
330 /* 2: find a window which we can re-assign */
331 Con *output = con_get_output(current);
332 Con *src = _get_sticky(output, current->sticky_group, current);
335 LOG("No window found for this sticky group\n");
336 workspace_reassign_sticky(current);
340 x_move_win(src, current);
341 current->window = src->window;
342 current->mapped = true;
346 x_reparent_child(current, src);
348 LOG("re-assigned window from src %p to dest %p\n", src, current);
351 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
352 workspace_reassign_sticky(current);
356 * Callback to reset the urgent flag of the given con to false. May be started by
357 * workspace_show to avoid urgency hints being lost by switching to a workspace
361 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
364 ev_timer_stop(main_loop, con->urgency_timer);
365 FREE(con->urgency_timer);
368 DLOG("Resetting urgency flag of con %p by timer\n", con);
369 con_set_urgency(con, false);
370 con_update_parents_urgency(con);
371 workspace_update_urgent_flag(con_get_workspace(con));
372 ipc_send_window_event("urgent", con);
378 * Switches to the given workspace
381 void workspace_show(Con *workspace) {
382 Con *current, *old = NULL;
383 Con *old_focus = focused;
385 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
386 if (con_is_internal(workspace))
389 /* disable fullscreen for the other workspaces and get the workspace we are
391 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
392 if (current->fullscreen_mode == CF_OUTPUT)
394 current->fullscreen_mode = CF_NONE;
397 /* enable fullscreen for the target workspace. If it happens to be the
398 * same one we are currently on anyways, we can stop here. */
399 workspace->fullscreen_mode = CF_OUTPUT;
400 current = con_get_workspace(focused);
401 if (workspace == current) {
402 DLOG("Not switching, already there.\n");
406 /* Remember currently focused workspace for switching back to it later with
407 * the 'workspace back_and_forth' command.
408 * NOTE: We have to duplicate the name as the original will be freed when
409 * the corresponding workspace is cleaned up.
410 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
411 * focused) are skipped, see bug #868. */
412 if (current && !con_is_internal(current)) {
413 FREE(previous_workspace_name);
414 previous_workspace_name = sstrdup(current->name);
415 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
418 workspace_reassign_sticky(workspace);
420 DLOG("switching to %p / %s\n", workspace, workspace->name);
421 Con *next = con_descend_focused(workspace);
423 /* Memorize current output */
424 Con *old_output = con_get_output(focused);
426 /* Display urgency hint for a while if the newly visible workspace would
427 * focus and thereby immediately destroy it */
428 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
430 next->urgent = false;
433 /* … but immediately reset urgency flags; they will be set to false by
434 * the timer callback in case the container is focused at the time of
436 focused->urgent = true;
437 workspace->urgent = true;
439 if (focused->urgency_timer == NULL) {
440 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
442 focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
443 /* use a repeating timer to allow for easy resets */
444 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
445 config.workspace_urgency_timer, config.workspace_urgency_timer);
446 focused->urgency_timer->data = focused;
447 ev_timer_start(main_loop, focused->urgency_timer);
449 DLOG("Resetting urgency timer of con %p on workspace %p\n",
451 ev_timer_again(main_loop, focused->urgency_timer);
456 ipc_send_workspace_event("focus", workspace, current);
458 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
459 /* Close old workspace if necessary. This must be done *after* doing
460 * urgency handling, because tree_close_internal() will do a con_focus() on the next
461 * client, which will clear the urgency flag too early. Also, there is no
462 * way for con_focus() to know about when to clear urgency immediately and
463 * when to defer it. */
464 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
465 /* check if this workspace is currently visible */
466 if (!workspace_is_visible(old)) {
467 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
468 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
469 tree_close_internal(old, DONT_KILL_WINDOW, false, false);
471 const unsigned char *payload;
473 y(get_buf, &payload, &length);
474 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
478 /* Avoid calling output_push_sticky_windows later with a freed container. */
479 if (old == old_focus) {
483 ewmh_update_number_of_desktops();
484 ewmh_update_desktop_names();
485 ewmh_update_desktop_viewport();
486 ewmh_update_wm_desktop();
490 workspace->fullscreen_mode = CF_OUTPUT;
491 LOG("focused now = %p / %s\n", focused, focused->name);
493 /* Set mouse pointer */
494 Con *new_output = con_get_output(focused);
495 if (old_output != new_output) {
496 x_set_warp_to(&next->rect);
499 /* Update the EWMH hints */
500 ewmh_update_current_desktop();
502 /* Push any sticky windows to the now visible workspace. */
503 output_push_sticky_windows(old_focus);
507 * Looks up the workspace by name and switches to it.
510 void workspace_show_by_name(const char *num) {
512 workspace = workspace_get(num, NULL);
513 workspace_show(workspace);
517 * Focuses the next workspace.
520 Con *workspace_next(void) {
521 Con *current = con_get_workspace(focused);
522 Con *next = NULL, *first = NULL, *first_opposite = NULL;
525 if (current->num == -1) {
526 /* If currently a named workspace, find next named workspace. */
527 if ((next = TAILQ_NEXT(current, nodes)) != NULL)
529 bool found_current = false;
530 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
531 /* Skip outputs starting with __, they are internal. */
532 if (con_is_internal(output))
534 NODES_FOREACH(output_get_content(output)) {
535 if (child->type != CT_WORKSPACE)
539 if (!first_opposite || (child->num != -1 && child->num < first_opposite->num))
540 first_opposite = child;
541 if (child == current) {
542 found_current = true;
543 } else if (child->num == -1 && found_current) {
550 /* If currently a numbered workspace, find next numbered workspace. */
551 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
552 /* Skip outputs starting with __, they are internal. */
553 if (con_is_internal(output))
555 NODES_FOREACH(output_get_content(output)) {
556 if (child->type != CT_WORKSPACE)
558 if (!first || (child->num != -1 && child->num < first->num))
560 if (!first_opposite && child->num == -1)
561 first_opposite = child;
562 if (child->num == -1)
564 /* Need to check child against current and next because we are
565 * traversing multiple lists and thus are not guaranteed the
566 * relative order between the list of workspaces. */
567 if (current->num < child->num && (!next || child->num < next->num))
574 next = first_opposite ? first_opposite : first;
580 * Focuses the previous workspace.
583 Con *workspace_prev(void) {
584 Con *current = con_get_workspace(focused);
585 Con *prev = NULL, *first_opposite = NULL, *last = NULL;
588 if (current->num == -1) {
589 /* If named workspace, find previous named workspace. */
590 prev = TAILQ_PREV(current, nodes_head, nodes);
591 if (prev && prev->num != -1)
594 bool found_current = false;
595 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
596 /* Skip outputs starting with __, they are internal. */
597 if (con_is_internal(output))
599 NODES_FOREACH_REVERSE(output_get_content(output)) {
600 if (child->type != CT_WORKSPACE)
604 if (!first_opposite || (child->num != -1 && child->num > first_opposite->num))
605 first_opposite = child;
606 if (child == current) {
607 found_current = true;
608 } else if (child->num == -1 && found_current) {
616 /* If numbered workspace, find previous numbered workspace. */
617 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
618 /* Skip outputs starting with __, they are internal. */
619 if (con_is_internal(output))
621 NODES_FOREACH_REVERSE(output_get_content(output)) {
622 if (child->type != CT_WORKSPACE)
624 if (!last || (child->num != -1 && last->num < child->num))
626 if (!first_opposite && child->num == -1)
627 first_opposite = child;
628 if (child->num == -1)
630 /* Need to check child against current and previous because we
631 * are traversing multiple lists and thus are not guaranteed
632 * the relative order between the list of workspaces. */
633 if (current->num > child->num && (!prev || child->num > prev->num))
640 prev = first_opposite ? first_opposite : last;
646 * Focuses the next workspace on the same output.
649 Con *workspace_next_on_output(void) {
650 Con *current = con_get_workspace(focused);
652 Con *output = con_get_output(focused);
654 if (current->num == -1) {
655 /* If currently a named workspace, find next named workspace. */
656 next = TAILQ_NEXT(current, nodes);
658 /* If currently a numbered workspace, find next numbered workspace. */
659 NODES_FOREACH(output_get_content(output)) {
660 if (child->type != CT_WORKSPACE)
662 if (child->num == -1)
664 /* Need to check child against current and next because we are
665 * traversing multiple lists and thus are not guaranteed the
666 * relative order between the list of workspaces. */
667 if (current->num < child->num && (!next || child->num < next->num))
672 /* Find next named workspace. */
674 bool found_current = false;
675 NODES_FOREACH(output_get_content(output)) {
676 if (child->type != CT_WORKSPACE)
678 if (child == current) {
679 found_current = true;
680 } else if (child->num == -1 && (current->num != -1 || found_current)) {
682 goto workspace_next_on_output_end;
687 /* Find first workspace. */
689 NODES_FOREACH(output_get_content(output)) {
690 if (child->type != CT_WORKSPACE)
692 if (!next || (child->num != -1 && child->num < next->num))
696 workspace_next_on_output_end:
701 * Focuses the previous workspace on same output.
704 Con *workspace_prev_on_output(void) {
705 Con *current = con_get_workspace(focused);
707 Con *output = con_get_output(focused);
708 DLOG("output = %s\n", output->name);
710 if (current->num == -1) {
711 /* If named workspace, find previous named workspace. */
712 prev = TAILQ_PREV(current, nodes_head, nodes);
713 if (prev && prev->num != -1)
716 /* If numbered workspace, find previous numbered workspace. */
717 NODES_FOREACH_REVERSE(output_get_content(output)) {
718 if (child->type != CT_WORKSPACE || child->num == -1)
720 /* Need to check child against current and previous because we
721 * are traversing multiple lists and thus are not guaranteed
722 * the relative order between the list of workspaces. */
723 if (current->num > child->num && (!prev || child->num > prev->num))
728 /* Find previous named workspace. */
730 bool found_current = false;
731 NODES_FOREACH_REVERSE(output_get_content(output)) {
732 if (child->type != CT_WORKSPACE)
734 if (child == current) {
735 found_current = true;
736 } else if (child->num == -1 && (current->num != -1 || found_current)) {
738 goto workspace_prev_on_output_end;
743 /* Find last workspace. */
745 NODES_FOREACH_REVERSE(output_get_content(output)) {
746 if (child->type != CT_WORKSPACE)
748 if (!prev || child->num > prev->num)
753 workspace_prev_on_output_end:
758 * Focuses the previously focused workspace.
761 void workspace_back_and_forth(void) {
762 if (!previous_workspace_name) {
763 DLOG("No previous workspace name set. Not switching.\n");
767 workspace_show_by_name(previous_workspace_name);
771 * Returns the previously focused workspace con, or NULL if unavailable.
774 Con *workspace_back_and_forth_get(void) {
775 if (!previous_workspace_name) {
776 DLOG("No previous workspace name set.\n");
781 workspace = workspace_get(previous_workspace_name, NULL);
786 static bool get_urgency_flag(Con *con) {
788 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
789 if (child->urgent || get_urgency_flag(child))
792 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
793 if (child->urgent || get_urgency_flag(child))
800 * Goes through all clients on the given workspace and updates the workspace’s
801 * urgent flag accordingly.
804 void workspace_update_urgent_flag(Con *ws) {
805 bool old_flag = ws->urgent;
806 ws->urgent = get_urgency_flag(ws);
807 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
809 if (old_flag != ws->urgent)
810 ipc_send_workspace_event("urgent", ws, NULL);
814 * 'Forces' workspace orientation by moving all cons into a new split-con with
815 * the same layout as the workspace and then changing the workspace layout.
818 void ws_force_orientation(Con *ws, orientation_t orientation) {
819 /* 1: create a new split container */
820 Con *split = con_new(NULL, NULL);
823 /* 2: copy layout from workspace */
824 split->layout = ws->layout;
826 /* 3: move the existing cons of this workspace below the new con */
827 Con **focus_order = get_focus_order(ws);
829 DLOG("Moving cons\n");
830 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
831 Con *child = TAILQ_FIRST(&(ws->nodes_head));
833 con_attach(child, split, true);
836 set_focus_order(split, focus_order);
839 /* 4: switch workspace layout */
840 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
841 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
843 /* 5: attach the new split container to the workspace */
844 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
845 con_attach(split, ws, false);
847 /* 6: fix the percentages */
852 * Called when a new con (with a window, not an empty or split con) should be
853 * attached to the workspace (for example when managing a new window or when
854 * moving an existing window to the workspace level).
856 * Depending on the workspace_layout setting, this function either returns the
857 * workspace itself (default layout) or creates a new stacked/tabbed con and
861 Con *workspace_attach_to(Con *ws) {
862 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
864 if (ws->workspace_layout == L_DEFAULT) {
865 DLOG("Default layout, just attaching it to the workspace itself.\n");
869 DLOG("Non-default layout, creating a new split container\n");
870 /* 1: create a new split container */
871 Con *new = con_new(NULL, NULL);
874 /* 2: set the requested layout on the split con */
875 new->layout = ws->workspace_layout;
877 /* 4: attach the new split container to the workspace */
878 DLOG("Attaching new split %p to workspace %p\n", new, ws);
879 con_attach(new, ws, false);
881 /* 5: fix the percentages */
888 * Creates a new container and re-parents all of children from the given
891 * The container inherits the layout from the workspace.
893 Con *workspace_encapsulate(Con *ws) {
894 if (TAILQ_EMPTY(&(ws->nodes_head))) {
895 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
899 Con *new = con_new(NULL, NULL);
901 new->layout = ws->layout;
903 Con **focus_order = get_focus_order(ws);
905 DLOG("Moving children of workspace %p / %s into container %p\n",
908 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
909 child = TAILQ_FIRST(&(ws->nodes_head));
911 con_attach(child, new, true);
914 set_focus_order(new, focus_order);
917 con_attach(new, ws, true);
923 * Move the given workspace to the specified output.
924 * This returns true if and only if moving the workspace was successful.
926 bool workspace_move_to_output(Con *ws, Output *output) {
927 LOG("Trying to move workspace %p / %s to output %p / \"%s\".\n", ws, ws->name, output, output_primary_name(output));
929 Output *current_output = get_output_for_con(ws);
930 if (current_output == NULL) {
931 ELOG("Cannot get current output. This is a bug in i3.\n");
935 Con *content = output_get_content(output->con);
936 LOG("got output %p with content %p\n", output, content);
938 Con *previously_visible_ws = TAILQ_FIRST(&(content->focus_head));
939 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
941 bool workspace_was_visible = workspace_is_visible(ws);
942 if (con_num_children(ws->parent) == 1) {
943 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
945 /* check if we can find a workspace assigned to this output */
946 bool used_assignment = false;
947 struct Workspace_Assignment *assignment;
948 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
949 if (assignment->output == NULL || strcmp(assignment->output, output_primary_name(current_output)) != 0)
951 /* check if this workspace is already attached to the tree */
952 if (get_existing_workspace_by_name(assignment->name) != NULL) {
956 /* so create the workspace referenced to by this assignment */
957 LOG("Creating workspace from assignment %s.\n", assignment->name);
958 workspace_get(assignment->name, NULL);
959 used_assignment = true;
963 /* if we couldn't create the workspace using an assignment, create
964 * it on the output */
965 if (!used_assignment)
966 create_workspace_on_output(current_output, ws->parent);
968 /* notify the IPC listeners */
969 ipc_send_workspace_event("init", ws, NULL);
973 /* detach from the old output and attach to the new output */
974 Con *old_content = ws->parent;
976 if (workspace_was_visible) {
977 /* The workspace which we just detached was visible, so focus
978 * the next one in the focus-stack. */
979 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
980 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
981 workspace_show(focus_ws);
983 con_attach(ws, content, false);
985 /* fix the coordinates of the floating containers */
987 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
988 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
990 ipc_send_workspace_event("move", ws, NULL);
991 if (workspace_was_visible) {
992 /* Focus the moved workspace on the destination output. */
996 /* NB: We cannot simply work with previously_visible_ws since it might
997 * have been cleaned up by workspace_show() already, depending on the
998 * focus order/number of other workspaces on the output.
999 * Instead, we loop through the available workspaces and only work with
1000 * previously_visible_ws if we still find it. */
1001 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1002 if (ws != previously_visible_ws)
1005 /* Call the on_remove_child callback of the workspace which previously
1006 * was visible on the destination output. Since it is no longer
1007 * visible, it might need to get cleaned up. */
1008 CALL(previously_visible_ws, on_remove_child);