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