2 #define I3__FILE__ "workspace.c"
4 * vim:ts=4:sw=4:expandtab
6 * i3 - an improved dynamic tiling window manager
7 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
9 * workspace.c: Modifying workspaces, accessing them, moving containers to
14 #include "yajl_utils.h"
16 #include <yajl/yajl_gen.h>
18 /* Stores a copy of the name of the last used workspace for the workspace
19 * back-and-forth switching. */
20 static char *previous_workspace_name = NULL;
23 * Sets ws->layout to splith/splitv if default_orientation was specified in the
24 * configfile. Otherwise, it uses splith/splitv depending on whether the output
25 * is higher than wide.
28 static void _workspace_apply_default_orientation(Con *ws) {
29 /* If default_orientation is set to NO_ORIENTATION we determine
30 * orientation depending on output resolution. */
31 if (config.default_orientation == NO_ORIENTATION) {
32 Con *output = con_get_output(ws);
33 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
34 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
35 output->rect.width, output->rect.height, ws->layout);
37 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
42 * Returns a pointer to the workspace with the given number (starting at 0),
43 * creating the workspace if necessary (by allocating the necessary amount of
44 * memory and initializing the data structures correctly).
47 Con *workspace_get(const char *num, bool *created) {
48 Con *output, *workspace = NULL;
50 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
51 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
53 if (workspace == NULL) {
54 LOG("Creating new workspace \"%s\"\n", num);
55 /* unless an assignment is found, we will create this workspace on the current output */
56 output = con_get_output(focused);
57 /* look for assignments */
58 struct Workspace_Assignment *assignment;
59 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
60 if (strcmp(assignment->name, num) != 0)
63 LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
64 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
67 Con *content = output_get_content(output);
68 LOG("got output %p with content %p\n", output, content);
69 /* We need to attach this container after setting its type. con_attach
70 * will handle CT_WORKSPACEs differently */
71 workspace = con_new(NULL, NULL);
73 sasprintf(&name, "[i3 con] workspace %s", num);
74 x_set_name(workspace, name);
76 workspace->type = CT_WORKSPACE;
77 FREE(workspace->name);
78 workspace->name = sstrdup(num);
79 workspace->workspace_layout = config.default_layout;
80 /* We set ->num to the number if this workspace’s name begins with a
81 * positive number. Otherwise it’s a named ws and num will be -1. */
83 long parsed_num = strtol(num, &endptr, 10);
84 if (parsed_num == LONG_MIN ||
85 parsed_num == LONG_MAX ||
89 else workspace->num = parsed_num;
90 LOG("num = %d\n", workspace->num);
92 workspace->parent = content;
93 _workspace_apply_default_orientation(workspace);
95 con_attach(workspace, content, false);
97 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
101 else if (created != NULL) {
109 * Returns a pointer to a new workspace in the given output. The workspace
110 * is created attached to the tree hierarchy through the given content
114 Con *create_workspace_on_output(Output *output, Con *content) {
115 /* add a workspace to this output */
119 Con *ws = con_new(NULL, NULL);
120 ws->type = CT_WORKSPACE;
122 /* try the configured workspace bindings first to find a free name */
124 TAILQ_FOREACH(bind, bindings, bindings) {
125 DLOG("binding with command %s\n", bind->command);
126 if (strlen(bind->command) < strlen("workspace ") ||
127 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
129 DLOG("relevant command = %s\n", bind->command);
130 char *target = bind->command + strlen("workspace ");
131 /* We check if this is the workspace
132 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
133 * Beware: The workspace names "next", "prev", "next_on_output",
134 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
135 * so we check before stripping the double quotes */
136 if (strncasecmp(target, "next", strlen("next")) == 0 ||
137 strncasecmp(target, "prev", strlen("prev")) == 0 ||
138 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
139 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
140 strncasecmp(target, "number", strlen("number")) == 0 ||
141 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
142 strncasecmp(target, "current", strlen("current")) == 0)
147 ws->name = strdup(target);
148 if (ws->name[strlen(ws->name)-1] == '"')
149 ws->name[strlen(ws->name)-1] = '\0';
150 DLOG("trying name *%s*\n", ws->name);
152 /* Ensure that this workspace is not assigned to a different output —
153 * otherwise we would create it, then move it over to its output, then
154 * find a new workspace, etc… */
155 bool assigned = false;
156 struct Workspace_Assignment *assignment;
157 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
158 if (strcmp(assignment->name, ws->name) != 0 ||
159 strcmp(assignment->output, output->name) == 0)
170 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
171 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
173 exists = (current != NULL);
175 /* Set ->num to the number of the workspace, if the name actually
176 * is a number or starts with a number */
178 long parsed_num = strtol(ws->name, &endptr, 10);
179 if (parsed_num == LONG_MIN ||
180 parsed_num == LONG_MAX ||
184 else ws->num = parsed_num;
185 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
192 /* get the next unused workspace number */
193 DLOG("Getting next unused workspace by number\n");
199 sasprintf(&(ws->name), "%d", c);
202 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
203 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
204 exists = (current != NULL);
206 DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
210 con_attach(ws, content, false);
212 sasprintf(&name, "[i3 con] workspace %s", ws->name);
213 x_set_name(ws, name);
216 ws->fullscreen_mode = CF_OUTPUT;
218 ws->workspace_layout = config.default_layout;
219 _workspace_apply_default_orientation(ws);
226 * Returns true if the workspace is currently visible. Especially important for
227 * multi-monitor environments, as they can have multiple currenlty active
231 bool workspace_is_visible(Con *ws) {
232 Con *output = con_get_output(ws);
235 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
236 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
241 * XXX: we need to clean up all this recursive walking code.
244 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
247 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
248 if (current != exclude &&
249 current->sticky_group != NULL &&
250 current->window != NULL &&
251 strcmp(current->sticky_group, sticky_group) == 0)
254 Con *recurse = _get_sticky(current, sticky_group, exclude);
259 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
260 if (current != exclude &&
261 current->sticky_group != NULL &&
262 current->window != NULL &&
263 strcmp(current->sticky_group, sticky_group) == 0)
266 Con *recurse = _get_sticky(current, sticky_group, exclude);
275 * Reassigns all child windows in sticky containers. Called when the user
276 * changes workspaces.
278 * XXX: what about sticky containers which contain containers?
281 static void workspace_reassign_sticky(Con *con) {
283 /* 1: go through all containers */
285 /* handle all children and floating windows of this node */
286 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
287 if (current->sticky_group == NULL) {
288 workspace_reassign_sticky(current);
292 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
293 /* 2: find a window which we can re-assign */
294 Con *output = con_get_output(current);
295 Con *src = _get_sticky(output, current->sticky_group, current);
298 LOG("No window found for this sticky group\n");
299 workspace_reassign_sticky(current);
303 x_move_win(src, current);
304 current->window = src->window;
305 current->mapped = true;
309 x_reparent_child(current, src);
311 LOG("re-assigned window from src %p to dest %p\n", src, current);
314 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
315 workspace_reassign_sticky(current);
319 * Callback to reset the urgent flag of the given con to false. May be started by
320 * _workspace_show to avoid urgency hints being lost by switching to a workspace
324 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
327 DLOG("Resetting urgency flag of con %p by timer\n", con);
329 con_update_parents_urgency(con);
330 workspace_update_urgent_flag(con_get_workspace(con));
333 ev_timer_stop(main_loop, con->urgency_timer);
334 FREE(con->urgency_timer);
338 * For the "focus" event we send, along the usual "change" field, also the
339 * current and previous workspace, in "current" and "old" respectively.
341 static void ipc_send_workspace_focus_event(Con *current, Con *old) {
342 setlocale(LC_NUMERIC, "C");
343 yajl_gen gen = ygenalloc();
351 dump_node(gen, current, false);
357 dump_node(gen, old, false);
361 const unsigned char *payload;
363 y(get_buf, &payload, &length);
365 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
367 setlocale(LC_NUMERIC, "");
370 static void _workspace_show(Con *workspace) {
371 Con *current, *old = NULL;
373 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
374 if (con_is_internal(workspace))
377 /* disable fullscreen for the other workspaces and get the workspace we are
379 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
380 if (current->fullscreen_mode == CF_OUTPUT)
382 current->fullscreen_mode = CF_NONE;
385 /* enable fullscreen for the target workspace. If it happens to be the
386 * same one we are currently on anyways, we can stop here. */
387 workspace->fullscreen_mode = CF_OUTPUT;
388 current = con_get_workspace(focused);
389 if (workspace == current) {
390 DLOG("Not switching, already there.\n");
394 /* Remember currently focused workspace for switching back to it later with
395 * the 'workspace back_and_forth' command.
396 * NOTE: We have to duplicate the name as the original will be freed when
397 * the corresponding workspace is cleaned up.
398 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
399 * focused) are skipped, see bug #868. */
400 if (current && !con_is_internal(current)) {
401 FREE(previous_workspace_name);
403 previous_workspace_name = sstrdup(current->name);
404 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
408 workspace_reassign_sticky(workspace);
410 DLOG("switching to %p / %s\n", workspace, workspace->name);
411 Con *next = con_descend_focused(workspace);
413 /* Memorize current output */
414 Con *old_output = con_get_output(focused);
416 /* Display urgency hint for a while if the newly visible workspace would
417 * focus and thereby immediately destroy it */
418 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
422 /* … but immediately reset urgency flags; they will be set to false by
423 * the timer callback in case the container is focused at the time of
425 focused->urgent = true;
426 workspace->urgent = true;
428 if (focused->urgency_timer == NULL) {
429 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
431 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
432 /* use a repeating timer to allow for easy resets */
433 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
434 config.workspace_urgency_timer, config.workspace_urgency_timer);
435 focused->urgency_timer->data = focused;
436 ev_timer_start(main_loop, focused->urgency_timer);
438 DLOG("Resetting urgency timer of con %p on workspace %p\n",
440 ev_timer_again(main_loop, focused->urgency_timer);
445 ipc_send_workspace_focus_event(workspace, old);
447 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
448 /* Close old workspace if necessary. This must be done *after* doing
449 * urgency handling, because tree_close() will do a con_focus() on the next
450 * client, which will clear the urgency flag too early. Also, there is no
451 * way for con_focus() to know about when to clear urgency immediately and
452 * when to defer it. */
453 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
454 /* check if this workspace is currently visible */
455 if (!workspace_is_visible(old)) {
456 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
457 tree_close(old, DONT_KILL_WINDOW, false, false);
458 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
462 workspace->fullscreen_mode = CF_OUTPUT;
463 LOG("focused now = %p / %s\n", focused, focused->name);
465 /* Set mouse pointer */
466 Con *new_output = con_get_output(focused);
467 if (old_output != new_output) {
468 x_set_warp_to(&next->rect);
471 /* Update the EWMH hints */
472 ewmh_update_current_desktop();
476 * Switches to the given workspace
479 void workspace_show(Con *workspace) {
480 _workspace_show(workspace);
484 * Looks up the workspace by name and switches to it.
487 void workspace_show_by_name(const char *num) {
489 workspace = workspace_get(num, NULL);
490 _workspace_show(workspace);
494 * Focuses the next workspace.
497 Con* workspace_next(void) {
498 Con *current = con_get_workspace(focused);
502 if (current->num == -1) {
503 /* If currently a named workspace, find next named workspace. */
504 next = TAILQ_NEXT(current, nodes);
506 /* If currently a numbered workspace, find next numbered workspace. */
507 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
508 /* Skip outputs starting with __, they are internal. */
509 if (con_is_internal(output))
511 NODES_FOREACH(output_get_content(output)) {
512 if (child->type != CT_WORKSPACE)
514 if (child->num == -1)
516 /* Need to check child against current and next because we are
517 * traversing multiple lists and thus are not guaranteed the
518 * relative order between the list of workspaces. */
519 if (current->num < child->num && (!next || child->num < next->num))
525 /* Find next named workspace. */
527 bool found_current = false;
528 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
529 /* Skip outputs starting with __, they are internal. */
530 if (con_is_internal(output))
532 NODES_FOREACH(output_get_content(output)) {
533 if (child->type != CT_WORKSPACE)
535 if (child == current) {
537 } else if (child->num == -1 && (current->num != -1 || found_current)) {
539 goto workspace_next_end;
545 /* Find first workspace. */
547 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
548 /* Skip outputs starting with __, they are internal. */
549 if (con_is_internal(output))
551 NODES_FOREACH(output_get_content(output)) {
552 if (child->type != CT_WORKSPACE)
554 if (!next || (child->num != -1 && child->num < next->num))
564 * Focuses the previous workspace.
567 Con* workspace_prev(void) {
568 Con *current = con_get_workspace(focused);
572 if (current->num == -1) {
573 /* If named workspace, find previous named workspace. */
574 prev = TAILQ_PREV(current, nodes_head, nodes);
575 if (prev && prev->num != -1)
578 /* If numbered workspace, find previous numbered workspace. */
579 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
580 /* Skip outputs starting with __, they are internal. */
581 if (con_is_internal(output))
583 NODES_FOREACH_REVERSE(output_get_content(output)) {
584 if (child->type != CT_WORKSPACE || child->num == -1)
586 /* Need to check child against current and previous because we
587 * are traversing multiple lists and thus are not guaranteed
588 * the relative order between the list of workspaces. */
589 if (current->num > child->num && (!prev || child->num > prev->num))
595 /* Find previous named workspace. */
597 bool found_current = false;
598 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
599 /* Skip outputs starting with __, they are internal. */
600 if (con_is_internal(output))
602 NODES_FOREACH_REVERSE(output_get_content(output)) {
603 if (child->type != CT_WORKSPACE)
605 if (child == current) {
606 found_current = true;
607 } else if (child->num == -1 && (current->num != -1 || found_current)) {
609 goto workspace_prev_end;
615 /* Find last workspace. */
617 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
618 /* Skip outputs starting with __, they are internal. */
619 if (con_is_internal(output))
621 NODES_FOREACH_REVERSE(output_get_content(output)) {
622 if (child->type != CT_WORKSPACE)
624 if (!prev || child->num > prev->num)
636 * Focuses the next workspace on the same output.
639 Con* workspace_next_on_output(void) {
640 Con *current = con_get_workspace(focused);
642 Con *output = con_get_output(focused);
644 if (current->num == -1) {
645 /* If currently a named workspace, find next named workspace. */
646 next = TAILQ_NEXT(current, nodes);
648 /* If currently a numbered workspace, find next numbered workspace. */
649 NODES_FOREACH(output_get_content(output)) {
650 if (child->type != CT_WORKSPACE)
652 if (child->num == -1)
654 /* Need to check child against current and next because we are
655 * traversing multiple lists and thus are not guaranteed the
656 * relative order between the list of workspaces. */
657 if (current->num < child->num && (!next || child->num < next->num))
662 /* Find next named workspace. */
664 bool found_current = false;
665 NODES_FOREACH(output_get_content(output)) {
666 if (child->type != CT_WORKSPACE)
668 if (child == current) {
670 } else if (child->num == -1 && (current->num != -1 || found_current)) {
672 goto workspace_next_on_output_end;
677 /* Find first workspace. */
679 NODES_FOREACH(output_get_content(output)) {
680 if (child->type != CT_WORKSPACE)
682 if (!next || (child->num != -1 && child->num < next->num))
686 workspace_next_on_output_end:
691 * Focuses the previous workspace on same output.
694 Con* workspace_prev_on_output(void) {
695 Con *current = con_get_workspace(focused);
697 Con *output = con_get_output(focused);
698 DLOG("output = %s\n", output->name);
700 if (current->num == -1) {
701 /* If named workspace, find previous named workspace. */
702 prev = TAILQ_PREV(current, nodes_head, nodes);
703 if (prev && prev->num != -1)
706 /* If numbered workspace, find previous numbered workspace. */
707 NODES_FOREACH_REVERSE(output_get_content(output)) {
708 if (child->type != CT_WORKSPACE || child->num == -1)
710 /* Need to check child against current and previous because we
711 * are traversing multiple lists and thus are not guaranteed
712 * the relative order between the list of workspaces. */
713 if (current->num > child->num && (!prev || child->num > prev->num))
718 /* Find previous named workspace. */
720 bool found_current = false;
721 NODES_FOREACH_REVERSE(output_get_content(output)) {
722 if (child->type != CT_WORKSPACE)
724 if (child == current) {
725 found_current = true;
726 } else if (child->num == -1 && (current->num != -1 || found_current)) {
728 goto workspace_prev_on_output_end;
733 /* Find last workspace. */
735 NODES_FOREACH_REVERSE(output_get_content(output)) {
736 if (child->type != CT_WORKSPACE)
738 if (!prev || child->num > prev->num)
743 workspace_prev_on_output_end:
748 * Focuses the previously focused workspace.
751 void workspace_back_and_forth(void) {
752 if (!previous_workspace_name) {
753 DLOG("No previous workspace name set. Not switching.");
757 workspace_show_by_name(previous_workspace_name);
761 * Returns the previously focused workspace con, or NULL if unavailable.
764 Con *workspace_back_and_forth_get(void) {
765 if (!previous_workspace_name) {
766 DLOG("no previous workspace name set.");
771 workspace = workspace_get(previous_workspace_name, NULL);
776 static bool get_urgency_flag(Con *con) {
778 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
779 if (child->urgent || get_urgency_flag(child))
782 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
783 if (child->urgent || get_urgency_flag(child))
790 * Goes through all clients on the given workspace and updates the workspace’s
791 * urgent flag accordingly.
794 void workspace_update_urgent_flag(Con *ws) {
795 bool old_flag = ws->urgent;
796 ws->urgent = get_urgency_flag(ws);
797 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
799 if (old_flag != ws->urgent)
800 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
804 * 'Forces' workspace orientation by moving all cons into a new split-con with
805 * the same layout as the workspace and then changing the workspace layout.
808 void ws_force_orientation(Con *ws, orientation_t orientation) {
809 /* 1: create a new split container */
810 Con *split = con_new(NULL, NULL);
813 /* 2: copy layout from workspace */
814 split->layout = ws->layout;
816 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
818 /* 3: move the existing cons of this workspace below the new con */
819 DLOG("Moving cons\n");
820 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
821 Con *child = TAILQ_FIRST(&(ws->nodes_head));
823 con_attach(child, split, true);
826 /* 4: switch workspace layout */
827 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
828 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
830 /* 5: attach the new split container to the workspace */
831 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
832 con_attach(split, ws, false);
834 /* 6: fix the percentages */
838 con_focus(old_focused);
842 * Called when a new con (with a window, not an empty or split con) should be
843 * attached to the workspace (for example when managing a new window or when
844 * moving an existing window to the workspace level).
846 * Depending on the workspace_layout setting, this function either returns the
847 * workspace itself (default layout) or creates a new stacked/tabbed con and
851 Con *workspace_attach_to(Con *ws) {
852 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
854 if (ws->workspace_layout == L_DEFAULT) {
855 DLOG("Default layout, just attaching it to the workspace itself.\n");
859 DLOG("Non-default layout, creating a new split container\n");
860 /* 1: create a new split container */
861 Con *new = con_new(NULL, NULL);
864 /* 2: set the requested layout on the split con */
865 new->layout = ws->workspace_layout;
867 /* 4: attach the new split container to the workspace */
868 DLOG("Attaching new split %p to workspace %p\n", new, ws);
869 con_attach(new, ws, false);
875 * Creates a new container and re-parents all of children from the given
878 * The container inherits the layout from the workspace.
880 Con *workspace_encapsulate(Con *ws) {
881 if (TAILQ_EMPTY(&(ws->nodes_head))) {
882 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
886 Con *new = con_new(NULL, NULL);
888 new->layout = ws->layout;
890 DLOG("Moving children of workspace %p / %s into container %p\n",
894 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
895 child = TAILQ_FIRST(&(ws->nodes_head));
897 con_attach(child, new, true);
900 con_attach(new, ws, true);