]> 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 /*
414  * Focuses the next workspace on the same output.
415  *
416  */
417 Con* workspace_next_on_output() {
418     Con *current = con_get_workspace(focused);
419     Con *next = NULL;
420     Con *output  = con_get_output(focused);
421
422     if (current->num == -1) {
423         /* If currently a named workspace, find next named workspace. */
424         next = TAILQ_NEXT(current, nodes);
425     } else {
426         /* If currently a numbered workspace, find next numbered workspace. */
427         NODES_FOREACH(output_get_content(output)) {
428             if (child->type != CT_WORKSPACE)
429                 continue;
430             if (child->num == -1)
431                 break;
432             /* Need to check child against current and next because we are
433              * traversing multiple lists and thus are not guaranteed the
434              * relative order between the list of workspaces. */
435             if (current->num < child->num && (!next || child->num < next->num))
436                 next = child;
437             }
438         }
439
440     /* Find next named workspace. */
441     if (!next) {
442         bool found_current = false;
443         NODES_FOREACH(output_get_content(output)) {
444             if (child->type != CT_WORKSPACE)
445                 continue;
446             if (child == current) {
447                 found_current = 1;
448             } else if (child->num == -1 && (current->num != -1 || found_current)) {
449                 next = child;
450                 goto workspace_next_on_output_end;
451             }
452         }
453     }
454
455     /* Find first workspace. */
456     if (!next) {
457         NODES_FOREACH(output_get_content(output)) {
458             if (child->type != CT_WORKSPACE)
459                 continue;
460             if (!next || (child->num != -1 && child->num < next->num))
461                 next = child;
462         }
463     }
464 workspace_next_on_output_end:
465     return next;
466 }
467
468 /*
469  * Focuses the previous workspace on same output.
470  *
471  */
472 Con* workspace_prev_on_output() {
473     Con *current = con_get_workspace(focused);
474     Con *prev = NULL;
475     Con *output  = con_get_output(focused);
476
477     if (current->num == -1) {
478         /* If named workspace, find previous named workspace. */
479         prev = TAILQ_PREV(current, nodes_head, nodes);
480         if (prev && prev->num != -1)
481             prev = NULL;
482     } else {
483         /* If numbered workspace, find previous numbered workspace. */
484         NODES_FOREACH_REVERSE(output_get_content(output)) {
485             if (child->type != CT_WORKSPACE || child->num == -1)
486                 continue;
487              /* Need to check child against current and previous because we
488              * are traversing multiple lists and thus are not guaranteed
489              * the relative order between the list of workspaces. */
490             if (current->num > child->num && (!prev || child->num > prev->num))
491                 prev = child;
492         }
493     }
494
495     /* Find previous named workspace. */
496     if (!prev) {
497         bool found_current = false;
498         NODES_FOREACH_REVERSE(output_get_content(output)) {
499             if (child->type != CT_WORKSPACE)
500                 continue;
501             if (child == current) {
502                 found_current = true;
503             } else if (child->num == -1 && (current->num != -1 || found_current)) {
504                 prev = child;
505                 goto workspace_prev_on_output_end;
506             }
507         }
508     }
509
510     /* Find last workspace. */
511     if (!prev) {
512         NODES_FOREACH_REVERSE(output_get_content(output)) {
513             if (child->type != CT_WORKSPACE)
514                 continue;
515             if (!prev || child->num > prev->num)
516                 prev = child;
517         }
518     }
519
520 workspace_prev_on_output_end:
521     return prev;
522 }
523
524 /*
525  * Focuses the previously focused workspace.
526  *
527  */
528 void workspace_back_and_forth() {
529     if (!previous_workspace_name) {
530         DLOG("No previous workspace name set. Not switching.");
531         return;
532     }
533
534     workspace_show_by_name(previous_workspace_name);
535 }
536
537 static bool get_urgency_flag(Con *con) {
538     Con *child;
539     TAILQ_FOREACH(child, &(con->nodes_head), nodes)
540         if (child->urgent || get_urgency_flag(child))
541             return true;
542
543     TAILQ_FOREACH(child, &(con->floating_head), floating_windows)
544         if (child->urgent || get_urgency_flag(child))
545             return true;
546
547     return false;
548 }
549
550 /*
551  * Goes through all clients on the given workspace and updates the workspace’s
552  * urgent flag accordingly.
553  *
554  */
555 void workspace_update_urgent_flag(Con *ws) {
556     bool old_flag = ws->urgent;
557     ws->urgent = get_urgency_flag(ws);
558     DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
559
560     if (old_flag != ws->urgent)
561         ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"urgent\"}");
562 }
563
564 /*
565  * 'Forces' workspace orientation by moving all cons into a new split-con with
566  * the same orientation as the workspace and then changing the workspace
567  * orientation.
568  *
569  */
570 void ws_force_orientation(Con *ws, orientation_t orientation) {
571     /* 1: create a new split container */
572     Con *split = con_new(NULL, NULL);
573     split->parent = ws;
574
575     /* 2: copy layout and orientation from workspace */
576     split->layout = ws->layout;
577     split->orientation = ws->orientation;
578
579     Con *old_focused = TAILQ_FIRST(&(ws->focus_head));
580
581     /* 3: move the existing cons of this workspace below the new con */
582     DLOG("Moving cons\n");
583     while (!TAILQ_EMPTY(&(ws->nodes_head))) {
584         Con *child = TAILQ_FIRST(&(ws->nodes_head));
585         con_detach(child);
586         con_attach(child, split, true);
587     }
588
589     /* 4: switch workspace orientation */
590     ws->orientation = orientation;
591
592     /* 5: attach the new split container to the workspace */
593     DLOG("Attaching new split to ws\n");
594     con_attach(split, ws, false);
595
596     /* 6: fix the percentages */
597     con_fix_percent(ws);
598
599     if (old_focused)
600         con_focus(old_focused);
601 }
602
603 /*
604  * Called when a new con (with a window, not an empty or split con) should be
605  * attached to the workspace (for example when managing a new window or when
606  * moving an existing window to the workspace level).
607  *
608  * Depending on the workspace_layout setting, this function either returns the
609  * workspace itself (default layout) or creates a new stacked/tabbed con and
610  * returns that.
611  *
612  */
613 Con *workspace_attach_to(Con *ws) {
614     DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
615
616     if (config.default_layout == L_DEFAULT) {
617         DLOG("Default layout, just attaching it to the workspace itself.\n");
618         return ws;
619     }
620
621     DLOG("Non-default layout, creating a new split container\n");
622     /* 1: create a new split container */
623     Con *new = con_new(NULL, NULL);
624     new->parent = ws;
625
626     /* 2: set the requested layout on the split con */
627     new->layout = config.default_layout;
628
629     /* 3: While the layout is irrelevant in stacked/tabbed mode, it needs
630      * to be set. Otherwise, this con will not be interpreted as a split
631      * container. */
632     if (config.default_orientation == NO_ORIENTATION) {
633         new->orientation = (ws->rect.height > ws->rect.width) ? VERT : HORIZ;
634     } else {
635         new->orientation = config.default_orientation;
636     }
637
638     /* 4: attach the new split container to the workspace */
639     DLOG("Attaching new split %p to workspace %p\n", new, ws);
640     con_attach(new, ws, false);
641
642     return new;
643 }