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 _workspace_apply_default_orientation(ws);
222 * Returns true if the workspace is currently visible. Especially important for
223 * multi-monitor environments, as they can have multiple currenlty active
227 bool workspace_is_visible(Con *ws) {
228 Con *output = con_get_output(ws);
231 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
232 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
237 * XXX: we need to clean up all this recursive walking code.
240 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
243 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
244 if (current != exclude &&
245 current->sticky_group != NULL &&
246 current->window != NULL &&
247 strcmp(current->sticky_group, sticky_group) == 0)
250 Con *recurse = _get_sticky(current, sticky_group, exclude);
255 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
256 if (current != exclude &&
257 current->sticky_group != NULL &&
258 current->window != NULL &&
259 strcmp(current->sticky_group, sticky_group) == 0)
262 Con *recurse = _get_sticky(current, sticky_group, exclude);
271 * Reassigns all child windows in sticky containers. Called when the user
272 * changes workspaces.
274 * XXX: what about sticky containers which contain containers?
277 static void workspace_reassign_sticky(Con *con) {
279 /* 1: go through all containers */
281 /* handle all children and floating windows of this node */
282 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
283 if (current->sticky_group == NULL) {
284 workspace_reassign_sticky(current);
288 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
289 /* 2: find a window which we can re-assign */
290 Con *output = con_get_output(current);
291 Con *src = _get_sticky(output, current->sticky_group, current);
294 LOG("No window found for this sticky group\n");
295 workspace_reassign_sticky(current);
299 x_move_win(src, current);
300 current->window = src->window;
301 current->mapped = true;
305 x_reparent_child(current, src);
307 LOG("re-assigned window from src %p to dest %p\n", src, current);
310 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
311 workspace_reassign_sticky(current);
315 * Callback to reset the urgent flag of the given con to false. May be started by
316 * _workspace_show to avoid urgency hints being lost by switching to a workspace
320 static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
323 DLOG("Resetting urgency flag of con %p by timer\n", con);
325 con_update_parents_urgency(con);
326 workspace_update_urgent_flag(con_get_workspace(con));
329 ev_timer_stop(main_loop, con->urgency_timer);
330 FREE(con->urgency_timer);
333 static void _workspace_show(Con *workspace) {
334 Con *current, *old = NULL;
336 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
337 if (con_is_internal(workspace))
340 /* disable fullscreen for the other workspaces and get the workspace we are
342 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
343 if (current->fullscreen_mode == CF_OUTPUT)
345 current->fullscreen_mode = CF_NONE;
348 /* enable fullscreen for the target workspace. If it happens to be the
349 * same one we are currently on anyways, we can stop here. */
350 workspace->fullscreen_mode = CF_OUTPUT;
351 current = con_get_workspace(focused);
352 if (workspace == current) {
353 DLOG("Not switching, already there.\n");
357 /* Remember currently focused workspace for switching back to it later with
358 * the 'workspace back_and_forth' command.
359 * NOTE: We have to duplicate the name as the original will be freed when
360 * the corresponding workspace is cleaned up. */
362 FREE(previous_workspace_name);
364 previous_workspace_name = sstrdup(current->name);
366 workspace_reassign_sticky(workspace);
368 DLOG("switching to %p / %s\n", workspace, workspace->name);
369 Con *next = con_descend_focused(workspace);
371 /* Memorize current output */
372 Con *old_output = con_get_output(focused);
374 /* Display urgency hint for a while if the newly visible workspace would
375 * focus and thereby immediately destroy it */
376 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
380 /* … but immediately reset urgency flags; they will be set to false by
381 * the timer callback in case the container is focused at the time of
383 focused->urgent = true;
384 workspace->urgent = true;
386 if (focused->urgency_timer == NULL) {
387 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
389 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
390 /* use a repeating timer to allow for easy resets */
391 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
392 config.workspace_urgency_timer, config.workspace_urgency_timer);
393 focused->urgency_timer->data = focused;
394 ev_timer_start(main_loop, focused->urgency_timer);
396 DLOG("Resetting urgency timer of con %p on workspace %p\n",
398 ev_timer_again(main_loop, focused->urgency_timer);
403 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
404 /* Close old workspace if necessary. This must be done *after* doing
405 * urgency handling, because tree_close() will do a con_focus() on the next
406 * client, which will clear the urgency flag too early. Also, there is no
407 * way for con_focus() to know about when to clear urgency immediately and
408 * when to defer it. */
409 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
410 /* check if this workspace is currently visible */
411 if (!workspace_is_visible(old)) {
412 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
413 tree_close(old, DONT_KILL_WINDOW, false, false);
414 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
418 workspace->fullscreen_mode = CF_OUTPUT;
419 LOG("focused now = %p / %s\n", focused, focused->name);
421 /* Set mouse pointer */
422 Con *new_output = con_get_output(focused);
423 if (old_output != new_output) {
424 x_set_warp_to(&next->rect);
427 /* Update the EWMH hints */
428 ewmh_update_current_desktop();
430 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
434 * Switches to the given workspace
437 void workspace_show(Con *workspace) {
438 _workspace_show(workspace);
442 * Looks up the workspace by name and switches to it.
445 void workspace_show_by_name(const char *num) {
447 workspace = workspace_get(num, NULL);
448 _workspace_show(workspace);
452 * Focuses the next workspace.
455 Con* workspace_next(void) {
456 Con *current = con_get_workspace(focused);
460 if (current->num == -1) {
461 /* If currently a named workspace, find next named workspace. */
462 next = TAILQ_NEXT(current, nodes);
464 /* If currently a numbered workspace, find next numbered workspace. */
465 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
466 /* Skip outputs starting with __, they are internal. */
467 if (con_is_internal(output))
469 NODES_FOREACH(output_get_content(output)) {
470 if (child->type != CT_WORKSPACE)
472 if (child->num == -1)
474 /* Need to check child against current and next because we are
475 * traversing multiple lists and thus are not guaranteed the
476 * relative order between the list of workspaces. */
477 if (current->num < child->num && (!next || child->num < next->num))
483 /* Find next named workspace. */
485 bool found_current = false;
486 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
487 /* Skip outputs starting with __, they are internal. */
488 if (con_is_internal(output))
490 NODES_FOREACH(output_get_content(output)) {
491 if (child->type != CT_WORKSPACE)
493 if (child == current) {
495 } else if (child->num == -1 && (current->num != -1 || found_current)) {
497 goto workspace_next_end;
503 /* Find first 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 (!next || (child->num != -1 && child->num < next->num))
522 * Focuses the previous workspace.
525 Con* workspace_prev(void) {
526 Con *current = con_get_workspace(focused);
530 if (current->num == -1) {
531 /* If named workspace, find previous named workspace. */
532 prev = TAILQ_PREV(current, nodes_head, nodes);
533 if (prev && prev->num != -1)
536 /* If numbered workspace, find previous numbered workspace. */
537 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
538 /* Skip outputs starting with __, they are internal. */
539 if (con_is_internal(output))
541 NODES_FOREACH_REVERSE(output_get_content(output)) {
542 if (child->type != CT_WORKSPACE || child->num == -1)
544 /* Need to check child against current and previous because we
545 * are traversing multiple lists and thus are not guaranteed
546 * the relative order between the list of workspaces. */
547 if (current->num > child->num && (!prev || child->num > prev->num))
553 /* Find previous named workspace. */
555 bool found_current = false;
556 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
557 /* Skip outputs starting with __, they are internal. */
558 if (con_is_internal(output))
560 NODES_FOREACH_REVERSE(output_get_content(output)) {
561 if (child->type != CT_WORKSPACE)
563 if (child == current) {
564 found_current = true;
565 } else if (child->num == -1 && (current->num != -1 || found_current)) {
567 goto workspace_prev_end;
573 /* Find last workspace. */
575 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
576 /* Skip outputs starting with __, they are internal. */
577 if (con_is_internal(output))
579 NODES_FOREACH_REVERSE(output_get_content(output)) {
580 if (child->type != CT_WORKSPACE)
582 if (!prev || child->num > prev->num)
594 * Focuses the next workspace on the same output.
597 Con* workspace_next_on_output(void) {
598 Con *current = con_get_workspace(focused);
600 Con *output = con_get_output(focused);
602 if (current->num == -1) {
603 /* If currently a named workspace, find next named workspace. */
604 next = TAILQ_NEXT(current, nodes);
606 /* If currently a numbered workspace, find next numbered workspace. */
607 NODES_FOREACH(output_get_content(output)) {
608 if (child->type != CT_WORKSPACE)
610 if (child->num == -1)
612 /* Need to check child against current and next because we are
613 * traversing multiple lists and thus are not guaranteed the
614 * relative order between the list of workspaces. */
615 if (current->num < child->num && (!next || child->num < next->num))
620 /* Find next named workspace. */
622 bool found_current = false;
623 NODES_FOREACH(output_get_content(output)) {
624 if (child->type != CT_WORKSPACE)
626 if (child == current) {
628 } else if (child->num == -1 && (current->num != -1 || found_current)) {
630 goto workspace_next_on_output_end;
635 /* Find first workspace. */
637 NODES_FOREACH(output_get_content(output)) {
638 if (child->type != CT_WORKSPACE)
640 if (!next || (child->num != -1 && child->num < next->num))
644 workspace_next_on_output_end:
649 * Focuses the previous workspace on same output.
652 Con* workspace_prev_on_output(void) {
653 Con *current = con_get_workspace(focused);
655 Con *output = con_get_output(focused);
656 DLOG("output = %s\n", output->name);
658 if (current->num == -1) {
659 /* If named workspace, find previous named workspace. */
660 prev = TAILQ_PREV(current, nodes_head, nodes);
661 if (prev && prev->num != -1)
664 /* If numbered workspace, find previous numbered workspace. */
665 NODES_FOREACH_REVERSE(output_get_content(output)) {
666 if (child->type != CT_WORKSPACE || child->num == -1)
668 /* Need to check child against current and previous because we
669 * are traversing multiple lists and thus are not guaranteed
670 * the relative order between the list of workspaces. */
671 if (current->num > child->num && (!prev || child->num > prev->num))
676 /* Find previous named workspace. */
678 bool found_current = false;
679 NODES_FOREACH_REVERSE(output_get_content(output)) {
680 if (child->type != CT_WORKSPACE)
682 if (child == current) {
683 found_current = true;
684 } else if (child->num == -1 && (current->num != -1 || found_current)) {
686 goto workspace_prev_on_output_end;
691 /* Find last workspace. */
693 NODES_FOREACH_REVERSE(output_get_content(output)) {
694 if (child->type != CT_WORKSPACE)
696 if (!prev || child->num > prev->num)
701 workspace_prev_on_output_end:
706 * Focuses the previously focused workspace.
709 void workspace_back_and_forth(void) {
710 if (!previous_workspace_name) {
711 DLOG("No previous workspace name set. Not switching.");
715 workspace_show_by_name(previous_workspace_name);
719 * Returns the previously focused workspace con, or NULL if unavailable.
722 Con *workspace_back_and_forth_get(void) {
723 if (!previous_workspace_name) {
724 DLOG("no previous workspace name set.");
729 workspace = workspace_get(previous_workspace_name, NULL);
734 static bool get_urgency_flag(Con *con) {
736 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
737 if (child->urgent || get_urgency_flag(child))
740 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
741 if (child->urgent || get_urgency_flag(child))
748 * Goes through all clients on the given workspace and updates the workspace’s
749 * urgent flag accordingly.
752 void workspace_update_urgent_flag(Con *ws) {
753 bool old_flag = ws->urgent;
754 ws->urgent = get_urgency_flag(ws);
755 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
757 if (old_flag != ws->urgent)
758 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
762 * 'Forces' workspace orientation by moving all cons into a new split-con with
763 * the same layout as the workspace and then changing the workspace layout.
766 void ws_force_orientation(Con *ws, orientation_t orientation) {
767 /* 1: create a new split container */
768 Con *split = con_new(NULL, NULL);
772 /* 2: copy layout from workspace */
773 split->layout = ws->layout;
775 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
777 /* 3: move the existing cons of this workspace below the new con */
778 DLOG("Moving cons\n");
779 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
780 Con *child = TAILQ_FIRST(&(ws->nodes_head));
782 con_attach(child, split, true);
785 /* 4: switch workspace layout */
786 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
787 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
789 /* 5: attach the new split container to the workspace */
790 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
791 con_attach(split, ws, false);
793 /* 6: fix the percentages */
797 con_focus(old_focused);
801 * Called when a new con (with a window, not an empty or split con) should be
802 * attached to the workspace (for example when managing a new window or when
803 * moving an existing window to the workspace level).
805 * Depending on the workspace_layout setting, this function either returns the
806 * workspace itself (default layout) or creates a new stacked/tabbed con and
810 Con *workspace_attach_to(Con *ws) {
811 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
813 if (ws->workspace_layout == L_DEFAULT) {
814 DLOG("Default layout, just attaching it to the workspace itself.\n");
818 DLOG("Non-default layout, creating a new split container\n");
819 /* 1: create a new split container */
820 Con *new = con_new(NULL, NULL);
824 /* 2: set the requested layout on the split con */
825 new->layout = ws->workspace_layout;
827 /* 4: attach the new split container to the workspace */
828 DLOG("Attaching new split %p to workspace %p\n", new, ws);
829 con_attach(new, ws, false);