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();
98 } else if (created != NULL) {
106 * Returns a pointer to a new workspace in the given output. The workspace
107 * is created attached to the tree hierarchy through the given content
111 Con *create_workspace_on_output(Output *output, Con *content) {
112 /* add a workspace to this output */
116 Con *ws = con_new(NULL, NULL);
117 ws->type = CT_WORKSPACE;
119 /* try the configured workspace bindings first to find a free name */
121 TAILQ_FOREACH(bind, bindings, bindings) {
122 DLOG("binding with command %s\n", bind->command);
123 if (strlen(bind->command) < strlen("workspace ") ||
124 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
126 DLOG("relevant command = %s\n", bind->command);
127 char *target = bind->command + strlen("workspace ");
128 while ((*target == ' ' || *target == '\t') && target != '\0')
130 /* We check if this is the workspace
131 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
132 * Beware: The workspace names "next", "prev", "next_on_output",
133 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
134 * so we check before stripping the double quotes */
135 if (strncasecmp(target, "next", strlen("next")) == 0 ||
136 strncasecmp(target, "prev", strlen("prev")) == 0 ||
137 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
138 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
139 strncasecmp(target, "number", strlen("number")) == 0 ||
140 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
141 strncasecmp(target, "current", strlen("current")) == 0)
145 if (strncasecmp(target, "__", strlen("__")) == 0) {
146 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
150 ws->name = strdup(target);
151 if (ws->name[strlen(ws->name) - 1] == '"')
152 ws->name[strlen(ws->name) - 1] = '\0';
153 DLOG("trying name *%s*\n", ws->name);
155 /* Ensure that this workspace is not assigned to a different output —
156 * otherwise we would create it, then move it over to its output, then
157 * find a new workspace, etc… */
158 bool assigned = false;
159 struct Workspace_Assignment *assignment;
160 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
161 if (strcmp(assignment->name, ws->name) != 0 ||
162 strcmp(assignment->output, output->name) == 0)
173 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
174 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
176 exists = (current != NULL);
178 /* Set ->num to the number of the workspace, if the name actually
179 * is a number or starts with a number */
180 ws->num = ws_name_to_number(ws->name);
181 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
188 /* get the next unused workspace number */
189 DLOG("Getting next unused workspace by number\n");
197 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
198 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
199 exists = (current != NULL);
201 DLOG("result for ws %d: exists = %d\n", c, exists);
203 sasprintf(&(ws->name), "%d", c);
205 con_attach(ws, content, false);
207 sasprintf(&name, "[i3 con] workspace %s", ws->name);
208 x_set_name(ws, name);
211 ws->fullscreen_mode = CF_OUTPUT;
213 ws->workspace_layout = config.default_layout;
214 _workspace_apply_default_orientation(ws);
220 * Returns true if the workspace is currently visible. Especially important for
221 * multi-monitor environments, as they can have multiple currenlty active
225 bool workspace_is_visible(Con *ws) {
226 Con *output = con_get_output(ws);
229 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
230 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
235 * XXX: we need to clean up all this recursive walking code.
238 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
241 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
242 if (current != exclude &&
243 current->sticky_group != NULL &&
244 current->window != NULL &&
245 strcmp(current->sticky_group, sticky_group) == 0)
248 Con *recurse = _get_sticky(current, sticky_group, exclude);
253 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
254 if (current != exclude &&
255 current->sticky_group != NULL &&
256 current->window != NULL &&
257 strcmp(current->sticky_group, sticky_group) == 0)
260 Con *recurse = _get_sticky(current, sticky_group, exclude);
269 * Reassigns all child windows in sticky containers. Called when the user
270 * changes workspaces.
272 * XXX: what about sticky containers which contain containers?
275 static void workspace_reassign_sticky(Con *con) {
277 /* 1: go through all containers */
279 /* handle all children and floating windows of this node */
280 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
281 if (current->sticky_group == NULL) {
282 workspace_reassign_sticky(current);
286 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
287 /* 2: find a window which we can re-assign */
288 Con *output = con_get_output(current);
289 Con *src = _get_sticky(output, current->sticky_group, current);
292 LOG("No window found for this sticky group\n");
293 workspace_reassign_sticky(current);
297 x_move_win(src, current);
298 current->window = src->window;
299 current->mapped = true;
303 x_reparent_child(current, src);
305 LOG("re-assigned window from src %p to dest %p\n", src, current);
308 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
309 workspace_reassign_sticky(current);
313 * Callback to reset the urgent flag of the given con to false. May be started by
314 * _workspace_show to avoid urgency hints being lost by switching to a workspace
318 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
321 DLOG("Resetting urgency flag of con %p by timer\n", con);
323 con_update_parents_urgency(con);
324 workspace_update_urgent_flag(con_get_workspace(con));
327 ev_timer_stop(main_loop, con->urgency_timer);
328 FREE(con->urgency_timer);
331 static void _workspace_show(Con *workspace) {
332 Con *current, *old = NULL;
334 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
335 if (con_is_internal(workspace))
338 /* disable fullscreen for the other workspaces and get the workspace we are
340 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
341 if (current->fullscreen_mode == CF_OUTPUT)
343 current->fullscreen_mode = CF_NONE;
346 /* enable fullscreen for the target workspace. If it happens to be the
347 * same one we are currently on anyways, we can stop here. */
348 workspace->fullscreen_mode = CF_OUTPUT;
349 current = con_get_workspace(focused);
350 if (workspace == current) {
351 DLOG("Not switching, already there.\n");
355 /* Remember currently focused workspace for switching back to it later with
356 * the 'workspace back_and_forth' command.
357 * NOTE: We have to duplicate the name as the original will be freed when
358 * the corresponding workspace is cleaned up.
359 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
360 * focused) are skipped, see bug #868. */
361 if (current && !con_is_internal(current)) {
362 FREE(previous_workspace_name);
364 previous_workspace_name = sstrdup(current->name);
365 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
369 workspace_reassign_sticky(workspace);
371 DLOG("switching to %p / %s\n", workspace, workspace->name);
372 Con *next = con_descend_focused(workspace);
374 /* Memorize current output */
375 Con *old_output = con_get_output(focused);
377 /* Display urgency hint for a while if the newly visible workspace would
378 * focus and thereby immediately destroy it */
379 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
383 /* … but immediately reset urgency flags; they will be set to false by
384 * the timer callback in case the container is focused at the time of
386 focused->urgent = true;
387 workspace->urgent = true;
389 if (focused->urgency_timer == NULL) {
390 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
392 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
393 /* use a repeating timer to allow for easy resets */
394 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
395 config.workspace_urgency_timer, config.workspace_urgency_timer);
396 focused->urgency_timer->data = focused;
397 ev_timer_start(main_loop, focused->urgency_timer);
399 DLOG("Resetting urgency timer of con %p on workspace %p\n",
401 ev_timer_again(main_loop, focused->urgency_timer);
406 ipc_send_workspace_focus_event(workspace, current);
408 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
409 /* Close old workspace if necessary. This must be done *after* doing
410 * urgency handling, because tree_close() will do a con_focus() on the next
411 * client, which will clear the urgency flag too early. Also, there is no
412 * way for con_focus() to know about when to clear urgency immediately and
413 * when to defer it. */
414 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
415 /* check if this workspace is currently visible */
416 if (!workspace_is_visible(old)) {
417 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
418 tree_close(old, DONT_KILL_WINDOW, false, false);
419 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
420 ewmh_update_number_of_desktops();
424 workspace->fullscreen_mode = CF_OUTPUT;
425 LOG("focused now = %p / %s\n", focused, focused->name);
427 /* Set mouse pointer */
428 Con *new_output = con_get_output(focused);
429 if (old_output != new_output) {
430 x_set_warp_to(&next->rect);
433 /* Update the EWMH hints */
434 ewmh_update_current_desktop();
438 * Switches to the given workspace
441 void workspace_show(Con *workspace) {
442 _workspace_show(workspace);
446 * Looks up the workspace by name and switches to it.
449 void workspace_show_by_name(const char *num) {
451 workspace = workspace_get(num, NULL);
452 _workspace_show(workspace);
456 * Focuses the next workspace.
459 Con *workspace_next(void) {
460 Con *current = con_get_workspace(focused);
464 if (current->num == -1) {
465 /* If currently a named workspace, find next named workspace. */
466 next = TAILQ_NEXT(current, nodes);
468 /* If currently a numbered workspace, find next numbered workspace. */
469 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
470 /* Skip outputs starting with __, they are internal. */
471 if (con_is_internal(output))
473 NODES_FOREACH(output_get_content(output)) {
474 if (child->type != CT_WORKSPACE)
476 if (child->num == -1)
478 /* Need to check child against current and next because we are
479 * traversing multiple lists and thus are not guaranteed the
480 * relative order between the list of workspaces. */
481 if (current->num < child->num && (!next || child->num < next->num))
487 /* Find next named workspace. */
489 bool found_current = false;
490 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
491 /* Skip outputs starting with __, they are internal. */
492 if (con_is_internal(output))
494 NODES_FOREACH(output_get_content(output)) {
495 if (child->type != CT_WORKSPACE)
497 if (child == current) {
499 } else if (child->num == -1 && (current->num != -1 || found_current)) {
501 goto workspace_next_end;
507 /* Find first workspace. */
509 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
510 /* Skip outputs starting with __, they are internal. */
511 if (con_is_internal(output))
513 NODES_FOREACH(output_get_content(output)) {
514 if (child->type != CT_WORKSPACE)
516 if (!next || (child->num != -1 && child->num < next->num))
526 * Focuses the previous workspace.
529 Con *workspace_prev(void) {
530 Con *current = con_get_workspace(focused);
534 if (current->num == -1) {
535 /* If named workspace, find previous named workspace. */
536 prev = TAILQ_PREV(current, nodes_head, nodes);
537 if (prev && prev->num != -1)
540 /* If numbered workspace, find previous numbered workspace. */
541 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
542 /* Skip outputs starting with __, they are internal. */
543 if (con_is_internal(output))
545 NODES_FOREACH_REVERSE(output_get_content(output)) {
546 if (child->type != CT_WORKSPACE || child->num == -1)
548 /* Need to check child against current and previous because we
549 * are traversing multiple lists and thus are not guaranteed
550 * the relative order between the list of workspaces. */
551 if (current->num > child->num && (!prev || child->num > prev->num))
557 /* Find previous named workspace. */
559 bool found_current = false;
560 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
561 /* Skip outputs starting with __, they are internal. */
562 if (con_is_internal(output))
564 NODES_FOREACH_REVERSE(output_get_content(output)) {
565 if (child->type != CT_WORKSPACE)
567 if (child == current) {
568 found_current = true;
569 } else if (child->num == -1 && (current->num != -1 || found_current)) {
571 goto workspace_prev_end;
577 /* Find last workspace. */
579 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
580 /* Skip outputs starting with __, they are internal. */
581 if (con_is_internal(output))
583 NODES_FOREACH_REVERSE(output_get_content(output)) {
584 if (child->type != CT_WORKSPACE)
586 if (!prev || child->num > prev->num)
597 * Focuses the next workspace on the same output.
600 Con *workspace_next_on_output(void) {
601 Con *current = con_get_workspace(focused);
603 Con *output = con_get_output(focused);
605 if (current->num == -1) {
606 /* If currently a named workspace, find next named workspace. */
607 next = TAILQ_NEXT(current, nodes);
609 /* If currently a numbered workspace, find next numbered workspace. */
610 NODES_FOREACH(output_get_content(output)) {
611 if (child->type != CT_WORKSPACE)
613 if (child->num == -1)
615 /* Need to check child against current and next because we are
616 * traversing multiple lists and thus are not guaranteed the
617 * relative order between the list of workspaces. */
618 if (current->num < child->num && (!next || child->num < next->num))
623 /* Find next named workspace. */
625 bool found_current = false;
626 NODES_FOREACH(output_get_content(output)) {
627 if (child->type != CT_WORKSPACE)
629 if (child == current) {
631 } else if (child->num == -1 && (current->num != -1 || found_current)) {
633 goto workspace_next_on_output_end;
638 /* Find first workspace. */
640 NODES_FOREACH(output_get_content(output)) {
641 if (child->type != CT_WORKSPACE)
643 if (!next || (child->num != -1 && child->num < next->num))
647 workspace_next_on_output_end:
652 * Focuses the previous workspace on same output.
655 Con *workspace_prev_on_output(void) {
656 Con *current = con_get_workspace(focused);
658 Con *output = con_get_output(focused);
659 DLOG("output = %s\n", output->name);
661 if (current->num == -1) {
662 /* If named workspace, find previous named workspace. */
663 prev = TAILQ_PREV(current, nodes_head, nodes);
664 if (prev && prev->num != -1)
667 /* If numbered workspace, find previous numbered workspace. */
668 NODES_FOREACH_REVERSE(output_get_content(output)) {
669 if (child->type != CT_WORKSPACE || child->num == -1)
671 /* Need to check child against current and previous because we
672 * are traversing multiple lists and thus are not guaranteed
673 * the relative order between the list of workspaces. */
674 if (current->num > child->num && (!prev || child->num > prev->num))
679 /* Find previous named workspace. */
681 bool found_current = false;
682 NODES_FOREACH_REVERSE(output_get_content(output)) {
683 if (child->type != CT_WORKSPACE)
685 if (child == current) {
686 found_current = true;
687 } else if (child->num == -1 && (current->num != -1 || found_current)) {
689 goto workspace_prev_on_output_end;
694 /* Find last workspace. */
696 NODES_FOREACH_REVERSE(output_get_content(output)) {
697 if (child->type != CT_WORKSPACE)
699 if (!prev || child->num > prev->num)
704 workspace_prev_on_output_end:
709 * Focuses the previously focused workspace.
712 void workspace_back_and_forth(void) {
713 if (!previous_workspace_name) {
714 DLOG("No previous workspace name set. Not switching.");
718 workspace_show_by_name(previous_workspace_name);
722 * Returns the previously focused workspace con, or NULL if unavailable.
725 Con *workspace_back_and_forth_get(void) {
726 if (!previous_workspace_name) {
727 DLOG("no previous workspace name set.");
732 workspace = workspace_get(previous_workspace_name, NULL);
737 static bool get_urgency_flag(Con *con) {
739 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
740 if (child->urgent || get_urgency_flag(child))
743 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
744 if (child->urgent || get_urgency_flag(child))
751 * Goes through all clients on the given workspace and updates the workspace’s
752 * urgent flag accordingly.
755 void workspace_update_urgent_flag(Con *ws) {
756 bool old_flag = ws->urgent;
757 ws->urgent = get_urgency_flag(ws);
758 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
760 if (old_flag != ws->urgent)
761 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
765 * 'Forces' workspace orientation by moving all cons into a new split-con with
766 * the same layout as the workspace and then changing the workspace layout.
769 void ws_force_orientation(Con *ws, orientation_t orientation) {
770 /* 1: create a new split container */
771 Con *split = con_new(NULL, NULL);
774 /* 2: copy layout from workspace */
775 split->layout = ws->layout;
777 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
779 /* 3: move the existing cons of this workspace below the new con */
780 DLOG("Moving cons\n");
781 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
782 Con *child = TAILQ_FIRST(&(ws->nodes_head));
784 con_attach(child, split, true);
787 /* 4: switch workspace layout */
788 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
789 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
791 /* 5: attach the new split container to the workspace */
792 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
793 con_attach(split, ws, false);
795 /* 6: fix the percentages */
799 con_focus(old_focused);
803 * Called when a new con (with a window, not an empty or split con) should be
804 * attached to the workspace (for example when managing a new window or when
805 * moving an existing window to the workspace level).
807 * Depending on the workspace_layout setting, this function either returns the
808 * workspace itself (default layout) or creates a new stacked/tabbed con and
812 Con *workspace_attach_to(Con *ws) {
813 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
815 if (ws->workspace_layout == L_DEFAULT) {
816 DLOG("Default layout, just attaching it to the workspace itself.\n");
820 DLOG("Non-default layout, creating a new split container\n");
821 /* 1: create a new split container */
822 Con *new = con_new(NULL, NULL);
825 /* 2: set the requested layout on the split con */
826 new->layout = ws->workspace_layout;
828 /* 4: attach the new split container to the workspace */
829 DLOG("Attaching new split %p to workspace %p\n", new, ws);
830 con_attach(new, ws, false);
836 * Creates a new container and re-parents all of children from the given
839 * The container inherits the layout from the workspace.
841 Con *workspace_encapsulate(Con *ws) {
842 if (TAILQ_EMPTY(&(ws->nodes_head))) {
843 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
847 Con *new = con_new(NULL, NULL);
849 new->layout = ws->layout;
851 DLOG("Moving children of workspace %p / %s into container %p\n",
855 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
856 child = TAILQ_FIRST(&(ws->nodes_head));
858 con_attach(child, new, true);
861 con_attach(new, ws, true);