xref: /illumos-gate/usr/src/lib/libcurses/screen/wnoutref.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include	<string.h>
45 #include	<sys/types.h>
46 #include	"curses_inc.h"
47 
48 /* Like refresh but does not output */
49 
50 int
51 wnoutrefresh(WINDOW *win)
52 {
53 	short	*bch, *ech, *sbch, *sech;
54 	chtype	**wcp, **scp, *wc, *sc;
55 	int	*hash;
56 	short	y, x, xorg, yorg, scrli, scrco,
57 		boty, sminy, smaxy, minx, maxx, lo, hi;
58 	bool	doall;
59 
60 	if (win->_parent)
61 		wsyncdown(win);
62 
63 	doall = win->_clear;
64 
65 	sminy = SP->Yabove;
66 	smaxy = sminy + LINES;
67 	scrli = curscr->_maxy;
68 	scrco = curscr->_maxx;
69 
70 	yorg = win->_begy + win->_yoffset;
71 	xorg = win->_begx;
72 
73 	/* save flags, cursor positions */
74 	SP->virt_scr->_leave = win->_leave;
75 	if ((!win->_leave && (win->_flags & (_WINCHANGED | _WINMOVED))) &&
76 	    ((y = win->_cury + yorg) >= 0) && (y < scrli) &&
77 	    ((x = win->_curx + xorg) >= 0) && (x < scrco)) {
78 		_virtscr->_cury = y;
79 		_virtscr->_curx = x;
80 	}
81 	if (!(win->_use_idc))
82 		_virtscr->_use_idc = FALSE;
83 	if (win->_use_idl)
84 		_virtscr->_use_idl = TRUE;
85 	if (win->_clear) {
86 		_virtscr->_clear = TRUE;
87 		win->_clear = FALSE;
88 		win->_flags |= _WINCHANGED;
89 	}
90 
91 	if (!(win->_flags & _WINCHANGED))
92 		goto done;
93 
94 	/* region to update */
95 	boty = win->_maxy+yorg;
96 	if (yorg >= sminy && yorg < smaxy && boty >= smaxy)
97 		boty = smaxy;
98 	else
99 		if (boty > scrli)
100 			boty = scrli;
101 	boty -= yorg;
102 
103 	minx = 0;
104 	if ((maxx = win->_maxx+xorg) > scrco)
105 		maxx = scrco;
106 	maxx -= xorg + 1;
107 
108 	/* update structure */
109 	bch = win->_firstch;
110 	ech = win->_lastch;
111 	wcp = win->_y;
112 
113 	hash = _VIRTHASH + yorg;
114 	sbch = _virtscr->_firstch + yorg;
115 	sech = _virtscr->_lastch + yorg;
116 	scp  = _virtscr->_y + yorg;
117 
118 	/* first time around, set proper top/bottom changed lines */
119 	if (curscr->_sync) {
120 		_VIRTTOP = scrli;
121 		_VIRTBOT = -1;
122 	}
123 
124 	/* update each line */
125 	for (y = 0; y < boty; ++y, ++hash, ++bch, ++ech, ++sbch,
126 	    ++sech, ++wcp, ++scp) {
127 		if (!doall && *bch == _INFINITY)
128 			continue;
129 
130 		lo = (doall || *bch == _REDRAW || *bch < minx) ? minx : *bch;
131 		hi = (doall || *bch == _REDRAW || *ech > maxx) ? maxx : *ech;
132 
133 		wc = *wcp;
134 		sc = *scp;
135 		/* adjust lo and hi so they contain whole characters */
136 		if (_scrmax > 1) {
137 			if (ISCBIT(wc[lo])) {
138 				for (x = lo - 1; x >= minx; --x)
139 					if (!ISCBIT(wc[x]))
140 						break;
141 					if (x < minx) {
142 						for (x = lo+1; x <= maxx; ++x)
143 							if (!ISCBIT(wc[x]))
144 								break;
145 						if (x > maxx)
146 							goto nextline;
147 					}
148 				lo = x;
149 			}
150 			if (ISMBIT(wc[hi])) {
151 				int		w;
152 				unsigned char	rb;
153 				for (x = hi; x >= lo; --x)
154 					if (!ISCBIT(wc[x]))
155 						break;
156 				/* LINTED */
157 				rb = (unsigned char) RBYTE(wc[x]);
158 				w = _curs_scrwidth[TYPE(rb)];
159 				hi = (x+w) <= maxx+1 ? x+w-1 : x;
160 			}
161 		}
162 
163 		if (hi < lo)
164 			continue;
165 
166 		/* clear partial multi-chars about to be overwritten */
167 		if (_scrmax > 1) {
168 			if (ISMBIT(sc[lo + xorg]))
169 				(void) _mbclrch(_virtscr, y + yorg, lo + xorg);
170 			if (ISMBIT(sc[hi + xorg]))
171 				(void) _mbclrch(_virtscr, y + yorg, hi + xorg);
172 		}
173 
174 		/* update the change structure */
175 		if (*bch == _REDRAW || *sbch == _REDRAW)
176 			*sbch =  _REDRAW;
177 		else {
178 			if (*sbch > lo+xorg)
179 				*sbch = lo+xorg;
180 			if (*sech < hi+xorg)
181 				*sech = hi+xorg;
182 		}
183 		if ((y + yorg) < _VIRTTOP)
184 			_VIRTTOP = y+yorg;
185 		if ((y + yorg) > _VIRTBOT)
186 			_VIRTBOT = y + yorg;
187 
188 		/* update the image */
189 		wc = *wcp + lo;
190 		sc = *scp + lo + xorg;
191 		(void) memcpy((char *) sc, (char *) wc, (size_t)
192 		    (((hi - lo) + 1) * sizeof (chtype)));
193 
194 		/* the hash value of the line */
195 		*hash = _NOHASH;
196 
197 nextline:
198 		*bch = _INFINITY;
199 		*ech = -1;
200 	}
201 
202 done:
203 	_virtscr->_flags |= _WINCHANGED;
204 	win->_flags &= ~(_WINCHANGED | _WINMOVED | _WINSDEL);
205 	return (OK);
206 }
207