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
15 /* Stores a copy of the name of the last used workspace for the workspace
16 * back-and-forth switching. */
17 static char *previous_workspace_name = NULL;
20 * Sets ws->layout to splith/splitv if default_orientation was specified in the
21 * configfile. Otherwise, it uses splith/splitv depending on whether the output
22 * is higher than wide.
25 static void _workspace_apply_default_orientation(Con *ws) {
26 /* If default_orientation is set to NO_ORIENTATION we determine
27 * orientation depending on output resolution. */
28 if (config.default_orientation == NO_ORIENTATION) {
29 Con *output = con_get_output(ws);
30 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
31 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
32 output->rect.width, output->rect.height, ws->layout);
34 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
39 * Returns a pointer to the workspace with the given number (starting at 0),
40 * creating the workspace if necessary (by allocating the necessary amount of
41 * memory and initializing the data structures correctly).
44 Con *workspace_get(const char *num, bool *created) {
45 Con *output, *workspace = NULL;
47 TAILQ_FOREACH (output, &(croot->nodes_head), nodes)
48 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
50 if (workspace == NULL) {
51 LOG("Creating new workspace \"%s\"\n", num);
52 /* unless an assignment is found, we will create this workspace on the current output */
53 output = con_get_output(focused);
54 /* look for assignments */
55 struct Workspace_Assignment *assignment;
57 /* We set workspace->num to the number if this workspace’s name begins
58 * with a positive number. Otherwise it’s a named ws and num will be
60 long parsed_num = ws_name_to_number(num);
62 TAILQ_FOREACH (assignment, &ws_assignments, ws_assignments) {
63 if (strcmp(assignment->name, num) == 0) {
64 DLOG("Found workspace name assignment to output \"%s\"\n", assignment->output);
65 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
67 } else if (parsed_num != -1 && name_is_digits(assignment->name) && ws_name_to_number(assignment->name) == parsed_num) {
68 DLOG("Found workspace number assignment to output \"%s\"\n", assignment->output);
69 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
73 Con *content = output_get_content(output);
74 LOG("got output %p with content %p\n", output, content);
75 /* We need to attach this container after setting its type. con_attach
76 * will handle CT_WORKSPACEs differently */
77 workspace = con_new(NULL, NULL);
79 sasprintf(&name, "[i3 con] workspace %s", num);
80 x_set_name(workspace, name);
82 workspace->type = CT_WORKSPACE;
83 FREE(workspace->name);
84 workspace->name = sstrdup(num);
85 workspace->workspace_layout = config.default_layout;
86 workspace->num = parsed_num;
87 LOG("num = %d\n", workspace->num);
89 workspace->parent = content;
90 _workspace_apply_default_orientation(workspace);
92 con_attach(workspace, content, false);
94 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
97 } else if (created != NULL) {
105 * Returns a pointer to a new workspace in the given output. The workspace
106 * is created attached to the tree hierarchy through the given content
110 Con *create_workspace_on_output(Output *output, Con *content) {
111 /* add a workspace to this output */
115 Con *ws = con_new(NULL, NULL);
116 ws->type = CT_WORKSPACE;
118 /* try the configured workspace bindings first to find a free name */
120 TAILQ_FOREACH (bind, bindings, bindings) {
121 DLOG("binding with command %s\n", bind->command);
122 if (strlen(bind->command) < strlen("workspace ") ||
123 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
125 DLOG("relevant command = %s\n", bind->command);
126 char *target = bind->command + strlen("workspace ");
127 while ((*target == ' ' || *target == '\t') && target != '\0')
129 /* We check if this is the workspace
130 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
131 * Beware: The workspace names "next", "prev", "next_on_output",
132 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
133 * so we check before stripping the double quotes */
134 if (strncasecmp(target, "next", strlen("next")) == 0 ||
135 strncasecmp(target, "prev", strlen("prev")) == 0 ||
136 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
137 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
138 strncasecmp(target, "number", strlen("number")) == 0 ||
139 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
140 strncasecmp(target, "current", strlen("current")) == 0)
144 if (strncasecmp(target, "__", strlen("__")) == 0) {
145 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
149 ws->name = strdup(target);
150 if (ws->name[strlen(ws->name) - 1] == '"')
151 ws->name[strlen(ws->name) - 1] = '\0';
152 DLOG("trying name *%s*\n", ws->name);
154 /* Ensure that this workspace is not assigned to a different output —
155 * otherwise we would create it, then move it over to its output, then
156 * find a new workspace, etc… */
157 bool assigned = false;
158 struct Workspace_Assignment *assignment;
159 TAILQ_FOREACH (assignment, &ws_assignments, ws_assignments) {
160 if (strcmp(assignment->name, ws->name) != 0 ||
161 strcmp(assignment->output, output->name) == 0)
172 TAILQ_FOREACH (out, &(croot->nodes_head), nodes)
173 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
175 exists = (current != NULL);
177 /* Set ->num to the number of the workspace, if the name actually
178 * is a number or starts with a number */
180 long parsed_num = strtol(ws->name, &endptr, 10);
181 if (parsed_num == LONG_MIN ||
182 parsed_num == LONG_MAX ||
187 ws->num = parsed_num;
188 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
195 /* get the next unused workspace number */
196 DLOG("Getting next unused workspace by number\n");
204 TAILQ_FOREACH (out, &(croot->nodes_head), nodes)
205 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
206 exists = (current != NULL);
208 DLOG("result for ws %d: exists = %d\n", c, exists);
210 sasprintf(&(ws->name), "%d", c);
212 con_attach(ws, content, false);
214 sasprintf(&name, "[i3 con] workspace %s", ws->name);
215 x_set_name(ws, name);
218 ws->fullscreen_mode = CF_OUTPUT;
220 ws->workspace_layout = config.default_layout;
221 _workspace_apply_default_orientation(ws);
227 * Returns true if the workspace is currently visible. Especially important for
228 * multi-monitor environments, as they can have multiple currenlty active
232 bool workspace_is_visible(Con *ws) {
233 Con *output = con_get_output(ws);
236 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
237 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
242 * XXX: we need to clean up all this recursive walking code.
245 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
248 TAILQ_FOREACH (current, &(con->nodes_head), nodes) {
249 if (current != exclude &&
250 current->sticky_group != NULL &&
251 current->window != NULL &&
252 strcmp(current->sticky_group, sticky_group) == 0)
255 Con *recurse = _get_sticky(current, sticky_group, exclude);
260 TAILQ_FOREACH (current, &(con->floating_head), floating_windows) {
261 if (current != exclude &&
262 current->sticky_group != NULL &&
263 current->window != NULL &&
264 strcmp(current->sticky_group, sticky_group) == 0)
267 Con *recurse = _get_sticky(current, sticky_group, exclude);
276 * Reassigns all child windows in sticky containers. Called when the user
277 * changes workspaces.
279 * XXX: what about sticky containers which contain containers?
282 static void workspace_reassign_sticky(Con *con) {
284 /* 1: go through all containers */
286 /* handle all children and floating windows of this node */
287 TAILQ_FOREACH (current, &(con->nodes_head), nodes) {
288 if (current->sticky_group == NULL) {
289 workspace_reassign_sticky(current);
293 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
294 /* 2: find a window which we can re-assign */
295 Con *output = con_get_output(current);
296 Con *src = _get_sticky(output, current->sticky_group, current);
299 LOG("No window found for this sticky group\n");
300 workspace_reassign_sticky(current);
304 x_move_win(src, current);
305 current->window = src->window;
306 current->mapped = true;
310 x_reparent_child(current, src);
312 LOG("re-assigned window from src %p to dest %p\n", src, current);
315 TAILQ_FOREACH (current, &(con->floating_head), floating_windows)
316 workspace_reassign_sticky(current);
320 * Callback to reset the urgent flag of the given con to false. May be started by
321 * _workspace_show to avoid urgency hints being lost by switching to a workspace
325 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
328 DLOG("Resetting urgency flag of con %p by timer\n", con);
330 con_update_parents_urgency(con);
331 workspace_update_urgent_flag(con_get_workspace(con));
334 ev_timer_stop(main_loop, con->urgency_timer);
335 FREE(con->urgency_timer);
338 static void _workspace_show(Con *workspace) {
339 Con *current, *old = NULL;
341 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
342 if (con_is_internal(workspace))
345 /* disable fullscreen for the other workspaces and get the workspace we are
347 TAILQ_FOREACH (current, &(workspace->parent->nodes_head), nodes) {
348 if (current->fullscreen_mode == CF_OUTPUT)
350 current->fullscreen_mode = CF_NONE;
353 /* enable fullscreen for the target workspace. If it happens to be the
354 * same one we are currently on anyways, we can stop here. */
355 workspace->fullscreen_mode = CF_OUTPUT;
356 current = con_get_workspace(focused);
357 if (workspace == current) {
358 DLOG("Not switching, already there.\n");
362 /* Remember currently focused workspace for switching back to it later with
363 * the 'workspace back_and_forth' command.
364 * NOTE: We have to duplicate the name as the original will be freed when
365 * the corresponding workspace is cleaned up.
366 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
367 * focused) are skipped, see bug #868. */
368 if (current && !con_is_internal(current)) {
369 FREE(previous_workspace_name);
371 previous_workspace_name = sstrdup(current->name);
372 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
376 workspace_reassign_sticky(workspace);
378 DLOG("switching to %p / %s\n", workspace, workspace->name);
379 Con *next = con_descend_focused(workspace);
381 /* Memorize current output */
382 Con *old_output = con_get_output(focused);
384 /* Display urgency hint for a while if the newly visible workspace would
385 * focus and thereby immediately destroy it */
386 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
390 /* … but immediately reset urgency flags; they will be set to false by
391 * the timer callback in case the container is focused at the time of
393 focused->urgent = true;
394 workspace->urgent = true;
396 if (focused->urgency_timer == NULL) {
397 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
399 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
400 /* use a repeating timer to allow for easy resets */
401 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
402 config.workspace_urgency_timer, config.workspace_urgency_timer);
403 focused->urgency_timer->data = focused;
404 ev_timer_start(main_loop, focused->urgency_timer);
406 DLOG("Resetting urgency timer of con %p on workspace %p\n",
408 ev_timer_again(main_loop, focused->urgency_timer);
413 ipc_send_workspace_focus_event(workspace, current);
415 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
416 /* Close old workspace if necessary. This must be done *after* doing
417 * urgency handling, because tree_close() will do a con_focus() on the next
418 * client, which will clear the urgency flag too early. Also, there is no
419 * way for con_focus() to know about when to clear urgency immediately and
420 * when to defer it. */
421 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
422 /* check if this workspace is currently visible */
423 if (!workspace_is_visible(old)) {
424 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
425 tree_close(old, DONT_KILL_WINDOW, false, false);
426 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
430 workspace->fullscreen_mode = CF_OUTPUT;
431 LOG("focused now = %p / %s\n", focused, focused->name);
433 /* Set mouse pointer */
434 Con *new_output = con_get_output(focused);
435 if (old_output != new_output) {
436 x_set_warp_to(&next->rect);
439 /* Update the EWMH hints */
440 ewmh_update_current_desktop();
444 * Switches to the given workspace
447 void workspace_show(Con *workspace) {
448 _workspace_show(workspace);
452 * Looks up the workspace by name and switches to it.
455 void workspace_show_by_name(const char *num) {
457 workspace = workspace_get(num, NULL);
458 _workspace_show(workspace);
462 * Focuses the next workspace.
465 Con *workspace_next(void) {
466 Con *current = con_get_workspace(focused);
470 if (current->num == -1) {
471 /* If currently a named workspace, find next named workspace. */
472 next = TAILQ_NEXT(current, nodes);
474 /* If currently a numbered workspace, find next numbered workspace. */
475 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
476 /* Skip outputs starting with __, they are internal. */
477 if (con_is_internal(output))
479 NODES_FOREACH (output_get_content(output)) {
480 if (child->type != CT_WORKSPACE)
482 if (child->num == -1)
484 /* Need to check child against current and next because we are
485 * traversing multiple lists and thus are not guaranteed the
486 * relative order between the list of workspaces. */
487 if (current->num < child->num && (!next || child->num < next->num))
493 /* Find next named workspace. */
495 bool found_current = false;
496 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
497 /* Skip outputs starting with __, they are internal. */
498 if (con_is_internal(output))
500 NODES_FOREACH (output_get_content(output)) {
501 if (child->type != CT_WORKSPACE)
503 if (child == current) {
505 } else if (child->num == -1 && (current->num != -1 || found_current)) {
507 goto workspace_next_end;
513 /* Find first workspace. */
515 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
516 /* Skip outputs starting with __, they are internal. */
517 if (con_is_internal(output))
519 NODES_FOREACH (output_get_content(output)) {
520 if (child->type != CT_WORKSPACE)
522 if (!next || (child->num != -1 && child->num < next->num))
532 * Focuses the previous workspace.
535 Con *workspace_prev(void) {
536 Con *current = con_get_workspace(focused);
540 if (current->num == -1) {
541 /* If named workspace, find previous named workspace. */
542 prev = TAILQ_PREV(current, nodes_head, nodes);
543 if (prev && prev->num != -1)
546 /* If numbered workspace, find previous numbered workspace. */
547 TAILQ_FOREACH_REVERSE (output, &(croot->nodes_head), nodes_head, nodes) {
548 /* Skip outputs starting with __, they are internal. */
549 if (con_is_internal(output))
551 NODES_FOREACH_REVERSE (output_get_content(output)) {
552 if (child->type != CT_WORKSPACE || child->num == -1)
554 /* Need to check child against current and previous because we
555 * are traversing multiple lists and thus are not guaranteed
556 * the relative order between the list of workspaces. */
557 if (current->num > child->num && (!prev || child->num > prev->num))
563 /* Find previous named workspace. */
565 bool found_current = false;
566 TAILQ_FOREACH_REVERSE (output, &(croot->nodes_head), nodes_head, nodes) {
567 /* Skip outputs starting with __, they are internal. */
568 if (con_is_internal(output))
570 NODES_FOREACH_REVERSE (output_get_content(output)) {
571 if (child->type != CT_WORKSPACE)
573 if (child == current) {
574 found_current = true;
575 } else if (child->num == -1 && (current->num != -1 || found_current)) {
577 goto workspace_prev_end;
583 /* Find last workspace. */
585 TAILQ_FOREACH_REVERSE (output, &(croot->nodes_head), nodes_head, nodes) {
586 /* Skip outputs starting with __, they are internal. */
587 if (con_is_internal(output))
589 NODES_FOREACH_REVERSE (output_get_content(output)) {
590 if (child->type != CT_WORKSPACE)
592 if (!prev || child->num > prev->num)
603 * Focuses the next workspace on the same output.
606 Con *workspace_next_on_output(void) {
607 Con *current = con_get_workspace(focused);
609 Con *output = con_get_output(focused);
611 if (current->num == -1) {
612 /* If currently a named workspace, find next named workspace. */
613 next = TAILQ_NEXT(current, nodes);
615 /* If currently a numbered workspace, find next numbered workspace. */
616 NODES_FOREACH (output_get_content(output)) {
617 if (child->type != CT_WORKSPACE)
619 if (child->num == -1)
621 /* Need to check child against current and next because we are
622 * traversing multiple lists and thus are not guaranteed the
623 * relative order between the list of workspaces. */
624 if (current->num < child->num && (!next || child->num < next->num))
629 /* Find next named workspace. */
631 bool found_current = false;
632 NODES_FOREACH (output_get_content(output)) {
633 if (child->type != CT_WORKSPACE)
635 if (child == current) {
637 } else if (child->num == -1 && (current->num != -1 || found_current)) {
639 goto workspace_next_on_output_end;
644 /* Find first workspace. */
646 NODES_FOREACH (output_get_content(output)) {
647 if (child->type != CT_WORKSPACE)
649 if (!next || (child->num != -1 && child->num < next->num))
653 workspace_next_on_output_end:
658 * Focuses the previous workspace on same output.
661 Con *workspace_prev_on_output(void) {
662 Con *current = con_get_workspace(focused);
664 Con *output = con_get_output(focused);
665 DLOG("output = %s\n", output->name);
667 if (current->num == -1) {
668 /* If named workspace, find previous named workspace. */
669 prev = TAILQ_PREV(current, nodes_head, nodes);
670 if (prev && prev->num != -1)
673 /* If numbered workspace, find previous numbered workspace. */
674 NODES_FOREACH_REVERSE (output_get_content(output)) {
675 if (child->type != CT_WORKSPACE || child->num == -1)
677 /* Need to check child against current and previous because we
678 * are traversing multiple lists and thus are not guaranteed
679 * the relative order between the list of workspaces. */
680 if (current->num > child->num && (!prev || child->num > prev->num))
685 /* Find previous named workspace. */
687 bool found_current = false;
688 NODES_FOREACH_REVERSE (output_get_content(output)) {
689 if (child->type != CT_WORKSPACE)
691 if (child == current) {
692 found_current = true;
693 } else if (child->num == -1 && (current->num != -1 || found_current)) {
695 goto workspace_prev_on_output_end;
700 /* Find last workspace. */
702 NODES_FOREACH_REVERSE (output_get_content(output)) {
703 if (child->type != CT_WORKSPACE)
705 if (!prev || child->num > prev->num)
710 workspace_prev_on_output_end:
715 * Focuses the previously focused workspace.
718 void workspace_back_and_forth(void) {
719 if (!previous_workspace_name) {
720 DLOG("No previous workspace name set. Not switching.");
724 workspace_show_by_name(previous_workspace_name);
728 * Returns the previously focused workspace con, or NULL if unavailable.
731 Con *workspace_back_and_forth_get(void) {
732 if (!previous_workspace_name) {
733 DLOG("no previous workspace name set.");
738 workspace = workspace_get(previous_workspace_name, NULL);
743 static bool get_urgency_flag(Con *con) {
745 TAILQ_FOREACH (child, &(con->nodes_head), nodes)
746 if (child->urgent || get_urgency_flag(child))
749 TAILQ_FOREACH (child, &(con->floating_head), floating_windows)
750 if (child->urgent || get_urgency_flag(child))
757 * Goes through all clients on the given workspace and updates the workspace’s
758 * urgent flag accordingly.
761 void workspace_update_urgent_flag(Con *ws) {
762 bool old_flag = ws->urgent;
763 ws->urgent = get_urgency_flag(ws);
764 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
766 if (old_flag != ws->urgent)
767 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
771 * 'Forces' workspace orientation by moving all cons into a new split-con with
772 * the same layout as the workspace and then changing the workspace layout.
775 void ws_force_orientation(Con *ws, orientation_t orientation) {
776 /* 1: create a new split container */
777 Con *split = con_new(NULL, NULL);
780 /* 2: copy layout from workspace */
781 split->layout = ws->layout;
783 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
785 /* 3: move the existing cons of this workspace below the new con */
786 DLOG("Moving cons\n");
787 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
788 Con *child = TAILQ_FIRST(&(ws->nodes_head));
790 con_attach(child, split, true);
793 /* 4: switch workspace layout */
794 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
795 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
797 /* 5: attach the new split container to the workspace */
798 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
799 con_attach(split, ws, false);
801 /* 6: fix the percentages */
805 con_focus(old_focused);
809 * Called when a new con (with a window, not an empty or split con) should be
810 * attached to the workspace (for example when managing a new window or when
811 * moving an existing window to the workspace level).
813 * Depending on the workspace_layout setting, this function either returns the
814 * workspace itself (default layout) or creates a new stacked/tabbed con and
818 Con *workspace_attach_to(Con *ws) {
819 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
821 if (ws->workspace_layout == L_DEFAULT) {
822 DLOG("Default layout, just attaching it to the workspace itself.\n");
826 DLOG("Non-default layout, creating a new split container\n");
827 /* 1: create a new split container */
828 Con *new = con_new(NULL, NULL);
831 /* 2: set the requested layout on the split con */
832 new->layout = ws->workspace_layout;
834 /* 4: attach the new split container to the workspace */
835 DLOG("Attaching new split %p to workspace %p\n", new, ws);
836 con_attach(new, ws, false);
842 * Creates a new container and re-parents all of children from the given
845 * The container inherits the layout from the workspace.
847 Con *workspace_encapsulate(Con *ws) {
848 if (TAILQ_EMPTY(&(ws->nodes_head))) {
849 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
853 Con *new = con_new(NULL, NULL);
855 new->layout = ws->layout;
857 DLOG("Moving children of workspace %p / %s into container %p\n",
861 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
862 child = TAILQ_FIRST(&(ws->nodes_head));
864 con_attach(child, new, true);
867 con_attach(new, ws, true);