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