1 /* $OpenBSD: wwscroll.c,v 1.6 2003/07/10 00:06:52 david Exp $ */ 2 /* $NetBSD: wwscroll.c,v 1.3 1995/09/28 10:35:53 tls Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Edward Wang at The University of California, Berkeley. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)wwscroll.c 8.1 (Berkeley) 6/6/93"; 39 #else 40 static char rcsid[] = "$OpenBSD: wwscroll.c,v 1.6 2003/07/10 00:06:52 david Exp $"; 41 #endif 42 #endif /* not lint */ 43 44 #include <stdlib.h> 45 46 #include "ww.h" 47 #include "tt.h" 48 49 wwscroll(w, n) 50 struct ww *w; 51 int n; 52 { 53 int dir; 54 int top; 55 56 if (n == 0) 57 return; 58 dir = n < 0 ? -1 : 1; 59 top = w->ww_b.t - n; 60 if (top > w->ww_w.t) 61 top = w->ww_w.t; 62 else if (top + w->ww_b.nr < w->ww_w.b) 63 top = w->ww_w.b - w->ww_b.nr; 64 n = abs(top - w->ww_b.t); 65 if (n < w->ww_i.nr) { 66 while (--n >= 0) { 67 (void) wwscroll1(w, w->ww_i.t, w->ww_i.b, dir, 0); 68 w->ww_buf += dir; 69 w->ww_b.t -= dir; 70 w->ww_b.b -= dir; 71 } 72 } else { 73 w->ww_buf -= top - w->ww_b.t; 74 w->ww_b.t = top; 75 w->ww_b.b = top + w->ww_b.nr; 76 wwredrawwin(w); 77 } 78 } 79 80 /* 81 * Scroll one line, between 'row1' and 'row2', in direction 'dir'. 82 * Don't adjust ww_scroll. 83 * And don't redraw 'leaveit' lines. 84 */ 85 wwscroll1(w, row1, row2, dir, leaveit) 86 struct ww *w; 87 int row1, row2, dir; 88 int leaveit; 89 { 90 int i; 91 int row1x, row2x; 92 int nvis; 93 int nvismax; 94 int scrolled = 0; 95 96 /* 97 * See how many lines on the screen are affected. 98 * And calculate row1x, row2x, and left at the same time. 99 */ 100 for (i = row1; i < row2 && w->ww_nvis[i] == 0; i++) 101 ; 102 if (i >= row2) /* can't do any fancy stuff */ 103 goto out; 104 row1x = i; 105 for (i = row2 - 1; i >= row1 && w->ww_nvis[i] == 0; i--) 106 ; 107 if (i <= row1x) 108 goto out; /* just one line is easy */ 109 row2x = i + 1; 110 111 /* 112 * See how much of this window is visible. 113 */ 114 nvismax = wwncol * (row2x - row1x); 115 nvis = 0; 116 for (i = row1x; i < row2x; i++) 117 nvis += w->ww_nvis[i]; 118 119 /* 120 * If it's a good idea to scroll and the terminal can, then do it. 121 */ 122 if (nvis < nvismax / 2) 123 goto no_scroll; /* not worth it */ 124 if ((dir > 0 ? tt.tt_scroll_down == 0 : tt.tt_scroll_up == 0) || 125 (tt.tt_scroll_top != row1x || tt.tt_scroll_bot != row2x - 1) && 126 tt.tt_setscroll == 0) 127 if (tt.tt_delline == 0 || tt.tt_insline == 0) 128 goto no_scroll; 129 xxscroll(dir, row1x, row2x); 130 scrolled = 1; 131 /* 132 * Fix up the old screen. 133 */ 134 { 135 union ww_char *tmp; 136 union ww_char **cpp, **cqq; 137 138 if (dir > 0) { 139 cpp = &wwos[row1x]; 140 cqq = cpp + 1; 141 tmp = *cpp; 142 for (i = row2x - row1x; --i > 0;) 143 *cpp++ = *cqq++; 144 *cpp = tmp; 145 } else { 146 cpp = &wwos[row2x]; 147 cqq = cpp - 1; 148 tmp = *cqq; 149 for (i = row2x - row1x; --i > 0;) 150 *--cpp = *--cqq; 151 *cqq = tmp; 152 } 153 for (i = wwncol; --i >= 0;) 154 tmp++->c_w = ' '; 155 } 156 157 no_scroll: 158 /* 159 * Fix the new screen. 160 */ 161 if (nvis == nvismax) { 162 /* 163 * Can shift whole lines. 164 */ 165 if (dir > 0) { 166 { 167 union ww_char *tmp; 168 union ww_char **cpp, **cqq; 169 170 cpp = &wwns[row1x]; 171 cqq = cpp + 1; 172 tmp = *cpp; 173 for (i = row2x - row1x; --i > 0;) 174 *cpp++ = *cqq++; 175 *cpp = tmp; 176 } 177 if (scrolled) { 178 char *p, *q; 179 180 p = &wwtouched[row1x]; 181 q = p + 1; 182 for (i = row2x - row1x; --i > 0;) 183 *p++ = *q++; 184 *p |= WWU_TOUCHED; 185 } else { 186 char *p; 187 188 p = &wwtouched[row1x]; 189 for (i = row2x - row1x; --i >= 0;) 190 *p++ |= WWU_TOUCHED; 191 } 192 wwredrawwin1(w, row1, row1x, dir); 193 wwredrawwin1(w, row2x - 1, row2 - leaveit, dir); 194 } else { 195 { 196 union ww_char *tmp; 197 union ww_char **cpp, **cqq; 198 199 cpp = &wwns[row2x]; 200 cqq = cpp - 1; 201 tmp = *cqq; 202 for (i = row2x - row1x; --i > 0;) 203 *--cpp = *--cqq; 204 *cqq = tmp; 205 } 206 if (scrolled) { 207 char *p, *q; 208 209 p = &wwtouched[row2x]; 210 q = p - 1; 211 for (i = row2x - row1x; --i > 0;) 212 *--p = *--q; 213 *q |= WWU_TOUCHED; 214 } else { 215 char *p; 216 217 p = &wwtouched[row1x]; 218 for (i = row2x - row1x; --i >= 0;) 219 *p++ |= WWU_TOUCHED; 220 } 221 wwredrawwin1(w, row1 + leaveit, row1x + 1, dir); 222 wwredrawwin1(w, row2x, row2, dir); 223 } 224 } else { 225 if (scrolled) { 226 char *p; 227 228 p = &wwtouched[row1x]; 229 for (i = row2x - row1x; --i >= 0;) 230 *p++ |= WWU_TOUCHED; 231 } 232 out: 233 if (dir > 0) 234 wwredrawwin1(w, row1, row2 - leaveit, dir); 235 else 236 wwredrawwin1(w, row1 + leaveit, row2, dir); 237 } 238 return scrolled; 239 } 240