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
14 #include "yajl_utils.h"
16 #include <yajl/yajl_gen.h>
18 /* Stores a copy of the name of the last used workspace for the workspace
19 * back-and-forth switching. */
20 static char *previous_workspace_name = NULL;
23 * Sets ws->layout to splith/splitv if default_orientation was specified in the
24 * configfile. Otherwise, it uses splith/splitv depending on whether the output
25 * is higher than wide.
28 static void _workspace_apply_default_orientation(Con *ws) {
29 /* If default_orientation is set to NO_ORIENTATION we determine
30 * orientation depending on output resolution. */
31 if (config.default_orientation == NO_ORIENTATION) {
32 Con *output = con_get_output(ws);
33 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
34 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
35 output->rect.width, output->rect.height, ws->layout);
37 ws->layout = (config.default_orientation == HORIZ) ? L_SPLITH : L_SPLITV;
42 * Returns a pointer to the workspace with the given number (starting at 0),
43 * creating the workspace if necessary (by allocating the necessary amount of
44 * memory and initializing the data structures correctly).
47 Con *workspace_get(const char *num, bool *created) {
48 Con *output, *workspace = NULL;
50 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
51 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
53 if (workspace == NULL) {
54 LOG("Creating new workspace \"%s\"\n", num);
55 /* unless an assignment is found, we will create this workspace on the current output */
56 output = con_get_output(focused);
57 /* look for assignments */
58 struct Workspace_Assignment *assignment;
59 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
60 if (strcmp(assignment->name, num) != 0)
63 LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
64 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
67 Con *content = output_get_content(output);
68 LOG("got output %p with content %p\n", output, content);
69 /* We need to attach this container after setting its type. con_attach
70 * will handle CT_WORKSPACEs differently */
71 workspace = con_new(NULL, NULL);
73 sasprintf(&name, "[i3 con] workspace %s", num);
74 x_set_name(workspace, name);
76 workspace->type = CT_WORKSPACE;
77 FREE(workspace->name);
78 workspace->name = sstrdup(num);
79 workspace->workspace_layout = config.default_layout;
80 /* We set ->num to the number if this workspace’s name begins with a
81 * positive number. Otherwise it’s a named ws and num will be -1. */
83 long parsed_num = strtol(num, &endptr, 10);
84 if (parsed_num == LONG_MIN ||
85 parsed_num == LONG_MAX ||
89 else workspace->num = parsed_num;
90 LOG("num = %d\n", workspace->num);
92 workspace->parent = content;
93 _workspace_apply_default_orientation(workspace);
95 con_attach(workspace, content, false);
97 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
101 else if (created != NULL) {
109 * Returns a pointer to a new workspace in the given output. The workspace
110 * is created attached to the tree hierarchy through the given content
114 Con *create_workspace_on_output(Output *output, Con *content) {
115 /* add a workspace to this output */
119 Con *ws = con_new(NULL, NULL);
120 ws->type = CT_WORKSPACE;
122 /* try the configured workspace bindings first to find a free name */
124 TAILQ_FOREACH(bind, bindings, bindings) {
125 DLOG("binding with command %s\n", bind->command);
126 if (strlen(bind->command) < strlen("workspace ") ||
127 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
129 DLOG("relevant command = %s\n", bind->command);
130 char *target = bind->command + strlen("workspace ");
131 /* We check if this is the workspace
132 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
133 * Beware: The workspace names "next", "prev", "next_on_output",
134 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
135 * so we check before stripping the double quotes */
136 if (strncasecmp(target, "next", strlen("next")) == 0 ||
137 strncasecmp(target, "prev", strlen("prev")) == 0 ||
138 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
139 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
140 strncasecmp(target, "number", strlen("number")) == 0 ||
141 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
142 strncasecmp(target, "current", strlen("current")) == 0)
147 ws->name = strdup(target);
148 if (ws->name[strlen(ws->name)-1] == '"')
149 ws->name[strlen(ws->name)-1] = '\0';
150 DLOG("trying name *%s*\n", ws->name);
152 /* Ensure that this workspace is not assigned to a different output —
153 * otherwise we would create it, then move it over to its output, then
154 * find a new workspace, etc… */
155 bool assigned = false;
156 struct Workspace_Assignment *assignment;
157 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
158 if (strcmp(assignment->name, ws->name) != 0 ||
159 strcmp(assignment->output, output->name) == 0)
170 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
171 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
173 exists = (current != NULL);
175 /* Set ->num to the number of the workspace, if the name actually
176 * is a number or starts with a number */
178 long parsed_num = strtol(ws->name, &endptr, 10);
179 if (parsed_num == LONG_MIN ||
180 parsed_num == LONG_MAX ||
184 else ws->num = parsed_num;
185 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
192 /* get the next unused workspace number */
193 DLOG("Getting next unused workspace by number\n");
199 sasprintf(&(ws->name), "%d", c);
202 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
203 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
204 exists = (current != NULL);
206 DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
210 con_attach(ws, content, false);
212 sasprintf(&name, "[i3 con] workspace %s", ws->name);
213 x_set_name(ws, name);
216 ws->fullscreen_mode = CF_OUTPUT;
218 ws->workspace_layout = config.default_layout;
219 _workspace_apply_default_orientation(ws);
226 * Returns true if the workspace is currently visible. Especially important for
227 * multi-monitor environments, as they can have multiple currenlty active
231 bool workspace_is_visible(Con *ws) {
232 Con *output = con_get_output(ws);
235 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
236 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
241 * XXX: we need to clean up all this recursive walking code.
244 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
247 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
248 if (current != exclude &&
249 current->sticky_group != NULL &&
250 current->window != NULL &&
251 strcmp(current->sticky_group, sticky_group) == 0)
254 Con *recurse = _get_sticky(current, sticky_group, exclude);
259 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
260 if (current != exclude &&
261 current->sticky_group != NULL &&
262 current->window != NULL &&
263 strcmp(current->sticky_group, sticky_group) == 0)
266 Con *recurse = _get_sticky(current, sticky_group, exclude);
275 * Reassigns all child windows in sticky containers. Called when the user
276 * changes workspaces.
278 * XXX: what about sticky containers which contain containers?
281 static void workspace_reassign_sticky(Con *con) {
283 /* 1: go through all containers */
285 /* handle all children and floating windows of this node */
286 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
287 if (current->sticky_group == NULL) {
288 workspace_reassign_sticky(current);
292 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
293 /* 2: find a window which we can re-assign */
294 Con *output = con_get_output(current);
295 Con *src = _get_sticky(output, current->sticky_group, current);
298 LOG("No window found for this sticky group\n");
299 workspace_reassign_sticky(current);
303 x_move_win(src, current);
304 current->window = src->window;
305 current->mapped = true;
309 x_reparent_child(current, src);
311 LOG("re-assigned window from src %p to dest %p\n", src, current);
314 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
315 workspace_reassign_sticky(current);
319 * Callback to reset the urgent flag of the given con to false. May be started by
320 * _workspace_show to avoid urgency hints being lost by switching to a workspace
324 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
327 DLOG("Resetting urgency flag of con %p by timer\n", con);
329 con_update_parents_urgency(con);
330 workspace_update_urgent_flag(con_get_workspace(con));
333 ev_timer_stop(main_loop, con->urgency_timer);
334 FREE(con->urgency_timer);
338 * For the "focus" event we send, along the usual "change" field, also the
339 * current and previous workspace, in "current" and "old" respectively.
341 static void _workspace_focus_event(Con *current, Con *old) {
342 yajl_gen gen = ygenalloc();
350 dump_node(gen, current, false);
356 dump_node(gen, old, false);
360 const unsigned char *payload;
362 y(get_buf, &payload, &length);
364 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
368 static void _workspace_show(Con *workspace) {
369 Con *current, *old = NULL;
371 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
372 if (con_is_internal(workspace))
375 /* disable fullscreen for the other workspaces and get the workspace we are
377 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
378 if (current->fullscreen_mode == CF_OUTPUT)
380 current->fullscreen_mode = CF_NONE;
383 /* enable fullscreen for the target workspace. If it happens to be the
384 * same one we are currently on anyways, we can stop here. */
385 workspace->fullscreen_mode = CF_OUTPUT;
386 current = con_get_workspace(focused);
387 if (workspace == current) {
388 DLOG("Not switching, already there.\n");
392 /* Remember currently focused workspace for switching back to it later with
393 * the 'workspace back_and_forth' command.
394 * NOTE: We have to duplicate the name as the original will be freed when
395 * the corresponding workspace is cleaned up.
396 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
397 * focused) are skipped, see bug #868. */
398 if (current && !con_is_internal(current)) {
399 FREE(previous_workspace_name);
401 previous_workspace_name = sstrdup(current->name);
402 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
406 workspace_reassign_sticky(workspace);
408 DLOG("switching to %p / %s\n", workspace, workspace->name);
409 Con *next = con_descend_focused(workspace);
411 /* Memorize current output */
412 Con *old_output = con_get_output(focused);
414 /* Display urgency hint for a while if the newly visible workspace would
415 * focus and thereby immediately destroy it */
416 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
420 /* … but immediately reset urgency flags; they will be set to false by
421 * the timer callback in case the container is focused at the time of
423 focused->urgent = true;
424 workspace->urgent = true;
426 if (focused->urgency_timer == NULL) {
427 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
429 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
430 /* use a repeating timer to allow for easy resets */
431 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
432 config.workspace_urgency_timer, config.workspace_urgency_timer);
433 focused->urgency_timer->data = focused;
434 ev_timer_start(main_loop, focused->urgency_timer);
436 DLOG("Resetting urgency timer of con %p on workspace %p\n",
438 ev_timer_again(main_loop, focused->urgency_timer);
443 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
444 /* Close old workspace if necessary. This must be done *after* doing
445 * urgency handling, because tree_close() will do a con_focus() on the next
446 * client, which will clear the urgency flag too early. Also, there is no
447 * way for con_focus() to know about when to clear urgency immediately and
448 * when to defer it. */
449 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
450 /* check if this workspace is currently visible */
451 if (!workspace_is_visible(old)) {
452 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
453 tree_close(old, DONT_KILL_WINDOW, false, false);
454 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
458 workspace->fullscreen_mode = CF_OUTPUT;
459 LOG("focused now = %p / %s\n", focused, focused->name);
461 /* Set mouse pointer */
462 Con *new_output = con_get_output(focused);
463 if (old_output != new_output) {
464 x_set_warp_to(&next->rect);
467 /* Update the EWMH hints */
468 ewmh_update_current_desktop();
470 _workspace_focus_event(workspace, current);
474 * Switches to the given workspace
477 void workspace_show(Con *workspace) {
478 _workspace_show(workspace);
482 * Looks up the workspace by name and switches to it.
485 void workspace_show_by_name(const char *num) {
487 workspace = workspace_get(num, NULL);
488 _workspace_show(workspace);
492 * Focuses the next workspace.
495 Con* workspace_next(void) {
496 Con *current = con_get_workspace(focused);
500 if (current->num == -1) {
501 /* If currently a named workspace, find next named workspace. */
502 next = TAILQ_NEXT(current, nodes);
504 /* If currently a numbered workspace, find next numbered workspace. */
505 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
506 /* Skip outputs starting with __, they are internal. */
507 if (con_is_internal(output))
509 NODES_FOREACH(output_get_content(output)) {
510 if (child->type != CT_WORKSPACE)
512 if (child->num == -1)
514 /* Need to check child against current and next because we are
515 * traversing multiple lists and thus are not guaranteed the
516 * relative order between the list of workspaces. */
517 if (current->num < child->num && (!next || child->num < next->num))
523 /* Find next named workspace. */
525 bool found_current = false;
526 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
527 /* Skip outputs starting with __, they are internal. */
528 if (con_is_internal(output))
530 NODES_FOREACH(output_get_content(output)) {
531 if (child->type != CT_WORKSPACE)
533 if (child == current) {
535 } else if (child->num == -1 && (current->num != -1 || found_current)) {
537 goto workspace_next_end;
543 /* Find first workspace. */
545 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
546 /* Skip outputs starting with __, they are internal. */
547 if (con_is_internal(output))
549 NODES_FOREACH(output_get_content(output)) {
550 if (child->type != CT_WORKSPACE)
552 if (!next || (child->num != -1 && child->num < next->num))
562 * Focuses the previous workspace.
565 Con* workspace_prev(void) {
566 Con *current = con_get_workspace(focused);
570 if (current->num == -1) {
571 /* If named workspace, find previous named workspace. */
572 prev = TAILQ_PREV(current, nodes_head, nodes);
573 if (prev && prev->num != -1)
576 /* If numbered workspace, find previous numbered workspace. */
577 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
578 /* Skip outputs starting with __, they are internal. */
579 if (con_is_internal(output))
581 NODES_FOREACH_REVERSE(output_get_content(output)) {
582 if (child->type != CT_WORKSPACE || child->num == -1)
584 /* Need to check child against current and previous because we
585 * are traversing multiple lists and thus are not guaranteed
586 * the relative order between the list of workspaces. */
587 if (current->num > child->num && (!prev || child->num > prev->num))
593 /* Find previous named workspace. */
595 bool found_current = false;
596 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
597 /* Skip outputs starting with __, they are internal. */
598 if (con_is_internal(output))
600 NODES_FOREACH_REVERSE(output_get_content(output)) {
601 if (child->type != CT_WORKSPACE)
603 if (child == current) {
604 found_current = true;
605 } else if (child->num == -1 && (current->num != -1 || found_current)) {
607 goto workspace_prev_end;
613 /* Find last workspace. */
615 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
616 /* Skip outputs starting with __, they are internal. */
617 if (con_is_internal(output))
619 NODES_FOREACH_REVERSE(output_get_content(output)) {
620 if (child->type != CT_WORKSPACE)
622 if (!prev || child->num > prev->num)
634 * Focuses the next workspace on the same output.
637 Con* workspace_next_on_output(void) {
638 Con *current = con_get_workspace(focused);
640 Con *output = con_get_output(focused);
642 if (current->num == -1) {
643 /* If currently a named workspace, find next named workspace. */
644 next = TAILQ_NEXT(current, nodes);
646 /* If currently a numbered workspace, find next numbered workspace. */
647 NODES_FOREACH(output_get_content(output)) {
648 if (child->type != CT_WORKSPACE)
650 if (child->num == -1)
652 /* Need to check child against current and next because we are
653 * traversing multiple lists and thus are not guaranteed the
654 * relative order between the list of workspaces. */
655 if (current->num < child->num && (!next || child->num < next->num))
660 /* Find next named workspace. */
662 bool found_current = false;
663 NODES_FOREACH(output_get_content(output)) {
664 if (child->type != CT_WORKSPACE)
666 if (child == current) {
668 } else if (child->num == -1 && (current->num != -1 || found_current)) {
670 goto workspace_next_on_output_end;
675 /* Find first workspace. */
677 NODES_FOREACH(output_get_content(output)) {
678 if (child->type != CT_WORKSPACE)
680 if (!next || (child->num != -1 && child->num < next->num))
684 workspace_next_on_output_end:
689 * Focuses the previous workspace on same output.
692 Con* workspace_prev_on_output(void) {
693 Con *current = con_get_workspace(focused);
695 Con *output = con_get_output(focused);
696 DLOG("output = %s\n", output->name);
698 if (current->num == -1) {
699 /* If named workspace, find previous named workspace. */
700 prev = TAILQ_PREV(current, nodes_head, nodes);
701 if (prev && prev->num != -1)
704 /* If numbered workspace, find previous numbered workspace. */
705 NODES_FOREACH_REVERSE(output_get_content(output)) {
706 if (child->type != CT_WORKSPACE || child->num == -1)
708 /* Need to check child against current and previous because we
709 * are traversing multiple lists and thus are not guaranteed
710 * the relative order between the list of workspaces. */
711 if (current->num > child->num && (!prev || child->num > prev->num))
716 /* Find previous named workspace. */
718 bool found_current = false;
719 NODES_FOREACH_REVERSE(output_get_content(output)) {
720 if (child->type != CT_WORKSPACE)
722 if (child == current) {
723 found_current = true;
724 } else if (child->num == -1 && (current->num != -1 || found_current)) {
726 goto workspace_prev_on_output_end;
731 /* Find last workspace. */
733 NODES_FOREACH_REVERSE(output_get_content(output)) {
734 if (child->type != CT_WORKSPACE)
736 if (!prev || child->num > prev->num)
741 workspace_prev_on_output_end:
746 * Focuses the previously focused workspace.
749 void workspace_back_and_forth(void) {
750 if (!previous_workspace_name) {
751 DLOG("No previous workspace name set. Not switching.");
755 workspace_show_by_name(previous_workspace_name);
759 * Returns the previously focused workspace con, or NULL if unavailable.
762 Con *workspace_back_and_forth_get(void) {
763 if (!previous_workspace_name) {
764 DLOG("no previous workspace name set.");
769 workspace = workspace_get(previous_workspace_name, NULL);
774 static bool get_urgency_flag(Con *con) {
776 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
777 if (child->urgent || get_urgency_flag(child))
780 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
781 if (child->urgent || get_urgency_flag(child))
788 * Goes through all clients on the given workspace and updates the workspace’s
789 * urgent flag accordingly.
792 void workspace_update_urgent_flag(Con *ws) {
793 bool old_flag = ws->urgent;
794 ws->urgent = get_urgency_flag(ws);
795 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
797 if (old_flag != ws->urgent)
798 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
802 * 'Forces' workspace orientation by moving all cons into a new split-con with
803 * the same layout as the workspace and then changing the workspace layout.
806 void ws_force_orientation(Con *ws, orientation_t orientation) {
807 /* 1: create a new split container */
808 Con *split = con_new(NULL, NULL);
811 /* 2: copy layout from workspace */
812 split->layout = ws->layout;
814 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
816 /* 3: move the existing cons of this workspace below the new con */
817 DLOG("Moving cons\n");
818 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
819 Con *child = TAILQ_FIRST(&(ws->nodes_head));
821 con_attach(child, split, true);
824 /* 4: switch workspace layout */
825 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
826 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
828 /* 5: attach the new split container to the workspace */
829 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
830 con_attach(split, ws, false);
832 /* 6: fix the percentages */
836 con_focus(old_focused);
840 * Called when a new con (with a window, not an empty or split con) should be
841 * attached to the workspace (for example when managing a new window or when
842 * moving an existing window to the workspace level).
844 * Depending on the workspace_layout setting, this function either returns the
845 * workspace itself (default layout) or creates a new stacked/tabbed con and
849 Con *workspace_attach_to(Con *ws) {
850 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
852 if (ws->workspace_layout == L_DEFAULT) {
853 DLOG("Default layout, just attaching it to the workspace itself.\n");
857 DLOG("Non-default layout, creating a new split container\n");
858 /* 1: create a new split container */
859 Con *new = con_new(NULL, NULL);
862 /* 2: set the requested layout on the split con */
863 new->layout = ws->workspace_layout;
865 /* 4: attach the new split container to the workspace */
866 DLOG("Attaching new split %p to workspace %p\n", new, ws);
867 con_attach(new, ws, false);
873 * Creates a new container and re-parents all of children from the given
876 * The container inherits the layout from the workspace.
878 Con *workspace_encapsulate(Con *ws) {
879 if (TAILQ_EMPTY(&(ws->nodes_head))) {
880 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
884 Con *new = con_new(NULL, NULL);
886 new->layout = ws->layout;
888 DLOG("Moving children of workspace %p / %s into container %p\n",
892 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
893 child = TAILQ_FIRST(&(ws->nodes_head));
895 con_attach(child, new, true);
898 con_attach(new, ws, true);