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