2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
7 * workspace.c: Functions for modifying workspaces
13 * Returns a pointer to the workspace with the given number (starting at 0),
14 * creating the workspace if necessary (by allocating the necessary amount of
15 * memory and initializing the data structures correctly).
18 Con *workspace_get(const char *num, bool *created) {
19 Con *output, *workspace = NULL;
21 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
22 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
24 if (workspace == NULL) {
25 LOG("Creating new workspace \"%s\"\n", num);
26 /* unless an assignment is found, we will create this workspace on the current output */
27 output = con_get_output(focused);
28 /* look for assignments */
29 struct Workspace_Assignment *assignment;
30 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
31 if (strcmp(assignment->name, num) != 0)
34 LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
35 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
38 Con *content = output_get_content(output);
39 LOG("got output %p with content %p\n", output, content);
40 /* We need to attach this container after setting its type. con_attach
41 * will handle CT_WORKSPACEs differently */
42 workspace = con_new(NULL, NULL);
44 asprintf(&name, "[i3 con] workspace %s", num);
45 x_set_name(workspace, name);
47 workspace->type = CT_WORKSPACE;
48 FREE(workspace->name);
49 workspace->name = sstrdup(num);
50 /* We set ->num to the number if this workspace’s name consists only of
51 * a positive number. Otherwise it’s a named ws and num will be -1. */
53 long parsed_num = strtol(num, NULL, 10);
54 if (parsed_num == LONG_MIN ||
55 parsed_num == LONG_MAX ||
58 else workspace->num = parsed_num;
59 LOG("num = %d\n", workspace->num);
61 /* If default_orientation is set to NO_ORIENTATION we
62 * determine workspace orientation from workspace size.
63 * Otherwise we just set the orientation to default_orientation. */
64 if (config.default_orientation == NO_ORIENTATION) {
65 workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
66 DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
67 workspace->rect.width, workspace->rect.height, workspace->orientation);
69 workspace->orientation = config.default_orientation;
72 con_attach(workspace, content, false);
74 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
78 else if (created != NULL) {
86 * Returns true if the workspace is currently visible. Especially important for
87 * multi-monitor environments, as they can have multiple currenlty active
91 bool workspace_is_visible(Con *ws) {
92 Con *output = con_get_output(ws);
95 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
96 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
101 * XXX: we need to clean up all this recursive walking code.
104 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
107 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
108 if (current != exclude &&
109 current->sticky_group != NULL &&
110 current->window != NULL &&
111 strcmp(current->sticky_group, sticky_group) == 0)
114 Con *recurse = _get_sticky(current, sticky_group, exclude);
119 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
120 if (current != exclude &&
121 current->sticky_group != NULL &&
122 current->window != NULL &&
123 strcmp(current->sticky_group, sticky_group) == 0)
126 Con *recurse = _get_sticky(current, sticky_group, exclude);
135 * Reassigns all child windows in sticky containers. Called when the user
136 * changes workspaces.
138 * XXX: what about sticky containers which contain containers?
141 static void workspace_reassign_sticky(Con *con) {
143 /* 1: go through all containers */
145 /* handle all children and floating windows of this node */
146 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
147 if (current->sticky_group == NULL) {
148 workspace_reassign_sticky(current);
152 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
153 /* 2: find a window which we can re-assign */
154 Con *output = con_get_output(current);
155 Con *src = _get_sticky(output, current->sticky_group, current);
158 LOG("No window found for this sticky group\n");
159 workspace_reassign_sticky(current);
163 x_move_win(src, current);
164 current->window = src->window;
165 current->mapped = true;
169 x_reparent_child(current, src);
171 LOG("re-assigned window from src %p to dest %p\n", src, current);
174 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
175 workspace_reassign_sticky(current);
179 * Switches to the given workspace
182 void workspace_show(const char *num) {
183 Con *workspace, *current, *old = NULL;
185 bool changed_num_workspaces;
186 workspace = workspace_get(num, &changed_num_workspaces);
188 /* disable fullscreen for the other workspaces and get the workspace we are
190 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
191 if (current->fullscreen_mode == CF_OUTPUT)
193 current->fullscreen_mode = CF_NONE;
196 /* enable fullscreen for the target workspace. If it happens to be the
197 * same one we are currently on anyways, we can stop here. */
198 workspace->fullscreen_mode = CF_OUTPUT;
199 if (workspace == con_get_workspace(focused)) {
200 DLOG("Not switching, already there.\n");
204 workspace_reassign_sticky(workspace);
206 LOG("switching to %p\n", workspace);
207 Con *next = con_descend_focused(workspace);
209 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
210 /* check if this workspace is currently visible */
211 if (!workspace_is_visible(old)) {
212 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
213 tree_close(old, DONT_KILL_WINDOW, false);
214 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
215 changed_num_workspaces = true;
220 workspace->fullscreen_mode = CF_OUTPUT;
221 LOG("focused now = %p / %s\n", focused, focused->name);
223 /* Update the EWMH hints */
224 if (changed_num_workspaces)
225 ewmh_update_workarea();
226 ewmh_update_current_desktop();
228 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
232 * Focuses the next workspace.
235 void workspace_next() {
236 Con *current = con_get_workspace(focused);
240 if (current->num == -1) {
241 /* If currently a named workspace, find next named workspace. */
242 next = TAILQ_NEXT(current, nodes);
244 /* If currently a numbered workspace, find next numbered workspace. */
245 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
246 NODES_FOREACH(output_get_content(output)) {
247 if (child->type != CT_WORKSPACE)
249 if (child->num == -1)
251 /* Need to check child against current and next because we are
252 * traversing multiple lists and thus are not guaranteed the
253 * relative order between the list of workspaces. */
254 if (current->num < child->num && (!next || child->num < next->num))
259 /* Find next named workspace. */
261 bool found_current = false;
262 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
263 NODES_FOREACH(output_get_content(output)) {
264 if (child->type != CT_WORKSPACE)
266 if (child == current) {
268 } else if (child->num == -1 && (current->num != -1 || found_current)) {
270 goto workspace_next_show;
275 /* Find first workspace. */
277 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
278 NODES_FOREACH(output_get_content(output)) {
279 if (child->type != CT_WORKSPACE)
281 if (!next || (child->num != -1 && child->num < next->num))
287 workspace_show(next->name);
291 * Focuses the previous workspace.
294 void workspace_prev() {
295 Con *current = con_get_workspace(focused);
299 if (current->num == -1) {
300 /* If named workspace, find previous named workspace. */
301 prev = TAILQ_PREV(current, nodes_head, nodes);
302 if (prev && prev->num != -1)
305 /* If numbered workspace, find previous numbered workspace. */
306 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
307 NODES_FOREACH_REVERSE(output_get_content(output)) {
308 if (child->type != CT_WORKSPACE || child->num == -1)
310 /* Need to check child against current and previous because we
311 * are traversing multiple lists and thus are not guaranteed
312 * the relative order between the list of workspaces. */
313 if (current->num > child->num && (!prev || child->num > prev->num))
318 /* Find previous named workspace. */
320 bool found_current = false;
321 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
322 NODES_FOREACH_REVERSE(output_get_content(output)) {
323 if (child->type != CT_WORKSPACE)
325 if (child == current) {
327 } else if (child->num == -1 && (current->num != -1 || found_current)) {
329 goto workspace_prev_show;
334 /* Find last workspace. */
336 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
337 NODES_FOREACH_REVERSE(output_get_content(output)) {
338 if (child->type != CT_WORKSPACE)
340 if (!prev || child->num > prev->num)
346 workspace_show(prev->name);
349 static bool get_urgency_flag(Con *con) {
351 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
352 if (child->urgent || get_urgency_flag(child))
355 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
356 if (child->urgent || get_urgency_flag(child))
363 * Goes through all clients on the given workspace and updates the workspace’s
364 * urgent flag accordingly.
367 void workspace_update_urgent_flag(Con *ws) {
368 bool old_flag = ws->urgent;
369 ws->urgent = get_urgency_flag(ws);
370 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
372 if (old_flag != ws->urgent)
373 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
377 * 'Forces' workspace orientation by moving all cons into a new split-con with
378 * the same orientation as the workspace and then changing the workspace
382 void ws_force_orientation(Con *ws, orientation_t orientation) {
383 /* 1: create a new split container */
384 Con *split = con_new(NULL, NULL);
387 /* 2: copy layout and orientation from workspace */
388 split->layout = ws->layout;
389 split->orientation = ws->orientation;
391 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
393 /* 3: move the existing cons of this workspace below the new con */
394 DLOG("Moving cons\n");
395 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
396 Con *child = TAILQ_FIRST(&(ws->nodes_head));
398 con_attach(child, split, true);
401 /* 4: switch workspace orientation */
402 ws->orientation = orientation;
404 /* 5: attach the new split container to the workspace */
405 DLOG("Attaching new split to ws\n");
406 con_attach(split, ws, false);
408 /* 6: fix the percentages */
412 con_focus(old_focused);
416 * Called when a new con (with a window, not an empty or split con) should be
417 * attached to the workspace (for example when managing a new window or when
418 * moving an existing window to the workspace level).
420 * Depending on the workspace_layout setting, this function either returns the
421 * workspace itself (default layout) or creates a new stacked/tabbed con and
425 Con *workspace_attach_to(Con *ws) {
426 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
428 if (config.default_layout == L_DEFAULT) {
429 DLOG("Default layout, just attaching it to the workspace itself.\n");
433 DLOG("Non-default layout, creating a new split container\n");
434 /* 1: create a new split container */
435 Con *new = con_new(NULL, NULL);
438 /* 2: set the requested layout on the split con */
439 new->layout = config.default_layout;
441 /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
442 * to be set. Otherwise, this con will not be interpreted as a split
444 if (config.default_orientation == NO_ORIENTATION) {
445 new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
447 new->orientation = config.default_orientation;
450 /* 4: attach the new split container to the workspace */
451 DLOG("Attaching new split %p to workspace %p\n", new, ws);
452 con_attach(new, ws, false);