2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
7 * workspace.c: Modifying workspaces, accessing them, moving containers to
13 /* Stores a copy of the name of the last used workspace for the workspace
14 * back-and-forth switching. */
15 static char *previous_workspace_name = NULL;
18 * Returns a pointer to the workspace with the given number (starting at 0),
19 * creating the workspace if necessary (by allocating the necessary amount of
20 * memory and initializing the data structures correctly).
23 Con *workspace_get(const char *num, bool *created) {
24 Con *output, *workspace = NULL;
26 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
27 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
29 if (workspace == NULL) {
30 LOG("Creating new workspace \"%s\"\n", num);
31 /* unless an assignment is found, we will create this workspace on the current output */
32 output = con_get_output(focused);
33 /* look for assignments */
34 struct Workspace_Assignment *assignment;
35 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
36 if (strcmp(assignment->name, num) != 0)
39 LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
40 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
43 Con *content = output_get_content(output);
44 LOG("got output %p with content %p\n", output, content);
45 /* We need to attach this container after setting its type. con_attach
46 * will handle CT_WORKSPACEs differently */
47 workspace = con_new(NULL, NULL);
49 sasprintf(&name, "[i3 con] workspace %s", num);
50 x_set_name(workspace, name);
52 workspace->type = CT_WORKSPACE;
53 FREE(workspace->name);
54 workspace->name = sstrdup(num);
55 /* We set ->num to the number if this workspace’s name begins with a
56 * positive number. Otherwise it’s a named ws and num will be -1. */
58 long parsed_num = strtol(num, &endptr, 10);
59 if (parsed_num == LONG_MIN ||
60 parsed_num == LONG_MAX ||
64 else workspace->num = parsed_num;
65 LOG("num = %d\n", workspace->num);
67 /* If default_orientation is set to NO_ORIENTATION we
68 * determine workspace orientation from workspace size.
69 * Otherwise we just set the orientation to default_orientation. */
70 if (config.default_orientation == NO_ORIENTATION) {
71 workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
72 DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
73 workspace->rect.width, workspace->rect.height, workspace->orientation);
75 workspace->orientation = config.default_orientation;
78 con_attach(workspace, content, false);
80 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
84 else if (created != NULL) {
92 * Returns a pointer to a new workspace in the given output. The workspace
93 * is created attached to the tree hierarchy through the given content
97 Con *create_workspace_on_output(Output *output, Con *content) {
98 /* add a workspace to this output */
102 Con *ws = con_new(NULL, NULL);
103 ws->type = CT_WORKSPACE;
105 /* try the configured workspace bindings first to find a free name */
107 TAILQ_FOREACH(bind, bindings, bindings) {
108 DLOG("binding with command %s\n", bind->command);
109 if (strlen(bind->command) < strlen("workspace ") ||
110 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
112 DLOG("relevant command = %s\n", bind->command);
113 char *target = bind->command + strlen("workspace ");
114 /* We check if this is the workspace
115 * next/prev/next_on_output/prev_on_output/back_and_forth command.
116 * Beware: The workspace names "next", "prev", "next_on_output",
117 * "prev_on_output" and "back_and_forth" are OK, so we check before
118 * stripping the double quotes */
119 if (strncasecmp(target, "next", strlen("next")) == 0 ||
120 strncasecmp(target, "prev", strlen("prev")) == 0 ||
121 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
122 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
123 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0)
128 ws->name = strdup(target);
129 if (ws->name[strlen(ws->name)-1] == '"')
130 ws->name[strlen(ws->name)-1] = '\0';
131 DLOG("trying name *%s*\n", ws->name);
133 /* Ensure that this workspace is not assigned to a different output —
134 * otherwise we would create it, then move it over to its output, then
135 * find a new workspace, etc… */
136 bool assigned = false;
137 struct Workspace_Assignment *assignment;
138 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
139 if (strcmp(assignment->name, ws->name) != 0 ||
140 strcmp(assignment->output, output->name) == 0)
151 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
152 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
154 exists = (current != NULL);
156 /* Set ->num to the number of the workspace, if the name actually
157 * is a number or starts with a number */
159 long parsed_num = strtol(ws->name, &endptr, 10);
160 if (parsed_num == LONG_MIN ||
161 parsed_num == LONG_MAX ||
165 else ws->num = parsed_num;
166 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
173 /* get the next unused workspace number */
174 DLOG("Getting next unused workspace by number\n");
180 sasprintf(&(ws->name), "%d", c);
183 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
184 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
185 exists = (current != NULL);
187 DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
191 con_attach(ws, content, false);
193 sasprintf(&name, "[i3 con] workspace %s", ws->name);
194 x_set_name(ws, name);
197 ws->fullscreen_mode = CF_OUTPUT;
199 /* If default_orientation is set to NO_ORIENTATION we determine
200 * orientation depending on output resolution. */
201 if (config.default_orientation == NO_ORIENTATION) {
202 ws->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
203 DLOG("Auto orientation. Workspace size set to (%d,%d), setting orientation to %d.\n",
204 output->rect.width, output->rect.height, ws->orientation);
206 ws->orientation = config.default_orientation;
213 * Returns true if the workspace is currently visible. Especially important for
214 * multi-monitor environments, as they can have multiple currenlty active
218 bool workspace_is_visible(Con *ws) {
219 Con *output = con_get_output(ws);
222 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
223 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
228 * XXX: we need to clean up all this recursive walking code.
231 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
234 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
235 if (current != exclude &&
236 current->sticky_group != NULL &&
237 current->window != NULL &&
238 strcmp(current->sticky_group, sticky_group) == 0)
241 Con *recurse = _get_sticky(current, sticky_group, exclude);
246 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
247 if (current != exclude &&
248 current->sticky_group != NULL &&
249 current->window != NULL &&
250 strcmp(current->sticky_group, sticky_group) == 0)
253 Con *recurse = _get_sticky(current, sticky_group, exclude);
262 * Reassigns all child windows in sticky containers. Called when the user
263 * changes workspaces.
265 * XXX: what about sticky containers which contain containers?
268 static void workspace_reassign_sticky(Con *con) {
270 /* 1: go through all containers */
272 /* handle all children and floating windows of this node */
273 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
274 if (current->sticky_group == NULL) {
275 workspace_reassign_sticky(current);
279 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
280 /* 2: find a window which we can re-assign */
281 Con *output = con_get_output(current);
282 Con *src = _get_sticky(output, current->sticky_group, current);
285 LOG("No window found for this sticky group\n");
286 workspace_reassign_sticky(current);
290 x_move_win(src, current);
291 current->window = src->window;
292 current->mapped = true;
296 x_reparent_child(current, src);
298 LOG("re-assigned window from src %p to dest %p\n", src, current);
301 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
302 workspace_reassign_sticky(current);
306 static void _workspace_show(Con *workspace, bool changed_num_workspaces) {
307 Con *current, *old = NULL;
309 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
310 if (workspace->name[0] == '_' && workspace->name[1] == '_')
313 /* disable fullscreen for the other workspaces and get the workspace we are
315 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
316 if (current->fullscreen_mode == CF_OUTPUT)
318 current->fullscreen_mode = CF_NONE;
321 /* enable fullscreen for the target workspace. If it happens to be the
322 * same one we are currently on anyways, we can stop here. */
323 workspace->fullscreen_mode = CF_OUTPUT;
324 current = con_get_workspace(focused);
325 if (workspace == current) {
326 DLOG("Not switching, already there.\n");
330 /* Remember currently focused workspace for switching back to it later with
331 * the 'workspace back_and_forth' command.
332 * NOTE: We have to duplicate the name as the original will be freed when
333 * the corresponding workspace is cleaned up. */
335 FREE(previous_workspace_name);
337 previous_workspace_name = sstrdup(current->name);
339 workspace_reassign_sticky(workspace);
341 LOG("switching to %p\n", workspace);
342 Con *next = con_descend_focused(workspace);
344 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
345 /* check if this workspace is currently visible */
346 if (!workspace_is_visible(old)) {
347 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
348 tree_close(old, DONT_KILL_WINDOW, false, false);
349 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
350 changed_num_workspaces = true;
354 /* Memorize current output */
355 Con *old_output = con_get_output(focused);
358 workspace->fullscreen_mode = CF_OUTPUT;
359 LOG("focused now = %p / %s\n", focused, focused->name);
361 /* Set mouse pointer */
362 Con *new_output = con_get_output(focused);
363 if (old_output != new_output) {
364 x_set_warp_to(&next->rect);
367 /* Update the EWMH hints */
368 ewmh_update_current_desktop();
370 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
374 * Switches to the given workspace
377 void workspace_show(Con *workspace) {
378 _workspace_show(workspace, false);
382 * Looks up the workspace by name and switches to it.
385 void workspace_show_by_name(const char *num) {
387 bool changed_num_workspaces;
388 workspace = workspace_get(num, &changed_num_workspaces);
389 _workspace_show(workspace, changed_num_workspaces);
393 * Focuses the next workspace.
396 Con* workspace_next(void) {
397 Con *current = con_get_workspace(focused);
401 if (current->num == -1) {
402 /* If currently a named workspace, find next named workspace. */
403 next = TAILQ_NEXT(current, nodes);
405 /* If currently a numbered workspace, find next numbered workspace. */
406 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
407 /* Skip outputs starting with __, they are internal. */
408 if (output->name[0] == '_' && output->name[1] == '_')
410 NODES_FOREACH(output_get_content(output)) {
411 if (child->type != CT_WORKSPACE)
413 if (child->num == -1)
415 /* Need to check child against current and next because we are
416 * traversing multiple lists and thus are not guaranteed the
417 * relative order between the list of workspaces. */
418 if (current->num < child->num && (!next || child->num < next->num))
424 /* Find next named workspace. */
426 bool found_current = false;
427 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
428 /* Skip outputs starting with __, they are internal. */
429 if (output->name[0] == '_' && output->name[1] == '_')
431 NODES_FOREACH(output_get_content(output)) {
432 if (child->type != CT_WORKSPACE)
434 if (child == current) {
436 } else if (child->num == -1 && (current->num != -1 || found_current)) {
438 goto workspace_next_end;
444 /* Find first workspace. */
446 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
447 /* Skip outputs starting with __, they are internal. */
448 if (output->name[0] == '_' && output->name[1] == '_')
450 NODES_FOREACH(output_get_content(output)) {
451 if (child->type != CT_WORKSPACE)
453 if (!next || (child->num != -1 && child->num < next->num))
463 * Focuses the previous workspace.
466 Con* workspace_prev(void) {
467 Con *current = con_get_workspace(focused);
471 if (current->num == -1) {
472 /* If named workspace, find previous named workspace. */
473 prev = TAILQ_PREV(current, nodes_head, nodes);
474 if (prev && prev->num != -1)
477 /* If numbered workspace, find previous numbered workspace. */
478 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
479 /* Skip outputs starting with __, they are internal. */
480 if (output->name[0] == '_' && output->name[1] == '_')
482 NODES_FOREACH_REVERSE(output_get_content(output)) {
483 if (child->type != CT_WORKSPACE || child->num == -1)
485 /* Need to check child against current and previous because we
486 * are traversing multiple lists and thus are not guaranteed
487 * the relative order between the list of workspaces. */
488 if (current->num > child->num && (!prev || child->num > prev->num))
494 /* Find previous named workspace. */
496 bool found_current = false;
497 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
498 /* Skip outputs starting with __, they are internal. */
499 if (output->name[0] == '_' && output->name[1] == '_')
501 NODES_FOREACH_REVERSE(output_get_content(output)) {
502 if (child->type != CT_WORKSPACE)
504 if (child == current) {
505 found_current = true;
506 } else if (child->num == -1 && (current->num != -1 || found_current)) {
508 goto workspace_prev_end;
514 /* Find last workspace. */
516 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
517 /* Skip outputs starting with __, they are internal. */
518 if (output->name[0] == '_' && output->name[1] == '_')
520 NODES_FOREACH_REVERSE(output_get_content(output)) {
521 if (child->type != CT_WORKSPACE)
523 if (!prev || child->num > prev->num)
535 * Focuses the next workspace on the same output.
538 Con* workspace_next_on_output(void) {
539 Con *current = con_get_workspace(focused);
541 Con *output = con_get_output(focused);
543 if (current->num == -1) {
544 /* If currently a named workspace, find next named workspace. */
545 next = TAILQ_NEXT(current, nodes);
547 /* If currently a numbered workspace, find next numbered workspace. */
548 NODES_FOREACH(output_get_content(output)) {
549 if (child->type != CT_WORKSPACE)
551 if (child->num == -1)
553 /* Need to check child against current and next because we are
554 * traversing multiple lists and thus are not guaranteed the
555 * relative order between the list of workspaces. */
556 if (current->num < child->num && (!next || child->num < next->num))
561 /* Find next named workspace. */
563 bool found_current = false;
564 NODES_FOREACH(output_get_content(output)) {
565 if (child->type != CT_WORKSPACE)
567 if (child == current) {
569 } else if (child->num == -1 && (current->num != -1 || found_current)) {
571 goto workspace_next_on_output_end;
576 /* Find first workspace. */
578 NODES_FOREACH(output_get_content(output)) {
579 if (child->type != CT_WORKSPACE)
581 if (!next || (child->num != -1 && child->num < next->num))
585 workspace_next_on_output_end:
590 * Focuses the previous workspace on same output.
593 Con* workspace_prev_on_output(void) {
594 Con *current = con_get_workspace(focused);
596 Con *output = con_get_output(focused);
597 DLOG("output = %s\n", output->name);
599 if (current->num == -1) {
600 /* If named workspace, find previous named workspace. */
601 prev = TAILQ_PREV(current, nodes_head, nodes);
602 if (prev && prev->num != -1)
605 /* If numbered workspace, find previous numbered workspace. */
606 NODES_FOREACH_REVERSE(output_get_content(output)) {
607 if (child->type != CT_WORKSPACE || child->num == -1)
609 /* Need to check child against current and previous because we
610 * are traversing multiple lists and thus are not guaranteed
611 * the relative order between the list of workspaces. */
612 if (current->num > child->num && (!prev || child->num > prev->num))
617 /* Find previous named workspace. */
619 bool found_current = false;
620 NODES_FOREACH_REVERSE(output_get_content(output)) {
621 if (child->type != CT_WORKSPACE)
623 if (child == current) {
624 found_current = true;
625 } else if (child->num == -1 && (current->num != -1 || found_current)) {
627 goto workspace_prev_on_output_end;
632 /* Find last workspace. */
634 NODES_FOREACH_REVERSE(output_get_content(output)) {
635 if (child->type != CT_WORKSPACE)
637 if (!prev || child->num > prev->num)
642 workspace_prev_on_output_end:
647 * Focuses the previously focused workspace.
650 void workspace_back_and_forth(void) {
651 if (!previous_workspace_name) {
652 DLOG("No previous workspace name set. Not switching.");
656 workspace_show_by_name(previous_workspace_name);
659 static bool get_urgency_flag(Con *con) {
661 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
662 if (child->urgent || get_urgency_flag(child))
665 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
666 if (child->urgent || get_urgency_flag(child))
673 * Goes through all clients on the given workspace and updates the workspace’s
674 * urgent flag accordingly.
677 void workspace_update_urgent_flag(Con *ws) {
678 bool old_flag = ws->urgent;
679 ws->urgent = get_urgency_flag(ws);
680 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
682 if (old_flag != ws->urgent)
683 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
687 * 'Forces' workspace orientation by moving all cons into a new split-con with
688 * the same orientation as the workspace and then changing the workspace
692 void ws_force_orientation(Con *ws, orientation_t orientation) {
693 /* 1: create a new split container */
694 Con *split = con_new(NULL, NULL);
697 /* 2: copy layout and orientation from workspace */
698 split->layout = ws->layout;
699 split->orientation = ws->orientation;
701 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
703 /* 3: move the existing cons of this workspace below the new con */
704 DLOG("Moving cons\n");
705 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
706 Con *child = TAILQ_FIRST(&(ws->nodes_head));
708 con_attach(child, split, true);
711 /* 4: switch workspace orientation */
712 ws->orientation = orientation;
714 /* 5: attach the new split container to the workspace */
715 DLOG("Attaching new split to ws\n");
716 con_attach(split, ws, false);
718 /* 6: fix the percentages */
722 con_focus(old_focused);
726 * Called when a new con (with a window, not an empty or split con) should be
727 * attached to the workspace (for example when managing a new window or when
728 * moving an existing window to the workspace level).
730 * Depending on the workspace_layout setting, this function either returns the
731 * workspace itself (default layout) or creates a new stacked/tabbed con and
735 Con *workspace_attach_to(Con *ws) {
736 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
738 if (config.default_layout == L_DEFAULT) {
739 DLOG("Default layout, just attaching it to the workspace itself.\n");
743 DLOG("Non-default layout, creating a new split container\n");
744 /* 1: create a new split container */
745 Con *new = con_new(NULL, NULL);
748 /* 2: set the requested layout on the split con */
749 new->layout = config.default_layout;
751 /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
752 * to be set. Otherwise, this con will not be interpreted as a split
754 if (config.default_orientation == NO_ORIENTATION) {
755 new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
757 new->orientation = config.default_orientation;
760 /* 4: attach the new split container to the workspace */
761 DLOG("Attaching new split %p to workspace %p\n", new, ws);
762 con_attach(new, ws, false);