1 /****************************************************************************
2 * Copyright 2020,2023 Thomas E. Dickey *
3 * Copyright 1998-2015,2016 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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 ****************************************************************************/
37 /******************************************************************************
40 hardscroll.c -- hardware-scrolling optimization for ncurses
43 void _nc_scroll_optimize(void)
48 This algorithm for computes optimum hardware scrolling to transform an
49 old screen (curscr) into a new screen (newscr) via vertical line moves.
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.
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.
64 Now, to a discussion of the line-move computation.
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.
71 Assume that the real screen starts with lines 0..23. Now, we have
72 the following possible line-oriented operations on the screen:
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.
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.
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.
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.
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.
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.
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
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.
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)
115 Use the following production:
117 hardscroll: hardscroll.c
118 $(CC) -g -DSCROLLDEBUG hardscroll.c -o hardscroll
120 Then just type scramble vectors and watch. The following test loads are
121 a representative sample of cases:
123 ----------------------------- CUT HERE ------------------------------------
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
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
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
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
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
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 ------------------------------------
144 Eric S. Raymond <esr@snark.thyrsus.com>, November 1994
145 New algorithm by Alexander V. Lukyanov <lav@yars.free.net>, Aug 1997
147 *****************************************************************************/
149 #include <curses.priv.h>
151 MODULE_ID("$Id: hardscroll.c,v 1.58 2023/09/09 16:04:08 Nicholas.Marriott Exp $")
153 #if defined(SCROLLDEBUG) || defined(HASHDEBUG)
156 # define screen_lines(sp) MAXLINES
157 NCURSES_EXPORT_VAR (int)
159 # define OLDNUM(sp,n) oldnums[n]
160 # define _tracef printf
162 # define TR(n, a) if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
164 extern NCURSES_EXPORT_VAR(unsigned) _nc_tracing;
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
171 NCURSES_EXPORT_VAR (int *)
172 _nc_oldnums = 0; /* obsolete: keep for ABI compat */
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 */
181 #define OLDNUM_SIZE(sp) (sp)->_oldnum_size
183 #endif /* defined(SCROLLDEBUG) || defined(HASHDEBUG) */
186 NCURSES_SP_NAME(_nc_scroll_optimize) (NCURSES_SP_DCL0)
187 /* scroll optimization to transform curscr to newscr */
190 int start, end, shift;
192 TR(TRACE_ICALLS, (T_CALLED("_nc_scroll_optimize(%p)"), (void *) SP_PARM));
194 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
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,
208 TR(TRACE_ICALLS, (T_RETURN("")));
211 oldnums(SP_PARM) = new_oldnums;
212 OLDNUM_SIZE(SP_PARM) = need_lines;
214 /* calculate the indices */
215 NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_ARG);
216 if (SP_PARM->hashtab_len < screen_lines(SP_PARM)) {
217 TR(TRACE_ICALLS, (T_RETURN("")));
221 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
224 if (USE_TRACEF(TRACE_UPDATE | TRACE_MOVE)) {
225 NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_ARG);
226 _nc_unlock_global(tracef);
230 /* pass 1 - from top to bottom scrolling up */
231 for (i = 0; i < screen_lines(SP_PARM);) {
232 while (i < screen_lines(SP_PARM)
233 && (OLDNUM(SP_PARM, i) == _NEWINDEX || OLDNUM(SP_PARM, i) <= i))
235 if (i >= screen_lines(SP_PARM))
238 shift = OLDNUM(SP_PARM, i) - i; /* shift > 0 */
242 while (i < screen_lines(SP_PARM)
243 && OLDNUM(SP_PARM, i) != _NEWINDEX
244 && OLDNUM(SP_PARM, i) - i == shift)
248 TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
249 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
250 if (NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_ARGx
254 screen_lines(SP_PARM) - 1) == ERR) {
255 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
258 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
261 /* pass 2 - from bottom to top scrolling down */
262 for (i = screen_lines(SP_PARM) - 1; i >= 0;) {
264 && (OLDNUM(SP_PARM, i) == _NEWINDEX
265 || OLDNUM(SP_PARM, i) >= i)) {
271 shift = OLDNUM(SP_PARM, i) - i; /* shift < 0 */
276 && OLDNUM(SP_PARM, i) != _NEWINDEX
277 && OLDNUM(SP_PARM, i) - i == shift) {
280 start = i + 1 - (-shift);
282 TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
283 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
284 if (NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_ARGx
288 screen_lines(SP_PARM) - 1) == ERR) {
289 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
292 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
294 TR(TRACE_ICALLS, (T_RETURN("")));
299 _nc_scroll_optimize(void)
301 NCURSES_SP_NAME(_nc_scroll_optimize) (CURRENT_SCREEN);
305 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
307 NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_DCL0)
308 /* dump the state of the real and virtual oldnum fields */
310 if (USE_TRACEF(TRACE_UPDATE | TRACE_MOVE)) {
312 size_t want = ((size_t) screen_lines(SP_PARM) + 1) * 4;
315 if ((buf = typeMalloc(char, want)) != 0) {
319 for (n = 0; n < screen_lines(SP_PARM); n++) {
320 int number = OLDNUM(SP_PARM, n);
321 if (number >= -99 && number < 999) {
322 _nc_SPRINTF(buf + strlen(buf),
323 _nc_SLIMIT(want - strlen(buf))
326 _nc_STRCAT(buf, " ??", want - strlen(buf));
338 NCURSES_SP_NAME(_nc_linedump) (CURRENT_SCREEN);
342 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */
347 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
349 char line[BUFSIZ], *st;
352 _nc_tracing = TRACE_MOVE;
357 for (n = 0; n < screen_lines(sp); n++)
358 oldnums[n] = _NEWINDEX;
360 /* grab the test vector */
361 if (fgets(line, sizeof(line), stdin) == (char *) NULL)
366 if (line[0] == '#') {
367 (void) fputs(line, stderr);
370 st = strtok(line, " ");
372 oldnums[n++] = atoi(st);
374 ((st = strtok((char *) NULL, " ")) != 0);
377 (void) fputs("Initial input:\n", stderr);
380 _nc_scroll_optimize();
384 #endif /* SCROLLDEBUG */
386 /* hardscroll.c ends here */