2 #define I3__FILE__ "workspace.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 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 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
33 output->rect.width, output->rect.height, ws->layout);
35 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
40 * Returns a pointer to the workspace with the given number (starting at 0),
41 * creating the workspace if necessary (by allocating the necessary amount of
42 * memory and initializing the data structures correctly).
45 Con *workspace_get(const char *num, bool *created) {
46 Con *output, *workspace = NULL;
48 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
49 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
51 if (workspace == NULL) {
52 LOG("Creating new workspace \"%s\"\n", num);
53 /* unless an assignment is found, we will create this workspace on the current output */
54 output = con_get_output(focused);
55 /* look for assignments */
56 struct Workspace_Assignment *assignment;
58 /* We set workspace->num to the number if this workspace’s name begins
59 * with a positive number. Otherwise it’s a named ws and num will be
61 long parsed_num = ws_name_to_number(num);
63 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
64 if (strcmp(assignment->name, num) == 0) {
65 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
66 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
68 } else if (parsed_num != -1 && name_is_digits(assignment->name) && ws_name_to_number(assignment->name) == parsed_num) {
69 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
70 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
74 Con *content = output_get_content(output);
75 LOG("got output %p with content %p\n", output, content);
76 /* We need to attach this container after setting its type. con_attach
77 * will handle CT_WORKSPACEs differently */
78 workspace = con_new(NULL, NULL);
80 sasprintf(&name, "[i3 con] workspace %s", num);
81 x_set_name(workspace, name);
83 workspace->type = CT_WORKSPACE;
84 FREE(workspace->name);
85 workspace->name = sstrdup(num);
86 workspace->workspace_layout = config.default_layout;
87 workspace->num = parsed_num;
88 LOG("num = %d\n", workspace->num);
90 workspace->parent = content;
91 _workspace_apply_default_orientation(workspace);
93 con_attach(workspace, content, false);
95 ipc_send_workspace_event("init", workspace, NULL);
96 ewmh_update_number_of_desktops();
97 ewmh_update_desktop_names();
98 ewmh_update_desktop_viewport();
101 } else if (created != NULL) {
109 * Returns a pointer to a new workspace in the given output. The workspace
110 * is created attached to the tree hierarchy through the given content
114 Con *create_workspace_on_output(Output *output, Con *content) {
115 /* add a workspace to this output */
119 Con *ws = con_new(NULL, NULL);
120 ws->type = CT_WORKSPACE;
122 /* try the configured workspace bindings first to find a free name */
124 TAILQ_FOREACH(bind, bindings, bindings) {
125 DLOG("binding with command %s\n", bind->command);
126 if (strlen(bind->command) < strlen("workspace ") ||
127 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
129 DLOG("relevant command = %s\n", bind->command);
130 const char *target = bind->command + strlen("workspace ");
131 while ((*target == ' ' || *target == '\t') && target != '\0')
133 /* We check if this is the workspace
134 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
135 * Beware: The workspace names "next", "prev", "next_on_output",
136 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
137 * so we check before stripping the double quotes */
138 if (strncasecmp(target, "next", strlen("next")) == 0 ||
139 strncasecmp(target, "prev", strlen("prev")) == 0 ||
140 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
141 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
142 strncasecmp(target, "number", strlen("number")) == 0 ||
143 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
144 strncasecmp(target, "current", strlen("current")) == 0)
146 char *target_name = parse_string(&target, false);
147 if (target_name == NULL)
149 if (strncasecmp(target_name, "__", strlen("__")) == 0) {
150 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
155 ws->name = target_name;
156 DLOG("trying name *%s*\n", ws->name);
158 /* Ensure that this workspace is not assigned to a different output —
159 * otherwise we would create it, then move it over to its output, then
160 * find a new workspace, etc… */
161 bool assigned = false;
162 struct Workspace_Assignment *assignment;
163 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
164 if (strcmp(assignment->name, ws->name) != 0 ||
165 strcmp(assignment->output, output->name) == 0)
176 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
177 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
179 exists = (current != NULL);
181 /* Set ->num to the number of the workspace, if the name actually
182 * is a number or starts with a number */
183 ws->num = ws_name_to_number(ws->name);
184 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
191 /* get the next unused workspace number */
192 DLOG("Getting next unused workspace by number\n");
200 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
201 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
202 exists = (current != NULL);
204 DLOG("result for ws %d: exists = %d\n", c, exists);
206 sasprintf(&(ws->name), "%d", c);
208 con_attach(ws, content, false);
210 sasprintf(&name, "[i3 con] workspace %s", ws->name);
211 x_set_name(ws, name);
214 ws->fullscreen_mode = CF_OUTPUT;
216 ws->workspace_layout = config.default_layout;
217 _workspace_apply_default_orientation(ws);
223 * Returns true if the workspace is currently visible. Especially important for
224 * multi-monitor environments, as they can have multiple currenlty active
228 bool workspace_is_visible(Con *ws) {
229 Con *output = con_get_output(ws);
232 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
233 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
238 * XXX: we need to clean up all this recursive walking code.
241 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
244 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
245 if (current != exclude &&
246 current->sticky_group != NULL &&
247 current->window != NULL &&
248 strcmp(current->sticky_group, sticky_group) == 0)
251 Con *recurse = _get_sticky(current, sticky_group, exclude);
256 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
257 if (current != exclude &&
258 current->sticky_group != NULL &&
259 current->window != NULL &&
260 strcmp(current->sticky_group, sticky_group) == 0)
263 Con *recurse = _get_sticky(current, sticky_group, exclude);
272 * Reassigns all child windows in sticky containers. Called when the user
273 * changes workspaces.
275 * XXX: what about sticky containers which contain containers?
278 static void workspace_reassign_sticky(Con *con) {
280 /* 1: go through all containers */
282 /* handle all children and floating windows of this node */
283 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
284 if (current->sticky_group == NULL) {
285 workspace_reassign_sticky(current);
289 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
290 /* 2: find a window which we can re-assign */
291 Con *output = con_get_output(current);
292 Con *src = _get_sticky(output, current->sticky_group, current);
295 LOG("No window found for this sticky group\n");
296 workspace_reassign_sticky(current);
300 x_move_win(src, current);
301 current->window = src->window;
302 current->mapped = true;
306 x_reparent_child(current, src);
308 LOG("re-assigned window from src %p to dest %p\n", src, current);
311 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
312 workspace_reassign_sticky(current);
316 * Callback to reset the urgent flag of the given con to false. May be started by
317 * _workspace_show to avoid urgency hints being lost by switching to a workspace
321 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
325 DLOG("Resetting urgency flag of con %p by timer\n", con);
327 con_update_parents_urgency(con);
328 workspace_update_urgent_flag(con_get_workspace(con));
329 ipc_send_window_event("urgent", con);
333 ev_timer_stop(main_loop, con->urgency_timer);
334 FREE(con->urgency_timer);
337 static void _workspace_show(Con *workspace) {
338 Con *current, *old = NULL;
340 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
341 if (con_is_internal(workspace))
344 /* disable fullscreen for the other workspaces and get the workspace we are
346 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
347 if (current->fullscreen_mode == CF_OUTPUT)
349 current->fullscreen_mode = CF_NONE;
352 /* enable fullscreen for the target workspace. If it happens to be the
353 * same one we are currently on anyways, we can stop here. */
354 workspace->fullscreen_mode = CF_OUTPUT;
355 current = con_get_workspace(focused);
356 if (workspace == current) {
357 DLOG("Not switching, already there.\n");
361 /* Remember currently focused workspace for switching back to it later with
362 * the 'workspace back_and_forth' command.
363 * NOTE: We have to duplicate the name as the original will be freed when
364 * the corresponding workspace is cleaned up.
365 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
366 * focused) are skipped, see bug #868. */
367 if (current && !con_is_internal(current)) {
368 FREE(previous_workspace_name);
370 previous_workspace_name = sstrdup(current->name);
371 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
375 workspace_reassign_sticky(workspace);
377 DLOG("switching to %p / %s\n", workspace, workspace->name);
378 Con *next = con_descend_focused(workspace);
380 /* Memorize current output */
381 Con *old_output = con_get_output(focused);
383 /* Display urgency hint for a while if the newly visible workspace would
384 * focus and thereby immediately destroy it */
385 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
387 next->urgent = false;
390 /* … but immediately reset urgency flags; they will be set to false by
391 * the timer callback in case the container is focused at the time of
393 focused->urgent = true;
394 workspace->urgent = true;
396 if (focused->urgency_timer == NULL) {
397 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
399 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
400 /* use a repeating timer to allow for easy resets */
401 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
402 config.workspace_urgency_timer, config.workspace_urgency_timer);
403 focused->urgency_timer->data = focused;
404 ev_timer_start(main_loop, focused->urgency_timer);
406 DLOG("Resetting urgency timer of con %p on workspace %p\n",
408 ev_timer_again(main_loop, focused->urgency_timer);
413 ipc_send_workspace_event("focus", workspace, current);
415 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
416 /* Close old workspace if necessary. This must be done *after* doing
417 * urgency handling, because tree_close() will do a con_focus() on the next
418 * client, which will clear the urgency flag too early. Also, there is no
419 * way for con_focus() to know about when to clear urgency immediately and
420 * when to defer it. */
421 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
422 /* check if this workspace is currently visible */
423 if (!workspace_is_visible(old)) {
424 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
425 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
426 tree_close(old, DONT_KILL_WINDOW, false, false);
428 const unsigned char *payload;
430 y(get_buf, &payload, &length);
431 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
435 ewmh_update_number_of_desktops();
436 ewmh_update_desktop_names();
437 ewmh_update_desktop_viewport();
441 workspace->fullscreen_mode = CF_OUTPUT;
442 LOG("focused now = %p / %s\n", focused, focused->name);
444 /* Set mouse pointer */
445 Con *new_output = con_get_output(focused);
446 if (old_output != new_output) {
447 x_set_warp_to(&next->rect);
450 /* Update the EWMH hints */
451 ewmh_update_current_desktop();
455 * Switches to the given workspace
458 void workspace_show(Con *workspace) {
459 _workspace_show(workspace);
463 * Looks up the workspace by name and switches to it.
466 void workspace_show_by_name(const char *num) {
468 workspace = workspace_get(num, NULL);
469 _workspace_show(workspace);
473 * Focuses the next workspace.
476 Con *workspace_next(void) {
477 Con *current = con_get_workspace(focused);
481 if (current->num == -1) {
482 /* If currently a named workspace, find next named workspace. */
483 next = TAILQ_NEXT(current, nodes);
485 /* If currently a numbered workspace, find next numbered workspace. */
486 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
487 /* Skip outputs starting with __, they are internal. */
488 if (con_is_internal(output))
490 NODES_FOREACH(output_get_content(output)) {
491 if (child->type != CT_WORKSPACE)
493 if (child->num == -1)
495 /* Need to check child against current and next because we are
496 * traversing multiple lists and thus are not guaranteed the
497 * relative order between the list of workspaces. */
498 if (current->num < child->num && (!next || child->num < next->num))
504 /* Find next named workspace. */
506 bool found_current = false;
507 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
508 /* Skip outputs starting with __, they are internal. */
509 if (con_is_internal(output))
511 NODES_FOREACH(output_get_content(output)) {
512 if (child->type != CT_WORKSPACE)
514 if (child == current) {
516 } else if (child->num == -1 && (current->num != -1 || found_current)) {
518 goto workspace_next_end;
524 /* Find first 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 (!next || (child->num != -1 && child->num < next->num))
543 * Focuses the previous workspace.
546 Con *workspace_prev(void) {
547 Con *current = con_get_workspace(focused);
551 if (current->num == -1) {
552 /* If named workspace, find previous named workspace. */
553 prev = TAILQ_PREV(current, nodes_head, nodes);
554 if (prev && prev->num != -1)
557 /* If numbered workspace, find previous numbered workspace. */
558 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
559 /* Skip outputs starting with __, they are internal. */
560 if (con_is_internal(output))
562 NODES_FOREACH_REVERSE(output_get_content(output)) {
563 if (child->type != CT_WORKSPACE || child->num == -1)
565 /* Need to check child against current and previous because we
566 * are traversing multiple lists and thus are not guaranteed
567 * the relative order between the list of workspaces. */
568 if (current->num > child->num && (!prev || child->num > prev->num))
574 /* Find previous named workspace. */
576 bool found_current = false;
577 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
578 /* Skip outputs starting with __, they are internal. */
579 if (con_is_internal(output))
581 NODES_FOREACH_REVERSE(output_get_content(output)) {
582 if (child->type != CT_WORKSPACE)
584 if (child == current) {
585 found_current = true;
586 } else if (child->num == -1 && (current->num != -1 || found_current)) {
588 goto workspace_prev_end;
594 /* Find last workspace. */
596 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
597 /* Skip outputs starting with __, they are internal. */
598 if (con_is_internal(output))
600 NODES_FOREACH_REVERSE(output_get_content(output)) {
601 if (child->type != CT_WORKSPACE)
603 if (!prev || child->num > prev->num)
614 * Focuses the next workspace on the same output.
617 Con *workspace_next_on_output(void) {
618 Con *current = con_get_workspace(focused);
620 Con *output = con_get_output(focused);
622 if (current->num == -1) {
623 /* If currently a named workspace, find next named workspace. */
624 next = TAILQ_NEXT(current, nodes);
626 /* If currently a numbered workspace, find next numbered workspace. */
627 NODES_FOREACH(output_get_content(output)) {
628 if (child->type != CT_WORKSPACE)
630 if (child->num == -1)
632 /* Need to check child against current and next because we are
633 * traversing multiple lists and thus are not guaranteed the
634 * relative order between the list of workspaces. */
635 if (current->num < child->num && (!next || child->num < next->num))
640 /* Find next named workspace. */
642 bool found_current = false;
643 NODES_FOREACH(output_get_content(output)) {
644 if (child->type != CT_WORKSPACE)
646 if (child == current) {
648 } else if (child->num == -1 && (current->num != -1 || found_current)) {
650 goto workspace_next_on_output_end;
655 /* Find first workspace. */
657 NODES_FOREACH(output_get_content(output)) {
658 if (child->type != CT_WORKSPACE)
660 if (!next || (child->num != -1 && child->num < next->num))
664 workspace_next_on_output_end:
669 * Focuses the previous workspace on same output.
672 Con *workspace_prev_on_output(void) {
673 Con *current = con_get_workspace(focused);
675 Con *output = con_get_output(focused);
676 DLOG("output = %s\n", output->name);
678 if (current->num == -1) {
679 /* If named workspace, find previous named workspace. */
680 prev = TAILQ_PREV(current, nodes_head, nodes);
681 if (prev && prev->num != -1)
684 /* If numbered workspace, find previous numbered workspace. */
685 NODES_FOREACH_REVERSE(output_get_content(output)) {
686 if (child->type != CT_WORKSPACE || child->num == -1)
688 /* Need to check child against current and previous because we
689 * are traversing multiple lists and thus are not guaranteed
690 * the relative order between the list of workspaces. */
691 if (current->num > child->num && (!prev || child->num > prev->num))
696 /* Find previous named workspace. */
698 bool found_current = false;
699 NODES_FOREACH_REVERSE(output_get_content(output)) {
700 if (child->type != CT_WORKSPACE)
702 if (child == current) {
703 found_current = true;
704 } else if (child->num == -1 && (current->num != -1 || found_current)) {
706 goto workspace_prev_on_output_end;
711 /* Find last workspace. */
713 NODES_FOREACH_REVERSE(output_get_content(output)) {
714 if (child->type != CT_WORKSPACE)
716 if (!prev || child->num > prev->num)
721 workspace_prev_on_output_end:
726 * Focuses the previously focused workspace.
729 void workspace_back_and_forth(void) {
730 if (!previous_workspace_name) {
731 DLOG("No previous workspace name set. Not switching.");
735 workspace_show_by_name(previous_workspace_name);
739 * Returns the previously focused workspace con, or NULL if unavailable.
742 Con *workspace_back_and_forth_get(void) {
743 if (!previous_workspace_name) {
744 DLOG("no previous workspace name set.");
749 workspace = workspace_get(previous_workspace_name, NULL);
754 static bool get_urgency_flag(Con *con) {
756 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
757 if (child->urgent || get_urgency_flag(child))
760 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
761 if (child->urgent || get_urgency_flag(child))
768 * Goes through all clients on the given workspace and updates the workspace’s
769 * urgent flag accordingly.
772 void workspace_update_urgent_flag(Con *ws) {
773 bool old_flag = ws->urgent;
774 ws->urgent = get_urgency_flag(ws);
775 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
777 if (old_flag != ws->urgent)
778 ipc_send_workspace_event("urgent", ws, NULL);
782 * 'Forces' workspace orientation by moving all cons into a new split-con with
783 * the same layout as the workspace and then changing the workspace layout.
786 void ws_force_orientation(Con *ws, orientation_t orientation) {
787 /* 1: create a new split container */
788 Con *split = con_new(NULL, NULL);
791 /* 2: copy layout from workspace */
792 split->layout = ws->layout;
794 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
796 /* 3: move the existing cons of this workspace below the new con */
797 DLOG("Moving cons\n");
798 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
799 Con *child = TAILQ_FIRST(&(ws->nodes_head));
801 con_attach(child, split, true);
804 /* 4: switch workspace layout */
805 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
806 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
808 /* 5: attach the new split container to the workspace */
809 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
810 con_attach(split, ws, false);
812 /* 6: fix the percentages */
816 con_focus(old_focused);
820 * Called when a new con (with a window, not an empty or split con) should be
821 * attached to the workspace (for example when managing a new window or when
822 * moving an existing window to the workspace level).
824 * Depending on the workspace_layout setting, this function either returns the
825 * workspace itself (default layout) or creates a new stacked/tabbed con and
829 Con *workspace_attach_to(Con *ws) {
830 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
832 if (ws->workspace_layout == L_DEFAULT) {
833 DLOG("Default layout, just attaching it to the workspace itself.\n");
837 DLOG("Non-default layout, creating a new split container\n");
838 /* 1: create a new split container */
839 Con *new = con_new(NULL, NULL);
842 /* 2: set the requested layout on the split con */
843 new->layout = ws->workspace_layout;
845 /* 4: attach the new split container to the workspace */
846 DLOG("Attaching new split %p to workspace %p\n", new, ws);
847 con_attach(new, ws, false);
849 /* 5: fix the percentages */
856 * Creates a new container and re-parents all of children from the given
859 * The container inherits the layout from the workspace.
861 Con *workspace_encapsulate(Con *ws) {
862 if (TAILQ_EMPTY(&(ws->nodes_head))) {
863 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
867 Con *new = con_new(NULL, NULL);
869 new->layout = ws->layout;
871 DLOG("Moving children of workspace %p / %s into container %p\n",
875 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
876 child = TAILQ_FIRST(&(ws->nodes_head));
878 con_attach(child, new, true);
881 con_attach(new, ws, true);
887 * Move the given workspace to the specified output.
888 * This returns true if and only if moving the workspace was successful.
890 bool workspace_move_to_output(Con *ws, char *name) {
891 LOG("Trying to move workspace %p / %s to output \"%s\".\n", ws, ws->name, name);
893 Con *current_output_con = con_get_output(ws);
894 if (!current_output_con) {
895 ELOG("Could not get the output container for workspace %p / %s.\n", ws, ws->name);
899 Output *current_output = get_output_by_name(current_output_con->name);
900 if (!current_output) {
901 ELOG("Cannot get current output. This is a bug in i3.\n");
904 Output *output = get_output_from_string(current_output, name);
906 ELOG("Could not get output from string \"%s\"\n", name);
910 Con *content = output_get_content(output->con);
911 LOG("got output %p with content %p\n", output, content);
913 Con *previously_visible_ws = TAILQ_FIRST(&(content->nodes_head));
914 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
916 bool workspace_was_visible = workspace_is_visible(ws);
917 if (con_num_children(ws->parent) == 1) {
918 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
920 /* check if we can find a workspace assigned to this output */
921 bool used_assignment = false;
922 struct Workspace_Assignment *assignment;
923 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
924 if (assignment->output == NULL || strcmp(assignment->output, current_output->name) != 0)
927 /* check if this workspace is already attached to the tree */
928 Con *workspace = NULL, *out;
929 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
930 GREP_FIRST(workspace, output_get_content(out),
931 !strcasecmp(child->name, assignment->name));
932 if (workspace != NULL)
935 /* so create the workspace referenced to by this assignment */
936 LOG("Creating workspace from assignment %s.\n", assignment->name);
937 workspace_get(assignment->name, NULL);
938 used_assignment = true;
942 /* if we couldn't create the workspace using an assignment, create
943 * it on the output */
944 if (!used_assignment)
945 create_workspace_on_output(current_output, ws->parent);
947 /* notify the IPC listeners */
948 ipc_send_workspace_event("init", ws, NULL);
952 /* detach from the old output and attach to the new output */
953 Con *old_content = ws->parent;
955 if (workspace_was_visible) {
956 /* The workspace which we just detached was visible, so focus
957 * the next one in the focus-stack. */
958 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
959 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
960 workspace_show(focus_ws);
962 con_attach(ws, content, false);
964 /* fix the coordinates of the floating containers */
966 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
967 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
969 ipc_send_workspace_event("move", ws, NULL);
970 if (workspace_was_visible) {
971 /* Focus the moved workspace on the destination output. */
975 /* NB: We cannot simply work with previously_visible_ws since it might
976 * have been cleaned up by workspace_show() already, depending on the
977 * focus order/number of other workspaces on the output.
978 * Instead, we loop through the available workspaces and only work with
979 * previously_visible_ws if we still find it. */
980 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
981 if (ws != previously_visible_ws)
984 /* Call the on_remove_child callback of the workspace which previously
985 * was visible on the destination output. Since it is no longer
986 * visible, it might need to get cleaned up. */
987 CALL(previously_visible_ws, on_remove_child);