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, &end, 10);
54 if (parsed_num == LONG_MIN ||
55 parsed_num == LONG_MAX ||
57 (end && *end != '\0'))
59 else workspace->num = parsed_num;
60 LOG("num = %d\n", workspace->num);
62 /* If default_orientation is set to NO_ORIENTATION we
63 * determine workspace orientation from workspace size.
64 * Otherwise we just set the orientation to default_orientation. */
65 if (config.default_orientation == NO_ORIENTATION) {
66 workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
67 DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
68 workspace->rect.width, workspace->rect.height, workspace->orientation);
70 workspace->orientation = config.default_orientation;
73 con_attach(workspace, content, false);
75 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
79 else if (created != NULL) {
87 * Returns true if the workspace is currently visible. Especially important for
88 * multi-monitor environments, as they can have multiple currenlty active
92 bool workspace_is_visible(Con *ws) {
93 Con *output = con_get_output(ws);
96 Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
97 LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
102 * XXX: we need to clean up all this recursive walking code.
105 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
108 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
109 if (current != exclude &&
110 current->sticky_group != NULL &&
111 current->window != NULL &&
112 strcmp(current->sticky_group, sticky_group) == 0)
115 Con *recurse = _get_sticky(current, sticky_group, exclude);
120 TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
121 if (current != exclude &&
122 current->sticky_group != NULL &&
123 current->window != NULL &&
124 strcmp(current->sticky_group, sticky_group) == 0)
127 Con *recurse = _get_sticky(current, sticky_group, exclude);
136 * Reassigns all child windows in sticky containers. Called when the user
137 * changes workspaces.
139 * XXX: what about sticky containers which contain containers?
142 static void workspace_reassign_sticky(Con *con) {
144 /* 1: go through all containers */
146 /* handle all children and floating windows of this node */
147 TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
148 if (current->sticky_group == NULL) {
149 workspace_reassign_sticky(current);
153 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
154 /* 2: find a window which we can re-assign */
155 Con *output = con_get_output(current);
156 Con *src = _get_sticky(output, current->sticky_group, current);
159 LOG("No window found for this sticky group\n");
160 workspace_reassign_sticky(current);
164 x_move_win(src, current);
165 current->window = src->window;
166 current->mapped = true;
170 x_reparent_child(current, src);
172 LOG("re-assigned window from src %p to dest %p\n", src, current);
175 TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
176 workspace_reassign_sticky(current);
180 * Switches to the given workspace
183 void workspace_show(const char *num) {
184 Con *workspace, *current, *old = NULL;
186 bool changed_num_workspaces;
187 workspace = workspace_get(num, &changed_num_workspaces);
189 /* disable fullscreen for the other workspaces and get the workspace we are
191 TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
192 if (current->fullscreen_mode == CF_OUTPUT)
194 current->fullscreen_mode = CF_NONE;
197 /* enable fullscreen for the target workspace. If it happens to be the
198 * same one we are currently on anyways, we can stop here. */
199 workspace->fullscreen_mode = CF_OUTPUT;
200 if (workspace == con_get_workspace(focused)) {
201 DLOG("Not switching, already there.\n");
205 workspace_reassign_sticky(workspace);
207 LOG("switching to %p\n", workspace);
208 Con *next = con_descend_focused(workspace);
210 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
211 /* check if this workspace is currently visible */
212 if (!workspace_is_visible(old)) {
213 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
214 tree_close(old, DONT_KILL_WINDOW, false);
215 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
216 changed_num_workspaces = true;
220 /* Memorize current output */
221 Con *old_output = con_get_output(focused);
224 workspace->fullscreen_mode = CF_OUTPUT;
225 LOG("focused now = %p / %s\n", focused, focused->name);
227 /* Set mouse pointer */
228 Con *new_output = con_get_output(focused);
229 if (old_output != new_output) {
230 xcb_warp_pointer_rect(conn, &next->rect);
233 /* Update the EWMH hints */
234 if (changed_num_workspaces)
235 ewmh_update_workarea();
236 ewmh_update_current_desktop();
238 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
242 * Focuses the next workspace.
245 void workspace_next() {
246 Con *current = con_get_workspace(focused);
250 if (current->num == -1) {
251 /* If currently a named workspace, find next named workspace. */
252 next = TAILQ_NEXT(current, nodes);
254 /* If currently a numbered workspace, find next numbered workspace. */
255 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
256 NODES_FOREACH(output_get_content(output)) {
257 if (child->type != CT_WORKSPACE)
259 if (child->num == -1)
261 /* Need to check child against current and next because we are
262 * traversing multiple lists and thus are not guaranteed the
263 * relative order between the list of workspaces. */
264 if (current->num < child->num && (!next || child->num < next->num))
269 /* Find next named workspace. */
271 bool found_current = false;
272 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
273 NODES_FOREACH(output_get_content(output)) {
274 if (child->type != CT_WORKSPACE)
276 if (child == current) {
278 } else if (child->num == -1 && (current->num != -1 || found_current)) {
280 goto workspace_next_show;
285 /* Find first workspace. */
287 TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
288 NODES_FOREACH(output_get_content(output)) {
289 if (child->type != CT_WORKSPACE)
291 if (!next || (child->num != -1 && child->num < next->num))
297 workspace_show(next->name);
301 * Focuses the previous workspace.
304 void workspace_prev() {
305 Con *current = con_get_workspace(focused);
309 if (current->num == -1) {
310 /* If named workspace, find previous named workspace. */
311 prev = TAILQ_PREV(current, nodes_head, nodes);
312 if (prev && prev->num != -1)
315 /* If numbered workspace, find previous numbered workspace. */
316 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
317 NODES_FOREACH_REVERSE(output_get_content(output)) {
318 if (child->type != CT_WORKSPACE || child->num == -1)
320 /* Need to check child against current and previous because we
321 * are traversing multiple lists and thus are not guaranteed
322 * the relative order between the list of workspaces. */
323 if (current->num > child->num && (!prev || child->num > prev->num))
328 /* Find previous named workspace. */
330 bool found_current = false;
331 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
332 NODES_FOREACH_REVERSE(output_get_content(output)) {
333 if (child->type != CT_WORKSPACE)
335 if (child == current) {
337 } else if (child->num == -1 && (current->num != -1 || found_current)) {
339 goto workspace_prev_show;
344 /* Find last workspace. */
346 TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
347 NODES_FOREACH_REVERSE(output_get_content(output)) {
348 if (child->type != CT_WORKSPACE)
350 if (!prev || child->num > prev->num)
356 workspace_show(prev->name);
359 static bool get_urgency_flag(Con *con) {
361 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
362 if (child->urgent || get_urgency_flag(child))
365 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
366 if (child->urgent || get_urgency_flag(child))
373 * Goes through all clients on the given workspace and updates the workspace’s
374 * urgent flag accordingly.
377 void workspace_update_urgent_flag(Con *ws) {
378 bool old_flag = ws->urgent;
379 ws->urgent = get_urgency_flag(ws);
380 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
382 if (old_flag != ws->urgent)
383 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
387 * 'Forces' workspace orientation by moving all cons into a new split-con with
388 * the same orientation as the workspace and then changing the workspace
392 void ws_force_orientation(Con *ws, orientation_t orientation) {
393 /* 1: create a new split container */
394 Con *split = con_new(NULL, NULL);
397 /* 2: copy layout and orientation from workspace */
398 split->layout = ws->layout;
399 split->orientation = ws->orientation;
401 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
403 /* 3: move the existing cons of this workspace below the new con */
404 DLOG("Moving cons\n");
405 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
406 Con *child = TAILQ_FIRST(&(ws->nodes_head));
408 con_attach(child, split, true);
411 /* 4: switch workspace orientation */
412 ws->orientation = orientation;
414 /* 5: attach the new split container to the workspace */
415 DLOG("Attaching new split to ws\n");
416 con_attach(split, ws, false);
418 /* 6: fix the percentages */
422 con_focus(old_focused);
426 * Called when a new con (with a window, not an empty or split con) should be
427 * attached to the workspace (for example when managing a new window or when
428 * moving an existing window to the workspace level).
430 * Depending on the workspace_layout setting, this function either returns the
431 * workspace itself (default layout) or creates a new stacked/tabbed con and
435 Con *workspace_attach_to(Con *ws) {
436 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
438 if (config.default_layout == L_DEFAULT) {
439 DLOG("Default layout, just attaching it to the workspace itself.\n");
443 DLOG("Non-default layout, creating a new split container\n");
444 /* 1: create a new split container */
445 Con *new = con_new(NULL, NULL);
448 /* 2: set the requested layout on the split con */
449 new->layout = config.default_layout;
451 /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
452 * to be set. Otherwise, this con will not be interpreted as a split
454 if (config.default_orientation == NO_ORIENTATION) {
455 new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
457 new->orientation = config.default_orientation;
460 /* 4: attach the new split container to the workspace */
461 DLOG("Attaching new split %p to workspace %p\n", new, ws);
462 con_attach(new, ws, false);