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 && child->num < first_opposite->num))
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)
547 if (!first || (child->num != -1 && child->num < first->num))
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 && child->num > first_opposite->num))
594 first_opposite = child;
595 if (child == current) {
596 found_current = true;
597 } else if (child->num == -1 && found_current) {
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)
613 if (!last || (child->num != -1 && last->num < child->num))
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;
635 * Focuses the next workspace on the same output.
638 Con *workspace_next_on_output(void) {
639 Con *current = con_get_workspace(focused);
641 Con *output = con_get_output(focused);
643 if (current->num == -1) {
644 /* If currently a named workspace, find next named workspace. */
645 next = TAILQ_NEXT(current, nodes);
647 /* If currently a numbered workspace, find next numbered workspace. */
648 NODES_FOREACH(output_get_content(output)) {
649 if (child->type != CT_WORKSPACE)
651 if (child->num == -1)
653 /* Need to check child against current and next because we are
654 * traversing multiple lists and thus are not guaranteed the
655 * relative order between the list of workspaces. */
656 if (current->num < child->num && (!next || child->num < next->num))
661 /* Find next named workspace. */
663 bool found_current = false;
664 NODES_FOREACH(output_get_content(output)) {
665 if (child->type != CT_WORKSPACE)
667 if (child == current) {
668 found_current = true;
669 } else if (child->num == -1 && (current->num != -1 || found_current)) {
671 goto workspace_next_on_output_end;
676 /* Find first workspace. */
678 NODES_FOREACH(output_get_content(output)) {
679 if (child->type != CT_WORKSPACE)
681 if (!next || (child->num != -1 && child->num < next->num))
685 workspace_next_on_output_end:
690 * Focuses the previous workspace on same output.
693 Con *workspace_prev_on_output(void) {
694 Con *current = con_get_workspace(focused);
696 Con *output = con_get_output(focused);
697 DLOG("output = %s\n", output->name);
699 if (current->num == -1) {
700 /* If named workspace, find previous named workspace. */
701 prev = TAILQ_PREV(current, nodes_head, nodes);
702 if (prev && prev->num != -1)
705 /* If numbered workspace, find previous numbered workspace. */
706 NODES_FOREACH_REVERSE(output_get_content(output)) {
707 if (child->type != CT_WORKSPACE || child->num == -1)
709 /* Need to check child against current and previous because we
710 * are traversing multiple lists and thus are not guaranteed
711 * the relative order between the list of workspaces. */
712 if (current->num > child->num && (!prev || child->num > prev->num))
717 /* Find previous named workspace. */
719 bool found_current = false;
720 NODES_FOREACH_REVERSE(output_get_content(output)) {
721 if (child->type != CT_WORKSPACE)
723 if (child == current) {
724 found_current = true;
725 } else if (child->num == -1 && (current->num != -1 || found_current)) {
727 goto workspace_prev_on_output_end;
732 /* Find last workspace. */
734 NODES_FOREACH_REVERSE(output_get_content(output)) {
735 if (child->type != CT_WORKSPACE)
737 if (!prev || child->num > prev->num)
742 workspace_prev_on_output_end:
747 * Focuses the previously focused workspace.
750 void workspace_back_and_forth(void) {
751 if (!previous_workspace_name) {
752 DLOG("No previous workspace name set. Not switching.\n");
756 workspace_show_by_name(previous_workspace_name);
760 * Returns the previously focused workspace con, or NULL if unavailable.
763 Con *workspace_back_and_forth_get(void) {
764 if (!previous_workspace_name) {
765 DLOG("No previous workspace name set.\n");
770 workspace = workspace_get(previous_workspace_name, NULL);
775 static bool get_urgency_flag(Con *con) {
777 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
778 if (child->urgent || get_urgency_flag(child))
781 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
782 if (child->urgent || get_urgency_flag(child))
789 * Goes through all clients on the given workspace and updates the workspace’s
790 * urgent flag accordingly.
793 void workspace_update_urgent_flag(Con *ws) {
794 bool old_flag = ws->urgent;
795 ws->urgent = get_urgency_flag(ws);
796 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
798 if (old_flag != ws->urgent)
799 ipc_send_workspace_event("urgent", ws, NULL);
803 * 'Forces' workspace orientation by moving all cons into a new split-con with
804 * the same layout as the workspace and then changing the workspace layout.
807 void ws_force_orientation(Con *ws, orientation_t orientation) {
808 /* 1: create a new split container */
809 Con *split = con_new(NULL, NULL);
812 /* 2: copy layout from workspace */
813 split->layout = ws->layout;
815 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
817 /* 3: move the existing cons of this workspace below the new con */
818 DLOG("Moving cons\n");
819 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
820 Con *child = TAILQ_FIRST(&(ws->nodes_head));
822 con_attach(child, split, true);
825 /* 4: switch workspace layout */
826 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
827 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
829 /* 5: attach the new split container to the workspace */
830 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
831 con_attach(split, ws, false);
833 /* 6: fix the percentages */
837 con_focus(old_focused);
841 * Called when a new con (with a window, not an empty or split con) should be
842 * attached to the workspace (for example when managing a new window or when
843 * moving an existing window to the workspace level).
845 * Depending on the workspace_layout setting, this function either returns the
846 * workspace itself (default layout) or creates a new stacked/tabbed con and
850 Con *workspace_attach_to(Con *ws) {
851 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
853 if (ws->workspace_layout == L_DEFAULT) {
854 DLOG("Default layout, just attaching it to the workspace itself.\n");
858 DLOG("Non-default layout, creating a new split container\n");
859 /* 1: create a new split container */
860 Con *new = con_new(NULL, NULL);
863 /* 2: set the requested layout on the split con */
864 new->layout = ws->workspace_layout;
866 /* 4: attach the new split container to the workspace */
867 DLOG("Attaching new split %p to workspace %p\n", new, ws);
868 con_attach(new, ws, false);
870 /* 5: fix the percentages */
877 * Creates a new container and re-parents all of children from the given
880 * The container inherits the layout from the workspace.
882 Con *workspace_encapsulate(Con *ws) {
883 if (TAILQ_EMPTY(&(ws->nodes_head))) {
884 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
888 Con *new = con_new(NULL, NULL);
890 new->layout = ws->layout;
892 DLOG("Moving children of workspace %p / %s into container %p\n",
896 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
897 child = TAILQ_FIRST(&(ws->nodes_head));
899 con_attach(child, new, true);
902 con_attach(new, ws, true);
908 * Move the given workspace to the specified output.
909 * This returns true if and only if moving the workspace was successful.
911 bool workspace_move_to_output(Con *ws, const char *name) {
912 LOG("Trying to move workspace %p / %s to output \"%s\".\n", ws, ws->name, name);
914 Con *current_output_con = con_get_output(ws);
915 if (!current_output_con) {
916 ELOG("Could not get the output container for workspace %p / %s.\n", ws, ws->name);
920 Output *current_output = get_output_by_name(current_output_con->name);
921 if (!current_output) {
922 ELOG("Cannot get current output. This is a bug in i3.\n");
925 Output *output = get_output_from_string(current_output, name);
927 ELOG("Could not get output from string \"%s\"\n", name);
931 Con *content = output_get_content(output->con);
932 LOG("got output %p with content %p\n", output, content);
934 Con *previously_visible_ws = TAILQ_FIRST(&(content->focus_head));
935 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
937 bool workspace_was_visible = workspace_is_visible(ws);
938 if (con_num_children(ws->parent) == 1) {
939 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
941 /* check if we can find a workspace assigned to this output */
942 bool used_assignment = false;
943 struct Workspace_Assignment *assignment;
944 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
945 if (assignment->output == NULL || strcmp(assignment->output, current_output->name) != 0)
948 /* check if this workspace is already attached to the tree */
949 Con *workspace = NULL, *out;
950 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
951 GREP_FIRST(workspace, output_get_content(out),
952 !strcasecmp(child->name, assignment->name));
953 if (workspace != NULL)
956 /* so create the workspace referenced to by this assignment */
957 LOG("Creating workspace from assignment %s.\n", assignment->name);
958 workspace_get(assignment->name, NULL);
959 used_assignment = true;
963 /* if we couldn't create the workspace using an assignment, create
964 * it on the output */
965 if (!used_assignment)
966 create_workspace_on_output(current_output, ws->parent);
968 /* notify the IPC listeners */
969 ipc_send_workspace_event("init", ws, NULL);
973 /* detach from the old output and attach to the new output */
974 Con *old_content = ws->parent;
976 if (workspace_was_visible) {
977 /* The workspace which we just detached was visible, so focus
978 * the next one in the focus-stack. */
979 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
980 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
981 workspace_show(focus_ws);
983 con_attach(ws, content, false);
985 /* fix the coordinates of the floating containers */
987 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
988 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
990 ipc_send_workspace_event("move", ws, NULL);
991 if (workspace_was_visible) {
992 /* Focus the moved workspace on the destination output. */
996 /* NB: We cannot simply work with previously_visible_ws since it might
997 * have been cleaned up by workspace_show() already, depending on the
998 * focus order/number of other workspaces on the output.
999 * Instead, we loop through the available workspaces and only work with
1000 * previously_visible_ws if we still find it. */
1001 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1002 if (ws != previously_visible_ws)
1005 /* Call the on_remove_child callback of the workspace which previously
1006 * was visible on the destination output. Since it is no longer
1007 * visible, it might need to get cleaned up. */
1008 CALL(previously_visible_ws, on_remove_child);