xref: /openbsd-src/lib/libcurses/base/lib_refresh.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: lib_refresh.c,v 1.4 2001/01/22 18:01:43 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  *	lib_refresh.c
38  *
39  *	The routines wrefresh() and wnoutrefresh().
40  *
41  */
42 
43 #include <curses.priv.h>
44 
45 MODULE_ID("$From: lib_refresh.c,v 1.28 2000/12/10 02:43:27 tom Exp $")
46 
47 NCURSES_EXPORT(int)
48 wrefresh(WINDOW *win)
49 {
50     int code;
51 
52     T((T_CALLED("wrefresh(%p)"), win));
53 
54     if (win == curscr) {
55 	curscr->_clear = TRUE;
56 	code = doupdate();
57     } else if ((code = wnoutrefresh(win)) == OK) {
58 	if (win->_clear)
59 	    newscr->_clear = TRUE;
60 	code = doupdate();
61 	/*
62 	 * Reset the clearok() flag in case it was set for the special
63 	 * case in hardscroll.c (if we don't reset it here, we'll get 2
64 	 * refreshes because the flag is copied from stdscr to newscr).
65 	 * Resetting the flag shouldn't do any harm, anyway.
66 	 */
67 	win->_clear = FALSE;
68     }
69     returnCode(code);
70 }
71 
72 NCURSES_EXPORT(int)
73 wnoutrefresh(WINDOW *win)
74 {
75     NCURSES_SIZE_T limit_x;
76     NCURSES_SIZE_T i, j;
77     NCURSES_SIZE_T begx;
78     NCURSES_SIZE_T begy;
79     NCURSES_SIZE_T m, n;
80 #if USE_SCROLL_HINTS
81     bool wide;
82 #endif
83 
84     T((T_CALLED("wnoutrefresh(%p)"), win));
85 #ifdef TRACE
86     if (_nc_tracing & TRACE_UPDATE)
87 	_tracedump("...win", win);
88 #endif /* TRACE */
89 
90     /*
91      * This function will break badly if we try to refresh a pad.
92      */
93     if ((win == 0)
94 	|| (win->_flags & _ISPAD))
95 	returnCode(ERR);
96 
97     /* put them here so "win == 0" won't break our code */
98     begx = win->_begx;
99     begy = win->_begy;
100 
101     newscr->_bkgd = win->_bkgd;
102     newscr->_attrs = win->_attrs;
103 
104     /* merge in change information from all subwindows of this window */
105     wsyncdown(win);
106 
107 #if USE_SCROLL_HINTS
108     /*
109      * For pure efficiency, we'd want to transfer scrolling information
110      * from the window to newscr whenever the window is wide enough that
111      * its update will dominate the cost of the update for the horizontal
112      * band of newscr that it occupies.  Unfortunately, this threshold
113      * tends to be complex to estimate, and in any case scrolling the
114      * whole band and rewriting the parts outside win's image would look
115      * really ugly.  So.  What we do is consider the window "wide" if it
116      * either (a) occupies the whole width of newscr, or (b) occupies
117      * all but at most one column on either vertical edge of the screen
118      * (this caters to fussy people who put boxes around full-screen
119      * windows).  Note that changing this formula will not break any code,
120      * merely change the costs of various update cases.
121      */
122     wide = (begx <= 1 && win->_maxx >= (newscr->_maxx - 1));
123 #endif
124 
125     win->_flags &= ~_HASMOVED;
126 
127     /*
128      * Microtweaking alert!  This double loop is one of the genuine
129      * hot spots in the code.  Even gcc doesn't seem to do enough
130      * common-subexpression chunking to make it really tense,
131      * so we'll force the issue.
132      */
133 
134     /* limit(n) */
135     limit_x = win->_maxx;
136     /* limit(j) */
137     if (limit_x > win->_maxx)
138 	limit_x = win->_maxx;
139 
140     for (i = 0, m = begy + win->_yoffset;
141 	 i <= win->_maxy && m <= newscr->_maxy;
142 	 i++, m++) {
143 	register struct ldat *nline = &newscr->_line[m];
144 	register struct ldat *oline = &win->_line[i];
145 
146 	if (oline->firstchar != _NOCHANGE) {
147 	    int last = oline->lastchar;
148 
149 	    if (last > limit_x)
150 		last = limit_x;
151 
152 	    for (j = oline->firstchar, n = j + begx; j <= last; j++, n++) {
153 		if (oline->text[j] != nline->text[n]) {
154 		    nline->text[n] = oline->text[j];
155 		    CHANGED_CELL(nline, n);
156 		}
157 	    }
158 
159 	}
160 #if USE_SCROLL_HINTS
161 	if (wide) {
162 	    int oind = oline->oldindex;
163 
164 	    nline->oldindex = (oind == _NEWINDEX) ? _NEWINDEX : begy + oind
165 		+ win->_yoffset;
166 	}
167 #endif /* USE_SCROLL_HINTS */
168 
169 	oline->firstchar = oline->lastchar = _NOCHANGE;
170 	if_USE_SCROLL_HINTS(oline->oldindex = i);
171     }
172 
173     if (win->_clear) {
174 	win->_clear = FALSE;
175 	newscr->_clear = TRUE;
176     }
177 
178     if (!win->_leaveok) {
179 	newscr->_cury = win->_cury + win->_begy + win->_yoffset;
180 	newscr->_curx = win->_curx + win->_begx;
181     }
182     newscr->_leaveok = win->_leaveok;
183 
184 #ifdef TRACE
185     if (_nc_tracing & TRACE_UPDATE)
186 	_tracedump("newscr", newscr);
187 #endif /* TRACE */
188     returnCode(OK);
189 }
190