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: Functions for modifying workspaces
12 /* Stores a copy of the name of the last used workspace for the workspace
13 * back-and-forth switching. */
14 static char *previous_workspace_name = NULL;
17 * Returns a pointer to the workspace with the given number (starting at 0),
18 * creating the workspace if necessary (by allocating the necessary amount of
19 * memory and initializing the data structures correctly).
22 Con *workspace_get(const char *num, bool *created) {
23 Con *output, *workspace = NULL;
25 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
26 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
28 if (workspace == NULL) {
29 LOG("Creating new workspace \"%s\"\n", num);
30 /* unless an assignment is found, we will create this workspace on the current output */
31 output = con_get_output(focused);
32 /* look for assignments */
33 struct Workspace_Assignment *assignment;
34 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
35 if (strcmp(assignment->name, num) != 0)
38 LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
39 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
42 Con *content = output_get_content(output);
43 LOG("got output %p with content %p\n", output, content);
44 /* We need to attach this container after setting its type. con_attach
45 * will handle CT_WORKSPACEs differently */
46 workspace = con_new(NULL, NULL);
48 asprintf(&name, "[i3 con] workspace %s", num);
49 x_set_name(workspace, name);
51 workspace->type = CT_WORKSPACE;
52 FREE(workspace->name);
53 workspace->name = sstrdup(num);
54 /* We set ->num to the number if this workspace’s name consists only of
55 * a positive number. Otherwise it’s a named ws and num will be -1. */
57 long parsed_num = strtol(num, &endptr, 10);
58 if (parsed_num == LONG_MIN ||
59 parsed_num == LONG_MAX ||
63 else workspace->num = parsed_num;
64 LOG("num = %d\n", workspace->num);
66 /* If default_orientation is set to NO_ORIENTATION we
67 * determine workspace orientation from workspace size.
68 * Otherwise we just set the orientation to default_orientation. */
69 if (config.default_orientation == NO_ORIENTATION) {
70 workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
71 DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
72 workspace->rect.width, workspace->rect.height, workspace->orientation);
74 workspace->orientation = config.default_orientation;
77 con_attach(workspace, content, false);
79 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
83 else if (created != NULL) {
91 * Returns true if the workspace is currently visible. Especially important for
92 * multi-monitor environments, as they can have multiple currenlty active
96 bool workspace_is_visible(Con *ws) {
97 Con *output = con_get_output(ws);
100 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
101 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
106 * XXX: we need to clean up all this recursive walking code.
109 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
112 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
113 if (current != exclude &&
114 current->sticky_group != NULL &&
115 current->window != NULL &&
116 strcmp(current->sticky_group, sticky_group) == 0)
119 Con *recurse = _get_sticky(current, sticky_group, exclude);
124 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
125 if (current != exclude &&
126 current->sticky_group != NULL &&
127 current->window != NULL &&
128 strcmp(current->sticky_group, sticky_group) == 0)
131 Con *recurse = _get_sticky(current, sticky_group, exclude);
140 * Reassigns all child windows in sticky containers. Called when the user
141 * changes workspaces.
143 * XXX: what about sticky containers which contain containers?
146 static void workspace_reassign_sticky(Con *con) {
148 /* 1: go through all containers */
150 /* handle all children and floating windows of this node */
151 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
152 if (current->sticky_group == NULL) {
153 workspace_reassign_sticky(current);
157 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
158 /* 2: find a window which we can re-assign */
159 Con *output = con_get_output(current);
160 Con *src = _get_sticky(output, current->sticky_group, current);
163 LOG("No window found for this sticky group\n");
164 workspace_reassign_sticky(current);
168 x_move_win(src, current);
169 current->window = src->window;
170 current->mapped = true;
174 x_reparent_child(current, src);
176 LOG("re-assigned window from src %p to dest %p\n", src, current);
179 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
180 workspace_reassign_sticky(current);
184 static void _workspace_show(Con *workspace, bool changed_num_workspaces) {
185 Con *current, *old = NULL;
187 /* disable fullscreen for the other workspaces and get the workspace we are
189 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
190 if (current->fullscreen_mode == CF_OUTPUT)
192 current->fullscreen_mode = CF_NONE;
195 /* enable fullscreen for the target workspace. If it happens to be the
196 * same one we are currently on anyways, we can stop here. */
197 workspace->fullscreen_mode = CF_OUTPUT;
198 current = con_get_workspace(focused);
199 if (workspace == current) {
200 DLOG("Not switching, already there.\n");
204 /* Remember currently focused workspace for switching back to it later with
205 * the 'workspace back_and_forth' command.
206 * NOTE: We have to duplicate the name as the original will be freed when
207 * the corresponding workspace is cleaned up. */
209 FREE(previous_workspace_name);
210 previous_workspace_name = sstrdup(current->name);
212 workspace_reassign_sticky(workspace);
214 LOG("switching to %p\n", workspace);
215 Con *next = con_descend_focused(workspace);
217 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
218 /* check if this workspace is currently visible */
219 if (!workspace_is_visible(old)) {
220 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
221 tree_close(old, DONT_KILL_WINDOW, false, false);
222 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
223 changed_num_workspaces = true;
227 /* Memorize current output */
228 Con *old_output = con_get_output(focused);
231 workspace->fullscreen_mode = CF_OUTPUT;
232 LOG("focused now = %p / %s\n", focused, focused->name);
234 /* Set mouse pointer */
235 Con *new_output = con_get_output(focused);
236 if (old_output != new_output) {
237 x_set_warp_to(&next->rect);
240 /* Update the EWMH hints */
241 if (changed_num_workspaces)
242 ewmh_update_workarea();
243 ewmh_update_current_desktop();
245 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
249 * Switches to the given workspace
252 void workspace_show(Con *workspace) {
253 _workspace_show(workspace, false);
257 * Looks up the workspace by name and switches to it.
260 void workspace_show_by_name(const char *num) {
262 bool changed_num_workspaces;
263 workspace = workspace_get(num, &changed_num_workspaces);
264 _workspace_show(workspace, changed_num_workspaces);
268 * Focuses the next workspace.
271 Con* workspace_next() {
272 Con *current = con_get_workspace(focused);
276 if (current->num == -1) {
277 /* If currently a named workspace, find next named workspace. */
278 next = TAILQ_NEXT(current, nodes);
280 /* If currently a numbered workspace, find next numbered workspace. */
281 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
282 NODES_FOREACH(output_get_content(output)) {
283 if (child->type != CT_WORKSPACE)
285 if (child->num == -1)
287 /* Need to check child against current and next because we are
288 * traversing multiple lists and thus are not guaranteed the
289 * relative order between the list of workspaces. */
290 if (current->num < child->num && (!next || child->num < next->num))
295 /* Find next named workspace. */
297 bool found_current = false;
298 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
299 NODES_FOREACH(output_get_content(output)) {
300 if (child->type != CT_WORKSPACE)
302 if (child == current) {
304 } else if (child->num == -1 && (current->num != -1 || found_current)) {
306 goto workspace_next_end;
311 /* Find first workspace. */
313 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
314 NODES_FOREACH(output_get_content(output)) {
315 if (child->type != CT_WORKSPACE)
317 if (!next || (child->num != -1 && child->num < next->num))
326 * Focuses the previous workspace.
329 Con* workspace_prev() {
330 Con *current = con_get_workspace(focused);
334 if (current->num == -1) {
335 /* If named workspace, find previous named workspace. */
336 prev = TAILQ_PREV(current, nodes_head, nodes);
337 if (prev && prev->num != -1)
340 /* If numbered workspace, find previous numbered workspace. */
341 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
342 NODES_FOREACH_REVERSE(output_get_content(output)) {
343 if (child->type != CT_WORKSPACE || child->num == -1)
345 /* Need to check child against current and previous because we
346 * are traversing multiple lists and thus are not guaranteed
347 * the relative order between the list of workspaces. */
348 if (current->num > child->num && (!prev || child->num > prev->num))
353 /* Find previous named workspace. */
355 bool found_current = false;
356 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
357 NODES_FOREACH_REVERSE(output_get_content(output)) {
358 if (child->type != CT_WORKSPACE)
360 if (child == current) {
362 } else if (child->num == -1 && (current->num != -1 || found_current)) {
364 goto workspace_prev_end;
369 /* Find last workspace. */
371 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
372 NODES_FOREACH_REVERSE(output_get_content(output)) {
373 if (child->type != CT_WORKSPACE)
375 if (!prev || child->num > prev->num)
385 * Focuses the previously focused workspace.
388 void workspace_back_and_forth() {
389 if (!previous_workspace_name) {
390 DLOG("No previous workspace name set. Not switching.");
394 workspace_show_by_name(previous_workspace_name);
397 static bool get_urgency_flag(Con *con) {
399 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
400 if (child->urgent || get_urgency_flag(child))
403 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
404 if (child->urgent || get_urgency_flag(child))
411 * Goes through all clients on the given workspace and updates the workspace’s
412 * urgent flag accordingly.
415 void workspace_update_urgent_flag(Con *ws) {
416 bool old_flag = ws->urgent;
417 ws->urgent = get_urgency_flag(ws);
418 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
420 if (old_flag != ws->urgent)
421 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
425 * 'Forces' workspace orientation by moving all cons into a new split-con with
426 * the same orientation as the workspace and then changing the workspace
430 void ws_force_orientation(Con *ws, orientation_t orientation) {
431 /* 1: create a new split container */
432 Con *split = con_new(NULL, NULL);
435 /* 2: copy layout and orientation from workspace */
436 split->layout = ws->layout;
437 split->orientation = ws->orientation;
439 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
441 /* 3: move the existing cons of this workspace below the new con */
442 DLOG("Moving cons\n");
443 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
444 Con *child = TAILQ_FIRST(&(ws->nodes_head));
446 con_attach(child, split, true);
449 /* 4: switch workspace orientation */
450 ws->orientation = orientation;
452 /* 5: attach the new split container to the workspace */
453 DLOG("Attaching new split to ws\n");
454 con_attach(split, ws, false);
456 /* 6: fix the percentages */
460 con_focus(old_focused);
464 * Called when a new con (with a window, not an empty or split con) should be
465 * attached to the workspace (for example when managing a new window or when
466 * moving an existing window to the workspace level).
468 * Depending on the workspace_layout setting, this function either returns the
469 * workspace itself (default layout) or creates a new stacked/tabbed con and
473 Con *workspace_attach_to(Con *ws) {
474 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
476 if (config.default_layout == L_DEFAULT) {
477 DLOG("Default layout, just attaching it to the workspace itself.\n");
481 DLOG("Non-default layout, creating a new split container\n");
482 /* 1: create a new split container */
483 Con *new = con_new(NULL, NULL);
486 /* 2: set the requested layout on the split con */
487 new->layout = config.default_layout;
489 /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
490 * to be set. Otherwise, this con will not be interpreted as a split
492 if (config.default_orientation == NO_ORIENTATION) {
493 new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
495 new->orientation = config.default_orientation;
498 /* 4: attach the new split container to the workspace */
499 DLOG("Attaching new split %p to workspace %p\n", new, ws);
500 con_attach(new, ws, false);