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\"}");
95 ewmh_update_number_of_desktops();
96 ewmh_update_desktop_viewport();
99 } else if (created != NULL) {
107 * Returns a pointer to a new workspace in the given output. The workspace
108 * is created attached to the tree hierarchy through the given content
112 Con *create_workspace_on_output(Output *output, Con *content) {
113 /* add a workspace to this output */
117 Con *ws = con_new(NULL, NULL);
118 ws->type = CT_WORKSPACE;
120 /* try the configured workspace bindings first to find a free name */
122 TAILQ_FOREACH(bind, bindings, bindings) {
123 DLOG("binding with command %s\n", bind->command);
124 if (strlen(bind->command) < strlen("workspace ") ||
125 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
127 DLOG("relevant command = %s\n", bind->command);
128 char *target = bind->command + strlen("workspace ");
129 while ((*target == ' ' || *target == '\t') && target != '\0')
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)
146 if (strncasecmp(target, "__", strlen("__")) == 0) {
147 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
151 ws->name = strdup(target);
152 if (ws->name[strlen(ws->name) - 1] == '"')
153 ws->name[strlen(ws->name) - 1] = '\0';
154 DLOG("trying name *%s*\n", ws->name);
156 /* Ensure that this workspace is not assigned to a different output —
157 * otherwise we would create it, then move it over to its output, then
158 * find a new workspace, etc… */
159 bool assigned = false;
160 struct Workspace_Assignment *assignment;
161 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
162 if (strcmp(assignment->name, ws->name) != 0 ||
163 strcmp(assignment->output, output->name) == 0)
174 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
175 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
177 exists = (current != NULL);
179 /* Set ->num to the number of the workspace, if the name actually
180 * is a number or starts with a number */
181 ws->num = ws_name_to_number(ws->name);
182 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
189 /* get the next unused workspace number */
190 DLOG("Getting next unused workspace by number\n");
198 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
199 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
200 exists = (current != NULL);
202 DLOG("result for ws %d: exists = %d\n", c, exists);
204 sasprintf(&(ws->name), "%d", c);
206 con_attach(ws, content, false);
208 sasprintf(&name, "[i3 con] workspace %s", ws->name);
209 x_set_name(ws, name);
212 ws->fullscreen_mode = CF_OUTPUT;
214 ws->workspace_layout = config.default_layout;
215 _workspace_apply_default_orientation(ws);
221 * Returns true if the workspace is currently visible. Especially important for
222 * multi-monitor environments, as they can have multiple currenlty active
226 bool workspace_is_visible(Con *ws) {
227 Con *output = con_get_output(ws);
230 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
231 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
236 * XXX: we need to clean up all this recursive walking code.
239 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
242 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
243 if (current != exclude &&
244 current->sticky_group != NULL &&
245 current->window != NULL &&
246 strcmp(current->sticky_group, sticky_group) == 0)
249 Con *recurse = _get_sticky(current, sticky_group, exclude);
254 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
255 if (current != exclude &&
256 current->sticky_group != NULL &&
257 current->window != NULL &&
258 strcmp(current->sticky_group, sticky_group) == 0)
261 Con *recurse = _get_sticky(current, sticky_group, exclude);
270 * Reassigns all child windows in sticky containers. Called when the user
271 * changes workspaces.
273 * XXX: what about sticky containers which contain containers?
276 static void workspace_reassign_sticky(Con *con) {
278 /* 1: go through all containers */
280 /* handle all children and floating windows of this node */
281 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
282 if (current->sticky_group == NULL) {
283 workspace_reassign_sticky(current);
287 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
288 /* 2: find a window which we can re-assign */
289 Con *output = con_get_output(current);
290 Con *src = _get_sticky(output, current->sticky_group, current);
293 LOG("No window found for this sticky group\n");
294 workspace_reassign_sticky(current);
298 x_move_win(src, current);
299 current->window = src->window;
300 current->mapped = true;
304 x_reparent_child(current, src);
306 LOG("re-assigned window from src %p to dest %p\n", src, current);
309 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
310 workspace_reassign_sticky(current);
314 * Callback to reset the urgent flag of the given con to false. May be started by
315 * _workspace_show to avoid urgency hints being lost by switching to a workspace
319 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
322 DLOG("Resetting urgency flag of con %p by timer\n", con);
324 con_update_parents_urgency(con);
325 workspace_update_urgent_flag(con_get_workspace(con));
328 ev_timer_stop(main_loop, con->urgency_timer);
329 FREE(con->urgency_timer);
332 static void _workspace_show(Con *workspace) {
333 Con *current, *old = NULL;
335 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
336 if (con_is_internal(workspace))
339 /* disable fullscreen for the other workspaces and get the workspace we are
341 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
342 if (current->fullscreen_mode == CF_OUTPUT)
344 current->fullscreen_mode = CF_NONE;
347 /* enable fullscreen for the target workspace. If it happens to be the
348 * same one we are currently on anyways, we can stop here. */
349 workspace->fullscreen_mode = CF_OUTPUT;
350 current = con_get_workspace(focused);
351 if (workspace == current) {
352 DLOG("Not switching, already there.\n");
356 /* Remember currently focused workspace for switching back to it later with
357 * the 'workspace back_and_forth' command.
358 * NOTE: We have to duplicate the name as the original will be freed when
359 * the corresponding workspace is cleaned up.
360 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
361 * focused) are skipped, see bug #868. */
362 if (current && !con_is_internal(current)) {
363 FREE(previous_workspace_name);
365 previous_workspace_name = sstrdup(current->name);
366 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
370 workspace_reassign_sticky(workspace);
372 DLOG("switching to %p / %s\n", workspace, workspace->name);
373 Con *next = con_descend_focused(workspace);
375 /* Memorize current output */
376 Con *old_output = con_get_output(focused);
378 /* Display urgency hint for a while if the newly visible workspace would
379 * focus and thereby immediately destroy it */
380 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
384 /* … but immediately reset urgency flags; they will be set to false by
385 * the timer callback in case the container is focused at the time of
387 focused->urgent = true;
388 workspace->urgent = true;
390 if (focused->urgency_timer == NULL) {
391 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
393 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
394 /* use a repeating timer to allow for easy resets */
395 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
396 config.workspace_urgency_timer, config.workspace_urgency_timer);
397 focused->urgency_timer->data = focused;
398 ev_timer_start(main_loop, focused->urgency_timer);
400 DLOG("Resetting urgency timer of con %p on workspace %p\n",
402 ev_timer_again(main_loop, focused->urgency_timer);
407 ipc_send_workspace_focus_event(workspace, current);
409 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
410 /* Close old workspace if necessary. This must be done *after* doing
411 * urgency handling, because tree_close() will do a con_focus() on the next
412 * client, which will clear the urgency flag too early. Also, there is no
413 * way for con_focus() to know about when to clear urgency immediately and
414 * when to defer it. */
415 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
416 /* check if this workspace is currently visible */
417 if (!workspace_is_visible(old)) {
418 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
419 tree_close(old, DONT_KILL_WINDOW, false, false);
420 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
421 ewmh_update_number_of_desktops();
422 ewmh_update_desktop_viewport();
426 workspace->fullscreen_mode = CF_OUTPUT;
427 LOG("focused now = %p / %s\n", focused, focused->name);
429 /* Set mouse pointer */
430 Con *new_output = con_get_output(focused);
431 if (old_output != new_output) {
432 x_set_warp_to(&next->rect);
435 /* Update the EWMH hints */
436 ewmh_update_current_desktop();
440 * Switches to the given workspace
443 void workspace_show(Con *workspace) {
444 _workspace_show(workspace);
448 * Looks up the workspace by name and switches to it.
451 void workspace_show_by_name(const char *num) {
453 workspace = workspace_get(num, NULL);
454 _workspace_show(workspace);
458 * Focuses the next workspace.
461 Con *workspace_next(void) {
462 Con *current = con_get_workspace(focused);
466 if (current->num == -1) {
467 /* If currently a named workspace, find next named workspace. */
468 next = TAILQ_NEXT(current, nodes);
470 /* If currently a numbered workspace, find next numbered workspace. */
471 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
472 /* Skip outputs starting with __, they are internal. */
473 if (con_is_internal(output))
475 NODES_FOREACH(output_get_content(output)) {
476 if (child->type != CT_WORKSPACE)
478 if (child->num == -1)
480 /* Need to check child against current and next because we are
481 * traversing multiple lists and thus are not guaranteed the
482 * relative order between the list of workspaces. */
483 if (current->num < child->num && (!next || child->num < next->num))
489 /* Find next named workspace. */
491 bool found_current = false;
492 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
493 /* Skip outputs starting with __, they are internal. */
494 if (con_is_internal(output))
496 NODES_FOREACH(output_get_content(output)) {
497 if (child->type != CT_WORKSPACE)
499 if (child == current) {
501 } else if (child->num == -1 && (current->num != -1 || found_current)) {
503 goto workspace_next_end;
509 /* Find first workspace. */
511 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
512 /* Skip outputs starting with __, they are internal. */
513 if (con_is_internal(output))
515 NODES_FOREACH(output_get_content(output)) {
516 if (child->type != CT_WORKSPACE)
518 if (!next || (child->num != -1 && child->num < next->num))
528 * Focuses the previous workspace.
531 Con *workspace_prev(void) {
532 Con *current = con_get_workspace(focused);
536 if (current->num == -1) {
537 /* If named workspace, find previous named workspace. */
538 prev = TAILQ_PREV(current, nodes_head, nodes);
539 if (prev && prev->num != -1)
542 /* If numbered workspace, find previous numbered workspace. */
543 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
544 /* Skip outputs starting with __, they are internal. */
545 if (con_is_internal(output))
547 NODES_FOREACH_REVERSE(output_get_content(output)) {
548 if (child->type != CT_WORKSPACE || child->num == -1)
550 /* Need to check child against current and previous because we
551 * are traversing multiple lists and thus are not guaranteed
552 * the relative order between the list of workspaces. */
553 if (current->num > child->num && (!prev || child->num > prev->num))
559 /* Find previous named workspace. */
561 bool found_current = false;
562 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
563 /* Skip outputs starting with __, they are internal. */
564 if (con_is_internal(output))
566 NODES_FOREACH_REVERSE(output_get_content(output)) {
567 if (child->type != CT_WORKSPACE)
569 if (child == current) {
570 found_current = true;
571 } else if (child->num == -1 && (current->num != -1 || found_current)) {
573 goto workspace_prev_end;
579 /* Find last workspace. */
581 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
582 /* Skip outputs starting with __, they are internal. */
583 if (con_is_internal(output))
585 NODES_FOREACH_REVERSE(output_get_content(output)) {
586 if (child->type != CT_WORKSPACE)
588 if (!prev || child->num > prev->num)
599 * Focuses the next workspace on the same output.
602 Con *workspace_next_on_output(void) {
603 Con *current = con_get_workspace(focused);
605 Con *output = con_get_output(focused);
607 if (current->num == -1) {
608 /* If currently a named workspace, find next named workspace. */
609 next = TAILQ_NEXT(current, nodes);
611 /* If currently a numbered workspace, find next numbered workspace. */
612 NODES_FOREACH(output_get_content(output)) {
613 if (child->type != CT_WORKSPACE)
615 if (child->num == -1)
617 /* Need to check child against current and next because we are
618 * traversing multiple lists and thus are not guaranteed the
619 * relative order between the list of workspaces. */
620 if (current->num < child->num && (!next || child->num < next->num))
625 /* Find next named workspace. */
627 bool found_current = false;
628 NODES_FOREACH(output_get_content(output)) {
629 if (child->type != CT_WORKSPACE)
631 if (child == current) {
633 } else if (child->num == -1 && (current->num != -1 || found_current)) {
635 goto workspace_next_on_output_end;
640 /* Find first workspace. */
642 NODES_FOREACH(output_get_content(output)) {
643 if (child->type != CT_WORKSPACE)
645 if (!next || (child->num != -1 && child->num < next->num))
649 workspace_next_on_output_end:
654 * Focuses the previous workspace on same output.
657 Con *workspace_prev_on_output(void) {
658 Con *current = con_get_workspace(focused);
660 Con *output = con_get_output(focused);
661 DLOG("output = %s\n", output->name);
663 if (current->num == -1) {
664 /* If named workspace, find previous named workspace. */
665 prev = TAILQ_PREV(current, nodes_head, nodes);
666 if (prev && prev->num != -1)
669 /* If numbered workspace, find previous numbered workspace. */
670 NODES_FOREACH_REVERSE(output_get_content(output)) {
671 if (child->type != CT_WORKSPACE || child->num == -1)
673 /* Need to check child against current and previous because we
674 * are traversing multiple lists and thus are not guaranteed
675 * the relative order between the list of workspaces. */
676 if (current->num > child->num && (!prev || child->num > prev->num))
681 /* Find previous named workspace. */
683 bool found_current = false;
684 NODES_FOREACH_REVERSE(output_get_content(output)) {
685 if (child->type != CT_WORKSPACE)
687 if (child == current) {
688 found_current = true;
689 } else if (child->num == -1 && (current->num != -1 || found_current)) {
691 goto workspace_prev_on_output_end;
696 /* Find last workspace. */
698 NODES_FOREACH_REVERSE(output_get_content(output)) {
699 if (child->type != CT_WORKSPACE)
701 if (!prev || child->num > prev->num)
706 workspace_prev_on_output_end:
711 * Focuses the previously focused workspace.
714 void workspace_back_and_forth(void) {
715 if (!previous_workspace_name) {
716 DLOG("No previous workspace name set. Not switching.");
720 workspace_show_by_name(previous_workspace_name);
724 * Returns the previously focused workspace con, or NULL if unavailable.
727 Con *workspace_back_and_forth_get(void) {
728 if (!previous_workspace_name) {
729 DLOG("no previous workspace name set.");
734 workspace = workspace_get(previous_workspace_name, NULL);
739 static bool get_urgency_flag(Con *con) {
741 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
742 if (child->urgent || get_urgency_flag(child))
745 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
746 if (child->urgent || get_urgency_flag(child))
753 * Goes through all clients on the given workspace and updates the workspace’s
754 * urgent flag accordingly.
757 void workspace_update_urgent_flag(Con *ws) {
758 bool old_flag = ws->urgent;
759 ws->urgent = get_urgency_flag(ws);
760 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
762 if (old_flag != ws->urgent)
763 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
767 * 'Forces' workspace orientation by moving all cons into a new split-con with
768 * the same layout as the workspace and then changing the workspace layout.
771 void ws_force_orientation(Con *ws, orientation_t orientation) {
772 /* 1: create a new split container */
773 Con *split = con_new(NULL, NULL);
776 /* 2: copy layout from workspace */
777 split->layout = ws->layout;
779 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
781 /* 3: move the existing cons of this workspace below the new con */
782 DLOG("Moving cons\n");
783 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
784 Con *child = TAILQ_FIRST(&(ws->nodes_head));
786 con_attach(child, split, true);
789 /* 4: switch workspace layout */
790 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
791 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
793 /* 5: attach the new split container to the workspace */
794 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
795 con_attach(split, ws, false);
797 /* 6: fix the percentages */
801 con_focus(old_focused);
805 * Called when a new con (with a window, not an empty or split con) should be
806 * attached to the workspace (for example when managing a new window or when
807 * moving an existing window to the workspace level).
809 * Depending on the workspace_layout setting, this function either returns the
810 * workspace itself (default layout) or creates a new stacked/tabbed con and
814 Con *workspace_attach_to(Con *ws) {
815 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
817 if (ws->workspace_layout == L_DEFAULT) {
818 DLOG("Default layout, just attaching it to the workspace itself.\n");
822 DLOG("Non-default layout, creating a new split container\n");
823 /* 1: create a new split container */
824 Con *new = con_new(NULL, NULL);
827 /* 2: set the requested layout on the split con */
828 new->layout = ws->workspace_layout;
830 /* 4: attach the new split container to the workspace */
831 DLOG("Attaching new split %p to workspace %p\n", new, ws);
832 con_attach(new, ws, false);
838 * Creates a new container and re-parents all of children from the given
841 * The container inherits the layout from the workspace.
843 Con *workspace_encapsulate(Con *ws) {
844 if (TAILQ_EMPTY(&(ws->nodes_head))) {
845 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
849 Con *new = con_new(NULL, NULL);
851 new->layout = ws->layout;
853 DLOG("Moving children of workspace %p / %s into container %p\n",
857 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
858 child = TAILQ_FIRST(&(ws->nodes_head));
860 con_attach(child, new, true);
863 con_attach(new, ws, true);