1*c7ef0cfcSnicm /* $OpenBSD: lib_scroll.c,v 1.5 2023/10/17 09:52:09 nicm Exp $ */
292dd1ec0Smillert
392dd1ec0Smillert /****************************************************************************
4*c7ef0cfcSnicm * Copyright 2019,2020 Thomas E. Dickey *
5*c7ef0cfcSnicm * Copyright 1998-2010,2011 Free Software Foundation, Inc. *
692dd1ec0Smillert * *
792dd1ec0Smillert * Permission is hereby granted, free of charge, to any person obtaining a *
892dd1ec0Smillert * copy of this software and associated documentation files (the *
992dd1ec0Smillert * "Software"), to deal in the Software without restriction, including *
1092dd1ec0Smillert * without limitation the rights to use, copy, modify, merge, publish, *
1192dd1ec0Smillert * distribute, distribute with modifications, sublicense, and/or sell *
1292dd1ec0Smillert * copies of the Software, and to permit persons to whom the Software is *
1392dd1ec0Smillert * furnished to do so, subject to the following conditions: *
1492dd1ec0Smillert * *
1592dd1ec0Smillert * The above copyright notice and this permission notice shall be included *
1692dd1ec0Smillert * in all copies or substantial portions of the Software. *
1792dd1ec0Smillert * *
1892dd1ec0Smillert * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
1992dd1ec0Smillert * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
2092dd1ec0Smillert * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
2192dd1ec0Smillert * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
2292dd1ec0Smillert * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
2392dd1ec0Smillert * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
2492dd1ec0Smillert * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
2592dd1ec0Smillert * *
2692dd1ec0Smillert * Except as contained in this notice, the name(s) of the above copyright *
2792dd1ec0Smillert * holders shall not be used in advertising or otherwise to promote the *
2892dd1ec0Smillert * sale, use or other dealings in this Software without prior written *
2992dd1ec0Smillert * authorization. *
3092dd1ec0Smillert ****************************************************************************/
3192dd1ec0Smillert
3292dd1ec0Smillert /****************************************************************************
3381d8c4e1Snicm * Author: Thomas E. Dickey 1996-2003 *
3481d8c4e1Snicm * and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
3592dd1ec0Smillert * and: Eric S. Raymond <esr@snark.thyrsus.com> *
3692dd1ec0Smillert ****************************************************************************/
3792dd1ec0Smillert
3892dd1ec0Smillert /*
3992dd1ec0Smillert ** lib_scroll.c
4092dd1ec0Smillert **
4192dd1ec0Smillert ** The routine wscrl(win, n).
4292dd1ec0Smillert ** positive n scroll the window up (ie. move lines down)
4392dd1ec0Smillert ** negative n scroll the window down (ie. move lines up)
4492dd1ec0Smillert **
4592dd1ec0Smillert */
4692dd1ec0Smillert
4792dd1ec0Smillert #include <curses.priv.h>
4892dd1ec0Smillert
49*c7ef0cfcSnicm MODULE_ID("$Id: lib_scroll.c,v 1.5 2023/10/17 09:52:09 nicm Exp $")
5092dd1ec0Smillert
NCURSES_EXPORT(void)5184af20ceSmillert NCURSES_EXPORT(void)
5281d8c4e1Snicm _nc_scroll_window(WINDOW *win,
5381d8c4e1Snicm int const n,
54*c7ef0cfcSnicm int const top,
55*c7ef0cfcSnicm int const bottom,
5681d8c4e1Snicm NCURSES_CH_T blank)
5792dd1ec0Smillert {
5881d8c4e1Snicm int limit;
5981d8c4e1Snicm int line;
6081d8c4e1Snicm int j;
61*c7ef0cfcSnicm size_t to_copy = (sizeof(NCURSES_CH_T) * (size_t) (win->_maxx + 1));
6292dd1ec0Smillert
6381d8c4e1Snicm TR(TRACE_MOVE, ("_nc_scroll_window(%p, %d, %ld, %ld)",
64*c7ef0cfcSnicm (void *) win, n, (long) top, (long) bottom));
6581d8c4e1Snicm
6681d8c4e1Snicm if (top < 0
6781d8c4e1Snicm || bottom < top
6881d8c4e1Snicm || bottom > win->_maxy) {
6981d8c4e1Snicm TR(TRACE_MOVE, ("nothing to scroll"));
7081d8c4e1Snicm return;
7181d8c4e1Snicm }
7292dd1ec0Smillert
7392dd1ec0Smillert /*
7492dd1ec0Smillert * This used to do a line-text pointer-shuffle instead of text copies.
7592dd1ec0Smillert * That (a) doesn't work when the window is derived and doesn't have
7692dd1ec0Smillert * its own storage, (b) doesn't save you a lot on modern machines
7792dd1ec0Smillert * anyway. Your typical memcpy implementations are coded in
7892dd1ec0Smillert * assembler using a tight BLT loop; for the size of copies we're
7992dd1ec0Smillert * talking here, the total execution time is dominated by the one-time
8092dd1ec0Smillert * setup cost. So there is no point in trying to be excessively
8192dd1ec0Smillert * clever -- esr.
8292dd1ec0Smillert */
83*c7ef0cfcSnicm #define BottomLimit(n) ((n) >= 0 && (n) >= top)
84*c7ef0cfcSnicm #define TopLimit(n) ((n) <= win->_maxy && (n) <= bottom)
8592dd1ec0Smillert
8692dd1ec0Smillert /* shift n lines downwards */
8792dd1ec0Smillert if (n < 0) {
8881d8c4e1Snicm limit = top - n;
89*c7ef0cfcSnicm for (line = bottom; line >= limit && BottomLimit(line); line--) {
9081d8c4e1Snicm TR(TRACE_MOVE, ("...copying %d to %d", line + n, line));
9192dd1ec0Smillert memcpy(win->_line[line].text,
9292dd1ec0Smillert win->_line[line + n].text,
9392dd1ec0Smillert to_copy);
9481d8c4e1Snicm if_USE_SCROLL_HINTS(win->_line[line].oldindex =
9584af20ceSmillert win->_line[line + n].oldindex);
9692dd1ec0Smillert }
97*c7ef0cfcSnicm for (line = top; line < limit && TopLimit(line); line++) {
9881d8c4e1Snicm TR(TRACE_MOVE, ("...filling %d", line));
9992dd1ec0Smillert for (j = 0; j <= win->_maxx; j++)
10092dd1ec0Smillert win->_line[line].text[j] = blank;
10192dd1ec0Smillert if_USE_SCROLL_HINTS(win->_line[line].oldindex = _NEWINDEX);
10292dd1ec0Smillert }
10392dd1ec0Smillert }
10492dd1ec0Smillert
10592dd1ec0Smillert /* shift n lines upwards */
10692dd1ec0Smillert if (n > 0) {
10781d8c4e1Snicm limit = bottom - n;
108*c7ef0cfcSnicm for (line = top; line <= limit && TopLimit(line); line++) {
10992dd1ec0Smillert memcpy(win->_line[line].text,
11092dd1ec0Smillert win->_line[line + n].text,
11192dd1ec0Smillert to_copy);
11284af20ceSmillert if_USE_SCROLL_HINTS(win->_line[line].oldindex =
11384af20ceSmillert win->_line[line + n].oldindex);
11492dd1ec0Smillert }
115*c7ef0cfcSnicm for (line = bottom; line > limit && BottomLimit(line); line--) {
11692dd1ec0Smillert for (j = 0; j <= win->_maxx; j++)
11792dd1ec0Smillert win->_line[line].text[j] = blank;
11892dd1ec0Smillert if_USE_SCROLL_HINTS(win->_line[line].oldindex = _NEWINDEX);
11992dd1ec0Smillert }
12092dd1ec0Smillert }
12192dd1ec0Smillert touchline(win, top, bottom - top + 1);
12281d8c4e1Snicm
12381d8c4e1Snicm if_WIDEC({
12481d8c4e1Snicm if (WINDOW_EXT(win, addch_used) != 0) {
12581d8c4e1Snicm int next = WINDOW_EXT(win, addch_y) + n;
12681d8c4e1Snicm if (next < 0 || next > win->_maxy) {
12781d8c4e1Snicm TR(TRACE_VIRTPUT,
12881d8c4e1Snicm ("Alert discarded multibyte on scroll"));
12981d8c4e1Snicm WINDOW_EXT(win, addch_y) = 0;
13081d8c4e1Snicm } else {
13181d8c4e1Snicm TR(TRACE_VIRTPUT, ("scrolled working position to %d,%d",
13281d8c4e1Snicm WINDOW_EXT(win, addch_y),
13381d8c4e1Snicm WINDOW_EXT(win, addch_x)));
13481d8c4e1Snicm WINDOW_EXT(win, addch_y) = next;
13581d8c4e1Snicm }
13681d8c4e1Snicm }
13781d8c4e1Snicm })
13892dd1ec0Smillert }
13992dd1ec0Smillert
14084af20ceSmillert NCURSES_EXPORT(int)
wscrl(WINDOW * win,int n)14192dd1ec0Smillert wscrl(WINDOW *win, int n)
14292dd1ec0Smillert {
143*c7ef0cfcSnicm T((T_CALLED("wscrl(%p,%d)"), (void *) win, n));
14492dd1ec0Smillert
14581d8c4e1Snicm if (!win || !win->_scroll) {
14681d8c4e1Snicm TR(TRACE_MOVE, ("...scrollok is false"));
14792dd1ec0Smillert returnCode(ERR);
14881d8c4e1Snicm }
14992dd1ec0Smillert
15081d8c4e1Snicm if (n != 0) {
15181d8c4e1Snicm _nc_scroll_window(win, n, win->_regtop, win->_regbottom, win->_nc_bkgd);
15292dd1ec0Smillert _nc_synchook(win);
15381d8c4e1Snicm }
15492dd1ec0Smillert returnCode(OK);
15592dd1ec0Smillert }
156