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"
15 * Stores a copy of the name of the last used workspace for the workspace
16 * back-and-forth switching.
19 char *previous_workspace_name = NULL;
21 /* NULL-terminated list of workspace names (in order) extracted from
23 static char **binding_workspace_names = NULL;
26 * Returns the workspace with the given name or NULL if such a workspace does
30 Con *get_existing_workspace_by_name(const char *name) {
31 Con *output, *workspace = NULL;
32 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
33 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, name));
40 * Returns the workspace with the given number or NULL if such a workspace does
44 Con *get_existing_workspace_by_num(int num) {
45 Con *output, *workspace = NULL;
46 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
47 GREP_FIRST(workspace, output_get_content(output), child->num == num);
54 * Sets ws->layout to splith/splitv if default_orientation was specified in the
55 * configfile. Otherwise, it uses splith/splitv depending on whether the output
56 * is higher than wide.
59 static void _workspace_apply_default_orientation(Con *ws) {
60 /* If default_orientation is set to NO_ORIENTATION we determine
61 * orientation depending on output resolution. */
62 if (config.default_orientation == NO_ORIENTATION) {
63 Con *output = con_get_output(ws);
64 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
65 ws->rect = output->rect;
66 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
67 output->rect.width, output->rect.height, ws->layout);
69 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
74 * Returns the first output that is assigned to a workspace specified by the
75 * given name or number or NULL if no such output exists. If there is a
76 * workspace with a matching name and another workspace with a matching number,
77 * the output assigned to the first one is returned.
78 * The order of the 'ws_assignments' queue is respected: if multiple assignments
79 * match the specified workspace, the first one is returned.
80 * If 'name' is NULL it will be ignored.
81 * If 'parsed_num' is -1 it will be ignored.
84 static Con *get_assigned_output(const char *name, long parsed_num) {
86 struct Workspace_Assignment *assignment;
87 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
88 if (name && strcmp(assignment->name, name) == 0) {
89 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
90 Output *assigned_by_name = get_output_by_name(assignment->output, true);
91 if (assigned_by_name) {
92 /* When the name matches exactly, skip numbered assignments. */
93 return assigned_by_name->con;
95 } else if (!output && /* Only keep the first numbered assignment. */
97 name_is_digits(assignment->name) &&
98 ws_name_to_number(assignment->name) == parsed_num) {
99 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
100 Output *assigned_by_num = get_output_by_name(assignment->output, true);
101 if (assigned_by_num) {
102 output = assigned_by_num->con;
111 * Returns true if the first output assigned to a workspace with the given
112 * workspace assignment is the same as the given output.
114 bool output_triggers_assignment(Output *output, struct Workspace_Assignment *assignment) {
115 Con *assigned = get_assigned_output(assignment->name, -1);
116 return assigned && assigned == output->con;
120 * Returns a pointer to the workspace with the given number (starting at 0),
121 * creating the workspace if necessary (by allocating the necessary amount of
122 * memory and initializing the data structures correctly).
125 Con *workspace_get(const char *num, bool *created) {
126 Con *workspace = get_existing_workspace_by_name(num);
128 if (workspace == NULL) {
129 LOG("Creating new workspace \"%s\"\n", num);
131 /* We set workspace->num to the number if this workspace’s name begins
132 * with a positive number. Otherwise it’s a named ws and num will be
134 long parsed_num = ws_name_to_number(num);
136 Con *output = get_assigned_output(num, parsed_num);
137 /* if an assignment is not found, we create this workspace on the current output */
139 output = con_get_output(focused);
142 Con *content = output_get_content(output);
143 LOG("got output %p with content %p\n", output, content);
144 /* We need to attach this container after setting its type. con_attach
145 * will handle CT_WORKSPACEs differently */
146 workspace = con_new(NULL, NULL);
148 sasprintf(&name, "[i3 con] workspace %s", num);
149 x_set_name(workspace, name);
151 workspace->type = CT_WORKSPACE;
152 FREE(workspace->name);
153 workspace->name = sstrdup(num);
154 workspace->workspace_layout = config.default_layout;
155 workspace->num = parsed_num;
156 LOG("num = %d\n", workspace->num);
158 workspace->parent = content;
159 _workspace_apply_default_orientation(workspace);
161 con_attach(workspace, content, false);
163 ipc_send_workspace_event("init", workspace, NULL);
164 ewmh_update_number_of_desktops();
165 ewmh_update_desktop_names();
166 ewmh_update_desktop_viewport();
167 ewmh_update_wm_desktop();
170 } else if (created != NULL) {
178 * Extracts workspace names from keybindings (e.g. “web” from “bindsym $mod+1
179 * workspace web”), so that when an output needs a workspace, i3 can start with
180 * the first configured one. Needs to be called before reorder_bindings() so
181 * that the config-file order is used, not the i3-internal order.
184 void extract_workspace_names_from_bindings(void) {
187 if (binding_workspace_names != NULL) {
188 for (int i = 0; binding_workspace_names[i] != NULL; i++) {
189 free(binding_workspace_names[i]);
191 FREE(binding_workspace_names);
193 TAILQ_FOREACH(bind, bindings, bindings) {
194 DLOG("binding with command %s\n", bind->command);
195 if (strlen(bind->command) < strlen("workspace ") ||
196 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
198 DLOG("relevant command = %s\n", bind->command);
199 const char *target = bind->command + strlen("workspace ");
200 while (*target == ' ' || *target == '\t')
202 /* We check if this is the workspace
203 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
204 * Beware: The workspace names "next", "prev", "next_on_output",
205 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
206 * so we check before stripping the double quotes */
207 if (strncasecmp(target, "next", strlen("next")) == 0 ||
208 strncasecmp(target, "prev", strlen("prev")) == 0 ||
209 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
210 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
211 strncasecmp(target, "number", strlen("number")) == 0 ||
212 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
213 strncasecmp(target, "current", strlen("current")) == 0)
215 char *target_name = parse_string(&target, false);
216 if (target_name == NULL)
218 if (strncasecmp(target_name, "__", strlen("__")) == 0) {
219 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
223 DLOG("Saving workspace name \"%s\"\n", target_name);
225 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
226 binding_workspace_names[n - 1] = target_name;
228 binding_workspace_names = srealloc(binding_workspace_names, ++n * sizeof(char *));
229 binding_workspace_names[n - 1] = NULL;
233 * Returns a pointer to a new workspace in the given output. The workspace
234 * is created attached to the tree hierarchy through the given content
238 Con *create_workspace_on_output(Output *output, Con *content) {
239 /* add a workspace to this output */
242 Con *ws = con_new(NULL, NULL);
243 ws->type = CT_WORKSPACE;
245 /* try the configured workspace bindings first to find a free name */
246 for (int n = 0; binding_workspace_names[n] != NULL; n++) {
247 char *target_name = binding_workspace_names[n];
248 /* Ensure that this workspace is not assigned to a different output —
249 * otherwise we would create it, then move it over to its output, then
250 * find a new workspace, etc… */
251 Con *assigned = get_assigned_output(target_name, -1);
252 if (assigned && assigned != output->con) {
256 exists = (get_existing_workspace_by_name(target_name) != NULL);
258 ws->name = sstrdup(target_name);
259 /* Set ->num to the number of the workspace, if the name actually
260 * is a number or starts with a number */
261 ws->num = ws_name_to_number(ws->name);
262 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
269 /* get the next unused workspace number */
270 DLOG("Getting next unused workspace by number\n");
274 Con *assigned = get_assigned_output(NULL, c);
275 exists = (get_existing_workspace_by_num(c) || (assigned && assigned != output->con));
276 DLOG("result for ws %d: exists = %d\n", c, exists);
279 sasprintf(&(ws->name), "%d", c);
281 con_attach(ws, content, false);
283 sasprintf(&name, "[i3 con] workspace %s", ws->name);
284 x_set_name(ws, name);
287 ws->fullscreen_mode = CF_OUTPUT;
289 ws->workspace_layout = config.default_layout;
290 _workspace_apply_default_orientation(ws);
292 ipc_send_workspace_event("init", ws, NULL);
297 * Returns true if the workspace is currently visible. Especially important for
298 * multi-monitor environments, as they can have multiple currenlty active
302 bool workspace_is_visible(Con *ws) {
303 Con *output = con_get_output(ws);
306 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
307 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
312 * XXX: we need to clean up all this recursive walking code.
315 static Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
318 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
319 if (current != exclude &&
320 current->sticky_group != NULL &&
321 current->window != NULL &&
322 strcmp(current->sticky_group, sticky_group) == 0)
325 Con *recurse = _get_sticky(current, sticky_group, exclude);
330 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
331 if (current != exclude &&
332 current->sticky_group != NULL &&
333 current->window != NULL &&
334 strcmp(current->sticky_group, sticky_group) == 0)
337 Con *recurse = _get_sticky(current, sticky_group, exclude);
346 * Reassigns all child windows in sticky containers. Called when the user
347 * changes workspaces.
349 * XXX: what about sticky containers which contain containers?
352 static void workspace_reassign_sticky(Con *con) {
354 /* 1: go through all containers */
356 /* handle all children and floating windows of this node */
357 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
358 if (current->sticky_group == NULL) {
359 workspace_reassign_sticky(current);
363 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
364 /* 2: find a window which we can re-assign */
365 Con *output = con_get_output(current);
366 Con *src = _get_sticky(output, current->sticky_group, current);
369 LOG("No window found for this sticky group\n");
370 workspace_reassign_sticky(current);
374 x_move_win(src, current);
375 current->window = src->window;
376 current->mapped = true;
380 x_reparent_child(current, src);
382 LOG("re-assigned window from src %p to dest %p\n", src, current);
385 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
386 workspace_reassign_sticky(current);
390 * Callback to reset the urgent flag of the given con to false. May be started by
391 * workspace_show to avoid urgency hints being lost by switching to a workspace
395 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
398 ev_timer_stop(main_loop, con->urgency_timer);
399 FREE(con->urgency_timer);
402 DLOG("Resetting urgency flag of con %p by timer\n", con);
403 con_set_urgency(con, false);
404 con_update_parents_urgency(con);
405 workspace_update_urgent_flag(con_get_workspace(con));
406 ipc_send_window_event("urgent", con);
412 * Switches to the given workspace
415 void workspace_show(Con *workspace) {
416 Con *current, *old = NULL;
418 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
419 if (con_is_internal(workspace))
422 /* disable fullscreen for the other workspaces and get the workspace we are
424 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
425 if (current->fullscreen_mode == CF_OUTPUT)
427 current->fullscreen_mode = CF_NONE;
430 /* enable fullscreen for the target workspace. If it happens to be the
431 * same one we are currently on anyways, we can stop here. */
432 workspace->fullscreen_mode = CF_OUTPUT;
433 current = con_get_workspace(focused);
434 if (workspace == current) {
435 DLOG("Not switching, already there.\n");
439 /* Used to correctly update focus when pushing sticky windows. Holds the
440 * previously focused container in the same output as workspace. For
441 * example, if a sticky window is focused and then we switch focus to a
442 * workspace in another output and then switch to a third workspace in the
443 * first output, the sticky window needs to be refocused. */
444 Con *old_focus = old ? con_descend_focused(old) : NULL;
446 /* Remember currently focused workspace for switching back to it later with
447 * the 'workspace back_and_forth' command.
448 * NOTE: We have to duplicate the name as the original will be freed when
449 * the corresponding workspace is cleaned up.
450 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
451 * focused) are skipped, see bug #868. */
452 if (current && !con_is_internal(current)) {
453 FREE(previous_workspace_name);
454 previous_workspace_name = sstrdup(current->name);
455 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
458 workspace_reassign_sticky(workspace);
460 DLOG("switching to %p / %s\n", workspace, workspace->name);
461 Con *next = con_descend_focused(workspace);
463 /* Memorize current output */
464 Con *old_output = con_get_output(focused);
466 /* Display urgency hint for a while if the newly visible workspace would
467 * focus and thereby immediately destroy it */
468 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
470 next->urgent = false;
473 /* … but immediately reset urgency flags; they will be set to false by
474 * the timer callback in case the container is focused at the time of
476 focused->urgent = true;
477 workspace->urgent = true;
479 if (focused->urgency_timer == NULL) {
480 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
482 focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
483 /* use a repeating timer to allow for easy resets */
484 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
485 config.workspace_urgency_timer, config.workspace_urgency_timer);
486 focused->urgency_timer->data = focused;
487 ev_timer_start(main_loop, focused->urgency_timer);
489 DLOG("Resetting urgency timer of con %p on workspace %p\n",
491 ev_timer_again(main_loop, focused->urgency_timer);
496 ipc_send_workspace_event("focus", workspace, current);
498 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
499 /* Close old workspace if necessary. This must be done *after* doing
500 * urgency handling, because tree_close_internal() will do a con_focus() on the next
501 * client, which will clear the urgency flag too early. Also, there is no
502 * way for con_focus() to know about when to clear urgency immediately and
503 * when to defer it. */
504 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
505 /* check if this workspace is currently visible */
506 if (!workspace_is_visible(old)) {
507 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
508 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
509 tree_close_internal(old, DONT_KILL_WINDOW, false);
511 const unsigned char *payload;
513 y(get_buf, &payload, &length);
514 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
518 /* Avoid calling output_push_sticky_windows later with a freed container. */
519 if (old == old_focus) {
523 ewmh_update_number_of_desktops();
524 ewmh_update_desktop_names();
525 ewmh_update_desktop_viewport();
526 ewmh_update_wm_desktop();
530 workspace->fullscreen_mode = CF_OUTPUT;
531 LOG("focused now = %p / %s\n", focused, focused->name);
533 /* Set mouse pointer */
534 Con *new_output = con_get_output(focused);
535 if (old_output != new_output) {
536 x_set_warp_to(&next->rect);
539 /* Update the EWMH hints */
540 ewmh_update_current_desktop();
542 /* Push any sticky windows to the now visible workspace. */
543 output_push_sticky_windows(old_focus);
547 * Looks up the workspace by name and switches to it.
550 void workspace_show_by_name(const char *num) {
552 workspace = workspace_get(num, NULL);
553 workspace_show(workspace);
557 * Focuses the next workspace.
560 Con *workspace_next(void) {
561 Con *current = con_get_workspace(focused);
562 Con *next = NULL, *first = NULL, *first_opposite = NULL;
565 if (current->num == -1) {
566 /* If currently a named workspace, find next named workspace. */
567 if ((next = TAILQ_NEXT(current, nodes)) != NULL)
569 bool found_current = false;
570 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
571 /* Skip outputs starting with __, they are internal. */
572 if (con_is_internal(output))
574 NODES_FOREACH(output_get_content(output)) {
575 if (child->type != CT_WORKSPACE)
579 if (!first_opposite || (child->num != -1 && child->num < first_opposite->num))
580 first_opposite = child;
581 if (child == current) {
582 found_current = true;
583 } else if (child->num == -1 && found_current) {
590 /* If currently a numbered workspace, find next numbered workspace. */
591 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
592 /* Skip outputs starting with __, they are internal. */
593 if (con_is_internal(output))
595 NODES_FOREACH(output_get_content(output)) {
596 if (child->type != CT_WORKSPACE)
598 if (!first || (child->num != -1 && child->num < first->num))
600 if (!first_opposite && child->num == -1)
601 first_opposite = child;
602 if (child->num == -1)
604 /* Need to check child against current and next because we are
605 * traversing multiple lists and thus are not guaranteed the
606 * relative order between the list of workspaces. */
607 if (current->num < child->num && (!next || child->num < next->num))
614 next = first_opposite ? first_opposite : first;
620 * Focuses the previous workspace.
623 Con *workspace_prev(void) {
624 Con *current = con_get_workspace(focused);
625 Con *prev = NULL, *first_opposite = NULL, *last = NULL;
628 if (current->num == -1) {
629 /* If named workspace, find previous named workspace. */
630 prev = TAILQ_PREV(current, nodes_head, nodes);
631 if (prev && prev->num != -1)
634 bool found_current = false;
635 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
636 /* Skip outputs starting with __, they are internal. */
637 if (con_is_internal(output))
639 NODES_FOREACH_REVERSE(output_get_content(output)) {
640 if (child->type != CT_WORKSPACE)
644 if (!first_opposite || (child->num != -1 && child->num > first_opposite->num))
645 first_opposite = child;
646 if (child == current) {
647 found_current = true;
648 } else if (child->num == -1 && found_current) {
656 /* If numbered workspace, find previous numbered workspace. */
657 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
658 /* Skip outputs starting with __, they are internal. */
659 if (con_is_internal(output))
661 NODES_FOREACH_REVERSE(output_get_content(output)) {
662 if (child->type != CT_WORKSPACE)
664 if (!last || (child->num != -1 && last->num < child->num))
666 if (!first_opposite && child->num == -1)
667 first_opposite = child;
668 if (child->num == -1)
670 /* Need to check child against current and previous because we
671 * are traversing multiple lists and thus are not guaranteed
672 * the relative order between the list of workspaces. */
673 if (current->num > child->num && (!prev || child->num > prev->num))
680 prev = first_opposite ? first_opposite : last;
686 * Focuses the next workspace on the same output.
689 Con *workspace_next_on_output(void) {
690 Con *current = con_get_workspace(focused);
692 Con *output = con_get_output(focused);
694 if (current->num == -1) {
695 /* If currently a named workspace, find next named workspace. */
696 next = TAILQ_NEXT(current, nodes);
698 /* If currently a numbered workspace, find next numbered workspace. */
699 NODES_FOREACH(output_get_content(output)) {
700 if (child->type != CT_WORKSPACE)
702 if (child->num == -1)
704 /* Need to check child against current and next because we are
705 * traversing multiple lists and thus are not guaranteed the
706 * relative order between the list of workspaces. */
707 if (current->num < child->num && (!next || child->num < next->num))
712 /* Find next named workspace. */
714 bool found_current = false;
715 NODES_FOREACH(output_get_content(output)) {
716 if (child->type != CT_WORKSPACE)
718 if (child == current) {
719 found_current = true;
720 } else if (child->num == -1 && (current->num != -1 || found_current)) {
722 goto workspace_next_on_output_end;
727 /* Find first workspace. */
729 NODES_FOREACH(output_get_content(output)) {
730 if (child->type != CT_WORKSPACE)
732 if (!next || (child->num != -1 && child->num < next->num))
736 workspace_next_on_output_end:
741 * Focuses the previous workspace on same output.
744 Con *workspace_prev_on_output(void) {
745 Con *current = con_get_workspace(focused);
747 Con *output = con_get_output(focused);
748 DLOG("output = %s\n", output->name);
750 if (current->num == -1) {
751 /* If named workspace, find previous named workspace. */
752 prev = TAILQ_PREV(current, nodes_head, nodes);
753 if (prev && prev->num != -1)
756 /* If numbered workspace, find previous numbered workspace. */
757 NODES_FOREACH_REVERSE(output_get_content(output)) {
758 if (child->type != CT_WORKSPACE || child->num == -1)
760 /* Need to check child against current and previous because we
761 * are traversing multiple lists and thus are not guaranteed
762 * the relative order between the list of workspaces. */
763 if (current->num > child->num && (!prev || child->num > prev->num))
768 /* Find previous named workspace. */
770 bool found_current = false;
771 NODES_FOREACH_REVERSE(output_get_content(output)) {
772 if (child->type != CT_WORKSPACE)
774 if (child == current) {
775 found_current = true;
776 } else if (child->num == -1 && (current->num != -1 || found_current)) {
778 goto workspace_prev_on_output_end;
783 /* Find last workspace. */
785 NODES_FOREACH_REVERSE(output_get_content(output)) {
786 if (child->type != CT_WORKSPACE)
788 if (!prev || child->num > prev->num)
793 workspace_prev_on_output_end:
798 * Focuses the previously focused workspace.
801 void workspace_back_and_forth(void) {
802 if (!previous_workspace_name) {
803 DLOG("No previous workspace name set. Not switching.\n");
807 workspace_show_by_name(previous_workspace_name);
811 * Returns the previously focused workspace con, or NULL if unavailable.
814 Con *workspace_back_and_forth_get(void) {
815 if (!previous_workspace_name) {
816 DLOG("No previous workspace name set.\n");
821 workspace = workspace_get(previous_workspace_name, NULL);
826 static bool get_urgency_flag(Con *con) {
828 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
829 if (child->urgent || get_urgency_flag(child))
832 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
833 if (child->urgent || get_urgency_flag(child))
840 * Goes through all clients on the given workspace and updates the workspace’s
841 * urgent flag accordingly.
844 void workspace_update_urgent_flag(Con *ws) {
845 bool old_flag = ws->urgent;
846 ws->urgent = get_urgency_flag(ws);
847 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
849 if (old_flag != ws->urgent)
850 ipc_send_workspace_event("urgent", ws, NULL);
854 * 'Forces' workspace orientation by moving all cons into a new split-con with
855 * the same layout as the workspace and then changing the workspace layout.
858 void ws_force_orientation(Con *ws, orientation_t orientation) {
859 /* 1: create a new split container */
860 Con *split = con_new(NULL, NULL);
863 /* 2: copy layout from workspace */
864 split->layout = ws->layout;
866 /* 3: move the existing cons of this workspace below the new con */
867 Con **focus_order = get_focus_order(ws);
869 DLOG("Moving cons\n");
870 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
871 Con *child = TAILQ_FIRST(&(ws->nodes_head));
873 con_attach(child, split, true);
876 set_focus_order(split, focus_order);
879 /* 4: switch workspace layout */
880 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
881 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
883 /* 5: attach the new split container to the workspace */
884 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
885 con_attach(split, ws, false);
887 /* 6: fix the percentages */
892 * Called when a new con (with a window, not an empty or split con) should be
893 * attached to the workspace (for example when managing a new window or when
894 * moving an existing window to the workspace level).
896 * Depending on the workspace_layout setting, this function either returns the
897 * workspace itself (default layout) or creates a new stacked/tabbed con and
901 Con *workspace_attach_to(Con *ws) {
902 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
904 if (ws->workspace_layout == L_DEFAULT) {
905 DLOG("Default layout, just attaching it to the workspace itself.\n");
909 DLOG("Non-default layout, creating a new split container\n");
910 /* 1: create a new split container */
911 Con *new = con_new(NULL, NULL);
914 /* 2: set the requested layout on the split con */
915 new->layout = ws->workspace_layout;
917 /* 4: attach the new split container to the workspace */
918 DLOG("Attaching new split %p to workspace %p\n", new, ws);
919 con_attach(new, ws, false);
921 /* 5: fix the percentages */
928 * Creates a new container and re-parents all of children from the given
931 * The container inherits the layout from the workspace.
933 Con *workspace_encapsulate(Con *ws) {
934 if (TAILQ_EMPTY(&(ws->nodes_head))) {
935 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
939 Con *new = con_new(NULL, NULL);
941 new->layout = ws->layout;
943 Con **focus_order = get_focus_order(ws);
945 DLOG("Moving children of workspace %p / %s into container %p\n",
948 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
949 child = TAILQ_FIRST(&(ws->nodes_head));
951 con_attach(child, new, true);
954 set_focus_order(new, focus_order);
957 con_attach(new, ws, true);
963 * Move the given workspace to the specified output.
965 void workspace_move_to_output(Con *ws, Output *output) {
966 DLOG("Moving workspace %p / %s to output %p / \"%s\".\n", ws, ws->name, output, output_primary_name(output));
968 Output *current_output = get_output_for_con(ws);
969 Con *content = output_get_content(output->con);
970 DLOG("got output %p with content %p\n", output, content);
972 if (ws->parent == content) {
973 DLOG("Nothing to do, workspace already there\n");
977 Con *previously_visible_ws = TAILQ_FIRST(&(content->focus_head));
978 if (previously_visible_ws) {
979 DLOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
981 DLOG("No previously visible workspace on output.\n");
984 bool workspace_was_visible = workspace_is_visible(ws);
985 if (con_num_children(ws->parent) == 1) {
986 DLOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
988 /* check if we can find a workspace assigned to this output */
989 bool used_assignment = false;
990 struct Workspace_Assignment *assignment;
991 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
992 if (!output_triggers_assignment(current_output, assignment)) {
995 /* check if this workspace is already attached to the tree */
996 if (get_existing_workspace_by_name(assignment->name) != NULL) {
1000 /* so create the workspace referenced to by this assignment */
1001 DLOG("Creating workspace from assignment %s.\n", assignment->name);
1002 workspace_get(assignment->name, NULL);
1003 used_assignment = true;
1007 /* if we couldn't create the workspace using an assignment, create it on
1008 * the output. Workspace init IPC events are sent either by
1009 * workspace_get or create_workspace_on_output. */
1010 if (!used_assignment) {
1011 create_workspace_on_output(current_output, ws->parent);
1014 DLOG("Detaching\n");
1016 /* detach from the old output and attach to the new output */
1017 Con *old_content = ws->parent;
1019 if (workspace_was_visible) {
1020 /* The workspace which we just detached was visible, so focus the next
1021 * one in the focus-stack. */
1022 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1023 DLOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1024 workspace_show(focus_ws);
1026 con_attach(ws, content, false);
1028 /* fix the coordinates of the floating containers */
1030 TAILQ_FOREACH(floating_con, &(ws->floating_head), floating_windows) {
1031 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1034 ipc_send_workspace_event("move", ws, NULL);
1035 if (workspace_was_visible) {
1036 /* Focus the moved workspace on the destination output. */
1040 if (!previously_visible_ws) {
1044 /* NB: We cannot simply work with previously_visible_ws since it might have
1045 * been cleaned up by workspace_show() already, depending on the focus
1046 * order/number of other workspaces on the output. Instead, we loop through
1047 * the available workspaces and only work with previously_visible_ws if we
1049 TAILQ_FOREACH(ws, &(content->nodes_head), nodes) {
1050 if (ws != previously_visible_ws) {
1054 /* Call the on_remove_child callback of the workspace which previously
1055 * was visible on the destination output. Since it is no longer visible,
1056 * it might need to get cleaned up. */
1057 CALL(previously_visible_ws, on_remove_child);