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 while((*target == ' ' || *target == '\t') && target != '\0')
133 /* We check if this is the workspace
134 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
135 * Beware: The workspace names "next", "prev", "next_on_output",
136 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
137 * so we check before stripping the double quotes */
138 if (strncasecmp(target, "next", strlen("next")) == 0 ||
139 strncasecmp(target, "prev", strlen("prev")) == 0 ||
140 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
141 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
142 strncasecmp(target, "number", strlen("number")) == 0 ||
143 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
144 strncasecmp(target, "current", strlen("current")) == 0)
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 ||
186 else ws->num = parsed_num;
187 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
194 /* get the next unused workspace number */
195 DLOG("Getting next unused workspace by number\n");
203 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
204 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
205 exists = (current != NULL);
207 DLOG("result for ws %d: exists = %d\n", c, exists);
209 sasprintf(&(ws->name), "%d", c);
211 con_attach(ws, content, false);
213 sasprintf(&name, "[i3 con] workspace %s", ws->name);
214 x_set_name(ws, name);
217 ws->fullscreen_mode = CF_OUTPUT;
219 ws->workspace_layout = config.default_layout;
220 _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);
339 * For the "focus" event we send, along the usual "change" field, also the
340 * current and previous workspace, in "current" and "old" respectively.
342 static void ipc_send_workspace_focus_event(Con *current, Con *old) {
343 setlocale(LC_NUMERIC, "C");
344 yajl_gen gen = ygenalloc();
352 dump_node(gen, current, false);
358 dump_node(gen, old, false);
362 const unsigned char *payload;
364 y(get_buf, &payload, &length);
366 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
368 setlocale(LC_NUMERIC, "");
371 static void _workspace_show(Con *workspace) {
372 Con *current, *old = NULL;
374 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
375 if (con_is_internal(workspace))
378 /* disable fullscreen for the other workspaces and get the workspace we are
380 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
381 if (current->fullscreen_mode == CF_OUTPUT)
383 current->fullscreen_mode = CF_NONE;
386 /* enable fullscreen for the target workspace. If it happens to be the
387 * same one we are currently on anyways, we can stop here. */
388 workspace->fullscreen_mode = CF_OUTPUT;
389 current = con_get_workspace(focused);
390 if (workspace == current) {
391 DLOG("Not switching, already there.\n");
395 /* Remember currently focused workspace for switching back to it later with
396 * the 'workspace back_and_forth' command.
397 * NOTE: We have to duplicate the name as the original will be freed when
398 * the corresponding workspace is cleaned up.
399 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
400 * focused) are skipped, see bug #868. */
401 if (current && !con_is_internal(current)) {
402 FREE(previous_workspace_name);
404 previous_workspace_name = sstrdup(current->name);
405 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
409 workspace_reassign_sticky(workspace);
411 DLOG("switching to %p / %s\n", workspace, workspace->name);
412 Con *next = con_descend_focused(workspace);
414 /* Memorize current output */
415 Con *old_output = con_get_output(focused);
417 /* Display urgency hint for a while if the newly visible workspace would
418 * focus and thereby immediately destroy it */
419 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
423 /* … but immediately reset urgency flags; they will be set to false by
424 * the timer callback in case the container is focused at the time of
426 focused->urgent = true;
427 workspace->urgent = true;
429 if (focused->urgency_timer == NULL) {
430 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
432 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
433 /* use a repeating timer to allow for easy resets */
434 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
435 config.workspace_urgency_timer, config.workspace_urgency_timer);
436 focused->urgency_timer->data = focused;
437 ev_timer_start(main_loop, focused->urgency_timer);
439 DLOG("Resetting urgency timer of con %p on workspace %p\n",
441 ev_timer_again(main_loop, focused->urgency_timer);
446 ipc_send_workspace_focus_event(workspace, current);
448 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
449 /* Close old workspace if necessary. This must be done *after* doing
450 * urgency handling, because tree_close() will do a con_focus() on the next
451 * client, which will clear the urgency flag too early. Also, there is no
452 * way for con_focus() to know about when to clear urgency immediately and
453 * when to defer it. */
454 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
455 /* check if this workspace is currently visible */
456 if (!workspace_is_visible(old)) {
457 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
458 tree_close(old, DONT_KILL_WINDOW, false, false);
459 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
463 workspace->fullscreen_mode = CF_OUTPUT;
464 LOG("focused now = %p / %s\n", focused, focused->name);
466 /* Set mouse pointer */
467 Con *new_output = con_get_output(focused);
468 if (old_output != new_output) {
469 x_set_warp_to(&next->rect);
472 /* Update the EWMH hints */
473 ewmh_update_current_desktop();
477 * Switches to the given workspace
480 void workspace_show(Con *workspace) {
481 _workspace_show(workspace);
485 * Looks up the workspace by name and switches to it.
488 void workspace_show_by_name(const char *num) {
490 workspace = workspace_get(num, NULL);
491 _workspace_show(workspace);
495 * Focuses the next workspace.
498 Con* workspace_next(void) {
499 Con *current = con_get_workspace(focused);
503 if (current->num == -1) {
504 /* If currently a named workspace, find next named workspace. */
505 next = TAILQ_NEXT(current, nodes);
507 /* If currently a numbered workspace, find next numbered workspace. */
508 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
509 /* Skip outputs starting with __, they are internal. */
510 if (con_is_internal(output))
512 NODES_FOREACH(output_get_content(output)) {
513 if (child->type != CT_WORKSPACE)
515 if (child->num == -1)
517 /* Need to check child against current and next because we are
518 * traversing multiple lists and thus are not guaranteed the
519 * relative order between the list of workspaces. */
520 if (current->num < child->num && (!next || child->num < next->num))
526 /* Find next named workspace. */
528 bool found_current = false;
529 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
530 /* Skip outputs starting with __, they are internal. */
531 if (con_is_internal(output))
533 NODES_FOREACH(output_get_content(output)) {
534 if (child->type != CT_WORKSPACE)
536 if (child == current) {
538 } else if (child->num == -1 && (current->num != -1 || found_current)) {
540 goto workspace_next_end;
546 /* Find first workspace. */
548 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
549 /* Skip outputs starting with __, they are internal. */
550 if (con_is_internal(output))
552 NODES_FOREACH(output_get_content(output)) {
553 if (child->type != CT_WORKSPACE)
555 if (!next || (child->num != -1 && child->num < next->num))
565 * Focuses the previous workspace.
568 Con* workspace_prev(void) {
569 Con *current = con_get_workspace(focused);
573 if (current->num == -1) {
574 /* If named workspace, find previous named workspace. */
575 prev = TAILQ_PREV(current, nodes_head, nodes);
576 if (prev && prev->num != -1)
579 /* If numbered workspace, find previous numbered workspace. */
580 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
581 /* Skip outputs starting with __, they are internal. */
582 if (con_is_internal(output))
584 NODES_FOREACH_REVERSE(output_get_content(output)) {
585 if (child->type != CT_WORKSPACE || child->num == -1)
587 /* Need to check child against current and previous because we
588 * are traversing multiple lists and thus are not guaranteed
589 * the relative order between the list of workspaces. */
590 if (current->num > child->num && (!prev || child->num > prev->num))
596 /* Find previous named workspace. */
598 bool found_current = false;
599 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
600 /* Skip outputs starting with __, they are internal. */
601 if (con_is_internal(output))
603 NODES_FOREACH_REVERSE(output_get_content(output)) {
604 if (child->type != CT_WORKSPACE)
606 if (child == current) {
607 found_current = true;
608 } else if (child->num == -1 && (current->num != -1 || found_current)) {
610 goto workspace_prev_end;
616 /* Find last workspace. */
618 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
619 /* Skip outputs starting with __, they are internal. */
620 if (con_is_internal(output))
622 NODES_FOREACH_REVERSE(output_get_content(output)) {
623 if (child->type != CT_WORKSPACE)
625 if (!prev || child->num > prev->num)
637 * Focuses the next workspace on the same output.
640 Con* workspace_next_on_output(void) {
641 Con *current = con_get_workspace(focused);
643 Con *output = con_get_output(focused);
645 if (current->num == -1) {
646 /* If currently a named workspace, find next named workspace. */
647 next = TAILQ_NEXT(current, nodes);
649 /* If currently a numbered workspace, find next numbered workspace. */
650 NODES_FOREACH(output_get_content(output)) {
651 if (child->type != CT_WORKSPACE)
653 if (child->num == -1)
655 /* Need to check child against current and next because we are
656 * traversing multiple lists and thus are not guaranteed the
657 * relative order between the list of workspaces. */
658 if (current->num < child->num && (!next || child->num < next->num))
663 /* Find next named workspace. */
665 bool found_current = false;
666 NODES_FOREACH(output_get_content(output)) {
667 if (child->type != CT_WORKSPACE)
669 if (child == current) {
671 } else if (child->num == -1 && (current->num != -1 || found_current)) {
673 goto workspace_next_on_output_end;
678 /* Find first workspace. */
680 NODES_FOREACH(output_get_content(output)) {
681 if (child->type != CT_WORKSPACE)
683 if (!next || (child->num != -1 && child->num < next->num))
687 workspace_next_on_output_end:
692 * Focuses the previous workspace on same output.
695 Con* workspace_prev_on_output(void) {
696 Con *current = con_get_workspace(focused);
698 Con *output = con_get_output(focused);
699 DLOG("output = %s\n", output->name);
701 if (current->num == -1) {
702 /* If named workspace, find previous named workspace. */
703 prev = TAILQ_PREV(current, nodes_head, nodes);
704 if (prev && prev->num != -1)
707 /* If numbered workspace, find previous numbered workspace. */
708 NODES_FOREACH_REVERSE(output_get_content(output)) {
709 if (child->type != CT_WORKSPACE || child->num == -1)
711 /* Need to check child against current and previous because we
712 * are traversing multiple lists and thus are not guaranteed
713 * the relative order between the list of workspaces. */
714 if (current->num > child->num && (!prev || child->num > prev->num))
719 /* Find previous named workspace. */
721 bool found_current = false;
722 NODES_FOREACH_REVERSE(output_get_content(output)) {
723 if (child->type != CT_WORKSPACE)
725 if (child == current) {
726 found_current = true;
727 } else if (child->num == -1 && (current->num != -1 || found_current)) {
729 goto workspace_prev_on_output_end;
734 /* Find last workspace. */
736 NODES_FOREACH_REVERSE(output_get_content(output)) {
737 if (child->type != CT_WORKSPACE)
739 if (!prev || child->num > prev->num)
744 workspace_prev_on_output_end:
749 * Focuses the previously focused workspace.
752 void workspace_back_and_forth(void) {
753 if (!previous_workspace_name) {
754 DLOG("No previous workspace name set. Not switching.");
758 workspace_show_by_name(previous_workspace_name);
762 * Returns the previously focused workspace con, or NULL if unavailable.
765 Con *workspace_back_and_forth_get(void) {
766 if (!previous_workspace_name) {
767 DLOG("no previous workspace name set.");
772 workspace = workspace_get(previous_workspace_name, NULL);
777 static bool get_urgency_flag(Con *con) {
779 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
780 if (child->urgent || get_urgency_flag(child))
783 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
784 if (child->urgent || get_urgency_flag(child))
791 * Goes through all clients on the given workspace and updates the workspace’s
792 * urgent flag accordingly.
795 void workspace_update_urgent_flag(Con *ws) {
796 bool old_flag = ws->urgent;
797 ws->urgent = get_urgency_flag(ws);
798 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
800 if (old_flag != ws->urgent)
801 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
805 * 'Forces' workspace orientation by moving all cons into a new split-con with
806 * the same layout as the workspace and then changing the workspace layout.
809 void ws_force_orientation(Con *ws, orientation_t orientation) {
810 /* 1: create a new split container */
811 Con *split = con_new(NULL, NULL);
814 /* 2: copy layout from workspace */
815 split->layout = ws->layout;
817 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
819 /* 3: move the existing cons of this workspace below the new con */
820 DLOG("Moving cons\n");
821 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
822 Con *child = TAILQ_FIRST(&(ws->nodes_head));
824 con_attach(child, split, true);
827 /* 4: switch workspace layout */
828 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
829 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
831 /* 5: attach the new split container to the workspace */
832 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
833 con_attach(split, ws, false);
835 /* 6: fix the percentages */
839 con_focus(old_focused);
843 * Called when a new con (with a window, not an empty or split con) should be
844 * attached to the workspace (for example when managing a new window or when
845 * moving an existing window to the workspace level).
847 * Depending on the workspace_layout setting, this function either returns the
848 * workspace itself (default layout) or creates a new stacked/tabbed con and
852 Con *workspace_attach_to(Con *ws) {
853 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
855 if (ws->workspace_layout == L_DEFAULT) {
856 DLOG("Default layout, just attaching it to the workspace itself.\n");
860 DLOG("Non-default layout, creating a new split container\n");
861 /* 1: create a new split container */
862 Con *new = con_new(NULL, NULL);
865 /* 2: set the requested layout on the split con */
866 new->layout = ws->workspace_layout;
868 /* 4: attach the new split container to the workspace */
869 DLOG("Attaching new split %p to workspace %p\n", new, ws);
870 con_attach(new, ws, false);
876 * Creates a new container and re-parents all of children from the given
879 * The container inherits the layout from the workspace.
881 Con *workspace_encapsulate(Con *ws) {
882 if (TAILQ_EMPTY(&(ws->nodes_head))) {
883 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
887 Con *new = con_new(NULL, NULL);
889 new->layout = ws->layout;
891 DLOG("Moving children of workspace %p / %s into container %p\n",
895 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
896 child = TAILQ_FIRST(&(ws->nodes_head));
898 con_attach(child, new, true);
901 con_attach(new, ws, true);