Annotation of src/usr.bin/tmux/resize.c, Revision 1.16
1.16 ! nicm 1: /* $OpenBSD: resize.c,v 1.15 2015/04/22 15:30:11 nicm Exp $ */
1.1 nicm 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;
1.2 nicm 51: struct window_pane *wp;
1.15 nicm 52: u_int i, ssx, ssy, has, limit;
1.14 nicm 53: int flag, has_status, is_zoomed, forced;
1.1 nicm 54:
1.7 nicm 55: RB_FOREACH(s, sessions, &sessions) {
1.9 nicm 56: has_status = options_get_number(&s->options, "status");
57:
1.12 nicm 58: s->attached = 0;
1.1 nicm 59: ssx = ssy = UINT_MAX;
1.15 nicm 60: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
61: c = ARRAY_ITEM(&clients, i);
1.4 nicm 62: if (c == NULL || c->flags & CLIENT_SUSPENDED)
1.1 nicm 63: continue;
64: if (c->session == s) {
65: if (c->tty.sx < ssx)
66: ssx = c->tty.sx;
1.9 nicm 67: if (has_status &&
68: !(c->flags & CLIENT_CONTROL) &&
69: c->tty.sy > 1 && c->tty.sy - 1 < ssy)
70: ssy = c->tty.sy - 1;
71: else if (c->tty.sy < ssy)
1.1 nicm 72: ssy = c->tty.sy;
1.12 nicm 73: s->attached++;
1.1 nicm 74: }
75: }
76: if (ssx == UINT_MAX || ssy == UINT_MAX) {
77: s->flags |= SESSION_UNATTACHED;
78: continue;
79: }
80: s->flags &= ~SESSION_UNATTACHED;
81:
1.9 nicm 82: if (has_status && ssy == 0)
83: ssy = 1;
84:
1.1 nicm 85: if (s->sx == ssx && s->sy == ssy)
86: continue;
87:
1.9 nicm 88: log_debug("session size %u,%u (was %u,%u)", ssx, ssy, s->sx,
89: s->sy);
1.1 nicm 90:
91: s->sx = ssx;
92: s->sy = ssy;
93: }
94:
1.15 nicm 95: RB_FOREACH(w, windows, &windows) {
96: if (w->active == NULL)
1.1 nicm 97: continue;
98: flag = options_get_number(&w->options, "aggressive-resize");
99:
100: ssx = ssy = UINT_MAX;
1.7 nicm 101: RB_FOREACH(s, sessions, &sessions) {
102: if (s->flags & SESSION_UNATTACHED)
1.1 nicm 103: continue;
104: if (flag)
105: has = s->curw->window == w;
106: else
1.16 ! nicm 107: has = session_has(s, w);
1.1 nicm 108: if (has) {
109: if (s->sx < ssx)
110: ssx = s->sx;
111: if (s->sy < ssy)
112: ssy = s->sy;
113: }
114: }
1.6 nicm 115: if (ssx == UINT_MAX || ssy == UINT_MAX)
1.1 nicm 116: continue;
117:
1.14 nicm 118: forced = 0;
1.1 nicm 119: limit = options_get_number(&w->options, "force-width");
1.14 nicm 120: if (limit >= PANE_MINIMUM && ssx > limit) {
1.1 nicm 121: ssx = limit;
1.14 nicm 122: forced |= WINDOW_FORCEWIDTH;
123: }
1.1 nicm 124: limit = options_get_number(&w->options, "force-height");
1.14 nicm 125: if (limit >= PANE_MINIMUM && ssy > limit) {
1.1 nicm 126: ssy = limit;
1.14 nicm 127: forced |= WINDOW_FORCEHEIGHT;
128: }
1.1 nicm 129:
130: if (w->sx == ssx && w->sy == ssy)
131: continue;
1.9 nicm 132: log_debug("window size %u,%u (was %u,%u)", ssx, ssy, w->sx,
133: w->sy);
1.14 nicm 134:
135: w->flags &= ~(WINDOW_FORCEWIDTH|WINDOW_FORCEHEIGHT);
136: w->flags |= forced;
1.1 nicm 137:
1.10 nicm 138: is_zoomed = w->flags & WINDOW_ZOOMED;
139: if (is_zoomed)
140: window_unzoom(w);
1.3 nicm 141: layout_resize(w, ssx, ssy);
1.1 nicm 142: window_resize(w, ssx, ssy);
1.10 nicm 143: if (is_zoomed && window_pane_visible(w->active))
144: window_zoom(w->active);
1.2 nicm 145:
146: /*
147: * If the current pane is now not visible, move to the next
148: * that is.
149: */
150: wp = w->active;
151: while (!window_pane_visible(w->active)) {
152: w->active = TAILQ_PREV(w->active, window_panes, entry);
153: if (w->active == NULL)
154: w->active = TAILQ_LAST(&w->panes, window_panes);
155: if (w->active == wp)
156: break;
157: }
158:
1.1 nicm 159: server_redraw_window(w);
1.8 nicm 160: notify_window_layout_changed(w);
1.1 nicm 161: }
162: }