FIXUP: HAVE_SYS_IOCTL_H
[mirror-ossqm-ncurses.git] / ncurses / tty / hardscroll.c
blobd7e29bfec004a022f8747796282cc0d730290724
1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 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 ****************************************************************************/
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 ****************************************************************************/
36 /******************************************************************************
38 NAME
39 hardscroll.c -- hardware-scrolling optimization for ncurses
41 SYNOPSIS
42 void _nc_scroll_optimize(void)
44 DESCRIPTION
45 OVERVIEW
47 This algorithm for computes optimum hardware scrolling to transform an
48 old screen (curscr) into a new screen (newscr) via vertical line moves.
50 Because the screen has a `grain' (there are insert/delete/scroll line
51 operations but no insert/delete/scroll column operations), it is efficient
52 break the update algorithm into two pieces: a first stage that does only line
53 moves, optimizing the end product of user-invoked insertions, deletions, and
54 scrolls; and a second phase (corresponding to the present doupdate code in
55 ncurses) that does only line transformations.
57 The common case we want hardware scrolling for is to handle line insertions
58 and deletions in screen-oriented text-editors. This two-stage approach will
59 accomplish that at a low computation and code-size cost.
61 LINE-MOVE COMPUTATION
63 Now, to a discussion of the line-move computation.
65 For expository purposes, consider the screen lines to be represented by
66 integers 0..23 (with the understanding that the value of 23 may vary).
67 Let a new line introduced by insertion, scrolling, or at the bottom of
68 the screen following a line delete be given the index -1.
70 Assume that the real screen starts with lines 0..23. Now, we have
71 the following possible line-oriented operations on the screen:
73 Insertion: inserts a line at a given screen row, forcing all lines below
74 to scroll forward. The last screen line is lost. For example, an insertion
75 at line 5 would produce: 0..4 -1 5..23.
77 Deletion: deletes a line at a given screen row, forcing all lines below
78 to scroll forward. The last screen line is made new. For example, a deletion
79 at line 7 would produce: 0..6 8..23 -1.
81 Scroll up: move a range of lines up 1. The bottom line of the range
82 becomes new. For example, scrolling up the region from 9 to 14 will
83 produce 0..8 10..14 -1 15..23.
85 Scroll down: move a range of lines down 1. The top line of the range
86 becomes new. For example, scrolling down the region from 12 to 16 will produce
87 0..11 -1 12..15 17..23.
89 Now, an obvious property of all these operations is that they preserve the
90 order of old lines, though not their position in the sequence.
92 The key trick of this algorithm is that the original line indices described
93 above are actually maintained as _line[].oldindex fields in the window
94 structure, and stick to each line through scroll and insert/delete operations.
96 Thus, it is possible at update time to look at the oldnum fields and compute
97 an optimal set of il/dl/scroll operations that will take the real screen
98 lines to the virtual screen lines. Once these vertical moves have been done,
99 we can hand off to the second stage of the update algorithm, which does line
100 transformations.
102 Note that the move computation does not need to have the full generality
103 of a diff algorithm (which it superficially resembles) because lines cannot
104 be moved out of order.
106 THE ALGORITHM
108 The scrolling is done in two passes. The first pass is from top to bottom
109 scroling hunks UP. The second one is from bottom to top scrolling hunks DOWN.
110 Obviously enough, no lines to be scrolled will be destroyed. (lav)
112 HOW TO TEST THIS:
114 Use the following production:
116 hardscroll: hardscroll.c
117 $(CC) -g -DSCROLLDEBUG hardscroll.c -o hardscroll
119 Then just type scramble vectors and watch. The following test loads are
120 a representative sample of cases:
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
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
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
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
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
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 ------------------------------------
142 AUTHOR
143 Eric S. Raymond <esr@snark.thyrsus.com>, November 1994
144 New algorithm by Alexander V. Lukyanov <lav@yars.free.net>, Aug 1997
146 *****************************************************************************/
148 #include <curses.priv.h>
150 MODULE_ID("$Id: hardscroll.c,v 1.47 2010/04/24 23:46:47 tom Exp $")
152 #if defined(SCROLLDEBUG) || defined(HASHDEBUG)
154 # undef screen_lines
155 # define screen_lines(sp) MAXLINES
156 NCURSES_EXPORT_VAR (int)
157 oldnums[MAXLINES];
158 # define OLDNUM(sp,n) oldnums[n]
159 # define _tracef printf
160 # undef TR
161 # define TR(n, a) if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
163 extern NCURSES_EXPORT_VAR(unsigned) _nc_tracing;
165 #else /* no debug */
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. */
170 NCURSES_EXPORT_VAR (int *)
171 _nc_oldnums = 0; /* obsolete: keep for ABI compat */
173 # ifdef USE_HASHMAP
174 # 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 */
180 #define OLDNUM_SIZE(sp) (sp)->_oldnum_size
182 #endif /* defined(SCROLLDEBUG) || defined(HASHDEBUG) */
184 NCURSES_EXPORT(void)
185 NCURSES_SP_NAME(_nc_scroll_optimize) (NCURSES_SP_DCL0)
186 /* scroll optimization to transform curscr to newscr */
188 int i;
189 int start, end, shift;
191 TR(TRACE_ICALLS, (T_CALLED("_nc_scroll_optimize(%p)"), (void *) SP_PARM));
193 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
194 #ifdef USE_HASHMAP
195 /* get enough storage */
196 if (OLDNUM_SIZE(SP_PARM) < screen_lines(SP_PARM)) {
197 int *new_oldnums = typeRealloc(int,
198 (size_t) screen_lines(SP_PARM),
199 oldnums(SP_PARM));
200 if (!new_oldnums)
201 return;
202 oldnums(SP_PARM) = new_oldnums;
203 OLDNUM_SIZE(SP_PARM) = screen_lines(SP_PARM);
205 /* calculate the indices */
206 NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_ARG);
207 #endif
208 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
210 #ifdef TRACE
211 if (USE_TRACEF(TRACE_UPDATE | TRACE_MOVE)) {
212 NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_ARG);
213 _nc_unlock_global(tracef);
215 #endif /* TRACE */
217 /* pass 1 - from top to bottom scrolling up */
218 for (i = 0; i < screen_lines(SP_PARM);) {
219 while (i < screen_lines(SP_PARM)
220 && (OLDNUM(SP_PARM, i) == _NEWINDEX || OLDNUM(SP_PARM, i) <= i))
221 i++;
222 if (i >= screen_lines(SP_PARM))
223 break;
225 shift = OLDNUM(SP_PARM, i) - i; /* shift > 0 */
226 start = i;
228 i++;
229 while (i < screen_lines(SP_PARM)
230 && OLDNUM(SP_PARM, i) != _NEWINDEX
231 && OLDNUM(SP_PARM, i) - i == shift)
232 i++;
233 end = i - 1 + shift;
235 TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
236 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
237 if (NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_ARGx
238 shift,
239 start,
240 end,
241 screen_lines(SP_PARM) - 1) == ERR) {
242 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
243 continue;
245 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
248 /* pass 2 - from bottom to top scrolling down */
249 for (i = screen_lines(SP_PARM) - 1; i >= 0;) {
250 while (i >= 0
251 && (OLDNUM(SP_PARM, i) == _NEWINDEX
252 || OLDNUM(SP_PARM, i) >= i)) {
253 i--;
255 if (i < 0)
256 break;
258 shift = OLDNUM(SP_PARM, i) - i; /* shift < 0 */
259 end = i;
261 i--;
262 while (i >= 0
263 && OLDNUM(SP_PARM, i) != _NEWINDEX
264 && OLDNUM(SP_PARM, i) - i == shift) {
265 i--;
267 start = i + 1 - (-shift);
269 TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
270 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
271 if (NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_ARGx
272 shift,
273 start,
274 end,
275 screen_lines(SP_PARM) - 1) == ERR) {
276 TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
277 continue;
279 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
281 TR(TRACE_ICALLS, (T_RETURN("")));
284 #if NCURSES_SP_FUNCS
285 NCURSES_EXPORT(void)
286 _nc_scroll_optimize(void)
288 NCURSES_SP_NAME(_nc_scroll_optimize) (CURRENT_SCREEN);
290 #endif
292 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
293 NCURSES_EXPORT(void)
294 NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_DCL0)
295 /* dump the state of the real and virtual oldnum fields */
297 int n;
298 char *buf = 0;
299 size_t want = ((size_t) screen_lines(SP_PARM) + 1) * 4;
301 if ((buf = typeMalloc(char, want)) != 0) {
303 *buf = '\0';
304 for (n = 0; n < screen_lines(SP_PARM); n++)
305 (void) sprintf(buf + strlen(buf), " %02d", OLDNUM(SP_PARM, n));
306 TR(TRACE_UPDATE | TRACE_MOVE, ("virt %s", buf));
307 free(buf);
311 #if NCURSES_SP_FUNCS
312 NCURSES_EXPORT(void)
313 _nc_linedump(void)
315 NCURSES_SP_NAME(_nc_linedump) (CURRENT_SCREEN);
317 #endif
319 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */
321 #ifdef SCROLLDEBUG
324 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
326 char line[BUFSIZ], *st;
328 #ifdef TRACE
329 _nc_tracing = TRACE_MOVE;
330 #endif
331 for (;;) {
332 int n;
334 for (n = 0; n < screen_lines; n++)
335 oldnums[n] = _NEWINDEX;
337 /* grab the test vector */
338 if (fgets(line, sizeof(line), stdin) == (char *) NULL)
339 exit(EXIT_SUCCESS);
341 /* parse it */
342 n = 0;
343 if (line[0] == '#') {
344 (void) fputs(line, stderr);
345 continue;
347 st = strtok(line, " ");
348 do {
349 oldnums[n++] = atoi(st);
350 } while
351 ((st = strtok((char *) NULL, " ")) != 0);
353 /* display it */
354 (void) fputs("Initial input:\n", stderr);
355 _nc_linedump();
357 _nc_scroll_optimize();
361 #endif /* SCROLLDEBUG */
363 /* hardscroll.c ends here */