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;
20 /* NULL-terminated list of workspace names (in order) extracted from
22 static char **binding_workspace_names = NULL;
25 * Sets ws->layout to splith/splitv if default_orientation was specified in the
26 * configfile. Otherwise, it uses splith/splitv depending on whether the output
27 * is higher than wide.
30 static void _workspace_apply_default_orientation(Con *ws) {
31 /* If default_orientation is set to NO_ORIENTATION we determine
32 * orientation depending on output resolution. */
33 if (config.default_orientation == NO_ORIENTATION) {
34 Con *output = con_get_output(ws);
35 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
36 ws->rect = output->rect;
37 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
38 output->rect.width, output->rect.height, ws->layout);
40 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
45 * Returns a pointer to the workspace with the given number (starting at 0),
46 * creating the workspace if necessary (by allocating the necessary amount of
47 * memory and initializing the data structures correctly).
50 Con *workspace_get(const char *num, bool *created) {
51 Con *output, *workspace = NULL;
53 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
54 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
56 if (workspace == NULL) {
57 LOG("Creating new workspace \"%s\"\n", num);
58 /* unless an assignment is found, we will create this workspace on the current output */
59 output = con_get_output(focused);
60 /* look for assignments */
61 struct Workspace_Assignment *assignment;
63 /* We set workspace->num to the number if this workspace’s name begins
64 * with a positive number. Otherwise it’s a named ws and num will be
66 long parsed_num = ws_name_to_number(num);
68 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
69 if (strcmp(assignment->name, num) == 0) {
70 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
71 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
73 } else if (parsed_num != -1 && name_is_digits(assignment->name) && ws_name_to_number(assignment->name) == parsed_num) {
74 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
75 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
79 Con *content = output_get_content(output);
80 LOG("got output %p with content %p\n", output, content);
81 /* We need to attach this container after setting its type. con_attach
82 * will handle CT_WORKSPACEs differently */
83 workspace = con_new(NULL, NULL);
85 sasprintf(&name, "[i3 con] workspace %s", num);
86 x_set_name(workspace, name);
88 workspace->type = CT_WORKSPACE;
89 FREE(workspace->name);
90 workspace->name = sstrdup(num);
91 workspace->workspace_layout = config.default_layout;
92 workspace->num = parsed_num;
93 LOG("num = %d\n", workspace->num);
95 workspace->parent = content;
96 _workspace_apply_default_orientation(workspace);
98 con_attach(workspace, content, false);
100 ipc_send_workspace_event("init", workspace, NULL);
101 ewmh_update_number_of_desktops();
102 ewmh_update_desktop_names();
103 ewmh_update_desktop_viewport();
104 ewmh_update_wm_desktop();
107 } else if (created != NULL) {
115 * Extracts workspace names from keybindings (e.g. “web” from “bindsym $mod+1
116 * workspace web”), so that when an output needs a workspace, i3 can start with
117 * the first configured one. Needs to be called before reorder_bindings() so
118 * that the config-file order is used, not the i3-internal order.
121 void extract_workspace_names_from_bindings(void) {
124 if (binding_workspace_names != NULL) {
125 for (int i = 0; binding_workspace_names[i] != NULL; i++) {
126 free(binding_workspace_names[i]);
128 FREE(binding_workspace_names);
130 TAILQ_FOREACH(bind, bindings, bindings) {
131 DLOG("binding with command %s\n", bind->command);
132 if (strlen(bind->command) < strlen("workspace ") ||
133 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
135 DLOG("relevant command = %s\n", bind->command);
136 const char *target = bind->command + strlen("workspace ");
137 while (*target == ' ' || *target == '\t')
139 /* We check if this is the workspace
140 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
141 * Beware: The workspace names "next", "prev", "next_on_output",
142 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
143 * so we check before stripping the double quotes */
144 if (strncasecmp(target, "next", strlen("next")) == 0 ||
145 strncasecmp(target, "prev", strlen("prev")) == 0 ||
146 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
147 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
148 strncasecmp(target, "number", strlen("number")) == 0 ||
149 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
150 strncasecmp(target, "current", strlen("current")) == 0)
152 char *target_name = parse_string(&target, false);
153 if (target_name == NULL)
155 if (strncasecmp(target_name, "__", strlen("__")) == 0) {
156 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
160 DLOG("Saving workspace name \"%s\"\n", target_name);
162 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
163 binding_workspace_names[n - 1] = target_name;
165 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
166 binding_workspace_names[n - 1] = NULL;
170 * Returns a pointer to a new workspace in the given output. The workspace
171 * is created attached to the tree hierarchy through the given content
175 Con *create_workspace_on_output(Output *output, Con *content) {
176 /* add a workspace to this output */
180 Con *ws = con_new(NULL, NULL);
181 ws->type = CT_WORKSPACE;
183 /* try the configured workspace bindings first to find a free name */
184 for (int n = 0; binding_workspace_names[n] != NULL; n++) {
185 char *target_name = binding_workspace_names[n];
186 /* Ensure that this workspace is not assigned to a different output —
187 * otherwise we would create it, then move it over to its output, then
188 * find a new workspace, etc… */
189 bool assigned = false;
190 struct Workspace_Assignment *assignment;
191 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
192 if (strcmp(assignment->name, target_name) != 0 ||
193 strcmp(assignment->output, output->name) == 0)
204 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
205 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, target_name));
206 exists = (current != NULL);
208 ws->name = sstrdup(target_name);
209 /* Set ->num to the number of the workspace, if the name actually
210 * is a number or starts with a number */
211 ws->num = ws_name_to_number(ws->name);
212 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
219 /* get the next unused workspace number */
220 DLOG("Getting next unused workspace by number\n");
228 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
229 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
230 exists = (current != NULL);
232 DLOG("result for ws %d: exists = %d\n", c, exists);
234 sasprintf(&(ws->name), "%d", c);
236 con_attach(ws, content, false);
238 sasprintf(&name, "[i3 con] workspace %s", ws->name);
239 x_set_name(ws, name);
242 ws->fullscreen_mode = CF_OUTPUT;
244 ws->workspace_layout = config.default_layout;
245 _workspace_apply_default_orientation(ws);
251 * Returns true if the workspace is currently visible. Especially important for
252 * multi-monitor environments, as they can have multiple currenlty active
256 bool workspace_is_visible(Con *ws) {
257 Con *output = con_get_output(ws);
260 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
261 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
266 * XXX: we need to clean up all this recursive walking code.
269 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
272 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
273 if (current != exclude &&
274 current->sticky_group != NULL &&
275 current->window != NULL &&
276 strcmp(current->sticky_group, sticky_group) == 0)
279 Con *recurse = _get_sticky(current, sticky_group, exclude);
284 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
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);
300 * Reassigns all child windows in sticky containers. Called when the user
301 * changes workspaces.
303 * XXX: what about sticky containers which contain containers?
306 static void workspace_reassign_sticky(Con *con) {
308 /* 1: go through all containers */
310 /* handle all children and floating windows of this node */
311 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
312 if (current->sticky_group == NULL) {
313 workspace_reassign_sticky(current);
317 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
318 /* 2: find a window which we can re-assign */
319 Con *output = con_get_output(current);
320 Con *src = _get_sticky(output, current->sticky_group, current);
323 LOG("No window found for this sticky group\n");
324 workspace_reassign_sticky(current);
328 x_move_win(src, current);
329 current->window = src->window;
330 current->mapped = true;
334 x_reparent_child(current, src);
336 LOG("re-assigned window from src %p to dest %p\n", src, current);
339 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
340 workspace_reassign_sticky(current);
344 * Callback to reset the urgent flag of the given con to false. May be started by
345 * _workspace_show to avoid urgency hints being lost by switching to a workspace
349 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
352 ev_timer_stop(main_loop, con->urgency_timer);
353 FREE(con->urgency_timer);
356 DLOG("Resetting urgency flag of con %p by timer\n", con);
357 con_set_urgency(con, false);
358 con_update_parents_urgency(con);
359 workspace_update_urgent_flag(con_get_workspace(con));
360 ipc_send_window_event("urgent", con);
365 static void _workspace_show(Con *workspace) {
366 Con *current, *old = NULL;
367 Con *old_focus = focused;
369 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
370 if (con_is_internal(workspace))
373 /* disable fullscreen for the other workspaces and get the workspace we are
375 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
376 if (current->fullscreen_mode == CF_OUTPUT)
378 current->fullscreen_mode = CF_NONE;
381 /* enable fullscreen for the target workspace. If it happens to be the
382 * same one we are currently on anyways, we can stop here. */
383 workspace->fullscreen_mode = CF_OUTPUT;
384 current = con_get_workspace(focused);
385 if (workspace == current) {
386 DLOG("Not switching, already there.\n");
390 /* Remember currently focused workspace for switching back to it later with
391 * the 'workspace back_and_forth' command.
392 * NOTE: We have to duplicate the name as the original will be freed when
393 * the corresponding workspace is cleaned up.
394 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
395 * focused) are skipped, see bug #868. */
396 if (current && !con_is_internal(current)) {
397 FREE(previous_workspace_name);
399 previous_workspace_name = sstrdup(current->name);
400 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
404 workspace_reassign_sticky(workspace);
406 DLOG("switching to %p / %s\n", workspace, workspace->name);
407 Con *next = con_descend_focused(workspace);
409 /* Memorize current output */
410 Con *old_output = con_get_output(focused);
412 /* Display urgency hint for a while if the newly visible workspace would
413 * focus and thereby immediately destroy it */
414 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
416 next->urgent = false;
419 /* … but immediately reset urgency flags; they will be set to false by
420 * the timer callback in case the container is focused at the time of
422 focused->urgent = true;
423 workspace->urgent = true;
425 if (focused->urgency_timer == NULL) {
426 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
428 focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
429 /* use a repeating timer to allow for easy resets */
430 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
431 config.workspace_urgency_timer, config.workspace_urgency_timer);
432 focused->urgency_timer->data = focused;
433 ev_timer_start(main_loop, focused->urgency_timer);
435 DLOG("Resetting urgency timer of con %p on workspace %p\n",
437 ev_timer_again(main_loop, focused->urgency_timer);
442 ipc_send_workspace_event("focus", workspace, current);
444 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
445 /* Close old workspace if necessary. This must be done *after* doing
446 * urgency handling, because tree_close_internal() will do a con_focus() on the next
447 * client, which will clear the urgency flag too early. Also, there is no
448 * way for con_focus() to know about when to clear urgency immediately and
449 * when to defer it. */
450 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
451 /* check if this workspace is currently visible */
452 if (!workspace_is_visible(old)) {
453 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
454 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
455 tree_close_internal(old, DONT_KILL_WINDOW, false, false);
457 const unsigned char *payload;
459 y(get_buf, &payload, &length);
460 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
464 ewmh_update_number_of_desktops();
465 ewmh_update_desktop_names();
466 ewmh_update_desktop_viewport();
467 ewmh_update_wm_desktop();
471 workspace->fullscreen_mode = CF_OUTPUT;
472 LOG("focused now = %p / %s\n", focused, focused->name);
474 /* Set mouse pointer */
475 Con *new_output = con_get_output(focused);
476 if (old_output != new_output) {
477 x_set_warp_to(&next->rect);
480 /* Update the EWMH hints */
481 ewmh_update_current_desktop();
483 /* Push any sticky windows to the now visible workspace. */
484 output_push_sticky_windows(old_focus);
488 * Switches to the given workspace
491 void workspace_show(Con *workspace) {
492 _workspace_show(workspace);
496 * Looks up the workspace by name and switches to it.
499 void workspace_show_by_name(const char *num) {
501 workspace = workspace_get(num, NULL);
502 _workspace_show(workspace);
506 * Focuses the next workspace.
509 Con *workspace_next(void) {
510 Con *current = con_get_workspace(focused);
511 Con *next = NULL, *first = NULL, *first_opposite = NULL;
514 if (current->num == -1) {
515 /* If currently a named workspace, find next named workspace. */
516 if ((next = TAILQ_NEXT(current, nodes)) != NULL)
518 bool found_current = false;
519 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
520 /* Skip outputs starting with __, they are internal. */
521 if (con_is_internal(output))
523 NODES_FOREACH(output_get_content(output)) {
524 if (child->type != CT_WORKSPACE)
528 if (!first_opposite && child->num != -1)
529 first_opposite = child;
530 if (child == current) {
531 found_current = true;
532 } else if (child->num == -1 && found_current) {
539 /* If currently a numbered workspace, find next numbered workspace. */
540 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
541 /* Skip outputs starting with __, they are internal. */
542 if (con_is_internal(output))
544 NODES_FOREACH(output_get_content(output)) {
545 if (child->type != CT_WORKSPACE)
549 if (!first_opposite && child->num == -1)
550 first_opposite = child;
551 if (child->num == -1)
553 /* Need to check child against current and next because we are
554 * traversing multiple lists and thus are not guaranteed the
555 * relative order between the list of workspaces. */
556 if (current->num < child->num && (!next || child->num < next->num))
563 next = first_opposite ? first_opposite : first;
569 * Focuses the previous workspace.
572 Con *workspace_prev(void) {
573 Con *current = con_get_workspace(focused);
574 Con *prev = NULL, *first_opposite = NULL, *last = NULL;
577 if (current->num == -1) {
578 /* If named workspace, find previous named workspace. */
579 prev = TAILQ_PREV(current, nodes_head, nodes);
580 if (prev && prev->num != -1)
583 bool found_current = false;
584 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
585 /* Skip outputs starting with __, they are internal. */
586 if (con_is_internal(output))
588 NODES_FOREACH_REVERSE(output_get_content(output)) {
589 if (child->type != CT_WORKSPACE)
593 if (!first_opposite && child->num != -1)
594 first_opposite = child;
595 if (child == current) {
596 found_current = true;
597 } else if (child->num == -1 && found_current) {
599 goto workspace_prev_end;
605 /* If numbered workspace, find previous numbered workspace. */
606 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
607 /* Skip outputs starting with __, they are internal. */
608 if (con_is_internal(output))
610 NODES_FOREACH_REVERSE(output_get_content(output)) {
611 if (child->type != CT_WORKSPACE)
615 if (!first_opposite && child->num == -1)
616 first_opposite = child;
617 if (child->num == -1)
619 /* Need to check child against current and previous because we
620 * are traversing multiple lists and thus are not guaranteed
621 * the relative order between the list of workspaces. */
622 if (current->num > child->num && (!prev || child->num > prev->num))
629 prev = first_opposite ? first_opposite : last;
636 * Focuses the next workspace on the same output.
639 Con *workspace_next_on_output(void) {
640 Con *current = con_get_workspace(focused);
642 Con *output = con_get_output(focused);
644 if (current->num == -1) {
645 /* If currently a named workspace, find next named workspace. */
646 next = TAILQ_NEXT(current, nodes);
648 /* If currently a numbered workspace, find next numbered workspace. */
649 NODES_FOREACH(output_get_content(output)) {
650 if (child->type != CT_WORKSPACE)
652 if (child->num == -1)
654 /* Need to check child against current and next because we are
655 * traversing multiple lists and thus are not guaranteed the
656 * relative order between the list of workspaces. */
657 if (current->num < child->num && (!next || child->num < next->num))
662 /* Find next named workspace. */
664 bool found_current = false;
665 NODES_FOREACH(output_get_content(output)) {
666 if (child->type != CT_WORKSPACE)
668 if (child == current) {
669 found_current = true;
670 } else if (child->num == -1 && (current->num != -1 || found_current)) {
672 goto workspace_next_on_output_end;
677 /* Find first workspace. */
679 NODES_FOREACH(output_get_content(output)) {
680 if (child->type != CT_WORKSPACE)
682 if (!next || (child->num != -1 && child->num < next->num))
686 workspace_next_on_output_end:
691 * Focuses the previous workspace on same output.
694 Con *workspace_prev_on_output(void) {
695 Con *current = con_get_workspace(focused);
697 Con *output = con_get_output(focused);
698 DLOG("output = %s\n", output->name);
700 if (current->num == -1) {
701 /* If named workspace, find previous named workspace. */
702 prev = TAILQ_PREV(current, nodes_head, nodes);
703 if (prev && prev->num != -1)
706 /* If numbered workspace, find previous numbered workspace. */
707 NODES_FOREACH_REVERSE(output_get_content(output)) {
708 if (child->type != CT_WORKSPACE || child->num == -1)
710 /* Need to check child against current and previous because we
711 * are traversing multiple lists and thus are not guaranteed
712 * the relative order between the list of workspaces. */
713 if (current->num > child->num && (!prev || child->num > prev->num))
718 /* Find previous named workspace. */
720 bool found_current = false;
721 NODES_FOREACH_REVERSE(output_get_content(output)) {
722 if (child->type != CT_WORKSPACE)
724 if (child == current) {
725 found_current = true;
726 } else if (child->num == -1 && (current->num != -1 || found_current)) {
728 goto workspace_prev_on_output_end;
733 /* Find last workspace. */
735 NODES_FOREACH_REVERSE(output_get_content(output)) {
736 if (child->type != CT_WORKSPACE)
738 if (!prev || child->num > prev->num)
743 workspace_prev_on_output_end:
748 * Focuses the previously focused workspace.
751 void workspace_back_and_forth(void) {
752 if (!previous_workspace_name) {
753 DLOG("No previous workspace name set. Not switching.\n");
757 workspace_show_by_name(previous_workspace_name);
761 * Returns the previously focused workspace con, or NULL if unavailable.
764 Con *workspace_back_and_forth_get(void) {
765 if (!previous_workspace_name) {
766 DLOG("No previous workspace name set.\n");
771 workspace = workspace_get(previous_workspace_name, NULL);
776 static bool get_urgency_flag(Con *con) {
778 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
779 if (child->urgent || get_urgency_flag(child))
782 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
783 if (child->urgent || get_urgency_flag(child))
790 * Goes through all clients on the given workspace and updates the workspace’s
791 * urgent flag accordingly.
794 void workspace_update_urgent_flag(Con *ws) {
795 bool old_flag = ws->urgent;
796 ws->urgent = get_urgency_flag(ws);
797 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
799 if (old_flag != ws->urgent)
800 ipc_send_workspace_event("urgent", ws, NULL);
804 * 'Forces' workspace orientation by moving all cons into a new split-con with
805 * the same layout as the workspace and then changing the workspace layout.
808 void ws_force_orientation(Con *ws, orientation_t orientation) {
809 /* 1: create a new split container */
810 Con *split = con_new(NULL, NULL);
813 /* 2: copy layout from workspace */
814 split->layout = ws->layout;
816 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
818 /* 3: move the existing cons of this workspace below the new con */
819 DLOG("Moving cons\n");
820 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
821 Con *child = TAILQ_FIRST(&(ws->nodes_head));
823 con_attach(child, split, true);
826 /* 4: switch workspace layout */
827 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
828 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
830 /* 5: attach the new split container to the workspace */
831 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
832 con_attach(split, ws, false);
834 /* 6: fix the percentages */
838 con_focus(old_focused);
842 * Called when a new con (with a window, not an empty or split con) should be
843 * attached to the workspace (for example when managing a new window or when
844 * moving an existing window to the workspace level).
846 * Depending on the workspace_layout setting, this function either returns the
847 * workspace itself (default layout) or creates a new stacked/tabbed con and
851 Con *workspace_attach_to(Con *ws) {
852 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
854 if (ws->workspace_layout == L_DEFAULT) {
855 DLOG("Default layout, just attaching it to the workspace itself.\n");
859 DLOG("Non-default layout, creating a new split container\n");
860 /* 1: create a new split container */
861 Con *new = con_new(NULL, NULL);
864 /* 2: set the requested layout on the split con */
865 new->layout = ws->workspace_layout;
867 /* 4: attach the new split container to the workspace */
868 DLOG("Attaching new split %p to workspace %p\n", new, ws);
869 con_attach(new, ws, false);
871 /* 5: fix the percentages */
878 * Creates a new container and re-parents all of children from the given
881 * The container inherits the layout from the workspace.
883 Con *workspace_encapsulate(Con *ws) {
884 if (TAILQ_EMPTY(&(ws->nodes_head))) {
885 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
889 Con *new = con_new(NULL, NULL);
891 new->layout = ws->layout;
893 DLOG("Moving children of workspace %p / %s into container %p\n",
897 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
898 child = TAILQ_FIRST(&(ws->nodes_head));
900 con_attach(child, new, true);
903 con_attach(new, ws, true);
909 * Move the given workspace to the specified output.
910 * This returns true if and only if moving the workspace was successful.
912 bool workspace_move_to_output(Con *ws, const char *name) {
913 LOG("Trying to move workspace %p / %s to output \"%s\".\n", ws, ws->name, name);
915 Con *current_output_con = con_get_output(ws);
916 if (!current_output_con) {
917 ELOG("Could not get the output container for workspace %p / %s.\n", ws, ws->name);
921 Output *current_output = get_output_by_name(current_output_con->name);
922 if (!current_output) {
923 ELOG("Cannot get current output. This is a bug in i3.\n");
926 Output *output = get_output_from_string(current_output, name);
928 ELOG("Could not get output from string \"%s\"\n", name);
932 Con *content = output_get_content(output->con);
933 LOG("got output %p with content %p\n", output, content);
935 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
936 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
938 bool workspace_was_visible = workspace_is_visible(ws);
939 if (con_num_children(ws->parent) == 1) {
940 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
942 /* check if we can find a workspace assigned to this output */
943 bool used_assignment = false;
944 struct Workspace_Assignment *assignment;
945 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
946 if (assignment->output == NULL || strcmp(assignment->output, current_output->name) != 0)
949 /* check if this workspace is already attached to the tree */
950 Con *workspace = NULL, *out;
951 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
952 GREP_FIRST(workspace, output_get_content(out),
953 !strcasecmp(child->name, assignment->name));
954 if (workspace != NULL)
957 /* so create the workspace referenced to by this assignment */
958 LOG("Creating workspace from assignment %s.\n", assignment->name);
959 workspace_get(assignment->name, NULL);
960 used_assignment = true;
964 /* if we couldn't create the workspace using an assignment, create
965 * it on the output */
966 if (!used_assignment)
967 create_workspace_on_output(current_output, ws->parent);
969 /* notify the IPC listeners */
970 ipc_send_workspace_event("init", ws, NULL);
974 /* detach from the old output and attach to the new output */
975 Con *old_content = ws->parent;
977 if (workspace_was_visible) {
978 /* The workspace which we just detached was visible, so focus
979 * the next one in the focus-stack. */
980 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
981 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
982 workspace_show(focus_ws);
984 con_attach(ws, content, false);
986 /* fix the coordinates of the floating containers */
988 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
989 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
991 ipc_send_workspace_event("move", ws, NULL);
992 if (workspace_was_visible) {
993 /* Focus the moved workspace on the destination output. */
997 /* NB: We cannot simply work with previously_visible_ws since it might
998 * have been cleaned up by workspace_show() already, depending on the
999 * focus order/number of other workspaces on the output.
1000 * Instead, we loop through the available workspaces and only work with
1001 * previously_visible_ws if we still find it. */
1002 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1003 if (ws != previously_visible_ws)
1006 /* Call the on_remove_child callback of the workspace which previously
1007 * was visible on the destination output. Since it is no longer
1008 * visible, it might need to get cleaned up. */
1009 CALL(previously_visible_ws, on_remove_child);