2 #define I3__FILE__ "workspace.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
9 * workspace.c: Modifying workspaces, accessing them, moving containers to
14 #include "yajl_utils.h"
16 /* Stores a copy of the name of the last used workspace for the workspace
17 * back-and-forth switching. */
18 static char *previous_workspace_name = NULL;
21 * Sets ws->layout to splith/splitv if default_orientation was specified in the
22 * configfile. Otherwise, it uses splith/splitv depending on whether the output
23 * is higher than wide.
26 static void _workspace_apply_default_orientation(Con *ws) {
27 /* If default_orientation is set to NO_ORIENTATION we determine
28 * orientation depending on output resolution. */
29 if (config.default_orientation == NO_ORIENTATION) {
30 Con *output = con_get_output(ws);
31 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
32 ws->rect = output->rect;
33 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
34 output->rect.width, output->rect.height, ws->layout);
36 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
41 * Returns a pointer to the workspace with the given number (starting at 0),
42 * creating the workspace if necessary (by allocating the necessary amount of
43 * memory and initializing the data structures correctly).
46 Con *workspace_get(const char *num, bool *created) {
47 Con *output, *workspace = NULL;
49 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
50 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
52 if (workspace == NULL) {
53 LOG("Creating new workspace \"%s\"\n", num);
54 /* unless an assignment is found, we will create this workspace on the current output */
55 output = con_get_output(focused);
56 /* look for assignments */
57 struct Workspace_Assignment *assignment;
59 /* We set workspace->num to the number if this workspace’s name begins
60 * with a positive number. Otherwise it’s a named ws and num will be
62 long parsed_num = ws_name_to_number(num);
64 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
65 if (strcmp(assignment->name, num) == 0) {
66 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
67 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
69 } else if (parsed_num != -1 && name_is_digits(assignment->name) && ws_name_to_number(assignment->name) == parsed_num) {
70 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
71 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
75 Con *content = output_get_content(output);
76 LOG("got output %p with content %p\n", output, content);
77 /* We need to attach this container after setting its type. con_attach
78 * will handle CT_WORKSPACEs differently */
79 workspace = con_new(NULL, NULL);
81 sasprintf(&name, "[i3 con] workspace %s", num);
82 x_set_name(workspace, name);
84 workspace->type = CT_WORKSPACE;
85 FREE(workspace->name);
86 workspace->name = sstrdup(num);
87 workspace->workspace_layout = config.default_layout;
88 workspace->num = parsed_num;
89 LOG("num = %d\n", workspace->num);
91 workspace->parent = content;
92 _workspace_apply_default_orientation(workspace);
94 con_attach(workspace, content, false);
96 ipc_send_workspace_event("init", workspace, NULL);
97 ewmh_update_number_of_desktops();
98 ewmh_update_desktop_names();
99 ewmh_update_desktop_viewport();
102 } else if (created != NULL) {
110 * Returns a pointer to a new workspace in the given output. The workspace
111 * is created attached to the tree hierarchy through the given content
115 Con *create_workspace_on_output(Output *output, Con *content) {
116 /* add a workspace to this output */
120 Con *ws = con_new(NULL, NULL);
121 ws->type = CT_WORKSPACE;
123 /* try the configured workspace bindings first to find a free name */
125 TAILQ_FOREACH(bind, bindings, bindings) {
126 DLOG("binding with command %s\n", bind->command);
127 if (strlen(bind->command) < strlen("workspace ") ||
128 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
130 DLOG("relevant command = %s\n", bind->command);
131 const char *target = bind->command + strlen("workspace ");
132 while (*target == ' ' || *target == '\t')
134 /* We check if this is the workspace
135 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
136 * Beware: The workspace names "next", "prev", "next_on_output",
137 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
138 * so we check before stripping the double quotes */
139 if (strncasecmp(target, "next", strlen("next")) == 0 ||
140 strncasecmp(target, "prev", strlen("prev")) == 0 ||
141 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
142 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
143 strncasecmp(target, "number", strlen("number")) == 0 ||
144 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
145 strncasecmp(target, "current", strlen("current")) == 0)
147 char *target_name = parse_string(&target, false);
148 if (target_name == NULL)
150 if (strncasecmp(target_name, "__", strlen("__")) == 0) {
151 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
156 ws->name = target_name;
157 DLOG("trying name *%s*\n", ws->name);
159 /* Ensure that this workspace is not assigned to a different output —
160 * otherwise we would create it, then move it over to its output, then
161 * find a new workspace, etc… */
162 bool assigned = false;
163 struct Workspace_Assignment *assignment;
164 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
165 if (strcmp(assignment->name, ws->name) != 0 ||
166 strcmp(assignment->output, output->name) == 0)
177 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
178 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
180 exists = (current != NULL);
182 /* Set ->num to the number of the workspace, if the name actually
183 * is a number or starts with a number */
184 ws->num = ws_name_to_number(ws->name);
185 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
192 /* get the next unused workspace number */
193 DLOG("Getting next unused workspace by number\n");
201 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
202 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
203 exists = (current != NULL);
205 DLOG("result for ws %d: exists = %d\n", c, exists);
207 sasprintf(&(ws->name), "%d", c);
209 con_attach(ws, content, false);
211 sasprintf(&name, "[i3 con] workspace %s", ws->name);
212 x_set_name(ws, name);
215 ws->fullscreen_mode = CF_OUTPUT;
217 ws->workspace_layout = config.default_layout;
218 _workspace_apply_default_orientation(ws);
224 * Returns true if the workspace is currently visible. Especially important for
225 * multi-monitor environments, as they can have multiple currenlty active
229 bool workspace_is_visible(Con *ws) {
230 Con *output = con_get_output(ws);
233 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
234 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
239 * XXX: we need to clean up all this recursive walking code.
242 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
245 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
246 if (current != exclude &&
247 current->sticky_group != NULL &&
248 current->window != NULL &&
249 strcmp(current->sticky_group, sticky_group) == 0)
252 Con *recurse = _get_sticky(current, sticky_group, exclude);
257 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
258 if (current != exclude &&
259 current->sticky_group != NULL &&
260 current->window != NULL &&
261 strcmp(current->sticky_group, sticky_group) == 0)
264 Con *recurse = _get_sticky(current, sticky_group, exclude);
273 * Reassigns all child windows in sticky containers. Called when the user
274 * changes workspaces.
276 * XXX: what about sticky containers which contain containers?
279 static void workspace_reassign_sticky(Con *con) {
281 /* 1: go through all containers */
283 /* handle all children and floating windows of this node */
284 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
285 if (current->sticky_group == NULL) {
286 workspace_reassign_sticky(current);
290 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
291 /* 2: find a window which we can re-assign */
292 Con *output = con_get_output(current);
293 Con *src = _get_sticky(output, current->sticky_group, current);
296 LOG("No window found for this sticky group\n");
297 workspace_reassign_sticky(current);
301 x_move_win(src, current);
302 current->window = src->window;
303 current->mapped = true;
307 x_reparent_child(current, src);
309 LOG("re-assigned window from src %p to dest %p\n", src, current);
312 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
313 workspace_reassign_sticky(current);
317 * Callback to reset the urgent flag of the given con to false. May be started by
318 * _workspace_show to avoid urgency hints being lost by switching to a workspace
322 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
325 ev_timer_stop(main_loop, con->urgency_timer);
326 FREE(con->urgency_timer);
329 DLOG("Resetting urgency flag of con %p by timer\n", con);
330 con_set_urgency(con, false);
331 con_update_parents_urgency(con);
332 workspace_update_urgent_flag(con_get_workspace(con));
333 ipc_send_window_event("urgent", con);
338 static void _workspace_show(Con *workspace) {
339 Con *current, *old = NULL;
341 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
342 if (con_is_internal(workspace))
345 /* disable fullscreen for the other workspaces and get the workspace we are
347 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
348 if (current->fullscreen_mode == CF_OUTPUT)
350 current->fullscreen_mode = CF_NONE;
353 /* enable fullscreen for the target workspace. If it happens to be the
354 * same one we are currently on anyways, we can stop here. */
355 workspace->fullscreen_mode = CF_OUTPUT;
356 current = con_get_workspace(focused);
357 if (workspace == current) {
358 DLOG("Not switching, already there.\n");
362 /* Remember currently focused workspace for switching back to it later with
363 * the 'workspace back_and_forth' command.
364 * NOTE: We have to duplicate the name as the original will be freed when
365 * the corresponding workspace is cleaned up.
366 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
367 * focused) are skipped, see bug #868. */
368 if (current && !con_is_internal(current)) {
369 FREE(previous_workspace_name);
371 previous_workspace_name = sstrdup(current->name);
372 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
376 workspace_reassign_sticky(workspace);
378 DLOG("switching to %p / %s\n", workspace, workspace->name);
379 Con *next = con_descend_focused(workspace);
381 /* Memorize current output */
382 Con *old_output = con_get_output(focused);
384 /* Display urgency hint for a while if the newly visible workspace would
385 * focus and thereby immediately destroy it */
386 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
388 next->urgent = false;
391 /* … but immediately reset urgency flags; they will be set to false by
392 * the timer callback in case the container is focused at the time of
394 focused->urgent = true;
395 workspace->urgent = true;
397 if (focused->urgency_timer == NULL) {
398 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
400 focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
401 /* use a repeating timer to allow for easy resets */
402 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
403 config.workspace_urgency_timer, config.workspace_urgency_timer);
404 focused->urgency_timer->data = focused;
405 ev_timer_start(main_loop, focused->urgency_timer);
407 DLOG("Resetting urgency timer of con %p on workspace %p\n",
409 ev_timer_again(main_loop, focused->urgency_timer);
414 ipc_send_workspace_event("focus", workspace, current);
416 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
417 /* Close old workspace if necessary. This must be done *after* doing
418 * urgency handling, because tree_close() will do a con_focus() on the next
419 * client, which will clear the urgency flag too early. Also, there is no
420 * way for con_focus() to know about when to clear urgency immediately and
421 * when to defer it. */
422 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
423 /* check if this workspace is currently visible */
424 if (!workspace_is_visible(old)) {
425 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
426 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
427 tree_close(old, DONT_KILL_WINDOW, false, false);
429 const unsigned char *payload;
431 y(get_buf, &payload, &length);
432 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
436 ewmh_update_number_of_desktops();
437 ewmh_update_desktop_names();
438 ewmh_update_desktop_viewport();
442 workspace->fullscreen_mode = CF_OUTPUT;
443 LOG("focused now = %p / %s\n", focused, focused->name);
445 /* Set mouse pointer */
446 Con *new_output = con_get_output(focused);
447 if (old_output != new_output) {
448 x_set_warp_to(&next->rect);
451 /* Update the EWMH hints */
452 ewmh_update_current_desktop();
454 /* Floating containers which are sticky need to be moved to the new workspace,
455 * but only on the same output. */
456 if (current != NULL && old_output == new_output) {
460 /* We can't simply iterate over the floating containers since moving a
461 * sticky container to the target workspace will modify that list.
462 * Instead, we first count the number of sticky containers, then memorize
463 * all of them and finally loop over that list to move them to the new
466 TAILQ_FOREACH(child, &(current->floating_head), floating_windows) {
467 if (con_is_sticky(child))
471 Con *sticky_cons[num_sticky];
473 TAILQ_FOREACH(child, &(current->floating_head), floating_windows) {
474 if (con_is_sticky(child))
475 sticky_cons[ctr++] = child;
478 for (int i = 0; i < num_sticky; i++) {
479 con_move_to_workspace(sticky_cons[i], workspace, true, false);
481 /* We want sticky containers to be at the end of the focus head. */
482 TAILQ_REMOVE(&(workspace->focus_head), sticky_cons[i], focused);
483 TAILQ_INSERT_TAIL(&(workspace->focus_head), sticky_cons[i], focused);
486 /* Focus the correct container since moving the sticky containers
487 * changed the focus. However, if no container was focused before,
488 * we can leave the focus at the sticky container. */
495 * Switches to the given workspace
498 void workspace_show(Con *workspace) {
499 _workspace_show(workspace);
503 * Looks up the workspace by name and switches to it.
506 void workspace_show_by_name(const char *num) {
508 workspace = workspace_get(num, NULL);
509 _workspace_show(workspace);
513 * Focuses the next workspace.
516 Con *workspace_next(void) {
517 Con *current = con_get_workspace(focused);
521 if (current->num == -1) {
522 /* If currently a named workspace, find next named workspace. */
523 next = TAILQ_NEXT(current, nodes);
525 /* If currently a numbered workspace, find next numbered workspace. */
526 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
527 /* Skip outputs starting with __, they are internal. */
528 if (con_is_internal(output))
530 NODES_FOREACH(output_get_content(output)) {
531 if (child->type != CT_WORKSPACE)
533 if (child->num == -1)
535 /* Need to check child against current and next because we are
536 * traversing multiple lists and thus are not guaranteed the
537 * relative order between the list of workspaces. */
538 if (current->num < child->num && (!next || child->num < next->num))
544 /* Find next named workspace. */
546 bool found_current = false;
547 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
548 /* Skip outputs starting with __, they are internal. */
549 if (con_is_internal(output))
551 NODES_FOREACH(output_get_content(output)) {
552 if (child->type != CT_WORKSPACE)
554 if (child == current) {
556 } else if (child->num == -1 && (current->num != -1 || found_current)) {
558 goto workspace_next_end;
564 /* Find first workspace. */
566 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
567 /* Skip outputs starting with __, they are internal. */
568 if (con_is_internal(output))
570 NODES_FOREACH(output_get_content(output)) {
571 if (child->type != CT_WORKSPACE)
573 if (!next || (child->num != -1 && child->num < next->num))
583 * Focuses the previous workspace.
586 Con *workspace_prev(void) {
587 Con *current = con_get_workspace(focused);
591 if (current->num == -1) {
592 /* If named workspace, find previous named workspace. */
593 prev = TAILQ_PREV(current, nodes_head, nodes);
594 if (prev && prev->num != -1)
597 /* If numbered workspace, find previous numbered workspace. */
598 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
599 /* Skip outputs starting with __, they are internal. */
600 if (con_is_internal(output))
602 NODES_FOREACH_REVERSE(output_get_content(output)) {
603 if (child->type != CT_WORKSPACE || child->num == -1)
605 /* Need to check child against current and previous because we
606 * are traversing multiple lists and thus are not guaranteed
607 * the relative order between the list of workspaces. */
608 if (current->num > child->num && (!prev || child->num > prev->num))
614 /* Find previous named workspace. */
616 bool found_current = false;
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 (child == current) {
625 found_current = true;
626 } else if (child->num == -1 && (current->num != -1 || found_current)) {
628 goto workspace_prev_end;
634 /* Find last workspace. */
636 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
637 /* Skip outputs starting with __, they are internal. */
638 if (con_is_internal(output))
640 NODES_FOREACH_REVERSE(output_get_content(output)) {
641 if (child->type != CT_WORKSPACE)
643 if (!prev || child->num > prev->num)
654 * Focuses the next workspace on the same output.
657 Con *workspace_next_on_output(void) {
658 Con *current = con_get_workspace(focused);
660 Con *output = con_get_output(focused);
662 if (current->num == -1) {
663 /* If currently a named workspace, find next named workspace. */
664 next = TAILQ_NEXT(current, nodes);
666 /* If currently a numbered workspace, find next numbered workspace. */
667 NODES_FOREACH(output_get_content(output)) {
668 if (child->type != CT_WORKSPACE)
670 if (child->num == -1)
672 /* Need to check child against current and next because we are
673 * traversing multiple lists and thus are not guaranteed the
674 * relative order between the list of workspaces. */
675 if (current->num < child->num && (!next || child->num < next->num))
680 /* Find next named workspace. */
682 bool found_current = false;
683 NODES_FOREACH(output_get_content(output)) {
684 if (child->type != CT_WORKSPACE)
686 if (child == current) {
688 } else if (child->num == -1 && (current->num != -1 || found_current)) {
690 goto workspace_next_on_output_end;
695 /* Find first workspace. */
697 NODES_FOREACH(output_get_content(output)) {
698 if (child->type != CT_WORKSPACE)
700 if (!next || (child->num != -1 && child->num < next->num))
704 workspace_next_on_output_end:
709 * Focuses the previous workspace on same output.
712 Con *workspace_prev_on_output(void) {
713 Con *current = con_get_workspace(focused);
715 Con *output = con_get_output(focused);
716 DLOG("output = %s\n", output->name);
718 if (current->num == -1) {
719 /* If named workspace, find previous named workspace. */
720 prev = TAILQ_PREV(current, nodes_head, nodes);
721 if (prev && prev->num != -1)
724 /* If numbered workspace, find previous numbered workspace. */
725 NODES_FOREACH_REVERSE(output_get_content(output)) {
726 if (child->type != CT_WORKSPACE || child->num == -1)
728 /* Need to check child against current and previous because we
729 * are traversing multiple lists and thus are not guaranteed
730 * the relative order between the list of workspaces. */
731 if (current->num > child->num && (!prev || child->num > prev->num))
736 /* Find previous named workspace. */
738 bool found_current = false;
739 NODES_FOREACH_REVERSE(output_get_content(output)) {
740 if (child->type != CT_WORKSPACE)
742 if (child == current) {
743 found_current = true;
744 } else if (child->num == -1 && (current->num != -1 || found_current)) {
746 goto workspace_prev_on_output_end;
751 /* Find last workspace. */
753 NODES_FOREACH_REVERSE(output_get_content(output)) {
754 if (child->type != CT_WORKSPACE)
756 if (!prev || child->num > prev->num)
761 workspace_prev_on_output_end:
766 * Focuses the previously focused workspace.
769 void workspace_back_and_forth(void) {
770 if (!previous_workspace_name) {
771 DLOG("No previous workspace name set. Not switching.\n");
775 workspace_show_by_name(previous_workspace_name);
779 * Returns the previously focused workspace con, or NULL if unavailable.
782 Con *workspace_back_and_forth_get(void) {
783 if (!previous_workspace_name) {
784 DLOG("No previous workspace name set.\n");
789 workspace = workspace_get(previous_workspace_name, NULL);
794 static bool get_urgency_flag(Con *con) {
796 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
797 if (child->urgent || get_urgency_flag(child))
800 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
801 if (child->urgent || get_urgency_flag(child))
808 * Goes through all clients on the given workspace and updates the workspace’s
809 * urgent flag accordingly.
812 void workspace_update_urgent_flag(Con *ws) {
813 bool old_flag = ws->urgent;
814 ws->urgent = get_urgency_flag(ws);
815 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
817 if (old_flag != ws->urgent)
818 ipc_send_workspace_event("urgent", ws, NULL);
822 * 'Forces' workspace orientation by moving all cons into a new split-con with
823 * the same layout as the workspace and then changing the workspace layout.
826 void ws_force_orientation(Con *ws, orientation_t orientation) {
827 /* 1: create a new split container */
828 Con *split = con_new(NULL, NULL);
831 /* 2: copy layout from workspace */
832 split->layout = ws->layout;
834 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
836 /* 3: move the existing cons of this workspace below the new con */
837 DLOG("Moving cons\n");
838 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
839 Con *child = TAILQ_FIRST(&(ws->nodes_head));
841 con_attach(child, split, true);
844 /* 4: switch workspace layout */
845 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
846 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
848 /* 5: attach the new split container to the workspace */
849 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
850 con_attach(split, ws, false);
852 /* 6: fix the percentages */
856 con_focus(old_focused);
860 * Called when a new con (with a window, not an empty or split con) should be
861 * attached to the workspace (for example when managing a new window or when
862 * moving an existing window to the workspace level).
864 * Depending on the workspace_layout setting, this function either returns the
865 * workspace itself (default layout) or creates a new stacked/tabbed con and
869 Con *workspace_attach_to(Con *ws) {
870 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
872 if (ws->workspace_layout == L_DEFAULT) {
873 DLOG("Default layout, just attaching it to the workspace itself.\n");
877 DLOG("Non-default layout, creating a new split container\n");
878 /* 1: create a new split container */
879 Con *new = con_new(NULL, NULL);
882 /* 2: set the requested layout on the split con */
883 new->layout = ws->workspace_layout;
885 /* 4: attach the new split container to the workspace */
886 DLOG("Attaching new split %p to workspace %p\n", new, ws);
887 con_attach(new, ws, false);
889 /* 5: fix the percentages */
896 * Creates a new container and re-parents all of children from the given
899 * The container inherits the layout from the workspace.
901 Con *workspace_encapsulate(Con *ws) {
902 if (TAILQ_EMPTY(&(ws->nodes_head))) {
903 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
907 Con *new = con_new(NULL, NULL);
909 new->layout = ws->layout;
911 DLOG("Moving children of workspace %p / %s into container %p\n",
915 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
916 child = TAILQ_FIRST(&(ws->nodes_head));
918 con_attach(child, new, true);
921 con_attach(new, ws, true);
927 * Move the given workspace to the specified output.
928 * This returns true if and only if moving the workspace was successful.
930 bool workspace_move_to_output(Con *ws, char *name) {
931 LOG("Trying to move workspace %p / %s to output \"%s\".\n", ws, ws->name, name);
933 Con *current_output_con = con_get_output(ws);
934 if (!current_output_con) {
935 ELOG("Could not get the output container for workspace %p / %s.\n", ws, ws->name);
939 Output *current_output = get_output_by_name(current_output_con->name);
940 if (!current_output) {
941 ELOG("Cannot get current output. This is a bug in i3.\n");
944 Output *output = get_output_from_string(current_output, name);
946 ELOG("Could not get output from string \"%s\"\n", name);
950 Con *content = output_get_content(output->con);
951 LOG("got output %p with content %p\n", output, content);
953 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
954 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
956 bool workspace_was_visible = workspace_is_visible(ws);
957 if (con_num_children(ws->parent) == 1) {
958 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
960 /* check if we can find a workspace assigned to this output */
961 bool used_assignment = false;
962 struct Workspace_Assignment *assignment;
963 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
964 if (assignment->output == NULL || strcmp(assignment->output, current_output->name) != 0)
967 /* check if this workspace is already attached to the tree */
968 Con *workspace = NULL, *out;
969 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
970 GREP_FIRST(workspace, output_get_content(out),
971 !strcasecmp(child->name, assignment->name));
972 if (workspace != NULL)
975 /* so create the workspace referenced to by this assignment */
976 LOG("Creating workspace from assignment %s.\n", assignment->name);
977 workspace_get(assignment->name, NULL);
978 used_assignment = true;
982 /* if we couldn't create the workspace using an assignment, create
983 * it on the output */
984 if (!used_assignment)
985 create_workspace_on_output(current_output, ws->parent);
987 /* notify the IPC listeners */
988 ipc_send_workspace_event("init", ws, NULL);
992 /* detach from the old output and attach to the new output */
993 Con *old_content = ws->parent;
995 if (workspace_was_visible) {
996 /* The workspace which we just detached was visible, so focus
997 * the next one in the focus-stack. */
998 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
999 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1000 workspace_show(focus_ws);
1002 con_attach(ws, content, false);
1004 /* fix the coordinates of the floating containers */
1006 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1007 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1009 ipc_send_workspace_event("move", ws, NULL);
1010 if (workspace_was_visible) {
1011 /* Focus the moved workspace on the destination output. */
1015 /* NB: We cannot simply work with previously_visible_ws since it might
1016 * have been cleaned up by workspace_show() already, depending on the
1017 * focus order/number of other workspaces on the output.
1018 * Instead, we loop through the available workspaces and only work with
1019 * previously_visible_ws if we still find it. */
1020 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1021 if (ws != previously_visible_ws)
1024 /* Call the on_remove_child callback of the workspace which previously
1025 * was visible on the destination output. Since it is no longer
1026 * visible, it might need to get cleaned up. */
1027 CALL(previously_visible_ws, on_remove_child);