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/number command.
116 * Beware: The workspace names "next", "prev", "next_on_output",
117 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
118 * so we check before 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, "number", strlen("number")) == 0 ||
124 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
125 strncasecmp(target, "current", strlen("current")) == 0)
130 ws->name = strdup(target);
131 if (ws->name[strlen(ws->name)-1] == '"')
132 ws->name[strlen(ws->name)-1] = '\0';
133 DLOG("trying name *%s*\n", ws->name);
135 /* Ensure that this workspace is not assigned to a different output —
136 * otherwise we would create it, then move it over to its output, then
137 * find a new workspace, etc… */
138 bool assigned = false;
139 struct Workspace_Assignment *assignment;
140 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
141 if (strcmp(assignment->name, ws->name) != 0 ||
142 strcmp(assignment->output, output->name) == 0)
153 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
154 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
156 exists = (current != NULL);
158 /* Set ->num to the number of the workspace, if the name actually
159 * is a number or starts with a number */
161 long parsed_num = strtol(ws->name, &endptr, 10);
162 if (parsed_num == LONG_MIN ||
163 parsed_num == LONG_MAX ||
167 else ws->num = parsed_num;
168 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
175 /* get the next unused workspace number */
176 DLOG("Getting next unused workspace by number\n");
182 sasprintf(&(ws->name), "%d", c);
185 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
186 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
187 exists = (current != NULL);
189 DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
193 con_attach(ws, content, false);
195 sasprintf(&name, "[i3 con] workspace %s", ws->name);
196 x_set_name(ws, name);
199 ws->fullscreen_mode = CF_OUTPUT;
201 /* If default_orientation is set to NO_ORIENTATION we determine
202 * orientation depending on output resolution. */
203 if (config.default_orientation == NO_ORIENTATION) {
204 ws->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
205 DLOG("Auto orientation. Workspace size set to (%d,%d), setting orientation to %d.\n",
206 output->rect.width, output->rect.height, ws->orientation);
208 ws->orientation = config.default_orientation;
215 * Returns true if the workspace is currently visible. Especially important for
216 * multi-monitor environments, as they can have multiple currenlty active
220 bool workspace_is_visible(Con *ws) {
221 Con *output = con_get_output(ws);
224 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
225 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
230 * XXX: we need to clean up all this recursive walking code.
233 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
236 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
237 if (current != exclude &&
238 current->sticky_group != NULL &&
239 current->window != NULL &&
240 strcmp(current->sticky_group, sticky_group) == 0)
243 Con *recurse = _get_sticky(current, sticky_group, exclude);
248 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
249 if (current != exclude &&
250 current->sticky_group != NULL &&
251 current->window != NULL &&
252 strcmp(current->sticky_group, sticky_group) == 0)
255 Con *recurse = _get_sticky(current, sticky_group, exclude);
264 * Reassigns all child windows in sticky containers. Called when the user
265 * changes workspaces.
267 * XXX: what about sticky containers which contain containers?
270 static void workspace_reassign_sticky(Con *con) {
272 /* 1: go through all containers */
274 /* handle all children and floating windows of this node */
275 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
276 if (current->sticky_group == NULL) {
277 workspace_reassign_sticky(current);
281 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
282 /* 2: find a window which we can re-assign */
283 Con *output = con_get_output(current);
284 Con *src = _get_sticky(output, current->sticky_group, current);
287 LOG("No window found for this sticky group\n");
288 workspace_reassign_sticky(current);
292 x_move_win(src, current);
293 current->window = src->window;
294 current->mapped = true;
298 x_reparent_child(current, src);
300 LOG("re-assigned window from src %p to dest %p\n", src, current);
303 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
304 workspace_reassign_sticky(current);
308 static void _workspace_show(Con *workspace) {
309 Con *current, *old = NULL;
311 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
312 if (workspace->name[0] == '_' && workspace->name[1] == '_')
315 /* disable fullscreen for the other workspaces and get the workspace we are
317 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
318 if (current->fullscreen_mode == CF_OUTPUT)
320 current->fullscreen_mode = CF_NONE;
323 /* enable fullscreen for the target workspace. If it happens to be the
324 * same one we are currently on anyways, we can stop here. */
325 workspace->fullscreen_mode = CF_OUTPUT;
326 current = con_get_workspace(focused);
327 if (workspace == current) {
328 DLOG("Not switching, already there.\n");
332 /* Remember currently focused workspace for switching back to it later with
333 * the 'workspace back_and_forth' command.
334 * NOTE: We have to duplicate the name as the original will be freed when
335 * the corresponding workspace is cleaned up. */
337 FREE(previous_workspace_name);
339 previous_workspace_name = sstrdup(current->name);
341 workspace_reassign_sticky(workspace);
343 LOG("switching to %p\n", workspace);
344 Con *next = con_descend_focused(workspace);
346 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
347 /* check if this workspace is currently visible */
348 if (!workspace_is_visible(old)) {
349 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
350 tree_close(old, DONT_KILL_WINDOW, false, false);
351 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
355 /* Memorize current output */
356 Con *old_output = con_get_output(focused);
359 workspace->fullscreen_mode = CF_OUTPUT;
360 LOG("focused now = %p / %s\n", focused, focused->name);
362 /* Set mouse pointer */
363 Con *new_output = con_get_output(focused);
364 if (old_output != new_output) {
365 x_set_warp_to(&next->rect);
368 /* Update the EWMH hints */
369 ewmh_update_current_desktop();
371 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
375 * Switches to the given workspace
378 void workspace_show(Con *workspace) {
379 _workspace_show(workspace);
383 * Looks up the workspace by name and switches to it.
386 void workspace_show_by_name(const char *num) {
388 bool changed_num_workspaces;
389 workspace = workspace_get(num, &changed_num_workspaces);
390 _workspace_show(workspace);
394 * Focuses the next workspace.
397 Con* workspace_next(void) {
398 Con *current = con_get_workspace(focused);
402 if (current->num == -1) {
403 /* If currently a named workspace, find next named workspace. */
404 next = TAILQ_NEXT(current, nodes);
406 /* If currently a numbered workspace, find next numbered workspace. */
407 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
408 /* Skip outputs starting with __, they are internal. */
409 if (output->name[0] == '_' && output->name[1] == '_')
411 NODES_FOREACH(output_get_content(output)) {
412 if (child->type != CT_WORKSPACE)
414 if (child->num == -1)
416 /* Need to check child against current and next because we are
417 * traversing multiple lists and thus are not guaranteed the
418 * relative order between the list of workspaces. */
419 if (current->num < child->num && (!next || child->num < next->num))
425 /* Find next named workspace. */
427 bool found_current = false;
428 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
429 /* Skip outputs starting with __, they are internal. */
430 if (output->name[0] == '_' && output->name[1] == '_')
432 NODES_FOREACH(output_get_content(output)) {
433 if (child->type != CT_WORKSPACE)
435 if (child == current) {
437 } else if (child->num == -1 && (current->num != -1 || found_current)) {
439 goto workspace_next_end;
445 /* Find first workspace. */
447 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
448 /* Skip outputs starting with __, they are internal. */
449 if (output->name[0] == '_' && output->name[1] == '_')
451 NODES_FOREACH(output_get_content(output)) {
452 if (child->type != CT_WORKSPACE)
454 if (!next || (child->num != -1 && child->num < next->num))
464 * Focuses the previous workspace.
467 Con* workspace_prev(void) {
468 Con *current = con_get_workspace(focused);
472 if (current->num == -1) {
473 /* If named workspace, find previous named workspace. */
474 prev = TAILQ_PREV(current, nodes_head, nodes);
475 if (prev && prev->num != -1)
478 /* If numbered workspace, find previous numbered workspace. */
479 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
480 /* Skip outputs starting with __, they are internal. */
481 if (output->name[0] == '_' && output->name[1] == '_')
483 NODES_FOREACH_REVERSE(output_get_content(output)) {
484 if (child->type != CT_WORKSPACE || child->num == -1)
486 /* Need to check child against current and previous because we
487 * are traversing multiple lists and thus are not guaranteed
488 * the relative order between the list of workspaces. */
489 if (current->num > child->num && (!prev || child->num > prev->num))
495 /* Find previous named workspace. */
497 bool found_current = false;
498 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
499 /* Skip outputs starting with __, they are internal. */
500 if (output->name[0] == '_' && output->name[1] == '_')
502 NODES_FOREACH_REVERSE(output_get_content(output)) {
503 if (child->type != CT_WORKSPACE)
505 if (child == current) {
506 found_current = true;
507 } else if (child->num == -1 && (current->num != -1 || found_current)) {
509 goto workspace_prev_end;
515 /* Find last workspace. */
517 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
518 /* Skip outputs starting with __, they are internal. */
519 if (output->name[0] == '_' && output->name[1] == '_')
521 NODES_FOREACH_REVERSE(output_get_content(output)) {
522 if (child->type != CT_WORKSPACE)
524 if (!prev || child->num > prev->num)
536 * Focuses the next workspace on the same output.
539 Con* workspace_next_on_output(void) {
540 Con *current = con_get_workspace(focused);
542 Con *output = con_get_output(focused);
544 if (current->num == -1) {
545 /* If currently a named workspace, find next named workspace. */
546 next = TAILQ_NEXT(current, nodes);
548 /* If currently a numbered workspace, find next numbered workspace. */
549 NODES_FOREACH(output_get_content(output)) {
550 if (child->type != CT_WORKSPACE)
552 if (child->num == -1)
554 /* Need to check child against current and next because we are
555 * traversing multiple lists and thus are not guaranteed the
556 * relative order between the list of workspaces. */
557 if (current->num < child->num && (!next || child->num < next->num))
562 /* Find next named workspace. */
564 bool found_current = false;
565 NODES_FOREACH(output_get_content(output)) {
566 if (child->type != CT_WORKSPACE)
568 if (child == current) {
570 } else if (child->num == -1 && (current->num != -1 || found_current)) {
572 goto workspace_next_on_output_end;
577 /* Find first workspace. */
579 NODES_FOREACH(output_get_content(output)) {
580 if (child->type != CT_WORKSPACE)
582 if (!next || (child->num != -1 && child->num < next->num))
586 workspace_next_on_output_end:
591 * Focuses the previous workspace on same output.
594 Con* workspace_prev_on_output(void) {
595 Con *current = con_get_workspace(focused);
597 Con *output = con_get_output(focused);
598 DLOG("output = %s\n", output->name);
600 if (current->num == -1) {
601 /* If named workspace, find previous named workspace. */
602 prev = TAILQ_PREV(current, nodes_head, nodes);
603 if (prev && prev->num != -1)
606 /* If numbered workspace, find previous numbered workspace. */
607 NODES_FOREACH_REVERSE(output_get_content(output)) {
608 if (child->type != CT_WORKSPACE || child->num == -1)
610 /* Need to check child against current and previous because we
611 * are traversing multiple lists and thus are not guaranteed
612 * the relative order between the list of workspaces. */
613 if (current->num > child->num && (!prev || child->num > prev->num))
618 /* Find previous named workspace. */
620 bool found_current = false;
621 NODES_FOREACH_REVERSE(output_get_content(output)) {
622 if (child->type != CT_WORKSPACE)
624 if (child == current) {
625 found_current = true;
626 } else if (child->num == -1 && (current->num != -1 || found_current)) {
628 goto workspace_prev_on_output_end;
633 /* Find last workspace. */
635 NODES_FOREACH_REVERSE(output_get_content(output)) {
636 if (child->type != CT_WORKSPACE)
638 if (!prev || child->num > prev->num)
643 workspace_prev_on_output_end:
648 * Focuses the previously focused workspace.
651 void workspace_back_and_forth(void) {
652 if (!previous_workspace_name) {
653 DLOG("No previous workspace name set. Not switching.");
657 workspace_show_by_name(previous_workspace_name);
660 static bool get_urgency_flag(Con *con) {
662 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
663 if (child->urgent || get_urgency_flag(child))
666 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
667 if (child->urgent || get_urgency_flag(child))
674 * Goes through all clients on the given workspace and updates the workspace’s
675 * urgent flag accordingly.
678 void workspace_update_urgent_flag(Con *ws) {
679 bool old_flag = ws->urgent;
680 ws->urgent = get_urgency_flag(ws);
681 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
683 if (old_flag != ws->urgent)
684 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
688 * 'Forces' workspace orientation by moving all cons into a new split-con with
689 * the same orientation as the workspace and then changing the workspace
693 void ws_force_orientation(Con *ws, orientation_t orientation) {
694 /* 1: create a new split container */
695 Con *split = con_new(NULL, NULL);
698 /* 2: copy layout and orientation from workspace */
699 split->layout = ws->layout;
700 split->orientation = ws->orientation;
702 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
704 /* 3: move the existing cons of this workspace below the new con */
705 DLOG("Moving cons\n");
706 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
707 Con *child = TAILQ_FIRST(&(ws->nodes_head));
709 con_attach(child, split, true);
712 /* 4: switch workspace orientation */
713 ws->orientation = orientation;
715 /* 5: attach the new split container to the workspace */
716 DLOG("Attaching new split to ws\n");
717 con_attach(split, ws, false);
719 /* 6: fix the percentages */
723 con_focus(old_focused);
727 * Called when a new con (with a window, not an empty or split con) should be
728 * attached to the workspace (for example when managing a new window or when
729 * moving an existing window to the workspace level).
731 * Depending on the workspace_layout setting, this function either returns the
732 * workspace itself (default layout) or creates a new stacked/tabbed con and
736 Con *workspace_attach_to(Con *ws) {
737 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
739 if (config.default_layout == L_DEFAULT) {
740 DLOG("Default layout, just attaching it to the workspace itself.\n");
744 DLOG("Non-default layout, creating a new split container\n");
745 /* 1: create a new split container */
746 Con *new = con_new(NULL, NULL);
749 /* 2: set the requested layout on the split con */
750 new->layout = config.default_layout;
752 /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
753 * to be set. Otherwise, this con will not be interpreted as a split
755 if (config.default_orientation == NO_ORIENTATION) {
756 new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
758 new->orientation = config.default_orientation;
761 /* 4: attach the new split container to the workspace */
762 DLOG("Attaching new split %p to workspace %p\n", new, ws);
763 con_attach(new, ws, false);