xref: /openbsd-src/lib/libcurses/base/lib_refresh.c (revision 1fe33145a0a9b1310a0413297a8deb871918b27f)
1 /*	$OpenBSD: lib_refresh.c,v 1.3 2000/06/19 03:53:44 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.25 2000/04/29 21:17:08 tom Exp $")
46 
47 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 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     bool wide;
81 
82     T((T_CALLED("wnoutrefresh(%p)"), win));
83 #ifdef TRACE
84     if (_nc_tracing & TRACE_UPDATE)
85 	_tracedump("...win", win);
86 #endif /* TRACE */
87 
88     /*
89      * This function will break badly if we try to refresh a pad.
90      */
91     if ((win == 0)
92 	|| (win->_flags & _ISPAD))
93 	returnCode(ERR);
94 
95     /* put them here so "win == 0" won't break our code */
96     begx = win->_begx;
97     begy = win->_begy;
98 
99     newscr->_bkgd = win->_bkgd;
100     newscr->_attrs = win->_attrs;
101 
102     /* merge in change information from all subwindows of this window */
103     wsyncdown(win);
104 
105     /*
106      * For pure efficiency, we'd want to transfer scrolling information
107      * from the window to newscr whenever the window is wide enough that
108      * its update will dominate the cost of the update for the horizontal
109      * band of newscr that it occupies.  Unfortunately, this threshold
110      * tends to be complex to estimate, and in any case scrolling the
111      * whole band and rewriting the parts outside win's image would look
112      * really ugly.  So.  What we do is consider the window "wide" if it
113      * either (a) occupies the whole width of newscr, or (b) occupies
114      * all but at most one column on either vertical edge of the screen
115      * (this caters to fussy people who put boxes around full-screen
116      * windows).  Note that changing this formula will not break any code,
117      * merely change the costs of various update cases.
118      */
119     wide = (begx <= 1 && win->_maxx >= (newscr->_maxx - 1));
120 
121     win->_flags &= ~_HASMOVED;
122 
123     /*
124      * Microtweaking alert!  This double loop is one of the genuine
125      * hot spots in the code.  Even gcc doesn't seem to do enough
126      * common-subexpression chunking to make it really tense,
127      * so we'll force the issue.
128      */
129 
130     /* limit(n) */
131     limit_x = win->_maxx;
132     /* limit(j) */
133     if (limit_x > win->_maxx)
134 	limit_x = win->_maxx;
135 
136     for (i = 0, m = begy + win->_yoffset;
137 	i <= win->_maxy && m <= newscr->_maxy;
138 	i++, m++) {
139 	register struct ldat *nline = &newscr->_line[m];
140 	register struct ldat *oline = &win->_line[i];
141 
142 	if (oline->firstchar != _NOCHANGE) {
143 	    int last = oline->lastchar;
144 
145 	    if (last > limit_x)
146 		last = limit_x;
147 
148 	    for (j = oline->firstchar, n = j + begx; j <= last; j++, n++) {
149 		if (oline->text[j] != nline->text[n]) {
150 		    nline->text[n] = oline->text[j];
151 		    CHANGED_CELL(nline, n);
152 		}
153 	    }
154 
155 	}
156 #if USE_SCROLL_HINTS
157 	if (wide) {
158 	    int oind = oline->oldindex;
159 
160 	    nline->oldindex = (oind == _NEWINDEX) ? _NEWINDEX : begy + oind
161 		+ win->_yoffset;
162 	}
163 #endif /* USE_SCROLL_HINTS */
164 
165 	oline->firstchar = oline->lastchar = _NOCHANGE;
166 	if_USE_SCROLL_HINTS(oline->oldindex = i);
167     }
168 
169     if (win->_clear) {
170 	win->_clear = FALSE;
171 	newscr->_clear = TRUE;
172     }
173 
174     if (!win->_leaveok) {
175 	newscr->_cury = win->_cury + win->_begy + win->_yoffset;
176 	newscr->_curx = win->_curx + win->_begx;
177     }
178     newscr->_leaveok = win->_leaveok;
179 
180 #ifdef TRACE
181     if (_nc_tracing & TRACE_UPDATE)
182 	_tracedump("newscr", newscr);
183 #endif /* TRACE */
184     returnCode(OK);
185 }
186