1 /* $OpenBSD: resize.c,v 1.25 2017/10/16 19:30:53 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 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 ssx, ssy, has, limit, lines; 53 int flag, is_zoomed, forced; 54 55 RB_FOREACH(s, sessions, &sessions) { 56 lines = status_line_size(s); 57 58 s->attached = 0; 59 ssx = ssy = UINT_MAX; 60 TAILQ_FOREACH(c, &clients, entry) { 61 if (c->flags & CLIENT_SUSPENDED) 62 continue; 63 if ((c->flags & (CLIENT_CONTROL|CLIENT_SIZECHANGED)) == 64 CLIENT_CONTROL) 65 continue; 66 if (c->session == s) { 67 if (c->tty.sx < ssx) 68 ssx = c->tty.sx; 69 c->flags &= ~CLIENT_STATUSOFF; 70 if (lines != 0 && lines + PANE_MINIMUM > c->tty.sy) 71 c->flags |= CLIENT_STATUSOFF; 72 if ((~c->flags & CLIENT_STATUSOFF) && 73 !(c->flags & CLIENT_CONTROL) && 74 c->tty.sy > lines && 75 c->tty.sy - lines < ssy) 76 ssy = c->tty.sy - lines; 77 else if (c->tty.sy < ssy) 78 ssy = c->tty.sy; 79 s->attached++; 80 } 81 } 82 if (ssx == UINT_MAX || ssy == UINT_MAX) { 83 s->flags |= SESSION_UNATTACHED; 84 continue; 85 } 86 s->flags &= ~SESSION_UNATTACHED; 87 88 if (lines != 0 && ssy == 0) 89 ssy = lines; 90 91 if (s->sx == ssx && s->sy == ssy) 92 continue; 93 94 log_debug("session $%u size %u,%u (was %u,%u)", s->id, ssx, ssy, 95 s->sx, s->sy); 96 97 s->sx = ssx; 98 s->sy = ssy; 99 100 status_update_saved(s); 101 } 102 103 RB_FOREACH(w, windows, &windows) { 104 if (w->active == NULL) 105 continue; 106 flag = options_get_number(w->options, "aggressive-resize"); 107 108 ssx = ssy = UINT_MAX; 109 RB_FOREACH(s, sessions, &sessions) { 110 if (s->flags & SESSION_UNATTACHED) 111 continue; 112 if (flag) 113 has = s->curw->window == w; 114 else 115 has = session_has(s, w); 116 if (has) { 117 if (s->sx < ssx) 118 ssx = s->sx; 119 if (s->sy < ssy) 120 ssy = s->sy; 121 } 122 } 123 if (ssx == UINT_MAX || ssy == UINT_MAX) 124 continue; 125 126 forced = 0; 127 limit = options_get_number(w->options, "force-width"); 128 if (limit >= PANE_MINIMUM && ssx > limit) { 129 ssx = limit; 130 forced |= WINDOW_FORCEWIDTH; 131 } 132 limit = options_get_number(w->options, "force-height"); 133 if (limit >= PANE_MINIMUM && ssy > limit) { 134 ssy = limit; 135 forced |= WINDOW_FORCEHEIGHT; 136 } 137 138 if (w->sx == ssx && w->sy == ssy) 139 continue; 140 log_debug("window @%u size %u,%u (was %u,%u)", w->id, ssx, ssy, 141 w->sx, w->sy); 142 143 w->flags &= ~(WINDOW_FORCEWIDTH|WINDOW_FORCEHEIGHT); 144 w->flags |= forced; 145 146 is_zoomed = w->flags & WINDOW_ZOOMED; 147 if (is_zoomed) 148 window_unzoom(w); 149 layout_resize(w, ssx, ssy); 150 window_resize(w, ssx, ssy); 151 if (is_zoomed && window_pane_visible(w->active)) 152 window_zoom(w->active); 153 154 /* 155 * If the current pane is now not visible, move to the next 156 * that is. 157 */ 158 wp = w->active; 159 while (!window_pane_visible(w->active)) { 160 w->active = TAILQ_PREV(w->active, window_panes, entry); 161 if (w->active == NULL) 162 w->active = TAILQ_LAST(&w->panes, window_panes); 163 if (w->active == wp) 164 break; 165 } 166 if (w->active == w->last) 167 w->last = NULL; 168 169 server_redraw_window(w); 170 notify_window("window-layout-changed", w); 171 } 172 } 173