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