]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Merge branch 'tree' into next
[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, CF_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 /*
235  * Focuses the next workspace.
236  *
237  */
238 void workspace_next() {
239     Con *ws = con_get_workspace(focused);
240     Con *next = TAILQ_NEXT(ws, nodes);
241     if (!next)
242         next = TAILQ_FIRST(&(ws->parent->nodes_head));
243
244     workspace_show(next->name);
245 }
246
247 /*
248  * Focuses the previous workspace.
249  *
250  */
251 void workspace_prev() {
252     Con *ws = con_get_workspace(focused);
253     Con *prev = TAILQ_PREV(ws, nodes_head, nodes);
254     if (!prev)
255         prev = TAILQ_LAST(&(ws->parent->nodes_head), nodes_head);
256
257     workspace_show(prev->name);
258 }
259
260 static bool get_urgency_flag(Con *con) {
261     Con *child;
262     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
263         if (child->urgent || get_urgency_flag(child))
264             return true;
265
266     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
267         if (child->urgent || get_urgency_flag(child))
268             return true;
269
270     return false;
271 }
272
273 /*
274  * Goes through all clients on the given workspace and updates the workspace’s
275  * urgent flag accordingly.
276  *
277  */
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);
282
283     if (old_flag != ws->urgent)
284         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
285 }
286
287 /*
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
290  * orientation.
291  *
292  */
293 void ws_force_orientation(Con *ws, orientation_t orientation) {
294     /* 1: create a new split container */
295     Con *split = con_new(NULL, NULL);
296     split->parent = ws;
297
298     /* 2: copy layout and orientation from workspace */
299     split->layout = ws->layout;
300     split->orientation = ws->orientation;
301
302     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
303
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));
308         con_detach(child);
309         con_attach(child, split, true);
310     }
311
312     /* 4: switch workspace orientation */
313     ws->orientation = orientation;
314
315     /* 5: attach the new split container to the workspace */
316     DLOG("Attaching new split to ws\n");
317     con_attach(split, ws, false);
318
319     /* 6: fix the percentages */
320     con_fix_percent(ws);
321
322     if (old_focused)
323         con_focus(old_focused);
324 }
325
326 /*
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).
330  *
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
333  * returns that.
334  *
335  */
336 Con *workspace_attach_to(Con *ws) {
337     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
338
339     if (config.default_layout == L_DEFAULT) {
340         DLOG("Default layout, just attaching it to the workspace itself.\n");
341         return ws;
342     }
343
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);
347     new->parent = ws;
348
349     /* 2: set the requested layout on the split con */
350     new->layout = config.default_layout;
351
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
354      * container. */
355     if (config.default_orientation == NO_ORIENTATION) {
356         new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
357     } else {
358         new->orientation = config.default_orientation;
359     }
360
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);
364
365     return new;
366 }