1 /****************************************************************************
2 * Copyright 2020 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.54 2020/02/02 23:34:34 tom 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,
209 oldnums(SP_PARM) = new_oldnums;
210 OLDNUM_SIZE(SP_PARM) = need_lines;
212 /* calculate the indices */
213 NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_ARG);
215 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
218 if (USE_TRACEF(TRACE_UPDATE | TRACE_MOVE)) {
219 NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_ARG);
220 _nc_unlock_global(tracef);
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))
229 if (i >= screen_lines(SP_PARM))
232 shift = OLDNUM(SP_PARM, i) - i; /* shift > 0 */
236 while (i < screen_lines(SP_PARM)
237 && OLDNUM(SP_PARM, i) != _NEWINDEX
238 && OLDNUM(SP_PARM, i) - i == shift)
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
248 screen_lines(SP_PARM) - 1) == ERR) {
249 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
252 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
255 /* pass 2 - from bottom to top scrolling down */
256 for (i = screen_lines(SP_PARM) - 1; i >= 0;) {
258 && (OLDNUM(SP_PARM, i) == _NEWINDEX
259 || OLDNUM(SP_PARM, i) >= i)) {
265 shift = OLDNUM(SP_PARM, i) - i; /* shift < 0 */
270 && OLDNUM(SP_PARM, i) != _NEWINDEX
271 && OLDNUM(SP_PARM, i) - i == shift) {
274 start = i + 1 - (-shift);
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
282 screen_lines(SP_PARM) - 1) == ERR) {
283 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
286 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
288 TR(TRACE_ICALLS, (T_RETURN("")));
293 _nc_scroll_optimize(void)
295 NCURSES_SP_NAME(_nc_scroll_optimize) (CURRENT_SCREEN);
299 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
301 NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_DCL0)
302 /* dump the state of the real and virtual oldnum fields */
305 size_t want = ((size_t) screen_lines(SP_PARM) + 1) * 4;
308 if ((buf = typeMalloc(char, want)) != 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));
325 NCURSES_SP_NAME(_nc_linedump) (CURRENT_SCREEN);
329 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */
334 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
336 char line[BUFSIZ], *st;
339 _nc_tracing = TRACE_MOVE;
344 for (n = 0; n < screen_lines(sp); n++)
345 oldnums[n] = _NEWINDEX;
347 /* grab the test vector */
348 if (fgets(line, sizeof(line), stdin) == (char *) NULL)
353 if (line[0] == '#') {
354 (void) fputs(line, stderr);
357 st = strtok(line, " ");
359 oldnums[n++] = atoi(st);
361 ((st = strtok((char *) NULL, " ")) != 0);
364 (void) fputs("Initial input:\n", stderr);
367 _nc_scroll_optimize();
371 #endif /* SCROLLDEBUG */
373 /* hardscroll.c ends here */