xref: /openbsd-src/lib/libcurses/base/lib_refresh.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /* $OpenBSD: lib_refresh.c,v 1.5 2010/01/12 23:22:06 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2006,2007 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  *     and: Thomas E. Dickey                        1996-on                 *
35  ****************************************************************************/
36 
37 /*
38  *	lib_refresh.c
39  *
40  *	The routines wrefresh() and wnoutrefresh().
41  *
42  */
43 
44 #include <curses.priv.h>
45 
46 MODULE_ID("$Id: lib_refresh.c,v 1.5 2010/01/12 23:22:06 nicm Exp $")
47 
48 NCURSES_EXPORT(int)
49 wrefresh(WINDOW *win)
50 {
51     int code;
52 
53     T((T_CALLED("wrefresh(%p)"), win));
54 
55     if (win == 0) {
56 	code = ERR;
57     } else if (win == curscr) {
58 	curscr->_clear = TRUE;
59 	code = doupdate();
60     } else if ((code = wnoutrefresh(win)) == OK) {
61 	if (win->_clear)
62 	    newscr->_clear = TRUE;
63 	code = doupdate();
64 	/*
65 	 * Reset the clearok() flag in case it was set for the special
66 	 * case in hardscroll.c (if we don't reset it here, we'll get 2
67 	 * refreshes because the flag is copied from stdscr to newscr).
68 	 * Resetting the flag shouldn't do any harm, anyway.
69 	 */
70 	win->_clear = FALSE;
71     }
72     returnCode(code);
73 }
74 
75 NCURSES_EXPORT(int)
76 wnoutrefresh(WINDOW *win)
77 {
78     NCURSES_SIZE_T limit_x;
79     NCURSES_SIZE_T src_row, src_col;
80     NCURSES_SIZE_T begx;
81     NCURSES_SIZE_T begy;
82     NCURSES_SIZE_T dst_row, dst_col;
83 #if USE_SCROLL_HINTS
84     bool wide;
85 #endif
86 
87     T((T_CALLED("wnoutrefresh(%p)"), win));
88 #ifdef TRACE
89     if (USE_TRACEF(TRACE_UPDATE)) {
90 	_tracedump("...win", win);
91 	_nc_unlock_global(tracef);
92     }
93 #endif /* TRACE */
94 
95     /*
96      * This function will break badly if we try to refresh a pad.
97      */
98     if ((win == 0)
99 	|| (win->_flags & _ISPAD))
100 	returnCode(ERR);
101 
102     /* put them here so "win == 0" won't break our code */
103     begx = win->_begx;
104     begy = win->_begy;
105 
106     newscr->_nc_bkgd = win->_nc_bkgd;
107     WINDOW_ATTRS(newscr) = WINDOW_ATTRS(win);
108 
109     /* merge in change information from all subwindows of this window */
110     wsyncdown(win);
111 
112 #if USE_SCROLL_HINTS
113     /*
114      * For pure efficiency, we'd want to transfer scrolling information
115      * from the window to newscr whenever the window is wide enough that
116      * its update will dominate the cost of the update for the horizontal
117      * band of newscr that it occupies.  Unfortunately, this threshold
118      * tends to be complex to estimate, and in any case scrolling the
119      * whole band and rewriting the parts outside win's image would look
120      * really ugly.  So.  What we do is consider the window "wide" if it
121      * either (a) occupies the whole width of newscr, or (b) occupies
122      * all but at most one column on either vertical edge of the screen
123      * (this caters to fussy people who put boxes around full-screen
124      * windows).  Note that changing this formula will not break any code,
125      * merely change the costs of various update cases.
126      */
127     wide = (begx <= 1 && win->_maxx >= (newscr->_maxx - 1));
128 #endif
129 
130     win->_flags &= ~_HASMOVED;
131 
132     /*
133      * Microtweaking alert!  This double loop is one of the genuine
134      * hot spots in the code.  Even gcc doesn't seem to do enough
135      * common-subexpression chunking to make it really tense,
136      * so we'll force the issue.
137      */
138 
139     /* limit(dst_col) */
140     limit_x = win->_maxx;
141     /* limit(src_col) */
142     if (limit_x > newscr->_maxx - begx)
143 	limit_x = newscr->_maxx - begx;
144 
145     for (src_row = 0, dst_row = begy + win->_yoffset;
146 	 src_row <= win->_maxy && dst_row <= newscr->_maxy;
147 	 src_row++, dst_row++) {
148 	register struct ldat *nline = &newscr->_line[dst_row];
149 	register struct ldat *oline = &win->_line[src_row];
150 
151 	if (oline->firstchar != _NOCHANGE) {
152 	    int last_src = oline->lastchar;
153 
154 	    if (last_src > limit_x)
155 		last_src = limit_x;
156 
157 	    src_col = oline->firstchar;
158 	    dst_col = src_col + begx;
159 
160 	    if_WIDEC({
161 		register int j;
162 
163 		/*
164 		 * Ensure that we will copy complete multi-column characters
165 		 * on the left-boundary.
166 		 */
167 		if (isWidecExt(oline->text[src_col])) {
168 		    j = 1 + dst_col - WidecExt(oline->text[src_col]);
169 		    if (j < 0)
170 			j = 0;
171 		    if (dst_col > j) {
172 			src_col -= (dst_col - j);
173 			dst_col = j;
174 		    }
175 		}
176 
177 		/*
178 		 * Ensure that we will copy complete multi-column characters
179 		 * on the right-boundary.
180 		 */
181 		j = last_src;
182 		if (WidecExt(oline->text[j])) {
183 		    ++j;
184 		    while (j <= limit_x) {
185 			if (isWidecBase(oline->text[j])) {
186 			    break;
187 			} else {
188 			    last_src = j;
189 			}
190 			++j;
191 		    }
192 		}
193 	    });
194 
195 	    if_WIDEC({
196 		static cchar_t blank = BLANK;
197 		int last_dst = begx + ((last_src < win->_maxx)
198 				       ? last_src
199 				       : win->_maxx);
200 		int fix_left = dst_col;
201 		int fix_right = last_dst;
202 		register int j;
203 
204 		/*
205 		 * Check for boundary cases where we may overwrite part of a
206 		 * multi-column character.  For those, wipe the remainder of
207 		 * the character to blanks.
208 		 */
209 		j = dst_col;
210 		if (isWidecExt(nline->text[j])) {
211 		    /*
212 		     * On the left, we only care about multi-column characters
213 		     * that extend into the changed region.
214 		     */
215 		    fix_left = 1 + j - WidecExt(nline->text[j]);
216 		    if (fix_left < 0)
217 			fix_left = 0;	/* only if cell is corrupt */
218 		}
219 
220 		j = last_dst;
221 		if (WidecExt(nline->text[j]) != 0) {
222 		    /*
223 		     * On the right, any multi-column character is a problem,
224 		     * unless it happens to be contained in the change, and
225 		     * ending at the right boundary of the change.  The
226 		     * computation for 'fix_left' accounts for the left-side of
227 		     * this character.  Find the end of the character.
228 		     */
229 		    ++j;
230 		    while (j <= newscr->_maxx && isWidecExt(nline->text[j])) {
231 			fix_right = j++;
232 		    }
233 		}
234 
235 		/*
236 		 * The analysis is simpler if we do the clearing afterwards.
237 		 * Do that now.
238 		 */
239 		if (fix_left < dst_col || fix_right > last_dst) {
240 		    for (j = fix_left; j <= fix_right; ++j) {
241 			nline->text[j] = blank;
242 			CHANGED_CELL(nline, j);
243 		    }
244 		}
245 	    });
246 
247 	    /*
248 	     * Copy the changed text.
249 	     */
250 	    for (; src_col <= last_src; src_col++, dst_col++) {
251 		if (!CharEq(oline->text[src_col], nline->text[dst_col])) {
252 		    nline->text[dst_col] = oline->text[src_col];
253 		    CHANGED_CELL(nline, dst_col);
254 		}
255 	    }
256 
257 	}
258 #if USE_SCROLL_HINTS
259 	if (wide) {
260 	    int oind = oline->oldindex;
261 
262 	    nline->oldindex = ((oind == _NEWINDEX)
263 			       ? _NEWINDEX
264 			       : (begy + oind + win->_yoffset));
265 	}
266 #endif /* USE_SCROLL_HINTS */
267 
268 	oline->firstchar = oline->lastchar = _NOCHANGE;
269 	if_USE_SCROLL_HINTS(oline->oldindex = src_row);
270     }
271 
272     if (win->_clear) {
273 	win->_clear = FALSE;
274 	newscr->_clear = TRUE;
275     }
276 
277     if (!win->_leaveok) {
278 	newscr->_cury = win->_cury + win->_begy + win->_yoffset;
279 	newscr->_curx = win->_curx + win->_begx;
280     }
281     newscr->_leaveok = win->_leaveok;
282 
283 #ifdef TRACE
284     if (USE_TRACEF(TRACE_UPDATE)) {
285 	_tracedump("newscr", newscr);
286 	_nc_unlock_global(tracef);
287     }
288 #endif /* TRACE */
289     returnCode(OK);
290 }
291