xref: /illumos-gate/usr/src/lib/libcurses/screen/prefresh.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 /*LINTLIBRARY*/
41 
42 #include	<sys/types.h>
43 #include	<stdlib.h>
44 #include	"curses_inc.h"
45 
46 /*
47  * Pad refresh. These routines are provided for upward compatibility
48  * with the 'pad' structure of Sys V.2. Since windows now can be of
49  * arbitrary size and derived windows can be moved within their
50  * parent windows effortlessly, a separate notion of 'pad' as
51  * a larger-than-screen window is no longer necessary.
52  *
53  * pminy, pminx: the area (pminy, pminx, maxy, maxx) of pad is refreshed
54  * sminy, sminx, smaxy, smaxx: the screen area to be affected.
55  */
56 
57 int
prefresh(WINDOW * pad,int pminy,int pminx,int sminy,int sminx,int smaxy,int smaxx)58 prefresh(WINDOW *pad, int pminy, int pminx, int sminy,
59 	int sminx, int smaxy, int smaxx)
60 {
61 	return (_prefresh(wrefresh, pad, pminy, pminx, sminy,
62 	    sminx, smaxy, smaxx));
63 }
64 
65 int
_prefresh(int (* func)(WINDOW *),WINDOW * pad,int pminy,int pminx,int sminy,int sminx,int smaxy,int smaxx)66 _prefresh(int (*func)(WINDOW *), WINDOW *pad, int pminy, int pminx,
67 	int sminy, int sminx, int smaxy, int smaxx)
68 {
69 
70 	/*
71 	 * If pad->_padwin doesn't exist(meaning that this is
72 	 * the first call to p*refresh) create it as a derived window
73 	 */
74 
75 	if (!pad->_padwin) {
76 		if ((pad->_padwin = derwin(pad, pad->_maxy, pad->_maxx,
77 		    0, 0)) == NULL) {
78 			goto bad;
79 		}
80 	/* else  pad->_padwin->_use_idl = TRUE; */
81 
82 	/*
83 	 * The following makes parent window to run in fast mode
84 	 * by creating an illusion that it has no derived windows
85 	 */
86 
87 		pad->_ndescs--;
88 	}
89 
90 	if (_padjust(pad, pminy, pminx, sminy, sminx, smaxy, smaxx) == ERR)
91 bad:
92 		return (ERR);
93 
94 	(*func)(pad->_padwin);
95 	return (OK);
96 }
97 
98 int
_padjust(WINDOW * pad,int pminy,int pminx,int sminy,int sminx,int smaxy,int smaxx)99 _padjust(WINDOW *pad, int pminy, int pminx, int sminy,
100 	int sminx, int smaxy, int smaxx)
101 {
102 	short	prows, pcols, y;
103 	WINDOW	*padwin = pad->_padwin;
104 	chtype	**p_y, **o_y;
105 
106 	/* make sure the area requested to be updated is on the pad */
107 
108 	if ((pminy >= pad->_maxy) || (pminx >= pad->_maxx))
109 		return (ERR);
110 
111 	/* determine the area of the pad to be updated 	*/
112 
113 	if (pminy < 0)
114 		pminy = 0;
115 	if (pminx < 0)
116 		pminx = 0;
117 
118 	/* determine the screen area affected */
119 
120 	if (sminy < 0)
121 		sminy = 0;
122 	if (sminx < 0)
123 		sminx = 0;
124 	if (smaxy < sminy)
125 		smaxy = LINES - 1;
126 	if (smaxx < sminx)
127 		smaxx = COLS - 1;
128 
129 	/*
130 	 * Modify the area of the pad to be updated taking into
131 	 * consideration screen parameters.
132 	 */
133 
134 	if ((prows = (smaxy - sminy) + 1) > (y = pad->_maxy - pminy))
135 		prows = y;
136 	if ((pcols = (smaxx - sminx) + 1) > (y = pad->_maxx - pminx))
137 		pcols = y;
138 
139 	if (((padwin->_cury = pad->_cury - pminy) < 0) ||
140 	    (padwin->_cury >= prows))
141 		padwin->_cury = 0;
142 	if (((padwin->_curx = pad->_curx - pminx) < 0) ||
143 	    (padwin->_curx >= pcols))
144 		padwin->_curx = 0;
145 
146 	padwin->_leave = pad->_leave;
147 	padwin->_use_idl = pad->_use_idl;
148 
149 
150 	/*
151 	 * If padwin refers to the same derwin, return.  Otherwise
152 	 * update the coordinates and malloc'ed areas of the padwin
153 	 */
154 
155 	if ((padwin->_begy == sminy) && (padwin->_begx == sminx) &&
156 	    (padwin->_maxy == prows) && (padwin->_maxx == pcols) &&
157 	    (padwin->_y[0] == (pad->_y[pminy] + pminx)) &&
158 	    (!(pad->_flags & _WINSDEL))) {
159 		goto good;
160 	}
161 
162 	/* update the coordinates of the pad */
163 
164 	padwin->_maxy = prows;
165 	padwin->_maxx = pcols;
166 	/* LINTED */
167 	padwin->_begy = (short)sminy;
168 	/* LINTED */
169 	padwin->_begx = (short)sminx;
170 	/* LINTED */
171 	padwin->_pary = (short)pminy;
172 	/* LINTED */
173 	padwin->_parx = (short)pminx;
174 
175 	/* update the malloc'ed areas */
176 
177 	p_y = padwin->_y;
178 	o_y = pad->_y;
179 
180 	for (y = 0; y < prows; y++, pminy++)
181 		p_y[y] = o_y[pminy] + pminx;
182 
183 	(void) wtouchln(padwin, 0, prows, TRUE);
184 good:
185 	return (OK);
186 }
187