i3
workspace.c
Go to the documentation of this file.
1/*
2 * vim:ts=4:sw=4:expandtab
3 *
4 * i3 - an improved tiling window manager
5 * © 2009 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#include "yajl_utils.h"
13
14/*
15 * Stores a copy of the name of the last used workspace for the workspace
16 * back-and-forth switching.
17 *
18 */
20
21/* NULL-terminated list of workspace names (in order) extracted from
22 * keybindings. */
23static char **binding_workspace_names = NULL;
24
25/*
26 * Returns the workspace with the given name or NULL if such a workspace does
27 * not exist.
28 *
29 */
31 Con *output, *workspace = NULL;
32 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
33 GREP_FIRST(workspace, output_get_content(output), !strcasecmp(child->name, name));
34 }
35
36 return workspace;
37}
38
39/*
40 * Returns the workspace with the given number or NULL if such a workspace does
41 * not exist.
42 *
43 */
45 Con *output, *workspace = NULL;
46 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
47 GREP_FIRST(workspace, output_get_content(output), child->num == num);
48 }
49
50 return workspace;
51}
52
53/*
54 * Sets ws->layout to splith/splitv if default_orientation was specified in the
55 * configfile. Otherwise, it uses splith/splitv depending on whether the output
56 * is higher than wide.
57 *
58 */
60 /* If default_orientation is set to NO_ORIENTATION we determine
61 * orientation depending on output resolution. */
63 Con *output = con_get_output(ws);
64 ws->layout = (output->rect.height > output->rect.width) ? L_SPLITV : L_SPLITH;
65 ws->rect = output->rect;
66 DLOG("Auto orientation. Workspace size set to (%d,%d), setting layout to %d.\n",
67 output->rect.width, output->rect.height, ws->layout);
68 } else {
70 }
71}
72
73/*
74 * Returns the first output that is assigned to a workspace specified by the
75 * given name or number. Returns NULL if no such output exists.
76 *
77 * If an assignment matches by number but there is an assignment later that
78 * matches by name, the second one is preferred.
79 * The order of the 'ws_assignments' queue is respected: if multiple
80 * assignments match the criteria, the first one is returned.
81 * 'name' is ignored when NULL, 'parsed_num' is ignored when it is -1.
82 *
83 */
84Con *get_assigned_output(const char *name, long parsed_num) {
85 Con *output = NULL;
86 struct Workspace_Assignment *assignment;
88 if (assignment->output == NULL) {
89 continue;
90 }
91
92 if (name && strcmp(assignment->name, name) == 0) {
93 DLOG("Found workspace name=\"%s\" assignment to output \"%s\"\n",
94 name, assignment->output);
95 Output *assigned_by_name = get_output_by_name(assignment->output, true);
96 if (assigned_by_name) {
97 /* When the name matches exactly, skip numbered assignments. */
98 return assigned_by_name->con;
99 }
100 } else if (!output && /* Only keep the first numbered assignment. */
101 parsed_num != -1 &&
102 name_is_digits(assignment->name) &&
103 ws_name_to_number(assignment->name) == parsed_num) {
104 DLOG("Found workspace number=%ld assignment to output \"%s\"\n",
105 parsed_num, assignment->output);
106 Output *assigned_by_num = get_output_by_name(assignment->output, true);
107 if (assigned_by_num) {
108 output = assigned_by_num->con;
109 }
110 }
111 }
112
113 return output;
114}
115
116/*
117 * Returns true if the first output assigned to a workspace with the given
118 * workspace assignment is the same as the given output.
119 */
121 Con *assigned = get_assigned_output(assignment->name, -1);
122 return assigned && assigned == output->con;
123}
124
125/*
126 * Returns a pointer to the workspace with the given number (starting at 0),
127 * creating the workspace if necessary (by allocating the necessary amount of
128 * memory and initializing the data structures correctly).
129 *
130 */
131Con *workspace_get(const char *num) {
132 Con *workspace = get_existing_workspace_by_name(num);
133 if (workspace) {
134 return workspace;
135 }
136
137 LOG("Creating new workspace \"%s\"\n", num);
138
139 /* We set workspace->num to the number if this workspace’s name begins with
140 * a positive number. Otherwise it’s a named ws and num will be 1. */
141 const int parsed_num = ws_name_to_number(num);
142
143 Con *output = get_assigned_output(num, parsed_num);
144 /* if an assignment is not found, we create this workspace on the current output */
145 if (!output) {
147 }
148
149 /* No parent because we need to attach this container after setting its
150 * type. con_attach will handle CT_WORKSPACEs differently. */
151 workspace = con_new(NULL, NULL);
152
153 char *name;
154 sasprintf(&name, "[i3 con] workspace %s", num);
155 x_set_name(workspace, name);
156 free(name);
157
158 FREE(workspace->name);
159 workspace->name = sstrdup(num);
161 workspace->num = parsed_num;
162 workspace->type = CT_WORKSPACE;
163 workspace->gaps = gaps_for_workspace(workspace);
164
165 con_attach(workspace, output_get_content(output), false);
167
168 ipc_send_workspace_event("init", workspace, NULL);
170
171 return workspace;
172}
173
174/*
175 * Extracts workspace names from keybindings (e.g. “web” from “bindsym $mod+1
176 * workspace web”), so that when an output needs a workspace, i3 can start with
177 * the first configured one. Needs to be called before reorder_bindings() so
178 * that the config-file order is used, not the i3-internal order.
179 *
180 */
182 Binding *bind;
183 int n = 0;
184 if (binding_workspace_names != NULL) {
185 for (int i = 0; binding_workspace_names[i] != NULL; i++) {
187 }
189 }
191 DLOG("binding with command %s\n", bind->command);
192 if (strlen(bind->command) < strlen("workspace ") ||
193 strncasecmp(bind->command, "workspace", strlen("workspace")) != 0) {
194 continue;
195 }
196 DLOG("relevant command = %s\n", bind->command);
197 const char *target = bind->command + strlen("workspace ");
198 while (*target == ' ' || *target == '\t') {
199 target++;
200 }
201 /* We check if this is the workspace
202 * next/prev/next_on_output/prev_on_output/back_and_forth command.
203 * Beware: The workspace names "next", "prev", "next_on_output",
204 * "prev_on_output", "back_and_forth" and "current" are OK,
205 * so we check before stripping the double quotes */
206 if (strncasecmp(target, "next", strlen("next")) == 0 ||
207 strncasecmp(target, "prev", strlen("prev")) == 0 ||
208 strncasecmp(target, "next_on_output", strlen("next_on_output")) == 0 ||
209 strncasecmp(target, "prev_on_output", strlen("prev_on_output")) == 0 ||
210 strncasecmp(target, "back_and_forth", strlen("back_and_forth")) == 0 ||
211 strncasecmp(target, "current", strlen("current")) == 0) {
212 continue;
213 }
214 if (strncasecmp(target, "--no-auto-back-and-forth", strlen("--no-auto-back-and-forth")) == 0) {
215 target += strlen("--no-auto-back-and-forth");
216 while (*target == ' ' || *target == '\t') {
217 target++;
218 }
219 }
220 if (strncasecmp(target, "number", strlen("number")) == 0) {
221 target += strlen("number");
222 while (*target == ' ' || *target == '\t') {
223 target++;
224 }
225 }
226 char *target_name = parse_string(&target, false);
227 if (target_name == NULL) {
228 continue;
229 }
230 if (strncasecmp(target_name, "__", strlen("__")) == 0) {
231 LOG("Cannot create workspace \"%s\". Names starting with __ are i3-internal.\n", target);
232 free(target_name);
233 continue;
234 }
235 DLOG("Saving workspace name \"%s\"\n", target_name);
236
238 binding_workspace_names[n - 1] = target_name;
239 }
241 binding_workspace_names[n - 1] = NULL;
242}
243
244/*
245 * Returns a pointer to a new workspace in the given output. The workspace
246 * is created attached to the tree hierarchy through the given content
247 * container.
248 *
249 */
251 /* add a workspace to this output */
252 bool exists = true;
253 Con *ws = con_new(NULL, NULL);
254 ws->type = CT_WORKSPACE;
255
256 /* try the configured workspace bindings first to find a free name */
257 for (int n = 0; binding_workspace_names[n] != NULL; n++) {
258 char *target_name = binding_workspace_names[n];
259 /* Ensure that this workspace is not assigned to a different output —
260 * otherwise we would create it, then move it over to its output, then
261 * find a new workspace, etc… */
262 Con *assigned = get_assigned_output(target_name, -1);
263 if (assigned && assigned != output->con) {
264 continue;
265 }
266
267 const int num = ws_name_to_number(target_name);
268 exists = (num == -1)
269 ? get_existing_workspace_by_name(target_name)
271 if (!exists) {
272 ws->name = sstrdup(target_name);
273 /* Set ->num to the number of the workspace, if the name actually
274 * is a number or starts with a number */
275 ws->num = num;
276 DLOG("Used number %d for workspace with name %s\n", ws->num, ws->name);
277
278 break;
279 }
280 }
281
282 if (exists) {
283 /* get the next unused workspace number */
284 DLOG("Getting next unused workspace by number\n");
285 int c = 0;
286 while (exists) {
287 c++;
288 Con *assigned = get_assigned_output(NULL, c);
289 exists = (get_existing_workspace_by_num(c) || (assigned && assigned != output->con));
290 DLOG("result for ws %d: exists = %d\n", c, exists);
291 }
292 ws->num = c;
293 sasprintf(&(ws->name), "%d", c);
294 }
295
296 con_attach(ws, content, false);
297
298 char *name;
299 sasprintf(&name, "[i3 con] workspace %s", ws->name);
300 x_set_name(ws, name);
301 free(name);
302
303 ws->gaps = gaps_for_workspace(ws);
304
306
309
310 ipc_send_workspace_event("init", ws, NULL);
311 return ws;
312}
313
314/*
315 * Returns true if the workspace is currently visible. Especially important for
316 * multi-monitor environments, as they can have multiple currently active
317 * workspaces.
318 *
319 */
322 if (output == NULL) {
323 return false;
324 }
326 return (fs == ws);
327}
328
329/*
330 * XXX: we need to clean up all this recursive walking code.
331 *
332 */
333static Con *_get_sticky(Con *con, const char *sticky_group, Con *exclude) {
334 Con *current;
335
336 TAILQ_FOREACH (current, &(con->nodes_head), nodes) {
337 if (current != exclude &&
338 current->sticky_group != NULL &&
339 current->window != NULL &&
340 strcmp(current->sticky_group, sticky_group) == 0) {
341 return current;
342 }
343
344 Con *recurse = _get_sticky(current, sticky_group, exclude);
345 if (recurse != NULL) {
346 return recurse;
347 }
348 }
349
350 TAILQ_FOREACH (current, &(con->floating_head), floating_windows) {
351 if (current != exclude &&
352 current->sticky_group != NULL &&
353 current->window != NULL &&
354 strcmp(current->sticky_group, sticky_group) == 0) {
355 return current;
356 }
357
358 Con *recurse = _get_sticky(current, sticky_group, exclude);
359 if (recurse != NULL) {
360 return recurse;
361 }
362 }
363
364 return NULL;
365}
366
367/*
368 * Reassigns all child windows in sticky containers. Called when the user
369 * changes workspaces.
370 *
371 * XXX: what about sticky containers which contain containers?
372 *
373 */
375 Con *current;
376 /* 1: go through all containers */
377
378 /* handle all children and floating windows of this node */
379 TAILQ_FOREACH (current, &(con->nodes_head), nodes) {
380 if (current->sticky_group == NULL) {
382 continue;
383 }
384
385 LOG("Ah, this one is sticky: %s / %p\n", current->name, current);
386 /* 2: find a window which we can re-assign */
387 Con *output = con_get_output(current);
388 Con *src = _get_sticky(output, current->sticky_group, current);
389
390 if (src == NULL) {
391 LOG("No window found for this sticky group\n");
393 continue;
394 }
395
396 x_move_win(src, current);
397 current->window = src->window;
398 current->mapped = true;
399 src->window = NULL;
400 src->mapped = false;
401
402 x_reparent_child(current, src);
403
404 LOG("re-assigned window from src %p to dest %p\n", src, current);
405 }
406
407 TAILQ_FOREACH (current, &(con->floating_head), floating_windows) {
409 }
410}
411
412/*
413 * Callback to reset the urgent flag of the given con to false. May be started by
414 * workspace_show to avoid urgency hints being lost by switching to a workspace
415 * focusing the con.
416 *
417 */
418static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents) {
419 Con *con = w->data;
420
421 ev_timer_stop(main_loop, con->urgency_timer);
422 FREE(con->urgency_timer);
423
424 if (con->urgent) {
425 DLOG("Resetting urgency flag of con %p by timer\n", con);
426 con_set_urgency(con, false);
429 ipc_send_window_event("urgent", con);
430 tree_render();
431 }
432}
433
434/*
435 * Switches to the given workspace
436 *
437 */
438void workspace_show(Con *workspace) {
439 Con *current, *old = NULL;
440
441 /* safe-guard against showing i3-internal workspaces like __i3_scratch */
442 if (con_is_internal(workspace)) {
443 return;
444 }
445
446 /* disable fullscreen for the other workspaces and get the workspace we are
447 * currently on. */
448 TAILQ_FOREACH (current, &(workspace->parent->nodes_head), nodes) {
449 if (current->fullscreen_mode == CF_OUTPUT) {
450 old = current;
451 }
452 current->fullscreen_mode = CF_NONE;
453 }
454
455 /* enable fullscreen for the target workspace. If it happens to be the
456 * same one we are currently on anyways, we can stop here. */
457 workspace->fullscreen_mode = CF_OUTPUT;
458 current = con_get_workspace(focused);
459 if (workspace == current) {
460 DLOG("Not switching, already there.\n");
461 return;
462 }
463
464 /* Used to correctly update focus when pushing sticky windows. Holds the
465 * previously focused container in the same output as workspace. For
466 * example, if a sticky window is focused and then we switch focus to a
467 * workspace in another output and then switch to a third workspace in the
468 * first output, the sticky window needs to be refocused. */
469 Con *old_focus = old ? con_descend_focused(old) : NULL;
470
471 /* Remember currently focused workspace for switching back to it later with
472 * the 'workspace back_and_forth' command.
473 * NOTE: We have to duplicate the name as the original will be freed when
474 * the corresponding workspace is cleaned up.
475 * NOTE: Internal cons such as __i3_scratch (when a scratchpad window is
476 * focused) are skipped, see bug #868. */
477 if (current && !con_is_internal(current)) {
480 DLOG("Setting previous_workspace_name = %s\n", previous_workspace_name);
481 }
482
483 workspace_reassign_sticky(workspace);
484
485 DLOG("switching to %p / %s\n", workspace, workspace->name);
486 Con *next = con_descend_focused(workspace);
487
488 /* Memorize current output */
489 Con *old_output = con_get_output(focused);
490
491 /* Display urgency hint for a while if the newly visible workspace would
492 * focus and thereby immediately destroy it */
493 if (next->urgent && (int)(config.workspace_urgency_timer * 1000) > 0) {
494 /* focus for now… */
495 next->urgent = false;
496 con_focus(next);
497
498 /* … but immediately reset urgency flags; they will be set to false by
499 * the timer callback in case the container is focused at the time of
500 * its expiration */
501 focused->urgent = true;
502 workspace->urgent = true;
503
504 if (focused->urgency_timer == NULL) {
505 DLOG("Deferring reset of urgency flag of con %p on newly shown workspace %p\n",
506 focused, workspace);
507 focused->urgency_timer = scalloc(1, sizeof(struct ev_timer));
508 /* use a repeating timer to allow for easy resets */
512 ev_timer_start(main_loop, focused->urgency_timer);
513 } else {
514 DLOG("Resetting urgency timer of con %p on workspace %p\n",
515 focused, workspace);
516 ev_timer_again(main_loop, focused->urgency_timer);
517 }
518 } else {
519 con_focus(next);
520 }
521
522 ipc_send_workspace_event("focus", workspace, current);
523
524 DLOG("old = %p / %s\n", old, (old ? old->name : "(null)"));
525 /* Close old workspace if necessary. This must be done *after* doing
526 * urgency handling, because tree_close_internal() will do a con_focus() on the next
527 * client, which will clear the urgency flag too early. Also, there is no
528 * way for con_focus() to know about when to clear urgency immediately and
529 * when to defer it. */
530 if (old && TAILQ_EMPTY(&(old->nodes_head)) && TAILQ_EMPTY(&(old->floating_head))) {
531 /* check if this workspace is currently visible */
532 if (!workspace_is_visible(old)) {
533 LOG("Closing old workspace (%p / %s), it is empty\n", old, old->name);
534 yajl_gen gen = ipc_marshal_workspace_event("empty", old, NULL);
536
537 const unsigned char *payload;
538 ylength length;
539 y(get_buf, &payload, &length);
540 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
541
542 y(free);
543
544 /* Avoid calling output_push_sticky_windows later with a freed container. */
545 if (old == old_focus) {
546 old_focus = NULL;
547 }
548
550 }
551 }
552
553 workspace->fullscreen_mode = CF_OUTPUT;
554 LOG("focused now = %p / %s\n", focused, focused->name);
555
556 /* Set mouse pointer */
557 Con *new_output = con_get_output(focused);
558 if (old_output != new_output) {
559 x_set_warp_to(&next->rect);
560 }
561
562 /* Update the EWMH hints */
564
565 /* Push any sticky windows to the now visible workspace. */
567}
568
569/*
570 * Looks up the workspace by name and switches to it.
571 *
572 */
573void workspace_show_by_name(const char *num) {
575}
576
577/*
578 * Focuses the next workspace.
579 *
580 */
582 Con *current = con_get_workspace(focused);
583 Con *next = NULL, *first = NULL, *first_opposite = NULL;
584 Con *output;
585
586 if (current->num == -1) {
587 /* If currently a named workspace, find next named workspace. */
588 if ((next = TAILQ_NEXT(current, nodes)) != NULL) {
589 return next;
590 }
591 bool found_current = false;
592 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
593 /* Skip outputs starting with __, they are internal. */
594 if (con_is_internal(output)) {
595 continue;
596 }
598 if (child->type != CT_WORKSPACE) {
599 continue;
600 }
601 if (!first) {
602 first = child;
603 }
604 if (!first_opposite || (child->num != -1 && child->num < first_opposite->num)) {
605 first_opposite = child;
606 }
607 if (child == current) {
608 found_current = true;
609 } else if (child->num == -1 && found_current) {
610 next = child;
611 return next;
612 }
613 }
614 }
615 } else {
616 /* If currently a numbered workspace, find next numbered workspace. */
617 bool found_current = false;
618 TAILQ_FOREACH (output, &(croot->nodes_head), nodes) {
619 /* Skip outputs starting with __, they are internal. */
620 if (con_is_internal(output)) {
621 continue;
622 }
624 if (child->type != CT_WORKSPACE) {
625 continue;
626 }
627 if (!first || (child->num != -1 && child->num < first->num)) {
628 first = child;
629 }
630 if (!first_opposite && child->num == -1) {
631 first_opposite = child;
632 }
633 if (child->num == -1) {
634 break;
635 }
636 /* Need to check child against current and next because we are
637 * traversing multiple lists and thus are not guaranteed the
638 * relative order between the list of workspaces. */
639 if (current->num < child->num && (!next || child->num < next->num)) {
640 next = child;
641 }
642
643 /* If two workspaces have the same number, but different names
644 * (eg '5:a', '5:b') then just take the next one. */
645 if (child == current) {
646 found_current = true;
647 } else if (found_current && current->num == child->num) {
648 return child;
649 }
650 }
651 }
652 }
653
654 if (!next) {
655 next = first_opposite ? first_opposite : first;
656 }
657
658 return next;
659}
660
661/*
662 * Focuses the previous workspace.
663 *
664 */
666 Con *current = con_get_workspace(focused);
667 Con *prev = NULL, *first_opposite = NULL, *last = NULL;
668 Con *output;
669
670 if (current->num == -1) {
671 /* If named workspace, find previous named workspace. */
672 prev = TAILQ_PREV(current, nodes_head, nodes);
673 if (prev && prev->num != -1) {
674 prev = NULL;
675 }
676 if (!prev) {
677 bool found_current = false;
678 TAILQ_FOREACH_REVERSE (output, &(croot->nodes_head), nodes_head, nodes) {
679 /* Skip outputs starting with __, they are internal. */
680 if (con_is_internal(output)) {
681 continue;
682 }
684 if (child->type != CT_WORKSPACE) {
685 continue;
686 }
687 if (!last) {
688 last = child;
689 }
690 if (!first_opposite || (child->num != -1 && child->num > first_opposite->num)) {
691 first_opposite = child;
692 }
693 if (child == current) {
694 found_current = true;
695 } else if (child->num == -1 && found_current) {
696 prev = child;
697 return prev;
698 }
699 }
700 }
701 }
702 } else {
703 /* If numbered workspace, find previous numbered workspace. */
704 bool found_current = false;
705 TAILQ_FOREACH_REVERSE (output, &(croot->nodes_head), nodes_head, nodes) {
706 /* Skip outputs starting with __, they are internal. */
707 if (con_is_internal(output)) {
708 continue;
709 }
711 if (child->type != CT_WORKSPACE) {
712 continue;
713 }
714 if (!last || (child->num != -1 && last->num < child->num)) {
715 last = child;
716 }
717 if (!first_opposite && child->num == -1) {
718 first_opposite = child;
719 }
720 if (child->num == -1) {
721 continue;
722 }
723 /* Need to check child against current and previous because we
724 * are traversing multiple lists and thus are not guaranteed
725 * the relative order between the list of workspaces. */
726 if (current->num > child->num && (!prev || child->num > prev->num)) {
727 prev = child;
728 }
729
730 /* If two workspaces have the same number, but different names
731 * (eg '5:a', '5:b') then just take the previous one. */
732 if (child == current) {
733 found_current = true;
734 } else if (found_current && current->num == child->num) {
735 return child;
736 }
737 }
738 }
739 }
740
741 if (!prev) {
742 prev = first_opposite ? first_opposite : last;
743 }
744
745 return prev;
746}
747
748/*
749 * Focuses the next workspace on the same output.
750 *
751 */
753 Con *current = con_get_workspace(focused);
754 Con *next = NULL;
756
757 if (current->num == -1) {
758 /* If currently a named workspace, find next named workspace. */
759 next = TAILQ_NEXT(current, nodes);
760 } else {
761 /* If currently a numbered workspace, find next numbered workspace. */
762 bool found_current = false;
764 if (child->type != CT_WORKSPACE) {
765 continue;
766 }
767 if (child->num == -1) {
768 break;
769 }
770 /* Need to check child against current and next because we are
771 * traversing multiple lists and thus are not guaranteed the
772 * relative order between the list of workspaces. */
773 if (current->num < child->num && (!next || child->num < next->num)) {
774 next = child;
775 }
776
777 /* If two workspaces have the same number, but different names
778 * (eg '5:a', '5:b') then just take the next one. */
779 if (child == current) {
780 found_current = true;
781 } else if (found_current && current->num == child->num) {
782 return child;
783 }
784 }
785 }
786
787 /* Find next named workspace. */
788 if (!next) {
789 bool found_current = false;
791 if (child->type != CT_WORKSPACE) {
792 continue;
793 }
794 if (child == current) {
795 found_current = true;
796 } else if (child->num == -1 && (current->num != -1 || found_current)) {
797 next = child;
798 goto workspace_next_on_output_end;
799 }
800 }
801 }
802
803 /* Find first workspace. */
804 if (!next) {
806 if (child->type != CT_WORKSPACE) {
807 continue;
808 }
809 if (!next || (child->num != -1 && child->num < next->num)) {
810 next = child;
811 }
812 }
813 }
814workspace_next_on_output_end:
815 return next;
816}
817
818/*
819 * Focuses the previous workspace on same output.
820 *
821 */
823 Con *current = con_get_workspace(focused);
824 Con *prev = NULL;
826 DLOG("output = %s\n", output->name);
827
828 if (current->num == -1) {
829 /* If named workspace, find previous named workspace. */
830 prev = TAILQ_PREV(current, nodes_head, nodes);
831 if (prev && prev->num != -1) {
832 prev = NULL;
833 }
834 } else {
835 /* If numbered workspace, find previous numbered workspace. */
836 bool found_current = false;
838 if (child->type != CT_WORKSPACE || child->num == -1) {
839 continue;
840 }
841 /* Need to check child against current and previous because we
842 * are traversing multiple lists and thus are not guaranteed
843 * the relative order between the list of workspaces. */
844 if (current->num > child->num && (!prev || child->num > prev->num)) {
845 prev = child;
846 }
847
848 /* If two workspaces have the same number, but different names
849 * (eg '5:a', '5:b') then just take the previous one. */
850 if (child == current) {
851 found_current = true;
852 } else if (found_current && current->num == child->num) {
853 return child;
854 }
855 }
856 }
857
858 /* Find previous named workspace. */
859 if (!prev) {
860 bool found_current = false;
862 if (child->type != CT_WORKSPACE) {
863 continue;
864 }
865 if (child == current) {
866 found_current = true;
867 } else if (child->num == -1 && (current->num != -1 || found_current)) {
868 prev = child;
869 goto workspace_prev_on_output_end;
870 }
871 }
872 }
873
874 /* Find last workspace. */
875 if (!prev) {
877 if (child->type != CT_WORKSPACE) {
878 continue;
879 }
880 if (!prev || child->num > prev->num) {
881 prev = child;
882 }
883 }
884 }
885
886workspace_prev_on_output_end:
887 return prev;
888}
889
890/*
891 * Focuses the previously focused workspace.
892 *
893 */
896 DLOG("No previous workspace name set. Not switching.\n");
897 return;
898 }
899
901}
902
903/*
904 * Returns the previously focused workspace con, or NULL if unavailable.
905 *
906 */
909 DLOG("No previous workspace name set.\n");
910 return NULL;
911 }
912
914}
915
916static bool get_urgency_flag(Con *con) {
917 Con *child;
918 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
919 if (child->urgent || get_urgency_flag(child)) {
920 return true;
921 }
922 }
923
924 TAILQ_FOREACH (child, &(con->floating_head), floating_windows) {
925 if (child->urgent || get_urgency_flag(child)) {
926 return true;
927 }
928 }
929
930 return false;
931}
932
933/*
934 * Goes through all clients on the given workspace and updates the workspace’s
935 * urgent flag accordingly.
936 *
937 */
939 bool old_flag = ws->urgent;
940 ws->urgent = get_urgency_flag(ws);
941 DLOG("Workspace urgency flag changed from %d to %d\n", old_flag, ws->urgent);
942
943 if (old_flag != ws->urgent) {
944 ipc_send_workspace_event("urgent", ws, NULL);
945 }
946}
947
948/*
949 * 'Forces' workspace orientation by moving all cons into a new split-con with
950 * the same layout as the workspace and then changing the workspace layout.
951 *
952 */
953void ws_force_orientation(Con *ws, orientation_t orientation) {
954 /* 1: create a new split container */
955 Con *split = con_new(NULL, NULL);
956 split->parent = ws;
957
958 /* 2: copy layout from workspace */
959 split->layout = ws->layout;
960
961 /* 3: move the existing cons of this workspace below the new con */
962 Con **focus_order = get_focus_order(ws);
963
964 DLOG("Moving cons\n");
965 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
966 Con *child = TAILQ_FIRST(&(ws->nodes_head));
967 con_detach(child);
968 con_attach(child, split, true);
969 }
970
971 set_focus_order(split, focus_order);
972 free(focus_order);
973
974 /* 4: switch workspace layout */
975 ws->layout = (orientation == HORIZ) ? L_SPLITH : L_SPLITV;
976 DLOG("split->layout = %d, ws->layout = %d\n", split->layout, ws->layout);
977
978 /* 5: attach the new split container to the workspace */
979 DLOG("Attaching new split (%p) to ws (%p)\n", split, ws);
980 con_attach(split, ws, false);
981
982 /* 6: fix the percentages */
983 con_fix_percent(ws);
984}
985
986/*
987 * Called when a new con (with a window, not an empty or split con) should be
988 * attached to the workspace (for example when managing a new window or when
989 * moving an existing window to the workspace level).
990 *
991 * Depending on the workspace_layout setting, this function either returns the
992 * workspace itself (default layout) or creates a new stacked/tabbed con and
993 * returns that.
994 *
995 */
997 DLOG("Attaching a window to workspace %p / %s\n", ws, ws->name);
998
999 if (ws->workspace_layout == L_DEFAULT) {
1000 DLOG("Default layout, just attaching it to the workspace itself.\n");
1001 return ws;
1002 }
1003
1004 DLOG("Non-default layout, creating a new split container\n");
1005 /* 1: create a new split container */
1006 Con *new = con_new(NULL, NULL);
1007 new->parent = ws;
1008
1009 /* 2: set the requested layout on the split con */
1010 new->layout = ws->workspace_layout;
1011
1012 /* 4: attach the new split container to the workspace */
1013 DLOG("Attaching new split %p to workspace %p\n", new, ws);
1014 con_attach(new, ws, false);
1015
1016 /* 5: fix the percentages */
1017 con_fix_percent(ws);
1018
1019 return new;
1020}
1021
1022/*
1023 * Creates a new container and re-parents all of children from the given
1024 * workspace into it.
1025 *
1026 * The container inherits the layout from the workspace.
1027 */
1029 if (TAILQ_EMPTY(&(ws->nodes_head))) {
1030 ELOG("Workspace %p / %s has no children to encapsulate\n", ws, ws->name);
1031 return NULL;
1032 }
1033
1034 Con *new = con_new(NULL, NULL);
1035 new->parent = ws;
1036 new->layout = ws->layout;
1037
1038 Con **focus_order = get_focus_order(ws);
1039
1040 DLOG("Moving children of workspace %p / %s into container %p\n",
1041 ws, ws->name, new);
1042 Con *child;
1043 while (!TAILQ_EMPTY(&(ws->nodes_head))) {
1044 child = TAILQ_FIRST(&(ws->nodes_head));
1045 con_detach(child);
1046 con_attach(child, new, true);
1047 }
1048
1049 set_focus_order(new, focus_order);
1050 free(focus_order);
1051
1052 con_attach(new, ws, true);
1053
1054 return new;
1055}
1056
1057/*
1058 * Move the given workspace to the specified output.
1059 */
1061 DLOG("Moving workspace %p / %s to output %p / \"%s\".\n", ws, ws->name, output, output_primary_name(output));
1062
1063 Output *current_output = get_output_for_con(ws);
1064 Con *content = output_get_content(output->con);
1065 DLOG("got output %p with content %p\n", output, content);
1066
1067 if (ws->parent == content) {
1068 DLOG("Nothing to do, workspace already there\n");
1069 return;
1070 }
1071
1072 Con *previously_visible_ws = TAILQ_FIRST(&(content->focus_head));
1073 if (previously_visible_ws) {
1074 DLOG("Previously visible workspace = %p / %s\n", previously_visible_ws, previously_visible_ws->name);
1075 } else {
1076 DLOG("No previously visible workspace on output.\n");
1077 }
1078
1079 bool workspace_was_visible = workspace_is_visible(ws);
1080 if (con_num_children(ws->parent) == 1) {
1081 DLOG("Creating a new workspace to replace \"%s\" (last on its output).\n", ws->name);
1082
1083 /* check if we can find a workspace assigned to this output */
1084 bool used_assignment = false;
1085 struct Workspace_Assignment *assignment;
1087 if (!output_triggers_assignment(current_output, assignment)) {
1088 continue;
1089 }
1090 /* check if this workspace's name or num is already attached to the tree */
1091 const int num = ws_name_to_number(assignment->name);
1092 const bool attached = (num == -1)
1095 if (attached) {
1096 continue;
1097 }
1098
1099 /* so create the workspace referenced to by this assignment */
1100 DLOG("Creating workspace from assignment %s.\n", assignment->name);
1101 workspace_get(assignment->name);
1102 used_assignment = true;
1103 break;
1104 }
1105
1106 /* if we couldn't create the workspace using an assignment, create it on
1107 * the output. Workspace init IPC events are sent either by
1108 * workspace_get or create_workspace_on_output. */
1109 if (!used_assignment) {
1110 create_workspace_on_output(current_output, ws->parent);
1111 }
1112 }
1113 DLOG("Detaching\n");
1114
1115 /* detach from the old output and attach to the new output */
1116 Con *old_content = ws->parent;
1117 con_detach(ws);
1118 if (workspace_was_visible) {
1119 /* The workspace which we just detached was visible, so focus the next
1120 * one in the focus-stack. */
1121 Con *focus_ws = TAILQ_FIRST(&(old_content->focus_head));
1122 DLOG("workspace was visible, focusing %p / %s now\n", focus_ws, focus_ws->name);
1123 workspace_show(focus_ws);
1124 }
1125 con_attach(ws, content, false);
1126
1127 /* fix the coordinates of the floating containers */
1128 Con *floating_con;
1129 TAILQ_FOREACH (floating_con, &(ws->floating_head), floating_windows) {
1130 floating_fix_coordinates(floating_con, &(old_content->rect), &(content->rect));
1131 }
1132
1133 ipc_send_workspace_event("move", ws, NULL);
1134 if (workspace_was_visible) {
1135 /* Focus the moved workspace on the destination output. */
1136 workspace_show(ws);
1137 }
1138
1140
1141 if (!previously_visible_ws) {
1142 return;
1143 }
1144
1145 /* NB: We cannot simply work with previously_visible_ws since it might have
1146 * been cleaned up by workspace_show() already, depending on the focus
1147 * order/number of other workspaces on the output. Instead, we loop through
1148 * the available workspaces and only work with previously_visible_ws if we
1149 * still find it. */
1150 TAILQ_FOREACH (ws, &(content->nodes_head), nodes) {
1151 if (ws != previously_visible_ws) {
1152 continue;
1153 }
1154
1155 /* Call the on_remove_child callback of the workspace which previously
1156 * was visible on the destination output. Since it is no longer visible,
1157 * it might need to get cleaned up. */
1158 CALL(previously_visible_ws, on_remove_child);
1159 break;
1160 }
1161}
#define y(x,...)
Definition commands.c:18
char * parse_string(const char **walk, bool as_word)
Parses a string (or word, if as_word is true).
Con * con_get_fullscreen_con(Con *con, fullscreen_mode_t fullscreen_mode)
Returns the first fullscreen node below this node.
Definition con.c:599
void con_set_urgency(Con *con, bool urgent)
Set urgency flag to the container, all the parent containers and the workspace.
Definition con.c:2431
void con_update_parents_urgency(Con *con)
Make all parent containers urgent if con is urgent or clear the urgent flag of all parent containers ...
Definition con.c:2401
Con * con_new(Con *parent, i3Window *window)
A wrapper for con_new_skeleton, to retain the old con_new behaviour.
Definition con.c:70
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition con.c:547
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition con.c:662
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition con.c:234
void set_focus_order(Con *con, Con **focus_order)
Clear the container's focus stack and re-add it using the provided container array.
Definition con.c:1051
void con_fix_percent(Con *con)
Updates the percent attribute of the children of the given container.
Definition con.c:1144
void con_attach(Con *con, Con *parent, bool ignore_focus)
Attaches the given container to the given parent.
Definition con.c:226
int con_num_children(Con *con)
Returns the number of children of this container.
Definition con.c:1075
void con_focus(Con *con)
Sets input focus to the given container.
Definition con.c:250
Con * con_get_output(Con *con)
Gets the output container (first container with CT_OUTPUT in hierarchy) this node is on.
Definition con.c:532
Con * con_descend_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1698
Con ** get_focus_order(Con *con)
Iterate over the container's focus stack and return an array with the containers inside it,...
Definition con.c:1031
Config config
Definition config.c:19
void ewmh_update_desktop_properties(void)
Updates all the EWMH desktop properties.
Definition ewmh.c:118
void ewmh_update_current_desktop(void)
Updates _NET_CURRENT_DESKTOP with the current desktop number.
Definition ewmh.c:28
void floating_fix_coordinates(Con *con, Rect *old_rect, Rect *new_rect)
Fixes the coordinates of the floating window whenever the window gets reassigned to a different outpu...
Definition floating.c:816
gaps_t gaps_for_workspace(Con *ws)
Returns the configured gaps for this workspace based on the workspace name, number,...
Definition gaps.c:124
void output_push_sticky_windows(Con *old_focus)
Iterates over all outputs and pushes sticky windows to the currently visible workspace on that output...
Definition output.c:83
char * output_primary_name(Output *output)
Retrieves the primary name of an output.
Definition output.c:53
Output * get_output_for_con(Con *con)
Retrieves the output for a given container.
Definition output.c:63
Con * output_get_content(Con *output)
Returns the output container below the given output container.
Definition output.c:16
Output * get_output_by_name(const char *name, const bool require_active)
Returns the output with the given name or NULL.
Definition randr.c:50
struct Con * focused
Definition tree.c:13
struct Con * croot
Definition tree.c:12
bool tree_close_internal(Con *con, kill_window_t kill_window, bool dont_kill_parent)
Closes the given container including all children.
Definition tree.c:192
void tree_render(void)
Renders the tree, that is rendering all outputs using render_con() and pushing the changes to X11 usi...
Definition tree.c:455
int ws_name_to_number(const char *name)
Parses the workspace name as a number.
Definition util.c:111
static void _workspace_apply_default_orientation(Con *ws)
Definition workspace.c:59
Con * workspace_back_and_forth_get(void)
Returns the previously focused workspace con, or NULL if unavailable.
Definition workspace.c:907
Con * get_existing_workspace_by_name(const char *name)
Returns the workspace with the given name or NULL if such a workspace does not exist.
Definition workspace.c:30
static Con * _get_sticky(Con *con, const char *sticky_group, Con *exclude)
Definition workspace.c:333
bool output_triggers_assignment(Output *output, struct Workspace_Assignment *assignment)
Returns true if the first output assigned to a workspace with the given workspace assignment is the s...
Definition workspace.c:120
Con * create_workspace_on_output(Output *output, Con *content)
Returns a pointer to a new workspace in the given output.
Definition workspace.c:250
static void workspace_defer_update_urgent_hint_cb(EV_P_ ev_timer *w, int revents)
Definition workspace.c:418
Con * workspace_next_on_output(void)
Returns the next workspace on the same output.
Definition workspace.c:752
void workspace_update_urgent_flag(Con *ws)
Goes through all clients on the given workspace and updates the workspace’s urgent flag accordingly.
Definition workspace.c:938
void workspace_show(Con *workspace)
Switches to the given workspace.
Definition workspace.c:438
bool workspace_is_visible(Con *ws)
Returns true if the workspace is currently visible.
Definition workspace.c:320
static void workspace_reassign_sticky(Con *con)
Definition workspace.c:374
Con * workspace_prev_on_output(void)
Returns the previous workspace on the same output.
Definition workspace.c:822
Con * workspace_attach_to(Con *ws)
Called when a new con (with a window, not an empty or split con) should be attached to the workspace ...
Definition workspace.c:996
Con * workspace_get(const char *num)
Returns a pointer to the workspace with the given number (starting at 0), creating the workspace if n...
Definition workspace.c:131
void workspace_back_and_forth(void)
Focuses the previously focused workspace.
Definition workspace.c:894
void workspace_move_to_output(Con *ws, Output *output)
Move the given workspace to the specified output.
Definition workspace.c:1060
static char ** binding_workspace_names
Definition workspace.c:23
Con * workspace_next(void)
Returns the next workspace.
Definition workspace.c:581
void extract_workspace_names_from_bindings(void)
Extracts workspace names from keybindings (e.g.
Definition workspace.c:181
void ws_force_orientation(Con *ws, orientation_t orientation)
'Forces' workspace orientation by moving all cons into a new split-con with the same orientation as t...
Definition workspace.c:953
Con * get_existing_workspace_by_num(int num)
Returns the workspace with the given number or NULL if such a workspace does not exist.
Definition workspace.c:44
Con * workspace_prev(void)
Returns the previous workspace.
Definition workspace.c:665
void workspace_show_by_name(const char *num)
Looks up the workspace by name and switches to it.
Definition workspace.c:573
static bool get_urgency_flag(Con *con)
Definition workspace.c:916
char * previous_workspace_name
Stores a copy of the name of the last used workspace for the workspace back-and-forth switching.
Definition workspace.c:19
Con * workspace_encapsulate(Con *ws)
Creates a new container and re-parents all of children from the given workspace into it.
Definition workspace.c:1028
Con * get_assigned_output(const char *name, long parsed_num)
Returns the first output that is assigned to a workspace specified by the given name or number.
Definition workspace.c:84
void x_move_win(Con *src, Con *dest)
Moves a child window from Container src to Container dest.
Definition x.c:234
void x_reparent_child(Con *con, Con *old)
Reparents the child window of the given container (necessary for sticky containers).
Definition x.c:219
void x_set_warp_to(Rect *rect)
Set warp_to coordinates.
Definition x.c:1545
void x_set_name(Con *con, const char *name)
Sets the WM_NAME property (so, no UTF8, but used only for debugging anyways) of the given name.
Definition x.c:1497
void ipc_send_workspace_event(const char *change, Con *current, Con *old)
For the workspace events we send, along with the usual "change" field, also the workspace container i...
Definition ipc.c:1634
void ipc_send_event(const char *event, uint32_t message_type, const char *payload)
Sends the specified event to all IPC clients which are currently connected and subscribed to this kin...
Definition ipc.c:147
yajl_gen ipc_marshal_workspace_event(const char *change, Con *current, Con *old)
Generates a json workspace event.
Definition ipc.c:1599
void ipc_send_window_event(const char *property, Con *con)
For the window events we send, along the usual "change" field, also the window container,...
Definition ipc.c:1650
struct ev_loop * main_loop
Definition main.c:79
struct ws_assignments_head ws_assignments
Definition main.c:101
struct bindings_head * bindings
Definition main.c:87
@ L_SPLITH
Definition data.h:108
@ L_SPLITV
Definition data.h:107
@ L_DEFAULT
Definition data.h:102
orientation_t
Definition data.h:60
@ HORIZ
Definition data.h:61
@ NO_ORIENTATION
Definition data.h:60
@ CF_OUTPUT
Definition data.h:630
@ CF_NONE
Definition data.h:629
@ DONT_KILL_WINDOW
Definition data.h:73
#define DLOG(fmt,...)
Definition libi3.h:105
#define LOG(fmt,...)
Definition libi3.h:95
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
#define ELOG(fmt,...)
Definition libi3.h:100
void * scalloc(size_t num, size_t size)
Safe-wrapper around calloc which exits if malloc returns NULL (meaning that there is no more memory a...
int sasprintf(char **strp, const char *fmt,...)
Safe-wrapper around asprintf which exits if it returns -1 (meaning that there is no more memory avail...
void * srealloc(void *ptr, size_t size)
Safe-wrapper around realloc which exits if realloc returns NULL (meaning that there is no more memory...
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_PREV(elm, headname, field)
Definition queue.h:342
#define TAILQ_FIRST(head)
Definition queue.h:336
#define TAILQ_FOREACH_REVERSE(var, head, headname, field)
Definition queue.h:352
#define TAILQ_NEXT(elm, field)
Definition queue.h:338
#define TAILQ_EMPTY(head)
Definition queue.h:344
#define NODES_FOREACH(head)
Definition util.h:29
#define CALL(obj, member,...)
Definition util.h:53
#define GREP_FIRST(dest, head, condition)
Definition util.h:38
#define NODES_FOREACH_REVERSE(head)
Definition util.h:33
#define FREE(pointer)
Definition util.h:47
size_t ylength
Definition yajl_utils.h:24
int default_orientation
Default orientation for new containers.
float workspace_urgency_timer
By default, urgency is cleared immediately when switching to another workspace leads to focusing the ...
layout_t default_layout
uint32_t height
Definition data.h:189
uint32_t width
Definition data.h:188
Stores which workspace (by name or number) goes to which output and its gaps config.
Definition data.h:235
Holds a keybinding, consisting of a keycode combined with modifiers and the command which is executed...
Definition data.h:306
char * command
Command, like in command mode.
Definition data.h:357
An Output is a physical output on your graphics driver.
Definition data.h:391
Con * con
Pointer to the Con which represents this output.
Definition data.h:411
A 'Con' represents everything from the X11 root window down to a single X11 window.
Definition data.h:643
struct Con * parent
Definition data.h:678
enum Con::@18 type
layout_t workspace_layout
Definition data.h:755
struct Rect rect
Definition data.h:682
gaps_t gaps
Only applicable for containers of type CT_WORKSPACE.
Definition data.h:676
layout_t layout
Definition data.h:755
bool mapped
Definition data.h:644
int num
the workspace number, if this Con is of type CT_WORKSPACE and the workspace is not a named workspace ...
Definition data.h:673
struct ev_timer * urgency_timer
Definition data.h:721
struct Window * window
Definition data.h:718
char * name
Definition data.h:692
char * sticky_group
Definition data.h:705
fullscreen_mode_t fullscreen_mode
Definition data.h:734
bool urgent
Definition data.h:648