]> 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-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     /* safe-guard against showing i3-internal workspaces like __i3_scratch */
189     if (workspace->name[0] == '_' && workspace->name[1] == '_')
190         return;
191
192     /* disable fullscreen for the other workspaces and get the workspace we are
193      * currently on. */
194     TAILQ_FOREACH(current, &(workspace->parent->nodes_head), nodes) {
195         if (current->fullscreen_mode == CF_OUTPUT)
196             old = current;
197         current->fullscreen_mode = CF_NONE;
198     }
199
200     /* enable fullscreen for the target workspace. If it happens to be the
201      * same one we are currently on anyways, we can stop here. */
202     workspace->fullscreen_mode = CF_OUTPUT;
203     current = con_get_workspace(focused);
204     if (workspace == current) {
205         DLOG("Not switching, already there.\n");
206         return;
207     }
208
209     /* Remember currently focused workspace for switching back to it later with
210      * the 'workspace back_and_forth' command.
211      * NOTE: We have to duplicate the name as the original will be freed when
212      * the corresponding workspace is cleaned up. */
213
214     FREE(previous_workspace_name);
215     if (current)
216         previous_workspace_name = sstrdup(current->name);
217
218     workspace_reassign_sticky(workspace);
219
220     LOG("switching to %p\n", workspace);
221     Con *next = con_descend_focused(workspace);
222
223     if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
224         /* check if this workspace is currently visible */
225         if (!workspace_is_visible(old)) {
226             LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
227             tree_close(old, DONT_KILL_WINDOW, false, false);
228             ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
229             changed_num_workspaces = true;
230         }
231     }
232
233     /* Memorize current output */
234     Con *old_output = con_get_output(focused);
235
236     con_focus(next);
237     workspace->fullscreen_mode = CF_OUTPUT;
238     LOG("focused now = %p / %s\n", focused, focused->name);
239
240     /* Set mouse pointer */
241     Con *new_output = con_get_output(focused);
242     if (old_output != new_output) {
243         x_set_warp_to(&next->rect);
244     }
245
246     /* Update the EWMH hints */
247     ewmh_update_current_desktop();
248
249     ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"focus\"}");
250 }
251
252 /*
253  * Switches to the given workspace
254  *
255  */
256 void workspace_show(Con *workspace) {
257     _workspace_show(workspace, false);
258 }
259
260 /*
261  * Looks up the workspace by name and switches to it.
262  *
263  */
264 void workspace_show_by_name(const char *num) {
265     Con *workspace;
266     bool changed_num_workspaces;
267     workspace = workspace_get(num, &changed_num_workspaces);
268     _workspace_show(workspace, changed_num_workspaces);
269 }
270
271 /*
272  * Focuses the next workspace.
273  *
274  */
275 Con* workspace_next() {
276     Con *current = con_get_workspace(focused);
277     Con *next = NULL;
278     Con *output;
279
280     if (current->num == -1) {
281         /* If currently a named workspace, find next named workspace. */
282         next = TAILQ_NEXT(current, nodes);
283     } else {
284         /* If currently a numbered workspace, find next numbered workspace. */
285         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
286             /* Skip outputs starting with __, they are internal. */
287             if (output->name[0] == '_' && output->name[1] == '_')
288                 continue;
289             NODES_FOREACH(output_get_content(output)) {
290                 if (child->type != CT_WORKSPACE)
291                     continue;
292                 if (child->num == -1)
293                     break;
294                 /* Need to check child against current and next because we are
295                  * traversing multiple lists and thus are not guaranteed the
296                  * relative order between the list of workspaces. */
297                 if (current->num < child->num && (!next || child->num < next->num))
298                     next = child;
299             }
300         }
301     }
302
303     /* Find next named workspace. */
304     if (!next) {
305         bool found_current = false;
306         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
307             /* Skip outputs starting with __, they are internal. */
308             if (output->name[0] == '_' && output->name[1] == '_')
309                 continue;
310             NODES_FOREACH(output_get_content(output)) {
311                 if (child->type != CT_WORKSPACE)
312                     continue;
313                 if (child == current) {
314                     found_current = 1;
315                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
316                     next = child;
317                     goto workspace_next_end;
318                 }
319             }
320         }
321     }
322
323     /* Find first workspace. */
324     if (!next) {
325         TAILQ_FOREACH(output, &(croot->nodes_head), nodes) {
326             /* Skip outputs starting with __, they are internal. */
327             if (output->name[0] == '_' && output->name[1] == '_')
328                 continue;
329             NODES_FOREACH(output_get_content(output)) {
330                 if (child->type != CT_WORKSPACE)
331                     continue;
332                 if (!next || (child->num != -1 && child->num < next->num))
333                     next = child;
334             }
335         }
336     }
337 workspace_next_end:
338     return next;
339 }
340
341 /*
342  * Focuses the previous workspace.
343  *
344  */
345 Con* workspace_prev() {
346     Con *current = con_get_workspace(focused);
347     Con *prev = NULL;
348     Con *output;
349
350     if (current->num == -1) {
351         /* If named workspace, find previous named workspace. */
352         prev = TAILQ_PREV(current, nodes_head, nodes);
353         if (prev && prev->num != -1)
354             prev = NULL;
355     } else {
356         /* If numbered workspace, find previous numbered workspace. */
357         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
358             /* Skip outputs starting with __, they are internal. */
359             if (output->name[0] == '_' && output->name[1] == '_')
360                 continue;
361             NODES_FOREACH_REVERSE(output_get_content(output)) {
362                 if (child->type != CT_WORKSPACE || child->num == -1)
363                     continue;
364                 /* Need to check child against current and previous because we
365                  * are traversing multiple lists and thus are not guaranteed
366                  * the relative order between the list of workspaces. */
367                 if (current->num > child->num && (!prev || child->num > prev->num))
368                     prev = child;
369             }
370         }
371     }
372
373     /* Find previous named workspace. */
374     if (!prev) {
375         bool found_current = false;
376         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
377             /* Skip outputs starting with __, they are internal. */
378             if (output->name[0] == '_' && output->name[1] == '_')
379                 continue;
380             NODES_FOREACH_REVERSE(output_get_content(output)) {
381                 if (child->type != CT_WORKSPACE)
382                     continue;
383                 if (child == current) {
384                     found_current = true;
385                 } else if (child->num == -1 && (current->num != -1 || found_current)) {
386                     prev = child;
387                     goto workspace_prev_end;
388                 }
389             }
390         }
391     }
392
393     /* Find last workspace. */
394     if (!prev) {
395         TAILQ_FOREACH_REVERSE(output, &(croot->nodes_head), nodes_head, nodes) {
396             /* Skip outputs starting with __, they are internal. */
397             if (output->name[0] == '_' && output->name[1] == '_')
398                 continue;
399             NODES_FOREACH_REVERSE(output_get_content(output)) {
400                 if (child->type != CT_WORKSPACE)
401                     continue;
402                 if (!prev || child->num > prev->num)
403                     prev = child;
404             }
405         }
406     }
407
408 workspace_prev_end:
409     return prev;
410 }
411
412 /*
413  * Focuses the previously focused workspace.
414  *
415  */
416 void workspace_back_and_forth() {
417     if (!previous_workspace_name) {
418         DLOG("No previous workspace name set. Not switching.");
419         return;
420     }
421
422     workspace_show_by_name(previous_workspace_name);
423 }
424
425 static bool get_urgency_flag(Con *con) {
426     Con *child;
427     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
428         if (child->urgent || get_urgency_flag(child))
429             return true;
430
431     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
432         if (child->urgent || get_urgency_flag(child))
433             return true;
434
435     return false;
436 }
437
438 /*
439  * Goes through all clients on the given workspace and updates the workspace’s
440  * urgent flag accordingly.
441  *
442  */
443 void workspace_update_urgent_flag(Con *ws) {
444     bool old_flag = ws->urgent;
445     ws->urgent = get_urgency_flag(ws);
446     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
447
448     if (old_flag != ws->urgent)
449         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
450 }
451
452 /*
453  * 'Forces' workspace orientation by moving all cons into a new split-con with
454  * the same orientation as the workspace and then changing the workspace
455  * orientation.
456  *
457  */
458 void ws_force_orientation(Con *ws, orientation_t orientation) {
459     /* 1: create a new split container */
460     Con *split = con_new(NULL, NULL);
461     split->parent = ws;
462
463     /* 2: copy layout and orientation from workspace */
464     split->layout = ws->layout;
465     split->orientation = ws->orientation;
466
467     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
468
469     /* 3: move the existing cons of this workspace below the new con */
470     DLOG("Moving cons\n");
471     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
472         Con *child = TAILQ_FIRST(&(ws->nodes_head));
473         con_detach(child);
474         con_attach(child, split, true);
475     }
476
477     /* 4: switch workspace orientation */
478     ws->orientation = orientation;
479
480     /* 5: attach the new split container to the workspace */
481     DLOG("Attaching new split to ws\n");
482     con_attach(split, ws, false);
483
484     /* 6: fix the percentages */
485     con_fix_percent(ws);
486
487     if (old_focused)
488         con_focus(old_focused);
489 }
490
491 /*
492  * Called when a new con (with a window, not an empty or split con) should be
493  * attached to the workspace (for example when managing a new window or when
494  * moving an existing window to the workspace level).
495  *
496  * Depending on the workspace_layout setting, this function either returns the
497  * workspace itself (default layout) or creates a new stacked/tabbed con and
498  * returns that.
499  *
500  */
501 Con *workspace_attach_to(Con *ws) {
502     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
503
504     if (config.default_layout == L_DEFAULT) {
505         DLOG("Default layout, just attaching it to the workspace itself.\n");
506         return ws;
507     }
508
509     DLOG("Non-default layout, creating a new split container\n");
510     /* 1: create a new split container */
511     Con *new = con_new(NULL, NULL);
512     new->parent = ws;
513
514     /* 2: set the requested layout on the split con */
515     new->layout = config.default_layout;
516
517     /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
518      * to be set. Otherwise, this con will not be interpreted as a split
519      * container. */
520     if (config.default_orientation == NO_ORIENTATION) {
521         new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
522     } else {
523         new->orientation = config.default_orientation;
524     }
525
526     /* 4: attach the new split container to the workspace */
527     DLOG("Attaching new split %p to workspace %p\n", new, ws);
528     con_attach(new, ws, false);
529
530     return new;
531 }