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 workspace_update_urgent_flag(con_get_workspace(con));
328 ev_timer_stop(main_loop, con->urgency_timer);
329 FREE(con->urgency_timer);
332 static void _workspace_show(Con *workspace) {
333 Con *current, *old = NULL;
335 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
336 if (workspace->name[0] == '_' && workspace->name[1] == '_')
339 /* disable fullscreen for the other workspaces and get the workspace we are
341 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
342 if (current->fullscreen_mode == CF_OUTPUT)
344 current->fullscreen_mode = CF_NONE;
347 /* enable fullscreen for the target workspace. If it happens to be the
348 * same one we are currently on anyways, we can stop here. */
349 workspace->fullscreen_mode = CF_OUTPUT;
350 current = con_get_workspace(focused);
351 if (workspace == current) {
352 DLOG("Not switching, already there.\n");
356 /* Remember currently focused workspace for switching back to it later with
357 * the 'workspace back_and_forth' command.
358 * NOTE: We have to duplicate the name as the original will be freed when
359 * the corresponding workspace is cleaned up. */
361 FREE(previous_workspace_name);
363 previous_workspace_name = sstrdup(current->name);
365 workspace_reassign_sticky(workspace);
367 LOG("switching to %p\n", workspace);
368 Con *next = con_descend_focused(workspace);
370 /* Memorize current output */
371 Con *old_output = con_get_output(focused);
373 /* Display urgency hint for a while if the newly visible workspace would
374 * focus and thereby immediately destroy it */
375 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
379 /* … but immediately reset urgency flags; they will be set to false by
380 * the timer callback in case the container is focused at the time of
382 focused->urgent = true;
383 workspace->urgent = true;
385 if (focused->urgency_timer == NULL) {
386 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
388 focused->urgency_timer = scalloc(sizeof(struct ev_timer));
389 /* use a repeating timer to allow for easy resets */
390 ev_timer_init(focused->urgency_timer, workspace_defer_update_urgent_hint_cb,
391 config.workspace_urgency_timer, config.workspace_urgency_timer);
392 focused->urgency_timer->data = focused;
393 ev_timer_start(main_loop, focused->urgency_timer);
395 DLOG("Resetting urgency timer of con %p on workspace %p\n",
397 ev_timer_again(main_loop, focused->urgency_timer);
402 /* Close old workspace if necessary. This must be done *after* doing
403 * urgency handling, because tree_close() will do a con_focus() on the next
404 * client, which will clear the urgency flag too early. Also, there is no
405 * way for con_focus() to know about when to clear urgency immediately and
406 * when to defer it. */
407 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
408 /* check if this workspace is currently visible */
409 if (!workspace_is_visible(old)) {
410 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
411 tree_close(old, DONT_KILL_WINDOW, false, false);
412 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
416 workspace->fullscreen_mode = CF_OUTPUT;
417 LOG("focused now = %p / %s\n", focused, focused->name);
419 /* Set mouse pointer */
420 Con *new_output = con_get_output(focused);
421 if (old_output != new_output) {
422 x_set_warp_to(&next->rect);
425 /* Update the EWMH hints */
426 ewmh_update_current_desktop();
428 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
432 * Switches to the given workspace
435 void workspace_show(Con *workspace) {
436 _workspace_show(workspace);
440 * Looks up the workspace by name and switches to it.
443 void workspace_show_by_name(const char *num) {
445 workspace = workspace_get(num, NULL);
446 _workspace_show(workspace);
450 * Focuses the next workspace.
453 Con* workspace_next(void) {
454 Con *current = con_get_workspace(focused);
458 if (current->num == -1) {
459 /* If currently a named workspace, find next named workspace. */
460 next = TAILQ_NEXT(current, nodes);
462 /* If currently a numbered workspace, find next numbered workspace. */
463 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
464 /* Skip outputs starting with __, they are internal. */
465 if (output->name[0] == '_' && output->name[1] == '_')
467 NODES_FOREACH(output_get_content(output)) {
468 if (child->type != CT_WORKSPACE)
470 if (child->num == -1)
472 /* Need to check child against current and next because we are
473 * traversing multiple lists and thus are not guaranteed the
474 * relative order between the list of workspaces. */
475 if (current->num < child->num && (!next || child->num < next->num))
481 /* Find next named workspace. */
483 bool found_current = false;
484 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
485 /* Skip outputs starting with __, they are internal. */
486 if (output->name[0] == '_' && output->name[1] == '_')
488 NODES_FOREACH(output_get_content(output)) {
489 if (child->type != CT_WORKSPACE)
491 if (child == current) {
493 } else if (child->num == -1 && (current->num != -1 || found_current)) {
495 goto workspace_next_end;
501 /* Find first workspace. */
503 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
504 /* Skip outputs starting with __, they are internal. */
505 if (output->name[0] == '_' && output->name[1] == '_')
507 NODES_FOREACH(output_get_content(output)) {
508 if (child->type != CT_WORKSPACE)
510 if (!next || (child->num != -1 && child->num < next->num))
520 * Focuses the previous workspace.
523 Con* workspace_prev(void) {
524 Con *current = con_get_workspace(focused);
528 if (current->num == -1) {
529 /* If named workspace, find previous named workspace. */
530 prev = TAILQ_PREV(current, nodes_head, nodes);
531 if (prev && prev->num != -1)
534 /* If numbered workspace, find previous numbered workspace. */
535 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
536 /* Skip outputs starting with __, they are internal. */
537 if (output->name[0] == '_' && output->name[1] == '_')
539 NODES_FOREACH_REVERSE(output_get_content(output)) {
540 if (child->type != CT_WORKSPACE || child->num == -1)
542 /* Need to check child against current and previous because we
543 * are traversing multiple lists and thus are not guaranteed
544 * the relative order between the list of workspaces. */
545 if (current->num > child->num && (!prev || child->num > prev->num))
551 /* Find previous named workspace. */
553 bool found_current = false;
554 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
555 /* Skip outputs starting with __, they are internal. */
556 if (output->name[0] == '_' && output->name[1] == '_')
558 NODES_FOREACH_REVERSE(output_get_content(output)) {
559 if (child->type != CT_WORKSPACE)
561 if (child == current) {
562 found_current = true;
563 } else if (child->num == -1 && (current->num != -1 || found_current)) {
565 goto workspace_prev_end;
571 /* Find last workspace. */
573 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
574 /* Skip outputs starting with __, they are internal. */
575 if (output->name[0] == '_' && output->name[1] == '_')
577 NODES_FOREACH_REVERSE(output_get_content(output)) {
578 if (child->type != CT_WORKSPACE)
580 if (!prev || child->num > prev->num)
592 * Focuses the next workspace on the same output.
595 Con* workspace_next_on_output(void) {
596 Con *current = con_get_workspace(focused);
598 Con *output = con_get_output(focused);
600 if (current->num == -1) {
601 /* If currently a named workspace, find next named workspace. */
602 next = TAILQ_NEXT(current, nodes);
604 /* If currently a numbered workspace, find next numbered workspace. */
605 NODES_FOREACH(output_get_content(output)) {
606 if (child->type != CT_WORKSPACE)
608 if (child->num == -1)
610 /* Need to check child against current and next because we are
611 * traversing multiple lists and thus are not guaranteed the
612 * relative order between the list of workspaces. */
613 if (current->num < child->num && (!next || child->num < next->num))
618 /* Find next named workspace. */
620 bool found_current = false;
621 NODES_FOREACH(output_get_content(output)) {
622 if (child->type != CT_WORKSPACE)
624 if (child == current) {
626 } else if (child->num == -1 && (current->num != -1 || found_current)) {
628 goto workspace_next_on_output_end;
633 /* Find first workspace. */
635 NODES_FOREACH(output_get_content(output)) {
636 if (child->type != CT_WORKSPACE)
638 if (!next || (child->num != -1 && child->num < next->num))
642 workspace_next_on_output_end:
647 * Focuses the previous workspace on same output.
650 Con* workspace_prev_on_output(void) {
651 Con *current = con_get_workspace(focused);
653 Con *output = con_get_output(focused);
654 DLOG("output = %s\n", output->name);
656 if (current->num == -1) {
657 /* If named workspace, find previous named workspace. */
658 prev = TAILQ_PREV(current, nodes_head, nodes);
659 if (prev && prev->num != -1)
662 /* If numbered workspace, find previous numbered workspace. */
663 NODES_FOREACH_REVERSE(output_get_content(output)) {
664 if (child->type != CT_WORKSPACE || child->num == -1)
666 /* Need to check child against current and previous because we
667 * are traversing multiple lists and thus are not guaranteed
668 * the relative order between the list of workspaces. */
669 if (current->num > child->num && (!prev || child->num > prev->num))
674 /* Find previous named workspace. */
676 bool found_current = false;
677 NODES_FOREACH_REVERSE(output_get_content(output)) {
678 if (child->type != CT_WORKSPACE)
680 if (child == current) {
681 found_current = true;
682 } else if (child->num == -1 && (current->num != -1 || found_current)) {
684 goto workspace_prev_on_output_end;
689 /* Find last workspace. */
691 NODES_FOREACH_REVERSE(output_get_content(output)) {
692 if (child->type != CT_WORKSPACE)
694 if (!prev || child->num > prev->num)
699 workspace_prev_on_output_end:
704 * Focuses the previously focused workspace.
707 void workspace_back_and_forth(void) {
708 if (!previous_workspace_name) {
709 DLOG("No previous workspace name set. Not switching.");
713 workspace_show_by_name(previous_workspace_name);
717 * Returns the previously focused workspace con, or NULL if unavailable.
720 Con *workspace_back_and_forth_get(void) {
721 if (!previous_workspace_name) {
722 DLOG("no previous workspace name set.");
727 workspace = workspace_get(previous_workspace_name, NULL);
732 static bool get_urgency_flag(Con *con) {
734 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
735 if (child->urgent || get_urgency_flag(child))
738 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
739 if (child->urgent || get_urgency_flag(child))
746 * Goes through all clients on the given workspace and updates the workspace’s
747 * urgent flag accordingly.
750 void workspace_update_urgent_flag(Con *ws) {
751 bool old_flag = ws->urgent;
752 ws->urgent = get_urgency_flag(ws);
753 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
755 if (old_flag != ws->urgent)
756 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
760 * 'Forces' workspace orientation by moving all cons into a new split-con with
761 * the same layout as the workspace and then changing the workspace layout.
764 void ws_force_orientation(Con *ws, orientation_t orientation) {
765 /* 1: create a new split container */
766 Con *split = con_new(NULL, NULL);
770 /* 2: copy layout from workspace */
771 split->layout = ws->layout;
773 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
775 /* 3: move the existing cons of this workspace below the new con */
776 DLOG("Moving cons\n");
777 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
778 Con *child = TAILQ_FIRST(&(ws->nodes_head));
780 con_attach(child, split, true);
783 /* 4: switch workspace layout */
784 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
785 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
787 /* 5: attach the new split container to the workspace */
788 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
789 con_attach(split, ws, false);
791 /* 6: fix the percentages */
795 con_focus(old_focused);
799 * Called when a new con (with a window, not an empty or split con) should be
800 * attached to the workspace (for example when managing a new window or when
801 * moving an existing window to the workspace level).
803 * Depending on the workspace_layout setting, this function either returns the
804 * workspace itself (default layout) or creates a new stacked/tabbed con and
808 Con *workspace_attach_to(Con *ws) {
809 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
811 if (ws->workspace_layout == L_DEFAULT) {
812 DLOG("Default layout, just attaching it to the workspace itself.\n");
816 DLOG("Non-default layout, creating a new split container\n");
817 /* 1: create a new split container */
818 Con *new = con_new(NULL, NULL);
822 /* 2: set the requested layout on the split con */
823 new->layout = ws->workspace_layout;
825 /* 4: attach the new split container to the workspace */
826 DLOG("Attaching new split %p to workspace %p\n", new, ws);
827 con_attach(new, ws, false);