]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tty/hardscroll.c
ncurses 6.4 - patch 20240420
[ncurses.git] / ncurses / tty / hardscroll.c
1 /****************************************************************************
2  * Copyright 2020,2023 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.58 2023/09/09 16:04:08 Nicholas.Marriott 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             TR(TRACE_ICALLS, (T_RETURN("")));
209             return;
210         }
211         oldnums(SP_PARM) = new_oldnums;
212         OLDNUM_SIZE(SP_PARM) = need_lines;
213     }
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("")));
218         return;
219     }
220 #endif
221 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
222
223 #ifdef TRACE
224     if (USE_TRACEF(TRACE_UPDATE | TRACE_MOVE)) {
225         NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_ARG);
226         _nc_unlock_global(tracef);
227     }
228 #endif /* TRACE */
229
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))
234             i++;
235         if (i >= screen_lines(SP_PARM))
236             break;
237
238         shift = OLDNUM(SP_PARM, i) - i;         /* shift > 0 */
239         start = i;
240
241         i++;
242         while (i < screen_lines(SP_PARM)
243                && OLDNUM(SP_PARM, i) != _NEWINDEX
244                && OLDNUM(SP_PARM, i) - i == shift)
245             i++;
246         end = i - 1 + shift;
247
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
251                                           shift,
252                                           start,
253                                           end,
254                                           screen_lines(SP_PARM) - 1) == ERR) {
255             TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
256             continue;
257         }
258 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
259     }
260
261     /* pass 2 - from bottom to top scrolling down */
262     for (i = screen_lines(SP_PARM) - 1; i >= 0;) {
263         while (i >= 0
264                && (OLDNUM(SP_PARM, i) == _NEWINDEX
265                    || OLDNUM(SP_PARM, i) >= i)) {
266             i--;
267         }
268         if (i < 0)
269             break;
270
271         shift = OLDNUM(SP_PARM, i) - i;         /* shift < 0 */
272         end = i;
273
274         i--;
275         while (i >= 0
276                && OLDNUM(SP_PARM, i) != _NEWINDEX
277                && OLDNUM(SP_PARM, i) - i == shift) {
278             i--;
279         }
280         start = i + 1 - (-shift);
281
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
285                                           shift,
286                                           start,
287                                           end,
288                                           screen_lines(SP_PARM) - 1) == ERR) {
289             TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
290             continue;
291         }
292 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
293     }
294     TR(TRACE_ICALLS, (T_RETURN("")));
295 }
296
297 #if NCURSES_SP_FUNCS
298 NCURSES_EXPORT(void)
299 _nc_scroll_optimize(void)
300 {
301     NCURSES_SP_NAME(_nc_scroll_optimize) (CURRENT_SCREEN);
302 }
303 #endif
304
305 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
306 NCURSES_EXPORT(void)
307 NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_DCL0)
308 /* dump the state of the real and virtual oldnum fields */
309 {
310     if (USE_TRACEF(TRACE_UPDATE | TRACE_MOVE)) {
311         char *buf = 0;
312         size_t want = ((size_t) screen_lines(SP_PARM) + 1) * 4;
313         (void) SP_PARM;
314
315         if ((buf = typeMalloc(char, want)) != 0) {
316             int n;
317
318             *buf = '\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))
324                                 " %02d", number);
325                 } else {
326                     _nc_STRCAT(buf, " ??", want - strlen(buf));
327                 }
328             }
329             free(buf);
330         }
331     }
332 }
333
334 #if NCURSES_SP_FUNCS
335 NCURSES_EXPORT(void)
336 _nc_linedump(void)
337 {
338     NCURSES_SP_NAME(_nc_linedump) (CURRENT_SCREEN);
339 }
340 #endif
341
342 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */
343
344 #ifdef SCROLLDEBUG
345
346 int
347 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
348 {
349     char line[BUFSIZ], *st;
350
351 #ifdef TRACE
352     _nc_tracing = TRACE_MOVE;
353 #endif
354     for (;;) {
355         int n;
356
357         for (n = 0; n < screen_lines(sp); n++)
358             oldnums[n] = _NEWINDEX;
359
360         /* grab the test vector */
361         if (fgets(line, sizeof(line), stdin) == (char *) NULL)
362             exit(EXIT_SUCCESS);
363
364         /* parse it */
365         n = 0;
366         if (line[0] == '#') {
367             (void) fputs(line, stderr);
368             continue;
369         }
370         st = strtok(line, " ");
371         do {
372             oldnums[n++] = atoi(st);
373         } while
374             ((st = strtok((char *) NULL, " ")) != 0);
375
376         /* display it */
377         (void) fputs("Initial input:\n", stderr);
378         _nc_linedump();
379
380         _nc_scroll_optimize();
381     }
382 }
383
384 #endif /* SCROLLDEBUG */
385
386 /* hardscroll.c ends here */