1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate /*
43*7c478bd9Sstevel@tonic-gate  * This routine writes parts of Srcwin onto Dstwin,
44*7c478bd9Sstevel@tonic-gate  * either non-destructively (over_lay = TRUE) or destructively
45*7c478bd9Sstevel@tonic-gate  * (over_lay = FALSE).
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #include	<string.h>
49*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
50*7c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate int
copywin(WINDOW * Srcwin,WINDOW * Dstwin,int minRowSrc,int minColSrc,int minRowDst,int minColDst,int maxRowDst,int maxColDst,int over_lay)53*7c478bd9Sstevel@tonic-gate copywin(WINDOW *Srcwin, WINDOW *Dstwin,
54*7c478bd9Sstevel@tonic-gate 	int minRowSrc, int minColSrc, int minRowDst,
55*7c478bd9Sstevel@tonic-gate 	int minColDst, int maxRowDst, int maxColDst,
56*7c478bd9Sstevel@tonic-gate 	int over_lay)
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	int		ySrc, yDst, which_copy, t;
59*7c478bd9Sstevel@tonic-gate 	int		height = (maxRowDst - minRowDst) + 1,
60*7c478bd9Sstevel@tonic-gate 			width = (maxColDst - minColDst) + 1;
61*7c478bd9Sstevel@tonic-gate 	chtype		**_yDst = Dstwin->_y, **_ySrc = Srcwin->_y,
62*7c478bd9Sstevel@tonic-gate 			bkSrc = Srcwin->_bkgd, atDst = Dstwin->_attrs,
63*7c478bd9Sstevel@tonic-gate 			*spSrc, *spDst, *epSrc, *epDst, *savepS,
64*7c478bd9Sstevel@tonic-gate 			*savepD, width_bytes, numcopied;
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #ifdef	DEBUG
67*7c478bd9Sstevel@tonic-gate 	if (outf)
68*7c478bd9Sstevel@tonic-gate 		fprintf(outf, "copywin(%0.2o, %0.2o);\n", Srcwin, Dstwin);
69*7c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate 	/*
72*7c478bd9Sstevel@tonic-gate 	 * If we are going to be copying from curscr,
73*7c478bd9Sstevel@tonic-gate 	 * first offset into curscr the offset the Dstwin knows about.
74*7c478bd9Sstevel@tonic-gate 	 */
75*7c478bd9Sstevel@tonic-gate 	if (Srcwin == curscr)
76*7c478bd9Sstevel@tonic-gate 		minRowSrc += Dstwin->_yoffset;
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	/*
79*7c478bd9Sstevel@tonic-gate 	 * There are three types of copy.
80*7c478bd9Sstevel@tonic-gate 	 * 0 - Straight memcpy allowed
81*7c478bd9Sstevel@tonic-gate 	 * 1 - We have to first check to see if the source character is a blank
82*7c478bd9Sstevel@tonic-gate 	 * 2 - Dstwin has attributes or bkgd that must changed
83*7c478bd9Sstevel@tonic-gate 	 * on a char-by-char basis.
84*7c478bd9Sstevel@tonic-gate 	 */
85*7c478bd9Sstevel@tonic-gate 	if ((which_copy = (over_lay) ? 1 :
86*7c478bd9Sstevel@tonic-gate 	    (2 * ((Dstwin->_attrs != A_NORMAL) ||
87*7c478bd9Sstevel@tonic-gate 	    (Dstwin->_bkgd != _BLNKCHAR)))) == 0)
88*7c478bd9Sstevel@tonic-gate 		width_bytes = width * (int)sizeof (chtype);
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	/* for each Row */
91*7c478bd9Sstevel@tonic-gate 	for (ySrc = minRowSrc, yDst = minRowDst; height-- > 0; ySrc++, yDst++) {
92*7c478bd9Sstevel@tonic-gate 		if (which_copy) {
93*7c478bd9Sstevel@tonic-gate 			spSrc = &_ySrc[ySrc][minColSrc];
94*7c478bd9Sstevel@tonic-gate 			spDst = &_yDst[yDst][minColDst];
95*7c478bd9Sstevel@tonic-gate 			numcopied = width;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 			epSrc = savepS = &_ySrc[ySrc][maxColDst];
98*7c478bd9Sstevel@tonic-gate 			epDst = savepD = &_yDst[yDst][maxColDst];
99*7c478bd9Sstevel@tonic-gate 		/* only copy into an area bounded by whole characters */
100*7c478bd9Sstevel@tonic-gate 			for (; spDst <= epDst; spSrc++, spDst++)
101*7c478bd9Sstevel@tonic-gate 				if (!ISCBIT(*spDst))
102*7c478bd9Sstevel@tonic-gate 					break;
103*7c478bd9Sstevel@tonic-gate 			if (spDst > epDst)
104*7c478bd9Sstevel@tonic-gate 				continue;
105*7c478bd9Sstevel@tonic-gate 			for (; epDst >= spDst; --epDst, --epSrc)
106*7c478bd9Sstevel@tonic-gate 				if (!ISCBIT(*epDst))
107*7c478bd9Sstevel@tonic-gate 					break;
108*7c478bd9Sstevel@tonic-gate 			t = _curs_scrwidth[TYPE(RBYTE(*epDst))] - 1;
109*7c478bd9Sstevel@tonic-gate 			if (epDst+t <= savepD)
110*7c478bd9Sstevel@tonic-gate 				epDst += t, epSrc += t;
111*7c478bd9Sstevel@tonic-gate 			else
112*7c478bd9Sstevel@tonic-gate 				epDst -= 1, epSrc -= 1;
113*7c478bd9Sstevel@tonic-gate 			if (epDst < spDst)
114*7c478bd9Sstevel@tonic-gate 				continue;
115*7c478bd9Sstevel@tonic-gate 			/* don't copy partial characters */
116*7c478bd9Sstevel@tonic-gate 			for (; spSrc <= epSrc; ++spSrc, ++spDst)
117*7c478bd9Sstevel@tonic-gate 				if (!ISCBIT(*spSrc))
118*7c478bd9Sstevel@tonic-gate 					break;
119*7c478bd9Sstevel@tonic-gate 			if (spSrc > epSrc)
120*7c478bd9Sstevel@tonic-gate 				continue;
121*7c478bd9Sstevel@tonic-gate 			for (; epSrc >= spSrc; --epSrc, --epDst)
122*7c478bd9Sstevel@tonic-gate 				if (!ISCBIT(*epSrc))
123*7c478bd9Sstevel@tonic-gate 					break;
124*7c478bd9Sstevel@tonic-gate 			t = _curs_scrwidth[TYPE(RBYTE(*epSrc))] - 1;
125*7c478bd9Sstevel@tonic-gate 			if (epSrc+t <= savepS)
126*7c478bd9Sstevel@tonic-gate 				epSrc += t, epDst += t;
127*7c478bd9Sstevel@tonic-gate 			else
128*7c478bd9Sstevel@tonic-gate 				epSrc -= 1, epDst -= 1;
129*7c478bd9Sstevel@tonic-gate 			if (epSrc < spSrc)
130*7c478bd9Sstevel@tonic-gate 				continue;
131*7c478bd9Sstevel@tonic-gate 		/* make sure that the copied-to place is clean */
132*7c478bd9Sstevel@tonic-gate 			if (ISCBIT(*spDst))
133*7c478bd9Sstevel@tonic-gate 				(void) _mbclrch(Dstwin, minRowDst,
134*7c478bd9Sstevel@tonic-gate 				    /*LINTED*/
135*7c478bd9Sstevel@tonic-gate 				    (intptr_t)(spDst - *_yDst[yDst]));
136*7c478bd9Sstevel@tonic-gate 			if (ISCBIT(*epDst))
137*7c478bd9Sstevel@tonic-gate 				(void) _mbclrch(Dstwin, minRowDst,
138*7c478bd9Sstevel@tonic-gate 				    /*LINTED*/
139*7c478bd9Sstevel@tonic-gate 				    (intptr_t)(epDst - *_yDst[yDst]));
140*7c478bd9Sstevel@tonic-gate 			/*LINTED*/
141*7c478bd9Sstevel@tonic-gate 			numcopied = (chtype) (epDst - spDst + 1);
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 			if (which_copy == 1) {		/* overlay */
144*7c478bd9Sstevel@tonic-gate 				for (; numcopied-- > 0; spSrc++, spDst++)
145*7c478bd9Sstevel@tonic-gate 			/* Check to see if the char is a "blank/bkgd". */
146*7c478bd9Sstevel@tonic-gate 					if (*spSrc != bkSrc)
147*7c478bd9Sstevel@tonic-gate 						*spDst = *spSrc | atDst;
148*7c478bd9Sstevel@tonic-gate 			} else {
149*7c478bd9Sstevel@tonic-gate 				for (; numcopied-- > 0; spSrc++, spDst++)
150*7c478bd9Sstevel@tonic-gate 					*spDst = *spSrc | atDst;
151*7c478bd9Sstevel@tonic-gate 			}
152*7c478bd9Sstevel@tonic-gate 		} else {
153*7c478bd9Sstevel@tonic-gate 			/* ... copy all chtypes */
154*7c478bd9Sstevel@tonic-gate 			(void) memcpy((char *)&_yDst[yDst][minColDst],
155*7c478bd9Sstevel@tonic-gate 			    (char *)&_ySrc[ySrc][minColSrc], width_bytes);
156*7c478bd9Sstevel@tonic-gate 		}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 		/* note that the line has changed */
159*7c478bd9Sstevel@tonic-gate 		if (minColDst < Dstwin->_firstch[yDst])
160*7c478bd9Sstevel@tonic-gate 			/*LINTED*/
161*7c478bd9Sstevel@tonic-gate 			Dstwin->_firstch[yDst] = (short)minColDst;
162*7c478bd9Sstevel@tonic-gate 		if (maxColDst > Dstwin->_lastch[yDst])
163*7c478bd9Sstevel@tonic-gate 			/*LINTED*/
164*7c478bd9Sstevel@tonic-gate 			Dstwin->_lastch[yDst] = (short)maxColDst;
165*7c478bd9Sstevel@tonic-gate 	}
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate #ifdef	_VR3_COMPAT_CODE
168*7c478bd9Sstevel@tonic-gate 	if (_y16update) {
169*7c478bd9Sstevel@tonic-gate 		(*_y16update)(Dstwin, (maxRowDst - minRowDst) + 1,
170*7c478bd9Sstevel@tonic-gate 		    (maxColDst - minColDst) + 1, minRowDst, minColDst);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate #endif	/* _VR3_COMPAT_CODE */
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	/* note that something in Dstwin has changed */
175*7c478bd9Sstevel@tonic-gate 	Dstwin->_flags |= _WINCHANGED;
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	if (Dstwin->_sync)
178*7c478bd9Sstevel@tonic-gate 		wsyncup(Dstwin);
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	return (Dstwin->_immed ? wrefresh(Dstwin) : OK);
181*7c478bd9Sstevel@tonic-gate }
182