2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
7 * workspace.c: Modifying workspaces, accessing them, moving containers to
12 #include "yajl_utils.h"
14 /* Stores a copy of the name of the last used workspace for the workspace
15 * back-and-forth switching. */
16 static char *previous_workspace_name = NULL;
18 /* NULL-terminated list of workspace names (in order) extracted from
20 static char **binding_workspace_names = NULL;
23 * Returns the workspace with the given name or NULL if such a workspace does
27 Con *get_existing_workspace_by_name(const char *name) {
28 Con *output, *workspace = NULL;
29 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
30 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, name));
37 * Returns the workspace with the given number or NULL if such a workspace does
41 Con *get_existing_workspace_by_num(int num) {
42 Con *output, *workspace = NULL;
43 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
44 GREP_FIRST(workspace, output_get_content(output), child->num == num);
51 * Sets ws->layout to splith/splitv if default_orientation was specified in the
52 * configfile. Otherwise, it uses splith/splitv depending on whether the output
53 * is higher than wide.
56 static void _workspace_apply_default_orientation(Con *ws) {
57 /* If default_orientation is set to NO_ORIENTATION we determine
58 * orientation depending on output resolution. */
59 if (config.default_orientation == NO_ORIENTATION) {
60 Con *output = con_get_output(ws);
61 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
62 ws->rect = output->rect;
63 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
64 output->rect.width, output->rect.height, ws->layout);
66 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
71 * Returns the first output that is assigned to a workspace specified by the
72 * given name or number or NULL if no such output exists. If there is a
73 * workspace with a matching name and another workspace with a matching number,
74 * the output assigned to the first one is returned.
75 * If 'name' is NULL it will be ignored.
76 * If 'parsed_num' is -1 it will be ignored.
79 static Con *get_assigned_output(const char *name, long parsed_num) {
82 struct Workspace_Assignment *assignment;
83 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
84 if (name && strcmp(assignment->name, name) == 0) {
85 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
86 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
88 } else if (parsed_num != -1 && name_is_digits(assignment->name) && ws_name_to_number(assignment->name) == parsed_num) {
89 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
90 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
98 * Returns a pointer to the workspace with the given number (starting at 0),
99 * creating the workspace if necessary (by allocating the necessary amount of
100 * memory and initializing the data structures correctly).
103 Con *workspace_get(const char *num, bool *created) {
104 Con *workspace = get_existing_workspace_by_name(num);
106 if (workspace == NULL) {
107 LOG("Creating new workspace \"%s\"\n", num);
109 /* We set workspace->num to the number if this workspace’s name begins
110 * with a positive number. Otherwise it’s a named ws and num will be
112 long parsed_num = ws_name_to_number(num);
114 Con *output = get_assigned_output(num, parsed_num);
115 /* if an assignment is not found, we create this workspace on the current output */
117 output = con_get_output(focused);
120 Con *content = output_get_content(output);
121 LOG("got output %p with content %p\n", output, content);
122 /* We need to attach this container after setting its type. con_attach
123 * will handle CT_WORKSPACEs differently */
124 workspace = con_new(NULL, NULL);
126 sasprintf(&name, "[i3 con] workspace %s", num);
127 x_set_name(workspace, name);
129 workspace->type = CT_WORKSPACE;
130 FREE(workspace->name);
131 workspace->name = sstrdup(num);
132 workspace->workspace_layout = config.default_layout;
133 workspace->num = parsed_num;
134 LOG("num = %d\n", workspace->num);
136 workspace->parent = content;
137 _workspace_apply_default_orientation(workspace);
139 con_attach(workspace, content, false);
141 ipc_send_workspace_event("init", workspace, NULL);
142 ewmh_update_number_of_desktops();
143 ewmh_update_desktop_names();
144 ewmh_update_desktop_viewport();
145 ewmh_update_wm_desktop();
148 } else if (created != NULL) {
156 * Extracts workspace names from keybindings (e.g. “web” from “bindsym $mod+1
157 * workspace web”), so that when an output needs a workspace, i3 can start with
158 * the first configured one. Needs to be called before reorder_bindings() so
159 * that the config-file order is used, not the i3-internal order.
162 void extract_workspace_names_from_bindings(void) {
165 if (binding_workspace_names != NULL) {
166 for (int i = 0; binding_workspace_names[i] != NULL; i++) {
167 free(binding_workspace_names[i]);
169 FREE(binding_workspace_names);
171 TAILQ_FOREACH(bind, bindings, bindings) {
172 DLOG("binding with command %s\n", bind->command);
173 if (strlen(bind->command) < strlen("workspace ") ||
174 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
176 DLOG("relevant command = %s\n", bind->command);
177 const char *target = bind->command + strlen("workspace ");
178 while (*target == ' ' || *target == '\t')
180 /* We check if this is the workspace
181 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
182 * Beware: The workspace names "next", "prev", "next_on_output",
183 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
184 * so we check before stripping the double quotes */
185 if (strncasecmp(target, "next", strlen("next")) == 0 ||
186 strncasecmp(target, "prev", strlen("prev")) == 0 ||
187 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
188 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
189 strncasecmp(target, "number", strlen("number")) == 0 ||
190 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
191 strncasecmp(target, "current", strlen("current")) == 0)
193 char *target_name = parse_string(&target, false);
194 if (target_name == NULL)
196 if (strncasecmp(target_name, "__", strlen("__")) == 0) {
197 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
201 DLOG("Saving workspace name \"%s\"\n", target_name);
203 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
204 binding_workspace_names[n - 1] = target_name;
206 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
207 binding_workspace_names[n - 1] = NULL;
211 * Returns a pointer to a new workspace in the given output. The workspace
212 * is created attached to the tree hierarchy through the given content
216 Con *create_workspace_on_output(Output *output, Con *content) {
217 /* add a workspace to this output */
220 Con *ws = con_new(NULL, NULL);
221 ws->type = CT_WORKSPACE;
223 /* try the configured workspace bindings first to find a free name */
224 for (int n = 0; binding_workspace_names[n] != NULL; n++) {
225 char *target_name = binding_workspace_names[n];
226 /* Ensure that this workspace is not assigned to a different output —
227 * otherwise we would create it, then move it over to its output, then
228 * find a new workspace, etc… */
229 Con *assigned = get_assigned_output(target_name, -1);
230 if (assigned && assigned != output->con) {
234 exists = (get_existing_workspace_by_name(target_name) != NULL);
236 ws->name = sstrdup(target_name);
237 /* Set ->num to the number of the workspace, if the name actually
238 * is a number or starts with a number */
239 ws->num = ws_name_to_number(ws->name);
240 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
247 /* get the next unused workspace number */
248 DLOG("Getting next unused workspace by number\n");
251 exists = (get_existing_workspace_by_num(++c) != NULL);
252 DLOG("result for ws %d: exists = %d\n", c, exists);
255 sasprintf(&(ws->name), "%d", c);
257 con_attach(ws, content, false);
259 sasprintf(&name, "[i3 con] workspace %s", ws->name);
260 x_set_name(ws, name);
263 ws->fullscreen_mode = CF_OUTPUT;
265 ws->workspace_layout = config.default_layout;
266 _workspace_apply_default_orientation(ws);
272 * Returns true if the workspace is currently visible. Especially important for
273 * multi-monitor environments, as they can have multiple currenlty active
277 bool workspace_is_visible(Con *ws) {
278 Con *output = con_get_output(ws);
281 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
282 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
287 * XXX: we need to clean up all this recursive walking code.
290 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
293 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
294 if (current != exclude &&
295 current->sticky_group != NULL &&
296 current->window != NULL &&
297 strcmp(current->sticky_group, sticky_group) == 0)
300 Con *recurse = _get_sticky(current, sticky_group, exclude);
305 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
306 if (current != exclude &&
307 current->sticky_group != NULL &&
308 current->window != NULL &&
309 strcmp(current->sticky_group, sticky_group) == 0)
312 Con *recurse = _get_sticky(current, sticky_group, exclude);
321 * Reassigns all child windows in sticky containers. Called when the user
322 * changes workspaces.
324 * XXX: what about sticky containers which contain containers?
327 static void workspace_reassign_sticky(Con *con) {
329 /* 1: go through all containers */
331 /* handle all children and floating windows of this node */
332 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
333 if (current->sticky_group == NULL) {
334 workspace_reassign_sticky(current);
338 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
339 /* 2: find a window which we can re-assign */
340 Con *output = con_get_output(current);
341 Con *src = _get_sticky(output, current->sticky_group, current);
344 LOG("No window found for this sticky group\n");
345 workspace_reassign_sticky(current);
349 x_move_win(src, current);
350 current->window = src->window;
351 current->mapped = true;
355 x_reparent_child(current, src);
357 LOG("re-assigned window from src %p to dest %p\n", src, current);
360 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
361 workspace_reassign_sticky(current);
365 * Callback to reset the urgent flag of the given con to false. May be started by
366 * workspace_show to avoid urgency hints being lost by switching to a workspace
370 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
373 ev_timer_stop(main_loop, con->urgency_timer);
374 FREE(con->urgency_timer);
377 DLOG("Resetting urgency flag of con %p by timer\n", con);
378 con_set_urgency(con, false);
379 con_update_parents_urgency(con);
380 workspace_update_urgent_flag(con_get_workspace(con));
381 ipc_send_window_event("urgent", con);
387 * Switches to the given workspace
390 void workspace_show(Con *workspace) {
391 Con *current, *old = NULL;
392 Con *old_focus = focused;
394 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
395 if (con_is_internal(workspace))
398 /* disable fullscreen for the other workspaces and get the workspace we are
400 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
401 if (current->fullscreen_mode == CF_OUTPUT)
403 current->fullscreen_mode = CF_NONE;
406 /* enable fullscreen for the target workspace. If it happens to be the
407 * same one we are currently on anyways, we can stop here. */
408 workspace->fullscreen_mode = CF_OUTPUT;
409 current = con_get_workspace(focused);
410 if (workspace == current) {
411 DLOG("Not switching, already there.\n");
415 /* Remember currently focused workspace for switching back to it later with
416 * the 'workspace back_and_forth' command.
417 * NOTE: We have to duplicate the name as the original will be freed when
418 * the corresponding workspace is cleaned up.
419 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
420 * focused) are skipped, see bug #868. */
421 if (current && !con_is_internal(current)) {
422 FREE(previous_workspace_name);
423 previous_workspace_name = sstrdup(current->name);
424 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
427 workspace_reassign_sticky(workspace);
429 DLOG("switching to %p / %s\n", workspace, workspace->name);
430 Con *next = con_descend_focused(workspace);
432 /* Memorize current output */
433 Con *old_output = con_get_output(focused);
435 /* Display urgency hint for a while if the newly visible workspace would
436 * focus and thereby immediately destroy it */
437 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
439 next->urgent = false;
442 /* … but immediately reset urgency flags; they will be set to false by
443 * the timer callback in case the container is focused at the time of
445 focused->urgent = true;
446 workspace->urgent = true;
448 if (focused->urgency_timer == NULL) {
449 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
451 focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
452 /* use a repeating timer to allow for easy resets */
453 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
454 config.workspace_urgency_timer, config.workspace_urgency_timer);
455 focused->urgency_timer->data = focused;
456 ev_timer_start(main_loop, focused->urgency_timer);
458 DLOG("Resetting urgency timer of con %p on workspace %p\n",
460 ev_timer_again(main_loop, focused->urgency_timer);
465 ipc_send_workspace_event("focus", workspace, current);
467 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
468 /* Close old workspace if necessary. This must be done *after* doing
469 * urgency handling, because tree_close_internal() will do a con_focus() on the next
470 * client, which will clear the urgency flag too early. Also, there is no
471 * way for con_focus() to know about when to clear urgency immediately and
472 * when to defer it. */
473 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
474 /* check if this workspace is currently visible */
475 if (!workspace_is_visible(old)) {
476 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
477 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
478 tree_close_internal(old, DONT_KILL_WINDOW, false, false);
480 const unsigned char *payload;
482 y(get_buf, &payload, &length);
483 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
487 /* Avoid calling output_push_sticky_windows later with a freed container. */
488 if (old == old_focus) {
492 ewmh_update_number_of_desktops();
493 ewmh_update_desktop_names();
494 ewmh_update_desktop_viewport();
495 ewmh_update_wm_desktop();
499 workspace->fullscreen_mode = CF_OUTPUT;
500 LOG("focused now = %p / %s\n", focused, focused->name);
502 /* Set mouse pointer */
503 Con *new_output = con_get_output(focused);
504 if (old_output != new_output) {
505 x_set_warp_to(&next->rect);
508 /* Update the EWMH hints */
509 ewmh_update_current_desktop();
511 /* Push any sticky windows to the now visible workspace. */
512 output_push_sticky_windows(old_focus);
516 * Looks up the workspace by name and switches to it.
519 void workspace_show_by_name(const char *num) {
521 workspace = workspace_get(num, NULL);
522 workspace_show(workspace);
526 * Focuses the next workspace.
529 Con *workspace_next(void) {
530 Con *current = con_get_workspace(focused);
531 Con *next = NULL, *first = NULL, *first_opposite = NULL;
534 if (current->num == -1) {
535 /* If currently a named workspace, find next named workspace. */
536 if ((next = TAILQ_NEXT(current, nodes)) != NULL)
538 bool found_current = false;
539 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
540 /* Skip outputs starting with __, they are internal. */
541 if (con_is_internal(output))
543 NODES_FOREACH(output_get_content(output)) {
544 if (child->type != CT_WORKSPACE)
548 if (!first_opposite || (child->num != -1 && child->num < first_opposite->num))
549 first_opposite = child;
550 if (child == current) {
551 found_current = true;
552 } else if (child->num == -1 && found_current) {
559 /* If currently a numbered workspace, find next numbered workspace. */
560 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
561 /* Skip outputs starting with __, they are internal. */
562 if (con_is_internal(output))
564 NODES_FOREACH(output_get_content(output)) {
565 if (child->type != CT_WORKSPACE)
567 if (!first || (child->num != -1 && child->num < first->num))
569 if (!first_opposite && child->num == -1)
570 first_opposite = child;
571 if (child->num == -1)
573 /* Need to check child against current and next because we are
574 * traversing multiple lists and thus are not guaranteed the
575 * relative order between the list of workspaces. */
576 if (current->num < child->num && (!next || child->num < next->num))
583 next = first_opposite ? first_opposite : first;
589 * Focuses the previous workspace.
592 Con *workspace_prev(void) {
593 Con *current = con_get_workspace(focused);
594 Con *prev = NULL, *first_opposite = NULL, *last = NULL;
597 if (current->num == -1) {
598 /* If named workspace, find previous named workspace. */
599 prev = TAILQ_PREV(current, nodes_head, nodes);
600 if (prev && prev->num != -1)
603 bool found_current = false;
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)
613 if (!first_opposite || (child->num != -1 && child->num > first_opposite->num))
614 first_opposite = child;
615 if (child == current) {
616 found_current = true;
617 } else if (child->num == -1 && found_current) {
625 /* If numbered workspace, find previous numbered workspace. */
626 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
627 /* Skip outputs starting with __, they are internal. */
628 if (con_is_internal(output))
630 NODES_FOREACH_REVERSE(output_get_content(output)) {
631 if (child->type != CT_WORKSPACE)
633 if (!last || (child->num != -1 && last->num < child->num))
635 if (!first_opposite && child->num == -1)
636 first_opposite = child;
637 if (child->num == -1)
639 /* Need to check child against current and previous because we
640 * are traversing multiple lists and thus are not guaranteed
641 * the relative order between the list of workspaces. */
642 if (current->num > child->num && (!prev || child->num > prev->num))
649 prev = first_opposite ? first_opposite : last;
655 * Focuses the next workspace on the same output.
658 Con *workspace_next_on_output(void) {
659 Con *current = con_get_workspace(focused);
661 Con *output = con_get_output(focused);
663 if (current->num == -1) {
664 /* If currently a named workspace, find next named workspace. */
665 next = TAILQ_NEXT(current, nodes);
667 /* If currently a numbered workspace, find next numbered workspace. */
668 NODES_FOREACH(output_get_content(output)) {
669 if (child->type != CT_WORKSPACE)
671 if (child->num == -1)
673 /* Need to check child against current and next because we are
674 * traversing multiple lists and thus are not guaranteed the
675 * relative order between the list of workspaces. */
676 if (current->num < child->num && (!next || child->num < next->num))
681 /* Find next named workspace. */
683 bool found_current = false;
684 NODES_FOREACH(output_get_content(output)) {
685 if (child->type != CT_WORKSPACE)
687 if (child == current) {
688 found_current = true;
689 } else if (child->num == -1 && (current->num != -1 || found_current)) {
691 goto workspace_next_on_output_end;
696 /* Find first workspace. */
698 NODES_FOREACH(output_get_content(output)) {
699 if (child->type != CT_WORKSPACE)
701 if (!next || (child->num != -1 && child->num < next->num))
705 workspace_next_on_output_end:
710 * Focuses the previous workspace on same output.
713 Con *workspace_prev_on_output(void) {
714 Con *current = con_get_workspace(focused);
716 Con *output = con_get_output(focused);
717 DLOG("output = %s\n", output->name);
719 if (current->num == -1) {
720 /* If named workspace, find previous named workspace. */
721 prev = TAILQ_PREV(current, nodes_head, nodes);
722 if (prev && prev->num != -1)
725 /* If numbered workspace, find previous numbered workspace. */
726 NODES_FOREACH_REVERSE(output_get_content(output)) {
727 if (child->type != CT_WORKSPACE || child->num == -1)
729 /* Need to check child against current and previous because we
730 * are traversing multiple lists and thus are not guaranteed
731 * the relative order between the list of workspaces. */
732 if (current->num > child->num && (!prev || child->num > prev->num))
737 /* Find previous named workspace. */
739 bool found_current = false;
740 NODES_FOREACH_REVERSE(output_get_content(output)) {
741 if (child->type != CT_WORKSPACE)
743 if (child == current) {
744 found_current = true;
745 } else if (child->num == -1 && (current->num != -1 || found_current)) {
747 goto workspace_prev_on_output_end;
752 /* Find last workspace. */
754 NODES_FOREACH_REVERSE(output_get_content(output)) {
755 if (child->type != CT_WORKSPACE)
757 if (!prev || child->num > prev->num)
762 workspace_prev_on_output_end:
767 * Focuses the previously focused workspace.
770 void workspace_back_and_forth(void) {
771 if (!previous_workspace_name) {
772 DLOG("No previous workspace name set. Not switching.\n");
776 workspace_show_by_name(previous_workspace_name);
780 * Returns the previously focused workspace con, or NULL if unavailable.
783 Con *workspace_back_and_forth_get(void) {
784 if (!previous_workspace_name) {
785 DLOG("No previous workspace name set.\n");
790 workspace = workspace_get(previous_workspace_name, NULL);
795 static bool get_urgency_flag(Con *con) {
797 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
798 if (child->urgent || get_urgency_flag(child))
801 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
802 if (child->urgent || get_urgency_flag(child))
809 * Goes through all clients on the given workspace and updates the workspace’s
810 * urgent flag accordingly.
813 void workspace_update_urgent_flag(Con *ws) {
814 bool old_flag = ws->urgent;
815 ws->urgent = get_urgency_flag(ws);
816 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
818 if (old_flag != ws->urgent)
819 ipc_send_workspace_event("urgent", ws, NULL);
823 * 'Forces' workspace orientation by moving all cons into a new split-con with
824 * the same layout as the workspace and then changing the workspace layout.
827 void ws_force_orientation(Con *ws, orientation_t orientation) {
828 /* 1: create a new split container */
829 Con *split = con_new(NULL, NULL);
832 /* 2: copy layout from workspace */
833 split->layout = ws->layout;
835 /* 3: move the existing cons of this workspace below the new con */
836 Con **focus_order = get_focus_order(ws);
838 DLOG("Moving cons\n");
839 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
840 Con *child = TAILQ_FIRST(&(ws->nodes_head));
842 con_attach(child, split, true);
845 set_focus_order(split, focus_order);
848 /* 4: switch workspace layout */
849 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
850 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
852 /* 5: attach the new split container to the workspace */
853 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
854 con_attach(split, ws, false);
856 /* 6: fix the percentages */
861 * Called when a new con (with a window, not an empty or split con) should be
862 * attached to the workspace (for example when managing a new window or when
863 * moving an existing window to the workspace level).
865 * Depending on the workspace_layout setting, this function either returns the
866 * workspace itself (default layout) or creates a new stacked/tabbed con and
870 Con *workspace_attach_to(Con *ws) {
871 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
873 if (ws->workspace_layout == L_DEFAULT) {
874 DLOG("Default layout, just attaching it to the workspace itself.\n");
878 DLOG("Non-default layout, creating a new split container\n");
879 /* 1: create a new split container */
880 Con *new = con_new(NULL, NULL);
883 /* 2: set the requested layout on the split con */
884 new->layout = ws->workspace_layout;
886 /* 4: attach the new split container to the workspace */
887 DLOG("Attaching new split %p to workspace %p\n", new, ws);
888 con_attach(new, ws, false);
890 /* 5: fix the percentages */
897 * Creates a new container and re-parents all of children from the given
900 * The container inherits the layout from the workspace.
902 Con *workspace_encapsulate(Con *ws) {
903 if (TAILQ_EMPTY(&(ws->nodes_head))) {
904 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
908 Con *new = con_new(NULL, NULL);
910 new->layout = ws->layout;
912 Con **focus_order = get_focus_order(ws);
914 DLOG("Moving children of workspace %p / %s into container %p\n",
917 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
918 child = TAILQ_FIRST(&(ws->nodes_head));
920 con_attach(child, new, true);
923 set_focus_order(new, focus_order);
926 con_attach(new, ws, true);
932 * Move the given workspace to the specified output.
933 * This returns true if and only if moving the workspace was successful.
935 bool workspace_move_to_output(Con *ws, Output *output) {
936 LOG("Trying to move workspace %p / %s to output %p / \"%s\".\n", ws, ws->name, output, output_primary_name(output));
938 Output *current_output = get_output_for_con(ws);
939 if (current_output == NULL) {
940 ELOG("Cannot get current output. This is a bug in i3.\n");
944 Con *content = output_get_content(output->con);
945 LOG("got output %p with content %p\n", output, content);
947 Con *previously_visible_ws = TAILQ_FIRST(&(content->focus_head));
948 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
950 bool workspace_was_visible = workspace_is_visible(ws);
951 if (con_num_children(ws->parent) == 1) {
952 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
954 /* check if we can find a workspace assigned to this output */
955 bool used_assignment = false;
956 struct Workspace_Assignment *assignment;
957 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
958 if (assignment->output == NULL || strcmp(assignment->output, output_primary_name(current_output)) != 0)
960 /* check if this workspace is already attached to the tree */
961 if (get_existing_workspace_by_name(assignment->name) != NULL) {
965 /* so create the workspace referenced to by this assignment */
966 LOG("Creating workspace from assignment %s.\n", assignment->name);
967 workspace_get(assignment->name, NULL);
968 used_assignment = true;
972 /* if we couldn't create the workspace using an assignment, create
973 * it on the output */
974 if (!used_assignment)
975 create_workspace_on_output(current_output, ws->parent);
977 /* notify the IPC listeners */
978 ipc_send_workspace_event("init", ws, NULL);
982 /* detach from the old output and attach to the new output */
983 Con *old_content = ws->parent;
985 if (workspace_was_visible) {
986 /* The workspace which we just detached was visible, so focus
987 * the next one in the focus-stack. */
988 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
989 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
990 workspace_show(focus_ws);
992 con_attach(ws, content, false);
994 /* fix the coordinates of the floating containers */
996 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
997 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
999 ipc_send_workspace_event("move", ws, NULL);
1000 if (workspace_was_visible) {
1001 /* Focus the moved workspace on the destination output. */
1005 /* NB: We cannot simply work with previously_visible_ws since it might
1006 * have been cleaned up by workspace_show() already, depending on the
1007 * focus order/number of other workspaces on the output.
1008 * Instead, we loop through the available workspaces and only work with
1009 * previously_visible_ws if we still find it. */
1010 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1011 if (ws != previously_visible_ws)
1014 /* Call the on_remove_child callback of the workspace which previously
1015 * was visible on the destination output. Since it is no longer
1016 * visible, it might need to get cleaned up. */
1017 CALL(previously_visible_ws, on_remove_child);