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