xref: /onnv-gate/usr/src/ucblib/libcurses/refresh.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
11*0Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
12*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
13*0Sstevel@tonic-gate  */
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
16*0Sstevel@tonic-gate 
17*0Sstevel@tonic-gate /*LINTLIBRARY*/
18*0Sstevel@tonic-gate 
19*0Sstevel@tonic-gate #ifndef lint
20*0Sstevel@tonic-gate static char
21*0Sstevel@tonic-gate sccsid[] = "@(#)refresh.c 1.8 89/08/24 SMI"; /* from UCB 5.1 85/06/07 */
22*0Sstevel@tonic-gate #endif /* not lint */
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate  * make the current screen look like "win" over the area coverd by
26*0Sstevel@tonic-gate  * win.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include	"curses.ext"
30*0Sstevel@tonic-gate #include	<term.h>
31*0Sstevel@tonic-gate #include	<string.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #ifdef DEBUG
34*0Sstevel@tonic-gate #define	DEBUGSTATIC
35*0Sstevel@tonic-gate #else
36*0Sstevel@tonic-gate #define	DEBUGSTATIC	static
37*0Sstevel@tonic-gate #endif
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate DEBUGSTATIC short	ly, lx;
40*0Sstevel@tonic-gate DEBUGSTATIC bool	curwin;
41*0Sstevel@tonic-gate WINDOW	*_win = NULL;
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate /* forward declarations */
44*0Sstevel@tonic-gate DEBUGSTATIC void domvcur(int, int, int, int);
45*0Sstevel@tonic-gate DEBUGSTATIC int makech(WINDOW *, short);
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate int
wrefresh(WINDOW * win)48*0Sstevel@tonic-gate wrefresh(WINDOW *win)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate 	short	wy;
51*0Sstevel@tonic-gate 	int	retval;
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate 	/*
54*0Sstevel@tonic-gate 	 * make sure were in visual state
55*0Sstevel@tonic-gate 	 */
56*0Sstevel@tonic-gate 	if (_endwin) {
57*0Sstevel@tonic-gate 		(void) _puts(VS);
58*0Sstevel@tonic-gate 		(void) _puts(TI);
59*0Sstevel@tonic-gate 		_endwin = FALSE;
60*0Sstevel@tonic-gate 	}
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	/*
63*0Sstevel@tonic-gate 	 * initialize loop parameters
64*0Sstevel@tonic-gate 	 */
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate 	ly = curscr->_cury;
67*0Sstevel@tonic-gate 	lx = curscr->_curx;
68*0Sstevel@tonic-gate 	_win = win;
69*0Sstevel@tonic-gate 	curwin = (win == curscr);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	if (win->_clear || curscr->_clear || curwin) {
72*0Sstevel@tonic-gate 		if ((win->_flags & _FULLWIN) || curscr->_clear) {
73*0Sstevel@tonic-gate 			(void) _puts(CL);
74*0Sstevel@tonic-gate 			ly = 0;
75*0Sstevel@tonic-gate 			lx = 0;
76*0Sstevel@tonic-gate 			if (!curwin) {
77*0Sstevel@tonic-gate 				curscr->_clear = FALSE;
78*0Sstevel@tonic-gate 				curscr->_cury = 0;
79*0Sstevel@tonic-gate 				curscr->_curx = 0;
80*0Sstevel@tonic-gate 				(void) werase(curscr);
81*0Sstevel@tonic-gate 			}
82*0Sstevel@tonic-gate 			(void) touchwin(win);
83*0Sstevel@tonic-gate 		}
84*0Sstevel@tonic-gate 		win->_clear = FALSE;
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 	if (!CA) {
87*0Sstevel@tonic-gate 		if (win->_curx != 0)
88*0Sstevel@tonic-gate 			(void) _putchar('\n');
89*0Sstevel@tonic-gate 		if (!curwin)
90*0Sstevel@tonic-gate 			(void) werase(curscr);
91*0Sstevel@tonic-gate 	}
92*0Sstevel@tonic-gate #ifdef DEBUG
93*0Sstevel@tonic-gate 	fprintf(outf, "REFRESH(%0.2o): curwin = %d\n", win, curwin);
94*0Sstevel@tonic-gate 	fprintf(outf, "REFRESH:\n\tfirstch\tlastch\n");
95*0Sstevel@tonic-gate #endif
96*0Sstevel@tonic-gate 	for (wy = 0; wy < win->_maxy; wy++) {
97*0Sstevel@tonic-gate #ifdef DEBUG
98*0Sstevel@tonic-gate 		fprintf(outf, "%d\t%d\t%d\n", wy, win->_firstch[wy],
99*0Sstevel@tonic-gate 		    win->_lastch[wy]);
100*0Sstevel@tonic-gate #endif
101*0Sstevel@tonic-gate 		if (win->_firstch[wy] != _NOCHANGE)
102*0Sstevel@tonic-gate 			if (makech(win, wy) == ERR)
103*0Sstevel@tonic-gate 				return (ERR);
104*0Sstevel@tonic-gate 			else {
105*0Sstevel@tonic-gate 				if (win->_firstch[wy] >= win->_ch_off)
106*0Sstevel@tonic-gate 					win->_firstch[wy] = win->_maxx +
107*0Sstevel@tonic-gate 							    win->_ch_off;
108*0Sstevel@tonic-gate 				if (win->_lastch[wy] < win->_maxx +
109*0Sstevel@tonic-gate 				    win->_ch_off)
110*0Sstevel@tonic-gate 					win->_lastch[wy] = win->_ch_off;
111*0Sstevel@tonic-gate 				if (win->_lastch[wy] < win->_firstch[wy])
112*0Sstevel@tonic-gate 					win->_firstch[wy] = _NOCHANGE;
113*0Sstevel@tonic-gate 			}
114*0Sstevel@tonic-gate #ifdef DEBUG
115*0Sstevel@tonic-gate 		fprintf(outf, "\t%d\t%d\n", win->_firstch[wy],
116*0Sstevel@tonic-gate 		    win->_lastch[wy]);
117*0Sstevel@tonic-gate #endif
118*0Sstevel@tonic-gate 	}
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	if (win == curscr)
121*0Sstevel@tonic-gate 		domvcur(ly, lx, win->_cury, win->_curx);
122*0Sstevel@tonic-gate 	else {
123*0Sstevel@tonic-gate 		if (win->_leave) {
124*0Sstevel@tonic-gate 			curscr->_cury = ly;
125*0Sstevel@tonic-gate 			curscr->_curx = lx;
126*0Sstevel@tonic-gate 			ly -= win->_begy;
127*0Sstevel@tonic-gate 			lx -= win->_begx;
128*0Sstevel@tonic-gate 			if (ly >= 0 && ly < win->_maxy && lx >= 0 &&
129*0Sstevel@tonic-gate 			    lx < win->_maxx) {
130*0Sstevel@tonic-gate 				win->_cury = ly;
131*0Sstevel@tonic-gate 				win->_curx = lx;
132*0Sstevel@tonic-gate 			}
133*0Sstevel@tonic-gate 			else
134*0Sstevel@tonic-gate 				win->_cury = win->_curx = 0;
135*0Sstevel@tonic-gate 		} else {
136*0Sstevel@tonic-gate 			domvcur(ly, lx, win->_cury + win->_begy,
137*0Sstevel@tonic-gate 				win->_curx + win->_begx);
138*0Sstevel@tonic-gate 			curscr->_cury = win->_cury + win->_begy;
139*0Sstevel@tonic-gate 			curscr->_curx = win->_curx + win->_begx;
140*0Sstevel@tonic-gate 		}
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 	retval = OK;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	_win = NULL;
145*0Sstevel@tonic-gate 	(void) fflush(stdout);
146*0Sstevel@tonic-gate 	return (retval);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate /*
150*0Sstevel@tonic-gate  * make a change on the screen
151*0Sstevel@tonic-gate  */
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate DEBUGSTATIC int
makech(WINDOW * win,short wy)154*0Sstevel@tonic-gate makech(WINDOW *win, short wy)
155*0Sstevel@tonic-gate {
156*0Sstevel@tonic-gate 	char	*nsp, *csp, *ce;
157*0Sstevel@tonic-gate 	short	wx, lch, y;
158*0Sstevel@tonic-gate 	intptr_t	nlsp, clsp;	/* last space in lines		*/
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	wx = win->_firstch[wy] - win->_ch_off;
161*0Sstevel@tonic-gate 	if (wx >= win->_maxx)
162*0Sstevel@tonic-gate 		return (OK);
163*0Sstevel@tonic-gate 	else if (wx < 0)
164*0Sstevel@tonic-gate 		wx = 0;
165*0Sstevel@tonic-gate 	lch = win->_lastch[wy] - win->_ch_off;
166*0Sstevel@tonic-gate 	if (lch < 0)
167*0Sstevel@tonic-gate 		return (OK);
168*0Sstevel@tonic-gate 	else if (lch >= win->_maxx)
169*0Sstevel@tonic-gate 		lch = win->_maxx - 1;
170*0Sstevel@tonic-gate 	y = wy + win->_begy;
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	if (curwin)
173*0Sstevel@tonic-gate 		csp = " ";
174*0Sstevel@tonic-gate 	else
175*0Sstevel@tonic-gate 		csp = &curscr->_y[wy + win->_begy][wx + win->_begx];
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	nsp = &win->_y[wy][wx];
178*0Sstevel@tonic-gate 	if (CE && !curwin) {
179*0Sstevel@tonic-gate 		for (ce = &win->_y[wy][win->_maxx - 1]; *ce == ' '; ce--)
180*0Sstevel@tonic-gate 			if (ce <= win->_y[wy])
181*0Sstevel@tonic-gate 				break;
182*0Sstevel@tonic-gate 		nlsp = ce - win->_y[wy];
183*0Sstevel@tonic-gate 	}
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	if (!curwin)
186*0Sstevel@tonic-gate 		ce = CE;
187*0Sstevel@tonic-gate 	else
188*0Sstevel@tonic-gate 		ce = NULL;
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	while (wx <= lch) {
191*0Sstevel@tonic-gate 		if (*nsp != *csp) {
192*0Sstevel@tonic-gate 			domvcur(ly, lx, y, wx + win->_begx);
193*0Sstevel@tonic-gate #ifdef DEBUG
194*0Sstevel@tonic-gate 			fprintf(outf, "MAKECH: 1: wx = %d, lx = %d\n", wx, lx);
195*0Sstevel@tonic-gate #endif
196*0Sstevel@tonic-gate 			ly = y;
197*0Sstevel@tonic-gate 			lx = wx + win->_begx;
198*0Sstevel@tonic-gate 			while (wx <= lch && *nsp != *csp) {
199*0Sstevel@tonic-gate 				if (ce != NULL && wx >= nlsp && *nsp == ' ') {
200*0Sstevel@tonic-gate 					/*
201*0Sstevel@tonic-gate 					 * check for clear to end-of-line
202*0Sstevel@tonic-gate 					 */
203*0Sstevel@tonic-gate 					ce = &curscr->_y[ly][COLS - 1];
204*0Sstevel@tonic-gate 					while (*ce == ' ')
205*0Sstevel@tonic-gate 						if (ce-- <= csp)
206*0Sstevel@tonic-gate 							break;
207*0Sstevel@tonic-gate 					clsp = ce - curscr->_y[ly] - win->_begx;
208*0Sstevel@tonic-gate #ifdef DEBUG
209*0Sstevel@tonic-gate 					fprintf(outf, "MAKECH: clsp = %d,"
210*0Sstevel@tonic-gate 					    " nlsp = %d\n", clsp, nlsp);
211*0Sstevel@tonic-gate #endif
212*0Sstevel@tonic-gate 					if (clsp - nlsp >= strlen(CE) &&
213*0Sstevel@tonic-gate 					    clsp < win->_maxx) {
214*0Sstevel@tonic-gate #ifdef DEBUG
215*0Sstevel@tonic-gate 						fprintf(outf, "MAKECH: using"
216*0Sstevel@tonic-gate 						    " CE\n");
217*0Sstevel@tonic-gate #endif
218*0Sstevel@tonic-gate 						(void) _puts(CE);
219*0Sstevel@tonic-gate 						lx = wx + win->_begx;
220*0Sstevel@tonic-gate 						while (wx++ <= clsp)
221*0Sstevel@tonic-gate 							*csp++ = ' ';
222*0Sstevel@tonic-gate 						return (OK);
223*0Sstevel@tonic-gate 					}
224*0Sstevel@tonic-gate 					ce = NULL;
225*0Sstevel@tonic-gate 				}
226*0Sstevel@tonic-gate 				/*
227*0Sstevel@tonic-gate 				 * enter/exit standout mode as appropriate
228*0Sstevel@tonic-gate 				 */
229*0Sstevel@tonic-gate 				if (SO && (*nsp&_STANDOUT) !=
230*0Sstevel@tonic-gate 				    (curscr->_flags&_STANDOUT)) {
231*0Sstevel@tonic-gate 					if (*nsp & _STANDOUT) {
232*0Sstevel@tonic-gate 						(void) _puts(SO);
233*0Sstevel@tonic-gate 						curscr->_flags |= _STANDOUT;
234*0Sstevel@tonic-gate 					} else {
235*0Sstevel@tonic-gate 						(void) _puts(SE);
236*0Sstevel@tonic-gate 						curscr->_flags &= ~_STANDOUT;
237*0Sstevel@tonic-gate 					}
238*0Sstevel@tonic-gate 				}
239*0Sstevel@tonic-gate 				wx++;
240*0Sstevel@tonic-gate 				if (wx >= win->_maxx && wy == win->_maxy - 1)
241*0Sstevel@tonic-gate 					if (win->_scroll) {
242*0Sstevel@tonic-gate 					    if ((curscr->_flags&_STANDOUT) &&
243*0Sstevel@tonic-gate 						(win->_flags & _ENDLINE))
244*0Sstevel@tonic-gate 						    if (!MS) {
245*0Sstevel@tonic-gate 							(void) _puts(SE);
246*0Sstevel@tonic-gate 							curscr->_flags &=
247*0Sstevel@tonic-gate 							    ~_STANDOUT;
248*0Sstevel@tonic-gate 						    }
249*0Sstevel@tonic-gate 					    if (!curwin)
250*0Sstevel@tonic-gate 						(void) _putchar((*csp = *nsp) &
251*0Sstevel@tonic-gate 						    0177);
252*0Sstevel@tonic-gate 					    else
253*0Sstevel@tonic-gate 						(void) _putchar(*nsp & 0177);
254*0Sstevel@tonic-gate 					    if (win->_flags&_FULLWIN && !curwin)
255*0Sstevel@tonic-gate 						(void) scroll(curscr);
256*0Sstevel@tonic-gate 					    if (!curwin) {
257*0Sstevel@tonic-gate 						    ly = wy + win->_begy;
258*0Sstevel@tonic-gate 						    lx = wx + win->_begx;
259*0Sstevel@tonic-gate 					    } else {
260*0Sstevel@tonic-gate 						    ly = win->_begy+win->_cury;
261*0Sstevel@tonic-gate 						    lx = win->_begx+win->_curx;
262*0Sstevel@tonic-gate 					    }
263*0Sstevel@tonic-gate 					    return (OK);
264*0Sstevel@tonic-gate 					} else if (win->_flags&_SCROLLWIN) {
265*0Sstevel@tonic-gate 					    wx = wx - 1;
266*0Sstevel@tonic-gate 					    lx = wx;
267*0Sstevel@tonic-gate 					    return (ERR);
268*0Sstevel@tonic-gate 					}
269*0Sstevel@tonic-gate 				if (!curwin)
270*0Sstevel@tonic-gate 					(void) _putchar((*csp++ = *nsp) & 0177);
271*0Sstevel@tonic-gate 				else
272*0Sstevel@tonic-gate 					(void) _putchar(*nsp & 0177);
273*0Sstevel@tonic-gate #ifdef FULLDEBUG
274*0Sstevel@tonic-gate 				fprintf(outf,
275*0Sstevel@tonic-gate 					"MAKECH:putchar(%c)\n", *nsp & 0177);
276*0Sstevel@tonic-gate #endif
277*0Sstevel@tonic-gate 				if (UC && (*nsp & _STANDOUT)) {
278*0Sstevel@tonic-gate 					(void) _putchar('\b');
279*0Sstevel@tonic-gate 					(void) _puts(UC);
280*0Sstevel@tonic-gate 				}
281*0Sstevel@tonic-gate 				nsp++;
282*0Sstevel@tonic-gate 			}
283*0Sstevel@tonic-gate #ifdef DEBUG
284*0Sstevel@tonic-gate 			fprintf(outf, "MAKECH: 2: wx = %d, lx = %d\n", wx, lx);
285*0Sstevel@tonic-gate #endif
286*0Sstevel@tonic-gate 			if (lx == wx + win->_begx)	/* if no change */
287*0Sstevel@tonic-gate 				break;
288*0Sstevel@tonic-gate 			lx = wx + win->_begx;
289*0Sstevel@tonic-gate 			if (lx >= COLS && AM) {
290*0Sstevel@tonic-gate 				lx = 0;
291*0Sstevel@tonic-gate 				ly++;
292*0Sstevel@tonic-gate 				/*
293*0Sstevel@tonic-gate 				 * xn glitch: chomps a newline after auto-wrap.
294*0Sstevel@tonic-gate 				 * we just feed it now and forget about it.
295*0Sstevel@tonic-gate 				 */
296*0Sstevel@tonic-gate 				if (XN) {
297*0Sstevel@tonic-gate 					(void) _putchar('\n');
298*0Sstevel@tonic-gate 					(void) _putchar('\r');
299*0Sstevel@tonic-gate 				}
300*0Sstevel@tonic-gate 			}
301*0Sstevel@tonic-gate 		} else if (wx <= lch)
302*0Sstevel@tonic-gate 			while (wx <= lch && *nsp == *csp) {
303*0Sstevel@tonic-gate 				nsp++;
304*0Sstevel@tonic-gate 				if (!curwin)
305*0Sstevel@tonic-gate 					csp++;
306*0Sstevel@tonic-gate 				++wx;
307*0Sstevel@tonic-gate 			}
308*0Sstevel@tonic-gate 		else
309*0Sstevel@tonic-gate 			break;
310*0Sstevel@tonic-gate #ifdef DEBUG
311*0Sstevel@tonic-gate 		fprintf(outf, "MAKECH: 3: wx = %d, lx = %d\n", wx, lx);
312*0Sstevel@tonic-gate #endif
313*0Sstevel@tonic-gate 	}
314*0Sstevel@tonic-gate 	return (OK);
315*0Sstevel@tonic-gate }
316*0Sstevel@tonic-gate 
317*0Sstevel@tonic-gate /*
318*0Sstevel@tonic-gate  * perform a mvcur, leaving standout mode if necessary
319*0Sstevel@tonic-gate  */
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate DEBUGSTATIC void
domvcur(int oy,int ox,int ny,int nx)322*0Sstevel@tonic-gate domvcur(int oy, int ox, int ny, int nx)
323*0Sstevel@tonic-gate {
324*0Sstevel@tonic-gate 	if (curscr->_flags & _STANDOUT && !MS) {
325*0Sstevel@tonic-gate 		(void) _puts(SE);
326*0Sstevel@tonic-gate 		curscr->_flags &= ~_STANDOUT;
327*0Sstevel@tonic-gate 	}
328*0Sstevel@tonic-gate 	(void) mvcur(oy, ox, ny, nx);
329*0Sstevel@tonic-gate }
330