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 * The order of the 'ws_assignments' queue is respected: if multiple assignments
76 * match the specified workspace, the first one is returned.
77 * If 'name' is NULL it will be ignored.
78 * If 'parsed_num' is -1 it will be ignored.
81 static Con *get_assigned_output(const char *name, long parsed_num) {
83 struct Workspace_Assignment *assignment;
84 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
85 if (name && strcmp(assignment->name, name) == 0) {
86 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
87 Output *assigned_by_name = get_output_by_name(assignment->output, true);
88 if (assigned_by_name) {
89 /* When the name matches exactly, skip numbered assignments. */
90 return assigned_by_name->con;
92 } else if (!output && /* Only keep the first numbered assignment. */
94 name_is_digits(assignment->name) &&
95 ws_name_to_number(assignment->name) == parsed_num) {
96 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
97 Output *assigned_by_num = get_output_by_name(assignment->output, true);
98 if (assigned_by_num) {
99 output = assigned_by_num->con;
108 * Returns true if the first output assigned to a workspace with the given
109 * workspace assignment is the same as the given output.
111 bool output_triggers_assignment(Output *output, struct Workspace_Assignment *assignment) {
112 Con *assigned = get_assigned_output(assignment->name, -1);
113 return assigned && assigned == output->con;
117 * Returns a pointer to the workspace with the given number (starting at 0),
118 * creating the workspace if necessary (by allocating the necessary amount of
119 * memory and initializing the data structures correctly).
122 Con *workspace_get(const char *num, bool *created) {
123 Con *workspace = get_existing_workspace_by_name(num);
125 if (workspace == NULL) {
126 LOG("Creating new workspace \"%s\"\n", num);
128 /* We set workspace->num to the number if this workspace’s name begins
129 * with a positive number. Otherwise it’s a named ws and num will be
131 long parsed_num = ws_name_to_number(num);
133 Con *output = get_assigned_output(num, parsed_num);
134 /* if an assignment is not found, we create this workspace on the current output */
136 output = con_get_output(focused);
139 Con *content = output_get_content(output);
140 LOG("got output %p with content %p\n", output, content);
141 /* We need to attach this container after setting its type. con_attach
142 * will handle CT_WORKSPACEs differently */
143 workspace = con_new(NULL, NULL);
145 sasprintf(&name, "[i3 con] workspace %s", num);
146 x_set_name(workspace, name);
148 workspace->type = CT_WORKSPACE;
149 FREE(workspace->name);
150 workspace->name = sstrdup(num);
151 workspace->workspace_layout = config.default_layout;
152 workspace->num = parsed_num;
153 LOG("num = %d\n", workspace->num);
155 workspace->parent = content;
156 _workspace_apply_default_orientation(workspace);
158 con_attach(workspace, content, false);
160 ipc_send_workspace_event("init", workspace, NULL);
161 ewmh_update_number_of_desktops();
162 ewmh_update_desktop_names();
163 ewmh_update_desktop_viewport();
164 ewmh_update_wm_desktop();
167 } else if (created != NULL) {
175 * Extracts workspace names from keybindings (e.g. “web” from “bindsym $mod+1
176 * workspace web”), so that when an output needs a workspace, i3 can start with
177 * the first configured one. Needs to be called before reorder_bindings() so
178 * that the config-file order is used, not the i3-internal order.
181 void extract_workspace_names_from_bindings(void) {
184 if (binding_workspace_names != NULL) {
185 for (int i = 0; binding_workspace_names[i] != NULL; i++) {
186 free(binding_workspace_names[i]);
188 FREE(binding_workspace_names);
190 TAILQ_FOREACH(bind, bindings, bindings) {
191 DLOG("binding with command %s\n", bind->command);
192 if (strlen(bind->command) < strlen("workspace ") ||
193 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
195 DLOG("relevant command = %s\n", bind->command);
196 const char *target = bind->command + strlen("workspace ");
197 while (*target == ' ' || *target == '\t')
199 /* We check if this is the workspace
200 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
201 * Beware: The workspace names "next", "prev", "next_on_output",
202 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
203 * so we check before stripping the double quotes */
204 if (strncasecmp(target, "next", strlen("next")) == 0 ||
205 strncasecmp(target, "prev", strlen("prev")) == 0 ||
206 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
207 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
208 strncasecmp(target, "number", strlen("number")) == 0 ||
209 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
210 strncasecmp(target, "current", strlen("current")) == 0)
212 char *target_name = parse_string(&target, false);
213 if (target_name == NULL)
215 if (strncasecmp(target_name, "__", strlen("__")) == 0) {
216 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
220 DLOG("Saving workspace name \"%s\"\n", target_name);
222 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
223 binding_workspace_names[n - 1] = target_name;
225 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
226 binding_workspace_names[n - 1] = NULL;
230 * Returns a pointer to a new workspace in the given output. The workspace
231 * is created attached to the tree hierarchy through the given content
235 Con *create_workspace_on_output(Output *output, Con *content) {
236 /* add a workspace to this output */
239 Con *ws = con_new(NULL, NULL);
240 ws->type = CT_WORKSPACE;
242 /* try the configured workspace bindings first to find a free name */
243 for (int n = 0; binding_workspace_names[n] != NULL; n++) {
244 char *target_name = binding_workspace_names[n];
245 /* Ensure that this workspace is not assigned to a different output —
246 * otherwise we would create it, then move it over to its output, then
247 * find a new workspace, etc… */
248 Con *assigned = get_assigned_output(target_name, -1);
249 if (assigned && assigned != output->con) {
253 exists = (get_existing_workspace_by_name(target_name) != NULL);
255 ws->name = sstrdup(target_name);
256 /* Set ->num to the number of the workspace, if the name actually
257 * is a number or starts with a number */
258 ws->num = ws_name_to_number(ws->name);
259 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
266 /* get the next unused workspace number */
267 DLOG("Getting next unused workspace by number\n");
271 Con *assigned = get_assigned_output(NULL, c);
272 exists = (get_existing_workspace_by_num(c) || (assigned && assigned != output->con));
273 DLOG("result for ws %d: exists = %d\n", c, exists);
276 sasprintf(&(ws->name), "%d", c);
278 con_attach(ws, content, false);
280 sasprintf(&name, "[i3 con] workspace %s", ws->name);
281 x_set_name(ws, name);
284 ws->fullscreen_mode = CF_OUTPUT;
286 ws->workspace_layout = config.default_layout;
287 _workspace_apply_default_orientation(ws);
293 * Returns true if the workspace is currently visible. Especially important for
294 * multi-monitor environments, as they can have multiple currenlty active
298 bool workspace_is_visible(Con *ws) {
299 Con *output = con_get_output(ws);
302 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
303 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
308 * XXX: we need to clean up all this recursive walking code.
311 static Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
314 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
315 if (current != exclude &&
316 current->sticky_group != NULL &&
317 current->window != NULL &&
318 strcmp(current->sticky_group, sticky_group) == 0)
321 Con *recurse = _get_sticky(current, sticky_group, exclude);
326 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
327 if (current != exclude &&
328 current->sticky_group != NULL &&
329 current->window != NULL &&
330 strcmp(current->sticky_group, sticky_group) == 0)
333 Con *recurse = _get_sticky(current, sticky_group, exclude);
342 * Reassigns all child windows in sticky containers. Called when the user
343 * changes workspaces.
345 * XXX: what about sticky containers which contain containers?
348 static void workspace_reassign_sticky(Con *con) {
350 /* 1: go through all containers */
352 /* handle all children and floating windows of this node */
353 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
354 if (current->sticky_group == NULL) {
355 workspace_reassign_sticky(current);
359 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
360 /* 2: find a window which we can re-assign */
361 Con *output = con_get_output(current);
362 Con *src = _get_sticky(output, current->sticky_group, current);
365 LOG("No window found for this sticky group\n");
366 workspace_reassign_sticky(current);
370 x_move_win(src, current);
371 current->window = src->window;
372 current->mapped = true;
376 x_reparent_child(current, src);
378 LOG("re-assigned window from src %p to dest %p\n", src, current);
381 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
382 workspace_reassign_sticky(current);
386 * Callback to reset the urgent flag of the given con to false. May be started by
387 * workspace_show to avoid urgency hints being lost by switching to a workspace
391 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
394 ev_timer_stop(main_loop, con->urgency_timer);
395 FREE(con->urgency_timer);
398 DLOG("Resetting urgency flag of con %p by timer\n", con);
399 con_set_urgency(con, false);
400 con_update_parents_urgency(con);
401 workspace_update_urgent_flag(con_get_workspace(con));
402 ipc_send_window_event("urgent", con);
408 * Switches to the given workspace
411 void workspace_show(Con *workspace) {
412 Con *current, *old = NULL;
414 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
415 if (con_is_internal(workspace))
418 /* disable fullscreen for the other workspaces and get the workspace we are
420 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
421 if (current->fullscreen_mode == CF_OUTPUT)
423 current->fullscreen_mode = CF_NONE;
426 /* enable fullscreen for the target workspace. If it happens to be the
427 * same one we are currently on anyways, we can stop here. */
428 workspace->fullscreen_mode = CF_OUTPUT;
429 current = con_get_workspace(focused);
430 if (workspace == current) {
431 DLOG("Not switching, already there.\n");
435 /* Used to correctly update focus when pushing sticky windows. Holds the
436 * previously focused container in the same output as workspace. For
437 * example, if a sticky window is focused and then we switch focus to a
438 * workspace in another output and then switch to a third workspace in the
439 * first output, the sticky window needs to be refocused. */
440 Con *old_focus = old ? con_descend_focused(old) : NULL;
442 /* Remember currently focused workspace for switching back to it later with
443 * the 'workspace back_and_forth' command.
444 * NOTE: We have to duplicate the name as the original will be freed when
445 * the corresponding workspace is cleaned up.
446 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
447 * focused) are skipped, see bug #868. */
448 if (current && !con_is_internal(current)) {
449 FREE(previous_workspace_name);
450 previous_workspace_name = sstrdup(current->name);
451 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
454 workspace_reassign_sticky(workspace);
456 DLOG("switching to %p / %s\n", workspace, workspace->name);
457 Con *next = con_descend_focused(workspace);
459 /* Memorize current output */
460 Con *old_output = con_get_output(focused);
462 /* Display urgency hint for a while if the newly visible workspace would
463 * focus and thereby immediately destroy it */
464 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
466 next->urgent = false;
469 /* … but immediately reset urgency flags; they will be set to false by
470 * the timer callback in case the container is focused at the time of
472 focused->urgent = true;
473 workspace->urgent = true;
475 if (focused->urgency_timer == NULL) {
476 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
478 focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
479 /* use a repeating timer to allow for easy resets */
480 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
481 config.workspace_urgency_timer, config.workspace_urgency_timer);
482 focused->urgency_timer->data = focused;
483 ev_timer_start(main_loop, focused->urgency_timer);
485 DLOG("Resetting urgency timer of con %p on workspace %p\n",
487 ev_timer_again(main_loop, focused->urgency_timer);
492 ipc_send_workspace_event("focus", workspace, current);
494 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
495 /* Close old workspace if necessary. This must be done *after* doing
496 * urgency handling, because tree_close_internal() will do a con_focus() on the next
497 * client, which will clear the urgency flag too early. Also, there is no
498 * way for con_focus() to know about when to clear urgency immediately and
499 * when to defer it. */
500 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
501 /* check if this workspace is currently visible */
502 if (!workspace_is_visible(old)) {
503 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
504 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
505 tree_close_internal(old, DONT_KILL_WINDOW, false);
507 const unsigned char *payload;
509 y(get_buf, &payload, &length);
510 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
514 /* Avoid calling output_push_sticky_windows later with a freed container. */
515 if (old == old_focus) {
519 ewmh_update_number_of_desktops();
520 ewmh_update_desktop_names();
521 ewmh_update_desktop_viewport();
522 ewmh_update_wm_desktop();
526 workspace->fullscreen_mode = CF_OUTPUT;
527 LOG("focused now = %p / %s\n", focused, focused->name);
529 /* Set mouse pointer */
530 Con *new_output = con_get_output(focused);
531 if (old_output != new_output) {
532 x_set_warp_to(&next->rect);
535 /* Update the EWMH hints */
536 ewmh_update_current_desktop();
538 /* Push any sticky windows to the now visible workspace. */
539 output_push_sticky_windows(old_focus);
543 * Looks up the workspace by name and switches to it.
546 void workspace_show_by_name(const char *num) {
548 workspace = workspace_get(num, NULL);
549 workspace_show(workspace);
553 * Focuses the next workspace.
556 Con *workspace_next(void) {
557 Con *current = con_get_workspace(focused);
558 Con *next = NULL, *first = NULL, *first_opposite = NULL;
561 if (current->num == -1) {
562 /* If currently a named workspace, find next named workspace. */
563 if ((next = TAILQ_NEXT(current, nodes)) != NULL)
565 bool found_current = false;
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)
575 if (!first_opposite || (child->num != -1 && child->num < first_opposite->num))
576 first_opposite = child;
577 if (child == current) {
578 found_current = true;
579 } else if (child->num == -1 && found_current) {
586 /* If currently a numbered workspace, find next numbered workspace. */
587 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
588 /* Skip outputs starting with __, they are internal. */
589 if (con_is_internal(output))
591 NODES_FOREACH(output_get_content(output)) {
592 if (child->type != CT_WORKSPACE)
594 if (!first || (child->num != -1 && child->num < first->num))
596 if (!first_opposite && child->num == -1)
597 first_opposite = child;
598 if (child->num == -1)
600 /* Need to check child against current and next because we are
601 * traversing multiple lists and thus are not guaranteed the
602 * relative order between the list of workspaces. */
603 if (current->num < child->num && (!next || child->num < next->num))
610 next = first_opposite ? first_opposite : first;
616 * Focuses the previous workspace.
619 Con *workspace_prev(void) {
620 Con *current = con_get_workspace(focused);
621 Con *prev = NULL, *first_opposite = NULL, *last = NULL;
624 if (current->num == -1) {
625 /* If named workspace, find previous named workspace. */
626 prev = TAILQ_PREV(current, nodes_head, nodes);
627 if (prev && prev->num != -1)
630 bool found_current = false;
631 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
632 /* Skip outputs starting with __, they are internal. */
633 if (con_is_internal(output))
635 NODES_FOREACH_REVERSE(output_get_content(output)) {
636 if (child->type != CT_WORKSPACE)
640 if (!first_opposite || (child->num != -1 && child->num > first_opposite->num))
641 first_opposite = child;
642 if (child == current) {
643 found_current = true;
644 } else if (child->num == -1 && found_current) {
652 /* If numbered workspace, find previous numbered workspace. */
653 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
654 /* Skip outputs starting with __, they are internal. */
655 if (con_is_internal(output))
657 NODES_FOREACH_REVERSE(output_get_content(output)) {
658 if (child->type != CT_WORKSPACE)
660 if (!last || (child->num != -1 && last->num < child->num))
662 if (!first_opposite && child->num == -1)
663 first_opposite = child;
664 if (child->num == -1)
666 /* Need to check child against current and previous because we
667 * are traversing multiple lists and thus are not guaranteed
668 * the relative order between the list of workspaces. */
669 if (current->num > child->num && (!prev || child->num > prev->num))
676 prev = first_opposite ? first_opposite : last;
682 * Focuses the next workspace on the same output.
685 Con *workspace_next_on_output(void) {
686 Con *current = con_get_workspace(focused);
688 Con *output = con_get_output(focused);
690 if (current->num == -1) {
691 /* If currently a named workspace, find next named workspace. */
692 next = TAILQ_NEXT(current, nodes);
694 /* If currently a numbered workspace, find next numbered workspace. */
695 NODES_FOREACH(output_get_content(output)) {
696 if (child->type != CT_WORKSPACE)
698 if (child->num == -1)
700 /* Need to check child against current and next because we are
701 * traversing multiple lists and thus are not guaranteed the
702 * relative order between the list of workspaces. */
703 if (current->num < child->num && (!next || child->num < next->num))
708 /* Find next named workspace. */
710 bool found_current = false;
711 NODES_FOREACH(output_get_content(output)) {
712 if (child->type != CT_WORKSPACE)
714 if (child == current) {
715 found_current = true;
716 } else if (child->num == -1 && (current->num != -1 || found_current)) {
718 goto workspace_next_on_output_end;
723 /* Find first workspace. */
725 NODES_FOREACH(output_get_content(output)) {
726 if (child->type != CT_WORKSPACE)
728 if (!next || (child->num != -1 && child->num < next->num))
732 workspace_next_on_output_end:
737 * Focuses the previous workspace on same output.
740 Con *workspace_prev_on_output(void) {
741 Con *current = con_get_workspace(focused);
743 Con *output = con_get_output(focused);
744 DLOG("output = %s\n", output->name);
746 if (current->num == -1) {
747 /* If named workspace, find previous named workspace. */
748 prev = TAILQ_PREV(current, nodes_head, nodes);
749 if (prev && prev->num != -1)
752 /* If numbered workspace, find previous numbered workspace. */
753 NODES_FOREACH_REVERSE(output_get_content(output)) {
754 if (child->type != CT_WORKSPACE || child->num == -1)
756 /* Need to check child against current and previous because we
757 * are traversing multiple lists and thus are not guaranteed
758 * the relative order between the list of workspaces. */
759 if (current->num > child->num && (!prev || child->num > prev->num))
764 /* Find previous named workspace. */
766 bool found_current = false;
767 NODES_FOREACH_REVERSE(output_get_content(output)) {
768 if (child->type != CT_WORKSPACE)
770 if (child == current) {
771 found_current = true;
772 } else if (child->num == -1 && (current->num != -1 || found_current)) {
774 goto workspace_prev_on_output_end;
779 /* Find last workspace. */
781 NODES_FOREACH_REVERSE(output_get_content(output)) {
782 if (child->type != CT_WORKSPACE)
784 if (!prev || child->num > prev->num)
789 workspace_prev_on_output_end:
794 * Focuses the previously focused workspace.
797 void workspace_back_and_forth(void) {
798 if (!previous_workspace_name) {
799 DLOG("No previous workspace name set. Not switching.\n");
803 workspace_show_by_name(previous_workspace_name);
807 * Returns the previously focused workspace con, or NULL if unavailable.
810 Con *workspace_back_and_forth_get(void) {
811 if (!previous_workspace_name) {
812 DLOG("No previous workspace name set.\n");
817 workspace = workspace_get(previous_workspace_name, NULL);
822 static bool get_urgency_flag(Con *con) {
824 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
825 if (child->urgent || get_urgency_flag(child))
828 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
829 if (child->urgent || get_urgency_flag(child))
836 * Goes through all clients on the given workspace and updates the workspace’s
837 * urgent flag accordingly.
840 void workspace_update_urgent_flag(Con *ws) {
841 bool old_flag = ws->urgent;
842 ws->urgent = get_urgency_flag(ws);
843 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
845 if (old_flag != ws->urgent)
846 ipc_send_workspace_event("urgent", ws, NULL);
850 * 'Forces' workspace orientation by moving all cons into a new split-con with
851 * the same layout as the workspace and then changing the workspace layout.
854 void ws_force_orientation(Con *ws, orientation_t orientation) {
855 /* 1: create a new split container */
856 Con *split = con_new(NULL, NULL);
859 /* 2: copy layout from workspace */
860 split->layout = ws->layout;
862 /* 3: move the existing cons of this workspace below the new con */
863 Con **focus_order = get_focus_order(ws);
865 DLOG("Moving cons\n");
866 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
867 Con *child = TAILQ_FIRST(&(ws->nodes_head));
869 con_attach(child, split, true);
872 set_focus_order(split, focus_order);
875 /* 4: switch workspace layout */
876 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
877 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
879 /* 5: attach the new split container to the workspace */
880 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
881 con_attach(split, ws, false);
883 /* 6: fix the percentages */
888 * Called when a new con (with a window, not an empty or split con) should be
889 * attached to the workspace (for example when managing a new window or when
890 * moving an existing window to the workspace level).
892 * Depending on the workspace_layout setting, this function either returns the
893 * workspace itself (default layout) or creates a new stacked/tabbed con and
897 Con *workspace_attach_to(Con *ws) {
898 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
900 if (ws->workspace_layout == L_DEFAULT) {
901 DLOG("Default layout, just attaching it to the workspace itself.\n");
905 DLOG("Non-default layout, creating a new split container\n");
906 /* 1: create a new split container */
907 Con *new = con_new(NULL, NULL);
910 /* 2: set the requested layout on the split con */
911 new->layout = ws->workspace_layout;
913 /* 4: attach the new split container to the workspace */
914 DLOG("Attaching new split %p to workspace %p\n", new, ws);
915 con_attach(new, ws, false);
917 /* 5: fix the percentages */
924 * Creates a new container and re-parents all of children from the given
927 * The container inherits the layout from the workspace.
929 Con *workspace_encapsulate(Con *ws) {
930 if (TAILQ_EMPTY(&(ws->nodes_head))) {
931 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
935 Con *new = con_new(NULL, NULL);
937 new->layout = ws->layout;
939 Con **focus_order = get_focus_order(ws);
941 DLOG("Moving children of workspace %p / %s into container %p\n",
944 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
945 child = TAILQ_FIRST(&(ws->nodes_head));
947 con_attach(child, new, true);
950 set_focus_order(new, focus_order);
953 con_attach(new, ws, true);
959 * Move the given workspace to the specified output.
960 * This returns true if and only if moving the workspace was successful.
962 bool workspace_move_to_output(Con *ws, Output *output) {
963 LOG("Trying to move workspace %p / %s to output %p / \"%s\".\n", ws, ws->name, output, output_primary_name(output));
965 Output *current_output = get_output_for_con(ws);
966 if (current_output == NULL) {
967 ELOG("Cannot get current output. This is a bug in i3.\n");
971 Con *content = output_get_content(output->con);
972 LOG("got output %p with content %p\n", output, content);
974 Con *previously_visible_ws = TAILQ_FIRST(&(content->focus_head));
975 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
977 bool workspace_was_visible = workspace_is_visible(ws);
978 if (con_num_children(ws->parent) == 1) {
979 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
981 /* check if we can find a workspace assigned to this output */
982 bool used_assignment = false;
983 struct Workspace_Assignment *assignment;
984 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
985 if (!output_triggers_assignment(current_output, assignment)) {
988 /* check if this workspace is already attached to the tree */
989 if (get_existing_workspace_by_name(assignment->name) != NULL) {
993 /* so create the workspace referenced to by this assignment */
994 LOG("Creating workspace from assignment %s.\n", assignment->name);
995 workspace_get(assignment->name, NULL);
996 used_assignment = true;
1000 /* if we couldn't create the workspace using an assignment, create
1001 * it on the output */
1002 if (!used_assignment)
1003 create_workspace_on_output(current_output, ws->parent);
1005 /* notify the IPC listeners */
1006 ipc_send_workspace_event("init", ws, NULL);
1008 DLOG("Detaching\n");
1010 /* detach from the old output and attach to the new output */
1011 Con *old_content = ws->parent;
1013 if (workspace_was_visible) {
1014 /* The workspace which we just detached was visible, so focus
1015 * the next one in the focus-stack. */
1016 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1017 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1018 workspace_show(focus_ws);
1020 con_attach(ws, content, false);
1022 /* fix the coordinates of the floating containers */
1024 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
1025 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1027 ipc_send_workspace_event("move", ws, NULL);
1028 if (workspace_was_visible) {
1029 /* Focus the moved workspace on the destination output. */
1033 /* NB: We cannot simply work with previously_visible_ws since it might
1034 * have been cleaned up by workspace_show() already, depending on the
1035 * focus order/number of other workspaces on the output.
1036 * Instead, we loop through the available workspaces and only work with
1037 * previously_visible_ws if we still find it. */
1038 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1039 if (ws != previously_visible_ws)
1042 /* Call the on_remove_child callback of the workspace which previously
1043 * was visible on the destination output. Since it is no longer
1044 * visible, it might need to get cleaned up. */
1045 CALL(previously_visible_ws, on_remove_child);