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
15 * Returns a pointer to the workspace with the given number (starting at 0),
16 * creating the workspace if necessary (by allocating the necessary amount of
17 * memory and initializing the data structures correctly).
20 Con *workspace_get(const char *num, bool *created) {
21 Con *output, *workspace = NULL;
23 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
24 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
26 if (workspace == NULL) {
27 LOG("Creating new workspace \"%s\"\n", num);
28 /* unless an assignment is found, we will create this workspace on the current output */
29 output = con_get_output(focused);
30 /* look for assignments */
31 struct Workspace_Assignment *assignment;
32 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
33 if (strcmp(assignment->name, num) != 0)
36 LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
37 GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
40 Con *content = output_get_content(output);
41 LOG("got output %p with content %p\n", output, content);
42 /* We need to attach this container after setting its type. con_attach
43 * will handle CT_WORKSPACEs differently */
44 workspace = con_new(NULL, NULL);
46 asprintf(&name, "[i3 con] workspace %s", num);
47 x_set_name(workspace, name);
49 workspace->type = CT_WORKSPACE;
50 FREE(workspace->name);
51 workspace->name = sstrdup(num);
52 /* We set ->num to the number if this workspace’s name consists only of
53 * a positive number. Otherwise it’s a named ws and num will be -1. */
55 long parsed_num = strtol(num, &end, 10);
56 if (parsed_num == LONG_MIN ||
57 parsed_num == LONG_MAX ||
59 (end && *end != '\0'))
61 else workspace->num = parsed_num;
62 LOG("num = %d\n", workspace->num);
64 /* If default_orientation is set to NO_ORIENTATION we
65 * determine workspace orientation from workspace size.
66 * Otherwise we just set the orientation to default_orientation. */
67 if (config.default_orientation == NO_ORIENTATION) {
68 workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
69 DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
70 workspace->rect.width, workspace->rect.height, workspace->orientation);
72 workspace->orientation = config.default_orientation;
75 con_attach(workspace, content, false);
77 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
81 else if (created != NULL) {
89 * Returns true if the workspace is currently visible. Especially important for
90 * multi-monitor environments, as they can have multiple currenlty active
94 bool workspace_is_visible(Con *ws) {
95 Con *output = con_get_output(ws);
98 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
99 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
104 * XXX: we need to clean up all this recursive walking code.
107 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
110 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
111 if (current != exclude &&
112 current->sticky_group != NULL &&
113 current->window != NULL &&
114 strcmp(current->sticky_group, sticky_group) == 0)
117 Con *recurse = _get_sticky(current, sticky_group, exclude);
122 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
123 if (current != exclude &&
124 current->sticky_group != NULL &&
125 current->window != NULL &&
126 strcmp(current->sticky_group, sticky_group) == 0)
129 Con *recurse = _get_sticky(current, sticky_group, exclude);
138 * Reassigns all child windows in sticky containers. Called when the user
139 * changes workspaces.
141 * XXX: what about sticky containers which contain containers?
144 static void workspace_reassign_sticky(Con *con) {
146 /* 1: go through all containers */
148 /* handle all children and floating windows of this node */
149 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
150 if (current->sticky_group == NULL) {
151 workspace_reassign_sticky(current);
155 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
156 /* 2: find a window which we can re-assign */
157 Con *output = con_get_output(current);
158 Con *src = _get_sticky(output, current->sticky_group, current);
161 LOG("No window found for this sticky group\n");
162 workspace_reassign_sticky(current);
166 x_move_win(src, current);
167 current->window = src->window;
168 current->mapped = true;
172 x_reparent_child(current, src);
174 LOG("re-assigned window from src %p to dest %p\n", src, current);
177 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
178 workspace_reassign_sticky(current);
182 * Switches to the given workspace
185 void workspace_show(const char *num) {
186 Con *workspace, *current, *old = NULL;
188 bool changed_num_workspaces;
189 workspace = workspace_get(num, &changed_num_workspaces);
191 /* disable fullscreen for the other workspaces and get the workspace we are
193 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
194 if (current->fullscreen_mode == CF_OUTPUT)
196 current->fullscreen_mode = CF_NONE;
199 /* enable fullscreen for the target workspace. If it happens to be the
200 * same one we are currently on anyways, we can stop here. */
201 workspace->fullscreen_mode = CF_OUTPUT;
202 if (workspace == con_get_workspace(focused)) {
203 DLOG("Not switching, already there.\n");
207 workspace_reassign_sticky(workspace);
209 LOG("switching to %p\n", workspace);
210 Con *next = con_descend_focused(workspace);
212 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
213 /* check if this workspace is currently visible */
214 if (!workspace_is_visible(old)) {
215 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
216 tree_close(old, DONT_KILL_WINDOW, false);
217 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
218 changed_num_workspaces = true;
223 workspace->fullscreen_mode = CF_OUTPUT;
224 LOG("focused now = %p / %s\n", focused, focused->name);
226 /* Update the EWMH hints */
227 if (changed_num_workspaces)
228 ewmh_update_workarea();
229 ewmh_update_current_desktop();
231 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
235 * Focuses the next workspace.
238 void workspace_next() {
239 Con *ws = con_get_workspace(focused);
240 Con *next = TAILQ_NEXT(ws, nodes);
242 next = TAILQ_FIRST(&(ws->parent->nodes_head));
244 workspace_show(next->name);
248 * Focuses the previous workspace.
251 void workspace_prev() {
252 Con *ws = con_get_workspace(focused);
253 Con *prev = TAILQ_PREV(ws, nodes_head, nodes);
255 prev = TAILQ_LAST(&(ws->parent->nodes_head), nodes_head);
257 workspace_show(prev->name);
260 static bool get_urgency_flag(Con *con) {
262 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
263 if (child->urgent || get_urgency_flag(child))
266 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
267 if (child->urgent || get_urgency_flag(child))
274 * Goes through all clients on the given workspace and updates the workspace’s
275 * urgent flag accordingly.
278 void workspace_update_urgent_flag(Con *ws) {
279 bool old_flag = ws->urgent;
280 ws->urgent = get_urgency_flag(ws);
281 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
283 if (old_flag != ws->urgent)
284 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
288 * 'Forces' workspace orientation by moving all cons into a new split-con with
289 * the same orientation as the workspace and then changing the workspace
293 void ws_force_orientation(Con *ws, orientation_t orientation) {
294 /* 1: create a new split container */
295 Con *split = con_new(NULL, NULL);
298 /* 2: copy layout and orientation from workspace */
299 split->layout = ws->layout;
300 split->orientation = ws->orientation;
302 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
304 /* 3: move the existing cons of this workspace below the new con */
305 DLOG("Moving cons\n");
306 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
307 Con *child = TAILQ_FIRST(&(ws->nodes_head));
309 con_attach(child, split, true);
312 /* 4: switch workspace orientation */
313 ws->orientation = orientation;
315 /* 5: attach the new split container to the workspace */
316 DLOG("Attaching new split to ws\n");
317 con_attach(split, ws, false);
319 /* 6: fix the percentages */
323 con_focus(old_focused);
327 * Called when a new con (with a window, not an empty or split con) should be
328 * attached to the workspace (for example when managing a new window or when
329 * moving an existing window to the workspace level).
331 * Depending on the workspace_layout setting, this function either returns the
332 * workspace itself (default layout) or creates a new stacked/tabbed con and
336 Con *workspace_attach_to(Con *ws) {
337 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
339 if (config.default_layout == L_DEFAULT) {
340 DLOG("Default layout, just attaching it to the workspace itself.\n");
344 DLOG("Non-default layout, creating a new split container\n");
345 /* 1: create a new split container */
346 Con *new = con_new(NULL, NULL);
349 /* 2: set the requested layout on the split con */
350 new->layout = config.default_layout;
352 /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
353 * to be set. Otherwise, this con will not be interpreted as a split
355 if (config.default_orientation == NO_ORIENTATION) {
356 new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
358 new->orientation = config.default_orientation;
361 /* 4: attach the new split container to the workspace */
362 DLOG("Attaching new split %p to workspace %p\n", new, ws);
363 con_attach(new, ws, false);