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 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. '__' is a reserved prefix.\n");
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 */
181 long parsed_num = strtol(ws->name, &endptr, 10);
182 if (parsed_num == LONG_MIN ||
183 parsed_num == LONG_MAX ||
187 else ws->num = parsed_num;
188 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
195 /* get the next unused workspace number */
196 DLOG("Getting next unused workspace by number\n");
204 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
205 GREP_FIRST(current, output_get_content(out), child->num == ws->num);
206 exists = (current != NULL);
208 DLOG("result for ws %d: exists = %d\n", c, exists);
210 sasprintf(&(ws->name), "%d", c);
212 con_attach(ws, content, false);
214 sasprintf(&name, "[i3 con] workspace %s", ws->name);
215 x_set_name(ws, name);
218 ws->fullscreen_mode = CF_OUTPUT;
220 ws->workspace_layout = config.default_layout;
221 _workspace_apply_default_orientation(ws);
228 * Returns true if the workspace is currently visible. Especially important for
229 * multi-monitor environments, as they can have multiple currenlty active
233 bool workspace_is_visible(Con *ws) {
234 Con *output = con_get_output(ws);
237 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
238 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
243 * XXX: we need to clean up all this recursive walking code.
246 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
249 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
250 if (current != exclude &&
251 current->sticky_group != NULL &&
252 current->window != NULL &&
253 strcmp(current->sticky_group, sticky_group) == 0)
256 Con *recurse = _get_sticky(current, sticky_group, exclude);
261 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
262 if (current != exclude &&
263 current->sticky_group != NULL &&
264 current->window != NULL &&
265 strcmp(current->sticky_group, sticky_group) == 0)
268 Con *recurse = _get_sticky(current, sticky_group, exclude);
277 * Reassigns all child windows in sticky containers. Called when the user
278 * changes workspaces.
280 * XXX: what about sticky containers which contain containers?
283 static void workspace_reassign_sticky(Con *con) {
285 /* 1: go through all containers */
287 /* handle all children and floating windows of this node */
288 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
289 if (current->sticky_group == NULL) {
290 workspace_reassign_sticky(current);
294 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
295 /* 2: find a window which we can re-assign */
296 Con *output = con_get_output(current);
297 Con *src = _get_sticky(output, current->sticky_group, current);
300 LOG("No window found for this sticky group\n");
301 workspace_reassign_sticky(current);
305 x_move_win(src, current);
306 current->window = src->window;
307 current->mapped = true;
311 x_reparent_child(current, src);
313 LOG("re-assigned window from src %p to dest %p\n", src, current);
316 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
317 workspace_reassign_sticky(current);
321 * Callback to reset the urgent flag of the given con to false. May be started by
322 * _workspace_show to avoid urgency hints being lost by switching to a workspace
326 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
329 DLOG("Resetting urgency flag of con %p by timer\n", con);
331 con_update_parents_urgency(con);
332 workspace_update_urgent_flag(con_get_workspace(con));
335 ev_timer_stop(main_loop, con->urgency_timer);
336 FREE(con->urgency_timer);
339 static void _workspace_show(Con *workspace) {
340 Con *current, *old = NULL;
342 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
343 if (con_is_internal(workspace))
346 /* disable fullscreen for the other workspaces and get the workspace we are
348 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
349 if (current->fullscreen_mode == CF_OUTPUT)
351 current->fullscreen_mode = CF_NONE;
354 /* enable fullscreen for the target workspace. If it happens to be the
355 * same one we are currently on anyways, we can stop here. */
356 workspace->fullscreen_mode = CF_OUTPUT;
357 current = con_get_workspace(focused);
358 if (workspace == current) {
359 DLOG("Not switching, already there.\n");
363 /* Remember currently focused workspace for switching back to it later with
364 * the 'workspace back_and_forth' command.
365 * NOTE: We have to duplicate the name as the original will be freed when
366 * the corresponding workspace is cleaned up.
367 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
368 * focused) are skipped, see bug #868. */
369 if (current && !con_is_internal(current)) {
370 FREE(previous_workspace_name);
372 previous_workspace_name = sstrdup(current->name);
373 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
377 workspace_reassign_sticky(workspace);
379 DLOG("switching to %p / %s\n", workspace, workspace->name);
380 Con *next = con_descend_focused(workspace);
382 /* Memorize current output */
383 Con *old_output = con_get_output(focused);
385 /* Display urgency hint for a while if the newly visible workspace would
386 * focus and thereby immediately destroy it */
387 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
391 /* … but immediately reset urgency flags; they will be set to false by
392 * the timer callback in case the container is focused at the time of
394 focused->urgent = true;
395 workspace->urgent = true;
397 if (focused->urgency_timer == NULL) {
398 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
400 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
401 /* use a repeating timer to allow for easy resets */
402 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
403 config.workspace_urgency_timer, config.workspace_urgency_timer);
404 focused->urgency_timer->data = focused;
405 ev_timer_start(main_loop, focused->urgency_timer);
407 DLOG("Resetting urgency timer of con %p on workspace %p\n",
409 ev_timer_again(main_loop, focused->urgency_timer);
414 ipc_send_workspace_focus_event(workspace, current);
416 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
417 /* Close old workspace if necessary. This must be done *after* doing
418 * urgency handling, because tree_close() will do a con_focus() on the next
419 * client, which will clear the urgency flag too early. Also, there is no
420 * way for con_focus() to know about when to clear urgency immediately and
421 * when to defer it. */
422 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
423 /* check if this workspace is currently visible */
424 if (!workspace_is_visible(old)) {
425 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
426 tree_close(old, DONT_KILL_WINDOW, false, false);
427 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
431 workspace->fullscreen_mode = CF_OUTPUT;
432 LOG("focused now = %p / %s\n", focused, focused->name);
434 /* Set mouse pointer */
435 Con *new_output = con_get_output(focused);
436 if (old_output != new_output) {
437 x_set_warp_to(&next->rect);
440 /* Update the EWMH hints */
441 ewmh_update_current_desktop();
445 * Switches to the given workspace
448 void workspace_show(Con *workspace) {
449 _workspace_show(workspace);
453 * Looks up the workspace by name and switches to it.
456 void workspace_show_by_name(const char *num) {
458 workspace = workspace_get(num, NULL);
459 _workspace_show(workspace);
463 * Focuses the next workspace.
466 Con* workspace_next(void) {
467 Con *current = con_get_workspace(focused);
471 if (current->num == -1) {
472 /* If currently a named workspace, find next named workspace. */
473 next = TAILQ_NEXT(current, nodes);
475 /* If currently a numbered workspace, find next numbered workspace. */
476 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
477 /* Skip outputs starting with __, they are internal. */
478 if (con_is_internal(output))
480 NODES_FOREACH(output_get_content(output)) {
481 if (child->type != CT_WORKSPACE)
483 if (child->num == -1)
485 /* Need to check child against current and next because we are
486 * traversing multiple lists and thus are not guaranteed the
487 * relative order between the list of workspaces. */
488 if (current->num < child->num && (!next || child->num < next->num))
494 /* Find next named workspace. */
496 bool found_current = false;
497 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
498 /* Skip outputs starting with __, they are internal. */
499 if (con_is_internal(output))
501 NODES_FOREACH(output_get_content(output)) {
502 if (child->type != CT_WORKSPACE)
504 if (child == current) {
506 } else if (child->num == -1 && (current->num != -1 || found_current)) {
508 goto workspace_next_end;
514 /* Find first workspace. */
516 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
517 /* Skip outputs starting with __, they are internal. */
518 if (con_is_internal(output))
520 NODES_FOREACH(output_get_content(output)) {
521 if (child->type != CT_WORKSPACE)
523 if (!next || (child->num != -1 && child->num < next->num))
533 * Focuses the previous workspace.
536 Con* workspace_prev(void) {
537 Con *current = con_get_workspace(focused);
541 if (current->num == -1) {
542 /* If named workspace, find previous named workspace. */
543 prev = TAILQ_PREV(current, nodes_head, nodes);
544 if (prev && prev->num != -1)
547 /* If numbered workspace, find previous numbered workspace. */
548 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
549 /* Skip outputs starting with __, they are internal. */
550 if (con_is_internal(output))
552 NODES_FOREACH_REVERSE(output_get_content(output)) {
553 if (child->type != CT_WORKSPACE || child->num == -1)
555 /* Need to check child against current and previous because we
556 * are traversing multiple lists and thus are not guaranteed
557 * the relative order between the list of workspaces. */
558 if (current->num > child->num && (!prev || child->num > prev->num))
564 /* Find previous named workspace. */
566 bool found_current = false;
567 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
568 /* Skip outputs starting with __, they are internal. */
569 if (con_is_internal(output))
571 NODES_FOREACH_REVERSE(output_get_content(output)) {
572 if (child->type != CT_WORKSPACE)
574 if (child == current) {
575 found_current = true;
576 } else if (child->num == -1 && (current->num != -1 || found_current)) {
578 goto workspace_prev_end;
584 /* Find last workspace. */
586 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
587 /* Skip outputs starting with __, they are internal. */
588 if (con_is_internal(output))
590 NODES_FOREACH_REVERSE(output_get_content(output)) {
591 if (child->type != CT_WORKSPACE)
593 if (!prev || child->num > prev->num)
605 * Focuses the next workspace on the same output.
608 Con* workspace_next_on_output(void) {
609 Con *current = con_get_workspace(focused);
611 Con *output = con_get_output(focused);
613 if (current->num == -1) {
614 /* If currently a named workspace, find next named workspace. */
615 next = TAILQ_NEXT(current, nodes);
617 /* If currently a numbered workspace, find next numbered workspace. */
618 NODES_FOREACH(output_get_content(output)) {
619 if (child->type != CT_WORKSPACE)
621 if (child->num == -1)
623 /* Need to check child against current and next because we are
624 * traversing multiple lists and thus are not guaranteed the
625 * relative order between the list of workspaces. */
626 if (current->num < child->num && (!next || child->num < next->num))
631 /* Find next named workspace. */
633 bool found_current = false;
634 NODES_FOREACH(output_get_content(output)) {
635 if (child->type != CT_WORKSPACE)
637 if (child == current) {
639 } else if (child->num == -1 && (current->num != -1 || found_current)) {
641 goto workspace_next_on_output_end;
646 /* Find first workspace. */
648 NODES_FOREACH(output_get_content(output)) {
649 if (child->type != CT_WORKSPACE)
651 if (!next || (child->num != -1 && child->num < next->num))
655 workspace_next_on_output_end:
660 * Focuses the previous workspace on same output.
663 Con* workspace_prev_on_output(void) {
664 Con *current = con_get_workspace(focused);
666 Con *output = con_get_output(focused);
667 DLOG("output = %s\n", output->name);
669 if (current->num == -1) {
670 /* If named workspace, find previous named workspace. */
671 prev = TAILQ_PREV(current, nodes_head, nodes);
672 if (prev && prev->num != -1)
675 /* If numbered workspace, find previous numbered workspace. */
676 NODES_FOREACH_REVERSE(output_get_content(output)) {
677 if (child->type != CT_WORKSPACE || child->num == -1)
679 /* Need to check child against current and previous because we
680 * are traversing multiple lists and thus are not guaranteed
681 * the relative order between the list of workspaces. */
682 if (current->num > child->num && (!prev || child->num > prev->num))
687 /* Find previous named workspace. */
689 bool found_current = false;
690 NODES_FOREACH_REVERSE(output_get_content(output)) {
691 if (child->type != CT_WORKSPACE)
693 if (child == current) {
694 found_current = true;
695 } else if (child->num == -1 && (current->num != -1 || found_current)) {
697 goto workspace_prev_on_output_end;
702 /* Find last workspace. */
704 NODES_FOREACH_REVERSE(output_get_content(output)) {
705 if (child->type != CT_WORKSPACE)
707 if (!prev || child->num > prev->num)
712 workspace_prev_on_output_end:
717 * Focuses the previously focused workspace.
720 void workspace_back_and_forth(void) {
721 if (!previous_workspace_name) {
722 DLOG("No previous workspace name set. Not switching.");
726 workspace_show_by_name(previous_workspace_name);
730 * Returns the previously focused workspace con, or NULL if unavailable.
733 Con *workspace_back_and_forth_get(void) {
734 if (!previous_workspace_name) {
735 DLOG("no previous workspace name set.");
740 workspace = workspace_get(previous_workspace_name, NULL);
745 static bool get_urgency_flag(Con *con) {
747 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
748 if (child->urgent || get_urgency_flag(child))
751 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
752 if (child->urgent || get_urgency_flag(child))
759 * Goes through all clients on the given workspace and updates the workspace’s
760 * urgent flag accordingly.
763 void workspace_update_urgent_flag(Con *ws) {
764 bool old_flag = ws->urgent;
765 ws->urgent = get_urgency_flag(ws);
766 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
768 if (old_flag != ws->urgent)
769 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
773 * 'Forces' workspace orientation by moving all cons into a new split-con with
774 * the same layout as the workspace and then changing the workspace layout.
777 void ws_force_orientation(Con *ws, orientation_t orientation) {
778 /* 1: create a new split container */
779 Con *split = con_new(NULL, NULL);
782 /* 2: copy layout from workspace */
783 split->layout = ws->layout;
785 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
787 /* 3: move the existing cons of this workspace below the new con */
788 DLOG("Moving cons\n");
789 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
790 Con *child = TAILQ_FIRST(&(ws->nodes_head));
792 con_attach(child, split, true);
795 /* 4: switch workspace layout */
796 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
797 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
799 /* 5: attach the new split container to the workspace */
800 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
801 con_attach(split, ws, false);
803 /* 6: fix the percentages */
807 con_focus(old_focused);
811 * Called when a new con (with a window, not an empty or split con) should be
812 * attached to the workspace (for example when managing a new window or when
813 * moving an existing window to the workspace level).
815 * Depending on the workspace_layout setting, this function either returns the
816 * workspace itself (default layout) or creates a new stacked/tabbed con and
820 Con *workspace_attach_to(Con *ws) {
821 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
823 if (ws->workspace_layout == L_DEFAULT) {
824 DLOG("Default layout, just attaching it to the workspace itself.\n");
828 DLOG("Non-default layout, creating a new split container\n");
829 /* 1: create a new split container */
830 Con *new = con_new(NULL, NULL);
833 /* 2: set the requested layout on the split con */
834 new->layout = ws->workspace_layout;
836 /* 4: attach the new split container to the workspace */
837 DLOG("Attaching new split %p to workspace %p\n", new, ws);
838 con_attach(new, ws, false);
844 * Creates a new container and re-parents all of children from the given
847 * The container inherits the layout from the workspace.
849 Con *workspace_encapsulate(Con *ws) {
850 if (TAILQ_EMPTY(&(ws->nodes_head))) {
851 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
855 Con *new = con_new(NULL, NULL);
857 new->layout = ws->layout;
859 DLOG("Moving children of workspace %p / %s into container %p\n",
863 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
864 child = TAILQ_FIRST(&(ws->nodes_head));
866 con_attach(child, new, true);
869 con_attach(new, ws, true);