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 consists only of
56 * a 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 true if the workspace is currently visible. Especially important for
93 * multi-monitor environments, as they can have multiple currenlty active
97 bool workspace_is_visible(Con *ws) {
98 Con *output = con_get_output(ws);
101 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
102 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
107 * XXX: we need to clean up all this recursive walking code.
110 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
113 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
114 if (current != exclude &&
115 current->sticky_group != NULL &&
116 current->window != NULL &&
117 strcmp(current->sticky_group, sticky_group) == 0)
120 Con *recurse = _get_sticky(current, sticky_group, exclude);
125 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
126 if (current != exclude &&
127 current->sticky_group != NULL &&
128 current->window != NULL &&
129 strcmp(current->sticky_group, sticky_group) == 0)
132 Con *recurse = _get_sticky(current, sticky_group, exclude);
141 * Reassigns all child windows in sticky containers. Called when the user
142 * changes workspaces.
144 * XXX: what about sticky containers which contain containers?
147 static void workspace_reassign_sticky(Con *con) {
149 /* 1: go through all containers */
151 /* handle all children and floating windows of this node */
152 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
153 if (current->sticky_group == NULL) {
154 workspace_reassign_sticky(current);
158 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
159 /* 2: find a window which we can re-assign */
160 Con *output = con_get_output(current);
161 Con *src = _get_sticky(output, current->sticky_group, current);
164 LOG("No window found for this sticky group\n");
165 workspace_reassign_sticky(current);
169 x_move_win(src, current);
170 current->window = src->window;
171 current->mapped = true;
175 x_reparent_child(current, src);
177 LOG("re-assigned window from src %p to dest %p\n", src, current);
180 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
181 workspace_reassign_sticky(current);
185 static void _workspace_show(Con *workspace, bool changed_num_workspaces) {
186 Con *current, *old = NULL;
188 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
189 if (workspace->name[0] == '_' && workspace->name[1] == '_')
192 /* disable fullscreen for the other workspaces and get the workspace we are
194 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
195 if (current->fullscreen_mode == CF_OUTPUT)
197 current->fullscreen_mode = CF_NONE;
200 /* enable fullscreen for the target workspace. If it happens to be the
201 * same one we are currently on anyways, we can stop here. */
202 workspace->fullscreen_mode = CF_OUTPUT;
203 current = con_get_workspace(focused);
204 if (workspace == current) {
205 DLOG("Not switching, already there.\n");
209 /* Remember currently focused workspace for switching back to it later with
210 * the 'workspace back_and_forth' command.
211 * NOTE: We have to duplicate the name as the original will be freed when
212 * the corresponding workspace is cleaned up. */
214 FREE(previous_workspace_name);
216 previous_workspace_name = sstrdup(current->name);
218 workspace_reassign_sticky(workspace);
220 LOG("switching to %p\n", workspace);
221 Con *next = con_descend_focused(workspace);
223 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
224 /* check if this workspace is currently visible */
225 if (!workspace_is_visible(old)) {
226 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
227 tree_close(old, DONT_KILL_WINDOW, false, false);
228 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
229 changed_num_workspaces = true;
233 /* Memorize current output */
234 Con *old_output = con_get_output(focused);
237 workspace->fullscreen_mode = CF_OUTPUT;
238 LOG("focused now = %p / %s\n", focused, focused->name);
240 /* Set mouse pointer */
241 Con *new_output = con_get_output(focused);
242 if (old_output != new_output) {
243 x_set_warp_to(&next->rect);
246 /* Update the EWMH hints */
247 ewmh_update_current_desktop();
249 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
253 * Switches to the given workspace
256 void workspace_show(Con *workspace) {
257 _workspace_show(workspace, false);
261 * Looks up the workspace by name and switches to it.
264 void workspace_show_by_name(const char *num) {
266 bool changed_num_workspaces;
267 workspace = workspace_get(num, &changed_num_workspaces);
268 _workspace_show(workspace, changed_num_workspaces);
272 * Focuses the next workspace.
275 Con* workspace_next() {
276 Con *current = con_get_workspace(focused);
280 if (current->num == -1) {
281 /* If currently a named workspace, find next named workspace. */
282 next = TAILQ_NEXT(current, nodes);
284 /* If currently a numbered workspace, find next numbered workspace. */
285 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
286 /* Skip outputs starting with __, they are internal. */
287 if (output->name[0] == '_' && output->name[1] == '_')
289 NODES_FOREACH(output_get_content(output)) {
290 if (child->type != CT_WORKSPACE)
292 if (child->num == -1)
294 /* Need to check child against current and next because we are
295 * traversing multiple lists and thus are not guaranteed the
296 * relative order between the list of workspaces. */
297 if (current->num < child->num && (!next || child->num < next->num))
303 /* Find next named workspace. */
305 bool found_current = false;
306 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
307 /* Skip outputs starting with __, they are internal. */
308 if (output->name[0] == '_' && output->name[1] == '_')
310 NODES_FOREACH(output_get_content(output)) {
311 if (child->type != CT_WORKSPACE)
313 if (child == current) {
315 } else if (child->num == -1 && (current->num != -1 || found_current)) {
317 goto workspace_next_end;
323 /* Find first workspace. */
325 TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
326 /* Skip outputs starting with __, they are internal. */
327 if (output->name[0] == '_' && output->name[1] == '_')
329 NODES_FOREACH(output_get_content(output)) {
330 if (child->type != CT_WORKSPACE)
332 if (!next || (child->num != -1 && child->num < next->num))
342 * Focuses the previous workspace.
345 Con* workspace_prev() {
346 Con *current = con_get_workspace(focused);
350 if (current->num == -1) {
351 /* If named workspace, find previous named workspace. */
352 prev = TAILQ_PREV(current, nodes_head, nodes);
353 if (prev && prev->num != -1)
356 /* If numbered workspace, find previous numbered workspace. */
357 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
358 /* Skip outputs starting with __, they are internal. */
359 if (output->name[0] == '_' && output->name[1] == '_')
361 NODES_FOREACH_REVERSE(output_get_content(output)) {
362 if (child->type != CT_WORKSPACE || child->num == -1)
364 /* Need to check child against current and previous because we
365 * are traversing multiple lists and thus are not guaranteed
366 * the relative order between the list of workspaces. */
367 if (current->num > child->num && (!prev || child->num > prev->num))
373 /* Find previous named workspace. */
375 bool found_current = false;
376 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
377 /* Skip outputs starting with __, they are internal. */
378 if (output->name[0] == '_' && output->name[1] == '_')
380 NODES_FOREACH_REVERSE(output_get_content(output)) {
381 if (child->type != CT_WORKSPACE)
383 if (child == current) {
384 found_current = true;
385 } else if (child->num == -1 && (current->num != -1 || found_current)) {
387 goto workspace_prev_end;
393 /* Find last workspace. */
395 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
396 /* Skip outputs starting with __, they are internal. */
397 if (output->name[0] == '_' && output->name[1] == '_')
399 NODES_FOREACH_REVERSE(output_get_content(output)) {
400 if (child->type != CT_WORKSPACE)
402 if (!prev || child->num > prev->num)
414 * Focuses the next workspace on the same output.
417 Con* workspace_next_on_output() {
418 Con *current = con_get_workspace(focused);
420 Con *output = con_get_output(focused);
422 if (current->num == -1) {
423 /* If currently a named workspace, find next named workspace. */
424 next = TAILQ_NEXT(current, nodes);
426 /* If currently a numbered workspace, find next numbered workspace. */
427 NODES_FOREACH(output_get_content(output)) {
428 if (child->type != CT_WORKSPACE)
430 if (child->num == -1)
432 /* Need to check child against current and next because we are
433 * traversing multiple lists and thus are not guaranteed the
434 * relative order between the list of workspaces. */
435 if (current->num < child->num && (!next || child->num < next->num))
440 /* Find next named workspace. */
442 bool found_current = false;
443 NODES_FOREACH(output_get_content(output)) {
444 if (child->type != CT_WORKSPACE)
446 if (child == current) {
448 } else if (child->num == -1 && (current->num != -1 || found_current)) {
450 goto workspace_next_on_output_end;
455 /* Find first workspace. */
457 NODES_FOREACH(output_get_content(output)) {
458 if (child->type != CT_WORKSPACE)
460 if (!next || (child->num != -1 && child->num < next->num))
464 workspace_next_on_output_end:
469 * Focuses the previous workspace on same output.
472 Con* workspace_prev_on_output() {
473 Con *current = con_get_workspace(focused);
475 Con *output = con_get_output(focused);
477 if (current->num == -1) {
478 /* If named workspace, find previous named workspace. */
479 prev = TAILQ_PREV(current, nodes_head, nodes);
480 if (prev && prev->num != -1)
483 /* If numbered workspace, find previous numbered workspace. */
484 NODES_FOREACH_REVERSE(output_get_content(output)) {
485 if (child->type != CT_WORKSPACE || child->num == -1)
487 /* Need to check child against current and previous because we
488 * are traversing multiple lists and thus are not guaranteed
489 * the relative order between the list of workspaces. */
490 if (current->num > child->num && (!prev || child->num > prev->num))
495 /* Find previous named workspace. */
497 bool found_current = false;
498 NODES_FOREACH_REVERSE(output_get_content(output)) {
499 if (child->type != CT_WORKSPACE)
501 if (child == current) {
502 found_current = true;
503 } else if (child->num == -1 && (current->num != -1 || found_current)) {
505 goto workspace_prev_on_output_end;
510 /* Find last workspace. */
512 NODES_FOREACH_REVERSE(output_get_content(output)) {
513 if (child->type != CT_WORKSPACE)
515 if (!prev || child->num > prev->num)
520 workspace_prev_on_output_end:
525 * Focuses the previously focused workspace.
528 void workspace_back_and_forth() {
529 if (!previous_workspace_name) {
530 DLOG("No previous workspace name set. Not switching.");
534 workspace_show_by_name(previous_workspace_name);
537 static bool get_urgency_flag(Con *con) {
539 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
540 if (child->urgent || get_urgency_flag(child))
543 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
544 if (child->urgent || get_urgency_flag(child))
551 * Goes through all clients on the given workspace and updates the workspace’s
552 * urgent flag accordingly.
555 void workspace_update_urgent_flag(Con *ws) {
556 bool old_flag = ws->urgent;
557 ws->urgent = get_urgency_flag(ws);
558 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
560 if (old_flag != ws->urgent)
561 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
565 * 'Forces' workspace orientation by moving all cons into a new split-con with
566 * the same orientation as the workspace and then changing the workspace
570 void ws_force_orientation(Con *ws, orientation_t orientation) {
571 /* 1: create a new split container */
572 Con *split = con_new(NULL, NULL);
575 /* 2: copy layout and orientation from workspace */
576 split->layout = ws->layout;
577 split->orientation = ws->orientation;
579 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
581 /* 3: move the existing cons of this workspace below the new con */
582 DLOG("Moving cons\n");
583 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
584 Con *child = TAILQ_FIRST(&(ws->nodes_head));
586 con_attach(child, split, true);
589 /* 4: switch workspace orientation */
590 ws->orientation = orientation;
592 /* 5: attach the new split container to the workspace */
593 DLOG("Attaching new split to ws\n");
594 con_attach(split, ws, false);
596 /* 6: fix the percentages */
600 con_focus(old_focused);
604 * Called when a new con (with a window, not an empty or split con) should be
605 * attached to the workspace (for example when managing a new window or when
606 * moving an existing window to the workspace level).
608 * Depending on the workspace_layout setting, this function either returns the
609 * workspace itself (default layout) or creates a new stacked/tabbed con and
613 Con *workspace_attach_to(Con *ws) {
614 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
616 if (config.default_layout == L_DEFAULT) {
617 DLOG("Default layout, just attaching it to the workspace itself.\n");
621 DLOG("Non-default layout, creating a new split container\n");
622 /* 1: create a new split container */
623 Con *new = con_new(NULL, NULL);
626 /* 2: set the requested layout on the split con */
627 new->layout = config.default_layout;
629 /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
630 * to be set. Otherwise, this con will not be interpreted as a split
632 if (config.default_orientation == NO_ORIENTATION) {
633 new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
635 new->orientation = config.default_orientation;
638 /* 4: attach the new split container to the workspace */
639 DLOG("Attaching new split %p to workspace %p\n", new, ws);
640 con_attach(new, ws, false);