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");
252 Con *assigned = get_assigned_output(NULL, c);
253 exists = (get_existing_workspace_by_num(c) || (assigned && assigned != output->con));
254 DLOG("result for ws %d: exists = %d\n", c, exists);
257 sasprintf(&(ws->name), "%d", c);
259 con_attach(ws, content, false);
261 sasprintf(&name, "[i3 con] workspace %s", ws->name);
262 x_set_name(ws, name);
265 ws->fullscreen_mode = CF_OUTPUT;
267 ws->workspace_layout = config.default_layout;
268 _workspace_apply_default_orientation(ws);
274 * Returns true if the workspace is currently visible. Especially important for
275 * multi-monitor environments, as they can have multiple currenlty active
279 bool workspace_is_visible(Con *ws) {
280 Con *output = con_get_output(ws);
283 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
284 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
289 * XXX: we need to clean up all this recursive walking code.
292 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
295 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
296 if (current != exclude &&
297 current->sticky_group != NULL &&
298 current->window != NULL &&
299 strcmp(current->sticky_group, sticky_group) == 0)
302 Con *recurse = _get_sticky(current, sticky_group, exclude);
307 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
308 if (current != exclude &&
309 current->sticky_group != NULL &&
310 current->window != NULL &&
311 strcmp(current->sticky_group, sticky_group) == 0)
314 Con *recurse = _get_sticky(current, sticky_group, exclude);
323 * Reassigns all child windows in sticky containers. Called when the user
324 * changes workspaces.
326 * XXX: what about sticky containers which contain containers?
329 static void workspace_reassign_sticky(Con *con) {
331 /* 1: go through all containers */
333 /* handle all children and floating windows of this node */
334 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
335 if (current->sticky_group == NULL) {
336 workspace_reassign_sticky(current);
340 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
341 /* 2: find a window which we can re-assign */
342 Con *output = con_get_output(current);
343 Con *src = _get_sticky(output, current->sticky_group, current);
346 LOG("No window found for this sticky group\n");
347 workspace_reassign_sticky(current);
351 x_move_win(src, current);
352 current->window = src->window;
353 current->mapped = true;
357 x_reparent_child(current, src);
359 LOG("re-assigned window from src %p to dest %p\n", src, current);
362 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
363 workspace_reassign_sticky(current);
367 * Callback to reset the urgent flag of the given con to false. May be started by
368 * workspace_show to avoid urgency hints being lost by switching to a workspace
372 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
375 ev_timer_stop(main_loop, con->urgency_timer);
376 FREE(con->urgency_timer);
379 DLOG("Resetting urgency flag of con %p by timer\n", con);
380 con_set_urgency(con, false);
381 con_update_parents_urgency(con);
382 workspace_update_urgent_flag(con_get_workspace(con));
383 ipc_send_window_event("urgent", con);
389 * Switches to the given workspace
392 void workspace_show(Con *workspace) {
393 Con *current, *old = NULL;
394 Con *old_focus = focused;
396 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
397 if (con_is_internal(workspace))
400 /* disable fullscreen for the other workspaces and get the workspace we are
402 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
403 if (current->fullscreen_mode == CF_OUTPUT)
405 current->fullscreen_mode = CF_NONE;
408 /* enable fullscreen for the target workspace. If it happens to be the
409 * same one we are currently on anyways, we can stop here. */
410 workspace->fullscreen_mode = CF_OUTPUT;
411 current = con_get_workspace(focused);
412 if (workspace == current) {
413 DLOG("Not switching, already there.\n");
417 /* Remember currently focused workspace for switching back to it later with
418 * the 'workspace back_and_forth' command.
419 * NOTE: We have to duplicate the name as the original will be freed when
420 * the corresponding workspace is cleaned up.
421 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
422 * focused) are skipped, see bug #868. */
423 if (current && !con_is_internal(current)) {
424 FREE(previous_workspace_name);
425 previous_workspace_name = sstrdup(current->name);
426 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
429 workspace_reassign_sticky(workspace);
431 DLOG("switching to %p / %s\n", workspace, workspace->name);
432 Con *next = con_descend_focused(workspace);
434 /* Memorize current output */
435 Con *old_output = con_get_output(focused);
437 /* Display urgency hint for a while if the newly visible workspace would
438 * focus and thereby immediately destroy it */
439 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
441 next->urgent = false;
444 /* … but immediately reset urgency flags; they will be set to false by
445 * the timer callback in case the container is focused at the time of
447 focused->urgent = true;
448 workspace->urgent = true;
450 if (focused->urgency_timer == NULL) {
451 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
453 focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
454 /* use a repeating timer to allow for easy resets */
455 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
456 config.workspace_urgency_timer, config.workspace_urgency_timer);
457 focused->urgency_timer->data = focused;
458 ev_timer_start(main_loop, focused->urgency_timer);
460 DLOG("Resetting urgency timer of con %p on workspace %p\n",
462 ev_timer_again(main_loop, focused->urgency_timer);
467 ipc_send_workspace_event("focus", workspace, current);
469 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
470 /* Close old workspace if necessary. This must be done *after* doing
471 * urgency handling, because tree_close_internal() will do a con_focus() on the next
472 * client, which will clear the urgency flag too early. Also, there is no
473 * way for con_focus() to know about when to clear urgency immediately and
474 * when to defer it. */
475 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
476 /* check if this workspace is currently visible */
477 if (!workspace_is_visible(old)) {
478 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
479 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
480 tree_close_internal(old, DONT_KILL_WINDOW, false, false);
482 const unsigned char *payload;
484 y(get_buf, &payload, &length);
485 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
489 /* Avoid calling output_push_sticky_windows later with a freed container. */
490 if (old == old_focus) {
494 ewmh_update_number_of_desktops();
495 ewmh_update_desktop_names();
496 ewmh_update_desktop_viewport();
497 ewmh_update_wm_desktop();
501 workspace->fullscreen_mode = CF_OUTPUT;
502 LOG("focused now = %p / %s\n", focused, focused->name);
504 /* Set mouse pointer */
505 Con *new_output = con_get_output(focused);
506 if (old_output != new_output) {
507 x_set_warp_to(&next->rect);
510 /* Update the EWMH hints */
511 ewmh_update_current_desktop();
513 /* Push any sticky windows to the now visible workspace. */
514 output_push_sticky_windows(old_focus);
518 * Looks up the workspace by name and switches to it.
521 void workspace_show_by_name(const char *num) {
523 workspace = workspace_get(num, NULL);
524 workspace_show(workspace);
528 * Focuses the next workspace.
531 Con *workspace_next(void) {
532 Con *current = con_get_workspace(focused);
533 Con *next = NULL, *first = NULL, *first_opposite = NULL;
536 if (current->num == -1) {
537 /* If currently a named workspace, find next named workspace. */
538 if ((next = TAILQ_NEXT(current, nodes)) != NULL)
540 bool found_current = false;
541 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
542 /* Skip outputs starting with __, they are internal. */
543 if (con_is_internal(output))
545 NODES_FOREACH(output_get_content(output)) {
546 if (child->type != CT_WORKSPACE)
550 if (!first_opposite || (child->num != -1 && child->num < first_opposite->num))
551 first_opposite = child;
552 if (child == current) {
553 found_current = true;
554 } else if (child->num == -1 && found_current) {
561 /* If currently a numbered workspace, find next numbered workspace. */
562 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
563 /* Skip outputs starting with __, they are internal. */
564 if (con_is_internal(output))
566 NODES_FOREACH(output_get_content(output)) {
567 if (child->type != CT_WORKSPACE)
569 if (!first || (child->num != -1 && child->num < first->num))
571 if (!first_opposite && child->num == -1)
572 first_opposite = child;
573 if (child->num == -1)
575 /* Need to check child against current and next because we are
576 * traversing multiple lists and thus are not guaranteed the
577 * relative order between the list of workspaces. */
578 if (current->num < child->num && (!next || child->num < next->num))
585 next = first_opposite ? first_opposite : first;
591 * Focuses the previous workspace.
594 Con *workspace_prev(void) {
595 Con *current = con_get_workspace(focused);
596 Con *prev = NULL, *first_opposite = NULL, *last = NULL;
599 if (current->num == -1) {
600 /* If named workspace, find previous named workspace. */
601 prev = TAILQ_PREV(current, nodes_head, nodes);
602 if (prev && prev->num != -1)
605 bool found_current = false;
606 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
607 /* Skip outputs starting with __, they are internal. */
608 if (con_is_internal(output))
610 NODES_FOREACH_REVERSE(output_get_content(output)) {
611 if (child->type != CT_WORKSPACE)
615 if (!first_opposite || (child->num != -1 && child->num > first_opposite->num))
616 first_opposite = child;
617 if (child == current) {
618 found_current = true;
619 } else if (child->num == -1 && found_current) {
627 /* If numbered workspace, find previous numbered workspace. */
628 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
629 /* Skip outputs starting with __, they are internal. */
630 if (con_is_internal(output))
632 NODES_FOREACH_REVERSE(output_get_content(output)) {
633 if (child->type != CT_WORKSPACE)
635 if (!last || (child->num != -1 && last->num < child->num))
637 if (!first_opposite && child->num == -1)
638 first_opposite = child;
639 if (child->num == -1)
641 /* Need to check child against current and previous because we
642 * are traversing multiple lists and thus are not guaranteed
643 * the relative order between the list of workspaces. */
644 if (current->num > child->num && (!prev || child->num > prev->num))
651 prev = first_opposite ? first_opposite : last;
657 * Focuses the next workspace on the same output.
660 Con *workspace_next_on_output(void) {
661 Con *current = con_get_workspace(focused);
663 Con *output = con_get_output(focused);
665 if (current->num == -1) {
666 /* If currently a named workspace, find next named workspace. */
667 next = TAILQ_NEXT(current, nodes);
669 /* If currently a numbered workspace, find next numbered workspace. */
670 NODES_FOREACH(output_get_content(output)) {
671 if (child->type != CT_WORKSPACE)
673 if (child->num == -1)
675 /* Need to check child against current and next because we are
676 * traversing multiple lists and thus are not guaranteed the
677 * relative order between the list of workspaces. */
678 if (current->num < child->num && (!next || child->num < next->num))
683 /* Find next named workspace. */
685 bool found_current = false;
686 NODES_FOREACH(output_get_content(output)) {
687 if (child->type != CT_WORKSPACE)
689 if (child == current) {
690 found_current = true;
691 } else if (child->num == -1 && (current->num != -1 || found_current)) {
693 goto workspace_next_on_output_end;
698 /* Find first workspace. */
700 NODES_FOREACH(output_get_content(output)) {
701 if (child->type != CT_WORKSPACE)
703 if (!next || (child->num != -1 && child->num < next->num))
707 workspace_next_on_output_end:
712 * Focuses the previous workspace on same output.
715 Con *workspace_prev_on_output(void) {
716 Con *current = con_get_workspace(focused);
718 Con *output = con_get_output(focused);
719 DLOG("output = %s\n", output->name);
721 if (current->num == -1) {
722 /* If named workspace, find previous named workspace. */
723 prev = TAILQ_PREV(current, nodes_head, nodes);
724 if (prev && prev->num != -1)
727 /* If numbered workspace, find previous numbered workspace. */
728 NODES_FOREACH_REVERSE(output_get_content(output)) {
729 if (child->type != CT_WORKSPACE || child->num == -1)
731 /* Need to check child against current and previous because we
732 * are traversing multiple lists and thus are not guaranteed
733 * the relative order between the list of workspaces. */
734 if (current->num > child->num && (!prev || child->num > prev->num))
739 /* Find previous named workspace. */
741 bool found_current = false;
742 NODES_FOREACH_REVERSE(output_get_content(output)) {
743 if (child->type != CT_WORKSPACE)
745 if (child == current) {
746 found_current = true;
747 } else if (child->num == -1 && (current->num != -1 || found_current)) {
749 goto workspace_prev_on_output_end;
754 /* Find last workspace. */
756 NODES_FOREACH_REVERSE(output_get_content(output)) {
757 if (child->type != CT_WORKSPACE)
759 if (!prev || child->num > prev->num)
764 workspace_prev_on_output_end:
769 * Focuses the previously focused workspace.
772 void workspace_back_and_forth(void) {
773 if (!previous_workspace_name) {
774 DLOG("No previous workspace name set. Not switching.\n");
778 workspace_show_by_name(previous_workspace_name);
782 * Returns the previously focused workspace con, or NULL if unavailable.
785 Con *workspace_back_and_forth_get(void) {
786 if (!previous_workspace_name) {
787 DLOG("No previous workspace name set.\n");
792 workspace = workspace_get(previous_workspace_name, NULL);
797 static bool get_urgency_flag(Con *con) {
799 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
800 if (child->urgent || get_urgency_flag(child))
803 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
804 if (child->urgent || get_urgency_flag(child))
811 * Goes through all clients on the given workspace and updates the workspace’s
812 * urgent flag accordingly.
815 void workspace_update_urgent_flag(Con *ws) {
816 bool old_flag = ws->urgent;
817 ws->urgent = get_urgency_flag(ws);
818 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
820 if (old_flag != ws->urgent)
821 ipc_send_workspace_event("urgent", ws, NULL);
825 * 'Forces' workspace orientation by moving all cons into a new split-con with
826 * the same layout as the workspace and then changing the workspace layout.
829 void ws_force_orientation(Con *ws, orientation_t orientation) {
830 /* 1: create a new split container */
831 Con *split = con_new(NULL, NULL);
834 /* 2: copy layout from workspace */
835 split->layout = ws->layout;
837 /* 3: move the existing cons of this workspace below the new con */
838 Con **focus_order = get_focus_order(ws);
840 DLOG("Moving cons\n");
841 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
842 Con *child = TAILQ_FIRST(&(ws->nodes_head));
844 con_attach(child, split, true);
847 set_focus_order(split, focus_order);
850 /* 4: switch workspace layout */
851 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
852 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
854 /* 5: attach the new split container to the workspace */
855 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
856 con_attach(split, ws, false);
858 /* 6: fix the percentages */
863 * Called when a new con (with a window, not an empty or split con) should be
864 * attached to the workspace (for example when managing a new window or when
865 * moving an existing window to the workspace level).
867 * Depending on the workspace_layout setting, this function either returns the
868 * workspace itself (default layout) or creates a new stacked/tabbed con and
872 Con *workspace_attach_to(Con *ws) {
873 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
875 if (ws->workspace_layout == L_DEFAULT) {
876 DLOG("Default layout, just attaching it to the workspace itself.\n");
880 DLOG("Non-default layout, creating a new split container\n");
881 /* 1: create a new split container */
882 Con *new = con_new(NULL, NULL);
885 /* 2: set the requested layout on the split con */
886 new->layout = ws->workspace_layout;
888 /* 4: attach the new split container to the workspace */
889 DLOG("Attaching new split %p to workspace %p\n", new, ws);
890 con_attach(new, ws, false);
892 /* 5: fix the percentages */
899 * Creates a new container and re-parents all of children from the given
902 * The container inherits the layout from the workspace.
904 Con *workspace_encapsulate(Con *ws) {
905 if (TAILQ_EMPTY(&(ws->nodes_head))) {
906 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
910 Con *new = con_new(NULL, NULL);
912 new->layout = ws->layout;
914 Con **focus_order = get_focus_order(ws);
916 DLOG("Moving children of workspace %p / %s into container %p\n",
919 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
920 child = TAILQ_FIRST(&(ws->nodes_head));
922 con_attach(child, new, true);
925 set_focus_order(new, focus_order);
928 con_attach(new, ws, true);
934 * Move the given workspace to the specified output.
935 * This returns true if and only if moving the workspace was successful.
937 bool workspace_move_to_output(Con *ws, Output *output) {
938 LOG("Trying to move workspace %p / %s to output %p / \"%s\".\n", ws, ws->name, output, output_primary_name(output));
940 Output *current_output = get_output_for_con(ws);
941 if (current_output == NULL) {
942 ELOG("Cannot get current output. This is a bug in i3.\n");
946 Con *content = output_get_content(output->con);
947 LOG("got output %p with content %p\n", output, content);
949 Con *previously_visible_ws = TAILQ_FIRST(&(content->focus_head));
950 LOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
952 bool workspace_was_visible = workspace_is_visible(ws);
953 if (con_num_children(ws->parent) == 1) {
954 LOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
956 /* check if we can find a workspace assigned to this output */
957 bool used_assignment = false;
958 struct Workspace_Assignment *assignment;
959 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
960 if (assignment->output == NULL || strcmp(assignment->output, output_primary_name(current_output)) != 0)
962 /* check if this workspace is already attached to the tree */
963 if (get_existing_workspace_by_name(assignment->name) != NULL) {
967 /* so create the workspace referenced to by this assignment */
968 LOG("Creating workspace from assignment %s.\n", assignment->name);
969 workspace_get(assignment->name, NULL);
970 used_assignment = true;
974 /* if we couldn't create the workspace using an assignment, create
975 * it on the output */
976 if (!used_assignment)
977 create_workspace_on_output(current_output, ws->parent);
979 /* notify the IPC listeners */
980 ipc_send_workspace_event("init", ws, NULL);
984 /* detach from the old output and attach to the new output */
985 Con *old_content = ws->parent;
987 if (workspace_was_visible) {
988 /* The workspace which we just detached was visible, so focus
989 * the next one in the focus-stack. */
990 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
991 LOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
992 workspace_show(focus_ws);
994 con_attach(ws, content, false);
996 /* fix the coordinates of the floating containers */
998 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows)
999 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1001 ipc_send_workspace_event("move", ws, NULL);
1002 if (workspace_was_visible) {
1003 /* Focus the moved workspace on the destination output. */
1007 /* NB: We cannot simply work with previously_visible_ws since it might
1008 * have been cleaned up by workspace_show() already, depending on the
1009 * focus order/number of other workspaces on the output.
1010 * Instead, we loop through the available workspaces and only work with
1011 * previously_visible_ws if we still find it. */
1012 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1013 if (ws != previously_visible_ws)
1016 /* Call the on_remove_child callback of the workspace which previously
1017 * was visible on the destination output. Since it is no longer
1018 * visible, it might need to get cleaned up. */
1019 CALL(previously_visible_ws, on_remove_child);