]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tty/hardscroll.c
e2147217bdb32236775289fa5e80de138bd97cbd
[ncurses.git] / ncurses / tty / hardscroll.c
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
3  *                                                                          *
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  ****************************************************************************/
33
34 /******************************************************************************
35
36 NAME
37    hardscroll.c -- hardware-scrolling optimization for ncurses
38
39 SYNOPSIS
40    void _nc_scroll_optimize(void)
41
42 DESCRIPTION
43                         OVERVIEW
44
45 This algorithm for computes optimum hardware scrolling to transform an
46 old screen (curscr) into a new screen (newscr) via vertical line moves.
47
48 Because the screen has a `grain' (there are insert/delete/scroll line
49 operations but no insert/delete/scroll column operations), it is efficient
50 break the update algorithm into two pieces: a first stage that does only line
51 moves, optimizing the end product of user-invoked insertions, deletions, and
52 scrolls; and a second phase (corresponding to the present doupdate code in
53 ncurses) that does only line transformations.
54
55 The common case we want hardware scrolling for is to handle line insertions
56 and deletions in screen-oriented text-editors.  This two-stage approach will
57 accomplish that at a low computation and code-size cost.
58
59                         LINE-MOVE COMPUTATION
60
61 Now, to a discussion of the line-move computation.
62
63 For expository purposes, consider the screen lines to be represented by
64 integers 0..23 (with the understanding that the value of 23 may vary).
65 Let a new line introduced by insertion, scrolling, or at the bottom of
66 the screen following a line delete be given the index -1.
67
68 Assume that the real screen starts with lines 0..23.  Now, we have
69 the following possible line-oriented operations on the screen:
70
71 Insertion: inserts a line at a given screen row, forcing all lines below
72 to scroll forward.  The last screen line is lost.  For example, an insertion
73 at line 5 would produce: 0..4 -1 5..23.
74
75 Deletion: deletes a line at a given screen row, forcing all lines below
76 to scroll forward.  The last screen line is made new.  For example, a deletion
77 at line 7 would produce: 0..6 8..23 -1.
78
79 Scroll up: move a range of lines up 1.  The bottom line of the range
80 becomes new.  For example, scrolling up the region from 9 to 14 will
81 produce 0..8 10..14 -1 15..23.
82
83 Scroll down: move a range of lines down 1.  The top line of the range
84 becomes new.  For example, scrolling down the region from 12 to 16 will produce
85 0..11 -1 12..15 17..23.
86
87 Now, an obvious property of all these operations is that they preserve the
88 order of old lines, though not their position in the sequence.
89
90 The key trick of this algorithm is that the original line indices described
91 above are actually maintained as _line[].oldindex fields in the window
92 structure, and stick to each line through scroll and insert/delete operations.
93
94 Thus, it is possible at update time to look at the oldnum fields and compute
95 an optimal set of il/dl/scroll operations that will take the real screen
96 lines to the virtual screen lines.  Once these vertical moves have been done,
97 we can hand off to the second stage of the update algorithm, which does line
98 transformations.
99
100 Note that the move computation does not need to have the full generality
101 of a diff algorithm (which it superficially resembles) because lines cannot
102 be moved out of order.
103
104                         THE ALGORITHM
105
106 The scrolling is done in two passes. The first pass is from top to bottom
107 scroling hunks UP. The second one is from bottom to top scrolling hunks DOWN.
108 Obviously enough, no lines to be scrolled will be destroyed. (lav)
109
110 HOW TO TEST THIS:
111
112 Use the following production:
113
114 hardscroll: hardscroll.c
115         $(CC) -g -DSCROLLDEBUG hardscroll.c -o hardscroll
116
117 Then just type scramble vectors and watch.  The following test loads are
118 a representative sample of cases:
119
120 -----------------------------  CUT HERE ------------------------------------
121 # No lines moved
122  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
123 #
124 # A scroll up
125  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 -1
126 #
127 # A scroll down
128 -1  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
129 #
130 # An insertion (after line 12)
131  0  1  2  3  4  5  6  7  8  9 10 11 12 -1 13 14 15 16 17 18 19 20 21 22
132 #
133 # A simple deletion (line 10)
134  0  1  2  3  4  5  6  7  8  9  11 12 13 14 15 16 17 18 19 20 21 22 23 -1
135 #
136 # A more complex case
137 -1 -1 -1 -1 -1  3  4  5  6  7  -1 -1  8  9 10 11 12 13 14 15 16 17 -1 -1
138 -----------------------------  CUT HERE ------------------------------------
139
140 AUTHOR
141     Eric S. Raymond <esr@snark.thyrsus.com>, November 1994
142     New algorithm by Alexander V. Lukyanov <lav@yars.free.net>, Aug 1997
143
144 *****************************************************************************/
145
146 #include <curses.priv.h>
147
148 MODULE_ID("$Id: hardscroll.c,v 1.39 2007/04/28 20:14:08 tom Exp $")
149
150 #if defined(SCROLLDEBUG) || defined(HASHDEBUG)
151
152 # undef screen_lines
153 # define screen_lines MAXLINES
154 NCURSES_EXPORT_VAR(int)
155 oldnums[MAXLINES];
156 # define OLDNUM(n)      oldnums[n]
157 # define _tracef        printf
158 # undef TR
159 # define TR(n, a)       if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
160
161 extern NCURSES_EXPORT_VAR(unsigned)     _nc_tracing;
162
163 #else /* no debug */
164
165 /* OLDNUM(n) indicates which line will be shifted to the position n.
166    if OLDNUM(n) == _NEWINDEX, then the line n in new, not shifted from
167    somewhere. */
168 NCURSES_EXPORT_VAR(int *)
169 _nc_oldnums = 0;                /* obsolete: keep for ABI compat */
170
171 # if USE_HASHMAP
172 #  define oldnums       SP->_oldnum_list
173 #  define OLDNUM(n)     oldnums[n]
174 # else                          /* !USE_HASHMAP */
175 #  define OLDNUM(n)     newscr->_line[n].oldindex
176 # endif                         /* !USE_HASHMAP */
177
178 #define OLDNUM_SIZE     SP->_oldnum_size
179
180 #endif /* defined(SCROLLDEBUG) || defined(HASHDEBUG) */
181
182 NCURSES_EXPORT(void)
183 _nc_scroll_optimize(void)
184 /* scroll optimization to transform curscr to newscr */
185 {
186     int i;
187     int start, end, shift;
188
189     TR(TRACE_ICALLS, ("_nc_scroll_optimize() begins"));
190
191 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
192 #if USE_HASHMAP
193     /* get enough storage */
194     if (OLDNUM_SIZE < screen_lines) {
195         int *new_oldnums = typeRealloc(int, screen_lines, oldnums);
196         if (!new_oldnums)
197             return;
198         oldnums = new_oldnums;
199         OLDNUM_SIZE = screen_lines;
200     }
201     /* calculate the indices */
202     _nc_hash_map();
203 #endif
204 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
205
206 #ifdef TRACE
207     if (_nc_tracing & (TRACE_UPDATE | TRACE_MOVE))
208         _nc_linedump();
209 #endif /* TRACE */
210
211     /* pass 1 - from top to bottom scrolling up */
212     for (i = 0; i < screen_lines;) {
213         while (i < screen_lines && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) <= i))
214             i++;
215         if (i >= screen_lines)
216             break;
217
218         shift = OLDNUM(i) - i;  /* shift > 0 */
219         start = i;
220
221         i++;
222         while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i
223                == shift)
224             i++;
225         end = i - 1 + shift;
226
227         TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
228 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
229         if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR) {
230             TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
231             continue;
232         }
233 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
234     }
235
236     /* pass 2 - from bottom to top scrolling down */
237     for (i = screen_lines - 1; i >= 0;) {
238         while (i >= 0 && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) >= i))
239             i--;
240         if (i < 0)
241             break;
242
243         shift = OLDNUM(i) - i;  /* shift < 0 */
244         end = i;
245
246         i--;
247         while (i >= 0 && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
248             i--;
249         start = i + 1 - (-shift);
250
251         TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
252 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
253         if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR) {
254             TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
255             continue;
256         }
257 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
258     }
259 }
260
261 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
262 NCURSES_EXPORT(void)
263 _nc_linedump(void)
264 /* dump the state of the real and virtual oldnum fields */
265 {
266     int n;
267     char *buf = 0;
268     size_t want = (screen_lines + 1) * 4;
269
270     buf = typeMalloc(char, want);
271
272     (void) strcpy(buf, "virt");
273     for (n = 0; n < screen_lines; n++)
274         (void) sprintf(buf + strlen(buf), " %02d", OLDNUM(n));
275     TR(TRACE_UPDATE | TRACE_MOVE, (buf));
276     free(buf);
277 }
278 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */
279
280 #ifdef SCROLLDEBUG
281
282 int
283 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
284 {
285     char line[BUFSIZ], *st;
286
287 #ifdef TRACE
288     _nc_tracing = TRACE_MOVE;
289 #endif
290     for (;;) {
291         int n;
292
293         for (n = 0; n < screen_lines; n++)
294             oldnums[n] = _NEWINDEX;
295
296         /* grab the test vector */
297         if (fgets(line, sizeof(line), stdin) == (char *) NULL)
298             exit(EXIT_SUCCESS);
299
300         /* parse it */
301         n = 0;
302         if (line[0] == '#') {
303             (void) fputs(line, stderr);
304             continue;
305         }
306         st = strtok(line, " ");
307         do {
308             oldnums[n++] = atoi(st);
309         } while
310             ((st = strtok((char *) NULL, " ")) != 0);
311
312         /* display it */
313         (void) fputs("Initial input:\n", stderr);
314         _nc_linedump();
315
316         _nc_scroll_optimize();
317     }
318 }
319
320 #endif /* SCROLLDEBUG */
321
322 /* hardscroll.c ends here */