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);
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);
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\"}");
234 static bool get_urgency_flag(Con *con) {
236 TAILQ_FOREACH(child, &(con->nodes_head), nodes)
237 if (child->urgent || get_urgency_flag(child))
240 TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
241 if (child->urgent || get_urgency_flag(child))
248 * Goes through all clients on the given workspace and updates the workspace’s
249 * urgent flag accordingly.
252 void workspace_update_urgent_flag(Con *ws) {
253 bool old_flag = ws->urgent;
254 ws->urgent = get_urgency_flag(ws);
255 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
257 if (old_flag != ws->urgent)
258 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
262 * 'Forces' workspace orientation by moving all cons into a new split-con with
263 * the same orientation as the workspace and then changing the workspace
267 void ws_force_orientation(Con *ws, orientation_t orientation) {
268 /* 1: create a new split container */
269 Con *split = con_new(NULL);
272 /* 2: copy layout and orientation from workspace */
273 split->layout = ws->layout;
274 split->orientation = ws->orientation;
276 Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
278 /* 3: move the existing cons of this workspace below the new con */
279 DLOG("Moving cons\n");
280 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
281 Con *child = TAILQ_FIRST(&(ws->nodes_head));
283 con_attach(child, split, true);
286 /* 4: switch workspace orientation */
287 ws->orientation = orientation;
289 /* 5: attach the new split container to the workspace */
290 DLOG("Attaching new split to ws\n");
291 con_attach(split, ws, false);
293 /* 6: fix the percentages */
297 con_focus(old_focused);