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_names();
97 ewmh_update_desktop_viewport();
100 } else if (created != NULL) {
108 * Returns a pointer to a new workspace in the given output. The workspace
109 * is created attached to the tree hierarchy through the given content
113 Con *create_workspace_on_output(Output *output, Con *content) {
114 /* add a workspace to this output */
118 Con *ws = con_new(NULL, NULL);
119 ws->type = CT_WORKSPACE;
121 /* try the configured workspace bindings first to find a free name */
123 TAILQ_FOREACH(bind, bindings, bindings) {
124 DLOG("binding with command %s\n", bind->command);
125 if (strlen(bind->command) < strlen("workspace ") ||
126 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
128 DLOG("relevant command = %s\n", bind->command);
129 char *target = bind->command + strlen("workspace ");
130 while ((*target == ' ' || *target == '\t') && target != '\0')
132 /* We check if this is the workspace
133 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
134 * Beware: The workspace names "next", "prev", "next_on_output",
135 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
136 * so we check before stripping the double quotes */
137 if (strncasecmp(target, "next", strlen("next")) == 0 ||
138 strncasecmp(target, "prev", strlen("prev")) == 0 ||
139 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
140 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
141 strncasecmp(target, "number", strlen("number")) == 0 ||
142 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
143 strncasecmp(target, "current", strlen("current")) == 0)
147 if (strncasecmp(target, "__", strlen("__")) == 0) {
148 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
152 ws->name = strdup(target);
153 if (ws->name[strlen(ws->name) - 1] == '"')
154 ws->name[strlen(ws->name) - 1] = '\0';
155 DLOG("trying name *%s*\n", ws->name);
157 /* Ensure that this workspace is not assigned to a different output —
158 * otherwise we would create it, then move it over to its output, then
159 * find a new workspace, etc… */
160 bool assigned = false;
161 struct Workspace_Assignment *assignment;
162 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
163 if (strcmp(assignment->name, ws->name) != 0 ||
164 strcmp(assignment->output, output->name) == 0)
175 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
176 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
178 exists = (current != NULL);
180 /* Set ->num to the number of the workspace, if the name actually
181 * is a number or starts with a number */
182 ws->num = ws_name_to_number(ws->name);
183 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
190 /* get the next unused workspace number */
191 DLOG("Getting next unused workspace by number\n");
199 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
200 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
201 exists = (current != NULL);
203 DLOG("result for ws %d: exists = %d\n", c, exists);
205 sasprintf(&(ws->name), "%d", c);
207 con_attach(ws, content, false);
209 sasprintf(&name, "[i3 con] workspace %s", ws->name);
210 x_set_name(ws, name);
213 ws->fullscreen_mode = CF_OUTPUT;
215 ws->workspace_layout = config.default_layout;
216 _workspace_apply_default_orientation(ws);
222 * Returns true if the workspace is currently visible. Especially important for
223 * multi-monitor environments, as they can have multiple currenlty active
227 bool workspace_is_visible(Con *ws) {
228 Con *output = con_get_output(ws);
231 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
232 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
237 * XXX: we need to clean up all this recursive walking code.
240 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
243 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
244 if (current != exclude &&
245 current->sticky_group != NULL &&
246 current->window != NULL &&
247 strcmp(current->sticky_group, sticky_group) == 0)
250 Con *recurse = _get_sticky(current, sticky_group, exclude);
255 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
256 if (current != exclude &&
257 current->sticky_group != NULL &&
258 current->window != NULL &&
259 strcmp(current->sticky_group, sticky_group) == 0)
262 Con *recurse = _get_sticky(current, sticky_group, exclude);
271 * Reassigns all child windows in sticky containers. Called when the user
272 * changes workspaces.
274 * XXX: what about sticky containers which contain containers?
277 static void workspace_reassign_sticky(Con *con) {
279 /* 1: go through all containers */
281 /* handle all children and floating windows of this node */
282 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
283 if (current->sticky_group == NULL) {
284 workspace_reassign_sticky(current);
288 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
289 /* 2: find a window which we can re-assign */
290 Con *output = con_get_output(current);
291 Con *src = _get_sticky(output, current->sticky_group, current);
294 LOG("No window found for this sticky group\n");
295 workspace_reassign_sticky(current);
299 x_move_win(src, current);
300 current->window = src->window;
301 current->mapped = true;
305 x_reparent_child(current, src);
307 LOG("re-assigned window from src %p to dest %p\n", src, current);
310 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
311 workspace_reassign_sticky(current);
315 * Callback to reset the urgent flag of the given con to false. May be started by
316 * _workspace_show to avoid urgency hints being lost by switching to a workspace
320 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
323 DLOG("Resetting urgency flag of con %p by timer\n", con);
325 con_update_parents_urgency(con);
326 workspace_update_urgent_flag(con_get_workspace(con));
329 ev_timer_stop(main_loop, con->urgency_timer);
330 FREE(con->urgency_timer);
333 static void _workspace_show(Con *workspace) {
334 Con *current, *old = NULL;
336 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
337 if (con_is_internal(workspace))
340 /* disable fullscreen for the other workspaces and get the workspace we are
342 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
343 if (current->fullscreen_mode == CF_OUTPUT)
345 current->fullscreen_mode = CF_NONE;
348 /* enable fullscreen for the target workspace. If it happens to be the
349 * same one we are currently on anyways, we can stop here. */
350 workspace->fullscreen_mode = CF_OUTPUT;
351 current = con_get_workspace(focused);
352 if (workspace == current) {
353 DLOG("Not switching, already there.\n");
357 /* Remember currently focused workspace for switching back to it later with
358 * the 'workspace back_and_forth' command.
359 * NOTE: We have to duplicate the name as the original will be freed when
360 * the corresponding workspace is cleaned up.
361 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
362 * focused) are skipped, see bug #868. */
363 if (current && !con_is_internal(current)) {
364 FREE(previous_workspace_name);
366 previous_workspace_name = sstrdup(current->name);
367 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
371 workspace_reassign_sticky(workspace);
373 DLOG("switching to %p / %s\n", workspace, workspace->name);
374 Con *next = con_descend_focused(workspace);
376 /* Memorize current output */
377 Con *old_output = con_get_output(focused);
379 /* Display urgency hint for a while if the newly visible workspace would
380 * focus and thereby immediately destroy it */
381 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
385 /* … but immediately reset urgency flags; they will be set to false by
386 * the timer callback in case the container is focused at the time of
388 focused->urgent = true;
389 workspace->urgent = true;
391 if (focused->urgency_timer == NULL) {
392 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
394 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
395 /* use a repeating timer to allow for easy resets */
396 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
397 config.workspace_urgency_timer, config.workspace_urgency_timer);
398 focused->urgency_timer->data = focused;
399 ev_timer_start(main_loop, focused->urgency_timer);
401 DLOG("Resetting urgency timer of con %p on workspace %p\n",
403 ev_timer_again(main_loop, focused->urgency_timer);
408 ipc_send_workspace_focus_event(workspace, current);
410 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
411 /* Close old workspace if necessary. This must be done *after* doing
412 * urgency handling, because tree_close() will do a con_focus() on the next
413 * client, which will clear the urgency flag too early. Also, there is no
414 * way for con_focus() to know about when to clear urgency immediately and
415 * when to defer it. */
416 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
417 /* check if this workspace is currently visible */
418 if (!workspace_is_visible(old)) {
419 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
420 tree_close(old, DONT_KILL_WINDOW, false, false);
421 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
422 ewmh_update_number_of_desktops();
423 ewmh_update_desktop_names();
424 ewmh_update_desktop_viewport();
428 workspace->fullscreen_mode = CF_OUTPUT;
429 LOG("focused now = %p / %s\n", focused, focused->name);
431 /* Set mouse pointer */
432 Con *new_output = con_get_output(focused);
433 if (old_output != new_output) {
434 x_set_warp_to(&next->rect);
437 /* Update the EWMH hints */
438 ewmh_update_current_desktop();
442 * Switches to the given workspace
445 void workspace_show(Con *workspace) {
446 _workspace_show(workspace);
450 * Looks up the workspace by name and switches to it.
453 void workspace_show_by_name(const char *num) {
455 workspace = workspace_get(num, NULL);
456 _workspace_show(workspace);
460 * Focuses the next workspace.
463 Con *workspace_next(void) {
464 Con *current = con_get_workspace(focused);
468 if (current->num == -1) {
469 /* If currently a named workspace, find next named workspace. */
470 next = TAILQ_NEXT(current, nodes);
472 /* If currently a numbered workspace, find next numbered workspace. */
473 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
474 /* Skip outputs starting with __, they are internal. */
475 if (con_is_internal(output))
477 NODES_FOREACH(output_get_content(output)) {
478 if (child->type != CT_WORKSPACE)
480 if (child->num == -1)
482 /* Need to check child against current and next because we are
483 * traversing multiple lists and thus are not guaranteed the
484 * relative order between the list of workspaces. */
485 if (current->num < child->num && (!next || child->num < next->num))
491 /* Find next named workspace. */
493 bool found_current = false;
494 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
495 /* Skip outputs starting with __, they are internal. */
496 if (con_is_internal(output))
498 NODES_FOREACH(output_get_content(output)) {
499 if (child->type != CT_WORKSPACE)
501 if (child == current) {
503 } else if (child->num == -1 && (current->num != -1 || found_current)) {
505 goto workspace_next_end;
511 /* Find first workspace. */
513 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
514 /* Skip outputs starting with __, they are internal. */
515 if (con_is_internal(output))
517 NODES_FOREACH(output_get_content(output)) {
518 if (child->type != CT_WORKSPACE)
520 if (!next || (child->num != -1 && child->num < next->num))
530 * Focuses the previous workspace.
533 Con *workspace_prev(void) {
534 Con *current = con_get_workspace(focused);
538 if (current->num == -1) {
539 /* If named workspace, find previous named workspace. */
540 prev = TAILQ_PREV(current, nodes_head, nodes);
541 if (prev && prev->num != -1)
544 /* If numbered workspace, find previous numbered workspace. */
545 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
546 /* Skip outputs starting with __, they are internal. */
547 if (con_is_internal(output))
549 NODES_FOREACH_REVERSE(output_get_content(output)) {
550 if (child->type != CT_WORKSPACE || child->num == -1)
552 /* Need to check child against current and previous because we
553 * are traversing multiple lists and thus are not guaranteed
554 * the relative order between the list of workspaces. */
555 if (current->num > child->num && (!prev || child->num > prev->num))
561 /* Find previous named workspace. */
563 bool found_current = false;
564 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
565 /* Skip outputs starting with __, they are internal. */
566 if (con_is_internal(output))
568 NODES_FOREACH_REVERSE(output_get_content(output)) {
569 if (child->type != CT_WORKSPACE)
571 if (child == current) {
572 found_current = true;
573 } else if (child->num == -1 && (current->num != -1 || found_current)) {
575 goto workspace_prev_end;
581 /* Find last workspace. */
583 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
584 /* Skip outputs starting with __, they are internal. */
585 if (con_is_internal(output))
587 NODES_FOREACH_REVERSE(output_get_content(output)) {
588 if (child->type != CT_WORKSPACE)
590 if (!prev || child->num > prev->num)
601 * Focuses the next workspace on the same output.
604 Con *workspace_next_on_output(void) {
605 Con *current = con_get_workspace(focused);
607 Con *output = con_get_output(focused);
609 if (current->num == -1) {
610 /* If currently a named workspace, find next named workspace. */
611 next = TAILQ_NEXT(current, nodes);
613 /* If currently a numbered workspace, find next numbered workspace. */
614 NODES_FOREACH(output_get_content(output)) {
615 if (child->type != CT_WORKSPACE)
617 if (child->num == -1)
619 /* Need to check child against current and next because we are
620 * traversing multiple lists and thus are not guaranteed the
621 * relative order between the list of workspaces. */
622 if (current->num < child->num && (!next || child->num < next->num))
627 /* Find next named workspace. */
629 bool found_current = false;
630 NODES_FOREACH(output_get_content(output)) {
631 if (child->type != CT_WORKSPACE)
633 if (child == current) {
635 } else if (child->num == -1 && (current->num != -1 || found_current)) {
637 goto workspace_next_on_output_end;
642 /* Find first workspace. */
644 NODES_FOREACH(output_get_content(output)) {
645 if (child->type != CT_WORKSPACE)
647 if (!next || (child->num != -1 && child->num < next->num))
651 workspace_next_on_output_end:
656 * Focuses the previous workspace on same output.
659 Con *workspace_prev_on_output(void) {
660 Con *current = con_get_workspace(focused);
662 Con *output = con_get_output(focused);
663 DLOG("output = %s\n", output->name);
665 if (current->num == -1) {
666 /* If named workspace, find previous named workspace. */
667 prev = TAILQ_PREV(current, nodes_head, nodes);
668 if (prev && prev->num != -1)
671 /* If numbered workspace, find previous numbered workspace. */
672 NODES_FOREACH_REVERSE(output_get_content(output)) {
673 if (child->type != CT_WORKSPACE || child->num == -1)
675 /* Need to check child against current and previous because we
676 * are traversing multiple lists and thus are not guaranteed
677 * the relative order between the list of workspaces. */
678 if (current->num > child->num && (!prev || child->num > prev->num))
683 /* Find previous named workspace. */
685 bool found_current = false;
686 NODES_FOREACH_REVERSE(output_get_content(output)) {
687 if (child->type != CT_WORKSPACE)
689 if (child == current) {
690 found_current = true;
691 } else if (child->num == -1 && (current->num != -1 || found_current)) {
693 goto workspace_prev_on_output_end;
698 /* Find last workspace. */
700 NODES_FOREACH_REVERSE(output_get_content(output)) {
701 if (child->type != CT_WORKSPACE)
703 if (!prev || child->num > prev->num)
708 workspace_prev_on_output_end:
713 * Focuses the previously focused workspace.
716 void workspace_back_and_forth(void) {
717 if (!previous_workspace_name) {
718 DLOG("No previous workspace name set. Not switching.");
722 workspace_show_by_name(previous_workspace_name);
726 * Returns the previously focused workspace con, or NULL if unavailable.
729 Con *workspace_back_and_forth_get(void) {
730 if (!previous_workspace_name) {
731 DLOG("no previous workspace name set.");
736 workspace = workspace_get(previous_workspace_name, NULL);
741 static bool get_urgency_flag(Con *con) {
743 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
744 if (child->urgent || get_urgency_flag(child))
747 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
748 if (child->urgent || get_urgency_flag(child))
755 * Goes through all clients on the given workspace and updates the workspace’s
756 * urgent flag accordingly.
759 void workspace_update_urgent_flag(Con *ws) {
760 bool old_flag = ws->urgent;
761 ws->urgent = get_urgency_flag(ws);
762 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
764 if (old_flag != ws->urgent)
765 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
769 * 'Forces' workspace orientation by moving all cons into a new split-con with
770 * the same layout as the workspace and then changing the workspace layout.
773 void ws_force_orientation(Con *ws, orientation_t orientation) {
774 /* 1: create a new split container */
775 Con *split = con_new(NULL, NULL);
778 /* 2: copy layout from workspace */
779 split->layout = ws->layout;
781 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
783 /* 3: move the existing cons of this workspace below the new con */
784 DLOG("Moving cons\n");
785 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
786 Con *child = TAILQ_FIRST(&(ws->nodes_head));
788 con_attach(child, split, true);
791 /* 4: switch workspace layout */
792 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
793 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
795 /* 5: attach the new split container to the workspace */
796 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
797 con_attach(split, ws, false);
799 /* 6: fix the percentages */
803 con_focus(old_focused);
807 * Called when a new con (with a window, not an empty or split con) should be
808 * attached to the workspace (for example when managing a new window or when
809 * moving an existing window to the workspace level).
811 * Depending on the workspace_layout setting, this function either returns the
812 * workspace itself (default layout) or creates a new stacked/tabbed con and
816 Con *workspace_attach_to(Con *ws) {
817 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
819 if (ws->workspace_layout == L_DEFAULT) {
820 DLOG("Default layout, just attaching it to the workspace itself.\n");
824 DLOG("Non-default layout, creating a new split container\n");
825 /* 1: create a new split container */
826 Con *new = con_new(NULL, NULL);
829 /* 2: set the requested layout on the split con */
830 new->layout = ws->workspace_layout;
832 /* 4: attach the new split container to the workspace */
833 DLOG("Attaching new split %p to workspace %p\n", new, ws);
834 con_attach(new, ws, false);
840 * Creates a new container and re-parents all of children from the given
843 * The container inherits the layout from the workspace.
845 Con *workspace_encapsulate(Con *ws) {
846 if (TAILQ_EMPTY(&(ws->nodes_head))) {
847 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
851 Con *new = con_new(NULL, NULL);
853 new->layout = ws->layout;
855 DLOG("Moving children of workspace %p / %s into container %p\n",
859 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
860 child = TAILQ_FIRST(&(ws->nodes_head));
862 con_attach(child, new, true);
865 con_attach(new, ws, true);