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