]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tty/hardscroll.c
ncurses 6.0 - patch 20170311
[ncurses.git] / ncurses / tty / hardscroll.c
index 0b227a109dcfba7b9a9c0ecef472b8bc53c4c5b1..f7c9e0d46f5295b95525da1cdc45da3fa02404f5 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2015,2016 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -29,6 +29,8 @@
 /****************************************************************************
  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
+ *     and: Thomas E. Dickey                        1996-on                 *
+ *     and: Alexander V Lukyanov                    1997-1998               *
  ****************************************************************************/
 
 /******************************************************************************
@@ -145,88 +147,104 @@ AUTHOR
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: hardscroll.c,v 1.40 2007/06/30 21:11:01 tom Exp $")
+MODULE_ID("$Id: hardscroll.c,v 1.53 2016/05/28 23:32:40 tom Exp $")
 
 #if defined(SCROLLDEBUG) || defined(HASHDEBUG)
 
 # undef screen_lines
-# define screen_lines MAXLINES
-NCURSES_EXPORT_VAR(int)
-oldnums[MAXLINES];
-# define OLDNUM(n)     oldnums[n]
+# define screen_lines(sp) MAXLINES
+NCURSES_EXPORT_VAR (int)
+  oldnums[MAXLINES];
+# define OLDNUM(sp,n)  oldnums[n]
 # define _tracef       printf
 # undef TR
 # define TR(n, a)      if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
 
-extern NCURSES_EXPORT_VAR(unsigned)     _nc_tracing;
+extern                         NCURSES_EXPORT_VAR(unsigned) _nc_tracing;
 
 #else /* no debug */
 
 /* OLDNUM(n) indicates which line will be shifted to the position n.
    if OLDNUM(n) == _NEWINDEX, then the line n in new, not shifted from
    somewhere. */
-NCURSES_EXPORT_VAR(int *)
-_nc_oldnums = 0;               /* obsolete: keep for ABI compat */
+NCURSES_EXPORT_VAR (int *)
+  _nc_oldnums = 0;             /* obsolete: keep for ABI compat */
 
 # if USE_HASHMAP
-#  define oldnums       SP->_oldnum_list
-#  define OLDNUM(n)    oldnums[n]
-# else                         /* !USE_HASHMAP */
-#  define OLDNUM(n)    newscr->_line[n].oldindex
-# endif                                /* !USE_HASHMAP */
+#  define oldnums(sp)   (sp)->_oldnum_list
+#  define OLDNUM(sp,n) oldnums(sp)[n]
+# else /* !USE_HASHMAP */
+#  define OLDNUM(sp,n) NewScreen(sp)->_line[n].oldindex
+# endif        /* !USE_HASHMAP */
 
-#define OLDNUM_SIZE     SP->_oldnum_size
+#define OLDNUM_SIZE(sp) (sp)->_oldnum_size
 
 #endif /* defined(SCROLLDEBUG) || defined(HASHDEBUG) */
 
 NCURSES_EXPORT(void)
-_nc_scroll_optimize(void)
+NCURSES_SP_NAME(_nc_scroll_optimize) (NCURSES_SP_DCL0)
 /* scroll optimization to transform curscr to newscr */
 {
     int i;
     int start, end, shift;
 
-    TR(TRACE_ICALLS, (T_CALLED("_nc_scroll_optimize")));
+    TR(TRACE_ICALLS, (T_CALLED("_nc_scroll_optimize(%p)"), (void *) SP_PARM));
 
 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
 #if USE_HASHMAP
     /* get enough storage */
-    if (OLDNUM_SIZE < screen_lines) {
-       int *new_oldnums = typeRealloc(int, screen_lines, oldnums);
+    assert(OLDNUM_SIZE(SP_PARM) >= 0);
+    assert(screen_lines(SP_PARM) > 0);
+    if ((oldnums(SP_PARM) == 0)
+       || (OLDNUM_SIZE(SP_PARM) < screen_lines(SP_PARM))) {
+       int need_lines = ((OLDNUM_SIZE(SP_PARM) < screen_lines(SP_PARM))
+                         ? screen_lines(SP_PARM)
+                         : OLDNUM_SIZE(SP_PARM));
+       int *new_oldnums = typeRealloc(int,
+                                      (size_t) need_lines,
+                                      oldnums(SP_PARM));
        if (!new_oldnums)
            return;
-       oldnums = new_oldnums;
-       OLDNUM_SIZE = screen_lines;
+       oldnums(SP_PARM) = new_oldnums;
+       OLDNUM_SIZE(SP_PARM) = need_lines;
     }
     /* calculate the indices */
-    _nc_hash_map();
+    NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_ARG);
 #endif
 #endif /* !defined(SCROLLDEBUG) && !defined(HASHDEBUG) */
 
 #ifdef TRACE
-    if (_nc_tracing & (TRACE_UPDATE | TRACE_MOVE))
-       _nc_linedump();
+    if (USE_TRACEF(TRACE_UPDATE | TRACE_MOVE)) {
+       NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_ARG);
+       _nc_unlock_global(tracef);
+    }
 #endif /* TRACE */
 
     /* pass 1 - from top to bottom scrolling up */
-    for (i = 0; i < screen_lines;) {
-       while (i < screen_lines && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) <= i))
+    for (i = 0; i < screen_lines(SP_PARM);) {
+       while (i < screen_lines(SP_PARM)
+              && (OLDNUM(SP_PARM, i) == _NEWINDEX || OLDNUM(SP_PARM, i) <= i))
            i++;
-       if (i >= screen_lines)
+       if (i >= screen_lines(SP_PARM))
            break;
 
-       shift = OLDNUM(i) - i;  /* shift > 0 */
+       shift = OLDNUM(SP_PARM, i) - i;         /* shift > 0 */
        start = i;
 
        i++;
-       while (i < screen_lines && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i
-              == shift)
+       while (i < screen_lines(SP_PARM)
+              && OLDNUM(SP_PARM, i) != _NEWINDEX
+              && OLDNUM(SP_PARM, i) - i == shift)
            i++;
        end = i - 1 + shift;
 
        TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
-       if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR) {
+       if (NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_ARGx
+                                         shift,
+                                         start,
+                                         end,
+                                         screen_lines(SP_PARM) - 1) == ERR) {
            TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
            continue;
        }
@@ -234,23 +252,33 @@ _nc_scroll_optimize(void)
     }
 
     /* pass 2 - from bottom to top scrolling down */
-    for (i = screen_lines - 1; i >= 0;) {
-       while (i >= 0 && (OLDNUM(i) == _NEWINDEX || OLDNUM(i) >= i))
+    for (i = screen_lines(SP_PARM) - 1; i >= 0;) {
+       while (i >= 0
+              && (OLDNUM(SP_PARM, i) == _NEWINDEX
+                  || OLDNUM(SP_PARM, i) >= i)) {
            i--;
+       }
        if (i < 0)
            break;
 
-       shift = OLDNUM(i) - i;  /* shift < 0 */
+       shift = OLDNUM(SP_PARM, i) - i;         /* shift < 0 */
        end = i;
 
        i--;
-       while (i >= 0 && OLDNUM(i) != _NEWINDEX && OLDNUM(i) - i == shift)
+       while (i >= 0
+              && OLDNUM(SP_PARM, i) != _NEWINDEX
+              && OLDNUM(SP_PARM, i) - i == shift) {
            i--;
+       }
        start = i + 1 - (-shift);
 
        TR(TRACE_UPDATE | TRACE_MOVE, ("scroll [%d, %d] by %d", start, end, shift));
 #if !defined(SCROLLDEBUG) && !defined(HASHDEBUG)
-       if (_nc_scrolln(shift, start, end, screen_lines - 1) == ERR) {
+       if (NCURSES_SP_NAME(_nc_scrolln) (NCURSES_SP_ARGx
+                                         shift,
+                                         start,
+                                         end,
+                                         screen_lines(SP_PARM) - 1) == ERR) {
            TR(TRACE_UPDATE | TRACE_MOVE, ("unable to scroll"));
            continue;
        }
@@ -259,23 +287,44 @@ _nc_scroll_optimize(void)
     TR(TRACE_ICALLS, (T_RETURN("")));
 }
 
+#if NCURSES_SP_FUNCS
+NCURSES_EXPORT(void)
+_nc_scroll_optimize(void)
+{
+    NCURSES_SP_NAME(_nc_scroll_optimize) (CURRENT_SCREEN);
+}
+#endif
+
 #if defined(TRACE) || defined(SCROLLDEBUG) || defined(HASHDEBUG)
 NCURSES_EXPORT(void)
-_nc_linedump(void)
+NCURSES_SP_NAME(_nc_linedump) (NCURSES_SP_DCL0)
 /* dump the state of the real and virtual oldnum fields */
 {
-    int n;
     char *buf = 0;
-    size_t want = (screen_lines + 1) * 4;
+    size_t want = ((size_t) screen_lines(SP_PARM) + 1) * 4;
+    (void) SP_PARM;
+
+    if ((buf = typeMalloc(char, want)) != 0) {
+       int n;
 
-    buf = typeMalloc(char, want);
+       *buf = '\0';
+       for (n = 0; n < screen_lines(SP_PARM); n++)
+           _nc_SPRINTF(buf + strlen(buf),
+                       _nc_SLIMIT(want - strlen(buf))
+                       " %02d", OLDNUM(SP_PARM, n));
+       TR(TRACE_UPDATE | TRACE_MOVE, ("virt %s", buf));
+       free(buf);
+    }
+}
 
-    (void) strcpy(buf, "virt");
-    for (n = 0; n < screen_lines; n++)
-       (void) sprintf(buf + strlen(buf), " %02d", OLDNUM(n));
-    TR(TRACE_UPDATE | TRACE_MOVE, (buf));
-    free(buf);
+#if NCURSES_SP_FUNCS
+NCURSES_EXPORT(void)
+_nc_linedump(void)
+{
+    NCURSES_SP_NAME(_nc_linedump) (CURRENT_SCREEN);
 }
+#endif
+
 #endif /* defined(TRACE) || defined(SCROLLDEBUG) */
 
 #ifdef SCROLLDEBUG
@@ -291,7 +340,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
     for (;;) {
        int n;
 
-       for (n = 0; n < screen_lines; n++)
+       for (n = 0; n < screen_lines(sp); n++)
            oldnums[n] = _NEWINDEX;
 
        /* grab the test vector */