blob: 2b1f079e4b9eccd57968492e1f3efd63674cefe5 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020,2023 Thomas E. Dickey *
3 * Copyright 1998-2015,2016 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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
39NAME
40 hardscroll.c -- hardware-scrolling optimization for ncurses
41
42SYNOPSIS
43 void _nc_scroll_optimize(void)
44
45DESCRIPTION
46 OVERVIEW
47
48This algorithm for computes optimum hardware scrolling to transform an
49old screen (curscr) into a new screen (newscr) via vertical line moves.
50
51Because the screen has a `grain' (there are insert/delete/scroll line
52operations but no insert/delete/scroll column operations), it is efficient
53break the update algorithm into two pieces: a first stage that does only line
54moves, optimizing the end product of user-invoked insertions, deletions, and
55scrolls; and a second phase (corresponding to the present doupdate code in
56ncurses) that does only line transformations.
57
58The common case we want hardware scrolling for is to handle line insertions
59and deletions in screen-oriented text-editors. This two-stage approach will
60accomplish that at a low computation and code-size cost.
61
62 LINE-MOVE COMPUTATION
63
64Now, to a discussion of the line-move computation.
65
66For expository purposes, consider the screen lines to be represented by
67integers 0..23 (with the understanding that the value of 23 may vary).
68Let a new line introduced by insertion, scrolling, or at the bottom of
69the screen following a line delete be given the index -1.
70
71Assume that the real screen starts with lines 0..23. Now, we have
72the following possible line-oriented operations on the screen:
73
74Insertion: inserts a line at a given screen row, forcing all lines below
75to scroll forward. The last screen line is lost. For example, an insertion
76at line 5 would produce: 0..4 -1 5..23.
77
78Deletion: deletes a line at a given screen row, forcing all lines below
79to scroll forward. The last screen line is made new. For example, a deletion
80at line 7 would produce: 0..6 8..23 -1.
81
82Scroll up: move a range of lines up 1. The bottom line of the range
83becomes new. For example, scrolling up the region from 9 to 14 will
84produce 0..8 10..14 -1 15..23.
85
86Scroll down: move a range of lines down 1. The top line of the range
87becomes new. For example, scrolling down the region from 12 to 16 will produce
880..11 -1 12..15 17..23.
89
90Now, an obvious property of all these operations is that they preserve the
91order of old lines, though not their position in the sequence.
92
93The key trick of this algorithm is that the original line indices described
94above are actually maintained as _line[].oldindex fields in the window
95structure, and stick to each line through scroll and insert/delete operations.
96
97Thus, it is possible at update time to look at the oldnum fields and compute
98an optimal set of il/dl/scroll operations that will take the real screen
99lines to the virtual screen lines. Once these vertical moves have been done,
100we can hand off to the second stage of the update algorithm, which does line
101transformations.
102
103Note that the move computation does not need to have the full generality
104of a diff algorithm (which it superficially resembles) because lines cannot
105be moved out of order.
106
107 THE ALGORITHM
108
109The scrolling is done in two passes. The first pass is from top to bottom
110scroling hunks UP. The second one is from bottom to top scrolling hunks DOWN.
111Obviously enough, no lines to be scrolled will be destroyed. (lav)
112
113HOW TO TEST THIS:
114
115Use the following production:
116
117hardscroll: hardscroll.c
118 $(CC) -g -DSCROLLDEBUG hardscroll.c -o hardscroll
119
120Then just type scramble vectors and watch. The following test loads are
121a 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
143AUTHOR
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
micky3879b9f5e72025-07-08 18:04:53 -0400151MODULE_ID("$Id: hardscroll.c,v 1.58 2023/09/09 16:04:08 Nicholas.Marriott Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530152
153#if defined(SCROLLDEBUG) || defined(HASHDEBUG)
154
155# undef screen_lines
Steve Kondikae271bc2015-11-15 02:50:53 +0100156# define screen_lines(sp) MAXLINES
157NCURSES_EXPORT_VAR (int)
158 oldnums[MAXLINES];
159# define OLDNUM(sp,n) oldnums[n]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530160# define _tracef printf
161# undef TR
162# define TR(n, a) if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
163
Steve Kondikae271bc2015-11-15 02:50:53 +0100164extern NCURSES_EXPORT_VAR(unsigned) _nc_tracing;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530165
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. */
Steve Kondikae271bc2015-11-15 02:50:53 +0100171NCURSES_EXPORT_VAR (int *)
172 _nc_oldnums = 0; /* obsolete: keep for ABI compat */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530173
174# if USE_HASHMAP
Steve Kondikae271bc2015-11-15 02:50:53 +0100175# 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 */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530180
Steve Kondikae271bc2015-11-15 02:50:53 +0100181#define OLDNUM_SIZE(sp) (sp)->_oldnum_size
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530182
183#endif /* defined(SCROLLDEBUG) || defined(HASHDEBUG) */
184
185NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100186NCURSES_SP_NAME(_nc_scroll_optimize) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530187/* scroll optimization to transform curscr to newscr */
188{
189 int i;
190 int start, end, shift;
191
Steve Kondikae271bc2015-11-15 02:50:53 +0100192 TR(TRACE_ICALLS, (T_CALLED("_nc_scroll_optimize(%p)"), (void *) SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530193
194#if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
195#if USE_HASHMAP
196 /* get enough storage */
Steve Kondikae271bc2015-11-15 02:50:53 +0100197 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));
micky3879b9f5e72025-07-08 18:04:53 -0400207 if (!new_oldnums) {
208 TR(TRACE_ICALLS, (T_RETURN("")));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530209 return;
micky3879b9f5e72025-07-08 18:04:53 -0400210 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100211 oldnums(SP_PARM) = new_oldnums;
212 OLDNUM_SIZE(SP_PARM) = need_lines;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530213 }
214 /* calculate the indices */
Steve Kondikae271bc2015-11-15 02:50:53 +0100215 NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_ARG);
micky3879b9f5e72025-07-08 18:04:53 -0400216 if (SP_PARM->hashtab_len < screen_lines(SP_PARM)) {
217 TR(TRACE_ICALLS, (T_RETURN("")));
218 return;
219 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530220#endif
221#endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
222
223#ifdef TRACE
224 if (USE_TRACEF(TRACE_UPDATE | TRACE_MOVE)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100225 NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530226 _nc_unlock_global(tracef);
227 }
228#endif /* TRACE */
229
230 /* pass 1 - from top to bottom scrolling up */
Steve Kondikae271bc2015-11-15 02:50:53 +0100231 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))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530234 i++;
Steve Kondikae271bc2015-11-15 02:50:53 +0100235 if (i >= screen_lines(SP_PARM))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530236 break;
237
Steve Kondikae271bc2015-11-15 02:50:53 +0100238 shift = OLDNUM(SP_PARM, i) - i; /* shift > 0 */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530239 start = i;
240
241 i++;
Steve Kondikae271bc2015-11-15 02:50:53 +0100242 while (i < screen_lines(SP_PARM)
243 && OLDNUM(SP_PARM, i) != _NEWINDEX
244 && OLDNUM(SP_PARM, i) - i == shift)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530245 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)
Steve Kondikae271bc2015-11-15 02:50:53 +0100250 if (NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_ARGx
251 shift,
252 start,
253 end,
254 screen_lines(SP_PARM) - 1) == ERR) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530255 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 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100262 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)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530266 i--;
Steve Kondikae271bc2015-11-15 02:50:53 +0100267 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530268 if (i < 0)
269 break;
270
Steve Kondikae271bc2015-11-15 02:50:53 +0100271 shift = OLDNUM(SP_PARM, i) - i; /* shift < 0 */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530272 end = i;
273
274 i--;
Steve Kondikae271bc2015-11-15 02:50:53 +0100275 while (i >= 0
276 && OLDNUM(SP_PARM, i) != _NEWINDEX
277 && OLDNUM(SP_PARM, i) - i == shift) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530278 i--;
Steve Kondikae271bc2015-11-15 02:50:53 +0100279 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530280 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)
Steve Kondikae271bc2015-11-15 02:50:53 +0100284 if (NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_ARGx
285 shift,
286 start,
287 end,
288 screen_lines(SP_PARM) - 1) == ERR) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530289 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
Steve Kondikae271bc2015-11-15 02:50:53 +0100297#if NCURSES_SP_FUNCS
298NCURSES_EXPORT(void)
299_nc_scroll_optimize(void)
300{
301 NCURSES_SP_NAME(_nc_scroll_optimize) (CURRENT_SCREEN);
302}
303#endif
304
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530305#if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
306NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100307NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530308/* dump the state of the real and virtual oldnum fields */
309{
micky3879b9f5e72025-07-08 18:04:53 -0400310 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;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530314
micky3879b9f5e72025-07-08 18:04:53 -0400315 if ((buf = typeMalloc(char, want)) != 0) {
316 int n;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530317
micky3879b9f5e72025-07-08 18:04:53 -0400318 *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 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530331 }
332}
Steve Kondikae271bc2015-11-15 02:50:53 +0100333
334#if NCURSES_SP_FUNCS
335NCURSES_EXPORT(void)
336_nc_linedump(void)
337{
338 NCURSES_SP_NAME(_nc_linedump) (CURRENT_SCREEN);
339}
340#endif
341
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530342#endif /* defined(TRACE) || defined(SCROLLDEBUG) */
343
344#ifdef SCROLLDEBUG
345
346int
347main(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
Steve Kondikae271bc2015-11-15 02:50:53 +0100357 for (n = 0; n < screen_lines(sp); n++)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530358 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 */