]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Make workspace_layout handle all cons at workspace level, not only the first one...
[i3/i3] / src / workspace.c
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2010 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * workspace.c: Functions for modifying workspaces
8  *
9  */
10 #include <limits.h>
11
12 #include "all.h"
13
14 /*
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).
18  *
19  */
20 Con *workspace_get(const char *num, bool *created) {
21     Con *output, *workspace = NULL;
22
23     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
24         GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
25
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)
34                 continue;
35
36             LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
37             GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
38             break;
39         }
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);
45         char *name;
46         asprintf(&name, "[i3 con] workspace %s", num);
47         x_set_name(workspace, name);
48         free(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. */
54         char *end;
55         long parsed_num = strtol(num, &end, 10);
56         if (parsed_num == LONG_MIN ||
57             parsed_num == LONG_MAX ||
58             parsed_num < 0 ||
59             (end && *end != '\0'))
60             workspace->num = -1;
61         else workspace->num = parsed_num;
62         LOG("num = %d\n", workspace->num);
63
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);
71         } else {
72             workspace->orientation = config.default_orientation;
73         }
74
75         con_attach(workspace, content, false);
76
77         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
78         if (created != NULL)
79             *created = true;
80     }
81     else if (created != NULL) {
82         *created = false;
83     }
84
85     return workspace;
86 }
87
88 /*
89  * Returns true if the workspace is currently visible. Especially important for
90  * multi-monitor environments, as they can have multiple currenlty active
91  * workspaces.
92  *
93  */
94 bool workspace_is_visible(Con *ws) {
95     Con *output = con_get_output(ws);
96     if (output == NULL)
97         return false;
98     Con *fs = con_get_fullscreen_con(output);
99     LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
100     return (fs == ws);
101 }
102
103 /*
104  * XXX: we need to clean up all this recursive walking code.
105  *
106  */
107 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
108     Con *current;
109
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)
115             return current;
116
117         Con *recurse = _get_sticky(current, sticky_group, exclude);
118         if (recurse != NULL)
119             return recurse;
120     }
121
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)
127             return current;
128
129         Con *recurse = _get_sticky(current, sticky_group, exclude);
130         if (recurse != NULL)
131             return recurse;
132     }
133
134     return NULL;
135 }
136
137 /*
138  * Reassigns all child windows in sticky containers. Called when the user
139  * changes workspaces.
140  *
141  * XXX: what about sticky containers which contain containers?
142  *
143  */
144 static void workspace_reassign_sticky(Con *con) {
145     Con *current;
146     /* 1: go through all containers */
147
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);
152             continue;
153         }
154
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);
159
160         if (src == NULL) {
161             LOG("No window found for this sticky group\n");
162             workspace_reassign_sticky(current);
163             continue;
164         }
165
166         x_move_win(src, current);
167         current->window = src->window;
168         current->mapped = true;
169         src->window = NULL;
170         src->mapped = false;
171
172         x_reparent_child(current, src);
173
174         LOG("re-assigned window from src %p to dest %p\n", src, current);
175     }
176
177     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
178         workspace_reassign_sticky(current);
179 }
180
181 /*
182  * Switches to the given workspace
183  *
184  */
185 void workspace_show(const char *num) {
186     Con *workspace, *current, *old = NULL;
187
188     bool changed_num_workspaces;
189     workspace = workspace_get(num, &changed_num_workspaces);
190
191     /* disable fullscreen for the other workspaces and get the workspace we are
192      * currently on. */
193     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
194         if (current->fullscreen_mode == CF_OUTPUT)
195             old = current;
196         current->fullscreen_mode = CF_NONE;
197     }
198
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");
204         return;
205     }
206
207     workspace_reassign_sticky(workspace);
208
209     LOG("switching to %p\n", workspace);
210     Con *next = con_descend_focused(workspace);
211
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;
219         }
220     }
221
222     con_focus(next);
223     workspace->fullscreen_mode = CF_OUTPUT;
224     LOG("focused now = %p / %s\n", focused, focused->name);
225
226     /* Update the EWMH hints */
227     if (changed_num_workspaces)
228         ewmh_update_workarea();
229     ewmh_update_current_desktop();
230
231     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
232 }
233
234 static bool get_urgency_flag(Con *con) {
235     Con *child;
236     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
237         if (child->urgent || get_urgency_flag(child))
238             return true;
239
240     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
241         if (child->urgent || get_urgency_flag(child))
242             return true;
243
244     return false;
245 }
246
247 /*
248  * Goes through all clients on the given workspace and updates the workspace’s
249  * urgent flag accordingly.
250  *
251  */
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);
256
257     if (old_flag != ws->urgent)
258         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
259 }
260
261 /*
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
264  * orientation.
265  *
266  */
267 void ws_force_orientation(Con *ws, orientation_t orientation) {
268     /* 1: create a new split container */
269     Con *split = con_new(NULL, NULL);
270     split->parent = ws;
271
272     /* 2: copy layout and orientation from workspace */
273     split->layout = ws->layout;
274     split->orientation = ws->orientation;
275
276     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
277
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));
282         con_detach(child);
283         con_attach(child, split, true);
284     }
285
286     /* 4: switch workspace orientation */
287     ws->orientation = orientation;
288
289     /* 5: attach the new split container to the workspace */
290     DLOG("Attaching new split to ws\n");
291     con_attach(split, ws, false);
292
293     /* 6: fix the percentages */
294     con_fix_percent(ws);
295
296     if (old_focused)
297         con_focus(old_focused);
298 }
299
300 /*
301  * Called when a new con (with a window, not an empty or split con) should be
302  * attached to the workspace (for example when managing a new window or when
303  * moving an existing window to the workspace level).
304  *
305  * Depending on the workspace_layout setting, this function either returns the
306  * workspace itself (default layout) or creates a new stacked/tabbed con and
307  * returns that.
308  *
309  */
310 Con *workspace_attach_to(Con *ws) {
311     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
312
313     if (config.default_layout == L_DEFAULT) {
314         DLOG("Default layout, just attaching it to the workspace itself.\n");
315         return ws;
316     }
317
318     DLOG("Non-default layout, creating a new split container\n");
319     /* 1: create a new split container */
320     Con *new = con_new(NULL, NULL);
321     new->parent = ws;
322
323     /* 2: set the requested layout on the split con */
324     new->layout = config.default_layout;
325
326     /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
327      * to be set. Otherwise, this con will not be interpreted as a split
328      * container. */
329     if (config.default_orientation == NO_ORIENTATION) {
330         new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
331     } else {
332         new->orientation = config.default_orientation;
333     }
334
335     /* 4: attach the new split container to the workspace */
336     DLOG("Attaching new split %p to workspace %p\n", new, ws);
337     con_attach(new, ws, false);
338
339     return new;
340 }