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 /* We set ->num to the number if this workspace’s name begins with a
77 * positive number. Otherwise it’s a named ws and num will be -1. */
79 long parsed_num = strtol(num, &endptr, 10);
80 if (parsed_num == LONG_MIN ||
81 parsed_num == LONG_MAX ||
85 else workspace->num = parsed_num;
86 LOG("num = %d\n", workspace->num);
88 workspace->parent = content;
89 _workspace_apply_default_orientation(workspace);
91 con_attach(workspace, content, false);
93 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
97 else if (created != NULL) {
105 * Returns a pointer to a new workspace in the given output. The workspace
106 * is created attached to the tree hierarchy through the given content
110 Con *create_workspace_on_output(Output *output, Con *content) {
111 /* add a workspace to this output */
115 Con *ws = con_new(NULL, NULL);
116 ws->type = CT_WORKSPACE;
118 /* try the configured workspace bindings first to find a free name */
120 TAILQ_FOREACH(bind, bindings, bindings) {
121 DLOG("binding with command %s\n", bind->command);
122 if (strlen(bind->command) < strlen("workspace ") ||
123 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0)
125 DLOG("relevant command = %s\n", bind->command);
126 char *target = bind->command + strlen("workspace ");
127 /* We check if this is the workspace
128 * next/prev/next_on_output/prev_on_output/back_and_forth/number command.
129 * Beware: The workspace names "next", "prev", "next_on_output",
130 * "prev_on_output", "number", "back_and_forth" and "current" are OK,
131 * so we check before stripping the double quotes */
132 if (strncasecmp(target, "next", strlen("next")) == 0 ||
133 strncasecmp(target, "prev", strlen("prev")) == 0 ||
134 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
135 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
136 strncasecmp(target, "number", strlen("number")) == 0 ||
137 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
138 strncasecmp(target, "current", strlen("current")) == 0)
143 ws->name = strdup(target);
144 if (ws->name[strlen(ws->name)-1] == '"')
145 ws->name[strlen(ws->name)-1] = '\0';
146 DLOG("trying name *%s*\n", ws->name);
148 /* Ensure that this workspace is not assigned to a different output —
149 * otherwise we would create it, then move it over to its output, then
150 * find a new workspace, etc… */
151 bool assigned = false;
152 struct Workspace_Assignment *assignment;
153 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
154 if (strcmp(assignment->name, ws->name) != 0 ||
155 strcmp(assignment->output, output->name) == 0)
166 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
167 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
169 exists = (current != NULL);
171 /* Set ->num to the number of the workspace, if the name actually
172 * is a number or starts with a number */
174 long parsed_num = strtol(ws->name, &endptr, 10);
175 if (parsed_num == LONG_MIN ||
176 parsed_num == LONG_MAX ||
180 else ws->num = parsed_num;
181 LOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
188 /* get the next unused workspace number */
189 DLOG("Getting next unused workspace by number\n");
195 sasprintf(&(ws->name), "%d", c);
198 TAILQ_FOREACH(out, &(croot->nodes_head), nodes)
199 GREP_FIRST(current, output_get_content(out), !strcasecmp(child->name, ws->name));
200 exists = (current != NULL);
202 DLOG("result for ws %s / %d: exists = %d\n", ws->name, c, exists);
206 con_attach(ws, content, false);
208 sasprintf(&name, "[i3 con] workspace %s", ws->name);
209 x_set_name(ws, name);
212 ws->fullscreen_mode = CF_OUTPUT;
214 _workspace_apply_default_orientation(ws);
221 * Returns true if the workspace is currently visible. Especially important for
222 * multi-monitor environments, as they can have multiple currenlty active
226 bool workspace_is_visible(Con *ws) {
227 Con *output = con_get_output(ws);
230 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
231 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
236 * XXX: we need to clean up all this recursive walking code.
239 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
242 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
243 if (current != exclude &&
244 current->sticky_group != NULL &&
245 current->window != NULL &&
246 strcmp(current->sticky_group, sticky_group) == 0)
249 Con *recurse = _get_sticky(current, sticky_group, exclude);
254 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
255 if (current != exclude &&
256 current->sticky_group != NULL &&
257 current->window != NULL &&
258 strcmp(current->sticky_group, sticky_group) == 0)
261 Con *recurse = _get_sticky(current, sticky_group, exclude);
270 * Reassigns all child windows in sticky containers. Called when the user
271 * changes workspaces.
273 * XXX: what about sticky containers which contain containers?
276 static void workspace_reassign_sticky(Con *con) {
278 /* 1: go through all containers */
280 /* handle all children and floating windows of this node */
281 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
282 if (current->sticky_group == NULL) {
283 workspace_reassign_sticky(current);
287 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
288 /* 2: find a window which we can re-assign */
289 Con *output = con_get_output(current);
290 Con *src = _get_sticky(output, current->sticky_group, current);
293 LOG("No window found for this sticky group\n");
294 workspace_reassign_sticky(current);
298 x_move_win(src, current);
299 current->window = src->window;
300 current->mapped = true;
304 x_reparent_child(current, src);
306 LOG("re-assigned window from src %p to dest %p\n", src, current);
309 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
310 workspace_reassign_sticky(current);
314 static void _workspace_show(Con *workspace) {
315 Con *current, *old = NULL;
317 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
318 if (workspace->name[0] == '_' && workspace->name[1] == '_')
321 /* disable fullscreen for the other workspaces and get the workspace we are
323 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
324 if (current->fullscreen_mode == CF_OUTPUT)
326 current->fullscreen_mode = CF_NONE;
329 /* enable fullscreen for the target workspace. If it happens to be the
330 * same one we are currently on anyways, we can stop here. */
331 workspace->fullscreen_mode = CF_OUTPUT;
332 current = con_get_workspace(focused);
333 if (workspace == current) {
334 DLOG("Not switching, already there.\n");
338 /* Remember currently focused workspace for switching back to it later with
339 * the 'workspace back_and_forth' command.
340 * NOTE: We have to duplicate the name as the original will be freed when
341 * the corresponding workspace is cleaned up. */
343 FREE(previous_workspace_name);
345 previous_workspace_name = sstrdup(current->name);
347 workspace_reassign_sticky(workspace);
349 LOG("switching to %p\n", workspace);
350 Con *next = con_descend_focused(workspace);
352 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
353 /* check if this workspace is currently visible */
354 if (!workspace_is_visible(old)) {
355 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
356 tree_close(old, DONT_KILL_WINDOW, false, false);
357 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
361 /* Memorize current output */
362 Con *old_output = con_get_output(focused);
365 workspace->fullscreen_mode = CF_OUTPUT;
366 LOG("focused now = %p / %s\n", focused, focused->name);
368 /* Set mouse pointer */
369 Con *new_output = con_get_output(focused);
370 if (old_output != new_output) {
371 x_set_warp_to(&next->rect);
374 /* Update the EWMH hints */
375 ewmh_update_current_desktop();
377 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
381 * Switches to the given workspace
384 void workspace_show(Con *workspace) {
385 _workspace_show(workspace);
389 * Looks up the workspace by name and switches to it.
392 void workspace_show_by_name(const char *num) {
394 bool changed_num_workspaces;
395 workspace = workspace_get(num, &changed_num_workspaces);
396 _workspace_show(workspace);
400 * Focuses the next workspace.
403 Con* workspace_next(void) {
404 Con *current = con_get_workspace(focused);
408 if (current->num == -1) {
409 /* If currently a named workspace, find next named workspace. */
410 next = TAILQ_NEXT(current, nodes);
412 /* If currently a numbered workspace, find next numbered workspace. */
413 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
414 /* Skip outputs starting with __, they are internal. */
415 if (output->name[0] == '_' && output->name[1] == '_')
417 NODES_FOREACH(output_get_content(output)) {
418 if (child->type != CT_WORKSPACE)
420 if (child->num == -1)
422 /* Need to check child against current and next because we are
423 * traversing multiple lists and thus are not guaranteed the
424 * relative order between the list of workspaces. */
425 if (current->num < child->num && (!next || child->num < next->num))
431 /* Find next named workspace. */
433 bool found_current = false;
434 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
435 /* Skip outputs starting with __, they are internal. */
436 if (output->name[0] == '_' && output->name[1] == '_')
438 NODES_FOREACH(output_get_content(output)) {
439 if (child->type != CT_WORKSPACE)
441 if (child == current) {
443 } else if (child->num == -1 && (current->num != -1 || found_current)) {
445 goto workspace_next_end;
451 /* Find first workspace. */
453 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
454 /* Skip outputs starting with __, they are internal. */
455 if (output->name[0] == '_' && output->name[1] == '_')
457 NODES_FOREACH(output_get_content(output)) {
458 if (child->type != CT_WORKSPACE)
460 if (!next || (child->num != -1 && child->num < next->num))
470 * Focuses the previous workspace.
473 Con* workspace_prev(void) {
474 Con *current = con_get_workspace(focused);
478 if (current->num == -1) {
479 /* If named workspace, find previous named workspace. */
480 prev = TAILQ_PREV(current, nodes_head, nodes);
481 if (prev && prev->num != -1)
484 /* If numbered workspace, find previous numbered workspace. */
485 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
486 /* Skip outputs starting with __, they are internal. */
487 if (output->name[0] == '_' && output->name[1] == '_')
489 NODES_FOREACH_REVERSE(output_get_content(output)) {
490 if (child->type != CT_WORKSPACE || child->num == -1)
492 /* Need to check child against current and previous because we
493 * are traversing multiple lists and thus are not guaranteed
494 * the relative order between the list of workspaces. */
495 if (current->num > child->num && (!prev || child->num > prev->num))
501 /* Find previous named workspace. */
503 bool found_current = false;
504 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
505 /* Skip outputs starting with __, they are internal. */
506 if (output->name[0] == '_' && output->name[1] == '_')
508 NODES_FOREACH_REVERSE(output_get_content(output)) {
509 if (child->type != CT_WORKSPACE)
511 if (child == current) {
512 found_current = true;
513 } else if (child->num == -1 && (current->num != -1 || found_current)) {
515 goto workspace_prev_end;
521 /* Find last workspace. */
523 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
524 /* Skip outputs starting with __, they are internal. */
525 if (output->name[0] == '_' && output->name[1] == '_')
527 NODES_FOREACH_REVERSE(output_get_content(output)) {
528 if (child->type != CT_WORKSPACE)
530 if (!prev || child->num > prev->num)
542 * Focuses the next workspace on the same output.
545 Con* workspace_next_on_output(void) {
546 Con *current = con_get_workspace(focused);
548 Con *output = con_get_output(focused);
550 if (current->num == -1) {
551 /* If currently a named workspace, find next named workspace. */
552 next = TAILQ_NEXT(current, nodes);
554 /* If currently a numbered workspace, find next numbered workspace. */
555 NODES_FOREACH(output_get_content(output)) {
556 if (child->type != CT_WORKSPACE)
558 if (child->num == -1)
560 /* Need to check child against current and next because we are
561 * traversing multiple lists and thus are not guaranteed the
562 * relative order between the list of workspaces. */
563 if (current->num < child->num && (!next || child->num < next->num))
568 /* Find next named workspace. */
570 bool found_current = false;
571 NODES_FOREACH(output_get_content(output)) {
572 if (child->type != CT_WORKSPACE)
574 if (child == current) {
576 } else if (child->num == -1 && (current->num != -1 || found_current)) {
578 goto workspace_next_on_output_end;
583 /* Find first workspace. */
585 NODES_FOREACH(output_get_content(output)) {
586 if (child->type != CT_WORKSPACE)
588 if (!next || (child->num != -1 && child->num < next->num))
592 workspace_next_on_output_end:
597 * Focuses the previous workspace on same output.
600 Con* workspace_prev_on_output(void) {
601 Con *current = con_get_workspace(focused);
603 Con *output = con_get_output(focused);
604 DLOG("output = %s\n", output->name);
606 if (current->num == -1) {
607 /* If named workspace, find previous named workspace. */
608 prev = TAILQ_PREV(current, nodes_head, nodes);
609 if (prev && prev->num != -1)
612 /* If numbered workspace, find previous numbered workspace. */
613 NODES_FOREACH_REVERSE(output_get_content(output)) {
614 if (child->type != CT_WORKSPACE || child->num == -1)
616 /* Need to check child against current and previous because we
617 * are traversing multiple lists and thus are not guaranteed
618 * the relative order between the list of workspaces. */
619 if (current->num > child->num && (!prev || child->num > prev->num))
624 /* Find previous named workspace. */
626 bool found_current = false;
627 NODES_FOREACH_REVERSE(output_get_content(output)) {
628 if (child->type != CT_WORKSPACE)
630 if (child == current) {
631 found_current = true;
632 } else if (child->num == -1 && (current->num != -1 || found_current)) {
634 goto workspace_prev_on_output_end;
639 /* Find last workspace. */
641 NODES_FOREACH_REVERSE(output_get_content(output)) {
642 if (child->type != CT_WORKSPACE)
644 if (!prev || child->num > prev->num)
649 workspace_prev_on_output_end:
654 * Focuses the previously focused workspace.
657 void workspace_back_and_forth(void) {
658 if (!previous_workspace_name) {
659 DLOG("No previous workspace name set. Not switching.");
663 workspace_show_by_name(previous_workspace_name);
666 static bool get_urgency_flag(Con *con) {
668 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
669 if (child->urgent || get_urgency_flag(child))
672 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
673 if (child->urgent || get_urgency_flag(child))
680 * Goes through all clients on the given workspace and updates the workspace’s
681 * urgent flag accordingly.
684 void workspace_update_urgent_flag(Con *ws) {
685 bool old_flag = ws->urgent;
686 ws->urgent = get_urgency_flag(ws);
687 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
689 if (old_flag != ws->urgent)
690 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
694 * 'Forces' workspace orientation by moving all cons into a new split-con with
695 * the same layout as the workspace and then changing the workspace layout.
698 void ws_force_orientation(Con *ws, orientation_t orientation) {
699 /* 1: create a new split container */
700 Con *split = con_new(NULL, NULL);
703 /* 2: copy layout from workspace */
704 split->layout = ws->layout;
706 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
708 /* 3: move the existing cons of this workspace below the new con */
709 DLOG("Moving cons\n");
710 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
711 Con *child = TAILQ_FIRST(&(ws->nodes_head));
713 con_attach(child, split, true);
716 /* 4: switch workspace layout */
717 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
719 /* 5: attach the new split container to the workspace */
720 DLOG("Attaching new split to ws\n");
721 con_attach(split, ws, false);
723 /* 6: fix the percentages */
727 con_focus(old_focused);
731 * Called when a new con (with a window, not an empty or split con) should be
732 * attached to the workspace (for example when managing a new window or when
733 * moving an existing window to the workspace level).
735 * Depending on the workspace_layout setting, this function either returns the
736 * workspace itself (default layout) or creates a new stacked/tabbed con and
740 Con *workspace_attach_to(Con *ws) {
741 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
743 if (config.default_layout == L_DEFAULT) {
744 DLOG("Default layout, just attaching it to the workspace itself.\n");
748 DLOG("Non-default layout, creating a new split container\n");
749 /* 1: create a new split container */
750 Con *new = con_new(NULL, NULL);
754 /* 2: set the requested layout on the split con */
755 new->layout = config.default_layout;
757 /* 4: attach the new split container to the workspace */
758 DLOG("Attaching new split %p to workspace %p\n", new, ws);
759 con_attach(new, ws, false);