xref: /openbsd-src/usr.bin/tmux/resize.c (revision 891a8d2917f69de90ad546931f8b459ef6c8f3c5)
1*891a8d29Snicm /* $OpenBSD: resize.c,v 1.52 2024/11/27 10:12:20 nicm Exp $ */
2311827fbSnicm 
3311827fbSnicm /*
498ca8272Snicm  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5311827fbSnicm  *
6311827fbSnicm  * Permission to use, copy, modify, and distribute this software for any
7311827fbSnicm  * purpose with or without fee is hereby granted, provided that the above
8311827fbSnicm  * copyright notice and this permission notice appear in all copies.
9311827fbSnicm  *
10311827fbSnicm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11311827fbSnicm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12311827fbSnicm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13311827fbSnicm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14311827fbSnicm  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15311827fbSnicm  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16311827fbSnicm  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17311827fbSnicm  */
18311827fbSnicm 
19311827fbSnicm #include <sys/types.h>
20311827fbSnicm 
21311827fbSnicm #include <string.h>
22311827fbSnicm 
23311827fbSnicm #include "tmux.h"
24311827fbSnicm 
257b470e93Snicm void
264a8b0ea5Snicm resize_window(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
277b470e93Snicm {
287b470e93Snicm 	int	zoomed;
297b470e93Snicm 
307b470e93Snicm 	/* Check size limits. */
317b470e93Snicm 	if (sx < WINDOW_MINIMUM)
327b470e93Snicm 		sx = WINDOW_MINIMUM;
337b470e93Snicm 	if (sx > WINDOW_MAXIMUM)
347b470e93Snicm 		sx = WINDOW_MAXIMUM;
357b470e93Snicm 	if (sy < WINDOW_MINIMUM)
367b470e93Snicm 		sy = WINDOW_MINIMUM;
377b470e93Snicm 	if (sy > WINDOW_MAXIMUM)
387b470e93Snicm 		sy = WINDOW_MAXIMUM;
397b470e93Snicm 
407b470e93Snicm 	/* If the window is zoomed, unzoom. */
417b470e93Snicm 	zoomed = w->flags & WINDOW_ZOOMED;
427b470e93Snicm 	if (zoomed)
430ad0daf4Snicm 		window_unzoom(w, 1);
447b470e93Snicm 
457b470e93Snicm 	/* Resize the layout first. */
467b470e93Snicm 	layout_resize(w, sx, sy);
477b470e93Snicm 
487b470e93Snicm 	/* Resize the window, it can be no smaller than the layout. */
497b470e93Snicm 	if (sx < w->layout_root->sx)
507b470e93Snicm 		sx = w->layout_root->sx;
517b470e93Snicm 	if (sy < w->layout_root->sy)
527b470e93Snicm 		sy = w->layout_root->sy;
534a8b0ea5Snicm 	window_resize(w, sx, sy, xpixel, ypixel);
548ef4e054Snicm 	log_debug("%s: @%u resized to %ux%u; layout %ux%u", __func__, w->id,
55d4ddf7e1Snicm 	    sx, sy, w->layout_root->sx, w->layout_root->sy);
567b470e93Snicm 
577b470e93Snicm 	/* Restore the window zoom state. */
587b470e93Snicm 	if (zoomed)
597b470e93Snicm 		window_zoom(w->active);
607b470e93Snicm 
617b470e93Snicm 	tty_update_window_offset(w);
627b470e93Snicm 	server_redraw_window(w);
637b470e93Snicm 	notify_window("window-layout-changed", w);
64ba37bc3cSnicm 	notify_window("window-resized", w);
65061703b1Snicm 	w->flags &= ~WINDOW_RESIZE;
667b470e93Snicm }
677b470e93Snicm 
68db993454Snicm static int
69db993454Snicm ignore_client_size(struct client *c)
70db993454Snicm {
71ed53a239Snicm 	struct client	*loop;
72ed53a239Snicm 
73db993454Snicm 	if (c->session == NULL)
74db993454Snicm 		return (1);
75db993454Snicm 	if (c->flags & CLIENT_NOSIZEFLAGS)
76db993454Snicm 		return (1);
77cfef6bbbSnicm 	if (c->flags & CLIENT_IGNORESIZE) {
78ed53a239Snicm 		/*
79cfef6bbbSnicm 		 * Ignore flagged clients if there are any attached clients
80cfef6bbbSnicm 		 * that aren't flagged.
81ed53a239Snicm 		 */
82ed53a239Snicm 		TAILQ_FOREACH (loop, &clients, entry) {
83ed53a239Snicm 			if (loop->session == NULL)
84ed53a239Snicm 				continue;
85ed53a239Snicm 			if (loop->flags & CLIENT_NOSIZEFLAGS)
86ed53a239Snicm 				continue;
87cfef6bbbSnicm 			if (~loop->flags & CLIENT_IGNORESIZE)
88ed53a239Snicm 				return (1);
89ed53a239Snicm 		}
90ed53a239Snicm 	}
9134ce0afcSnicm 	if ((c->flags & CLIENT_CONTROL) &&
9234ce0afcSnicm 	    (~c->flags & CLIENT_SIZECHANGED) &&
9334ce0afcSnicm 	    (~c->flags & CLIENT_WINDOWSIZECHANGED))
94db993454Snicm 		return (1);
95db993454Snicm 	return (0);
96db993454Snicm }
97db993454Snicm 
98a76f4211Snicm static u_int
99a76f4211Snicm clients_with_window(struct window *w)
1007b470e93Snicm {
1011be6ca1cSnicm 	struct client	*loop;
102a76f4211Snicm 	u_int		 n = 0;
1037b470e93Snicm 
104a76f4211Snicm 	TAILQ_FOREACH(loop, &clients, entry) {
105a76f4211Snicm 		if (ignore_client_size(loop) || !session_has(loop->session, w))
106a76f4211Snicm 			continue;
107a76f4211Snicm 		if (++n > 1)
108a76f4211Snicm 			break;
109a76f4211Snicm 	}
110a76f4211Snicm 	return (n);
111a76f4211Snicm }
112a76f4211Snicm 
113a76f4211Snicm static int
1142077c06bSnicm clients_calculate_size(int type, int current, struct client *c,
1152077c06bSnicm     struct session *s, struct window *w, int (*skip_client)(struct client *,
1162077c06bSnicm     int, int, struct session *, struct window *), u_int *sx, u_int *sy,
1172077c06bSnicm     u_int *xpixel, u_int *ypixel)
118a76f4211Snicm {
119a76f4211Snicm 	struct client		*loop;
12034ce0afcSnicm 	struct client_window	*cw;
121a76f4211Snicm 	u_int			 cx, cy, n = 0;
122a76f4211Snicm 
123a76f4211Snicm 	/*
124a76f4211Snicm 	 * Start comparing with 0 for largest and UINT_MAX for smallest or
125a76f4211Snicm 	 * latest.
126a76f4211Snicm 	 */
12734ce0afcSnicm 	if (type == WINDOW_SIZE_LARGEST) {
12834ce0afcSnicm 		*sx = 0;
12934ce0afcSnicm 		*sy = 0;
13034ce0afcSnicm 	} else if (type == WINDOW_SIZE_MANUAL) {
13134ce0afcSnicm 		*sx = w->manual_sx;
13234ce0afcSnicm 		*sy = w->manual_sy;
13334ce0afcSnicm 		log_debug("%s: manual size %ux%u", __func__, *sx, *sy);
13434ce0afcSnicm 	} else {
13534ce0afcSnicm 		*sx = UINT_MAX;
13634ce0afcSnicm 		*sy = UINT_MAX;
13734ce0afcSnicm 	}
1384a8b0ea5Snicm 	*xpixel = *ypixel = 0;
139a76f4211Snicm 
140a76f4211Snicm 	/*
141a76f4211Snicm 	 * For latest, count the number of clients with this window. We only
142a76f4211Snicm 	 * care if there is more than one.
143a76f4211Snicm 	 */
144cad9aae3Snicm 	if (type == WINDOW_SIZE_LATEST && w != NULL)
145a76f4211Snicm 		n = clients_with_window(w);
146a76f4211Snicm 
14734ce0afcSnicm 	/* Skip setting the size if manual */
14834ce0afcSnicm 	if (type == WINDOW_SIZE_MANUAL)
14934ce0afcSnicm 		goto skip;
15034ce0afcSnicm 
151a76f4211Snicm 	/* Loop over the clients and work out the size. */
1521be6ca1cSnicm 	TAILQ_FOREACH(loop, &clients, entry) {
1532077c06bSnicm 		if (loop != c && ignore_client_size(loop)) {
15434ce0afcSnicm 			log_debug("%s: ignoring %s (1)", __func__, loop->name);
1557b470e93Snicm 			continue;
1562077c06bSnicm 		}
1572077c06bSnicm 		if (loop != c && skip_client(loop, type, current, s, w)) {
15834ce0afcSnicm 			log_debug("%s: skipping %s (1)", __func__, loop->name);
1597b470e93Snicm 			continue;
1602077c06bSnicm 		}
1617b470e93Snicm 
162a76f4211Snicm 		/*
163a76f4211Snicm 		 * If there are multiple clients attached, only accept the
164a76f4211Snicm 		 * latest client; otherwise let the only client be chosen as
165a76f4211Snicm 		 * for smallest.
166a76f4211Snicm 		 */
1672077c06bSnicm 		if (type == WINDOW_SIZE_LATEST && n > 1 && loop != w->latest) {
1682077c06bSnicm 			log_debug("%s: %s is not latest", __func__, loop->name);
169a76f4211Snicm 			continue;
1702077c06bSnicm 		}
171a76f4211Snicm 
17234ce0afcSnicm 		/*
17334ce0afcSnicm 		 * If the client has a per-window size, use this instead if it is
17434ce0afcSnicm 		 * smaller.
17534ce0afcSnicm 		 */
17634ce0afcSnicm 		if (w != NULL)
17734ce0afcSnicm 			cw = server_client_get_client_window(loop, w->id);
17834ce0afcSnicm 		else
17934ce0afcSnicm 			cw = NULL;
18034ce0afcSnicm 
181a76f4211Snicm 		/* Work out this client's size. */
18255f2f580Snicm 		if (cw != NULL && cw->sx != 0 && cw->sy != 0) {
18334ce0afcSnicm 			cx = cw->sx;
18434ce0afcSnicm 			cy = cw->sy;
18534ce0afcSnicm 		} else {
1861be6ca1cSnicm 			cx = loop->tty.sx;
1871be6ca1cSnicm 			cy = loop->tty.sy - status_line_size(loop);
18834ce0afcSnicm 		}
1897b470e93Snicm 
190a76f4211Snicm 		/*
191a76f4211Snicm 		 * If it is larger or smaller than the best so far, update the
192a76f4211Snicm 		 * new size.
193a76f4211Snicm 		 */
194a76f4211Snicm 		if (type == WINDOW_SIZE_LARGEST) {
1957b470e93Snicm 			if (cx > *sx)
1967b470e93Snicm 				*sx = cx;
1977b470e93Snicm 			if (cy > *sy)
1987b470e93Snicm 				*sy = cy;
199a76f4211Snicm 		} else {
2007b470e93Snicm 			if (cx < *sx)
2017b470e93Snicm 				*sx = cx;
2027b470e93Snicm 			if (cy < *sy)
2037b470e93Snicm 				*sy = cy;
204a76f4211Snicm 		}
205a76f4211Snicm 		if (loop->tty.xpixel > *xpixel && loop->tty.ypixel > *ypixel) {
2064a8b0ea5Snicm 			*xpixel = loop->tty.xpixel;
2074a8b0ea5Snicm 			*ypixel = loop->tty.ypixel;
2084a8b0ea5Snicm 		}
2092077c06bSnicm 		log_debug("%s: after %s (%ux%u), size is %ux%u", __func__,
2102077c06bSnicm 		    loop->name, cx, cy, *sx, *sy);
2117b470e93Snicm 	}
2128ef4e054Snicm 	if (*sx != UINT_MAX && *sy != UINT_MAX)
2138ef4e054Snicm 		log_debug("%s: calculated size %ux%u", __func__, *sx, *sy);
2148ef4e054Snicm 	else
2158ef4e054Snicm 		log_debug("%s: no calculated size", __func__);
216a76f4211Snicm 
21734ce0afcSnicm skip:
21834ce0afcSnicm 	/*
21934ce0afcSnicm 	 * Do not allow any size to be larger than the per-client window size
22034ce0afcSnicm 	 * if one exists.
22134ce0afcSnicm 	 */
22234ce0afcSnicm 	if (w != NULL) {
22334ce0afcSnicm 		TAILQ_FOREACH(loop, &clients, entry) {
22434ce0afcSnicm 			if (loop != c && ignore_client_size(loop))
22534ce0afcSnicm 				continue;
22634ce0afcSnicm 			if (loop != c && skip_client(loop, type, current, s, w))
22734ce0afcSnicm 				continue;
22834ce0afcSnicm 
22934ce0afcSnicm 			/* Look up per-window size if any. */
23034ce0afcSnicm 			if (~loop->flags & CLIENT_WINDOWSIZECHANGED)
23134ce0afcSnicm 				continue;
23234ce0afcSnicm 			cw = server_client_get_client_window(loop, w->id);
23334ce0afcSnicm 			if (cw == NULL)
23434ce0afcSnicm 				continue;
23534ce0afcSnicm 
23634ce0afcSnicm 			/* Clamp the size. */
23734ce0afcSnicm 			log_debug("%s: %s size for @%u is %ux%u", __func__,
23834ce0afcSnicm 			    loop->name, w->id, cw->sx, cw->sy);
23934ce0afcSnicm 			if (cw->sx != 0 && *sx > cw->sx)
24034ce0afcSnicm 				*sx = cw->sx;
24134ce0afcSnicm 			if (cw->sy != 0 && *sy > cw->sy)
24234ce0afcSnicm 				*sy = cw->sy;
24334ce0afcSnicm 		}
24434ce0afcSnicm 	}
24534ce0afcSnicm 	if (*sx != UINT_MAX && *sy != UINT_MAX)
24634ce0afcSnicm 		log_debug("%s: calculated size %ux%u", __func__, *sx, *sy);
24734ce0afcSnicm 	else
24834ce0afcSnicm 		log_debug("%s: no calculated size", __func__);
24934ce0afcSnicm 
250a76f4211Snicm 	/* Return whether a suitable size was found. */
25134ce0afcSnicm 	if (type == WINDOW_SIZE_MANUAL) {
25234ce0afcSnicm 		log_debug("%s: type is manual", __func__);
25334ce0afcSnicm 		return (1);
25434ce0afcSnicm 	}
2552077c06bSnicm 	if (type == WINDOW_SIZE_LARGEST) {
2562077c06bSnicm 		log_debug("%s: type is largest", __func__);
257a76f4211Snicm 		return (*sx != 0 && *sy != 0);
2582077c06bSnicm 	}
2592077c06bSnicm 	if (type == WINDOW_SIZE_LATEST)
2602077c06bSnicm 		log_debug("%s: type is latest", __func__);
2612077c06bSnicm 	else
2622077c06bSnicm 		log_debug("%s: type is smallest", __func__);
263a76f4211Snicm 	return (*sx != UINT_MAX && *sy != UINT_MAX);
264a76f4211Snicm }
265a76f4211Snicm 
266a76f4211Snicm static int
267*891a8d29Snicm default_window_size_skip_client(struct client *loop, __unused int type,
268a76f4211Snicm     __unused int current, struct session *s, struct window *w)
269a76f4211Snicm {
270a76f4211Snicm 	if (w != NULL && !session_has(loop->session, w))
271a76f4211Snicm 		return (1);
272a76f4211Snicm 	if (w == NULL && loop->session != s)
273a76f4211Snicm 		return (1);
274a76f4211Snicm 	return (0);
275a76f4211Snicm }
276a76f4211Snicm 
277a76f4211Snicm void
278a76f4211Snicm default_window_size(struct client *c, struct session *s, struct window *w,
279a76f4211Snicm 	u_int *sx, u_int *sy, u_int *xpixel, u_int *ypixel, int type)
280a76f4211Snicm {
281a76f4211Snicm 	const char	*value;
282a76f4211Snicm 
283a76f4211Snicm 	/* Get type if not provided. */
284a76f4211Snicm 	if (type == -1)
285a76f4211Snicm 		type = options_get_number(global_w_options, "window-size");
286a76f4211Snicm 
287a76f4211Snicm 	/*
288a76f4211Snicm 	 * Latest clients can use the given client if suitable. If there is no
289a76f4211Snicm 	 * client and no window, use the default size as for manual type.
290a76f4211Snicm 	 */
2918ef4e054Snicm 	if (type == WINDOW_SIZE_LATEST && c != NULL && !ignore_client_size(c)) {
2921be6ca1cSnicm 		*sx = c->tty.sx;
2931be6ca1cSnicm 		*sy = c->tty.sy - status_line_size(c);
2944a8b0ea5Snicm 		*xpixel = c->tty.xpixel;
2954a8b0ea5Snicm 		*ypixel = c->tty.ypixel;
2962077c06bSnicm 		log_debug("%s: using %ux%u from %s", __func__, *sx, *sy,
2972077c06bSnicm 		    c->name);
2987b470e93Snicm 		goto done;
299a76f4211Snicm 	}
3007b470e93Snicm 
301a76f4211Snicm 	/*
302a8571561Snicm 	 * Ignore the given client if it is a control client - the creating
303a8571561Snicm 	 * client should only affect the size if it is not a control client.
304a8571561Snicm 	 */
305a8571561Snicm 	if (c != NULL && (c->flags & CLIENT_CONTROL))
306a8571561Snicm 		c = NULL;
307a8571561Snicm 
308a8571561Snicm 	/*
309a76f4211Snicm 	 * Look for a client to base the size on. If none exists (or the type
310a76f4211Snicm 	 * is manual), use the default-size option.
311a76f4211Snicm 	 */
3122077c06bSnicm 	if (!clients_calculate_size(type, 0, c, s, w,
313a76f4211Snicm 	    default_window_size_skip_client, sx, sy, xpixel, ypixel)) {
3147b470e93Snicm 		value = options_get_string(s->options, "default-size");
3157b470e93Snicm 		if (sscanf(value, "%ux%u", sx, sy) != 2) {
3167b470e93Snicm 			*sx = 80;
3177b470e93Snicm 			*sy = 24;
3187b470e93Snicm 		}
3192077c06bSnicm 		log_debug("%s: using %ux%u from default-size", __func__, *sx,
3202077c06bSnicm 		    *sy);
321a76f4211Snicm 	}
3227b470e93Snicm 
3237b470e93Snicm done:
324a76f4211Snicm 	/* Make sure the limits are enforced. */
3257b470e93Snicm 	if (*sx < WINDOW_MINIMUM)
3267b470e93Snicm 		*sx = WINDOW_MINIMUM;
3277b470e93Snicm 	if (*sx > WINDOW_MAXIMUM)
3287b470e93Snicm 		*sx = WINDOW_MAXIMUM;
3297b470e93Snicm 	if (*sy < WINDOW_MINIMUM)
3307b470e93Snicm 		*sy = WINDOW_MINIMUM;
3317b470e93Snicm 	if (*sy > WINDOW_MAXIMUM)
3327b470e93Snicm 		*sy = WINDOW_MAXIMUM;
3332077c06bSnicm 	log_debug("%s: resulting size is %ux%u", __func__, *sx, *sy);
3347b470e93Snicm }
335311827fbSnicm 
336a76f4211Snicm static int
337a76f4211Snicm recalculate_size_skip_client(struct client *loop, __unused int type,
338a76f4211Snicm     int current, __unused struct session *s, struct window *w)
339a76f4211Snicm {
340a76f4211Snicm 	/*
341a76f4211Snicm 	 * If the current flag is set, then skip any client where this window
342a76f4211Snicm 	 * is not the current window - this is used for aggressive-resize.
343a76f4211Snicm 	 * Otherwise skip any session that doesn't contain the window.
344a76f4211Snicm 	 */
3458c20af2cSnicm 	if (loop->session->curw == NULL)
3468c20af2cSnicm 		return (1);
347a76f4211Snicm 	if (current)
348a76f4211Snicm 		return (loop->session->curw->window != w);
349a76f4211Snicm 	return (session_has(loop->session, w) == 0);
350a76f4211Snicm }
351a76f4211Snicm 
352311827fbSnicm void
3533e796c5aSnicm recalculate_size(struct window *w, int now)
354f4f81a00Snicm {
355a76f4211Snicm 	u_int	sx, sy, xpixel = 0, ypixel = 0;
356a76f4211Snicm 	int	type, current, changed;
357f4f81a00Snicm 
358a76f4211Snicm 	/*
359a76f4211Snicm 	 * Do not attempt to resize windows which have no pane, they must be on
360a76f4211Snicm 	 * the way to destruction.
361a76f4211Snicm 	 */
362f4f81a00Snicm 	if (w->active == NULL)
363f4f81a00Snicm 		return;
3648ef4e054Snicm 	log_debug("%s: @%u is %ux%u", __func__, w->id, w->sx, w->sy);
365f4f81a00Snicm 
366a76f4211Snicm 	/*
367a76f4211Snicm 	 * Type is manual, smallest, largest, latest. Current is the
368a76f4211Snicm 	 * aggressive-resize option (do not resize based on clients where the
369a76f4211Snicm 	 * window is not the current window).
370a76f4211Snicm 	 */
371f4f81a00Snicm 	type = options_get_number(w->options, "window-size");
372f4f81a00Snicm 	current = options_get_number(w->options, "aggressive-resize");
373f4f81a00Snicm 
374a76f4211Snicm 	/* Look for a suitable client and get the new size. */
3752077c06bSnicm 	changed = clients_calculate_size(type, current, NULL, NULL, w,
376a76f4211Snicm 	    recalculate_size_skip_client, &sx, &sy, &xpixel, &ypixel);
377f4f81a00Snicm 
378a76f4211Snicm 	/*
379a76f4211Snicm 	 * Make sure the size has actually changed. If the window has already
380a76f4211Snicm 	 * got a resize scheduled, then use the new size; otherwise the old.
381a76f4211Snicm 	 */
382061703b1Snicm 	if (w->flags & WINDOW_RESIZE) {
3833e796c5aSnicm 		if (!now && changed && w->new_sx == sx && w->new_sy == sy)
384061703b1Snicm 			changed = 0;
385061703b1Snicm 	} else {
3863e796c5aSnicm 		if (!now && changed && w->sx == sx && w->sy == sy)
387f4f81a00Snicm 			changed = 0;
388061703b1Snicm 	}
389f4f81a00Snicm 
390a76f4211Snicm 	/*
391a76f4211Snicm 	 * If the size hasn't changed, update the window offset but not the
392a76f4211Snicm 	 * size.
393a76f4211Snicm 	 */
394f4f81a00Snicm 	if (!changed) {
3958ef4e054Snicm 		log_debug("%s: @%u no size change", __func__, w->id);
396f4f81a00Snicm 		tty_update_window_offset(w);
397f4f81a00Snicm 		return;
398f4f81a00Snicm 	}
399a76f4211Snicm 
400a76f4211Snicm 	/*
401a76f4211Snicm 	 * If the now flag is set or if the window is sized manually, change
402a76f4211Snicm 	 * the size immediately. Otherwise set the flag and it will be done
403a76f4211Snicm 	 * later.
404a76f4211Snicm 	 */
4058ef4e054Snicm 	log_debug("%s: @%u new size %ux%u", __func__, w->id, sx, sy);
4063e796c5aSnicm 	if (now || type == WINDOW_SIZE_MANUAL)
4074a8b0ea5Snicm 		resize_window(w, sx, sy, xpixel, ypixel);
408061703b1Snicm 	else {
409061703b1Snicm 		w->new_sx = sx;
410061703b1Snicm 		w->new_sy = sy;
411061703b1Snicm 		w->new_xpixel = xpixel;
412061703b1Snicm 		w->new_ypixel = ypixel;
413061703b1Snicm 
414061703b1Snicm 		w->flags |= WINDOW_RESIZE;
415061703b1Snicm 		tty_update_window_offset(w);
416061703b1Snicm 	}
417f4f81a00Snicm }
418f4f81a00Snicm 
419f4f81a00Snicm void
420311827fbSnicm recalculate_sizes(void)
421311827fbSnicm {
4223e796c5aSnicm 	recalculate_sizes_now(0);
4233e796c5aSnicm }
4243e796c5aSnicm 
4253e796c5aSnicm void
4263e796c5aSnicm recalculate_sizes_now(int now)
4273e796c5aSnicm {
428311827fbSnicm 	struct session	*s;
429311827fbSnicm 	struct client	*c;
430311827fbSnicm 	struct window	*w;
431311827fbSnicm 
4327b470e93Snicm 	/*
4337b470e93Snicm 	 * Clear attached count and update saved status line information for
4347b470e93Snicm 	 * each session.
4357b470e93Snicm 	 */
43659996dc3Snicm 	RB_FOREACH(s, sessions, &sessions) {
437dfbd3895Snicm 		s->attached = 0;
438b2140406Snicm 		status_update_cache(s);
439311827fbSnicm 	}
440311827fbSnicm 
4417b470e93Snicm 	/*
4427b470e93Snicm 	 * Increment attached count and check the status line size for each
4437b470e93Snicm 	 * client.
4447b470e93Snicm 	 */
4457b470e93Snicm 	TAILQ_FOREACH(c, &clients, entry) {
446b5f8268cSnicm 		s = c->session;
447b5f8268cSnicm 		if (s != NULL && !(c->flags & CLIENT_UNATTACHEDFLAGS))
448b5f8268cSnicm 			s->attached++;
449db993454Snicm 		if (ignore_client_size(c))
4507b470e93Snicm 			continue;
451f415a97bSnicm 		if (c->tty.sy <= s->statuslines || (c->flags & CLIENT_CONTROL))
4527b470e93Snicm 			c->flags |= CLIENT_STATUSOFF;
4537b470e93Snicm 		else
4547b470e93Snicm 			c->flags &= ~CLIENT_STATUSOFF;
4557b470e93Snicm 	}
4567b470e93Snicm 
4577b470e93Snicm 	/* Walk each window and adjust the size. */
458f4f81a00Snicm 	RB_FOREACH(w, windows, &windows)
4593e796c5aSnicm 		recalculate_size(w, now);
460311827fbSnicm }
461