xref: /minix/external/bsd/tmux/dist/resize.c (revision 9f988b79)
1 /* $Id: resize.c,v 1.1.1.2 2011/08/17 18:40:05 jmmv Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <string.h>
22 
23 #include "tmux.h"
24 
25 /*
26  * Recalculate window and session sizes.
27  *
28  * Every session has the size of the smallest client it is attached to and
29  * every window the size of the smallest session it is attached to.
30  *
31  * So, when a client is resized or a session attached to or detached from a
32  * client, the window sizes must be recalculated. For each session, find the
33  * smallest client it is attached to, and resize it to that size. Then for
34  * every window, find the smallest session it is attached to, resize it to that
35  * size and clear and redraw every client with it as the current window.
36  *
37  * This is quite inefficient - better/additional data structures are needed
38  * to make it better.
39  *
40  * As a side effect, this function updates the SESSION_UNATTACHED flag. This
41  * flag is necessary to make sure unattached sessions do not limit the size of
42  * windows that are attached both to them and to other (attached) sessions.
43  */
44 
45 void
46 recalculate_sizes(void)
47 {
48 	struct session		*s;
49 	struct client		*c;
50 	struct window		*w;
51 	struct window_pane	*wp;
52 	u_int		 	 i, j, ssx, ssy, has, limit;
53 	int		 	 flag;
54 
55 	RB_FOREACH(s, sessions, &sessions) {
56 		ssx = ssy = UINT_MAX;
57 		for (j = 0; j < ARRAY_LENGTH(&clients); j++) {
58 			c = ARRAY_ITEM(&clients, j);
59 			if (c == NULL || c->flags & CLIENT_SUSPENDED)
60 				continue;
61 			if (c->session == s) {
62 				if (c->tty.sx < ssx)
63 					ssx = c->tty.sx;
64 				if (c->tty.sy < ssy)
65 					ssy = c->tty.sy;
66 			}
67 		}
68 		if (ssx == UINT_MAX || ssy == UINT_MAX) {
69 			s->flags |= SESSION_UNATTACHED;
70 			continue;
71 		}
72 		s->flags &= ~SESSION_UNATTACHED;
73 
74 		if (options_get_number(&s->options, "status")) {
75 			if (ssy == 0)
76 				ssy = 1;
77 			else
78 				ssy--;
79 		}
80 		if (s->sx == ssx && s->sy == ssy)
81 			continue;
82 
83 		log_debug(
84 		    "session size %u,%u (was %u,%u)", ssx, ssy, s->sx, s->sy);
85 
86 		s->sx = ssx;
87 		s->sy = ssy;
88 	}
89 
90 	for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
91 		w = ARRAY_ITEM(&windows, i);
92 		if (w == NULL)
93 			continue;
94 		flag = options_get_number(&w->options, "aggressive-resize");
95 
96 		ssx = ssy = UINT_MAX;
97 		RB_FOREACH(s, sessions, &sessions) {
98 			if (s->flags & SESSION_UNATTACHED)
99 				continue;
100 			if (flag)
101 				has = s->curw->window == w;
102 			else
103 				has = session_has(s, w) != NULL;
104 			if (has) {
105 				if (s->sx < ssx)
106 					ssx = s->sx;
107 				if (s->sy < ssy)
108 					ssy = s->sy;
109 			}
110 		}
111 		if (ssx == UINT_MAX || ssy == UINT_MAX)
112 			continue;
113 
114 		limit = options_get_number(&w->options, "force-width");
115 		if (limit != 0 && ssx > limit)
116 			ssx = limit;
117 		limit = options_get_number(&w->options, "force-height");
118 		if (limit != 0 && ssy > limit)
119 			ssy = limit;
120 
121 		if (w->sx == ssx && w->sy == ssy)
122 			continue;
123 
124 		log_debug(
125 		    "window size %u,%u (was %u,%u)", ssx, ssy, w->sx, w->sy);
126 
127 		layout_resize(w, ssx, ssy);
128 		window_resize(w, ssx, ssy);
129 
130 		/*
131 		 * If the current pane is now not visible, move to the next
132 		 * that is.
133 		 */
134 		wp = w->active;
135 		while (!window_pane_visible(w->active)) {
136 			w->active = TAILQ_PREV(w->active, window_panes, entry);
137 			if (w->active == NULL)
138 				w->active = TAILQ_LAST(&w->panes, window_panes);
139 			if (w->active == wp)
140 			       break;
141 		}
142 
143 		server_redraw_window(w);
144 	}
145 }
146