1 /* $OpenBSD: hardscroll.c,v 1.6 2003/03/18 16:55:54 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /****************************************************************************** 37 38 NAME 39 hardscroll.c -- hardware-scrolling optimization for ncurses 40 41 SYNOPSIS 42 void _nc_scroll_optimize(void) 43 44 DESCRIPTION 45 OVERVIEW 46 47 This algorithm for computes optimum hardware scrolling to transform an 48 old screen (curscr) into a new screen (newscr) via vertical line moves. 49 50 Because the screen has a `grain' (there are insert/delete/scroll line 51 operations but no insert/delete/scroll column operations), it is efficient 52 break the update algorithm into two pieces: a first stage that does only line 53 moves, optimizing the end product of user-invoked insertions, deletions, and 54 scrolls; and a second phase (corresponding to the present doupdate code in 55 ncurses) that does only line transformations. 56 57 The common case we want hardware scrolling for is to handle line insertions 58 and deletions in screen-oriented text-editors. This two-stage approach will 59 accomplish that at a low computation and code-size cost. 60 61 LINE-MOVE COMPUTATION 62 63 Now, to a discussion of the line-move computation. 64 65 For expository purposes, consider the screen lines to be represented by 66 integers 0..23 (with the understanding that the value of 23 may vary). 67 Let a new line introduced by insertion, scrolling, or at the bottom of 68 the screen following a line delete be given the index -1. 69 70 Assume that the real screen starts with lines 0..23. Now, we have 71 the following possible line-oriented operations on the screen: 72 73 Insertion: inserts a line at a given screen row, forcing all lines below 74 to scroll forward. The last screen line is lost. For example, an insertion 75 at line 5 would produce: 0..4 -1 5..23. 76 77 Deletion: deletes a line at a given screen row, forcing all lines below 78 to scroll forward. The last screen line is made new. For example, a deletion 79 at line 7 would produce: 0..6 8..23 -1. 80 81 Scroll up: move a range of lines up 1. The bottom line of the range 82 becomes new. For example, scrolling up the region from 9 to 14 will 83 produce 0..8 10..14 -1 15..23. 84 85 Scroll down: move a range of lines down 1. The top line of the range 86 becomes new. For example, scrolling down the region from 12 to 16 will produce 87 0..11 -1 12..15 17..23. 88 89 Now, an obvious property of all these operations is that they preserve the 90 order of old lines, though not their position in the sequence. 91 92 The key trick of this algorithm is that the original line indices described 93 above are actually maintained as _line[].oldindex fields in the window 94 structure, and stick to each line through scroll and insert/delete operations. 95 96 Thus, it is possible at update time to look at the oldnum fields and compute 97 an optimal set of il/dl/scroll operations that will take the real screen 98 lines to the virtual screen lines. Once these vertical moves have been done, 99 we can hand off to the second stage of the update algorithm, which does line 100 transformations. 101 102 Note that the move computation does not need to have the full generality 103 of a diff algorithm (which it superficially resembles) because lines cannot 104 be moved out of order. 105 106 THE ALGORITHM 107 108 The scrolling is done in two passes. The first pass is from top to bottom 109 scroling hunks UP. The second one is from bottom to top scrolling hunks DOWN. 110 Obviously enough, no lines to be scrolled will be destroyed. (lav) 111 112 HOW TO TEST THIS: 113 114 Use the following production: 115 116 hardscroll: hardscroll.c 117 $(CC) -g -DSCROLLDEBUG hardscroll.c -o hardscroll 118 119 Then just type scramble vectors and watch. The following test loads are 120 a representative sample of cases: 121 122 ----------------------------- CUT HERE ------------------------------------ 123 # No lines moved 124 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 125 # 126 # A scroll up 127 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 -1 128 # 129 # A scroll down 130 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 131 # 132 # An insertion (after line 12) 133 0 1 2 3 4 5 6 7 8 9 10 11 12 -1 13 14 15 16 17 18 19 20 21 22 134 # 135 # A simple deletion (line 10) 136 0 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 21 22 23 -1 137 # 138 # A more complex case 139 -1 -1 -1 -1 -1 3 4 5 6 7 -1 -1 8 9 10 11 12 13 14 15 16 17 -1 -1 140 ----------------------------- CUT HERE ------------------------------------ 141 142 AUTHOR 143 Eric S. Raymond <esr@snark.thyrsus.com>, November 1994 144 New algorithm by Alexander V. Lukyanov <lav@yars.free.net>, Aug 1997 145 146 *****************************************************************************/ 147 148 #include <curses.priv.h> 149 150 MODULE_ID("$From: hardscroll.c,v 1.36 2001/01/14 00:17:28 tom Exp $") 151 152 #if defined(SCROLLDEBUG) || defined(HASHDEBUG) 153 154 # undef screen_lines 155 # define screen_lines MAXLINES 156 NCURSES_EXPORT_VAR(int) 157 oldnums[MAXLINES]; 158 # define OLDNUM(n) oldnums[n] 159 # define _tracef printf 160 # undef TR 161 # define TR(n, a) if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); } 162 163 #else /* no debug */ 164 165 /* OLDNUM(n) indicates which line will be shifted to the position n. 166 if OLDNUM(n) == _NEWINDEX, then the line n in new, not shifted from 167 somewhere. */ 168 NCURSES_EXPORT_VAR(int *) 169 _nc_oldnums = 0; 170 171 # if USE_HASHMAP 172 static int oldnums_allocated = 0; 173 # define oldnums _nc_oldnums 174 # define OLDNUM(n) oldnums[n] 175 # else /* !USE_HASHMAP */ 176 # define OLDNUM(n) newscr->_line[n].oldindex 177 # endif /* !USE_HASHMAP */ 178 179 #endif /* defined(SCROLLDEBUG) || defined(HASHDEBUG) */ 180 181 NCURSES_EXPORT(void) 182 _nc_scroll_optimize(void) 183 /* scroll optimization to transform curscr to newscr */ 184 { 185 int i; 186 int start, end, shift; 187 188 TR(TRACE_ICALLS, ("_nc_scroll_optimize() begins")); 189 190 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG) 191 #if USE_HASHMAP 192 /* get enough storage */ 193 if (oldnums_allocated < screen_lines) { 194 int *new_oldnums = typeRealloc(int, screen_lines, oldnums); 195 if (!new_oldnums) 196 return; 197 oldnums = new_oldnums; 198 oldnums_allocated = screen_lines; 199 } 200 /* calculate the indices */ 201 _nc_hash_map(); 202 #endif 203 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */ 204 205 #ifdef TRACE 206 if (_nc_tracing & (TRACE_UPDATE | TRACE_MOVE)) 207 _nc_linedump(); 208 #endif /* TRACE */ 209 210 /* pass 1 - from top to bottom scrolling up */ 211 for (i = 0; i < screen_lines;) { 212 while (i < screen_lines && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) <= i)) 213 i++; 214 if (i >= screen_lines) 215 break; 216 217 shift = OLDNUM(i) - i; /* shift > 0 */ 218 start = i; 219 220 i++; 221 while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i 222 == shift) 223 i++; 224 end = i - 1 + shift; 225 226 TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift)); 227 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG) 228 if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR) { 229 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll")); 230 continue; 231 } 232 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */ 233 } 234 235 /* pass 2 - from bottom to top scrolling down */ 236 for (i = screen_lines - 1; i >= 0;) { 237 while (i >= 0 && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) >= i)) 238 i--; 239 if (i < 0) 240 break; 241 242 shift = OLDNUM(i) - i; /* shift < 0 */ 243 end = i; 244 245 i--; 246 while (i >= 0 && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift) 247 i--; 248 start = i + 1 - (-shift); 249 250 TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift)); 251 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG) 252 if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR) { 253 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll")); 254 continue; 255 } 256 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */ 257 } 258 } 259 260 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG) 261 NCURSES_EXPORT(void) 262 _nc_linedump(void) 263 /* dump the state of the real and virtual oldnum fields */ 264 { 265 static size_t have; 266 static char *buf; 267 268 int n; 269 size_t want = (screen_lines + 1) * 4; 270 271 if (have < want) 272 buf = typeMalloc(char, have = want); 273 274 (void) strlcpy(buf, "virt", have); 275 for (n = 0; n < screen_lines; n++) 276 (void) snprintf(buf + strlen(buf), have - strlen(buf), " %02d", 277 OLDNUM(n)); 278 TR(TRACE_UPDATE | TRACE_MOVE, (buf)); 279 #if NO_LEAKS 280 free(buf); 281 have = 0; 282 #endif 283 } 284 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */ 285 286 #ifdef SCROLLDEBUG 287 288 int 289 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) 290 { 291 char line[BUFSIZ], *st, *last; 292 293 #ifdef TRACE 294 _nc_tracing = TRACE_MOVE; 295 #endif 296 for (;;) { 297 int n; 298 299 for (n = 0; n < screen_lines; n++) 300 oldnums[n] = _NEWINDEX; 301 302 /* grab the test vector */ 303 if (fgets(line, sizeof(line), stdin) == (char *) NULL) 304 exit(EXIT_SUCCESS); 305 306 /* parse it */ 307 n = 0; 308 if (line[0] == '#') { 309 (void) fputs(line, stderr); 310 continue; 311 } 312 st = strtok_r(line, " ", &last); 313 do { 314 oldnums[n++] = atoi(st); 315 } while 316 ((st = strtok_r((char *) NULL, " ", &last)) != 0); 317 318 /* display it */ 319 (void) fputs("Initial input:\n", stderr); 320 _nc_linedump(); 321 322 _nc_scroll_optimize(); 323 } 324 } 325 326 #endif /* SCROLLDEBUG */ 327 328 /* hardscroll.c ends here */ 329