1 /* $OpenBSD: lib_scroll.c,v 1.5 2023/10/17 09:52:09 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright 2019,2020 Thomas E. Dickey * 5 * Copyright 1998-2010,2011 Free Software Foundation, Inc. * 6 * * 7 * Permission is hereby granted, free of charge, to any person obtaining a * 8 * copy of this software and associated documentation files (the * 9 * "Software"), to deal in the Software without restriction, including * 10 * without limitation the rights to use, copy, modify, merge, publish, * 11 * distribute, distribute with modifications, sublicense, and/or sell * 12 * copies of the Software, and to permit persons to whom the Software is * 13 * furnished to do so, subject to the following conditions: * 14 * * 15 * The above copyright notice and this permission notice shall be included * 16 * in all copies or substantial portions of the Software. * 17 * * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 25 * * 26 * Except as contained in this notice, the name(s) of the above copyright * 27 * holders shall not be used in advertising or otherwise to promote the * 28 * sale, use or other dealings in this Software without prior written * 29 * authorization. * 30 ****************************************************************************/ 31 32 /**************************************************************************** 33 * Author: Thomas E. Dickey 1996-2003 * 34 * and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 35 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 36 ****************************************************************************/ 37 38 /* 39 ** lib_scroll.c 40 ** 41 ** The routine wscrl(win, n). 42 ** positive n scroll the window up (ie. move lines down) 43 ** negative n scroll the window down (ie. move lines up) 44 ** 45 */ 46 47 #include <curses.priv.h> 48 49 MODULE_ID("$Id: lib_scroll.c,v 1.5 2023/10/17 09:52:09 nicm Exp $") 50 51 NCURSES_EXPORT(void) 52 _nc_scroll_window(WINDOW *win, 53 int const n, 54 int const top, 55 int const bottom, 56 NCURSES_CH_T blank) 57 { 58 int limit; 59 int line; 60 int j; 61 size_t to_copy = (sizeof(NCURSES_CH_T) * (size_t) (win->_maxx + 1)); 62 63 TR(TRACE_MOVE, ("_nc_scroll_window(%p, %d, %ld, %ld)", 64 (void *) win, n, (long) top, (long) bottom)); 65 66 if (top < 0 67 || bottom < top 68 || bottom > win->_maxy) { 69 TR(TRACE_MOVE, ("nothing to scroll")); 70 return; 71 } 72 73 /* 74 * This used to do a line-text pointer-shuffle instead of text copies. 75 * That (a) doesn't work when the window is derived and doesn't have 76 * its own storage, (b) doesn't save you a lot on modern machines 77 * anyway. Your typical memcpy implementations are coded in 78 * assembler using a tight BLT loop; for the size of copies we're 79 * talking here, the total execution time is dominated by the one-time 80 * setup cost. So there is no point in trying to be excessively 81 * clever -- esr. 82 */ 83 #define BottomLimit(n) ((n) >= 0 && (n) >= top) 84 #define TopLimit(n) ((n) <= win->_maxy && (n) <= bottom) 85 86 /* shift n lines downwards */ 87 if (n < 0) { 88 limit = top - n; 89 for (line = bottom; line >= limit && BottomLimit(line); line--) { 90 TR(TRACE_MOVE, ("...copying %d to %d", line + n, line)); 91 memcpy(win->_line[line].text, 92 win->_line[line + n].text, 93 to_copy); 94 if_USE_SCROLL_HINTS(win->_line[line].oldindex = 95 win->_line[line + n].oldindex); 96 } 97 for (line = top; line < limit && TopLimit(line); line++) { 98 TR(TRACE_MOVE, ("...filling %d", line)); 99 for (j = 0; j <= win->_maxx; j++) 100 win->_line[line].text[j] = blank; 101 if_USE_SCROLL_HINTS(win->_line[line].oldindex = _NEWINDEX); 102 } 103 } 104 105 /* shift n lines upwards */ 106 if (n > 0) { 107 limit = bottom - n; 108 for (line = top; line <= limit && TopLimit(line); line++) { 109 memcpy(win->_line[line].text, 110 win->_line[line + n].text, 111 to_copy); 112 if_USE_SCROLL_HINTS(win->_line[line].oldindex = 113 win->_line[line + n].oldindex); 114 } 115 for (line = bottom; line > limit && BottomLimit(line); line--) { 116 for (j = 0; j <= win->_maxx; j++) 117 win->_line[line].text[j] = blank; 118 if_USE_SCROLL_HINTS(win->_line[line].oldindex = _NEWINDEX); 119 } 120 } 121 touchline(win, top, bottom - top + 1); 122 123 if_WIDEC({ 124 if (WINDOW_EXT(win, addch_used) != 0) { 125 int next = WINDOW_EXT(win, addch_y) + n; 126 if (next < 0 || next > win->_maxy) { 127 TR(TRACE_VIRTPUT, 128 ("Alert discarded multibyte on scroll")); 129 WINDOW_EXT(win, addch_y) = 0; 130 } else { 131 TR(TRACE_VIRTPUT, ("scrolled working position to %d,%d", 132 WINDOW_EXT(win, addch_y), 133 WINDOW_EXT(win, addch_x))); 134 WINDOW_EXT(win, addch_y) = next; 135 } 136 } 137 }) 138 } 139 140 NCURSES_EXPORT(int) 141 wscrl(WINDOW *win, int n) 142 { 143 T((T_CALLED("wscrl(%p,%d)"), (void *) win, n)); 144 145 if (!win || !win->_scroll) { 146 TR(TRACE_MOVE, ("...scrollok is false")); 147 returnCode(ERR); 148 } 149 150 if (n != 0) { 151 _nc_scroll_window(win, n, win->_regtop, win->_regbottom, win->_nc_bkgd); 152 _nc_synchook(win); 153 } 154 returnCode(OK); 155 } 156