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