xref: /openbsd/usr.bin/tmux/layout.c (revision d2117533)
1 /* $OpenBSD: layout.c,v 1.49 2024/11/05 09:41:17 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5  * Copyright (c) 2016 Stephen Kent <smkent@smkent.net>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 
22 #include <stdlib.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * The window layout is a tree of cells each of which can be one of: a
28  * left-right container for a list of cells, a top-bottom container for a list
29  * of cells, or a container for a window pane.
30  *
31  * Each window has a pointer to the root of its layout tree (containing its
32  * panes), every pane has a pointer back to the cell containing it, and each
33  * cell a pointer to its parent cell.
34  */
35 
36 static u_int	layout_resize_check(struct window *, struct layout_cell *,
37 		    enum layout_type);
38 static int	layout_resize_pane_grow(struct window *, struct layout_cell *,
39 		    enum layout_type, int, int);
40 static int	layout_resize_pane_shrink(struct window *, struct layout_cell *,
41 		    enum layout_type, int);
42 static u_int	layout_new_pane_size(struct window *, u_int,
43 		    struct layout_cell *, enum layout_type, u_int, u_int,
44 		    u_int);
45 static int	layout_set_size_check(struct window *, struct layout_cell *,
46 		    enum layout_type, int);
47 static void	layout_resize_child_cells(struct window *,
48 		    struct layout_cell *);
49 
50 struct layout_cell *
layout_create_cell(struct layout_cell * lcparent)51 layout_create_cell(struct layout_cell *lcparent)
52 {
53 	struct layout_cell	*lc;
54 
55 	lc = xmalloc(sizeof *lc);
56 	lc->type = LAYOUT_WINDOWPANE;
57 	lc->parent = lcparent;
58 
59 	TAILQ_INIT(&lc->cells);
60 
61 	lc->sx = UINT_MAX;
62 	lc->sy = UINT_MAX;
63 
64 	lc->xoff = UINT_MAX;
65 	lc->yoff = UINT_MAX;
66 
67 	lc->wp = NULL;
68 
69 	return (lc);
70 }
71 
72 void
layout_free_cell(struct layout_cell * lc)73 layout_free_cell(struct layout_cell *lc)
74 {
75 	struct layout_cell	*lcchild;
76 
77 	switch (lc->type) {
78 	case LAYOUT_LEFTRIGHT:
79 	case LAYOUT_TOPBOTTOM:
80 		while (!TAILQ_EMPTY(&lc->cells)) {
81 			lcchild = TAILQ_FIRST(&lc->cells);
82 			TAILQ_REMOVE(&lc->cells, lcchild, entry);
83 			layout_free_cell(lcchild);
84 		}
85 		break;
86 	case LAYOUT_WINDOWPANE:
87 		if (lc->wp != NULL)
88 			lc->wp->layout_cell = NULL;
89 		break;
90 	}
91 
92 	free(lc);
93 }
94 
95 void
layout_print_cell(struct layout_cell * lc,const char * hdr,u_int n)96 layout_print_cell(struct layout_cell *lc, const char *hdr, u_int n)
97 {
98 	struct layout_cell	*lcchild;
99 	const char		*type;
100 
101 	switch (lc->type) {
102 	case LAYOUT_LEFTRIGHT:
103 		type = "LEFTRIGHT";
104 		break;
105 	case LAYOUT_TOPBOTTOM:
106 		type = "TOPBOTTOM";
107 		break;
108 	case LAYOUT_WINDOWPANE:
109 		type = "WINDOWPANE";
110 		break;
111 	default:
112 		type = "UNKNOWN";
113 		break;
114 	}
115 	log_debug("%s:%*s%p type %s [parent %p] wp=%p [%u,%u %ux%u]", hdr, n,
116 	    " ", lc, type, lc->parent, lc->wp, lc->xoff, lc->yoff, lc->sx,
117 	    lc->sy);
118 	switch (lc->type) {
119 	case LAYOUT_LEFTRIGHT:
120 	case LAYOUT_TOPBOTTOM:
121 		TAILQ_FOREACH(lcchild, &lc->cells, entry)
122 		    	layout_print_cell(lcchild, hdr, n + 1);
123 		break;
124 	case LAYOUT_WINDOWPANE:
125 		break;
126 	}
127 }
128 
129 struct layout_cell *
layout_search_by_border(struct layout_cell * lc,u_int x,u_int y)130 layout_search_by_border(struct layout_cell *lc, u_int x, u_int y)
131 {
132 	struct layout_cell	*lcchild, *last = NULL;
133 
134 	TAILQ_FOREACH(lcchild, &lc->cells, entry) {
135 		if (x >= lcchild->xoff && x < lcchild->xoff + lcchild->sx &&
136 		    y >= lcchild->yoff && y < lcchild->yoff + lcchild->sy) {
137 			/* Inside the cell - recurse. */
138 			return (layout_search_by_border(lcchild, x, y));
139 		}
140 
141 		if (last == NULL) {
142 			last = lcchild;
143 			continue;
144 		}
145 
146 		switch (lc->type) {
147 		case LAYOUT_LEFTRIGHT:
148 			if (x < lcchild->xoff && x >= last->xoff + last->sx)
149 				return (last);
150 			break;
151 		case LAYOUT_TOPBOTTOM:
152 			if (y < lcchild->yoff && y >= last->yoff + last->sy)
153 				return (last);
154 			break;
155 		case LAYOUT_WINDOWPANE:
156 			break;
157 		}
158 
159 		last = lcchild;
160 	}
161 
162 	return (NULL);
163 }
164 
165 void
layout_set_size(struct layout_cell * lc,u_int sx,u_int sy,u_int xoff,u_int yoff)166 layout_set_size(struct layout_cell *lc, u_int sx, u_int sy, u_int xoff,
167     u_int yoff)
168 {
169 	lc->sx = sx;
170 	lc->sy = sy;
171 
172 	lc->xoff = xoff;
173 	lc->yoff = yoff;
174 }
175 
176 void
layout_make_leaf(struct layout_cell * lc,struct window_pane * wp)177 layout_make_leaf(struct layout_cell *lc, struct window_pane *wp)
178 {
179 	lc->type = LAYOUT_WINDOWPANE;
180 
181 	TAILQ_INIT(&lc->cells);
182 
183 	wp->layout_cell = lc;
184 	lc->wp = wp;
185 }
186 
187 void
layout_make_node(struct layout_cell * lc,enum layout_type type)188 layout_make_node(struct layout_cell *lc, enum layout_type type)
189 {
190 	if (type == LAYOUT_WINDOWPANE)
191 		fatalx("bad layout type");
192 	lc->type = type;
193 
194 	TAILQ_INIT(&lc->cells);
195 
196 	if (lc->wp != NULL)
197 		lc->wp->layout_cell = NULL;
198 	lc->wp = NULL;
199 }
200 
201 /* Fix cell offsets for a child cell. */
202 static void
layout_fix_offsets1(struct layout_cell * lc)203 layout_fix_offsets1(struct layout_cell *lc)
204 {
205 	struct layout_cell	*lcchild;
206 	u_int			 xoff, yoff;
207 
208 	if (lc->type == LAYOUT_LEFTRIGHT) {
209 		xoff = lc->xoff;
210 		TAILQ_FOREACH(lcchild, &lc->cells, entry) {
211 			lcchild->xoff = xoff;
212 			lcchild->yoff = lc->yoff;
213 			if (lcchild->type != LAYOUT_WINDOWPANE)
214 				layout_fix_offsets1(lcchild);
215 			xoff += lcchild->sx + 1;
216 		}
217 	} else {
218 		yoff = lc->yoff;
219 		TAILQ_FOREACH(lcchild, &lc->cells, entry) {
220 			lcchild->xoff = lc->xoff;
221 			lcchild->yoff = yoff;
222 			if (lcchild->type != LAYOUT_WINDOWPANE)
223 				layout_fix_offsets1(lcchild);
224 			yoff += lcchild->sy + 1;
225 		}
226 	}
227 }
228 
229 /* Update cell offsets based on their sizes. */
230 void
layout_fix_offsets(struct window * w)231 layout_fix_offsets(struct window *w)
232 {
233 	struct layout_cell      *lc = w->layout_root;
234 
235 	lc->xoff = 0;
236 	lc->yoff = 0;
237 
238 	layout_fix_offsets1(lc);
239 }
240 
241 /* Is this a top cell? */
242 static int
layout_cell_is_top(struct window * w,struct layout_cell * lc)243 layout_cell_is_top(struct window *w, struct layout_cell *lc)
244 {
245 	struct layout_cell	*next;
246 
247 	while (lc != w->layout_root) {
248 		next = lc->parent;
249 		if (next->type == LAYOUT_TOPBOTTOM &&
250 		    lc != TAILQ_FIRST(&next->cells))
251 			return (0);
252 		lc = next;
253 	}
254 	return (1);
255 }
256 
257 /* Is this a bottom cell? */
258 static int
layout_cell_is_bottom(struct window * w,struct layout_cell * lc)259 layout_cell_is_bottom(struct window *w, struct layout_cell *lc)
260 {
261 	struct layout_cell	*next;
262 
263 	while (lc != w->layout_root) {
264 		next = lc->parent;
265 		if (next->type == LAYOUT_TOPBOTTOM &&
266 		    lc != TAILQ_LAST(&next->cells, layout_cells))
267 			return (0);
268 		lc = next;
269 	}
270 	return (1);
271 }
272 
273 /*
274  * Returns 1 if we need to add an extra line for the pane status line. This is
275  * the case for the most upper or lower panes only.
276  */
277 static int
layout_add_horizontal_border(struct window * w,struct layout_cell * lc,int status)278 layout_add_horizontal_border(struct window *w, struct layout_cell *lc,
279     int status)
280 {
281 	if (status == PANE_STATUS_TOP)
282 		return (layout_cell_is_top(w, lc));
283 	if (status == PANE_STATUS_BOTTOM)
284 		return (layout_cell_is_bottom(w, lc));
285 	return (0);
286 }
287 
288 /* Update pane offsets and sizes based on their cells. */
289 void
layout_fix_panes(struct window * w,struct window_pane * skip)290 layout_fix_panes(struct window *w, struct window_pane *skip)
291 {
292 	struct window_pane	*wp;
293 	struct layout_cell	*lc;
294 	int			 status, scrollbars, sb_pos;
295 	u_int			 sx, sy, mode;
296 
297 	status = options_get_number(w->options, "pane-border-status");
298 	scrollbars = options_get_number(w->options, "pane-scrollbars");
299 	sb_pos = options_get_number(w->options, "pane-scrollbars-position");
300 
301 	TAILQ_FOREACH(wp, &w->panes, entry) {
302 		if ((lc = wp->layout_cell) == NULL || wp == skip)
303 			continue;
304 
305 		wp->xoff = lc->xoff;
306 		wp->yoff = lc->yoff;
307 		sx = lc->sx;
308 		sy = lc->sy;
309 
310 		if (layout_add_horizontal_border(w, lc, status)) {
311 			if (status == PANE_STATUS_TOP)
312 				wp->yoff++;
313 			sy--;
314 		}
315 
316 		mode = window_pane_mode(wp);
317 		if (scrollbars == PANE_SCROLLBARS_ALWAYS ||
318 		    (scrollbars == PANE_SCROLLBARS_MODAL &&
319 		    mode != WINDOW_PANE_NO_MODE)) {
320 			if (sb_pos == PANE_SCROLLBARS_LEFT) {
321 				sx = sx - PANE_SCROLLBARS_WIDTH;
322 				wp->xoff = wp->xoff + PANE_SCROLLBARS_WIDTH;
323 			} else /* sb_pos == PANE_SCROLLBARS_RIGHT */
324 				sx = sx - PANE_SCROLLBARS_WIDTH;
325 			wp->flags |= PANE_REDRAWSCROLLBAR;
326 		}
327 
328 		window_pane_resize(wp, sx, sy);
329 	}
330 }
331 
332 /* Count the number of available cells in a layout. */
333 u_int
layout_count_cells(struct layout_cell * lc)334 layout_count_cells(struct layout_cell *lc)
335 {
336 	struct layout_cell	*lcchild;
337 	u_int			 count;
338 
339 	switch (lc->type) {
340 	case LAYOUT_WINDOWPANE:
341 		return (1);
342 	case LAYOUT_LEFTRIGHT:
343 	case LAYOUT_TOPBOTTOM:
344 		count = 0;
345 		TAILQ_FOREACH(lcchild, &lc->cells, entry)
346 			count += layout_count_cells(lcchild);
347 		return (count);
348 	default:
349 		fatalx("bad layout type");
350 	}
351 }
352 
353 /* Calculate how much size is available to be removed from a cell. */
354 static u_int
layout_resize_check(struct window * w,struct layout_cell * lc,enum layout_type type)355 layout_resize_check(struct window *w, struct layout_cell *lc,
356     enum layout_type type)
357 {
358 	struct layout_cell	*lcchild;
359 	u_int			 available, minimum;
360 	int			 status, scrollbars;
361 
362 	status = options_get_number(w->options, "pane-border-status");
363 	scrollbars = options_get_number(w->options, "pane-scrollbars");
364 
365 	if (lc->type == LAYOUT_WINDOWPANE) {
366 		/* Space available in this cell only. */
367 		if (type == LAYOUT_LEFTRIGHT) {
368 			available = lc->sx;
369 			if (scrollbars)
370 				minimum = PANE_MINIMUM + PANE_SCROLLBARS_WIDTH;
371 			else
372 				minimum = PANE_MINIMUM;
373 		} else {
374 			available = lc->sy;
375 			if (layout_add_horizontal_border(w, lc, status))
376 				minimum = PANE_MINIMUM + 1;
377 			else
378 				minimum = PANE_MINIMUM;
379 		}
380 		if (available > minimum)
381 			available -= minimum;
382 		else
383 			available = 0;
384 	} else if (lc->type == type) {
385 		/* Same type: total of available space in all child cells. */
386 		available = 0;
387 		TAILQ_FOREACH(lcchild, &lc->cells, entry)
388 			available += layout_resize_check(w, lcchild, type);
389 	} else {
390 		/* Different type: minimum of available space in child cells. */
391 		minimum = UINT_MAX;
392 		TAILQ_FOREACH(lcchild, &lc->cells, entry) {
393 			available = layout_resize_check(w, lcchild, type);
394 			if (available < minimum)
395 				minimum = available;
396 		}
397 		available = minimum;
398 	}
399 
400 	return (available);
401 }
402 
403 /*
404  * Adjust cell size evenly, including altering its children. This function
405  * expects the change to have already been bounded to the space available.
406  */
407 void
layout_resize_adjust(struct window * w,struct layout_cell * lc,enum layout_type type,int change)408 layout_resize_adjust(struct window *w, struct layout_cell *lc,
409     enum layout_type type, int change)
410 {
411 	struct layout_cell	*lcchild;
412 
413 	/* Adjust the cell size. */
414 	if (type == LAYOUT_LEFTRIGHT)
415 		lc->sx += change;
416 	else
417 		lc->sy += change;
418 
419 	/* If this is a leaf cell, that is all that is necessary. */
420 	if (type == LAYOUT_WINDOWPANE)
421 		return;
422 
423 	/* Child cell runs in a different direction. */
424 	if (lc->type != type) {
425 		TAILQ_FOREACH(lcchild, &lc->cells, entry)
426 			layout_resize_adjust(w, lcchild, type, change);
427 		return;
428 	}
429 
430 	/*
431 	 * Child cell runs in the same direction. Adjust each child equally
432 	 * until no further change is possible.
433 	 */
434 	while (change != 0) {
435 		TAILQ_FOREACH(lcchild, &lc->cells, entry) {
436 			if (change == 0)
437 				break;
438 			if (change > 0) {
439 				layout_resize_adjust(w, lcchild, type, 1);
440 				change--;
441 				continue;
442 			}
443 			if (layout_resize_check(w, lcchild, type) > 0) {
444 				layout_resize_adjust(w, lcchild, type, -1);
445 				change++;
446 			}
447 		}
448 	}
449 }
450 
451 /* Destroy a cell and redistribute the space. */
452 void
layout_destroy_cell(struct window * w,struct layout_cell * lc,struct layout_cell ** lcroot)453 layout_destroy_cell(struct window *w, struct layout_cell *lc,
454     struct layout_cell **lcroot)
455 {
456 	struct layout_cell     *lcother, *lcparent;
457 
458 	/*
459 	 * If no parent, this is the last pane so window close is imminent and
460 	 * there is no need to resize anything.
461 	 */
462 	lcparent = lc->parent;
463 	if (lcparent == NULL) {
464 		layout_free_cell(lc);
465 		*lcroot = NULL;
466 		return;
467 	}
468 
469 	/* Merge the space into the previous or next cell. */
470 	if (lc == TAILQ_FIRST(&lcparent->cells))
471 		lcother = TAILQ_NEXT(lc, entry);
472 	else
473 		lcother = TAILQ_PREV(lc, layout_cells, entry);
474 	if (lcother != NULL && lcparent->type == LAYOUT_LEFTRIGHT)
475 		layout_resize_adjust(w, lcother, lcparent->type, lc->sx + 1);
476 	else if (lcother != NULL)
477 		layout_resize_adjust(w, lcother, lcparent->type, lc->sy + 1);
478 
479 	/* Remove this from the parent's list. */
480 	TAILQ_REMOVE(&lcparent->cells, lc, entry);
481 	layout_free_cell(lc);
482 
483 	/*
484 	 * If the parent now has one cell, remove the parent from the tree and
485 	 * replace it by that cell.
486 	 */
487 	lc = TAILQ_FIRST(&lcparent->cells);
488 	if (TAILQ_NEXT(lc, entry) == NULL) {
489 		TAILQ_REMOVE(&lcparent->cells, lc, entry);
490 
491 		lc->parent = lcparent->parent;
492 		if (lc->parent == NULL) {
493 			lc->xoff = 0; lc->yoff = 0;
494 			*lcroot = lc;
495 		} else
496 			TAILQ_REPLACE(&lc->parent->cells, lcparent, lc, entry);
497 
498 		layout_free_cell(lcparent);
499 	}
500 }
501 
502 void
layout_init(struct window * w,struct window_pane * wp)503 layout_init(struct window *w, struct window_pane *wp)
504 {
505 	struct layout_cell	*lc;
506 
507 	lc = w->layout_root = layout_create_cell(NULL);
508 	layout_set_size(lc, w->sx, w->sy, 0, 0);
509 	layout_make_leaf(lc, wp);
510 	layout_fix_panes(w, NULL);
511 }
512 
513 void
layout_free(struct window * w)514 layout_free(struct window *w)
515 {
516 	layout_free_cell(w->layout_root);
517 }
518 
519 /* Resize the entire layout after window resize. */
520 void
layout_resize(struct window * w,u_int sx,u_int sy)521 layout_resize(struct window *w, u_int sx, u_int sy)
522 {
523 	struct layout_cell	*lc = w->layout_root;
524 	int			 xlimit, ylimit, xchange, ychange;
525 
526 	/*
527 	 * Adjust horizontally. Do not attempt to reduce the layout lower than
528 	 * the minimum (more than the amount returned by layout_resize_check).
529 	 *
530 	 * This can mean that the window size is smaller than the total layout
531 	 * size: redrawing this is handled at a higher level, but it does leave
532 	 * a problem with growing the window size here: if the current size is
533 	 * < the minimum, growing proportionately by adding to each pane is
534 	 * wrong as it would keep the layout size larger than the window size.
535 	 * Instead, spread the difference between the minimum and the new size
536 	 * out proportionately - this should leave the layout fitting the new
537 	 * window size.
538 	 */
539 	xchange = sx - lc->sx;
540 	xlimit = layout_resize_check(w, lc, LAYOUT_LEFTRIGHT);
541 	if (xchange < 0 && xchange < -xlimit)
542 		xchange = -xlimit;
543 	if (xlimit == 0) {
544 		if (sx <= lc->sx)	/* lc->sx is minimum possible */
545 			xchange = 0;
546 		else
547 			xchange = sx - lc->sx;
548 	}
549 	if (xchange != 0)
550 		layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, xchange);
551 
552 	/* Adjust vertically in a similar fashion. */
553 	ychange = sy - lc->sy;
554 	ylimit = layout_resize_check(w, lc, LAYOUT_TOPBOTTOM);
555 	if (ychange < 0 && ychange < -ylimit)
556 		ychange = -ylimit;
557 	if (ylimit == 0) {
558 		if (sy <= lc->sy)	/* lc->sy is minimum possible */
559 			ychange = 0;
560 		else
561 			ychange = sy - lc->sy;
562 	}
563 	if (ychange != 0)
564 		layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, ychange);
565 
566 	/* Fix cell offsets. */
567 	layout_fix_offsets(w);
568 	layout_fix_panes(w, NULL);
569 }
570 
571 /* Resize a pane to an absolute size. */
572 void
layout_resize_pane_to(struct window_pane * wp,enum layout_type type,u_int new_size)573 layout_resize_pane_to(struct window_pane *wp, enum layout_type type,
574     u_int new_size)
575 {
576 	struct layout_cell     *lc, *lcparent;
577 	int			change, size;
578 
579 	lc = wp->layout_cell;
580 
581 	/* Find next parent of the same type. */
582 	lcparent = lc->parent;
583 	while (lcparent != NULL && lcparent->type != type) {
584 		lc = lcparent;
585 		lcparent = lc->parent;
586 	}
587 	if (lcparent == NULL)
588 		return;
589 
590 	/* Work out the size adjustment. */
591 	if (type == LAYOUT_LEFTRIGHT)
592 		size = lc->sx;
593 	else
594 		size = lc->sy;
595 	if (lc == TAILQ_LAST(&lcparent->cells, layout_cells))
596 		change = size - new_size;
597 	else
598 		change = new_size - size;
599 
600 	/* Resize the pane. */
601 	layout_resize_pane(wp, type, change, 1);
602 }
603 
604 void
layout_resize_layout(struct window * w,struct layout_cell * lc,enum layout_type type,int change,int opposite)605 layout_resize_layout(struct window *w, struct layout_cell *lc,
606     enum layout_type type, int change, int opposite)
607 {
608 	int	needed, size;
609 
610 	/* Grow or shrink the cell. */
611 	needed = change;
612 	while (needed != 0) {
613 		if (change > 0) {
614 			size = layout_resize_pane_grow(w, lc, type, needed,
615 			    opposite);
616 			needed -= size;
617 		} else {
618 			size = layout_resize_pane_shrink(w, lc, type, needed);
619 			needed += size;
620 		}
621 
622 		if (size == 0)	/* no more change possible */
623 			break;
624 	}
625 
626 	/* Fix cell offsets. */
627 	layout_fix_offsets(w);
628 	layout_fix_panes(w, NULL);
629 	notify_window("window-layout-changed", w);
630 }
631 
632 /* Resize a single pane within the layout. */
633 void
layout_resize_pane(struct window_pane * wp,enum layout_type type,int change,int opposite)634 layout_resize_pane(struct window_pane *wp, enum layout_type type, int change,
635     int opposite)
636 {
637 	struct layout_cell	*lc, *lcparent;
638 
639 	lc = wp->layout_cell;
640 
641 	/* Find next parent of the same type. */
642 	lcparent = lc->parent;
643 	while (lcparent != NULL && lcparent->type != type) {
644 		lc = lcparent;
645 		lcparent = lc->parent;
646 	}
647 	if (lcparent == NULL)
648 		return;
649 
650 	/* If this is the last cell, move back one. */
651 	if (lc == TAILQ_LAST(&lcparent->cells, layout_cells))
652 		lc = TAILQ_PREV(lc, layout_cells, entry);
653 
654 	layout_resize_layout(wp->window, lc, type, change, opposite);
655 }
656 
657 /* Helper function to grow pane. */
658 static int
layout_resize_pane_grow(struct window * w,struct layout_cell * lc,enum layout_type type,int needed,int opposite)659 layout_resize_pane_grow(struct window *w, struct layout_cell *lc,
660     enum layout_type type, int needed, int opposite)
661 {
662 	struct layout_cell	*lcadd, *lcremove;
663 	u_int			 size = 0;
664 
665 	/* Growing. Always add to the current cell. */
666 	lcadd = lc;
667 
668 	/* Look towards the tail for a suitable cell for reduction. */
669 	lcremove = TAILQ_NEXT(lc, entry);
670 	while (lcremove != NULL) {
671 		size = layout_resize_check(w, lcremove, type);
672 		if (size > 0)
673 			break;
674 		lcremove = TAILQ_NEXT(lcremove, entry);
675 	}
676 
677 	/* If none found, look towards the head. */
678 	if (opposite && lcremove == NULL) {
679 		lcremove = TAILQ_PREV(lc, layout_cells, entry);
680 		while (lcremove != NULL) {
681 			size = layout_resize_check(w, lcremove, type);
682 			if (size > 0)
683 				break;
684 			lcremove = TAILQ_PREV(lcremove, layout_cells, entry);
685 		}
686 	}
687 	if (lcremove == NULL)
688 		return (0);
689 
690 	/* Change the cells. */
691 	if (size > (u_int) needed)
692 		size = needed;
693 	layout_resize_adjust(w, lcadd, type, size);
694 	layout_resize_adjust(w, lcremove, type, -size);
695 	return (size);
696 }
697 
698 /* Helper function to shrink pane. */
699 static int
layout_resize_pane_shrink(struct window * w,struct layout_cell * lc,enum layout_type type,int needed)700 layout_resize_pane_shrink(struct window *w, struct layout_cell *lc,
701     enum layout_type type, int needed)
702 {
703 	struct layout_cell	*lcadd, *lcremove;
704 	u_int			 size;
705 
706 	/* Shrinking. Find cell to remove from by walking towards head. */
707 	lcremove = lc;
708 	do {
709 		size = layout_resize_check(w, lcremove, type);
710 		if (size != 0)
711 			break;
712 		lcremove = TAILQ_PREV(lcremove, layout_cells, entry);
713 	} while (lcremove != NULL);
714 	if (lcremove == NULL)
715 		return (0);
716 
717 	/* And add onto the next cell (from the original cell). */
718 	lcadd = TAILQ_NEXT(lc, entry);
719 	if (lcadd == NULL)
720 		return (0);
721 
722 	/* Change the cells. */
723 	if (size > (u_int) -needed)
724 		size = -needed;
725 	layout_resize_adjust(w, lcadd, type, size);
726 	layout_resize_adjust(w, lcremove, type, -size);
727 	return (size);
728 }
729 
730 /* Assign window pane to newly split cell. */
731 void
layout_assign_pane(struct layout_cell * lc,struct window_pane * wp,int do_not_resize)732 layout_assign_pane(struct layout_cell *lc, struct window_pane *wp,
733     int do_not_resize)
734 {
735 	layout_make_leaf(lc, wp);
736 	if (do_not_resize)
737 		layout_fix_panes(wp->window, wp);
738 	else
739 		layout_fix_panes(wp->window, NULL);
740 }
741 
742 /* Calculate the new pane size for resized parent. */
743 static u_int
layout_new_pane_size(struct window * w,u_int previous,struct layout_cell * lc,enum layout_type type,u_int size,u_int count_left,u_int size_left)744 layout_new_pane_size(struct window *w, u_int previous, struct layout_cell *lc,
745     enum layout_type type, u_int size, u_int count_left, u_int size_left)
746 {
747 	u_int	new_size, min, max, available;
748 
749 	/* If this is the last cell, it can take all of the remaining size. */
750 	if (count_left == 1)
751 		return (size_left);
752 
753 	/* How much is available in this parent? */
754 	available = layout_resize_check(w, lc, type);
755 
756 	/*
757 	 * Work out the minimum size of this cell and the new size
758 	 * proportionate to the previous size.
759 	 */
760 	min = (PANE_MINIMUM + 1) * (count_left - 1);
761 	if (type == LAYOUT_LEFTRIGHT) {
762 		if (lc->sx - available > min)
763 			min = lc->sx - available;
764 		new_size = (lc->sx * size) / previous;
765 	} else {
766 		if (lc->sy - available > min)
767 			min = lc->sy - available;
768 		new_size = (lc->sy * size) / previous;
769 	}
770 
771 	/* Check against the maximum and minimum size. */
772 	max = size_left - min;
773 	if (new_size > max)
774 		new_size = max;
775 	if (new_size < PANE_MINIMUM)
776 		new_size = PANE_MINIMUM;
777 	return (new_size);
778 }
779 
780 /* Check if the cell and all its children can be resized to a specific size. */
781 static int
layout_set_size_check(struct window * w,struct layout_cell * lc,enum layout_type type,int size)782 layout_set_size_check(struct window *w, struct layout_cell *lc,
783     enum layout_type type, int size)
784 {
785 	struct layout_cell	*lcchild;
786 	u_int			 new_size, available, previous, count, idx;
787 
788 	/* Cells with no children must just be bigger than minimum. */
789 	if (lc->type == LAYOUT_WINDOWPANE)
790 		return (size >= PANE_MINIMUM);
791 	available = size;
792 
793 	/* Count number of children. */
794 	count = 0;
795 	TAILQ_FOREACH(lcchild, &lc->cells, entry)
796 		count++;
797 
798 	/* Check new size will work for each child. */
799 	if (lc->type == type) {
800 		if (available < (count * 2) - 1)
801 			return (0);
802 
803 		if (type == LAYOUT_LEFTRIGHT)
804 			previous = lc->sx;
805 		else
806 			previous = lc->sy;
807 
808 		idx = 0;
809 		TAILQ_FOREACH(lcchild, &lc->cells, entry) {
810 			new_size = layout_new_pane_size(w, previous, lcchild,
811 			    type, size, count - idx, available);
812 			if (idx == count - 1) {
813 				if (new_size > available)
814 					return (0);
815 				available -= new_size;
816 			} else {
817 				if (new_size + 1 > available)
818 					return (0);
819 				available -= new_size + 1;
820 			}
821 			if (!layout_set_size_check(w, lcchild, type, new_size))
822 				return (0);
823 			idx++;
824 		}
825 	} else {
826 		TAILQ_FOREACH(lcchild, &lc->cells, entry) {
827 			if (lcchild->type == LAYOUT_WINDOWPANE)
828 				continue;
829 			if (!layout_set_size_check(w, lcchild, type, size))
830 				return (0);
831 		}
832 	}
833 
834 	return (1);
835 }
836 
837 /* Resize all child cells to fit within the current cell. */
838 static void
layout_resize_child_cells(struct window * w,struct layout_cell * lc)839 layout_resize_child_cells(struct window *w, struct layout_cell *lc)
840 {
841 	struct layout_cell	*lcchild;
842 	u_int			 previous, available, count, idx;
843 
844 	if (lc->type == LAYOUT_WINDOWPANE)
845 		return;
846 
847 	/* What is the current size used? */
848 	count = 0;
849 	previous = 0;
850 	TAILQ_FOREACH(lcchild, &lc->cells, entry) {
851 		count++;
852 		if (lc->type == LAYOUT_LEFTRIGHT)
853 			previous += lcchild->sx;
854 		else if (lc->type == LAYOUT_TOPBOTTOM)
855 			previous += lcchild->sy;
856 	}
857 	previous += (count - 1);
858 
859 	/* And how much is available? */
860 	available = 0;
861 	if (lc->type == LAYOUT_LEFTRIGHT)
862 		available = lc->sx;
863 	else if (lc->type == LAYOUT_TOPBOTTOM)
864 		available = lc->sy;
865 
866 	/* Resize children into the new size. */
867 	idx = 0;
868 	TAILQ_FOREACH(lcchild, &lc->cells, entry) {
869 		if (lc->type == LAYOUT_TOPBOTTOM) {
870 			lcchild->sx = lc->sx;
871 			lcchild->xoff = lc->xoff;
872 		} else {
873 			lcchild->sx = layout_new_pane_size(w, previous, lcchild,
874 			    lc->type, lc->sx, count - idx, available);
875 			available -= (lcchild->sx + 1);
876 		}
877 		if (lc->type == LAYOUT_LEFTRIGHT)
878 			lcchild->sy = lc->sy;
879 		else {
880 			lcchild->sy = layout_new_pane_size(w, previous, lcchild,
881 			    lc->type, lc->sy, count - idx, available);
882 			available -= (lcchild->sy + 1);
883 		}
884 		layout_resize_child_cells(w, lcchild);
885 		idx++;
886 	}
887 }
888 
889 /*
890  * Split a pane into two. size is a hint, or -1 for default half/half
891  * split. This must be followed by layout_assign_pane before much else happens!
892  */
893 struct layout_cell *
layout_split_pane(struct window_pane * wp,enum layout_type type,int size,int flags)894 layout_split_pane(struct window_pane *wp, enum layout_type type, int size,
895     int flags)
896 {
897 	struct layout_cell     *lc, *lcparent, *lcnew, *lc1, *lc2;
898 	u_int			sx, sy, xoff, yoff, size1, size2, minimum;
899 	u_int			new_size, saved_size, resize_first = 0;
900 	int			full_size = (flags & SPAWN_FULLSIZE), status;
901 	int			scrollbars;
902 
903 	/*
904 	 * If full_size is specified, add a new cell at the top of the window
905 	 * layout. Otherwise, split the cell for the current pane.
906 	 */
907 	if (full_size)
908 		lc = wp->window->layout_root;
909 	else
910 		lc = wp->layout_cell;
911 	status = options_get_number(wp->window->options, "pane-border-status");
912 	scrollbars = options_get_number(wp->window->options, "pane-scrollbars");
913 
914 	/* Copy the old cell size. */
915 	sx = lc->sx;
916 	sy = lc->sy;
917 	xoff = lc->xoff;
918 	yoff = lc->yoff;
919 
920 	/* Check there is enough space for the two new panes. */
921 	switch (type) {
922 	case LAYOUT_LEFTRIGHT:
923 		if (scrollbars)
924 			minimum = PANE_MINIMUM * 2 + PANE_SCROLLBARS_WIDTH;
925 		else
926 			minimum = PANE_MINIMUM * 2 + 1;
927 		if (sx < minimum)
928 			return (NULL);
929 		break;
930 	case LAYOUT_TOPBOTTOM:
931 		if (layout_add_horizontal_border(wp->window, lc, status))
932 			minimum = PANE_MINIMUM * 2 + 2;
933 		else
934 			minimum = PANE_MINIMUM * 2 + 1;
935 		if (sy < minimum)
936 			return (NULL);
937 		break;
938 	default:
939 		fatalx("bad layout type");
940 	}
941 
942 	/*
943 	 * Calculate new cell sizes. size is the target size or -1 for middle
944 	 * split, size1 is the size of the top/left and size2 the bottom/right.
945 	 */
946 	if (type == LAYOUT_LEFTRIGHT)
947 		saved_size = sx;
948 	else
949 		saved_size = sy;
950 	if (size < 0)
951 		size2 = ((saved_size + 1) / 2) - 1;
952 	else if (flags & SPAWN_BEFORE)
953 		size2 = saved_size - size - 1;
954 	else
955 		size2 = size;
956 	if (size2 < PANE_MINIMUM)
957 		size2 = PANE_MINIMUM;
958 	else if (size2 > saved_size - 2)
959 		size2 = saved_size - 2;
960 	size1 = saved_size - 1 - size2;
961 
962 	/* Which size are we using? */
963 	if (flags & SPAWN_BEFORE)
964 		new_size = size2;
965 	else
966 		new_size = size1;
967 
968 	/* Confirm there is enough space for full size pane. */
969 	if (full_size && !layout_set_size_check(wp->window, lc, type, new_size))
970 		return (NULL);
971 
972 	if (lc->parent != NULL && lc->parent->type == type) {
973 		/*
974 		 * If the parent exists and is of the same type as the split,
975 		 * create a new cell and insert it after this one.
976 		 */
977 		lcparent = lc->parent;
978 		lcnew = layout_create_cell(lcparent);
979 		if (flags & SPAWN_BEFORE)
980 			TAILQ_INSERT_BEFORE(lc, lcnew, entry);
981 		else
982 			TAILQ_INSERT_AFTER(&lcparent->cells, lc, lcnew, entry);
983 	} else if (full_size && lc->parent == NULL && lc->type == type) {
984 		/*
985 		 * If the new full size pane is the same type as the root
986 		 * split, insert the new pane under the existing root cell
987 		 * instead of creating a new root cell. The existing layout
988 		 * must be resized before inserting the new cell.
989 		 */
990 		if (lc->type == LAYOUT_LEFTRIGHT) {
991 			lc->sx = new_size;
992 			layout_resize_child_cells(wp->window, lc);
993 			lc->sx = saved_size;
994 		} else if (lc->type == LAYOUT_TOPBOTTOM) {
995 			lc->sy = new_size;
996 			layout_resize_child_cells(wp->window, lc);
997 			lc->sy = saved_size;
998 		}
999 		resize_first = 1;
1000 
1001 		/* Create the new cell. */
1002 		lcnew = layout_create_cell(lc);
1003 		size = saved_size - 1 - new_size;
1004 		if (lc->type == LAYOUT_LEFTRIGHT)
1005 			layout_set_size(lcnew, size, sy, 0, 0);
1006 		else if (lc->type == LAYOUT_TOPBOTTOM)
1007 			layout_set_size(lcnew, sx, size, 0, 0);
1008 		if (flags & SPAWN_BEFORE)
1009 			TAILQ_INSERT_HEAD(&lc->cells, lcnew, entry);
1010 		else
1011 			TAILQ_INSERT_TAIL(&lc->cells, lcnew, entry);
1012 	} else {
1013 		/*
1014 		 * Otherwise create a new parent and insert it.
1015 		 */
1016 
1017 		/* Create and insert the replacement parent. */
1018 		lcparent = layout_create_cell(lc->parent);
1019 		layout_make_node(lcparent, type);
1020 		layout_set_size(lcparent, sx, sy, xoff, yoff);
1021 		if (lc->parent == NULL)
1022 			wp->window->layout_root = lcparent;
1023 		else
1024 			TAILQ_REPLACE(&lc->parent->cells, lc, lcparent, entry);
1025 
1026 		/* Insert the old cell. */
1027 		lc->parent = lcparent;
1028 		TAILQ_INSERT_HEAD(&lcparent->cells, lc, entry);
1029 
1030 		/* Create the new child cell. */
1031 		lcnew = layout_create_cell(lcparent);
1032 		if (flags & SPAWN_BEFORE)
1033 			TAILQ_INSERT_HEAD(&lcparent->cells, lcnew, entry);
1034 		else
1035 			TAILQ_INSERT_TAIL(&lcparent->cells, lcnew, entry);
1036 	}
1037 	if (flags & SPAWN_BEFORE) {
1038 		lc1 = lcnew;
1039 		lc2 = lc;
1040 	} else {
1041 		lc1 = lc;
1042 		lc2 = lcnew;
1043 	}
1044 
1045 	/*
1046 	 * Set new cell sizes. size1 is the size of the top/left and size2 the
1047 	 * bottom/right.
1048 	 */
1049 	if (!resize_first && type == LAYOUT_LEFTRIGHT) {
1050 		layout_set_size(lc1, size1, sy, xoff, yoff);
1051 		layout_set_size(lc2, size2, sy, xoff + lc1->sx + 1, yoff);
1052 	} else if (!resize_first && type == LAYOUT_TOPBOTTOM) {
1053 		layout_set_size(lc1, sx, size1, xoff, yoff);
1054 		layout_set_size(lc2, sx, size2, xoff, yoff + lc1->sy + 1);
1055 	}
1056 	if (full_size) {
1057 		if (!resize_first)
1058 			layout_resize_child_cells(wp->window, lc);
1059 		layout_fix_offsets(wp->window);
1060 	} else
1061 		layout_make_leaf(lc, wp);
1062 
1063 	return (lcnew);
1064 }
1065 
1066 /* Destroy the cell associated with a pane. */
1067 void
layout_close_pane(struct window_pane * wp)1068 layout_close_pane(struct window_pane *wp)
1069 {
1070 	struct window	*w = wp->window;
1071 
1072 	/* Remove the cell. */
1073 	layout_destroy_cell(w, wp->layout_cell, &w->layout_root);
1074 
1075 	/* Fix pane offsets and sizes. */
1076 	if (w->layout_root != NULL) {
1077 		layout_fix_offsets(w);
1078 		layout_fix_panes(w, NULL);
1079 	}
1080 	notify_window("window-layout-changed", w);
1081 }
1082 
1083 int
layout_spread_cell(struct window * w,struct layout_cell * parent)1084 layout_spread_cell(struct window *w, struct layout_cell *parent)
1085 {
1086 	struct layout_cell	*lc;
1087 	u_int			 number, each, size, this;
1088 	int			 change, changed, status, scrollbars;
1089 
1090 	number = 0;
1091 	TAILQ_FOREACH (lc, &parent->cells, entry)
1092 		number++;
1093 	if (number <= 1)
1094 		return (0);
1095 	status = options_get_number(w->options, "pane-border-status");
1096 	scrollbars = options_get_number(w->options, "pane-scrollbars");
1097 
1098 	if (parent->type == LAYOUT_LEFTRIGHT) {
1099 		if (scrollbars)
1100 			size = parent->sx - PANE_SCROLLBARS_WIDTH;
1101 		else
1102 			size = parent->sx;
1103 	}
1104 	else if (parent->type == LAYOUT_TOPBOTTOM) {
1105 		if (layout_add_horizontal_border(w, parent, status))
1106 			size = parent->sy - 1;
1107 		else
1108 			size = parent->sy;
1109 	} else
1110 		return (0);
1111 	if (size < number - 1)
1112 		return (0);
1113 	each = (size - (number - 1)) / number;
1114 	if (each == 0)
1115 		return (0);
1116 
1117 	changed = 0;
1118 	TAILQ_FOREACH (lc, &parent->cells, entry) {
1119 		if (TAILQ_NEXT(lc, entry) == NULL)
1120 			each = size - ((each + 1) * (number - 1));
1121 		change = 0;
1122 		if (parent->type == LAYOUT_LEFTRIGHT) {
1123 			change = each - (int)lc->sx;
1124 			layout_resize_adjust(w, lc, LAYOUT_LEFTRIGHT, change);
1125 		} else if (parent->type == LAYOUT_TOPBOTTOM) {
1126 			if (layout_add_horizontal_border(w, lc, status))
1127 				this = each + 1;
1128 			else
1129 				this = each;
1130 			change = this - (int)lc->sy;
1131 			layout_resize_adjust(w, lc, LAYOUT_TOPBOTTOM, change);
1132 		}
1133 		if (change != 0)
1134 			changed = 1;
1135 	}
1136 	return (changed);
1137 }
1138 
1139 void
layout_spread_out(struct window_pane * wp)1140 layout_spread_out(struct window_pane *wp)
1141 {
1142 	struct layout_cell	*parent;
1143 	struct window		*w = wp->window;
1144 
1145 	parent = wp->layout_cell->parent;
1146 	if (parent == NULL)
1147 		return;
1148 
1149 	do {
1150 		if (layout_spread_cell(w, parent)) {
1151 			layout_fix_offsets(w);
1152 			layout_fix_panes(w, NULL);
1153 			break;
1154 		}
1155 	} while ((parent = parent->parent) != NULL);
1156 }
1157