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