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 * Sets ws->layout to splith/splitv if default_orientation was specified in the
24 * configfile. Otherwise, it uses splith/splitv depending on whether the output
25 * is higher than wide.
28 static void _workspace_apply_default_orientation(Con *ws) {
29 /* If default_orientation is set to NO_ORIENTATION we determine
30 * orientation depending on output resolution. */
31 if (config.default_orientation == NO_ORIENTATION) {
32 Con *output = con_get_output(ws);
33 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
34 ws->rect = output->rect;
35 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
36 output->rect.width, output->rect.height, ws->layout);
38 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
43 * Returns a pointer to the workspace with the given number (starting at 0),
44 * creating the workspace if necessary (by allocating the necessary amount of
45 * memory and initializing the data structures correctly).
48 Con *workspace_get(const char *num, bool *created) {
49 Con *output, *workspace = NULL;
51 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
52 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
54 if (workspace == NULL) {
55 LOG("Creating new workspace \"%s\"\n", num);
56 /* unless an assignment is found, we will create this workspace on the current output */
57 output = con_get_output(focused);
58 /* look for assignments */
59 struct Workspace_Assignment *assignment;
61 /* We set workspace->num to the number if this workspace’s name begins
62 * with a positive number. Otherwise it’s a named ws and num will be
64 long parsed_num = ws_name_to_number(num);
66 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
67 if (strcmp(assignment->name, num) == 0) {
68 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
69 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
71 } else if (parsed_num != -1 && name_is_digits(assignment->name) && ws_name_to_number(assignment->name) == parsed_num) {
72 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
73 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
77 Con *content = output_get_content(output);
78 LOG("got output %p with content %p\n", output, content);
79 /* We need to attach this container after setting its type. con_attach
80 * will handle CT_WORKSPACEs differently */
81 workspace = con_new(NULL, NULL);
83 sasprintf(&name, "[i3 con] workspace %s", num);
84 x_set_name(workspace, name);
86 workspace->type = CT_WORKSPACE;
87 FREE(workspace->name);
88 workspace->name = sstrdup(num);
89 workspace->workspace_layout = config.default_layout;
90 workspace->num = parsed_num;
91 LOG("num = %d\n", workspace->num);
93 workspace->parent = content;
94 _workspace_apply_default_orientation(workspace);
96 con_attach(workspace, content, false);
98 ipc_send_workspace_event("init", workspace, NULL);
99 ewmh_update_number_of_desktops();
100 ewmh_update_desktop_names();
101 ewmh_update_desktop_viewport();
102 ewmh_update_wm_desktop();
105 } else if (created != NULL) {
113 * Extracts workspace names from keybindings (e.g. “web” from “bindsym $mod+1
114 * workspace web”), so that when an output needs a workspace, i3 can start with
115 * the first configured one. Needs to be called before reorder_bindings() so
116 * that the config-file order is used, not the i3-internal order.
119 void extract_workspace_names_from_bindings(void) {
122 if (binding_workspace_names != NULL) {
123 for (int i = 0; binding_workspace_names[i] != NULL; i++) {
124 free(binding_workspace_names[i]);
126 FREE(binding_workspace_names);
128 TAILQ_FOREACH(bind, bindings, bindings) {
129 DLOG("binding with command %s\n", bind->command);
130 if (strlen(bind->command) < strlen("workspace ") ||
131 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
133 DLOG("relevant command = %s\n", bind->command);
134 const char *target = bind->command + strlen("workspace ");
135 while (*target == ' ' || *target == '\t')
137 /* We check if this is the workspace
138 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
139 * Beware: The workspace names "next", "prev", "next_on_output",
140 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
141 * so we check before stripping the double quotes */
142 if (strncasecmp(target, "next", strlen("next")) == 0 ||
143 strncasecmp(target, "prev", strlen("prev")) == 0 ||
144 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
145 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
146 strncasecmp(target, "number", strlen("number")) == 0 ||
147 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
148 strncasecmp(target, "current", strlen("current")) == 0)
150 char *target_name = parse_string(&target, false);
151 if (target_name == NULL)
153 if (strncasecmp(target_name, "__", strlen("__")) == 0) {
154 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
158 DLOG("Saving workspace name \"%s\"\n", target_name);
160 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
161 binding_workspace_names[n - 1] = target_name;
163 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
164 binding_workspace_names[n - 1] = NULL;
168 * Returns a pointer to a new workspace in the given output. The workspace
169 * is created attached to the tree hierarchy through the given content
173 Con *create_workspace_on_output(Output *output, Con *content) {
174 /* add a workspace to this output */
178 Con *ws = con_new(NULL, NULL);
179 ws->type = CT_WORKSPACE;
181 /* try the configured workspace bindings first to find a free name */
182 for (int n = 0; binding_workspace_names[n] != NULL; n++) {
183 char *target_name = binding_workspace_names[n];
184 /* Ensure that this workspace is not assigned to a different output —
185 * otherwise we would create it, then move it over to its output, then
186 * find a new workspace, etc… */
187 bool assigned = false;
188 struct Workspace_Assignment *assignment;
189 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
190 if (strcmp(assignment->name, target_name) != 0 ||
191 strcmp(assignment->output, output->name) == 0)
202 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
203 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, target_name));
204 exists = (current != NULL);
206 ws->name = sstrdup(target_name);
207 /* Set ->num to the number of the workspace, if the name actually
208 * is a number or starts with a number */
209 ws->num = ws_name_to_number(ws->name);
210 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
217 /* get the next unused workspace number */
218 DLOG("Getting next unused workspace by number\n");
226 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
227 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
228 exists = (current != NULL);
230 DLOG("result for ws %d: exists = %d\n", c, exists);
232 sasprintf(&(ws->name), "%d", c);
234 con_attach(ws, content, false);
236 sasprintf(&name, "[i3 con] workspace %s", ws->name);
237 x_set_name(ws, name);
240 ws->fullscreen_mode = CF_OUTPUT;
242 ws->workspace_layout = config.default_layout;
243 _workspace_apply_default_orientation(ws);
249 * Returns true if the workspace is currently visible. Especially important for
250 * multi-monitor environments, as they can have multiple currenlty active
254 bool workspace_is_visible(Con *ws) {
255 Con *output = con_get_output(ws);
258 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
259 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
264 * XXX: we need to clean up all this recursive walking code.
267 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
270 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
271 if (current != exclude &&
272 current->sticky_group != NULL &&
273 current->window != NULL &&
274 strcmp(current->sticky_group, sticky_group) == 0)
277 Con *recurse = _get_sticky(current, sticky_group, exclude);
282 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
283 if (current != exclude &&
284 current->sticky_group != NULL &&
285 current->window != NULL &&
286 strcmp(current->sticky_group, sticky_group) == 0)
289 Con *recurse = _get_sticky(current, sticky_group, exclude);
298 * Reassigns all child windows in sticky containers. Called when the user
299 * changes workspaces.
301 * XXX: what about sticky containers which contain containers?
304 static void workspace_reassign_sticky(Con *con) {
306 /* 1: go through all containers */
308 /* handle all children and floating windows of this node */
309 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
310 if (current->sticky_group == NULL) {
311 workspace_reassign_sticky(current);
315 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
316 /* 2: find a window which we can re-assign */
317 Con *output = con_get_output(current);
318 Con *src = _get_sticky(output, current->sticky_group, current);
321 LOG("No window found for this sticky group\n");
322 workspace_reassign_sticky(current);
326 x_move_win(src, current);
327 current->window = src->window;
328 current->mapped = true;
332 x_reparent_child(current, src);
334 LOG("re-assigned window from src %p to dest %p\n", src, current);
337 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
338 workspace_reassign_sticky(current);
342 * Callback to reset the urgent flag of the given con to false. May be started by
343 * _workspace_show to avoid urgency hints being lost by switching to a workspace
347 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
350 ev_timer_stop(main_loop, con->urgency_timer);
351 FREE(con->urgency_timer);
354 DLOG("Resetting urgency flag of con %p by timer\n", con);
355 con_set_urgency(con, false);
356 con_update_parents_urgency(con);
357 workspace_update_urgent_flag(con_get_workspace(con));
358 ipc_send_window_event("urgent", con);
363 static void _workspace_show(Con *workspace) {
364 Con *current, *old = NULL;
365 Con *old_focus = focused;
367 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
368 if (con_is_internal(workspace))
371 /* disable fullscreen for the other workspaces and get the workspace we are
373 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
374 if (current->fullscreen_mode == CF_OUTPUT)
376 current->fullscreen_mode = CF_NONE;
379 /* enable fullscreen for the target workspace. If it happens to be the
380 * same one we are currently on anyways, we can stop here. */
381 workspace->fullscreen_mode = CF_OUTPUT;
382 current = con_get_workspace(focused);
383 if (workspace == current) {
384 DLOG("Not switching, already there.\n");
388 /* Remember currently focused workspace for switching back to it later with
389 * the 'workspace back_and_forth' command.
390 * NOTE: We have to duplicate the name as the original will be freed when
391 * the corresponding workspace is cleaned up.
392 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
393 * focused) are skipped, see bug #868. */
394 if (current && !con_is_internal(current)) {
395 FREE(previous_workspace_name);
397 previous_workspace_name = sstrdup(current->name);
398 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
402 workspace_reassign_sticky(workspace);
404 DLOG("switching to %p / %s\n", workspace, workspace->name);
405 Con *next = con_descend_focused(workspace);
407 /* Memorize current output */
408 Con *old_output = con_get_output(focused);
410 /* Display urgency hint for a while if the newly visible workspace would
411 * focus and thereby immediately destroy it */
412 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
414 next->urgent = false;
417 /* … but immediately reset urgency flags; they will be set to false by
418 * the timer callback in case the container is focused at the time of
420 focused->urgent = true;
421 workspace->urgent = true;
423 if (focused->urgency_timer == NULL) {
424 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
426 focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
427 /* use a repeating timer to allow for easy resets */
428 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
429 config.workspace_urgency_timer, config.workspace_urgency_timer);
430 focused->urgency_timer->data = focused;
431 ev_timer_start(main_loop, focused->urgency_timer);
433 DLOG("Resetting urgency timer of con %p on workspace %p\n",
435 ev_timer_again(main_loop, focused->urgency_timer);
440 ipc_send_workspace_event("focus", workspace, current);
442 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
443 /* Close old workspace if necessary. This must be done *after* doing
444 * urgency handling, because tree_close_internal() will do a con_focus() on the next
445 * client, which will clear the urgency flag too early. Also, there is no
446 * way for con_focus() to know about when to clear urgency immediately and
447 * when to defer it. */
448 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
449 /* check if this workspace is currently visible */
450 if (!workspace_is_visible(old)) {
451 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
452 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
453 tree_close_internal(old, DONT_KILL_WINDOW, false, false);
455 const unsigned char *payload;
457 y(get_buf, &payload, &length);
458 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
462 ewmh_update_number_of_desktops();
463 ewmh_update_desktop_names();
464 ewmh_update_desktop_viewport();
465 ewmh_update_wm_desktop();
469 workspace->fullscreen_mode = CF_OUTPUT;
470 LOG("focused now = %p / %s\n", focused, focused->name);
472 /* Set mouse pointer */
473 Con *new_output = con_get_output(focused);
474 if (old_output != new_output) {
475 x_set_warp_to(&next->rect);
478 /* Update the EWMH hints */
479 ewmh_update_current_desktop();
481 /* Push any sticky windows to the now visible workspace. */
482 output_push_sticky_windows(old_focus);
486 * Switches to the given workspace
489 void workspace_show(Con *workspace) {
490 _workspace_show(workspace);
494 * Looks up the workspace by name and switches to it.
497 void workspace_show_by_name(const char *num) {
499 workspace = workspace_get(num, NULL);
500 _workspace_show(workspace);
504 * Focuses the next workspace.
507 Con *workspace_next(void) {
508 Con *current = con_get_workspace(focused);
509 Con *next = NULL, *first = NULL, *first_opposite = NULL;
512 if (current->num == -1) {
513 /* If currently a named workspace, find next named workspace. */
514 if ((next = TAILQ_NEXT(current, nodes)) != NULL)
516 bool found_current = false;
517 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
518 /* Skip outputs starting with __, they are internal. */
519 if (con_is_internal(output))
521 NODES_FOREACH(output_get_content(output)) {
522 if (child->type != CT_WORKSPACE)
526 if (!first_opposite || (child->num != -1 && child->num < first_opposite->num))
527 first_opposite = child;
528 if (child == current) {
529 found_current = true;
530 } else if (child->num == -1 && found_current) {
537 /* If currently a numbered workspace, find next numbered workspace. */
538 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
539 /* Skip outputs starting with __, they are internal. */
540 if (con_is_internal(output))
542 NODES_FOREACH(output_get_content(output)) {
543 if (child->type != CT_WORKSPACE)
545 if (!first || (child->num != -1 && child->num < first->num))
547 if (!first_opposite && child->num == -1)
548 first_opposite = child;
549 if (child->num == -1)
551 /* Need to check child against current and next because we are
552 * traversing multiple lists and thus are not guaranteed the
553 * relative order between the list of workspaces. */
554 if (current->num < child->num && (!next || child->num < next->num))
561 next = first_opposite ? first_opposite : first;
567 * Focuses the previous workspace.
570 Con *workspace_prev(void) {
571 Con *current = con_get_workspace(focused);
572 Con *prev = NULL, *first_opposite = NULL, *last = NULL;
575 if (current->num == -1) {
576 /* If named workspace, find previous named workspace. */
577 prev = TAILQ_PREV(current, nodes_head, nodes);
578 if (prev && prev->num != -1)
581 bool found_current = false;
582 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
583 /* Skip outputs starting with __, they are internal. */
584 if (con_is_internal(output))
586 NODES_FOREACH_REVERSE(output_get_content(output)) {
587 if (child->type != CT_WORKSPACE)
591 if (!first_opposite || (child->num != -1 && child->num > first_opposite->num))
592 first_opposite = child;
593 if (child == current) {
594 found_current = true;
595 } else if (child->num == -1 && found_current) {
603 /* If numbered workspace, find previous numbered workspace. */
604 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
605 /* Skip outputs starting with __, they are internal. */
606 if (con_is_internal(output))
608 NODES_FOREACH_REVERSE(output_get_content(output)) {
609 if (child->type != CT_WORKSPACE)
611 if (!last || (child->num != -1 && last->num < child->num))
613 if (!first_opposite && child->num == -1)
614 first_opposite = child;
615 if (child->num == -1)
617 /* Need to check child against current and previous because we
618 * are traversing multiple lists and thus are not guaranteed
619 * the relative order between the list of workspaces. */
620 if (current->num > child->num && (!prev || child->num > prev->num))
627 prev = first_opposite ? first_opposite : last;
633 * Focuses the next workspace on the same output.
636 Con *workspace_next_on_output(void) {
637 Con *current = con_get_workspace(focused);
639 Con *output = con_get_output(focused);
641 if (current->num == -1) {
642 /* If currently a named workspace, find next named workspace. */
643 next = TAILQ_NEXT(current, nodes);
645 /* If currently a numbered workspace, find next numbered workspace. */
646 NODES_FOREACH(output_get_content(output)) {
647 if (child->type != CT_WORKSPACE)
649 if (child->num == -1)
651 /* Need to check child against current and next because we are
652 * traversing multiple lists and thus are not guaranteed the
653 * relative order between the list of workspaces. */
654 if (current->num < child->num && (!next || child->num < next->num))
659 /* Find next named workspace. */
661 bool found_current = false;
662 NODES_FOREACH(output_get_content(output)) {
663 if (child->type != CT_WORKSPACE)
665 if (child == current) {
666 found_current = true;
667 } else if (child->num == -1 && (current->num != -1 || found_current)) {
669 goto workspace_next_on_output_end;
674 /* Find first workspace. */
676 NODES_FOREACH(output_get_content(output)) {
677 if (child->type != CT_WORKSPACE)
679 if (!next || (child->num != -1 && child->num < next->num))
683 workspace_next_on_output_end:
688 * Focuses the previous workspace on same output.
691 Con *workspace_prev_on_output(void) {
692 Con *current = con_get_workspace(focused);
694 Con *output = con_get_output(focused);
695 DLOG("output = %s\n", output->name);
697 if (current->num == -1) {
698 /* If named workspace, find previous named workspace. */
699 prev = TAILQ_PREV(current, nodes_head, nodes);
700 if (prev && prev->num != -1)
703 /* If numbered workspace, find previous numbered workspace. */
704 NODES_FOREACH_REVERSE(output_get_content(output)) {
705 if (child->type != CT_WORKSPACE || child->num == -1)
707 /* Need to check child against current and previous because we
708 * are traversing multiple lists and thus are not guaranteed
709 * the relative order between the list of workspaces. */
710 if (current->num > child->num && (!prev || child->num > prev->num))
715 /* Find previous named workspace. */
717 bool found_current = false;
718 NODES_FOREACH_REVERSE(output_get_content(output)) {
719 if (child->type != CT_WORKSPACE)
721 if (child == current) {
722 found_current = true;
723 } else if (child->num == -1 && (current->num != -1 || found_current)) {
725 goto workspace_prev_on_output_end;
730 /* Find last workspace. */
732 NODES_FOREACH_REVERSE(output_get_content(output)) {
733 if (child->type != CT_WORKSPACE)
735 if (!prev || child->num > prev->num)
740 workspace_prev_on_output_end:
745 * Focuses the previously focused workspace.
748 void workspace_back_and_forth(void) {
749 if (!previous_workspace_name) {
750 DLOG("No previous workspace name set. Not switching.\n");
754 workspace_show_by_name(previous_workspace_name);
758 * Returns the previously focused workspace con, or NULL if unavailable.
761 Con *workspace_back_and_forth_get(void) {
762 if (!previous_workspace_name) {
763 DLOG("No previous workspace name set.\n");
768 workspace = workspace_get(previous_workspace_name, NULL);
773 static bool get_urgency_flag(Con *con) {
775 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
776 if (child->urgent || get_urgency_flag(child))
779 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
780 if (child->urgent || get_urgency_flag(child))
787 * Goes through all clients on the given workspace and updates the workspace’s
788 * urgent flag accordingly.
791 void workspace_update_urgent_flag(Con *ws) {
792 bool old_flag = ws->urgent;
793 ws->urgent = get_urgency_flag(ws);
794 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
796 if (old_flag != ws->urgent)
797 ipc_send_workspace_event("urgent", ws, NULL);
801 * 'Forces' workspace orientation by moving all cons into a new split-con with
802 * the same layout as the workspace and then changing the workspace layout.
805 void ws_force_orientation(Con *ws, orientation_t orientation) {
806 /* 1: create a new split container */
807 Con *split = con_new(NULL, NULL);
810 /* 2: copy layout from workspace */
811 split->layout = ws->layout;
813 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
815 /* 3: move the existing cons of this workspace below the new con */
816 DLOG("Moving cons\n");
817 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
818 Con *child = TAILQ_FIRST(&(ws->nodes_head));
820 con_attach(child, split, true);
823 /* 4: switch workspace layout */
824 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
825 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
827 /* 5: attach the new split container to the workspace */
828 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
829 con_attach(split, ws, false);
831 /* 6: fix the percentages */
835 con_focus(old_focused);
839 * Called when a new con (with a window, not an empty or split con) should be
840 * attached to the workspace (for example when managing a new window or when
841 * moving an existing window to the workspace level).
843 * Depending on the workspace_layout setting, this function either returns the
844 * workspace itself (default layout) or creates a new stacked/tabbed con and
848 Con *workspace_attach_to(Con *ws) {
849 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
851 if (ws->workspace_layout == L_DEFAULT) {
852 DLOG("Default layout, just attaching it to the workspace itself.\n");
856 DLOG("Non-default layout, creating a new split container\n");
857 /* 1: create a new split container */
858 Con *new = con_new(NULL, NULL);
861 /* 2: set the requested layout on the split con */
862 new->layout = ws->workspace_layout;
864 /* 4: attach the new split container to the workspace */
865 DLOG("Attaching new split %p to workspace %p\n", new, ws);
866 con_attach(new, ws, false);
868 /* 5: fix the percentages */
875 * Creates a new container and re-parents all of children from the given
878 * The container inherits the layout from the workspace.
880 Con *workspace_encapsulate(Con *ws) {
881 if (TAILQ_EMPTY(&(ws->nodes_head))) {
882 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
886 Con *new = con_new(NULL, NULL);
888 new->layout = ws->layout;
890 DLOG("Moving children of workspace %p / %s into container %p\n",
894 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
895 child = TAILQ_FIRST(&(ws->nodes_head));
897 con_attach(child, new, true);
900 con_attach(new, ws, true);
906 * Move the given workspace to the specified output.
907 * This returns true if and only if moving the workspace was successful.
909 bool workspace_move_to_output(Con *ws, const char *name) {
910 LOG("Trying to move workspace %p / %s to output \"%s\".\n", ws, ws->name, name);
912 Output *current_output = get_output_for_con(ws);
913 if (current_output == NULL) {
914 ELOG("Cannot get current output. This is a bug in i3.\n");
918 Output *output = get_output_from_string(current_output, name);
920 ELOG("Could not get output from string \"%s\"\n", name);
924 Con *content = output_get_content(output->con);
925 LOG("got output %p with content %p\n", output, content);
927 Con *previously_visible_ws = TAILQ_FIRST(&(content->focus_head));
928 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
930 bool workspace_was_visible = workspace_is_visible(ws);
931 if (con_num_children(ws->parent) == 1) {
932 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
934 /* check if we can find a workspace assigned to this output */
935 bool used_assignment = false;
936 struct Workspace_Assignment *assignment;
937 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
938 if (assignment->output == NULL || strcmp(assignment->output, current_output->name) != 0)
941 /* check if this workspace is already attached to the tree */
942 Con *workspace = NULL, *out;
943 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
944 GREP_FIRST(workspace, output_get_content(out),
945 !strcasecmp(child->name, assignment->name));
946 if (workspace != NULL)
949 /* so create the workspace referenced to by this assignment */
950 LOG("Creating workspace from assignment %s.\n", assignment->name);
951 workspace_get(assignment->name, NULL);
952 used_assignment = true;
956 /* if we couldn't create the workspace using an assignment, create
957 * it on the output */
958 if (!used_assignment)
959 create_workspace_on_output(current_output, ws->parent);
961 /* notify the IPC listeners */
962 ipc_send_workspace_event("init", ws, NULL);
966 /* detach from the old output and attach to the new output */
967 Con *old_content = ws->parent;
969 if (workspace_was_visible) {
970 /* The workspace which we just detached was visible, so focus
971 * the next one in the focus-stack. */
972 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
973 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
974 workspace_show(focus_ws);
976 con_attach(ws, content, false);
978 /* fix the coordinates of the floating containers */
980 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
981 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
983 ipc_send_workspace_event("move", ws, NULL);
984 if (workspace_was_visible) {
985 /* Focus the moved workspace on the destination output. */
989 /* NB: We cannot simply work with previously_visible_ws since it might
990 * have been cleaned up by workspace_show() already, depending on the
991 * focus order/number of other workspaces on the output.
992 * Instead, we loop through the available workspaces and only work with
993 * previously_visible_ws if we still find it. */
994 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
995 if (ws != previously_visible_ws)
998 /* Call the on_remove_child callback of the workspace which previously
999 * was visible on the destination output. Since it is no longer
1000 * visible, it might need to get cleaned up. */
1001 CALL(previously_visible_ws, on_remove_child);