i3
con.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 * con.c: Functions which deal with containers directly (creating containers,
8 * searching containers, getting specific properties from containers,
9 * …).
10 *
11 */
12#include "all.h"
13#include "yajl_utils.h"
14
15static void con_on_remove_child(Con *con);
16
17/*
18 * force parent split containers to be redrawn
19 *
20 */
22 Con *parent = con;
23
24 while (parent != NULL && parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
25 if (!con_is_leaf(parent)) {
26 FREE(parent->deco_render_params);
27 }
28
29 parent = parent->parent;
30 }
31}
32
33/*
34 * Create a new container (and attach it to the given parent, if not NULL).
35 * This function only initializes the data structures.
36 *
37 */
38Con *con_new_skeleton(Con *parent, i3Window *window) {
39 Con *new = scalloc(1, sizeof(Con));
40 new->on_remove_child = con_on_remove_child;
42 new->type = CT_CON;
43 new->window = window;
44 new->border_style = new->max_user_border_style = config.default_border;
45 new->current_border_width = -1;
46 new->window_icon_padding = -1;
47 if (window) {
48 new->depth = window->depth;
49 } else {
50 new->depth = root_depth;
51 }
52 DLOG("opening window\n");
53
54 TAILQ_INIT(&(new->floating_head));
55 TAILQ_INIT(&(new->nodes_head));
56 TAILQ_INIT(&(new->focus_head));
57 TAILQ_INIT(&(new->swallow_head));
58 TAILQ_INIT(&(new->marks_head));
59
60 if (parent != NULL) {
61 con_attach(new, parent, false);
62 }
63
64 return new;
65}
66
67/* A wrapper for con_new_skeleton, to retain the old con_new behaviour
68 *
69 */
70Con *con_new(Con *parent, i3Window *window) {
71 Con *new = con_new_skeleton(parent, window);
72 x_con_init(new);
73 return new;
74}
75
76/*
77 * Frees the specified container.
78 *
79 */
80void con_free(Con *con) {
81 free(con->name);
84 while (!TAILQ_EMPTY(&(con->swallow_head))) {
85 Match *match = TAILQ_FIRST(&(con->swallow_head));
86 TAILQ_REMOVE(&(con->swallow_head), match, matches);
87 match_free(match);
88 free(match);
89 }
90 while (!TAILQ_EMPTY(&(con->marks_head))) {
91 mark_t *mark = TAILQ_FIRST(&(con->marks_head));
92 TAILQ_REMOVE(&(con->marks_head), mark, marks);
93 FREE(mark->name);
94 FREE(mark);
95 }
96 DLOG("con %p freed\n", con);
97 free(con);
98}
99
100static void _con_attach(Con *con, Con *parent, Con *previous, bool ignore_focus) {
101 con->parent = parent;
102 Con *loop;
103 Con *current = previous;
104 struct nodes_head *nodes_head = &(parent->nodes_head);
105 struct focus_head *focus_head = &(parent->focus_head);
106
107 /* Workspaces are handled differently: they need to be inserted at the
108 * right position. */
109 if (con->type == CT_WORKSPACE) {
110 DLOG("it's a workspace. num = %d\n", con->num);
111 if (con->num == -1 || TAILQ_EMPTY(nodes_head)) {
112 TAILQ_INSERT_TAIL(nodes_head, con, nodes);
113 } else {
114 current = TAILQ_FIRST(nodes_head);
115 if (con->num < current->num) {
116 /* we need to insert the container at the beginning */
117 TAILQ_INSERT_HEAD(nodes_head, con, nodes);
118 } else {
119 while (current->num != -1 && con->num >= current->num) {
120 current = TAILQ_NEXT(current, nodes);
121 if (current == TAILQ_END(nodes_head)) {
122 current = NULL;
123 break;
124 }
125 }
126 /* we need to insert con before current, if current is not NULL */
127 if (current) {
128 TAILQ_INSERT_BEFORE(current, con, nodes);
129 } else {
130 TAILQ_INSERT_TAIL(nodes_head, con, nodes);
131 }
132 }
133 }
134 goto add_to_focus_head;
135 }
136
137 if (parent->type == CT_DOCKAREA) {
138 /* Insert dock client, sorting alphanumerically by class and then
139 * instance name. This makes dock client order deterministic. As a side
140 * effect, bars without a custom bar id will be sorted according to
141 * their declaration order in the config file. See #3491. */
142 current = NULL;
143 TAILQ_FOREACH (loop, nodes_head, nodes) {
144 int result = strcasecmp_nullable(con->window->class_class, loop->window->class_class);
145 if (result == 0) {
147 }
148 if (result < 0) {
149 current = loop;
150 break;
151 }
152 }
153 if (current) {
154 TAILQ_INSERT_BEFORE(loop, con, nodes);
155 } else {
156 TAILQ_INSERT_TAIL(nodes_head, con, nodes);
157 }
158 goto add_to_focus_head;
159 }
160
161 if (con->type == CT_FLOATING_CON) {
162 DLOG("Inserting into floating containers\n");
163 TAILQ_INSERT_TAIL(&(parent->floating_head), con, floating_windows);
164 } else {
165 if (!ignore_focus) {
166 /* Get the first tiling container in focus stack */
167 TAILQ_FOREACH (loop, &(parent->focus_head), focused) {
168 if (loop->type == CT_FLOATING_CON) {
169 continue;
170 }
171 current = loop;
172 break;
173 }
174 }
175
176 /* When the container is not a split container (but contains a window)
177 * and is attached to a workspace, we check if the user configured a
178 * workspace_layout. This is done in workspace_attach_to, which will
179 * provide us with the container to which we should attach (either the
180 * workspace or a new split container with the configured
181 * workspace_layout).
182 */
183 if (con->window != NULL &&
184 parent->type == CT_WORKSPACE &&
185 parent->workspace_layout != L_DEFAULT) {
186 DLOG("Parent is a workspace. Applying default layout...\n");
187 Con *target = workspace_attach_to(parent);
188
189 /* Attach the original con to this new split con instead */
190 nodes_head = &(target->nodes_head);
191 focus_head = &(target->focus_head);
192 con->parent = target;
193 current = NULL;
194
195 DLOG("done\n");
196 }
197
198 /* Insert the container after the tiling container, if found.
199 * When adding to a CT_OUTPUT, just append one after another. */
200 if (current != NULL && parent->type != CT_OUTPUT) {
201 DLOG("Inserting con = %p after con %p\n", con, current);
202 TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
203 } else {
204 TAILQ_INSERT_TAIL(nodes_head, con, nodes);
205 }
206 }
207
208add_to_focus_head:
209 /* We insert to the TAIL because con_focus() will correct this.
210 * This way, we have the option to insert Cons without having
211 * to focus them. */
212 TAILQ_INSERT_TAIL(focus_head, con, focused);
214}
215
216/*
217 * Attaches the given container to the given parent. This happens when moving
218 * a container or when inserting a new container at a specific place in the
219 * tree.
220 *
221 * ignore_focus is to just insert the Con at the end (useful when creating a
222 * new split container *around* some containers, that is, detaching and
223 * attaching them in order without wanting to mess with the focus in between).
224 *
225 */
226void con_attach(Con *con, Con *parent, bool ignore_focus) {
227 _con_attach(con, parent, NULL, ignore_focus);
228}
229
230/*
231 * Detaches the given container from its current parent
232 *
233 */
234void con_detach(Con *con) {
236 if (con->type == CT_FLOATING_CON) {
237 TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
238 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
239 } else {
240 TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
241 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
242 }
243}
244
245/*
246 * Sets input focus to the given container. Will be updated in X11 in the next
247 * run of x_push_changes().
248 *
249 */
250void con_focus(Con *con) {
251 assert(con != NULL);
252 DLOG("con_focus = %p\n", con);
253
254 /* 1: set focused-pointer to the new con */
255 /* 2: exchange the position of the container in focus stack of the parent all the way up */
256 TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
257 TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
258 if (con->parent->parent != NULL) {
259 con_focus(con->parent);
260 }
261
262 focused = con;
263 /* We can't blindly reset non-leaf containers since they might have
264 * other urgent children. Therefore we only reset leafs and propagate
265 * the changes upwards via con_update_parents_urgency() which does proper
266 * checks before resetting the urgency.
267 */
268 if (con->urgent && con_is_leaf(con)) {
269 con_set_urgency(con, false);
272 ipc_send_window_event("urgent", con);
273 }
274}
275
276/*
277 * Raise container to the top if it is floating or inside some floating
278 * container.
279 *
280 */
281static void con_raise(Con *con) {
282 Con *floating = con_inside_floating(con);
283 if (floating) {
284 floating_raise_con(floating);
285 }
286}
287
288/*
289 * Sets input focus to the given container and raises it to the top.
290 *
291 */
292void con_activate(Con *con) {
293 con_focus(con);
294 con_raise(con);
295}
296
297/*
298 * Activates the container like in con_activate but removes fullscreen
299 * restrictions and properly warps the pointer if needed.
300 *
301 */
303 Con *ws = con_get_workspace(con);
304 Con *previous_focus = focused;
305 Con *fullscreen_on_ws = con_get_fullscreen_covering_ws(ws);
306
307 if (fullscreen_on_ws && fullscreen_on_ws != con && !con_has_parent(con, fullscreen_on_ws)) {
308 con_disable_fullscreen(fullscreen_on_ws);
309 }
310
311 con_activate(con);
312
313 /* If the container is not on the current workspace, workspace_show() will
314 * switch to a different workspace and (if enabled) trigger a mouse pointer
315 * warp to the currently focused container (!) on the target workspace.
316 *
317 * Therefore, before calling workspace_show(), we make sure that 'con' will
318 * be focused on the workspace. However, we cannot just con_focus(con)
319 * because then the pointer will not be warped at all (the code thinks we
320 * are already there).
321 *
322 * So we focus 'con' to make it the currently focused window of the target
323 * workspace, then revert focus. */
324 if (ws != con_get_workspace(previous_focus)) {
325 con_activate(previous_focus);
326 /* Now switch to the workspace, then focus */
327 workspace_show(ws);
328 con_activate(con);
329 }
330}
331
332/*
333 * Closes the given container.
334 *
335 */
336void con_close(Con *con, kill_window_t kill_window) {
337 assert(con != NULL);
338 DLOG("Closing con = %p.\n", con);
339
340 /* We never close output or root containers. */
341 if (con->type == CT_OUTPUT || con->type == CT_ROOT) {
342 DLOG("con = %p is of type %d, not closing anything.\n", con, con->type);
343 return;
344 }
345
346 if (con->type == CT_WORKSPACE) {
347 DLOG("con = %p is a workspace, closing all children instead.\n", con);
348 Con *child, *nextchild;
349 for (child = TAILQ_FIRST(&(con->focus_head)); child;) {
350 nextchild = TAILQ_NEXT(child, focused);
351 DLOG("killing child = %p.\n", child);
352 tree_close_internal(child, kill_window, false);
353 child = nextchild;
354 }
355
356 return;
357 }
358
359 tree_close_internal(con, kill_window, false);
360}
361
362/*
363 * Returns true when this node is a leaf node (has no children)
364 *
365 */
366bool con_is_leaf(Con *con) {
367 return TAILQ_EMPTY(&(con->nodes_head));
368}
369
370/*
371 * Returns true when this con is a leaf node with a managed X11 window (e.g.,
372 * excluding dock containers)
373 */
375 return (con != NULL && con->window != NULL && con->window->id != XCB_WINDOW_NONE && con_get_workspace(con) != NULL);
376}
377
378/*
379 * Returns true if this node has regular or floating children.
380 *
381 */
383 return (!con_is_leaf(con) || !TAILQ_EMPTY(&(con->floating_head)));
384}
385
386/*
387 * Returns true if a container should be considered split.
388 *
389 */
390bool con_is_split(Con *con) {
391 if (con_is_leaf(con)) {
392 return false;
393 }
394
395 switch (con->layout) {
396 case L_DOCKAREA:
397 case L_OUTPUT:
398 return false;
399
400 default:
401 return true;
402 }
403}
404
405/*
406 * This will only return true for containers which have some parent with
407 * a tabbed / stacked parent of which they are not the currently focused child.
408 *
409 */
410bool con_is_hidden(Con *con) {
411 Con *current = con;
412
413 /* ascend to the workspace level and memorize the highest-up container
414 * which is stacked or tabbed. */
415 while (current != NULL && current->type != CT_WORKSPACE) {
416 Con *parent = current->parent;
417 if (parent != NULL && (parent->layout == L_TABBED || parent->layout == L_STACKED)) {
418 if (TAILQ_FIRST(&(parent->focus_head)) != current) {
419 return true;
420 }
421 }
422
423 current = parent;
424 }
425
426 return false;
427}
428
429/*
430 * Returns true if the container is maximized in the given orientation.
431 *
432 * If the container is floating, it is not considered maximized. Otherwise, it
433 * is maximized if it doesn't share space with any other container in the given
434 * orientation. For example, if a workspace contains a single splitv container
435 * with three children, none of them are considered vertically maximized, but
436 * they are all considered horizontally maximized.
437 *
438 * Passing "maximized" hints to the application can help it make the right
439 * choices about how to draw its borders. See discussion in
440 * https://github.com/i3/i3/pull/2380.
441 */
442bool con_is_maximized(Con *con, orientation_t orientation) {
443 /* Fullscreen containers are considered maximized. */
444 if (con->fullscreen_mode != CF_NONE) {
445 return true;
446 }
447
448 /* Look up the container layout which corresponds to the given
449 * orientation. */
450 layout_t layout;
451 switch (orientation) {
452 case HORIZ:
453 layout = L_SPLITH;
454 break;
455 case VERT:
456 layout = L_SPLITV;
457 break;
458 default:
459 assert(false);
460 }
461
462 /* Go through all parents, stopping once we reach the workspace node. */
463 Con *current = con;
464 while (true) {
465 Con *parent = current->parent;
466 if (parent == NULL || current->type == CT_WORKSPACE) {
467 /* We are done searching. We found no reason that the container
468 * should not be considered maximized. */
469 return true;
470 }
471
472 if (parent->layout == layout && con_num_children(parent) > 1) {
473 /* The parent has a split in the indicated direction, which
474 * means none of its children are maximized in that direction. */
475 return false;
476 }
477
478 /* Floating containers and their children are not considered
479 * maximized. */
480 if (parent->type == CT_FLOATING_CON) {
481 return false;
482 }
483
484 current = parent;
485 }
486}
487
488/*
489 * Returns whether the container or any of its children is sticky.
490 *
491 */
492bool con_is_sticky(Con *con) {
493 if (con->sticky) {
494 return true;
495 }
496
497 Con *child;
498 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
499 if (con_is_sticky(child)) {
500 return true;
501 }
502 }
503
504 return false;
505}
506
507/*
508 * Returns true if this node accepts a window (if the node swallows windows,
509 * it might already have swallowed enough and cannot hold any more).
510 *
511 */
513 /* 1: workspaces never accept direct windows */
514 if (con->type == CT_WORKSPACE) {
515 return false;
516 }
517
518 if (con_is_split(con)) {
519 DLOG("container %p does not accept windows, it is a split container.\n", con);
520 return false;
521 }
522
523 /* TODO: if this is a swallowing container, we need to check its max_clients */
524 return (con->window == NULL);
525}
526
527/*
528 * Gets the output container (first container with CT_OUTPUT in hierarchy) this
529 * node is on.
530 *
531 */
533 Con *result = con;
534 while (result != NULL && result->type != CT_OUTPUT) {
535 result = result->parent;
536 }
537 /* We must be able to get an output because focus can never be set higher
538 * in the tree (root node cannot be focused). */
539 assert(result != NULL);
540 return result;
541}
542
543/*
544 * Gets the workspace container this node is on.
545 *
546 */
548 Con *result = con;
549 while (result != NULL && result->type != CT_WORKSPACE) {
550 result = result->parent;
551 }
552 return result;
553}
554
555/*
556 * Searches parents of the given 'con' until it reaches one with the specified
557 * 'orientation'. Aborts when it comes across a floating_con.
558 *
559 */
561 DLOG("Searching for parent of Con %p with orientation %d\n", con, orientation);
562 Con *parent = con->parent;
563 if (parent->type == CT_FLOATING_CON) {
564 return NULL;
565 }
566 while (con_orientation(parent) != orientation) {
567 DLOG("Need to go one level further up\n");
568 parent = parent->parent;
569 /* Abort when we reach a floating con, or an output con */
570 if (parent &&
571 (parent->type == CT_FLOATING_CON ||
572 parent->type == CT_OUTPUT ||
573 (parent->parent && parent->parent->type == CT_OUTPUT))) {
574 parent = NULL;
575 }
576 if (parent == NULL) {
577 break;
578 }
579 }
580 DLOG("Result: %p\n", parent);
581 return parent;
582}
583
584/*
585 * helper data structure for the breadth-first-search in
586 * con_get_fullscreen_con()
587 *
588 */
589struct bfs_entry {
591
592 TAILQ_ENTRY(bfs_entry) entries;
593};
594
595/*
596 * Returns the first fullscreen node below this node.
597 *
598 */
600 Con *current, *child;
601
602 /* TODO: is breadth-first-search really appropriate? (check as soon as
603 * fullscreen levels and fullscreen for containers is implemented) */
604 TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
605 struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
606 entry->con = con;
607 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
608
609 while (!TAILQ_EMPTY(&bfs_head)) {
610 entry = TAILQ_FIRST(&bfs_head);
611 current = entry->con;
612 if (current != con && current->fullscreen_mode == fullscreen_mode) {
613 /* empty the queue */
614 while (!TAILQ_EMPTY(&bfs_head)) {
615 entry = TAILQ_FIRST(&bfs_head);
616 TAILQ_REMOVE(&bfs_head, entry, entries);
617 free(entry);
618 }
619 return current;
620 }
621
622 TAILQ_REMOVE(&bfs_head, entry, entries);
623 free(entry);
624
625 TAILQ_FOREACH (child, &(current->nodes_head), nodes) {
626 entry = smalloc(sizeof(struct bfs_entry));
627 entry->con = child;
628 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
629 }
630
631 TAILQ_FOREACH (child, &(current->floating_head), floating_windows) {
632 entry = smalloc(sizeof(struct bfs_entry));
633 entry->con = child;
634 TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
635 }
636 }
637
638 return NULL;
639}
640
641/*
642 * Returns the fullscreen node that covers the given workspace if it exists.
643 * This is either a CF_GLOBAL fullscreen container anywhere or a CF_OUTPUT
644 * fullscreen container in the workspace.
645 *
646 */
648 if (!ws) {
649 return NULL;
650 }
652 if (!fs) {
654 }
655 return fs;
656}
657
658/*
659 * Returns true if the container is internal, such as __i3_scratch
660 *
661 */
663 return (con->name[0] == '_' && con->name[1] == '_');
664}
665
666/*
667 * Returns true if the node is floating.
668 *
669 */
671 assert(con != NULL);
672 return (con->floating >= FLOATING_AUTO_ON);
673}
674
675/*
676 * Returns true if the container is a docked container.
677 *
678 */
680 if (con->parent == NULL) {
681 return false;
682 }
683
684 if (con->parent->type == CT_DOCKAREA) {
685 return true;
686 }
687
688 return con_is_docked(con->parent);
689}
690
691/*
692 * Checks if the given container is either floating or inside some floating
693 * container. It returns the FLOATING_CON container.
694 *
695 */
697 if (con == NULL) {
698 return NULL;
699 }
700
701 if (con->type == CT_FLOATING_CON) {
702 return con;
703 }
704
705 if (con->floating >= FLOATING_AUTO_ON) {
706 return con->parent;
707 }
708
709 if (con->type == CT_WORKSPACE || con->type == CT_OUTPUT) {
710 return NULL;
711 }
712
714}
715
716/*
717 * Checks if the given container is inside a focused container.
718 *
719 */
721 if (con == focused) {
722 return true;
723 }
724 if (!con->parent) {
725 return false;
726 }
728}
729
730/*
731 * Checks if the container has the given parent as an actual parent.
732 *
733 */
734bool con_has_parent(Con *con, Con *parent) {
735 Con *current = con->parent;
736 if (current == NULL) {
737 return false;
738 }
739
740 if (current == parent) {
741 return true;
742 }
743
744 return con_has_parent(current, parent);
745}
746
747/*
748 * Returns the container with the given client window ID or NULL if no such
749 * container exists.
750 *
751 */
752Con *con_by_window_id(xcb_window_t window) {
753 Con *con;
755 if (con->window != NULL && con->window->id == window) {
756 return con;
757 }
758 }
759 return NULL;
760}
761
762/*
763 * Returns the container with the given container ID or NULL if no such
764 * container exists.
765 *
766 */
767Con *con_by_con_id(long target) {
768 Con *con;
770 if (con == (Con *)target) {
771 return con;
772 }
773 }
774
775 return NULL;
776}
777
778/*
779 * Returns true if the given container (still) exists.
780 * This can be used, e.g., to make sure a container hasn't been closed in the meantime.
781 *
782 */
784 return con_by_con_id((long)con) != NULL;
785}
786
787/*
788 * Returns the container with the given frame ID or NULL if no such container
789 * exists.
790 *
791 */
792Con *con_by_frame_id(xcb_window_t frame) {
793 Con *con;
795 if (con->frame.id == frame) {
796 return con;
797 }
798 }
799 return NULL;
800}
801
802/*
803 * Returns the container with the given mark or NULL if no such container
804 * exists.
805 *
806 */
807Con *con_by_mark(const char *mark) {
808 Con *con;
810 if (con_has_mark(con, mark)) {
811 return con;
812 }
813 }
814
815 return NULL;
816}
817
818/*
819 * Start from a container and traverse the transient_for linked list. Returns
820 * true if target window is found in the list. Protects againsts potential
821 * cycles.
822 *
823 */
824bool con_find_transient_for_window(Con *start, xcb_window_t target) {
825 Con *transient_con = start;
826 int count = con_num_windows(croot);
827 while (transient_con != NULL &&
828 transient_con->window != NULL &&
829 transient_con->window->transient_for != XCB_NONE) {
830 DLOG("transient_con = 0x%08x, transient_con->window->transient_for = 0x%08x, target = 0x%08x\n",
831 transient_con->window->id, transient_con->window->transient_for, target);
832 if (transient_con->window->transient_for == target) {
833 return true;
834 }
835 Con *next_transient = con_by_window_id(transient_con->window->transient_for);
836 if (next_transient == NULL) {
837 break;
838 }
839 /* Some clients (e.g. x11-ssh-askpass) actually set WM_TRANSIENT_FOR to
840 * their own window id, so break instead of looping endlessly. */
841 if (transient_con == next_transient) {
842 break;
843 }
844 transient_con = next_transient;
845
846 if (count-- <= 0) { /* Avoid cycles, see #4404 */
847 break;
848 }
849 }
850 return false;
851}
852
853/*
854 * Returns true if and only if the given containers holds the mark.
855 *
856 */
857bool con_has_mark(Con *con, const char *mark) {
858 mark_t *current;
859 TAILQ_FOREACH (current, &(con->marks_head), marks) {
860 if (strcmp(current->name, mark) == 0) {
861 return true;
862 }
863 }
864
865 return false;
866}
867
868/*
869 * Toggles the mark on a container.
870 * If the container already has this mark, the mark is removed.
871 * Otherwise, the mark is assigned to the container.
872 *
873 */
874void con_mark_toggle(Con *con, const char *mark, mark_mode_t mode) {
875 assert(con != NULL);
876 DLOG("Toggling mark \"%s\" on con = %p.\n", mark, con);
877
878 if (con_has_mark(con, mark)) {
879 con_unmark(con, mark);
880 } else {
881 con_mark(con, mark, mode);
882 }
883}
884
885/*
886 * Assigns a mark to the container.
887 *
888 */
889void con_mark(Con *con, const char *mark, mark_mode_t mode) {
890 assert(con != NULL);
891 DLOG("Setting mark \"%s\" on con = %p.\n", mark, con);
892
893 con_unmark(NULL, mark);
894 if (mode == MM_REPLACE) {
895 DLOG("Removing all existing marks on con = %p.\n", con);
896
897 mark_t *current;
898 while (!TAILQ_EMPTY(&(con->marks_head))) {
899 current = TAILQ_FIRST(&(con->marks_head));
900 con_unmark(con, current->name);
901 }
902 }
903
904 mark_t *new = scalloc(1, sizeof(mark_t));
905 new->name = sstrdup(mark);
906 TAILQ_INSERT_TAIL(&(con->marks_head), new, marks);
907 ipc_send_window_event("mark", con);
908
909 con->mark_changed = true;
910}
911
912/*
913 * Removes marks from containers.
914 * If con is NULL, all containers are considered.
915 * If name is NULL, this removes all existing marks.
916 * Otherwise, it will only remove the given mark (if it is present).
917 *
918 */
919void con_unmark(Con *con, const char *name) {
920 Con *current;
921 if (name == NULL) {
922 DLOG("Unmarking all containers.\n");
923 TAILQ_FOREACH (current, &all_cons, all_cons) {
924 if (con != NULL && current != con) {
925 continue;
926 }
927
928 if (TAILQ_EMPTY(&(current->marks_head))) {
929 continue;
930 }
931
932 mark_t *mark;
933 while (!TAILQ_EMPTY(&(current->marks_head))) {
934 mark = TAILQ_FIRST(&(current->marks_head));
935 FREE(mark->name);
936 TAILQ_REMOVE(&(current->marks_head), mark, marks);
937 FREE(mark);
938
939 ipc_send_window_event("mark", current);
940 }
941
942 current->mark_changed = true;
943 }
944 } else {
945 DLOG("Removing mark \"%s\".\n", name);
946 current = (con == NULL) ? con_by_mark(name) : con;
947 if (current == NULL) {
948 DLOG("No container found with this mark, so there is nothing to do.\n");
949 return;
950 }
951
952 DLOG("Found mark on con = %p. Removing it now.\n", current);
953 current->mark_changed = true;
954
955 mark_t *mark;
956 TAILQ_FOREACH (mark, &(current->marks_head), marks) {
957 if (strcmp(mark->name, name) != 0) {
958 continue;
959 }
960
961 FREE(mark->name);
962 TAILQ_REMOVE(&(current->marks_head), mark, marks);
963 FREE(mark);
964
965 ipc_send_window_event("mark", current);
966 break;
967 }
968 }
969}
970
971/*
972 * Returns the first container below 'con' which wants to swallow this window
973 * TODO: priority
974 *
975 */
976Con *con_for_window(Con *con, i3Window *window, Match **store_match) {
977 Con *child;
978 Match *match;
979
980 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
981 TAILQ_FOREACH (match, &(child->swallow_head), matches) {
982 if (!match_matches_window(match, window)) {
983 continue;
984 }
985 if (store_match != NULL) {
986 *store_match = match;
987 }
988 return child;
989 }
990 Con *result = con_for_window(child, window, store_match);
991 if (result != NULL) {
992 return result;
993 }
994 }
995
996 TAILQ_FOREACH (child, &(con->floating_head), floating_windows) {
997 TAILQ_FOREACH (match, &(child->swallow_head), matches) {
998 if (!match_matches_window(match, window)) {
999 continue;
1000 }
1001 if (store_match != NULL) {
1002 *store_match = match;
1003 }
1004 return child;
1005 }
1006 Con *result = con_for_window(child, window, store_match);
1007 if (result != NULL) {
1008 return result;
1009 }
1010 }
1011
1012 return NULL;
1013}
1014
1016 int focus_heads = 0;
1017
1018 Con *current;
1019 TAILQ_FOREACH (current, &(con->focus_head), focused) {
1020 focus_heads++;
1021 }
1022
1023 return focus_heads;
1024}
1025
1026/*
1027 * Iterate over the container's focus stack and return an array with the
1028 * containers inside it, ordered from higher focus order to lowest.
1029 *
1030 */
1032 const int focus_heads = num_focus_heads(con);
1033 Con **focus_order = smalloc(focus_heads * sizeof(Con *));
1034 Con *current;
1035 int idx = 0;
1036 TAILQ_FOREACH (current, &(con->focus_head), focused) {
1037 assert(idx < focus_heads);
1038 focus_order[idx++] = current;
1039 }
1040
1041 return focus_order;
1042}
1043
1044/*
1045 * Clear the container's focus stack and re-add it using the provided container
1046 * array. The function doesn't check if the provided array contains the same
1047 * containers with the previous focus stack but will not add floating containers
1048 * in the new focus stack if container is not a workspace.
1049 *
1050 */
1051void set_focus_order(Con *con, Con **focus_order) {
1052 int focus_heads = 0;
1053 while (!TAILQ_EMPTY(&(con->focus_head))) {
1054 Con *current = TAILQ_FIRST(&(con->focus_head));
1055
1056 TAILQ_REMOVE(&(con->focus_head), current, focused);
1057 focus_heads++;
1058 }
1059
1060 for (int idx = 0; idx < focus_heads; idx++) {
1061 /* Useful when encapsulating a workspace. */
1062 if (con->type != CT_WORKSPACE && con_inside_floating(focus_order[idx])) {
1063 focus_heads++;
1064 continue;
1065 }
1066
1067 TAILQ_INSERT_TAIL(&(con->focus_head), focus_order[idx], focused);
1068 }
1069}
1070
1071/*
1072 * Returns the number of children of this container.
1073 *
1074 */
1076 Con *child;
1077 int children = 0;
1078
1079 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
1080 children++;
1081 }
1082
1083 return children;
1084}
1085
1086/*
1087 * Returns the number of visible non-floating children of this container.
1088 * For example, if the container contains a hsplit which has two children,
1089 * this will return 2 instead of 1.
1090 */
1092 if (con == NULL) {
1093 return 0;
1094 }
1095
1096 int children = 0;
1097 Con *current = NULL;
1098 TAILQ_FOREACH (current, &(con->nodes_head), nodes) {
1099 /* Visible leaf nodes are a child. */
1100 if (!con_is_hidden(current) && con_is_leaf(current)) {
1101 children++;
1102 }
1103 /* All other containers need to be recursed. */
1104 else {
1105 children += con_num_visible_children(current);
1106 }
1107 }
1108
1109 return children;
1110}
1111
1112/*
1113 * Count the number of windows (i.e., leaf containers).
1114 *
1115 */
1117 if (con == NULL) {
1118 return 0;
1119 }
1120
1122 return 1;
1123 }
1124
1125 int num = 0;
1126 Con *current = NULL;
1127 TAILQ_FOREACH (current, &(con->nodes_head), nodes) {
1128 num += con_num_windows(current);
1129 }
1130
1131 TAILQ_FOREACH (current, &(con->floating_head), floating_windows) {
1132 num += con_num_windows(current);
1133 }
1134
1135 return num;
1136}
1137
1138/*
1139 * Updates the percent attribute of the children of the given container. This
1140 * function needs to be called when a window is added or removed from a
1141 * container.
1142 *
1143 */
1145 Con *child;
1146 int children = con_num_children(con);
1147
1148 /* calculate how much we have distributed and how many containers with a
1149 * percentage set we have */
1150 double total = 0.0;
1151 int children_with_percent = 0;
1152 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
1153 if (child->percent > 0.0) {
1154 total += child->percent;
1155 ++children_with_percent;
1156 }
1157 }
1158
1159 /* if there were children without a percentage set, set to a value that
1160 * will make those children proportional to all others */
1161 if (children_with_percent != children) {
1162 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
1163 if (child->percent <= 0.0) {
1164 if (children_with_percent == 0) {
1165 total += (child->percent = 1.0);
1166 } else {
1167 total += (child->percent = total / children_with_percent);
1168 }
1169 }
1170 }
1171 }
1172
1173 /* if we got a zero, just distribute the space equally, otherwise
1174 * distribute according to the proportions we got */
1175 if (total == 0.0) {
1176 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
1177 child->percent = 1.0 / children;
1178 }
1179 } else if (total != 1.0) {
1180 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
1181 child->percent /= total;
1182 }
1183 }
1184}
1185
1186/*
1187 * Toggles fullscreen mode for the given container. If there already is a
1188 * fullscreen container on this workspace, fullscreen will be disabled and then
1189 * enabled for the container the user wants to have in fullscreen mode.
1190 *
1191 */
1192void con_toggle_fullscreen(Con *con, int fullscreen_mode) {
1193 if (con->type == CT_WORKSPACE) {
1194 DLOG("You cannot make a workspace fullscreen.\n");
1195 return;
1196 }
1197
1198 DLOG("toggling fullscreen for %p / %s\n", con, con->name);
1199
1200 if (con->fullscreen_mode == CF_NONE) {
1201 con_enable_fullscreen(con, fullscreen_mode);
1202 } else {
1204 }
1205}
1206
1207/*
1208 * Sets the specified fullscreen mode for the given container, sends the
1209 * “fullscreen_mode” event and changes the XCB fullscreen property of the
1210 * container’s window, if any.
1211 *
1212 */
1213static void con_set_fullscreen_mode(Con *con, fullscreen_mode_t fullscreen_mode) {
1214 con->fullscreen_mode = fullscreen_mode;
1215
1216 DLOG("mode now: %d\n", con->fullscreen_mode);
1217
1218 /* Send an ipc window "fullscreen_mode" event */
1219 ipc_send_window_event("fullscreen_mode", con);
1220
1221 /* update _NET_WM_STATE if this container has a window */
1222 /* TODO: when a window is assigned to a container which is already
1223 * fullscreened, this state needs to be pushed to the client, too */
1224 if (con->window == NULL) {
1225 return;
1226 }
1227
1228 if (con->fullscreen_mode != CF_NONE) {
1229 DLOG("Setting _NET_WM_STATE_FULLSCREEN for con = %p / window = %d.\n", con, con->window->id);
1230 xcb_add_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_FULLSCREEN);
1231 } else {
1232 DLOG("Removing _NET_WM_STATE_FULLSCREEN for con = %p / window = %d.\n", con, con->window->id);
1233 xcb_remove_property_atom(conn, con->window->id, A__NET_WM_STATE, A__NET_WM_STATE_FULLSCREEN);
1234 }
1235}
1236
1237/*
1238 * Enables fullscreen mode for the given container, if necessary.
1239 *
1240 * If the container’s mode is already CF_OUTPUT or CF_GLOBAL, the container is
1241 * kept fullscreen but its mode is set to CF_GLOBAL and CF_OUTPUT,
1242 * respectively.
1243 *
1244 * Other fullscreen containers will be disabled first, if they hide the new
1245 * one.
1246 *
1247 */
1249 if (con->type == CT_WORKSPACE) {
1250 DLOG("You cannot make a workspace fullscreen.\n");
1251 return;
1252 }
1253
1254 assert(fullscreen_mode == CF_GLOBAL || fullscreen_mode == CF_OUTPUT);
1255
1256 if (fullscreen_mode == CF_GLOBAL) {
1257 DLOG("enabling global fullscreen for %p / %s\n", con, con->name);
1258 } else {
1259 DLOG("enabling fullscreen for %p / %s\n", con, con->name);
1260 }
1261
1262 if (con->fullscreen_mode == fullscreen_mode) {
1263 DLOG("fullscreen already enabled for %p / %s\n", con, con->name);
1264 return;
1265 }
1266
1267 Con *con_ws = con_get_workspace(con);
1268
1269 /* Disable any fullscreen container that would conflict the new one. */
1270 Con *fullscreen = con_get_fullscreen_con(croot, CF_GLOBAL);
1271 if (fullscreen == NULL) {
1272 fullscreen = con_get_fullscreen_con(con_ws, CF_OUTPUT);
1273 }
1274 if (fullscreen != NULL) {
1275 con_disable_fullscreen(fullscreen);
1276 }
1277
1278 /* Set focus to new fullscreen container. Unless in global fullscreen mode
1279 * and on another workspace restore focus afterwards.
1280 * Switch to the container’s workspace if mode is global. */
1281 Con *cur_ws = con_get_workspace(focused);
1282 Con *old_focused = focused;
1283 if (fullscreen_mode == CF_GLOBAL && cur_ws != con_ws) {
1284 workspace_show(con_ws);
1285 }
1287 if (fullscreen_mode != CF_GLOBAL && cur_ws != con_ws) {
1288 con_activate(old_focused);
1289 }
1290
1291 con_set_fullscreen_mode(con, fullscreen_mode);
1292}
1293
1294/*
1295 * Disables fullscreen mode for the given container regardless of the mode, if
1296 * necessary.
1297 *
1298 */
1300 if (con->type == CT_WORKSPACE) {
1301 DLOG("You cannot make a workspace fullscreen.\n");
1302 return;
1303 }
1304
1305 DLOG("disabling fullscreen for %p / %s\n", con, con->name);
1306
1307 if (con->fullscreen_mode == CF_NONE) {
1308 DLOG("fullscreen already disabled for %p / %s\n", con, con->name);
1309 return;
1310 }
1311
1313}
1314
1315static bool _con_move_to_con(Con *con, Con *target, bool behind_focused, bool fix_coordinates, bool dont_warp, bool ignore_focus, bool fix_percentage) {
1316 Con *orig_target = target;
1317
1318 /* Prevent moving if this would violate the fullscreen focus restrictions. */
1319 Con *target_ws = con_get_workspace(target);
1320 if (!ignore_focus && !con_fullscreen_permits_focusing(target_ws)) {
1321 LOG("Cannot move out of a fullscreen container.\n");
1322 return false;
1323 }
1324
1325 if (con_is_floating(con)) {
1326 DLOG("Container is floating, using parent instead.\n");
1327 con = con->parent;
1328 }
1329
1330 Con *source_ws = con_get_workspace(con);
1331
1332 if (con->type == CT_WORKSPACE) {
1333 /* Re-parent all of the old workspace's floating windows. */
1334 Con *child;
1335 while (!TAILQ_EMPTY(&(source_ws->floating_head))) {
1336 child = TAILQ_FIRST(&(source_ws->floating_head));
1337 con_move_to_workspace(child, target_ws, true, true, false);
1338 }
1339
1340 /* If there are no non-floating children, ignore the workspace. */
1341 if (con_is_leaf(con)) {
1342 return false;
1343 }
1344
1346 if (con == NULL) {
1347 ELOG("Workspace failed to move its contents into a container!\n");
1348 return false;
1349 }
1350 }
1351
1352 /* Save the urgency state so that we can restore it. */
1353 bool urgent = con->urgent;
1354
1355 /* Save the current workspace. So we can call workspace_show() by the end
1356 * of this function. */
1357 Con *current_ws = con_get_workspace(focused);
1358
1359 Con *source_output = con_get_output(con),
1360 *dest_output = con_get_output(target_ws);
1361
1362 /* 1: save the container which is going to be focused after the current
1363 * container is moved away */
1364 Con *focus_next = NULL;
1365 if (!ignore_focus && source_ws == current_ws && target_ws != source_ws) {
1366 focus_next = con_descend_focused(source_ws);
1367 if (focus_next == con || con_has_parent(focus_next, con)) {
1368 focus_next = con_next_focused(con);
1369 }
1370 }
1371
1372 /* 2: we go up one level, but only when target is a normal container */
1373 if (target->type != CT_WORKSPACE) {
1374 DLOG("target originally = %p / %s / type %d\n", target, target->name, target->type);
1375 target = target->parent;
1376 }
1377
1378 /* 3: if the original target is the direct child of a floating container, we
1379 * can't move con next to it - floating containers have only one child - so
1380 * we get the workspace instead. */
1381 if (target->type == CT_FLOATING_CON) {
1382 DLOG("floatingcon, going up even further\n");
1383 orig_target = target;
1384 target = target->parent;
1385 }
1386
1387 if (con->type == CT_FLOATING_CON) {
1388 Con *ws = con_get_workspace(target);
1389 DLOG("This is a floating window, using workspace %p / %s\n", ws, ws->name);
1390 target = ws;
1391 }
1392
1393 if (source_output != dest_output) {
1394 /* Take the relative coordinates of the current output, then add them
1395 * to the coordinate space of the correct output */
1396 if (fix_coordinates && con->type == CT_FLOATING_CON) {
1397 floating_fix_coordinates(con, &(source_output->rect), &(dest_output->rect));
1398 } else {
1399 DLOG("Not fixing coordinates, fix_coordinates flag = %d\n", fix_coordinates);
1400 }
1401 }
1402
1403 /* If moving a fullscreen container and the destination already has a
1404 * fullscreen window on it, un-fullscreen the target's fullscreen con.
1405 * con->fullscreen_mode is not enough in some edge cases:
1406 * 1. con is CT_FLOATING_CON, child is fullscreen.
1407 * 2. con is the parent of a fullscreen container, can be triggered by
1408 * moving the parent with command criteria.
1409 */
1410 Con *fullscreen = con_get_fullscreen_con(target_ws, CF_OUTPUT);
1411 const bool con_has_fullscreen = con->fullscreen_mode != CF_NONE ||
1414 if (con_has_fullscreen && fullscreen != NULL) {
1415 con_toggle_fullscreen(fullscreen, CF_OUTPUT);
1416 fullscreen = NULL;
1417 }
1418
1419 DLOG("Re-attaching container to %p / %s\n", target, target->name);
1420 /* 4: re-attach the con to the parent of this focused container */
1421 Con *parent = con->parent;
1422 con_detach(con);
1423 _con_attach(con, target, behind_focused ? NULL : orig_target, !behind_focused);
1424
1425 /* 5: fix the percentages */
1426 if (fix_percentage) {
1427 con_fix_percent(parent);
1428 con->percent = 0.0;
1429 con_fix_percent(target);
1430 }
1431
1432 /* 6: focus the con on the target workspace, but only within that
1433 * workspace, that is, don’t move focus away if the target workspace is
1434 * invisible.
1435 * We don’t focus the con for i3 pseudo workspaces like __i3_scratch and
1436 * we don’t focus when there is a fullscreen con on that workspace. We
1437 * also don't do it if the caller requested to ignore focus. */
1438 if (!ignore_focus && !con_is_internal(target_ws) && !fullscreen) {
1439 /* We need to save the focused workspace on the output in case the
1440 * new workspace is hidden and it's necessary to immediately switch
1441 * back to the originally-focused workspace. */
1442 Con *old_focus_ws = TAILQ_FIRST(&(output_get_content(dest_output)->focus_head));
1443 Con *old_focus = focused;
1445
1446 if (old_focus_ws == current_ws && old_focus->type != CT_WORKSPACE) {
1447 /* Restore focus to the currently focused container. */
1448 con_activate(old_focus);
1449 } else if (con_get_workspace(focused) != old_focus_ws) {
1450 /* Restore focus if the output's focused workspace has changed. */
1451 con_focus(con_descend_focused(old_focus_ws));
1452 }
1453 }
1454
1455 /* 7: when moving to another workspace, we leave the focus on the current
1456 * workspace. (see also #809) */
1457 if (!ignore_focus) {
1458 workspace_show(current_ws);
1459 if (dont_warp) {
1460 DLOG("x_set_warp_to(NULL) because dont_warp is set\n");
1461 x_set_warp_to(NULL);
1462 }
1463 }
1464
1465 /* Set focus only if con was on current workspace before moving.
1466 * Otherwise we would give focus to some window on different workspace. */
1467 if (focus_next) {
1468 con_activate(con_descend_focused(focus_next));
1469 }
1470
1471 /* 8. If anything within the container is associated with a startup sequence,
1472 * delete it so child windows won't be created on the old workspace. */
1473 if (!con_is_leaf(con)) {
1474 Con *child;
1475 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
1476 if (!child->window) {
1477 continue;
1478 }
1480 }
1481 }
1482
1483 if (con->window) {
1485 }
1486
1487 /* 9. If the container was marked urgent, move the urgency hint. */
1488 if (urgent) {
1490 con_set_urgency(con, true);
1491 }
1492
1493 /* Ensure the container will be redrawn. */
1495
1496 CALL(parent, on_remove_child);
1497
1498 ipc_send_window_event("move", con);
1500 return true;
1501}
1502
1504 /* For target containers in the scratchpad, we just send the window to the scratchpad. */
1505 if (con_get_workspace(target) == workspace_get("__i3_scratch")) {
1506 DLOG("target container is in the scratchpad, moving container to scratchpad.\n");
1508 return true;
1509 }
1510
1511 /* For floating target containers, we just send the window to the same workspace. */
1512 if (con_is_floating(target)) {
1513 DLOG("target container is floating, moving container to target's workspace.\n");
1514 con_move_to_workspace(con, con_get_workspace(target), true, false, false);
1515 return true;
1516 }
1517
1518 if (target->type == CT_WORKSPACE && con_is_leaf(target)) {
1519 DLOG("target container is an empty workspace, simply moving the container there.\n");
1520 con_move_to_workspace(con, target, true, false, false);
1521 return true;
1522 }
1523
1524 /* For split containers, we use the currently focused container within it.
1525 * This allows setting marks on, e.g., tabbed containers which will move
1526 * con to a new tab behind the focused tab. */
1527 if (con_is_split(target)) {
1528 DLOG("target is a split container, descending to the currently focused child.\n");
1529 target = TAILQ_FIRST(&(target->focus_head));
1530 }
1531
1532 if (con == target || con_has_parent(target, con)) {
1533 DLOG("cannot move the container to or inside itself, aborting.\n");
1534 return false;
1535 }
1536
1537 return _con_move_to_con(con, target, false, true, false, false, true);
1538}
1539
1540/*
1541 * Moves the given container to the given mark.
1542 *
1543 */
1544bool con_move_to_mark(Con *con, const char *mark) {
1545 Con *target = con_by_mark(mark);
1546 if (target == NULL) {
1547 DLOG("found no container with mark \"%s\"\n", mark);
1548 return false;
1549 }
1550
1551 return con_move_to_target(con, target);
1552}
1553
1554/*
1555 * Moves the given container to the currently focused container on the given
1556 * workspace.
1557 *
1558 * The fix_coordinates flag will translate the current coordinates (offset from
1559 * the monitor position basically) to appropriate coordinates on the
1560 * destination workspace.
1561 * Not enabling this behaviour comes in handy when this function gets called by
1562 * floating_maybe_reassign_ws, which will only "move" a floating window when it
1563 * *already* changed its coordinates to a different output.
1564 *
1565 * The dont_warp flag disables pointer warping and will be set when this
1566 * function is called while dragging a floating window.
1567 *
1568 * If ignore_focus is set, the container will be moved without modifying focus
1569 * at all.
1570 *
1571 * TODO: is there a better place for this function?
1572 *
1573 */
1574void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp, bool ignore_focus) {
1575 assert(workspace->type == CT_WORKSPACE);
1576
1577 Con *source_ws = con_get_workspace(con);
1578 if (workspace == source_ws) {
1579 DLOG("Not moving, already there\n");
1580 return;
1581 }
1582
1583 Con *target = con_descend_focused(workspace);
1584 _con_move_to_con(con, target, true, fix_coordinates, dont_warp, ignore_focus, true);
1585}
1586
1587/*
1588 * Moves the given container to the currently focused container on the
1589 * visible workspace on the given output.
1590 *
1591 */
1592void con_move_to_output(Con *con, Output *output, bool fix_coordinates) {
1593 Con *ws = NULL;
1595 assert(ws != NULL);
1596 DLOG("Moving con %p to output %s\n", con, output_primary_name(output));
1597 con_move_to_workspace(con, ws, fix_coordinates, false, false);
1598}
1599
1600/*
1601 * Moves the given container to the currently focused container on the
1602 * visible workspace on the output specified by the given name.
1603 * The current output for the container is used to resolve relative names
1604 * such as left, right, up, down.
1605 *
1606 */
1607bool con_move_to_output_name(Con *con, const char *name, bool fix_coordinates) {
1608 Output *current_output = get_output_for_con(con);
1609 Output *output = get_output_from_string(current_output, name);
1610 if (output == NULL) {
1611 ELOG("Could not find output \"%s\"\n", name);
1612 return false;
1613 }
1614
1615 con_move_to_output(con, output, fix_coordinates);
1616 return true;
1617}
1618
1619/*
1620 * Returns the orientation of the given container (for stacked containers,
1621 * vertical orientation is used regardless of the actual orientation of the
1622 * container).
1623 *
1624 */
1626 switch (con->layout) {
1627 case L_SPLITV:
1628 /* stacking containers behave like they are in vertical orientation */
1629 case L_STACKED:
1630 return VERT;
1631
1632 case L_SPLITH:
1633 /* tabbed containers behave like they are in vertical orientation */
1634 case L_TABBED:
1635 return HORIZ;
1636
1637 case L_DEFAULT:
1638 ELOG("Someone called con_orientation() on a con with L_DEFAULT, this is a bug in the code.\n");
1639 assert(false);
1640
1641 case L_DOCKAREA:
1642 case L_OUTPUT:
1643 ELOG("con_orientation() called on dockarea/output (%d) container %p\n", con->layout, con);
1644 assert(false);
1645 }
1646 /* should not be reached */
1647 assert(false);
1648}
1649
1650/*
1651 * Returns the container which will be focused next when the given container
1652 * is not available anymore. Called in tree_close_internal and con_move_to_workspace
1653 * to properly restore focus.
1654 *
1655 */
1657 /* dock clients cannot be focused, so we focus the workspace instead */
1658 if (con->parent->type == CT_DOCKAREA) {
1659 DLOG("selecting workspace for dock client\n");
1661 }
1662 if (con_is_floating(con)) {
1663 con = con->parent;
1664 }
1665
1666 /* if 'con' is not the first entry in the focus stack, use the first one as
1667 * it’s currently focused already */
1668 Con *next = TAILQ_FIRST(&(con->parent->focus_head));
1669 if (next != con) {
1670 DLOG("Using first entry %p\n", next);
1671 } else {
1672 /* try to focus the next container on the same level as this one or fall
1673 * back to its parent */
1674 if (!(next = TAILQ_NEXT(con, focused))) {
1675 next = con->parent;
1676 }
1677 }
1678
1679 /* now go down the focus stack as far as
1680 * possible, excluding the current container */
1681 while (!TAILQ_EMPTY(&(next->focus_head)) && TAILQ_FIRST(&(next->focus_head)) != con) {
1682 next = TAILQ_FIRST(&(next->focus_head));
1683 }
1684
1685 if (con->type == CT_FLOATING_CON && next != con->parent) {
1686 next = con_descend_focused(next);
1687 }
1688
1689 return next;
1690}
1691
1692/*
1693 * Returns the focused con inside this client, descending the tree as far as
1694 * possible. This comes in handy when attaching a con to a workspace at the
1695 * currently focused position, for example.
1696 *
1697 */
1699 Con *next = con;
1700 while (next != focused && !TAILQ_EMPTY(&(next->focus_head))) {
1701 next = TAILQ_FIRST(&(next->focus_head));
1702 }
1703 return next;
1704}
1705
1706/*
1707 * Returns the focused con inside this client, descending the tree as far as
1708 * possible. This comes in handy when attaching a con to a workspace at the
1709 * currently focused position, for example.
1710 *
1711 * Works like con_descend_focused but considers only tiling cons.
1712 *
1713 */
1715 Con *next = con;
1716 Con *before;
1717 Con *child;
1718 if (next == focused) {
1719 return next;
1720 }
1721 do {
1722 before = next;
1723 TAILQ_FOREACH (child, &(next->focus_head), focused) {
1724 if (child->type == CT_FLOATING_CON) {
1725 continue;
1726 }
1727
1728 next = child;
1729 break;
1730 }
1731 } while (before != next && next != focused);
1732 return next;
1733}
1734
1735/*
1736 * Returns the leftmost, rightmost, etc. container in sub-tree. For example, if
1737 * direction is D_LEFT, then we return the rightmost container and if direction
1738 * is D_RIGHT, we return the leftmost container. This is because if we are
1739 * moving D_LEFT, and thus want the rightmost container.
1740 *
1741 */
1743 Con *most = NULL;
1744 Con *current;
1745 int orientation = con_orientation(con);
1746 DLOG("con_descend_direction(%p, orientation %d, direction %d)\n", con, orientation, direction);
1747 if (direction == D_LEFT || direction == D_RIGHT) {
1748 if (orientation == HORIZ) {
1749 /* If the direction is horizontal, we can use either the first
1750 * (D_RIGHT) or the last con (D_LEFT) */
1751 if (direction == D_RIGHT) {
1752 most = TAILQ_FIRST(&(con->nodes_head));
1753 } else {
1754 most = TAILQ_LAST(&(con->nodes_head), nodes_head);
1755 }
1756 } else if (orientation == VERT) {
1757 /* Wrong orientation. We use the last focused con. Within that con,
1758 * we recurse to chose the left/right con or at least the last
1759 * focused one. */
1760 TAILQ_FOREACH (current, &(con->focus_head), focused) {
1761 if (current->type != CT_FLOATING_CON) {
1762 most = current;
1763 break;
1764 }
1765 }
1766 } else {
1767 /* If the con has no orientation set, it’s not a split container
1768 * but a container with a client window, so stop recursing */
1769 return con;
1770 }
1771 }
1772
1773 if (direction == D_UP || direction == D_DOWN) {
1774 if (orientation == VERT) {
1775 /* If the direction is vertical, we can use either the first
1776 * (D_DOWN) or the last con (D_UP) */
1777 if (direction == D_UP) {
1778 most = TAILQ_LAST(&(con->nodes_head), nodes_head);
1779 } else {
1780 most = TAILQ_FIRST(&(con->nodes_head));
1781 }
1782 } else if (orientation == HORIZ) {
1783 /* Wrong orientation. We use the last focused con. Within that con,
1784 * we recurse to chose the top/bottom con or at least the last
1785 * focused one. */
1786 TAILQ_FOREACH (current, &(con->focus_head), focused) {
1787 if (current->type != CT_FLOATING_CON) {
1788 most = current;
1789 break;
1790 }
1791 }
1792 } else {
1793 /* If the con has no orientation set, it’s not a split container
1794 * but a container with a client window, so stop recursing */
1795 return con;
1796 }
1797 }
1798
1799 if (!most) {
1800 return con;
1801 }
1802 return con_descend_direction(most, direction);
1803}
1804
1805static bool has_outer_gaps(gaps_t gaps) {
1806 return gaps.top > 0 ||
1807 gaps.right > 0 ||
1808 gaps.bottom > 0 ||
1809 gaps.left > 0;
1810}
1811
1812/*
1813 * Returns whether the window decoration (title bar) should be drawn into the
1814 * X11 frame window of this container (default) or into the X11 frame window of
1815 * the parent container (for stacked/tabbed containers).
1816 *
1817 */
1819 return con_is_leaf(con) &&
1821 (con->parent == NULL ||
1822 (con->parent->layout != L_TABBED &&
1823 con->parent->layout != L_STACKED));
1824}
1825
1829 if (!con_is_floating(con)) {
1830 return (Rect){0, 0, 0, 0};
1831 }
1832 }
1833
1834 adjacent_t borders_to_hide = ADJ_NONE;
1835 int border_width = con->current_border_width;
1836 DLOG("The border width for con is set to: %d\n", con->current_border_width);
1837 Rect result;
1838 if (con->current_border_width < 0) {
1839 if (con_is_floating(con)) {
1841 } else {
1842 border_width = config.default_border_width;
1843 }
1844 }
1845 DLOG("Effective border width is set to: %d\n", border_width);
1846 /* Shortcut to avoid calling con_adjacent_borders() on dock containers. */
1847 int border_style = con_border_style(con);
1848 if (border_style == BS_NONE) {
1849 return (Rect){0, 0, 0, 0};
1850 }
1851 if (border_style == BS_NORMAL) {
1852 result = (Rect){border_width, 0, -(2 * border_width), -(border_width)};
1853 } else {
1854 result = (Rect){border_width, border_width, -(2 * border_width), -(2 * border_width)};
1855 }
1856
1857 /* If hide_edge_borders is set to no_gaps and it did not pass the no border check, show all borders */
1859 borders_to_hide = con_adjacent_borders(con) & HEBM_NONE;
1860 } else {
1861 borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
1862 }
1863
1864 if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
1865 result.x -= border_width;
1866 result.width += border_width;
1867 }
1868 if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
1869 result.width += border_width;
1870 }
1871 if (borders_to_hide & ADJ_UPPER_SCREEN_EDGE && (border_style != BS_NORMAL)) {
1872 result.y -= border_width;
1873 result.height += border_width;
1874 }
1875 if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
1876 result.height += border_width;
1877 }
1878 return result;
1879}
1880
1881/*
1882 * Returns a "relative" Rect which contains the amount of pixels that need to
1883 * be added to the original Rect to get the final position (obviously the
1884 * amount of pixels for normal, 1pixel and borderless are different).
1885 *
1886 */
1889 if (con_border_style(con) == BS_NORMAL &&
1891 const int deco_height = render_deco_height();
1892 result.y += deco_height;
1893 result.height -= deco_height;
1894 }
1895 return result;
1896}
1897
1898/*
1899 * Returns adjacent borders of the window. We need this if hide_edge_borders is
1900 * enabled.
1901 */
1903 adjacent_t result = ADJ_NONE;
1904 /* Floating windows are never adjacent to any other window, so
1905 don’t hide their border(s). This prevents bug #998. */
1906 if (con_is_floating(con)) {
1907 return result;
1908 }
1909
1910 Con *workspace = con_get_workspace(con);
1911 if (con->rect.x == workspace->rect.x) {
1912 result |= ADJ_LEFT_SCREEN_EDGE;
1913 }
1914 if (con->rect.x + con->rect.width == workspace->rect.x + workspace->rect.width) {
1915 result |= ADJ_RIGHT_SCREEN_EDGE;
1916 }
1917 if (con->rect.y == workspace->rect.y) {
1918 result |= ADJ_UPPER_SCREEN_EDGE;
1919 }
1920 if (con->rect.y + con->rect.height == workspace->rect.y + workspace->rect.height) {
1921 result |= ADJ_LOWER_SCREEN_EDGE;
1922 }
1923 return result;
1924}
1925
1926/*
1927 * Use this function to get a container’s border style. This is important
1928 * because when inside a stack, the border style is always BS_NORMAL.
1929 * For tabbed mode, the same applies, with one exception: when the container is
1930 * borderless and the only element in the tabbed container, the border is not
1931 * rendered.
1932 *
1933 * For children of a CT_DOCKAREA, the border style is always none.
1934 *
1935 */
1938 DLOG("this one is fullscreen! overriding BS_NONE\n");
1939 return BS_NONE;
1940 }
1941
1942 if (con->parent != NULL) {
1943 if (con->parent->layout == L_STACKED) {
1944 return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1945 }
1946
1948 return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1949 }
1950
1951 if (con->parent->type == CT_DOCKAREA) {
1952 return BS_NONE;
1953 }
1954 }
1955
1956 return con->border_style;
1957}
1958
1959/*
1960 * Sets the given border style on con, correctly keeping the position/size of a
1961 * floating window.
1962 *
1963 */
1964void con_set_border_style(Con *con, border_style_t border_style, int border_width) {
1965 if (border_style > con->max_user_border_style) {
1966 border_style = con->max_user_border_style;
1967 }
1968
1969 /* Handle the simple case: non-floating containerns */
1970 if (!con_is_floating(con)) {
1971 con->border_style = border_style;
1972 con->current_border_width = border_width;
1973 return;
1974 }
1975
1976 /* For floating containers, we want to keep the position/size of the
1977 * *window* itself. We first add the border pixels to con->rect to make
1978 * con->rect represent the absolute position of the window (same for
1979 * parent). Then, we change the border style and subtract the new border
1980 * pixels. For the parent, we do the same also for the decoration. */
1981 Con *parent = con->parent;
1983
1984 con->rect = rect_add(con->rect, bsr);
1985 parent->rect = rect_add(parent->rect, bsr);
1986
1987 /* Change the border style, get new border/decoration values. */
1988 con->border_style = border_style;
1989 con->current_border_width = border_width;
1991
1992 con->rect = rect_sub(con->rect, bsr);
1993 parent->rect = rect_sub(parent->rect, bsr);
1994}
1995
1996/*
1997 * This function changes the layout of a given container. Use it to handle
1998 * special cases like changing a whole workspace to stacked/tabbed (creates a
1999 * new split container before).
2000 *
2001 */
2003 DLOG("con_set_layout(%p, %d), con->type = %d\n",
2004 con, layout, con->type);
2005
2006 /* Users can focus workspaces, but not any higher in the hierarchy.
2007 * Focus on the workspace is a special case, since in every other case, the
2008 * user means "change the layout of the parent split container". */
2009 if (con->type != CT_WORKSPACE) {
2010 con = con->parent;
2011 }
2012
2013 /* We fill in last_split_layout when switching to a different layout
2014 * since there are many places in the code that don’t use
2015 * con_set_layout(). */
2016 if (con->layout == L_SPLITH || con->layout == L_SPLITV) {
2018 }
2019
2020 /* When the container type is CT_WORKSPACE, the user wants to change the
2021 * whole workspace into stacked/tabbed mode. To do this and still allow
2022 * intuitive operations (like level-up and then opening a new window), we
2023 * need to create a new split container. */
2024 if (con->type == CT_WORKSPACE) {
2025 if (con_num_children(con) == 0) {
2026 layout_t ws_layout = (layout == L_STACKED || layout == L_TABBED) ? layout : L_DEFAULT;
2027 DLOG("Setting workspace_layout to %d\n", ws_layout);
2028 con->workspace_layout = ws_layout;
2029 DLOG("Setting layout to %d\n", layout);
2030 con->layout = layout;
2031 } else if (layout == L_STACKED || layout == L_TABBED || layout == L_SPLITV || layout == L_SPLITH) {
2032 DLOG("Creating new split container\n");
2033 /* 1: create a new split container */
2034 Con *new = con_new(NULL, NULL);
2035 new->parent = con;
2036
2037 /* 2: Set the requested layout on the split container and mark it as
2038 * split. */
2039 new->layout = layout;
2040 new->last_split_layout = con->last_split_layout;
2041
2042 /* 3: move the existing cons of this workspace below the new con */
2043 Con **focus_order = get_focus_order(con);
2044
2045 DLOG("Moving cons\n");
2046 Con *child;
2047 while (!TAILQ_EMPTY(&(con->nodes_head))) {
2048 child = TAILQ_FIRST(&(con->nodes_head));
2049 con_detach(child);
2050 con_attach(child, new, true);
2051 }
2052
2053 set_focus_order(new, focus_order);
2054 free(focus_order);
2055
2056 /* 4: attach the new split container to the workspace */
2057 DLOG("Attaching new split to ws\n");
2058 con_attach(new, con, false);
2059
2062 return;
2063 }
2064 }
2065
2066 if (con->type != CT_WORKSPACE && con->parent->type != CT_WORKSPACE &&
2068 /* Special case: Avoid creating redundant containers (#3001):
2069 * split h / v (tree_split()) will avoid creating new containers when
2070 * the target container is already a single child in L_SPLITH /
2071 * L_SPLITV. However, if the layout is tabbed / stacked, a new split is
2072 * created. This means, however, that when the user continuously
2073 * switches between split h/v and tabbed / stacked, an endless series
2074 * of 1-child containers will be created. Since a single level of split
2075 * containers on top of tabbed / stacked containers are useful, we want
2076 * to avoid this situation here.
2077 * Example of past behaviour: S[V[w]] -> S[S[w]] -> S[S[V[w]]] -> …
2078 * Example of desired behaviour: S[V[w]] -> S[w] -> S[v[w]] -> …
2079 * Therefore, when both the current & parent containers have a single
2080 * child, we just close the redundant middle container and proceed with
2081 * the parent. */
2082 Con *parent = con->parent;
2083 Con *child = TAILQ_FIRST(&(con->nodes_head));
2084 con_detach(child);
2085 con_attach(child, parent, true);
2088 con = parent;
2089 }
2090
2091 if (layout == L_DEFAULT) {
2092 /* Special case: the layout formerly known as "default" (in combination
2093 * with an orientation). Since we switched to splith/splitv layouts,
2094 * using the "default" layout (which "only" should happen when using
2095 * legacy configs) is using the last split layout (either splith or
2096 * splitv) in order to still do the same thing. */
2098 /* In case last_split_layout was not initialized… */
2099 if (con->layout == L_DEFAULT) {
2100 con->layout = L_SPLITH;
2101 }
2102 } else {
2103 con->layout = layout;
2104 }
2106}
2107
2108/*
2109 * This function toggles the layout of a given container. toggle_mode can be
2110 * either 'default' (toggle only between stacked/tabbed/last_split_layout),
2111 * 'split' (toggle only between splitv/splith) or 'all' (toggle between all
2112 * layouts).
2113 *
2114 */
2115void con_toggle_layout(Con *con, const char *toggle_mode) {
2116 Con *parent = con;
2117 /* Users can focus workspaces, but not any higher in the hierarchy.
2118 * Focus on the workspace is a special case, since in every other case, the
2119 * user means "change the layout of the parent split container". */
2120 if (con->type != CT_WORKSPACE) {
2121 parent = con->parent;
2122 }
2123 DLOG("con_toggle_layout(%p, %s), parent = %p\n", con, toggle_mode, parent);
2124
2125 const char delim[] = " ";
2126
2127 if (strcasecmp(toggle_mode, "split") == 0 || strstr(toggle_mode, delim)) {
2128 /* L_DEFAULT is used as a placeholder value to distinguish if
2129 * the first layout has already been saved. (it can never be L_DEFAULT) */
2130 layout_t new_layout = L_DEFAULT;
2131 bool current_layout_found = false;
2132 char *tm_dup = sstrdup(toggle_mode);
2133 char *cur_tok = strtok(tm_dup, delim);
2134
2135 for (layout_t layout; cur_tok != NULL; cur_tok = strtok(NULL, delim)) {
2136 if (strcasecmp(cur_tok, "split") == 0) {
2137 /* Toggle between splits. When the current layout is not a split
2138 * layout, we just switch back to last_split_layout. Otherwise, we
2139 * change to the opposite split layout. */
2140 if (parent->layout != L_SPLITH && parent->layout != L_SPLITV) {
2141 layout = parent->last_split_layout;
2142 /* In case last_split_layout was not initialized… */
2143 if (layout == L_DEFAULT) {
2144 layout = L_SPLITH;
2145 }
2146 } else {
2147 layout = (parent->layout == L_SPLITH) ? L_SPLITV : L_SPLITH;
2148 }
2149 } else {
2150 bool success = layout_from_name(cur_tok, &layout);
2151 if (!success || layout == L_DEFAULT) {
2152 ELOG("The token '%s' was not recognized and has been skipped.\n", cur_tok);
2153 continue;
2154 }
2155 }
2156
2157 /* If none of the specified layouts match the current,
2158 * fall back to the first layout in the list */
2159 if (new_layout == L_DEFAULT) {
2160 new_layout = layout;
2161 }
2162
2163 /* We found the active layout in the last iteration, so
2164 * now let's activate the current layout (next in list) */
2165 if (current_layout_found) {
2166 new_layout = layout;
2167 break;
2168 }
2169
2170 if (parent->layout == layout) {
2171 current_layout_found = true;
2172 }
2173 }
2174 free(tm_dup);
2175
2176 if (new_layout != L_DEFAULT) {
2177 con_set_layout(con, new_layout);
2178 }
2179 } else if (strcasecmp(toggle_mode, "all") == 0 || strcasecmp(toggle_mode, "default") == 0) {
2180 if (parent->layout == L_STACKED) {
2182 } else if (parent->layout == L_TABBED) {
2183 if (strcasecmp(toggle_mode, "all") == 0) {
2185 } else {
2187 }
2188 } else if (parent->layout == L_SPLITH || parent->layout == L_SPLITV) {
2189 if (strcasecmp(toggle_mode, "all") == 0) {
2190 /* When toggling through all modes, we toggle between
2191 * splith/splitv, whereas normally we just directly jump to
2192 * stacked. */
2193 if (parent->layout == L_SPLITH) {
2195 } else {
2197 }
2198 } else {
2200 }
2201 }
2202 }
2203}
2204
2205/*
2206 * Callback which will be called when removing a child from the given con.
2207 * Kills the container if it is empty and replaces it with the child if there
2208 * is exactly one child.
2209 *
2210 */
2212 DLOG("on_remove_child\n");
2213
2214 /* Every container 'above' (in the hierarchy) the workspace content should
2215 * not be closed when the last child was removed */
2216 if (con->type == CT_OUTPUT ||
2217 con->type == CT_ROOT ||
2218 con->type == CT_DOCKAREA ||
2219 (con->parent != NULL && con->parent->type == CT_OUTPUT)) {
2220 DLOG("not handling, type = %d, name = %s\n", con->type, con->name);
2221 return;
2222 }
2223
2224 /* For workspaces, close them only if they're not visible anymore */
2225 if (con->type == CT_WORKSPACE) {
2226 if (TAILQ_EMPTY(&(con->focus_head)) && !workspace_is_visible(con)) {
2227 LOG("Closing old workspace (%p / %s), it is empty\n", con, con->name);
2228 yajl_gen gen = ipc_marshal_workspace_event("empty", con, NULL);
2230
2231 const unsigned char *payload;
2232 ylength length;
2233 y(get_buf, &payload, &length);
2234 ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, (const char *)payload);
2235
2236 y(free);
2237 }
2238 return;
2239 }
2240
2244
2245 /* TODO: check if this container would swallow any other client and
2246 * don’t close it automatically. */
2247 int children = con_num_children(con);
2248 if (children == 0) {
2249 DLOG("Container empty, closing\n");
2251 return;
2252 }
2253}
2254
2255/*
2256 * Determines the minimum size of the given con by looking at its children (for
2257 * split/stacked/tabbed cons). Will be called when resizing floating cons
2258 *
2259 */
2261 DLOG("Determining minimum size for con %p\n", con);
2262
2263 if (con_is_leaf(con)) {
2264 DLOG("leaf node, returning 75x50\n");
2265 return (Rect){0, 0, 75, 50};
2266 }
2267
2268 if (con->type == CT_FLOATING_CON) {
2269 DLOG("floating con\n");
2270 Con *child = TAILQ_FIRST(&(con->nodes_head));
2271 return con_minimum_size(child);
2272 }
2273
2274 if (con->layout == L_STACKED || con->layout == L_TABBED) {
2275 uint32_t max_width = 0, max_height = 0, deco_height = 0;
2276 Con *child;
2277 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
2278 Rect min = con_minimum_size(child);
2279 deco_height += child->deco_rect.height;
2280 max_width = max(max_width, min.width);
2281 max_height = max(max_height, min.height);
2282 }
2283 DLOG("stacked/tabbed now, returning %d x %d + deco_rect = %d\n",
2284 max_width, max_height, deco_height);
2285 return (Rect){0, 0, max_width, max_height + deco_height};
2286 }
2287
2288 /* For horizontal/vertical split containers we sum up the width (h-split)
2289 * or height (v-split) and use the maximum of the height (h-split) or width
2290 * (v-split) as minimum size. */
2291 if (con_is_split(con)) {
2292 uint32_t width = 0, height = 0;
2293 Con *child;
2294 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
2295 Rect min = con_minimum_size(child);
2296 if (con->layout == L_SPLITH) {
2297 width += min.width;
2298 height = max(height, min.height);
2299 } else {
2300 height += min.height;
2301 width = max(width, min.width);
2302 }
2303 }
2304 DLOG("split container, returning width = %d x height = %d\n", width, height);
2305 return (Rect){0, 0, width, height};
2306 }
2307
2308 ELOG("Unhandled case, type = %d, layout = %d, split = %d\n",
2310 assert(false);
2311}
2312
2313/*
2314 * Returns true if changing the focus to con would be allowed considering
2315 * the fullscreen focus constraints. Specifically, if a fullscreen container or
2316 * any of its descendants is focused, this function returns true if and only if
2317 * focusing con would mean that focus would still be visible on screen, i.e.,
2318 * the newly focused container would not be obscured by a fullscreen container.
2319 *
2320 * In the simplest case, if a fullscreen container or any of its descendants is
2321 * fullscreen, this functions returns true if con is the fullscreen container
2322 * itself or any of its descendants, as this means focus wouldn't escape the
2323 * boundaries of the fullscreen container.
2324 *
2325 * In case the fullscreen container is of type CF_OUTPUT, this function returns
2326 * true if con is on a different workspace, as focus wouldn't be obscured by
2327 * the fullscreen container that is constrained to a different workspace.
2328 *
2329 * Note that this same logic can be applied to moving containers. If a
2330 * container can be focused under the fullscreen focus constraints, it can also
2331 * become a parent or sibling to the currently focused container.
2332 *
2333 */
2335 /* No focus, no problem. */
2336 if (!focused) {
2337 return true;
2338 }
2339
2340 /* Find the first fullscreen ascendent. */
2341 Con *fs = focused;
2342 while (fs && fs->fullscreen_mode == CF_NONE) {
2343 fs = fs->parent;
2344 }
2345
2346 /* fs must be non-NULL since the workspace con doesn’t have CF_NONE and
2347 * there always has to be a workspace con in the hierarchy. */
2348 assert(fs != NULL);
2349 /* The most common case is we hit the workspace level. In this
2350 * situation, changing focus is also harmless. */
2351 assert(fs->fullscreen_mode != CF_NONE);
2352 if (fs->type == CT_WORKSPACE) {
2353 return true;
2354 }
2355
2356 /* Allow it if the container itself is the fullscreen container. */
2357 if (con == fs) {
2358 return true;
2359 }
2360
2361 /* If fullscreen is per-output, the focus being in a different workspace is
2362 * sufficient to guarantee that change won't leave fullscreen in bad shape. */
2363 if (fs->fullscreen_mode == CF_OUTPUT &&
2365 return true;
2366 }
2367
2368 /* Allow it only if the container to be focused is contained within the
2369 * current fullscreen container. */
2370 return con_has_parent(con, fs);
2371}
2372
2373/*
2374 *
2375 * Checks if the given container has an urgent child.
2376 *
2377 */
2379 Con *child;
2380
2381 if (con_is_leaf(con)) {
2382 return con->urgent;
2383 }
2384
2385 /* We are not interested in floating windows since they can only be
2386 * attached to a workspace → nodes_head instead of focus_head */
2387 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
2388 if (con_has_urgent_child(child)) {
2389 return true;
2390 }
2391 }
2392
2393 return false;
2394}
2395
2396/*
2397 * Make all parent containers urgent if con is urgent or clear the urgent flag
2398 * of all parent containers if there are no more urgent children left.
2399 *
2400 */
2402 Con *parent = con->parent;
2403
2404 /* Urgency hints should not be set on any container higher up in the
2405 * hierarchy than the workspace level. Unfortunately, since the content
2406 * container has type == CT_CON, that’s not easy to verify in the loop
2407 * below, so we need another condition to catch that case: */
2408 if (con->type == CT_WORKSPACE) {
2409 return;
2410 }
2411
2412 bool new_urgency_value = con->urgent;
2413 while (parent && parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
2414 if (new_urgency_value) {
2415 parent->urgent = true;
2416 } else {
2417 /* We can only reset the urgency when the parent
2418 * has no other urgent children */
2419 if (!con_has_urgent_child(parent)) {
2420 parent->urgent = false;
2421 }
2422 }
2423 parent = parent->parent;
2424 }
2425}
2426
2427/*
2428 * Set urgency flag to the container, all the parent containers and the workspace.
2429 *
2430 */
2431void con_set_urgency(Con *con, bool urgent) {
2432 if (urgent && focused == con) {
2433 DLOG("Ignoring urgency flag for current client\n");
2434 return;
2435 }
2436
2437 const bool old_urgent = con->urgent;
2438
2439 if (con->urgency_timer == NULL) {
2440 con->urgent = urgent;
2441 } else {
2442 DLOG("Discarding urgency WM_HINT because timer is running\n");
2443 }
2444
2445 if (con->window) {
2446 if (con->urgent) {
2447 gettimeofday(&con->window->urgent, NULL);
2448 } else {
2449 con->window->urgent.tv_sec = 0;
2450 con->window->urgent.tv_usec = 0;
2451 }
2452 }
2453
2455
2456 Con *ws;
2457 /* Set the urgency flag on the workspace, if a workspace could be found
2458 * (for dock clients, that is not the case). */
2459 if ((ws = con_get_workspace(con)) != NULL) {
2461 }
2462
2463 if (con->urgent != old_urgent) {
2464 LOG("Urgency flag changed to %d\n", con->urgent);
2465 ipc_send_window_event("urgent", con);
2466 }
2467}
2468
2469/*
2470 * Create a string representing the subtree under con.
2471 *
2472 */
2474 /* this code works as follows:
2475 * 1) create a string with the layout type (D/V/H/T/S) and an opening bracket
2476 * 2) append the tree representation of the children to the string
2477 * 3) add closing bracket
2478 *
2479 * The recursion ends when we hit a leaf, in which case we return the
2480 * class_instance of the contained window.
2481 */
2482
2483 /* end of recursion */
2484 if (con_is_leaf(con)) {
2485 if (!con->window) {
2486 return sstrdup("nowin");
2487 }
2488
2489 if (!con->window->class_instance) {
2490 return sstrdup("noinstance");
2491 }
2492
2493 return sstrdup(con->window->class_instance);
2494 }
2495
2496 char *buf;
2497 /* 1) add the Layout type to buf */
2498 if (con->layout == L_DEFAULT) {
2499 buf = sstrdup("D[");
2500 } else if (con->layout == L_SPLITV) {
2501 buf = sstrdup("V[");
2502 } else if (con->layout == L_SPLITH) {
2503 buf = sstrdup("H[");
2504 } else if (con->layout == L_TABBED) {
2505 buf = sstrdup("T[");
2506 } else if (con->layout == L_STACKED) {
2507 buf = sstrdup("S[");
2508 } else {
2509 ELOG("BUG: Code not updated to account for new layout type\n");
2510 assert(false);
2511 }
2512
2513 /* 2) append representation of children */
2514 Con *child;
2515 TAILQ_FOREACH (child, &(con->nodes_head), nodes) {
2516 char *child_txt = con_get_tree_representation(child);
2517
2518 char *tmp_buf;
2519 sasprintf(&tmp_buf, "%s%s%s", buf,
2520 (TAILQ_FIRST(&(con->nodes_head)) == child ? "" : " "), child_txt);
2521 free(buf);
2522 buf = tmp_buf;
2523 free(child_txt);
2524 }
2525
2526 /* 3) close the brackets */
2527 char *complete_buf;
2528 sasprintf(&complete_buf, "%s]", buf);
2529 free(buf);
2530
2531 return complete_buf;
2532}
2533
2534/*
2535 * Returns the container's title considering the current title format.
2536 *
2537 */
2539 assert(con->title_format != NULL);
2540
2541 i3Window *win = con->window;
2542
2543 /* We need to ensure that we only escape the window title if pango
2544 * is used by the current font. */
2545 const bool pango_markup = font_is_pango();
2546
2547 char *title;
2548 char *class;
2549 char *instance;
2550 char *machine;
2551 if (win == NULL) {
2553 class = sstrdup("i3-frame");
2554 instance = sstrdup("i3-frame");
2555 machine = sstrdup("");
2556 } else {
2557 title = pango_escape_markup(sstrdup((win->name == NULL) ? "" : i3string_as_utf8(win->name)));
2558 class = pango_escape_markup(sstrdup((win->class_class == NULL) ? "" : win->class_class));
2559 instance = pango_escape_markup(sstrdup((win->class_instance == NULL) ? "" : win->class_instance));
2560 machine = pango_escape_markup(sstrdup((win->machine == NULL) ? "" : win->machine));
2561 }
2562
2563 placeholder_t placeholders[] = {
2564 {.name = "%title", .value = title},
2565 {.name = "%class", .value = class},
2566 {.name = "%instance", .value = instance},
2567 {.name = "%machine", .value = machine},
2568 };
2569 const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
2570
2571 char *formatted_str = format_placeholders(con->title_format, &placeholders[0], num);
2572 i3String *formatted = i3string_from_utf8(formatted_str);
2573 i3string_set_markup(formatted, pango_markup);
2574
2575 free(formatted_str);
2576 free(title);
2577 free(class);
2578 free(instance);
2579
2580 return formatted;
2581}
2582
2583/*
2584 * Swaps the two containers.
2585 *
2586 */
2587bool con_swap(Con *first, Con *second) {
2588 assert(first != NULL);
2589 assert(second != NULL);
2590 DLOG("Swapping containers %p / %p\n", first, second);
2591
2592 if (first->type != CT_CON) {
2593 ELOG("Only regular containers can be swapped, but found con = %p with type = %d.\n", first, first->type);
2594 return false;
2595 }
2596
2597 if (second->type != CT_CON) {
2598 ELOG("Only regular containers can be swapped, but found con = %p with type = %d.\n", second, second->type);
2599 return false;
2600 }
2601
2602 if (first == second) {
2603 DLOG("Swapping container %p with itself, nothing to do.\n", first);
2604 return false;
2605 }
2606
2607 if (con_has_parent(first, second) || con_has_parent(second, first)) {
2608 ELOG("Cannot swap containers %p and %p because they are in a parent-child relationship.\n", first, second);
2609 return false;
2610 }
2611
2612 Con *ws1 = con_get_workspace(first);
2613 Con *ws2 = con_get_workspace(second);
2614 Con *restore_focus = NULL;
2615 if (ws1 == ws2 && ws1 == con_get_workspace(focused)) {
2616 /* Preserve focus in the current workspace. */
2617 restore_focus = focused;
2618 } else if (first == focused || con_has_parent(focused, first)) {
2619 restore_focus = second;
2620 } else if (second == focused || con_has_parent(focused, second)) {
2621 restore_focus = first;
2622 }
2623
2624#define SWAP_CONS_IN_TREE(headname, field) \
2625 do { \
2626 struct headname *head1 = &(first->parent->headname); \
2627 struct headname *head2 = &(second->parent->headname); \
2628 Con *first_prev = TAILQ_PREV(first, headname, field); \
2629 Con *second_prev = TAILQ_PREV(second, headname, field); \
2630 if (second_prev == first) { \
2631 TAILQ_SWAP(first, second, head1, field); \
2632 } else if (first_prev == second) { \
2633 TAILQ_SWAP(second, first, head1, field); \
2634 } else { \
2635 TAILQ_REMOVE(head1, first, field); \
2636 TAILQ_REMOVE(head2, second, field); \
2637 if (second_prev == NULL) { \
2638 TAILQ_INSERT_HEAD(head2, first, field); \
2639 } else { \
2640 TAILQ_INSERT_AFTER(head2, second_prev, first, field); \
2641 } \
2642 if (first_prev == NULL) { \
2643 TAILQ_INSERT_HEAD(head1, second, field); \
2644 } else { \
2645 TAILQ_INSERT_AFTER(head1, first_prev, second, field); \
2646 } \
2647 } \
2648 } while (0)
2649
2650 SWAP_CONS_IN_TREE(nodes_head, nodes);
2651 SWAP_CONS_IN_TREE(focus_head, focused);
2652 SWAP(first->parent, second->parent, Con *);
2653
2654 /* Floating nodes are children of CT_FLOATING_CONs, they are listed in
2655 * nodes_head and focus_head like all other containers. Thus, we don't need
2656 * to do anything special other than swapping the floating status and the
2657 * relevant rects. */
2658 SWAP(first->floating, second->floating, int);
2659 SWAP(first->rect, second->rect, Rect);
2660 SWAP(first->window_rect, second->window_rect, Rect);
2661
2662 /* We need to copy each other's percentages to ensure that the geometry
2663 * doesn't change during the swap. */
2664 SWAP(first->percent, second->percent, double);
2665
2666 if (restore_focus) {
2667 con_focus(restore_focus);
2668 }
2669
2670 /* Update new parents' & workspaces' urgency. */
2671 con_set_urgency(first, first->urgent);
2672 con_set_urgency(second, second->urgent);
2673
2674 /* Exchange fullscreen modes, can't use SWAP because we need to call the
2675 * correct functions. */
2676 fullscreen_mode_t second_fullscreen_mode = second->fullscreen_mode;
2677 if (first->fullscreen_mode == CF_NONE) {
2678 con_disable_fullscreen(second);
2679 } else {
2680 con_enable_fullscreen(second, first->fullscreen_mode);
2681 }
2682 if (second_fullscreen_mode == CF_NONE) {
2684 } else {
2685 con_enable_fullscreen(first, second_fullscreen_mode);
2686 }
2687
2688 /* We don't actually need this since percentages-wise we haven't changed
2689 * anything, but we'll better be safe than sorry and just make sure as we'd
2690 * otherwise crash i3. */
2691 con_fix_percent(first->parent);
2692 con_fix_percent(second->parent);
2693
2694 FREE(first->deco_render_params);
2695 FREE(second->deco_render_params);
2698
2699 return true;
2700}
2701
2702/*
2703 * Returns container's rect size depending on its orientation.
2704 * i.e. its width when horizontal, its height when vertical.
2705 *
2706 */
2708 return (con_orientation(con) == HORIZ ? con->rect.width : con->rect.height);
2709}
2710
2711/*
2712 * Merges container specific data that should move with the window (e.g. marks,
2713 * title format, and the window itself) into another container, and closes the
2714 * old container.
2715 *
2716 */
2717void con_merge_into(Con *old, Con *new) {
2718 new->window = old->window;
2719 old->window = NULL;
2720
2721 if (old->title_format) {
2722 FREE(new->title_format);
2723 new->title_format = old->title_format;
2724 old->title_format = NULL;
2725 }
2726
2727 if (old->sticky_group) {
2728 FREE(new->sticky_group);
2729 new->sticky_group = old->sticky_group;
2730 old->sticky_group = NULL;
2731 }
2732
2733 new->sticky = old->sticky;
2734
2735 con_set_urgency(new, old->urgent);
2736
2737 mark_t *mark;
2738 TAILQ_FOREACH (mark, &(old->marks_head), marks) {
2739 TAILQ_INSERT_TAIL(&(new->marks_head), mark, marks);
2740 ipc_send_window_event("mark", new);
2741 }
2742 new->mark_changed = (TAILQ_FIRST(&(old->marks_head)) != NULL);
2743 TAILQ_INIT(&(old->marks_head));
2744
2746}
2747
2748/*
2749 * Returns true if the container is within any stacked/tabbed split container.
2750 *
2751 */
2753 if (con->parent == NULL) {
2754 return false;
2755 }
2756 if (con->parent->layout == L_STACKED ||
2757 con->parent->layout == L_TABBED) {
2758 return true;
2759 }
2761}
#define y(x,...)
Definition commands.c:18
char * con_get_tree_representation(Con *con)
Create a string representing the subtree under con.
Definition con.c:2473
void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp, bool ignore_focus)
Moves the given container to the currently focused container on the given workspace.
Definition con.c:1574
bool con_move_to_target(Con *con, Con *target)
Definition con.c:1503
bool con_move_to_mark(Con *con, const char *mark)
Moves the given container to the given mark.
Definition con.c:1544
bool con_move_to_output_name(Con *con, const char *name, bool fix_coordinates)
Moves the given container to the currently focused container on the visible workspace on the output s...
Definition con.c:1607
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
bool con_is_floating(Con *con)
Returns true if the node is floating.
Definition con.c:670
bool con_inside_stacked_or_tabbed(Con *con)
Returns true if the container is within any stacked/tabbed split container.
Definition con.c:2752
Con * con_for_window(Con *con, i3Window *window, Match **store_match)
Returns the first container below 'con' which wants to swallow this window TODO: priority.
Definition con.c:976
void con_close(Con *con, kill_window_t kill_window)
Closes the given container.
Definition con.c:336
static void con_raise(Con *con)
Definition con.c:281
orientation_t con_orientation(Con *con)
Returns the orientation of the given container (for stacked containers, vertical orientation is used ...
Definition con.c:1625
void con_force_split_parents_redraw(Con *con)
force parent split containers to be redrawn
Definition con.c:21
void con_unmark(Con *con, const char *name)
Removes marks from containers.
Definition con.c:919
bool con_has_managed_window(Con *con)
Returns true when this con is a leaf node with a managed X11 window (e.g., excluding dock containers)
Definition con.c:374
Con * con_descend_direction(Con *con, direction_t direction)
Returns the leftmost, rightmost, etc.
Definition con.c:1742
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
bool con_is_hidden(Con *con)
This will only return true for containers which have some parent with a tabbed / stacked parent of wh...
Definition con.c:410
Con * con_get_workspace(Con *con)
Gets the workspace container this node is on.
Definition con.c:547
bool con_find_transient_for_window(Con *start, xcb_window_t target)
Start from a container and traverse the transient_for linked list.
Definition con.c:824
bool con_is_split(Con *con)
Returns true if a container should be considered split.
Definition con.c:390
bool con_has_children(Con *con)
Returns true if this node has regular or floating children.
Definition con.c:382
bool con_is_maximized(Con *con, orientation_t orientation)
Returns true if the container is maximized in the given orientation.
Definition con.c:442
static Rect con_border_style_rect_without_title(Con *con)
Definition con.c:1826
Con * con_descend_tiling_focused(Con *con)
Returns the focused con inside this client, descending the tree as far as possible.
Definition con.c:1714
void con_disable_fullscreen(Con *con)
Disables fullscreen mode for the given container, if necessary.
Definition con.c:1299
static int num_focus_heads(Con *con)
Definition con.c:1015
bool con_is_docked(Con *con)
Returns true if the container is a docked container.
Definition con.c:679
void con_set_border_style(Con *con, border_style_t border_style, int border_width)
Sets the given border style on con, correctly keeping the position/size of a floating window.
Definition con.c:1964
int con_num_visible_children(Con *con)
Returns the number of visible non-floating children of this container.
Definition con.c:1091
Con * con_by_con_id(long target)
Returns the container with the given container ID or NULL if no such container exists.
Definition con.c:767
Con * con_new_skeleton(Con *parent, i3Window *window)
Create a new container (and attach it to the given parent, if not NULL).
Definition con.c:38
void con_mark(Con *con, const char *mark, mark_mode_t mode)
Assigns a mark to the container.
Definition con.c:889
Con * con_by_window_id(xcb_window_t window)
Returns the container with the given client window ID or NULL if no such container exists.
Definition con.c:752
void con_activate_unblock(Con *con)
Activates the container like in con_activate but removes fullscreen restrictions and properly warps t...
Definition con.c:302
Con * con_by_mark(const char *mark)
Returns the container with the given mark or NULL if no such container exists.
Definition con.c:807
static void con_on_remove_child(Con *con)
Definition con.c:2211
Rect con_border_style_rect(Con *con)
Returns a "relative" Rect which contains the amount of pixels that need to be added to the original R...
Definition con.c:1887
Con * con_get_fullscreen_covering_ws(Con *ws)
Returns the fullscreen node that covers the given workspace if it exists.
Definition con.c:647
void con_mark_toggle(Con *con, const char *mark, mark_mode_t mode)
Toggles the mark on a container.
Definition con.c:874
Con * con_inside_floating(Con *con)
Checks if the given container is either floating or inside some floating container.
Definition con.c:696
bool con_fullscreen_permits_focusing(Con *con)
Returns true if changing the focus to con would be allowed considering the fullscreen focus constrain...
Definition con.c:2334
bool con_swap(Con *first, Con *second)
Swaps the two containers.
Definition con.c:2587
bool con_accepts_window(Con *con)
Returns true if this node accepts a window (if the node swallows windows, it might already have swall...
Definition con.c:512
uint32_t con_rect_size_in_orientation(Con *con)
Returns given container's rect size depending on its orientation.
Definition con.c:2707
bool con_is_internal(Con *con)
Returns true if the container is internal, such as __i3_scratch.
Definition con.c:662
bool con_exists(Con *con)
Returns true if the given container (still) exists.
Definition con.c:783
void con_detach(Con *con)
Detaches the given container from its current parent.
Definition con.c:234
void con_move_to_output(Con *con, Output *output, bool fix_coordinates)
Moves the given container to the currently focused container on the visible workspace on the given ou...
Definition con.c:1592
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
int con_border_style(Con *con)
Use this function to get a container’s border style.
Definition con.c:1936
void con_toggle_fullscreen(Con *con, int fullscreen_mode)
Toggles fullscreen mode for the given container.
Definition con.c:1192
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
Rect con_minimum_size(Con *con)
Determines the minimum size of the given con by looking at its children (for split/stacked/tabbed con...
Definition con.c:2260
i3String * con_parse_title_format(Con *con)
Returns the window title considering the current title format.
Definition con.c:2538
static void _con_attach(Con *con, Con *parent, Con *previous, bool ignore_focus)
Definition con.c:100
static bool has_outer_gaps(gaps_t gaps)
Definition con.c:1805
bool con_inside_focused(Con *con)
Checks if the given container is inside a focused container.
Definition con.c:720
#define SWAP_CONS_IN_TREE(headname, field)
void con_free(Con *con)
Frees the specified container.
Definition con.c:80
static bool _con_move_to_con(Con *con, Con *target, bool behind_focused, bool fix_coordinates, bool dont_warp, bool ignore_focus, bool fix_percentage)
Definition con.c:1315
void con_merge_into(Con *old, Con *new)
Merges container specific data that should move with the window (e.g.
Definition con.c:2717
Con * con_by_frame_id(xcb_window_t frame)
Returns the container with the given frame ID or NULL if no such container exists.
Definition con.c:792
bool con_is_leaf(Con *con)
Returns true when this node is a leaf node (has no children)
Definition con.c:366
int con_num_children(Con *con)
Returns the number of children of this container.
Definition con.c:1075
adjacent_t con_adjacent_borders(Con *con)
Returns adjacent borders of the window.
Definition con.c:1902
void con_set_layout(Con *con, layout_t layout)
This function changes the layout of a given container.
Definition con.c:2002
void con_activate(Con *con)
Sets input focus to the given container and raises it to the top.
Definition con.c:292
Con * con_parent_with_orientation(Con *con, orientation_t orientation)
Searches parents of the given 'con' until it reaches one with the specified 'orientation'.
Definition con.c:560
bool con_draw_decoration_into_frame(Con *con)
Returns whether the window decoration (title bar) should be drawn into the X11 frame window of this c...
Definition con.c:1818
void con_toggle_layout(Con *con, const char *toggle_mode)
This function toggles the layout of a given container.
Definition con.c:2115
bool con_has_mark(Con *con, const char *mark)
Returns true if and only if the given containers holds the mark.
Definition con.c:857
Con * con_next_focused(Con *con)
Returns the container which will be focused next when the given container is not available anymore.
Definition con.c:1656
bool con_has_urgent_child(Con *con)
Checks if the given container has an urgent child.
Definition con.c:2378
void con_enable_fullscreen(Con *con, fullscreen_mode_t fullscreen_mode)
Enables fullscreen mode for the given container, if necessary.
Definition con.c:1248
bool con_has_parent(Con *con, Con *parent)
Checks if the container has the given parent as an actual parent.
Definition con.c:734
int con_num_windows(Con *con)
Count the number of windows (i.e., leaf containers).
Definition con.c:1116
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
bool con_is_sticky(Con *con)
Returns whether the container or any of its children is sticky.
Definition con.c:492
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
static void con_set_fullscreen_mode(Con *con, fullscreen_mode_t fullscreen_mode)
Definition con.c:1213
Config config
Definition config.c:19
void ewmh_update_wm_desktop(void)
Updates _NET_WM_DESKTOP for all windows.
Definition ewmh.c:186
void floating_raise_con(Con *con)
Raises the given container in the list of floating containers.
Definition floating.c:479
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 calculate_effective_gaps(Con *con)
Calculates the effective gap sizes for a container.
Definition gaps.c:16
struct pending_marks * marks
void match_free(Match *match)
Frees the given match.
Definition match.c:279
bool match_matches_window(Match *match, i3Window *window)
Check if a match data structure matches the given window.
Definition match.c:90
Output * get_output_from_string(Output *current_output, const char *output_str)
Returns an 'output' corresponding to one of left/right/down/up or a specific output name.
Definition output.c:33
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
int render_deco_height(void)
Returns the height for the decorations.
Definition render.c:27
void scratchpad_move(Con *con)
Moves the specified window to the __i3_scratch workspace, making it floating and setting the appropri...
Definition scratchpad.c:19
void startup_sequence_delete_by_window(i3Window *win)
Deletes the startup sequence for a window if it exists.
Definition startup.c:372
struct Con * focused
Definition tree.c:13
void tree_flatten(Con *con)
tree_flatten() removes pairs of redundant split containers, e.g.: [workspace, horizontal] [v-split] [...
Definition tree.c:660
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
struct all_cons_head all_cons
Definition tree.c:15
Rect rect_add(Rect a, Rect b)
Definition util.c:39
char * pango_escape_markup(char *input)
Escapes the given string if a pango font is currently used.
Definition util.c:317
int min(int a, int b)
Definition util.c:24
bool layout_from_name(const char *layout_str, layout_t *out)
Set 'out' to the layout_t value for the given layout.
Definition util.c:84
Rect rect_sub(Rect a, Rect b)
Definition util.c:46
int max(int a, int b)
Definition util.c:28
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
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
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
void x_con_init(Con *con)
Initializes the X11 part for the given container.
Definition x.c:129
void x_set_warp_to(Rect *rect)
Set warp_to coordinates.
Definition x.c:1545
void xcb_add_property_atom(xcb_connection_t *conn, xcb_window_t window, xcb_atom_t property, xcb_atom_t atom)
Add an atom to a list of atoms the given property defines.
Definition xcb.c:224
void xcb_remove_property_atom(xcb_connection_t *conn, xcb_window_t window, xcb_atom_t property, xcb_atom_t atom)
Remove an atom from a list of atoms the given property defines without removing any other potentially...
Definition xcb.c:234
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
xcb_connection_t * conn
XCB connection and root screen.
Definition main.c:54
uint8_t root_depth
Definition main.c:75
@ HEBM_SMART_NO_GAPS
Definition data.h:93
@ HEBM_SMART
Definition data.h:92
@ HEBM_NONE
Definition data.h:88
struct Rect Rect
Definition data.h:44
layout_t
Container layouts.
Definition data.h:101
@ L_STACKED
Definition data.h:103
@ L_TABBED
Definition data.h:104
@ L_DOCKAREA
Definition data.h:105
@ L_OUTPUT
Definition data.h:106
@ L_SPLITH
Definition data.h:108
@ L_SPLITV
Definition data.h:107
@ L_DEFAULT
Definition data.h:102
mark_mode_t
Definition data.h:95
@ MM_REPLACE
Definition data.h:95
orientation_t
Definition data.h:60
@ VERT
Definition data.h:62
@ HORIZ
Definition data.h:61
adjacent_t
describes if the window is adjacent to the output (physical screen) edges.
Definition data.h:78
@ ADJ_LEFT_SCREEN_EDGE
Definition data.h:79
@ ADJ_LOWER_SCREEN_EDGE
Definition data.h:82
@ ADJ_RIGHT_SCREEN_EDGE
Definition data.h:80
@ ADJ_UPPER_SCREEN_EDGE
Definition data.h:81
@ ADJ_NONE
Definition data.h:78
fullscreen_mode_t
Fullscreen modes.
Definition data.h:629
@ CF_OUTPUT
Definition data.h:630
@ CF_GLOBAL
Definition data.h:631
@ CF_NONE
Definition data.h:629
border_style_t
Definition data.h:65
@ BS_NONE
Definition data.h:66
@ BS_NORMAL
Definition data.h:68
kill_window_t
parameter to specify whether tree_close_internal() and x_window_kill() should kill only this specific...
Definition data.h:73
@ DONT_KILL_WINDOW
Definition data.h:73
direction_t
Definition data.h:56
@ D_RIGHT
Definition data.h:57
@ D_LEFT
Definition data.h:56
@ D_UP
Definition data.h:58
@ D_DOWN
Definition data.h:59
struct _i3String i3String
Opaque data structure for storing strings.
Definition libi3.h:49
#define DLOG(fmt,...)
Definition libi3.h:105
#define LOG(fmt,...)
Definition libi3.h:95
int strcasecmp_nullable(const char *a, const char *b)
Like strcasecmp but considers the case where either string is NULL.
char * sstrdup(const char *str)
Safe-wrapper around strdup which exits if malloc returns NULL (meaning that there is no more memory a...
char * format_placeholders(char *format, placeholder_t *placeholders, int num)
Replaces occurrences of the defined placeholders in the format string.
#define ELOG(fmt,...)
Definition libi3.h:100
const char * i3string_as_utf8(i3String *str)
Returns the UTF-8 encoded version of the i3String.
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 i3string_set_markup(i3String *str, bool pango_markup)
Set whether the i3String should use Pango markup.
i3String * i3string_from_utf8(const char *from_utf8)
Build an i3String from an UTF-8 encoded string.
bool font_is_pango(void)
Returns true if and only if the current font is a pango font.
void * smalloc(size_t size)
Safe-wrapper around malloc which exits if malloc returns NULL (meaning that there is no more memory a...
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:347
#define TAILQ_END(head)
Definition queue.h:337
#define TAILQ_INIT(head)
Definition queue.h:360
#define TAILQ_HEAD(name, type)
Definition queue.h:318
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:376
#define TAILQ_FIRST(head)
Definition queue.h:336
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:402
#define TAILQ_NEXT(elm, field)
Definition queue.h:338
#define TAILQ_HEAD_INITIALIZER(head)
Definition queue.h:324
#define TAILQ_EMPTY(head)
Definition queue.h:344
#define TAILQ_INSERT_BEFORE(listelm, elm, field)
Definition queue.h:394
#define TAILQ_LAST(head, headname)
Definition queue.h:339
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition queue.h:366
#define TAILQ_ENTRY(type)
Definition queue.h:327
#define TAILQ_INSERT_AFTER(head, listelm, elm, field)
Definition queue.h:384
#define CALL(obj, member,...)
Definition util.h:53
#define GREP_FIRST(dest, head, condition)
Definition util.h:38
#define SWAP(first, second, type)
Definition util.h:55
#define FREE(pointer)
Definition util.h:47
size_t ylength
Definition yajl_utils.h:24
Con * con
Definition con.c:590
int default_border_width
hide_edge_borders_mode_t hide_edge_borders
Remove borders if they are adjacent to the screen edge.
int default_floating_border_width
border_style_t default_border
The default border style for new windows.
Definition data.h:146
int left
Definition data.h:151
int right
Definition data.h:149
int top
Definition data.h:148
int bottom
Definition data.h:150
Stores a rectangle, for example the size of a window, the child window etc.
Definition data.h:185
uint32_t height
Definition data.h:189
uint32_t x
Definition data.h:186
uint32_t y
Definition data.h:187
uint32_t width
Definition data.h:188
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 'Window' is a type which contains an xcb_window_t and all the related information (hints like _NET_...
Definition data.h:424
char * class_instance
Definition data.h:438
struct timeval urgent
When this window was marked urgent.
Definition data.h:476
i3String * name
The name of the window.
Definition data.h:441
xcb_window_t id
Definition data.h:425
char * class_class
Definition data.h:437
xcb_window_t transient_for
Definition data.h:430
uint16_t depth
Depth of the window.
Definition data.h:482
A "match" is a data structure which acts like a mask or expression to match certain windows or not.
Definition data.h:529
Definition data.h:633
char * name
Definition data.h:634
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
struct Rect deco_rect
Definition data.h:688
enum Con::@18 type
layout_t workspace_layout
Definition data.h:755
double percent
Definition data.h:712
layout_t last_split_layout
Definition data.h:755
struct Rect rect
Definition data.h:682
int current_border_width
Definition data.h:716
bool sticky
Definition data.h:739
border_style_t max_user_border_style
Definition data.h:764
layout_t layout
Definition data.h:755
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 Rect window_rect
Definition data.h:685
struct Window * window
Definition data.h:718
char * title_format
The format with which the window's name should be displayed.
Definition data.h:695
surface_t frame
Definition data.h:658
border_style_t border_style
Definition data.h:757
char * name
Definition data.h:692
char * sticky_group
Definition data.h:705
struct deco_render_params * deco_render_params
Cache for the decoration rendering.
Definition data.h:724
bool mark_changed
Definition data.h:710
enum Con::@19 floating
floating? (= not in tiling layout) This cannot be simply a bool because we want to keep track of whet...
fullscreen_mode_t fullscreen_mode
Definition data.h:734
bool urgent
Definition data.h:648
Helper structure for usage in format_placeholders().
Definition libi3.h:546
xcb_drawable_t id
Definition libi3.h:571