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;
56 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
57 if (strcmp(assignment->name, num) != 0)
60 LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
61 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
64 Con *content = output_get_content(output);
65 LOG("got output %p with content %p\n", output, content);
66 /* We need to attach this container after setting its type. con_attach
67 * will handle CT_WORKSPACEs differently */
68 workspace = con_new(NULL, NULL);
70 sasprintf(&name, "[i3 con] workspace %s", num);
71 x_set_name(workspace, name);
73 workspace->type = CT_WORKSPACE;
74 FREE(workspace->name);
75 workspace->name = sstrdup(num);
76 workspace->workspace_layout = config.default_layout;
77 /* We set ->num to the number if this workspace’s name begins with a
78 * positive number. Otherwise it’s a named ws and num will be -1. */
80 long parsed_num = strtol(num, &endptr, 10);
81 if (parsed_num == LONG_MIN ||
82 parsed_num == LONG_MAX ||
86 else 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\"}");
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 /* We check if this is the workspace
129 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
130 * Beware: The workspace names "next", "prev", "next_on_output",
131 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
132 * so we check before stripping the double quotes */
133 if (strncasecmp(target, "next", strlen("next")) == 0 ||
134 strncasecmp(target, "prev", strlen("prev")) == 0 ||
135 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
136 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
137 strncasecmp(target, "number", strlen("number")) == 0 ||
138 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
139 strncasecmp(target, "current", strlen("current")) == 0)
144 ws->name = strdup(target);
145 if (ws->name[strlen(ws->name)-1] == '"')
146 ws->name[strlen(ws->name)-1] = '\0';
147 DLOG("trying name *%s*\n", ws->name);
149 /* Ensure that this workspace is not assigned to a different output —
150 * otherwise we would create it, then move it over to its output, then
151 * find a new workspace, etc… */
152 bool assigned = false;
153 struct Workspace_Assignment *assignment;
154 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
155 if (strcmp(assignment->name, ws->name) != 0 ||
156 strcmp(assignment->output, output->name) == 0)
167 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
168 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
170 exists = (current != NULL);
172 /* Set ->num to the number of the workspace, if the name actually
173 * is a number or starts with a number */
175 long parsed_num = strtol(ws->name, &endptr, 10);
176 if (parsed_num == LONG_MIN ||
177 parsed_num == LONG_MAX ||
181 else ws->num = parsed_num;
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");
196 sasprintf(&(ws->name), "%d", c);
199 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
200 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
201 exists = (current != NULL);
203 DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
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);
223 * Returns true if the workspace is currently visible. Especially important for
224 * multi-monitor environments, as they can have multiple currenlty active
228 bool workspace_is_visible(Con *ws) {
229 Con *output = con_get_output(ws);
232 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
233 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
238 * XXX: we need to clean up all this recursive walking code.
241 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
244 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
245 if (current != exclude &&
246 current->sticky_group != NULL &&
247 current->window != NULL &&
248 strcmp(current->sticky_group, sticky_group) == 0)
251 Con *recurse = _get_sticky(current, sticky_group, exclude);
256 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
257 if (current != exclude &&
258 current->sticky_group != NULL &&
259 current->window != NULL &&
260 strcmp(current->sticky_group, sticky_group) == 0)
263 Con *recurse = _get_sticky(current, sticky_group, exclude);
272 * Reassigns all child windows in sticky containers. Called when the user
273 * changes workspaces.
275 * XXX: what about sticky containers which contain containers?
278 static void workspace_reassign_sticky(Con *con) {
280 /* 1: go through all containers */
282 /* handle all children and floating windows of this node */
283 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
284 if (current->sticky_group == NULL) {
285 workspace_reassign_sticky(current);
289 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
290 /* 2: find a window which we can re-assign */
291 Con *output = con_get_output(current);
292 Con *src = _get_sticky(output, current->sticky_group, current);
295 LOG("No window found for this sticky group\n");
296 workspace_reassign_sticky(current);
300 x_move_win(src, current);
301 current->window = src->window;
302 current->mapped = true;
306 x_reparent_child(current, src);
308 LOG("re-assigned window from src %p to dest %p\n", src, current);
311 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
312 workspace_reassign_sticky(current);
316 * Callback to reset the urgent flag of the given con to false. May be started by
317 * _workspace_show to avoid urgency hints being lost by switching to a workspace
321 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
324 DLOG("Resetting urgency flag of con %p by timer\n", con);
326 con_update_parents_urgency(con);
327 workspace_update_urgent_flag(con_get_workspace(con));
330 ev_timer_stop(main_loop, con->urgency_timer);
331 FREE(con->urgency_timer);
334 static void _workspace_show(Con *workspace) {
335 Con *current, *old = NULL;
337 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
338 if (con_is_internal(workspace))
341 /* disable fullscreen for the other workspaces and get the workspace we are
343 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
344 if (current->fullscreen_mode == CF_OUTPUT)
346 current->fullscreen_mode = CF_NONE;
349 /* enable fullscreen for the target workspace. If it happens to be the
350 * same one we are currently on anyways, we can stop here. */
351 workspace->fullscreen_mode = CF_OUTPUT;
352 current = con_get_workspace(focused);
353 if (workspace == current) {
354 DLOG("Not switching, already there.\n");
358 /* Remember currently focused workspace for switching back to it later with
359 * the 'workspace back_and_forth' command.
360 * NOTE: We have to duplicate the name as the original will be freed when
361 * the corresponding workspace is cleaned up.
362 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
363 * focused) are skipped, see bug #868. */
364 if (current && !con_is_internal(current)) {
365 FREE(previous_workspace_name);
367 previous_workspace_name = sstrdup(current->name);
368 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
372 workspace_reassign_sticky(workspace);
374 DLOG("switching to %p / %s\n", workspace, workspace->name);
375 Con *next = con_descend_focused(workspace);
377 /* Memorize current output */
378 Con *old_output = con_get_output(focused);
380 /* Display urgency hint for a while if the newly visible workspace would
381 * focus and thereby immediately destroy it */
382 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
386 /* … but immediately reset urgency flags; they will be set to false by
387 * the timer callback in case the container is focused at the time of
389 focused->urgent = true;
390 workspace->urgent = true;
392 if (focused->urgency_timer == NULL) {
393 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
395 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
396 /* use a repeating timer to allow for easy resets */
397 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
398 config.workspace_urgency_timer, config.workspace_urgency_timer);
399 focused->urgency_timer->data = focused;
400 ev_timer_start(main_loop, focused->urgency_timer);
402 DLOG("Resetting urgency timer of con %p on workspace %p\n",
404 ev_timer_again(main_loop, focused->urgency_timer);
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\"}");
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();
436 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
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)
600 * Focuses the next workspace on the same output.
603 Con* workspace_next_on_output(void) {
604 Con *current = con_get_workspace(focused);
606 Con *output = con_get_output(focused);
608 if (current->num == -1) {
609 /* If currently a named workspace, find next named workspace. */
610 next = TAILQ_NEXT(current, nodes);
612 /* If currently a numbered workspace, find next numbered workspace. */
613 NODES_FOREACH(output_get_content(output)) {
614 if (child->type != CT_WORKSPACE)
616 if (child->num == -1)
618 /* Need to check child against current and next because we are
619 * traversing multiple lists and thus are not guaranteed the
620 * relative order between the list of workspaces. */
621 if (current->num < child->num && (!next || child->num < next->num))
626 /* Find next named workspace. */
628 bool found_current = false;
629 NODES_FOREACH(output_get_content(output)) {
630 if (child->type != CT_WORKSPACE)
632 if (child == current) {
634 } else if (child->num == -1 && (current->num != -1 || found_current)) {
636 goto workspace_next_on_output_end;
641 /* Find first workspace. */
643 NODES_FOREACH(output_get_content(output)) {
644 if (child->type != CT_WORKSPACE)
646 if (!next || (child->num != -1 && child->num < next->num))
650 workspace_next_on_output_end:
655 * Focuses the previous workspace on same output.
658 Con* workspace_prev_on_output(void) {
659 Con *current = con_get_workspace(focused);
661 Con *output = con_get_output(focused);
662 DLOG("output = %s\n", output->name);
664 if (current->num == -1) {
665 /* If named workspace, find previous named workspace. */
666 prev = TAILQ_PREV(current, nodes_head, nodes);
667 if (prev && prev->num != -1)
670 /* If numbered workspace, find previous numbered workspace. */
671 NODES_FOREACH_REVERSE(output_get_content(output)) {
672 if (child->type != CT_WORKSPACE || child->num == -1)
674 /* Need to check child against current and previous because we
675 * are traversing multiple lists and thus are not guaranteed
676 * the relative order between the list of workspaces. */
677 if (current->num > child->num && (!prev || child->num > prev->num))
682 /* Find previous named workspace. */
684 bool found_current = false;
685 NODES_FOREACH_REVERSE(output_get_content(output)) {
686 if (child->type != CT_WORKSPACE)
688 if (child == current) {
689 found_current = true;
690 } else if (child->num == -1 && (current->num != -1 || found_current)) {
692 goto workspace_prev_on_output_end;
697 /* Find last workspace. */
699 NODES_FOREACH_REVERSE(output_get_content(output)) {
700 if (child->type != CT_WORKSPACE)
702 if (!prev || child->num > prev->num)
707 workspace_prev_on_output_end:
712 * Focuses the previously focused workspace.
715 void workspace_back_and_forth(void) {
716 if (!previous_workspace_name) {
717 DLOG("No previous workspace name set. Not switching.");
721 workspace_show_by_name(previous_workspace_name);
725 * Returns the previously focused workspace con, or NULL if unavailable.
728 Con *workspace_back_and_forth_get(void) {
729 if (!previous_workspace_name) {
730 DLOG("no previous workspace name set.");
735 workspace = workspace_get(previous_workspace_name, NULL);
740 static bool get_urgency_flag(Con *con) {
742 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
743 if (child->urgent || get_urgency_flag(child))
746 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
747 if (child->urgent || get_urgency_flag(child))
754 * Goes through all clients on the given workspace and updates the workspace’s
755 * urgent flag accordingly.
758 void workspace_update_urgent_flag(Con *ws) {
759 bool old_flag = ws->urgent;
760 ws->urgent = get_urgency_flag(ws);
761 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
763 if (old_flag != ws->urgent)
764 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
768 * 'Forces' workspace orientation by moving all cons into a new split-con with
769 * the same layout as the workspace and then changing the workspace layout.
772 void ws_force_orientation(Con *ws, orientation_t orientation) {
773 /* 1: create a new split container */
774 Con *split = con_new(NULL, NULL);
777 /* 2: copy layout from workspace */
778 split->layout = ws->layout;
780 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
782 /* 3: move the existing cons of this workspace below the new con */
783 DLOG("Moving cons\n");
784 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
785 Con *child = TAILQ_FIRST(&(ws->nodes_head));
787 con_attach(child, split, true);
790 /* 4: switch workspace layout */
791 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
792 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
794 /* 5: attach the new split container to the workspace */
795 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
796 con_attach(split, ws, false);
798 /* 6: fix the percentages */
802 con_focus(old_focused);
806 * Called when a new con (with a window, not an empty or split con) should be
807 * attached to the workspace (for example when managing a new window or when
808 * moving an existing window to the workspace level).
810 * Depending on the workspace_layout setting, this function either returns the
811 * workspace itself (default layout) or creates a new stacked/tabbed con and
815 Con *workspace_attach_to(Con *ws) {
816 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
818 if (ws->workspace_layout == L_DEFAULT) {
819 DLOG("Default layout, just attaching it to the workspace itself.\n");
823 DLOG("Non-default layout, creating a new split container\n");
824 /* 1: create a new split container */
825 Con *new = con_new(NULL, NULL);
828 /* 2: set the requested layout on the split con */
829 new->layout = ws->workspace_layout;
831 /* 4: attach the new split container to the workspace */
832 DLOG("Attaching new split %p to workspace %p\n", new, ws);
833 con_attach(new, ws, false);
839 * Creates a new container and re-parents all of children from the given
842 * The container inherits the layout from the workspace.
844 Con *workspace_encapsulate(Con *ws) {
845 if (TAILQ_EMPTY(&(ws->nodes_head))) {
846 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
850 Con *new = con_new(NULL, NULL);
852 new->layout = ws->layout;
854 DLOG("Moving children of workspace %p / %s into container %p\n",
858 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
859 child = TAILQ_FIRST(&(ws->nodes_head));
861 con_attach(child, new, true);
864 con_attach(new, ws, true);