]> git.sur5r.net Git - i3/i3/blob - src/workspace.c
Merge branch 'master' 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 "all.h"
11
12 /*
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).
16  *
17  */
18 Con *workspace_get(const char *num, bool *created) {
19     Con *output, *workspace = NULL;
20
21     TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
22         GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, num));
23
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)
32                 continue;
33
34             LOG("Found workspace assignment to output \"%s\"\n", assignment->output);
35             GREP_FIRST(output, croot, !strcmp(child->name, assignment->output));
36             break;
37         }
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);
43         char *name;
44         asprintf(&name, "[i3 con] workspace %s", num);
45         x_set_name(workspace, name);
46         free(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. */
52
53         long parsed_num = strtol(num, NULL, 10);
54         if (parsed_num == LONG_MIN ||
55             parsed_num == LONG_MAX ||
56             parsed_num <= 0)
57             workspace->num = -1;
58         else workspace->num = parsed_num;
59         LOG("num = %d\n", workspace->num);
60
61         /* If default_orientation is set to NO_ORIENTATION we
62          * determine workspace orientation from workspace size.
63          * Otherwise we just set the orientation to default_orientation. */
64         if (config.default_orientation == NO_ORIENTATION) {
65             workspace->orientation = (output->rect.height > output->rect.width) ? VERT : HORIZ;
66             DLOG("Auto orientation. Output resolution set to (%d,%d), setting orientation to %d.\n",
67                  workspace->rect.width, workspace->rect.height, workspace->orientation);
68         } else {
69             workspace->orientation = config.default_orientation;
70         }
71
72         con_attach(workspace, content, false);
73
74         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"init\"}");
75         if (created != NULL)
76             *created = true;
77     }
78     else if (created != NULL) {
79         *created = false;
80     }
81
82     return workspace;
83 }
84
85 /*
86  * Returns true if the workspace is currently visible. Especially important for
87  * multi-monitor environments, as they can have multiple currenlty active
88  * workspaces.
89  *
90  */
91 bool workspace_is_visible(Con *ws) {
92     Con *output = con_get_output(ws);
93     if (output == NULL)
94         return false;
95     Con *fs = con_get_fullscreen_con(output, CF_OUTPUT);
96     LOG("workspace visible? fs = %p, ws = %p\n", fs, ws);
97     return (fs == ws);
98 }
99
100 /*
101  * XXX: we need to clean up all this recursive walking code.
102  *
103  */
104 Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
105     Con *current;
106
107     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
108         if (current != exclude &&
109             current->sticky_group != NULL &&
110             current->window != NULL &&
111             strcmp(current->sticky_group, sticky_group) == 0)
112             return current;
113
114         Con *recurse = _get_sticky(current, sticky_group, exclude);
115         if (recurse != NULL)
116             return recurse;
117     }
118
119     TAILQ_FOREACH(current, &(con->floating_head), floating_windows) {
120         if (current != exclude &&
121             current->sticky_group != NULL &&
122             current->window != NULL &&
123             strcmp(current->sticky_group, sticky_group) == 0)
124             return current;
125
126         Con *recurse = _get_sticky(current, sticky_group, exclude);
127         if (recurse != NULL)
128             return recurse;
129     }
130
131     return NULL;
132 }
133
134 /*
135  * Reassigns all child windows in sticky containers. Called when the user
136  * changes workspaces.
137  *
138  * XXX: what about sticky containers which contain containers?
139  *
140  */
141 static void workspace_reassign_sticky(Con *con) {
142     Con *current;
143     /* 1: go through all containers */
144
145     /* handle all children and floating windows of this node */
146     TAILQ_FOREACH(current, &(con->nodes_head), nodes) {
147         if (current->sticky_group == NULL) {
148             workspace_reassign_sticky(current);
149             continue;
150         }
151
152         LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
153         /* 2: find a window which we can re-assign */
154         Con *output = con_get_output(current);
155         Con *src = _get_sticky(output, current->sticky_group, current);
156
157         if (src == NULL) {
158             LOG("No window found for this sticky group\n");
159             workspace_reassign_sticky(current);
160             continue;
161         }
162
163         x_move_win(src, current);
164         current->window = src->window;
165         current->mapped = true;
166         src->window = NULL;
167         src->mapped = false;
168
169         x_reparent_child(current, src);
170
171         LOG("re-assigned window from src %p to dest %p\n", src, current);
172     }
173
174     TAILQ_FOREACH(current, &(con->floating_head), floating_windows)
175         workspace_reassign_sticky(current);
176 }
177
178 /*
179  * Switches to the given workspace
180  *
181  */
182 void workspace_show(const char *num) {
183     Con *workspace, *current, *old = NULL;
184
185     bool changed_num_workspaces;
186     workspace = workspace_get(num, &changed_num_workspaces);
187
188     /* disable fullscreen for the other workspaces and get the workspace we are
189      * currently on. */
190     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
191         if (current->fullscreen_mode == CF_OUTPUT)
192             old = current;
193         current->fullscreen_mode = CF_NONE;
194     }
195
196     /* enable fullscreen for the target workspace. If it happens to be the
197      * same one we are currently on anyways, we can stop here. */
198     workspace->fullscreen_mode = CF_OUTPUT;
199     if (workspace == con_get_workspace(focused)) {
200         DLOG("Not switching, already there.\n");
201         return;
202     }
203
204     workspace_reassign_sticky(workspace);
205
206     LOG("switching to %p\n", workspace);
207     Con *next = con_descend_focused(workspace);
208
209     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
210         /* check if this workspace is currently visible */
211         if (!workspace_is_visible(old)) {
212             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
213             tree_close(old, DONT_KILL_WINDOW, false);
214             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
215             changed_num_workspaces = true;
216         }
217     }
218
219     /* Memorize current output */
220     Con *old_output = con_get_output(focused);
221
222     con_focus(next);
223     workspace->fullscreen_mode = CF_OUTPUT;
224     LOG("focused now = %p / %s\n", focused, focused->name);
225
226     /* Set mouse pointer */
227     Con *new_output = con_get_output(focused);
228     if (old_output != new_output) {
229        xcb_warp_pointer_rect(conn, &next->rect);
230     }
231
232     /* Update the EWMH hints */
233     if (changed_num_workspaces)
234         ewmh_update_workarea();
235     ewmh_update_current_desktop();
236
237     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
238 }
239
240 /*
241  * Focuses the next workspace.
242  *
243  */
244 void workspace_next() {
245     Con *current = con_get_workspace(focused);
246     Con *next = NULL;
247     Con *output;
248
249     if (current->num == -1) {
250         /* If currently a named workspace, find next named workspace. */
251         next = TAILQ_NEXT(current, nodes);
252     } else {
253         /* If currently a numbered workspace, find next numbered workspace. */
254         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
255             NODES_FOREACH(output_get_content(output)) {
256                 if (child->type != CT_WORKSPACE)
257                     continue;
258                 if (child->num == -1)
259                     break;
260                 /* Need to check child against current and next because we are
261                  * traversing multiple lists and thus are not guaranteed the
262                  * relative order between the list of workspaces. */
263                 if (current->num < child->num && (!next || child->num < next->num))
264                     next = child;
265             }
266     }
267
268     /* Find next named workspace. */
269     if (!next) {
270         bool found_current = false;
271         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
272             NODES_FOREACH(output_get_content(output)) {
273                 if (child->type != CT_WORKSPACE)
274                     continue;
275                 if (child == current) {
276                     found_current = 1;
277                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
278                     next = child;
279                     goto workspace_next_show;
280                 }
281             }
282     }
283
284     /* Find first workspace. */
285     if (!next) {
286         TAILQ_FOREACH(output, &(croot->nodes_head), nodes)
287             NODES_FOREACH(output_get_content(output)) {
288                 if (child->type != CT_WORKSPACE)
289                     continue;
290                 if (!next || (child->num != -1 && child->num < next->num))
291                     next = child;
292             }
293     }
294
295 workspace_next_show:
296     workspace_show(next->name);
297 }
298
299 /*
300  * Focuses the previous workspace.
301  *
302  */
303 void workspace_prev() {
304     Con *current = con_get_workspace(focused);
305     Con *prev = NULL;
306     Con *output;
307
308     if (current->num == -1) {
309         /* If named workspace, find previous named workspace. */
310         prev = TAILQ_PREV(current, nodes_head, nodes);
311         if (prev && prev->num != -1)
312             prev = NULL;
313     } else {
314         /* If numbered workspace, find previous numbered workspace. */
315         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
316             NODES_FOREACH_REVERSE(output_get_content(output)) {
317                 if (child->type != CT_WORKSPACE || child->num == -1)
318                     continue;
319                 /* Need to check child against current and previous because we
320                  * are traversing multiple lists and thus are not guaranteed
321                  * the relative order between the list of workspaces. */
322                 if (current->num > child->num && (!prev || child->num > prev->num))
323                     prev = child;
324             }
325     }
326
327     /* Find previous named workspace. */
328     if (!prev) {
329         bool found_current = false;
330         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
331             NODES_FOREACH_REVERSE(output_get_content(output)) {
332                 if (child->type != CT_WORKSPACE)
333                     continue;
334                 if (child == current) {
335                     found_current = 1;
336                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
337                     prev = child;
338                     goto workspace_prev_show;
339                 }
340             }
341     }
342
343     /* Find last workspace. */
344     if (!prev) {
345         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes)
346             NODES_FOREACH_REVERSE(output_get_content(output)) {
347                 if (child->type != CT_WORKSPACE)
348                     continue;
349                 if (!prev || child->num > prev->num)
350                     prev = child;
351             }
352     }
353
354 workspace_prev_show:
355     workspace_show(prev->name);
356 }
357
358 static bool get_urgency_flag(Con *con) {
359     Con *child;
360     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
361         if (child->urgent || get_urgency_flag(child))
362             return true;
363
364     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
365         if (child->urgent || get_urgency_flag(child))
366             return true;
367
368     return false;
369 }
370
371 /*
372  * Goes through all clients on the given workspace and updates the workspace’s
373  * urgent flag accordingly.
374  *
375  */
376 void workspace_update_urgent_flag(Con *ws) {
377     bool old_flag = ws->urgent;
378     ws->urgent = get_urgency_flag(ws);
379     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
380
381     if (old_flag != ws->urgent)
382         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
383 }
384
385 /*
386  * 'Forces' workspace orientation by moving all cons into a new split-con with
387  * the same orientation as the workspace and then changing the workspace
388  * orientation.
389  *
390  */
391 void ws_force_orientation(Con *ws, orientation_t orientation) {
392     /* 1: create a new split container */
393     Con *split = con_new(NULL, NULL);
394     split->parent = ws;
395
396     /* 2: copy layout and orientation from workspace */
397     split->layout = ws->layout;
398     split->orientation = ws->orientation;
399
400     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
401
402     /* 3: move the existing cons of this workspace below the new con */
403     DLOG("Moving cons\n");
404     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
405         Con *child = TAILQ_FIRST(&(ws->nodes_head));
406         con_detach(child);
407         con_attach(child, split, true);
408     }
409
410     /* 4: switch workspace orientation */
411     ws->orientation = orientation;
412
413     /* 5: attach the new split container to the workspace */
414     DLOG("Attaching new split to ws\n");
415     con_attach(split, ws, false);
416
417     /* 6: fix the percentages */
418     con_fix_percent(ws);
419
420     if (old_focused)
421         con_focus(old_focused);
422 }
423
424 /*
425  * Called when a new con (with a window, not an empty or split con) should be
426  * attached to the workspace (for example when managing a new window or when
427  * moving an existing window to the workspace level).
428  *
429  * Depending on the workspace_layout setting, this function either returns the
430  * workspace itself (default layout) or creates a new stacked/tabbed con and
431  * returns that.
432  *
433  */
434 Con *workspace_attach_to(Con *ws) {
435     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
436
437     if (config.default_layout == L_DEFAULT) {
438         DLOG("Default layout, just attaching it to the workspace itself.\n");
439         return ws;
440     }
441
442     DLOG("Non-default layout, creating a new split container\n");
443     /* 1: create a new split container */
444     Con *new = con_new(NULL, NULL);
445     new->parent = ws;
446
447     /* 2: set the requested layout on the split con */
448     new->layout = config.default_layout;
449
450     /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
451      * to be set. Otherwise, this con will not be interpreted as a split
452      * container. */
453     if (config.default_orientation == NO_ORIENTATION) {
454         new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
455     } else {
456         new->orientation = config.default_orientation;
457     }
458
459     /* 4: attach the new split container to the workspace */
460     DLOG("Attaching new split %p to workspace %p\n", new, ws);
461     con_attach(new, ws, false);
462
463     return new;
464 }