i3
con.c
Go to the documentation of this file.
1 #undef I3__FILE__
2 #define I3__FILE__ "con.c"
3 /*
4  * vim:ts=4:sw=4:expandtab
5  *
6  * i3 - an improved dynamic tiling window manager
7  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
8  *
9  * con.c: Functions which deal with containers directly (creating containers,
10  * searching containers, getting specific properties from containers,
11  * …).
12  *
13  */
14 #include "all.h"
15 
16 char *colors[] = {
17  "#ff0000",
18  "#00FF00",
19  "#0000FF",
20  "#ff00ff",
21  "#00ffff",
22  "#ffff00",
23  "#aa0000",
24  "#00aa00",
25  "#0000aa",
26  "#aa00aa"
27 };
28 
29 static void con_on_remove_child(Con *con);
30 
31 /*
32  * force parent split containers to be redrawn
33  *
34  */
36  Con *parent = con;
37 
38  while (parent && parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
39  if (!con_is_leaf(parent))
40  FREE(parent->deco_render_params);
41  parent = parent->parent;
42  }
43 }
44 
45 /*
46  * Create a new container (and attach it to the given parent, if not NULL).
47  * This function initializes the data structures and creates the appropriate
48  * X11 IDs using x_con_init().
49  *
50  */
51 Con *con_new(Con *parent, i3Window *window) {
52  Con *new = scalloc(sizeof(Con));
53  new->on_remove_child = con_on_remove_child;
55  new->type = CT_CON;
56  new->window = window;
57  new->border_style = config.default_border;
58  new->current_border_width = -1;
59  static int cnt = 0;
60  DLOG("opening window %d\n", cnt);
61 
62  /* TODO: remove window coloring after test-phase */
63  DLOG("color %s\n", colors[cnt]);
64  new->name = strdup(colors[cnt]);
65  //uint32_t cp = get_colorpixel(colors[cnt]);
66  cnt++;
67  if ((cnt % (sizeof(colors) / sizeof(char*))) == 0)
68  cnt = 0;
69  if (window)
70  x_con_init(new, window->depth);
71  else
72  x_con_init(new, XCB_COPY_FROM_PARENT);
73 
74  TAILQ_INIT(&(new->floating_head));
75  TAILQ_INIT(&(new->nodes_head));
76  TAILQ_INIT(&(new->focus_head));
77  TAILQ_INIT(&(new->swallow_head));
78 
79  if (parent != NULL)
80  con_attach(new, parent, false);
81 
82  return new;
83 }
84 
85 /*
86  * Attaches the given container to the given parent. This happens when moving
87  * a container or when inserting a new container at a specific place in the
88  * tree.
89  *
90  * ignore_focus is to just insert the Con at the end (useful when creating a
91  * new split container *around* some containers, that is, detaching and
92  * attaching them in order without wanting to mess with the focus in between).
93  *
94  */
95 void con_attach(Con *con, Con *parent, bool ignore_focus) {
96  con->parent = parent;
97  Con *loop;
98  Con *current = NULL;
99  struct nodes_head *nodes_head = &(parent->nodes_head);
100  struct focus_head *focus_head = &(parent->focus_head);
101 
102  /* Workspaces are handled differently: they need to be inserted at the
103  * right position. */
104  if (con->type == CT_WORKSPACE) {
105  DLOG("it's a workspace. num = %d\n", con->num);
106  if (con->num == -1 || TAILQ_EMPTY(nodes_head)) {
107  TAILQ_INSERT_TAIL(nodes_head, con, nodes);
108  } else {
109  current = TAILQ_FIRST(nodes_head);
110  if (con->num < current->num) {
111  /* we need to insert the container at the beginning */
112  TAILQ_INSERT_HEAD(nodes_head, con, nodes);
113  } else {
114  while (current->num != -1 && con->num > current->num) {
115  current = TAILQ_NEXT(current, nodes);
116  if (current == TAILQ_END(nodes_head)) {
117  current = NULL;
118  break;
119  }
120  }
121  /* we need to insert con after current, if current is not NULL */
122  if (current)
123  TAILQ_INSERT_BEFORE(current, con, nodes);
124  else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
125  }
126  }
127  goto add_to_focus_head;
128  }
129 
130  if (con->type == CT_FLOATING_CON) {
131  DLOG("Inserting into floating containers\n");
132  TAILQ_INSERT_TAIL(&(parent->floating_head), con, floating_windows);
133  } else {
134  if (!ignore_focus) {
135  /* Get the first tiling container in focus stack */
136  TAILQ_FOREACH(loop, &(parent->focus_head), focused) {
137  if (loop->type == CT_FLOATING_CON)
138  continue;
139  current = loop;
140  break;
141  }
142  }
143 
144  /* When the container is not a split container (but contains a window)
145  * and is attached to a workspace, we check if the user configured a
146  * workspace_layout. This is done in workspace_attach_to, which will
147  * provide us with the container to which we should attach (either the
148  * workspace or a new split container with the configured
149  * workspace_layout).
150  */
151  if (con->window != NULL &&
152  parent->type == CT_WORKSPACE &&
153  parent->workspace_layout != L_DEFAULT) {
154  DLOG("Parent is a workspace. Applying default layout...\n");
155  Con *target = workspace_attach_to(parent);
156 
157  /* Attach the original con to this new split con instead */
158  nodes_head = &(target->nodes_head);
159  focus_head = &(target->focus_head);
160  con->parent = target;
161  current = NULL;
162 
163  DLOG("done\n");
164  }
165 
166  /* Insert the container after the tiling container, if found.
167  * When adding to a CT_OUTPUT, just append one after another. */
168  if (current && parent->type != CT_OUTPUT) {
169  DLOG("Inserting con = %p after last focused tiling con %p\n",
170  con, current);
171  TAILQ_INSERT_AFTER(nodes_head, current, con, nodes);
172  } else TAILQ_INSERT_TAIL(nodes_head, con, nodes);
173  }
174 
175 add_to_focus_head:
176  /* We insert to the TAIL because con_focus() will correct this.
177  * This way, we have the option to insert Cons without having
178  * to focus them. */
179  TAILQ_INSERT_TAIL(focus_head, con, focused);
181 }
182 
183 /*
184  * Detaches the given container from its current parent
185  *
186  */
187 void con_detach(Con *con) {
189  if (con->type == CT_FLOATING_CON) {
190  TAILQ_REMOVE(&(con->parent->floating_head), con, floating_windows);
191  TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
192  } else {
193  TAILQ_REMOVE(&(con->parent->nodes_head), con, nodes);
194  TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
195  }
196 }
197 
198 /*
199  * Sets input focus to the given container. Will be updated in X11 in the next
200  * run of x_push_changes().
201  *
202  */
203 void con_focus(Con *con) {
204  assert(con != NULL);
205  DLOG("con_focus = %p\n", con);
206 
207  /* 1: set focused-pointer to the new con */
208  /* 2: exchange the position of the container in focus stack of the parent all the way up */
209  TAILQ_REMOVE(&(con->parent->focus_head), con, focused);
210  TAILQ_INSERT_HEAD(&(con->parent->focus_head), con, focused);
211  if (con->parent->parent != NULL)
212  con_focus(con->parent);
213 
214  focused = con;
215  /* We can't blindly reset non-leaf containers since they might have
216  * other urgent children. Therefore we only reset leafs and propagate
217  * the changes upwards via con_update_parents_urgency() which does proper
218  * checks before resetting the urgency.
219  */
220  if (con->urgent && con_is_leaf(con)) {
221  con->urgent = false;
224  }
225 }
226 
227 /*
228  * Returns true when this node is a leaf node (has no children)
229  *
230  */
231 bool con_is_leaf(Con *con) {
232  return TAILQ_EMPTY(&(con->nodes_head));
233 }
234 
239 bool con_has_children(Con *con) {
240  return (!con_is_leaf(con) || !TAILQ_EMPTY(&(con->floating_head)));
241 }
242 
243 /*
244  * Returns true if a container should be considered split.
245  *
246  */
247 bool con_is_split(Con *con) {
248  if (con_is_leaf(con))
249  return false;
250 
251  switch (con->layout) {
252  case L_DOCKAREA:
253  case L_OUTPUT:
254  return false;
255 
256  default:
257  return true;
258  }
259 }
260 
261 /*
262  * Returns true if this node accepts a window (if the node swallows windows,
263  * it might already have swallowed enough and cannot hold any more).
264  *
265  */
267  /* 1: workspaces never accept direct windows */
268  if (con->type == CT_WORKSPACE)
269  return false;
270 
271  if (con_is_split(con)) {
272  DLOG("container %p does not accept windows, it is a split container.\n", con);
273  return false;
274  }
275 
276  /* TODO: if this is a swallowing container, we need to check its max_clients */
277  return (con->window == NULL);
278 }
279 
280 /*
281  * Gets the output container (first container with CT_OUTPUT in hierarchy) this
282  * node is on.
283  *
284  */
286  Con *result = con;
287  while (result != NULL && result->type != CT_OUTPUT)
288  result = result->parent;
289  /* We must be able to get an output because focus can never be set higher
290  * in the tree (root node cannot be focused). */
291  assert(result != NULL);
292  return result;
293 }
294 
295 /*
296  * Gets the workspace container this node is on.
297  *
298  */
300  Con *result = con;
301  while (result != NULL && result->type != CT_WORKSPACE)
302  result = result->parent;
303  return result;
304 }
305 
306 /*
307  * Searches parenst of the given 'con' until it reaches one with the specified
308  * 'orientation'. Aborts when it comes across a floating_con.
309  *
310  */
312  DLOG("Searching for parent of Con %p with orientation %d\n", con, orientation);
313  Con *parent = con->parent;
314  if (parent->type == CT_FLOATING_CON)
315  return NULL;
316  while (con_orientation(parent) != orientation) {
317  DLOG("Need to go one level further up\n");
318  parent = parent->parent;
319  /* Abort when we reach a floating con, or an output con */
320  if (parent &&
321  (parent->type == CT_FLOATING_CON ||
322  parent->type == CT_OUTPUT ||
323  (parent->parent && parent->parent->type == CT_OUTPUT)))
324  parent = NULL;
325  if (parent == NULL)
326  break;
327  }
328  DLOG("Result: %p\n", parent);
329  return parent;
330 }
331 
332 /*
333  * helper data structure for the breadth-first-search in
334  * con_get_fullscreen_con()
335  *
336  */
337 struct bfs_entry {
339 
340  TAILQ_ENTRY(bfs_entry) entries;
341 };
342 
343 /*
344  * Returns the first fullscreen node below this node.
345  *
346  */
347 Con *con_get_fullscreen_con(Con *con, int fullscreen_mode) {
348  Con *current, *child;
349 
350  /* TODO: is breadth-first-search really appropriate? (check as soon as
351  * fullscreen levels and fullscreen for containers is implemented) */
352  TAILQ_HEAD(bfs_head, bfs_entry) bfs_head = TAILQ_HEAD_INITIALIZER(bfs_head);
353  struct bfs_entry *entry = smalloc(sizeof(struct bfs_entry));
354  entry->con = con;
355  TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
356 
357  while (!TAILQ_EMPTY(&bfs_head)) {
358  entry = TAILQ_FIRST(&bfs_head);
359  current = entry->con;
360  if (current != con && current->fullscreen_mode == fullscreen_mode) {
361  /* empty the queue */
362  while (!TAILQ_EMPTY(&bfs_head)) {
363  entry = TAILQ_FIRST(&bfs_head);
364  TAILQ_REMOVE(&bfs_head, entry, entries);
365  free(entry);
366  }
367  return current;
368  }
369 
370  TAILQ_REMOVE(&bfs_head, entry, entries);
371  free(entry);
372 
373  TAILQ_FOREACH(child, &(current->nodes_head), nodes) {
374  entry = smalloc(sizeof(struct bfs_entry));
375  entry->con = child;
376  TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
377  }
378 
379  TAILQ_FOREACH(child, &(current->floating_head), floating_windows) {
380  entry = smalloc(sizeof(struct bfs_entry));
381  entry->con = child;
382  TAILQ_INSERT_TAIL(&bfs_head, entry, entries);
383  }
384  }
385 
386  return NULL;
387 }
388 
394  return (con->name[0] == '_' && con->name[1] == '_');
395 }
396 
397 /*
398  * Returns true if the node is floating.
399  *
400  */
402  assert(con != NULL);
403  DLOG("checking if con %p is floating\n", con);
404  return (con->floating >= FLOATING_AUTO_ON);
405 }
406 
407 /*
408  * Checks if the given container is either floating or inside some floating
409  * container. It returns the FLOATING_CON container.
410  *
411  */
413  assert(con != NULL);
414  if (con->type == CT_FLOATING_CON)
415  return con;
416 
417  if (con->floating >= FLOATING_AUTO_ON)
418  return con->parent;
419 
420  if (con->type == CT_WORKSPACE || con->type == CT_OUTPUT)
421  return NULL;
422 
423  return con_inside_floating(con->parent);
424 }
425 
426 /*
427  * Checks if the given container is inside a focused container.
428  *
429  */
431  if (con == focused)
432  return true;
433  if (!con->parent)
434  return false;
435  return con_inside_focused(con->parent);
436 }
437 
438 /*
439  * Returns the container with the given client window ID or NULL if no such
440  * container exists.
441  *
442  */
443 Con *con_by_window_id(xcb_window_t window) {
444  Con *con;
446  if (con->window != NULL && con->window->id == window)
447  return con;
448  return NULL;
449 }
450 
451 /*
452  * Returns the container with the given frame ID or NULL if no such container
453  * exists.
454  *
455  */
456 Con *con_by_frame_id(xcb_window_t frame) {
457  Con *con;
459  if (con->frame == frame)
460  return con;
461  return NULL;
462 }
463 
464 /*
465  * Returns the first container below 'con' which wants to swallow this window
466  * TODO: priority
467  *
468  */
469 Con *con_for_window(Con *con, i3Window *window, Match **store_match) {
470  Con *child;
471  Match *match;
472  //DLOG("searching con for window %p starting at con %p\n", window, con);
473  //DLOG("class == %s\n", window->class_class);
474 
475  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
476  TAILQ_FOREACH(match, &(child->swallow_head), matches) {
477  if (!match_matches_window(match, window))
478  continue;
479  if (store_match != NULL)
480  *store_match = match;
481  return child;
482  }
483  Con *result = con_for_window(child, window, store_match);
484  if (result != NULL)
485  return result;
486  }
487 
488  TAILQ_FOREACH(child, &(con->floating_head), floating_windows) {
489  TAILQ_FOREACH(match, &(child->swallow_head), matches) {
490  if (!match_matches_window(match, window))
491  continue;
492  if (store_match != NULL)
493  *store_match = match;
494  return child;
495  }
496  Con *result = con_for_window(child, window, store_match);
497  if (result != NULL)
498  return result;
499  }
500 
501  return NULL;
502 }
503 
504 /*
505  * Returns the number of children of this container.
506  *
507  */
509  Con *child;
510  int children = 0;
511 
512  TAILQ_FOREACH(child, &(con->nodes_head), nodes)
513  children++;
514 
515  return children;
516 }
517 
518 /*
519  * Updates the percent attribute of the children of the given container. This
520  * function needs to be called when a window is added or removed from a
521  * container.
522  *
523  */
525  Con *child;
526  int children = con_num_children(con);
527 
528  // calculate how much we have distributed and how many containers
529  // with a percentage set we have
530  double total = 0.0;
531  int children_with_percent = 0;
532  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
533  if (child->percent > 0.0) {
534  total += child->percent;
535  ++children_with_percent;
536  }
537  }
538 
539  // if there were children without a percentage set, set to a value that
540  // will make those children proportional to all others
541  if (children_with_percent != children) {
542  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
543  if (child->percent <= 0.0) {
544  if (children_with_percent == 0)
545  total += (child->percent = 1.0);
546  else total += (child->percent = total / children_with_percent);
547  }
548  }
549  }
550 
551  // if we got a zero, just distribute the space equally, otherwise
552  // distribute according to the proportions we got
553  if (total == 0.0) {
554  TAILQ_FOREACH(child, &(con->nodes_head), nodes)
555  child->percent = 1.0 / children;
556  } else if (total != 1.0) {
557  TAILQ_FOREACH(child, &(con->nodes_head), nodes)
558  child->percent /= total;
559  }
560 }
561 
562 /*
563  * Toggles fullscreen mode for the given container. Fullscreen mode will not be
564  * entered when there already is a fullscreen container on this workspace.
565  *
566  */
567 void con_toggle_fullscreen(Con *con, int fullscreen_mode) {
568  Con *workspace, *fullscreen;
569 
570  if (con->type == CT_WORKSPACE) {
571  DLOG("You cannot make a workspace fullscreen.\n");
572  return;
573  }
574 
575  DLOG("toggling fullscreen for %p / %s\n", con, con->name);
576  if (con->fullscreen_mode == CF_NONE) {
577  /* 1: check if there already is a fullscreen con */
578  if (fullscreen_mode == CF_GLOBAL)
579  fullscreen = con_get_fullscreen_con(croot, CF_GLOBAL);
580  else {
581  workspace = con_get_workspace(con);
582  fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
583  }
584  if (fullscreen != NULL) {
585  /* Disable fullscreen for the currently fullscreened
586  * container and enable it for the one the user wants
587  * to have in fullscreen mode. */
588  LOG("Disabling fullscreen for (%p/%s) upon user request\n",
589  fullscreen, fullscreen->name);
590  fullscreen->fullscreen_mode = CF_NONE;
591  }
592 
593  /* 2: enable fullscreen */
594  con->fullscreen_mode = fullscreen_mode;
595  } else {
596  /* 1: disable fullscreen */
597  con->fullscreen_mode = CF_NONE;
598  }
599 
600  DLOG("mode now: %d\n", con->fullscreen_mode);
601 
602  /* update _NET_WM_STATE if this container has a window */
603  /* TODO: when a window is assigned to a container which is already
604  * fullscreened, this state needs to be pushed to the client, too */
605  if (con->window == NULL)
606  return;
607 
608  uint32_t values[1];
609  unsigned int num = 0;
610 
611  if (con->fullscreen_mode != CF_NONE)
612  values[num++] = A__NET_WM_STATE_FULLSCREEN;
613 
614  xcb_change_property(conn, XCB_PROP_MODE_REPLACE, con->window->id,
615  A__NET_WM_STATE, XCB_ATOM_ATOM, 32, num, values);
616 }
617 
618 /*
619  * Moves the given container to the currently focused container on the given
620  * workspace.
621  *
622  * The fix_coordinates flag will translate the current coordinates (offset from
623  * the monitor position basically) to appropriate coordinates on the
624  * destination workspace.
625  * Not enabling this behaviour comes in handy when this function gets called by
626  * floating_maybe_reassign_ws, which will only "move" a floating window when it
627  * *already* changed its coordinates to a different output.
628  *
629  * The dont_warp flag disables pointer warping and will be set when this
630  * function is called while dragging a floating window.
631  *
632  * TODO: is there a better place for this function?
633  *
634  */
635 void con_move_to_workspace(Con *con, Con *workspace, bool fix_coordinates, bool dont_warp) {
636  /* Prevent moving if this would violate the fullscreen focus restrictions. */
637  if (!con_fullscreen_permits_focusing(workspace)) {
638  LOG("Cannot move out of a fullscreen container");
639  return;
640  }
641 
642  if (con_is_floating(con)) {
643  DLOG("Using FLOATINGCON instead\n");
644  con = con->parent;
645  }
646 
647  Con *source_ws = con_get_workspace(con);
648  if (workspace == source_ws) {
649  DLOG("Not moving, already there\n");
650  return;
651  }
652 
653  if (con->type == CT_WORKSPACE) {
654  /* Re-parent all of the old workspace's floating windows. */
655  Con *child;
656  while (!TAILQ_EMPTY(&(source_ws->floating_head))) {
657  child = TAILQ_FIRST(&(source_ws->floating_head));
658  con_move_to_workspace(child, workspace, true, true);
659  }
660 
661  /* If there are no non-floating children, ignore the workspace. */
662  if (con_is_leaf(con))
663  return;
664 
665  con = workspace_encapsulate(con);
666  if (con == NULL) {
667  ELOG("Workspace failed to move its contents into a container!\n");
668  return;
669  }
670  }
671 
672  /* Save the current workspace. So we can call workspace_show() by the end
673  * of this function. */
674  Con *current_ws = con_get_workspace(focused);
675 
676  Con *source_output = con_get_output(con),
677  *dest_output = con_get_output(workspace);
678 
679  /* 1: save the container which is going to be focused after the current
680  * container is moved away */
681  Con *focus_next = con_next_focused(con);
682 
683  /* 2: get the focused container of this workspace */
684  Con *next = con_descend_focused(workspace);
685 
686  /* 3: we go up one level, but only when next is a normal container */
687  if (next->type != CT_WORKSPACE) {
688  DLOG("next originally = %p / %s / type %d\n", next, next->name, next->type);
689  next = next->parent;
690  }
691 
692  /* 4: if the target container is floating, we get the workspace instead.
693  * Only tiling windows need to get inserted next to the current container.
694  * */
695  Con *floatingcon = con_inside_floating(next);
696  if (floatingcon != NULL) {
697  DLOG("floatingcon, going up even further\n");
698  next = floatingcon->parent;
699  }
700 
701  if (con->type == CT_FLOATING_CON) {
702  Con *ws = con_get_workspace(next);
703  DLOG("This is a floating window, using workspace %p / %s\n", ws, ws->name);
704  next = ws;
705  }
706 
707  if (source_output != dest_output) {
708  /* Take the relative coordinates of the current output, then add them
709  * to the coordinate space of the correct output */
710  if (fix_coordinates && con->type == CT_FLOATING_CON) {
711  floating_fix_coordinates(con, &(source_output->rect), &(dest_output->rect));
712  } else DLOG("Not fixing coordinates, fix_coordinates flag = %d\n", fix_coordinates);
713 
714  /* If moving to a visible workspace, call show so it can be considered
715  * focused. Must do before attaching because workspace_show checks to see
716  * if focused container is in its area. */
717  if (workspace_is_visible(workspace)) {
718  workspace_show(workspace);
719 
720  /* Don’t warp if told so (when dragging floating windows with the
721  * mouse for example) */
722  if (dont_warp)
723  x_set_warp_to(NULL);
724  else
725  x_set_warp_to(&(con->rect));
726  }
727  }
728 
729  /* If moving a fullscreen container and the destination already has a
730  * fullscreen window on it, un-fullscreen the target's fullscreen con. */
731  Con *fullscreen = con_get_fullscreen_con(workspace, CF_OUTPUT);
732  if (con->fullscreen_mode != CF_NONE && fullscreen != NULL) {
733  con_toggle_fullscreen(fullscreen, CF_OUTPUT);
734  fullscreen = NULL;
735  }
736 
737  DLOG("Re-attaching container to %p / %s\n", next, next->name);
738  /* 5: re-attach the con to the parent of this focused container */
739  Con *parent = con->parent;
740  con_detach(con);
741  con_attach(con, next, false);
742 
743  /* 6: fix the percentages */
744  con_fix_percent(parent);
745  con->percent = 0.0;
746  con_fix_percent(next);
747 
748  /* 7: focus the con on the target workspace, but only within that
749  * workspace, that is, don’t move focus away if the target workspace is
750  * invisible.
751  * We don’t focus the con for i3 pseudo workspaces like __i3_scratch and
752  * we don’t focus when there is a fullscreen con on that workspace. */
753  if (!con_is_internal(workspace) && !fullscreen) {
754  /* We need to save the focused workspace on the output in case the
755  * new workspace is hidden and it's necessary to immediately switch
756  * back to the originally-focused workspace. */
757  Con *old_focus = TAILQ_FIRST(&(output_get_content(dest_output)->focus_head));
759 
760  /* Restore focus if the output's focused workspace has changed. */
761  if (con_get_workspace(focused) != old_focus)
762  con_focus(old_focus);
763  }
764 
765  /* 8: when moving to another workspace, we leave the focus on the current
766  * workspace. (see also #809) */
767 
768  /* Descend focus stack in case focus_next is a workspace which can
769  * occur if we move to the same workspace. Also show current workspace
770  * to ensure it is focused. */
771  workspace_show(current_ws);
772 
773  /* Set focus only if con was on current workspace before moving.
774  * Otherwise we would give focus to some window on different workspace. */
775  if (source_ws == current_ws)
776  con_focus(con_descend_focused(focus_next));
777 
778  /* If anything within the container is associated with a startup sequence,
779  * delete it so child windows won't be created on the old workspace. */
780  struct Startup_Sequence *sequence;
781  xcb_get_property_cookie_t cookie;
782  xcb_get_property_reply_t *startup_id_reply;
783 
784  if (!con_is_leaf(con)) {
785  Con *child;
786  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
787  if (!child->window)
788  continue;
789 
790  cookie = xcb_get_property(conn, false, child->window->id,
791  A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
792  startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
793 
794  sequence = startup_sequence_get(child->window, startup_id_reply, true);
795  if (sequence != NULL)
796  startup_sequence_delete(sequence);
797  }
798  }
799 
800  if (con->window) {
801  cookie = xcb_get_property(conn, false, con->window->id,
802  A__NET_STARTUP_ID, XCB_GET_PROPERTY_TYPE_ANY, 0, 512);
803  startup_id_reply = xcb_get_property_reply(conn, cookie, NULL);
804 
805  sequence = startup_sequence_get(con->window, startup_id_reply, true);
806  if (sequence != NULL)
807  startup_sequence_delete(sequence);
808  }
809 
810  CALL(parent, on_remove_child);
811 }
812 
813 /*
814  * Returns the orientation of the given container (for stacked containers,
815  * vertical orientation is used regardless of the actual orientation of the
816  * container).
817  *
818  */
820  switch (con->layout) {
821  case L_SPLITV:
822  /* stacking containers behave like they are in vertical orientation */
823  case L_STACKED:
824  return VERT;
825 
826  case L_SPLITH:
827  /* tabbed containers behave like they are in vertical orientation */
828  case L_TABBED:
829  return HORIZ;
830 
831  case L_DEFAULT:
832  DLOG("Someone called con_orientation() on a con with L_DEFAULT, this is a bug in the code.\n");
833  assert(false);
834  return HORIZ;
835 
836  case L_DOCKAREA:
837  case L_OUTPUT:
838  DLOG("con_orientation() called on dockarea/output (%d) container %p\n", con->layout, con);
839  assert(false);
840  return HORIZ;
841 
842  default:
843  DLOG("con_orientation() ran into default\n");
844  assert(false);
845  }
846 }
847 
848 /*
849  * Returns the container which will be focused next when the given container
850  * is not available anymore. Called in tree_close and con_move_to_workspace
851  * to properly restore focus.
852  *
853  */
855  Con *next;
856  /* floating containers are attached to a workspace, so we focus either the
857  * next floating container (if any) or the workspace itself. */
858  if (con->type == CT_FLOATING_CON) {
859  DLOG("selecting next for CT_FLOATING_CON\n");
860  next = TAILQ_NEXT(con, floating_windows);
861  DLOG("next = %p\n", next);
862  if (!next) {
863  next = TAILQ_PREV(con, floating_head, floating_windows);
864  DLOG("using prev, next = %p\n", next);
865  }
866  if (!next) {
867  Con *ws = con_get_workspace(con);
868  next = ws;
869  DLOG("no more floating containers for next = %p, restoring workspace focus\n", next);
870  while (next != TAILQ_END(&(ws->focus_head)) && !TAILQ_EMPTY(&(next->focus_head))) {
871  next = TAILQ_FIRST(&(next->focus_head));
872  if (next == con) {
873  DLOG("skipping container itself, we want the next client\n");
874  next = TAILQ_NEXT(next, focused);
875  }
876  }
877  if (next == TAILQ_END(&(ws->focus_head))) {
878  DLOG("Focus list empty, returning ws\n");
879  next = ws;
880  }
881  } else {
882  /* Instead of returning the next CT_FLOATING_CON, we descend it to
883  * get an actual window to focus. */
884  next = con_descend_focused(next);
885  }
886  return next;
887  }
888 
889  /* dock clients cannot be focused, so we focus the workspace instead */
890  if (con->parent->type == CT_DOCKAREA) {
891  DLOG("selecting workspace for dock client\n");
893  }
894 
895  /* if 'con' is not the first entry in the focus stack, use the first one as
896  * it’s currently focused already */
897  Con *first = TAILQ_FIRST(&(con->parent->focus_head));
898  if (first != con) {
899  DLOG("Using first entry %p\n", first);
900  next = first;
901  } else {
902  /* try to focus the next container on the same level as this one or fall
903  * back to its parent */
904  if (!(next = TAILQ_NEXT(con, focused)))
905  next = con->parent;
906  }
907 
908  /* now go down the focus stack as far as
909  * possible, excluding the current container */
910  while (!TAILQ_EMPTY(&(next->focus_head)) &&
911  TAILQ_FIRST(&(next->focus_head)) != con)
912  next = TAILQ_FIRST(&(next->focus_head));
913 
914  return next;
915 }
916 
917 /*
918  * Get the next/previous container in the specified orientation. This may
919  * travel up until it finds a container with suitable orientation.
920  *
921  */
922 Con *con_get_next(Con *con, char way, orientation_t orientation) {
923  DLOG("con_get_next(way=%c, orientation=%d)\n", way, orientation);
924  /* 1: get the first parent with the same orientation */
925  Con *cur = con;
926  while (con_orientation(cur->parent) != orientation) {
927  DLOG("need to go one level further up\n");
928  if (cur->parent->type == CT_WORKSPACE) {
929  LOG("that's a workspace, we can't go further up\n");
930  return NULL;
931  }
932  cur = cur->parent;
933  }
934 
935  /* 2: chose next (or previous) */
936  Con *next;
937  if (way == 'n') {
938  next = TAILQ_NEXT(cur, nodes);
939  /* if we are at the end of the list, we need to wrap */
940  if (next == TAILQ_END(&(parent->nodes_head)))
941  return NULL;
942  } else {
943  next = TAILQ_PREV(cur, nodes_head, nodes);
944  /* if we are at the end of the list, we need to wrap */
945  if (next == TAILQ_END(&(cur->nodes_head)))
946  return NULL;
947  }
948  DLOG("next = %p\n", next);
949 
950  return next;
951 }
952 
953 /*
954  * Returns the focused con inside this client, descending the tree as far as
955  * possible. This comes in handy when attaching a con to a workspace at the
956  * currently focused position, for example.
957  *
958  */
960  Con *next = con;
961  while (next != focused && !TAILQ_EMPTY(&(next->focus_head)))
962  next = TAILQ_FIRST(&(next->focus_head));
963  return next;
964 }
965 
966 /*
967  * Returns the focused con inside this client, descending the tree as far as
968  * possible. This comes in handy when attaching a con to a workspace at the
969  * currently focused position, for example.
970  *
971  * Works like con_descend_focused but considers only tiling cons.
972  *
973  */
975  Con *next = con;
976  Con *before;
977  Con *child;
978  if (next == focused)
979  return next;
980  do {
981  before = next;
982  TAILQ_FOREACH(child, &(next->focus_head), focused) {
983  if (child->type == CT_FLOATING_CON)
984  continue;
985 
986  next = child;
987  break;
988  }
989  } while (before != next && next != focused);
990  return next;
991 }
992 
993 /*
994  * Returns the leftmost, rightmost, etc. container in sub-tree. For example, if
995  * direction is D_LEFT, then we return the rightmost container and if direction
996  * is D_RIGHT, we return the leftmost container. This is because if we are
997  * moving D_LEFT, and thus want the rightmost container.
998  *
999  */
1001  Con *most = NULL;
1002  Con *current;
1003  int orientation = con_orientation(con);
1004  DLOG("con_descend_direction(%p, orientation %d, direction %d)\n", con, orientation, direction);
1005  if (direction == D_LEFT || direction == D_RIGHT) {
1006  if (orientation == HORIZ) {
1007  /* If the direction is horizontal, we can use either the first
1008  * (D_RIGHT) or the last con (D_LEFT) */
1009  if (direction == D_RIGHT)
1010  most = TAILQ_FIRST(&(con->nodes_head));
1011  else most = TAILQ_LAST(&(con->nodes_head), nodes_head);
1012  } else if (orientation == VERT) {
1013  /* Wrong orientation. We use the last focused con. Within that con,
1014  * we recurse to chose the left/right con or at least the last
1015  * focused one. */
1016  TAILQ_FOREACH(current, &(con->focus_head), focused) {
1017  if (current->type != CT_FLOATING_CON) {
1018  most = current;
1019  break;
1020  }
1021  }
1022  } else {
1023  /* If the con has no orientation set, it’s not a split container
1024  * but a container with a client window, so stop recursing */
1025  return con;
1026  }
1027  }
1028 
1029  if (direction == D_UP || direction == D_DOWN) {
1030  if (orientation == VERT) {
1031  /* If the direction is vertical, we can use either the first
1032  * (D_DOWN) or the last con (D_UP) */
1033  if (direction == D_UP)
1034  most = TAILQ_LAST(&(con->nodes_head), nodes_head);
1035  else most = TAILQ_FIRST(&(con->nodes_head));
1036  } else if (orientation == HORIZ) {
1037  /* Wrong orientation. We use the last focused con. Within that con,
1038  * we recurse to chose the top/bottom con or at least the last
1039  * focused one. */
1040  TAILQ_FOREACH(current, &(con->focus_head), focused) {
1041  if (current->type != CT_FLOATING_CON) {
1042  most = current;
1043  break;
1044  }
1045  }
1046  } else {
1047  /* If the con has no orientation set, it’s not a split container
1048  * but a container with a client window, so stop recursing */
1049  return con;
1050  }
1051  }
1052 
1053  if (!most)
1054  return con;
1055  return con_descend_direction(most, direction);
1056 }
1057 
1058 /*
1059  * Returns a "relative" Rect which contains the amount of pixels that need to
1060  * be added to the original Rect to get the final position (obviously the
1061  * amount of pixels for normal, 1pixel and borderless are different).
1062  *
1063  */
1065  adjacent_t borders_to_hide = ADJ_NONE;
1066  int border_width = con->current_border_width;
1067  DLOG("The border width for con is set to: %d\n", con->current_border_width);
1068  Rect result;
1069  if (con->current_border_width < 0)
1070  border_width = config.default_border_width;
1071  DLOG("Effective border width is set to: %d\n", border_width);
1072  /* Shortcut to avoid calling con_adjacent_borders() on dock containers. */
1073  int border_style = con_border_style(con);
1074  if (border_style == BS_NONE)
1075  return (Rect){ 0, 0, 0, 0 };
1076  borders_to_hide = con_adjacent_borders(con) & config.hide_edge_borders;
1077  if (border_style == BS_NORMAL) {
1078  result = (Rect){border_width, 0 , -(2 * border_width), -(border_width)};
1079  } else {
1080  result = (Rect){border_width, border_width, -(2 * border_width), -(2 * border_width)};
1081  }
1082  if (borders_to_hide & ADJ_LEFT_SCREEN_EDGE) {
1083  result.x -= border_width;
1084  result.width += border_width;
1085  }
1086  if (borders_to_hide & ADJ_RIGHT_SCREEN_EDGE) {
1087  result.width += border_width;
1088  }
1089  if (borders_to_hide & ADJ_UPPER_SCREEN_EDGE && (border_style != BS_NORMAL)) {
1090  result.y -= border_width;
1091  result.height += border_width;
1092  }
1093  if (borders_to_hide & ADJ_LOWER_SCREEN_EDGE) {
1094  result.height += border_width;
1095  }
1096  return result;
1097 
1098 }
1099 
1100 /*
1101  * Returns adjacent borders of the window. We need this if hide_edge_borders is
1102  * enabled.
1103  */
1105  adjacent_t result = ADJ_NONE;
1107  if (con->rect.x == workspace->rect.x)
1108  result |= ADJ_LEFT_SCREEN_EDGE;
1109  if (con->rect.x + con->rect.width == workspace->rect.x + workspace->rect.width)
1110  result |= ADJ_RIGHT_SCREEN_EDGE;
1111  if (con->rect.y == workspace->rect.y)
1112  result |= ADJ_UPPER_SCREEN_EDGE;
1113  if (con->rect.y + con->rect.height == workspace->rect.y + workspace->rect.height)
1114  result |= ADJ_LOWER_SCREEN_EDGE;
1115  return result;
1116 }
1117 
1118 /*
1119  * Use this function to get a container’s border style. This is important
1120  * because when inside a stack, the border style is always BS_NORMAL.
1121  * For tabbed mode, the same applies, with one exception: when the container is
1122  * borderless and the only element in the tabbed container, the border is not
1123  * rendered.
1124  *
1125  * For children of a CT_DOCKAREA, the border style is always none.
1126  *
1127  */
1129  Con *fs = con_get_fullscreen_con(con->parent, CF_OUTPUT);
1130  if (fs == con) {
1131  DLOG("this one is fullscreen! overriding BS_NONE\n");
1132  return BS_NONE;
1133  }
1134 
1135  if (con->parent->layout == L_STACKED)
1136  return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1137 
1138  if (con->parent->layout == L_TABBED && con->border_style != BS_NORMAL)
1139  return (con_num_children(con->parent) == 1 ? con->border_style : BS_NORMAL);
1140 
1141  if (con->parent->type == CT_DOCKAREA)
1142  return BS_NONE;
1143 
1144  return con->border_style;
1145 }
1146 
1147 /*
1148  * Sets the given border style on con, correctly keeping the position/size of a
1149  * floating window.
1150  *
1151  */
1152 void con_set_border_style(Con *con, int border_style, int border_width) {
1153  /* Handle the simple case: non-floating containerns */
1154  if (!con_is_floating(con)) {
1155  con->border_style = border_style;
1156  con->current_border_width = border_width;
1157  return;
1158  }
1159 
1160  /* For floating containers, we want to keep the position/size of the
1161  * *window* itself. We first add the border pixels to con->rect to make
1162  * con->rect represent the absolute position of the window. Then, we change
1163  * the border and subtract the new border pixels. Afterwards, we update
1164  * parent->rect to contain con. */
1165  DLOG("This is a floating container\n");
1166 
1167  Rect bsr = con_border_style_rect(con);
1168  con->rect.x += bsr.x;
1169  con->rect.y += bsr.y;
1170  con->rect.width += bsr.width;
1171  con->rect.height += bsr.height;
1172 
1173  /* Change the border style, get new border/decoration values. */
1174  con->border_style = border_style;
1175  con->current_border_width = border_width;
1176  bsr = con_border_style_rect(con);
1177  int deco_height =
1178  (con->border_style == BS_NORMAL ? render_deco_height() : 0);
1179 
1180  con->rect.x -= bsr.x;
1181  con->rect.y -= bsr.y;
1182  con->rect.width -= bsr.width;
1183  con->rect.height -= bsr.height;
1184 
1185  Con *parent = con->parent;
1186  parent->rect.x = con->rect.x;
1187  parent->rect.y = con->rect.y - deco_height;
1188  parent->rect.width = con->rect.width;
1189  parent->rect.height = con->rect.height + deco_height;
1190 }
1191 
1192 /*
1193  * This function changes the layout of a given container. Use it to handle
1194  * special cases like changing a whole workspace to stacked/tabbed (creates a
1195  * new split container before).
1196  *
1197  */
1198 void con_set_layout(Con *con, int layout) {
1199  DLOG("con_set_layout(%p, %d), con->type = %d\n",
1200  con, layout, con->type);
1201 
1202  /* Users can focus workspaces, but not any higher in the hierarchy.
1203  * Focus on the workspace is a special case, since in every other case, the
1204  * user means "change the layout of the parent split container". */
1205  if (con->type != CT_WORKSPACE)
1206  con = con->parent;
1207 
1208  /* We fill in last_split_layout when switching to a different layout
1209  * since there are many places in the code that don’t use
1210  * con_set_layout(). */
1211  if (con->layout == L_SPLITH || con->layout == L_SPLITV)
1212  con->last_split_layout = con->layout;
1213 
1214  /* When the container type is CT_WORKSPACE, the user wants to change the
1215  * whole workspace into stacked/tabbed mode. To do this and still allow
1216  * intuitive operations (like level-up and then opening a new window), we
1217  * need to create a new split container. */
1218  if (con->type == CT_WORKSPACE &&
1219  (layout == L_STACKED || layout == L_TABBED)) {
1220  if (con_num_children(con) == 0) {
1221  DLOG("Setting workspace_layout to %d\n", layout);
1222  con->workspace_layout = layout;
1223  } else {
1224  DLOG("Creating new split container\n");
1225  /* 1: create a new split container */
1226  Con *new = con_new(NULL, NULL);
1227  new->parent = con;
1228 
1229  /* 2: Set the requested layout on the split container and mark it as
1230  * split. */
1231  new->layout = layout;
1233 
1234  Con *old_focused = TAILQ_FIRST(&(con->focus_head));
1235  if (old_focused == TAILQ_END(&(con->focus_head)))
1236  old_focused = NULL;
1237 
1238  /* 3: move the existing cons of this workspace below the new con */
1239  DLOG("Moving cons\n");
1240  Con *child;
1241  while (!TAILQ_EMPTY(&(con->nodes_head))) {
1242  child = TAILQ_FIRST(&(con->nodes_head));
1243  con_detach(child);
1244  con_attach(child, new, true);
1245  }
1246 
1247  /* 4: attach the new split container to the workspace */
1248  DLOG("Attaching new split to ws\n");
1249  con_attach(new, con, false);
1250 
1251  if (old_focused)
1252  con_focus(old_focused);
1253 
1255  }
1257  return;
1258  }
1259 
1260  if (layout == L_DEFAULT) {
1261  /* Special case: the layout formerly known as "default" (in combination
1262  * with an orientation). Since we switched to splith/splitv layouts,
1263  * using the "default" layout (which "only" should happen when using
1264  * legacy configs) is using the last split layout (either splith or
1265  * splitv) in order to still do the same thing.
1266  *
1267  * Starting from v4.6 though, we will nag users about using "layout
1268  * default", and in v4.9 we will remove it entirely (with an
1269  * appropriate i3-migrate-config mechanism). */
1270  con->layout = con->last_split_layout;
1271  /* In case last_split_layout was not initialized… */
1272  if (con->layout == L_DEFAULT)
1273  con->layout = L_SPLITH;
1274  } else {
1275  con->layout = layout;
1276  }
1278 }
1279 
1280 /*
1281  * This function toggles the layout of a given container. toggle_mode can be
1282  * either 'default' (toggle only between stacked/tabbed/last_split_layout),
1283  * 'split' (toggle only between splitv/splith) or 'all' (toggle between all
1284  * layouts).
1285  *
1286  */
1287 void con_toggle_layout(Con *con, const char *toggle_mode) {
1288  Con *parent = con;
1289  /* Users can focus workspaces, but not any higher in the hierarchy.
1290  * Focus on the workspace is a special case, since in every other case, the
1291  * user means "change the layout of the parent split container". */
1292  if (con->type != CT_WORKSPACE)
1293  parent = con->parent;
1294  DLOG("con_toggle_layout(%p, %s), parent = %p\n", con, toggle_mode, parent);
1295 
1296  if (strcmp(toggle_mode, "split") == 0) {
1297  /* Toggle between splits. When the current layout is not a split
1298  * layout, we just switch back to last_split_layout. Otherwise, we
1299  * change to the opposite split layout. */
1300  if (parent->layout != L_SPLITH && parent->layout != L_SPLITV)
1301  con_set_layout(con, parent->last_split_layout);
1302  else {
1303  if (parent->layout == L_SPLITH)
1304  con_set_layout(con, L_SPLITV);
1305  else con_set_layout(con, L_SPLITH);
1306  }
1307  } else {
1308  if (parent->layout == L_STACKED)
1309  con_set_layout(con, L_TABBED);
1310  else if (parent->layout == L_TABBED) {
1311  if (strcmp(toggle_mode, "all") == 0)
1312  con_set_layout(con, L_SPLITH);
1313  else con_set_layout(con, parent->last_split_layout);
1314  } else if (parent->layout == L_SPLITH || parent->layout == L_SPLITV) {
1315  if (strcmp(toggle_mode, "all") == 0) {
1316  /* When toggling through all modes, we toggle between
1317  * splith/splitv, whereas normally we just directly jump to
1318  * stacked. */
1319  if (parent->layout == L_SPLITH)
1320  con_set_layout(con, L_SPLITV);
1321  else con_set_layout(con, L_STACKED);
1322  } else {
1323  con_set_layout(con, L_STACKED);
1324  }
1325  }
1326  }
1327 }
1328 
1329 /*
1330  * Callback which will be called when removing a child from the given con.
1331  * Kills the container if it is empty and replaces it with the child if there
1332  * is exactly one child.
1333  *
1334  */
1335 static void con_on_remove_child(Con *con) {
1336  DLOG("on_remove_child\n");
1337 
1338  /* Every container 'above' (in the hierarchy) the workspace content should
1339  * not be closed when the last child was removed */
1340  if (con->type == CT_OUTPUT ||
1341  con->type == CT_ROOT ||
1342  con->type == CT_DOCKAREA) {
1343  DLOG("not handling, type = %d\n", con->type);
1344  return;
1345  }
1346 
1347  /* For workspaces, close them only if they're not visible anymore */
1348  if (con->type == CT_WORKSPACE) {
1349  if (TAILQ_EMPTY(&(con->focus_head)) && !workspace_is_visible(con)) {
1350  LOG("Closing old workspace (%p / %s), it is empty\n", con, con->name);
1351  tree_close(con, DONT_KILL_WINDOW, false, false);
1352  ipc_send_event("workspace", I3_IPC_EVENT_WORKSPACE, "{\"change\":\"empty\"}");
1353  }
1354  return;
1355  }
1356 
1358 
1359  /* TODO: check if this container would swallow any other client and
1360  * don’t close it automatically. */
1361  int children = con_num_children(con);
1362  if (children == 0) {
1363  DLOG("Container empty, closing\n");
1364  tree_close(con, DONT_KILL_WINDOW, false, false);
1365  return;
1366  }
1367 }
1368 
1369 /*
1370  * Determines the minimum size of the given con by looking at its children (for
1371  * split/stacked/tabbed cons). Will be called when resizing floating cons
1372  *
1373  */
1375  DLOG("Determining minimum size for con %p\n", con);
1376 
1377  if (con_is_leaf(con)) {
1378  DLOG("leaf node, returning 75x50\n");
1379  return (Rect){ 0, 0, 75, 50 };
1380  }
1381 
1382  if (con->type == CT_FLOATING_CON) {
1383  DLOG("floating con\n");
1384  Con *child = TAILQ_FIRST(&(con->nodes_head));
1385  return con_minimum_size(child);
1386  }
1387 
1388  if (con->layout == L_STACKED || con->layout == L_TABBED) {
1389  uint32_t max_width = 0, max_height = 0, deco_height = 0;
1390  Con *child;
1391  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1392  Rect min = con_minimum_size(child);
1393  deco_height += child->deco_rect.height;
1394  max_width = max(max_width, min.width);
1395  max_height = max(max_height, min.height);
1396  }
1397  DLOG("stacked/tabbed now, returning %d x %d + deco_rect = %d\n",
1398  max_width, max_height, deco_height);
1399  return (Rect){ 0, 0, max_width, max_height + deco_height };
1400  }
1401 
1402  /* For horizontal/vertical split containers we sum up the width (h-split)
1403  * or height (v-split) and use the maximum of the height (h-split) or width
1404  * (v-split) as minimum size. */
1405  if (con_is_split(con)) {
1406  uint32_t width = 0, height = 0;
1407  Con *child;
1408  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1409  Rect min = con_minimum_size(child);
1410  if (con->layout == L_SPLITH) {
1411  width += min.width;
1412  height = max(height, min.height);
1413  } else {
1414  height += min.height;
1415  width = max(width, min.width);
1416  }
1417  }
1418  DLOG("split container, returning width = %d x height = %d\n", width, height);
1419  return (Rect){ 0, 0, width, height };
1420  }
1421 
1422  ELOG("Unhandled case, type = %d, layout = %d, split = %d\n",
1423  con->type, con->layout, con_is_split(con));
1424  assert(false);
1425 }
1426 
1427 /*
1428  * Returns true if changing the focus to con would be allowed considering
1429  * the fullscreen focus constraints. Specifically, if a fullscreen container or
1430  * any of its descendants is focused, this function returns true if and only if
1431  * focusing con would mean that focus would still be visible on screen, i.e.,
1432  * the newly focused container would not be obscured by a fullscreen container.
1433  *
1434  * In the simplest case, if a fullscreen container or any of its descendants is
1435  * fullscreen, this functions returns true if con is the fullscreen container
1436  * itself or any of its descendants, as this means focus wouldn't escape the
1437  * boundaries of the fullscreen container.
1438  *
1439  * In case the fullscreen container is of type CF_OUTPUT, this function returns
1440  * true if con is on a different workspace, as focus wouldn't be obscured by
1441  * the fullscreen container that is constrained to a different workspace.
1442  *
1443  * Note that this same logic can be applied to moving containers. If a
1444  * container can be focused under the fullscreen focus constraints, it can also
1445  * become a parent or sibling to the currently focused container.
1446  *
1447  */
1449  /* No focus, no problem. */
1450  if (!focused)
1451  return true;
1452 
1453  /* Find the first fullscreen ascendent. */
1454  Con *fs = focused;
1455  while (fs && fs->fullscreen_mode == CF_NONE)
1456  fs = fs->parent;
1457 
1458  /* fs must be non-NULL since the workspace con doesn’t have CF_NONE and
1459  * there always has to be a workspace con in the hierarchy. */
1460  assert(fs != NULL);
1461  /* The most common case is we hit the workspace level. In this
1462  * situation, changing focus is also harmless. */
1463  assert(fs->fullscreen_mode != CF_NONE);
1464  if (fs->type == CT_WORKSPACE)
1465  return true;
1466 
1467  /* Allow it if the container itself is the fullscreen container. */
1468  if (con == fs)
1469  return true;
1470 
1471  /* If fullscreen is per-output, the focus being in a different workspace is
1472  * sufficient to guarantee that change won't leave fullscreen in bad shape. */
1473  if (fs->fullscreen_mode == CF_OUTPUT &&
1474  con_get_workspace(con) != con_get_workspace(fs)) {
1475  return true;
1476  }
1477 
1478  /* Allow it only if the container to be focused is contained within the
1479  * current fullscreen container. */
1480  do {
1481  if (con->parent == fs)
1482  return true;
1483  con = con->parent;
1484  } while (con);
1485 
1486  /* Focusing con would hide it behind a fullscreen window, disallow it. */
1487  return false;
1488 }
1489 
1490 /*
1491  *
1492  * Checks if the given container has an urgent child.
1493  *
1494  */
1496  Con *child;
1497 
1498  if (con_is_leaf(con))
1499  return con->urgent;
1500 
1501  /* We are not interested in floating windows since they can only be
1502  * attached to a workspace → nodes_head instead of focus_head */
1503  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1504  if (con_has_urgent_child(child))
1505  return true;
1506  }
1507 
1508  return false;
1509 }
1510 
1511 /*
1512  * Make all parent containers urgent if con is urgent or clear the urgent flag
1513  * of all parent containers if there are no more urgent children left.
1514  *
1515  */
1517  Con *parent = con->parent;
1518 
1519  bool new_urgency_value = con->urgent;
1520  while (parent && parent->type != CT_WORKSPACE && parent->type != CT_DOCKAREA) {
1521  if (new_urgency_value) {
1522  parent->urgent = true;
1523  } else {
1524  /* We can only reset the urgency when the parent
1525  * has no other urgent children */
1526  if (!con_has_urgent_child(parent))
1527  parent->urgent = false;
1528  }
1529  parent = parent->parent;
1530  }
1531 }
1532 
1533 /*
1534  * Set urgency flag to the container, all the parent containers and the workspace.
1535  *
1536  */
1537 void con_set_urgency(Con *con, bool urgent) {
1538  if (focused == con) {
1539  DLOG("Ignoring urgency flag for current client\n");
1540  con->window->urgent.tv_sec = 0;
1541  con->window->urgent.tv_usec = 0;
1542  return;
1543  }
1544 
1545  if (con->urgency_timer == NULL) {
1546  con->urgent = urgent;
1547  } else
1548  DLOG("Discarding urgency WM_HINT because timer is running\n");
1549 
1550  //CLIENT_LOG(con);
1551  if (con->window) {
1552  if (con->urgent) {
1553  gettimeofday(&con->window->urgent, NULL);
1554  } else {
1555  con->window->urgent.tv_sec = 0;
1556  con->window->urgent.tv_usec = 0;
1557  }
1558  }
1559 
1561 
1562  if (con->urgent == urgent)
1563  LOG("Urgency flag changed to %d\n", con->urgent);
1564 
1565  Con *ws;
1566  /* Set the urgency flag on the workspace, if a workspace could be found
1567  * (for dock clients, that is not the case). */
1568  if ((ws = con_get_workspace(con)) != NULL)
1570 }
1571 
1572 /*
1573  * Create a string representing the subtree under con.
1574  *
1575  */
1577  /* this code works as follows:
1578  * 1) create a string with the layout type (D/V/H/T/S) and an opening bracket
1579  * 2) append the tree representation of the children to the string
1580  * 3) add closing bracket
1581  *
1582  * The recursion ends when we hit a leaf, in which case we return the
1583  * class_instance of the contained window.
1584  */
1585 
1586  /* end of recursion */
1587  if (con_is_leaf(con)) {
1588  if (!con->window)
1589  return sstrdup("nowin");
1590 
1591  if (!con->window->class_instance)
1592  return sstrdup("noinstance");
1593 
1594  return sstrdup(con->window->class_instance);
1595  }
1596 
1597  char *buf;
1598  /* 1) add the Layout type to buf */
1599  if (con->layout == L_DEFAULT)
1600  buf = sstrdup("D[");
1601  else if (con->layout == L_SPLITV)
1602  buf = sstrdup("V[");
1603  else if (con->layout == L_SPLITH)
1604  buf = sstrdup("H[");
1605  else if (con->layout == L_TABBED)
1606  buf = sstrdup("T[");
1607  else if (con->layout == L_STACKED)
1608  buf = sstrdup("S[");
1609  else {
1610  ELOG("BUG: Code not updated to account for new layout type\n");
1611  assert(false);
1612  }
1613 
1614  /* 2) append representation of children */
1615  Con *child;
1616  TAILQ_FOREACH(child, &(con->nodes_head), nodes) {
1617  char *child_txt = con_get_tree_representation(child);
1618 
1619  char *tmp_buf;
1620  sasprintf(&tmp_buf, "%s%s%s", buf,
1621  (TAILQ_FIRST(&(con->nodes_head)) == child ? "" : " "), child_txt);
1622  free(buf);
1623  buf = tmp_buf;
1624  }
1625 
1626  /* 3) close the brackets */
1627  char *complete_buf;
1628  sasprintf(&complete_buf, "%s]", buf);
1629  free(buf);
1630 
1631  return complete_buf;
1632 }