xref: /onnv-gate/usr/src/lib/libcurses/screen/scr_reset.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 1997 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 #include	"curses_inc.h"
45*0Sstevel@tonic-gate #include	<sys/types.h>
46*0Sstevel@tonic-gate #include	<sys/stat.h>
47*0Sstevel@tonic-gate #include	<stdlib.h>
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate /*
50*0Sstevel@tonic-gate  * Initialize the screen image to be the image contained
51*0Sstevel@tonic-gate  * in the given file. This is usually used in a child process
52*0Sstevel@tonic-gate  * to initialize its idea of the screen image to be that of its
53*0Sstevel@tonic-gate  * parent.
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  * filep:	pointer to the output stream
56*0Sstevel@tonic-gate  * type:	0: <curses> should assume that the physical screen is
57*0Sstevel@tonic-gate  *		   EXACTLY as stored in the file. Therefore, we take
58*0Sstevel@tonic-gate  *		   special care to make sure that /dev/tty and the terminal
59*0Sstevel@tonic-gate  *		   did not change in any way.  This information can then
60*0Sstevel@tonic-gate  *		   be used in the update optimization of the new program
61*0Sstevel@tonic-gate  *		   so that the screen does not have to be cleared.  Instead,
62*0Sstevel@tonic-gate  *		   curses, by knowing what's on the screen can optimally
63*0Sstevel@tonic-gate  *		   update it with the information of the new program.
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  *		1: Tell <curses> that the stored image should be
66*0Sstevel@tonic-gate  *		   the physical image.  Sort of like a huge waddstr onto
67*0Sstevel@tonic-gate  *		   curscr.  This can be used when a library wants to save
68*0Sstevel@tonic-gate  *		   a screen image and restore it at a later time.
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  *		2: Tell <curses> that the stored image is the physical
71*0Sstevel@tonic-gate  *		   image and also it is what the new program wants on the
72*0Sstevel@tonic-gate  *		   screen.  This can be be thought of as a screen inheritance
73*0Sstevel@tonic-gate  *		   function.
74*0Sstevel@tonic-gate  */
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate int
scr_reset(FILE * filep,int type)77*0Sstevel@tonic-gate scr_reset(FILE *filep, int type)
78*0Sstevel@tonic-gate {
79*0Sstevel@tonic-gate 	WINDOW		*win = NULL, *win1 = NULL;
80*0Sstevel@tonic-gate 	int		*hash, y;
81*0Sstevel@tonic-gate 	char		clearit = FALSE;
82*0Sstevel@tonic-gate 	short		magic;
83*0Sstevel@tonic-gate 	struct	stat	statbuf;
84*0Sstevel@tonic-gate 	time_t		ttytime;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	if (type != 1 && exit_ca_mode && *exit_ca_mode && non_rev_rmcup) {
87*0Sstevel@tonic-gate 		if (type == 0)
88*0Sstevel@tonic-gate 			goto err;
89*0Sstevel@tonic-gate 		else {
90*0Sstevel@tonic-gate #ifdef	DEBUG
91*0Sstevel@tonic-gate 			if (outf)
92*0Sstevel@tonic-gate 				fprintf(outf, "clear it because of "
93*0Sstevel@tonic-gate 				    "exit_ca_mode\n");
94*0Sstevel@tonic-gate #endif	/* DEBUG */
95*0Sstevel@tonic-gate 			clearit = TRUE;
96*0Sstevel@tonic-gate 		}
97*0Sstevel@tonic-gate 	}
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	/* check magic number */
100*0Sstevel@tonic-gate 	if (fread((char *) &magic, sizeof (short), 1, filep) != 1)
101*0Sstevel@tonic-gate 		goto err;
102*0Sstevel@tonic-gate 	if (magic != SVR3_DUMP_MAGIC_NUMBER)
103*0Sstevel@tonic-gate 		goto err;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	/* get modification time of image in file */
106*0Sstevel@tonic-gate 	if (fread((char *) &ttytime, sizeof (time_t), 1, filep) != 1)
107*0Sstevel@tonic-gate 		goto err;
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	if ((type != 1) && ((ttyname(cur_term->Filedes) == NULL) ||
110*0Sstevel@tonic-gate 	    (fstat(cur_term->Filedes, &statbuf) < 0) ||
111*0Sstevel@tonic-gate 	    (statbuf.st_mtime != ttytime))) {
112*0Sstevel@tonic-gate 		if (type == 0)
113*0Sstevel@tonic-gate 			goto err;
114*0Sstevel@tonic-gate 		else {
115*0Sstevel@tonic-gate #ifdef	DEBUG
116*0Sstevel@tonic-gate 			if (outf)
117*0Sstevel@tonic-gate 				fprintf(outf, "Filedes = %hd, "
118*0Sstevel@tonic-gate 				    "statbuf.st_mtime = %d, "
119*0Sstevel@tonic-gate 				    "ttytime = %d\n", cur_term->Filedes,
120*0Sstevel@tonic-gate 				    statbuf.st_mtime, ttytime);
121*0Sstevel@tonic-gate #endif	/* DEBUG */
122*0Sstevel@tonic-gate 			clearit = TRUE;
123*0Sstevel@tonic-gate 		}
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	/* if get here, everything is ok, read the curscr image */
127*0Sstevel@tonic-gate 	if (((win = getwin(filep)) == NULL) ||
128*0Sstevel@tonic-gate 	    ((type == 2) && ((win1 = dupwin(win)) == NULL)) ||
129*0Sstevel@tonic-gate 	    (win->_maxy != curscr->_maxy) || (win->_maxx != curscr->_maxx) ||
130*0Sstevel@tonic-gate 	    /* soft labels */
131*0Sstevel@tonic-gate 	    (fread((char *) &magic, sizeof (int), 1, filep) != 1))
132*0Sstevel@tonic-gate 		goto err;
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	/*
135*0Sstevel@tonic-gate 	* if soft labels were dumped, we would like either read them
136*0Sstevel@tonic-gate 	* or advance the file pointer pass them
137*0Sstevel@tonic-gate 	*/
138*0Sstevel@tonic-gate 	if (magic) {
139*0Sstevel@tonic-gate 		short	i, labmax, lablen;
140*0Sstevel@tonic-gate 		SLK_MAP	*slk = SP->slk;
141*0Sstevel@tonic-gate 		/*
142*0Sstevel@tonic-gate 		 * Why doesn't the following line and the two below
143*0Sstevel@tonic-gate 		 * that access those variables work ?
144*0Sstevel@tonic-gate 		 */
145*0Sstevel@tonic-gate 		/*
146*0Sstevel@tonic-gate 		 * char	**labdis = SP->slk->_ldis, **labval = SP->slk->_lval;
147*0Sstevel@tonic-gate 		*/
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 		if ((fread((char *) &labmax, sizeof (short), 1, filep) != 1) ||
150*0Sstevel@tonic-gate 		    (fread((char *) &lablen, sizeof (short), 1, filep) != 1)) {
151*0Sstevel@tonic-gate 			goto err;
152*0Sstevel@tonic-gate 		}
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 		if (slk != NULL) {
155*0Sstevel@tonic-gate 			if ((labmax != slk->_num) ||
156*0Sstevel@tonic-gate 			    (lablen != (slk->_len + 1)))
157*0Sstevel@tonic-gate 				goto err;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 			for (i = 0; i < labmax; i++) {
160*0Sstevel@tonic-gate 				/*
161*0Sstevel@tonic-gate 				 * if ((fread(labdis[i], sizeof (char), lablen,
162*0Sstevel@tonic-gate 				 * filep) != lablen) ||
163*0Sstevel@tonic-gate 				 * (fread(labval[i], sizeof (char), lablen,
164*0Sstevel@tonic-gate 				 * filep != lablen))
165*0Sstevel@tonic-gate 				*/
166*0Sstevel@tonic-gate 				if ((fread(slk->_ldis[i], sizeof (char),
167*0Sstevel@tonic-gate 				    lablen, filep) != lablen) ||
168*0Sstevel@tonic-gate 				    (fread(slk->_lval[i],
169*0Sstevel@tonic-gate 				    sizeof (char), lablen, filep) != lablen)) {
170*0Sstevel@tonic-gate 					goto err;
171*0Sstevel@tonic-gate 				}
172*0Sstevel@tonic-gate 			}
173*0Sstevel@tonic-gate 			(*_do_slk_tch)();
174*0Sstevel@tonic-gate 		} else {
175*0Sstevel@tonic-gate 			if (fseek(filep, (long) (2 * labmax * lablen *
176*0Sstevel@tonic-gate 			    sizeof (char)), 1) != 0)
177*0Sstevel@tonic-gate 				goto err;
178*0Sstevel@tonic-gate 		}
179*0Sstevel@tonic-gate 	}
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	/* read the color information(if any) from the file 		*/
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	if (fread((char *) &magic, sizeof (int), 1, filep) != 1)
184*0Sstevel@tonic-gate 		goto err;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	if (magic) {
187*0Sstevel@tonic-gate 		int  colors, color_pairs;
188*0Sstevel@tonic-gate 		bool could_change;
189*0Sstevel@tonic-gate 		int i;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	/* if the new terminal doesn't support colors, or it supports    */
192*0Sstevel@tonic-gate 	/* less colors (or color_pairs) than the old terminal, or	 */
193*0Sstevel@tonic-gate 	/* start_color() has not been called, simply advance  the file	 */
194*0Sstevel@tonic-gate 	/* pointer pass the color related info.				 */
195*0Sstevel@tonic-gate 	/* Note: must to read the first line of color info, even if the  */
196*0Sstevel@tonic-gate 	/* new terminal doesn't support color, in order to know how to   */
197*0Sstevel@tonic-gate 	/* deal with the rest of the file				 */
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		if ((fread((char *) &colors, sizeof (int), 1, filep) != 1) ||
200*0Sstevel@tonic-gate 		    (fread((char *) &color_pairs, sizeof (int), 1,
201*0Sstevel@tonic-gate 		    filep) != 1) || (fread((char *) &could_change,
202*0Sstevel@tonic-gate 		    sizeof (char), 1, filep) != 1))
203*0Sstevel@tonic-gate 			goto err;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 		if (max_pairs == -1 || cur_term->_pairs_tbl == NULL ||
206*0Sstevel@tonic-gate 		    colors > max_colors || color_pairs > max_pairs) {
207*0Sstevel@tonic-gate 			if (fseek(filep, (long) (colors * sizeof (_Color) +
208*0Sstevel@tonic-gate 			    color_pairs * sizeof (_Color_pair)), 1) != 0)
209*0Sstevel@tonic-gate 				goto err;
210*0Sstevel@tonic-gate 		} else {
211*0Sstevel@tonic-gate 			_Color_pair *ptp, *save_ptp;
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 	    /* if both old and new terminals could modify colors, read in */
214*0Sstevel@tonic-gate 	    /* color table, and call init_color for each color		  */
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 			if (could_change) {
217*0Sstevel@tonic-gate 				if (can_change) {
218*0Sstevel@tonic-gate 					_Color	 *ctp, *save_ctp;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 					if ((save_ctp = (ctp = (_Color *)
221*0Sstevel@tonic-gate 					    malloc(colors *
222*0Sstevel@tonic-gate 					    sizeof (_Color)))) == NULL)
223*0Sstevel@tonic-gate 						goto err;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 					if (fread(ctp, sizeof (_Color),
226*0Sstevel@tonic-gate 					    colors, filep) != colors)
227*0Sstevel@tonic-gate 						goto err;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 					for (i = 0; i < colors; i++, ctp++) {
230*0Sstevel@tonic-gate 						/* LINTED */
231*0Sstevel@tonic-gate 						(void) init_color((short)i,
232*0Sstevel@tonic-gate 						    ctp->r, ctp->g, ctp->b);
233*0Sstevel@tonic-gate 					}
234*0Sstevel@tonic-gate 					free(save_ctp);
235*0Sstevel@tonic-gate 				} else {
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 		/* the old terminal could modify colors, by the new one */
238*0Sstevel@tonic-gate 		/* cannot skip over color_table info.			*/
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 					if (fseek(filep, (long) (colors *
241*0Sstevel@tonic-gate 					    sizeof (_Color)), 1) != 0)
242*0Sstevel@tonic-gate 						goto err;
243*0Sstevel@tonic-gate 				}
244*0Sstevel@tonic-gate 			}
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	    /* read color_pairs info. call init_pair for each pair	*/
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 			if ((save_ptp = (ptp = (_Color_pair *)
249*0Sstevel@tonic-gate 			    malloc(color_pairs * sizeof (_Color_pair)))) ==
250*0Sstevel@tonic-gate 			    NULL)
251*0Sstevel@tonic-gate 				goto err;
252*0Sstevel@tonic-gate 			if (fread(ptp, sizeof (_Color_pair), color_pairs,
253*0Sstevel@tonic-gate 			    filep) != color_pairs) {
254*0Sstevel@tonic-gate err:
255*0Sstevel@tonic-gate 				if (win != NULL)
256*0Sstevel@tonic-gate 					(void) delwin(win);
257*0Sstevel@tonic-gate 				if (win1 != NULL)
258*0Sstevel@tonic-gate 					(void) delwin(win1);
259*0Sstevel@tonic-gate 				if (type == 0)
260*0Sstevel@tonic-gate 					curscr->_clear = TRUE;
261*0Sstevel@tonic-gate 				return (ERR);
262*0Sstevel@tonic-gate 			}
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate 			for (i = 1, ++ptp; i <= color_pairs; i++, ptp++) {
265*0Sstevel@tonic-gate 				if (ptp->init)
266*0Sstevel@tonic-gate 					/* LINTED */
267*0Sstevel@tonic-gate 					(void) init_pair((short)i,
268*0Sstevel@tonic-gate 					     ptp->foreground, ptp->background);
269*0Sstevel@tonic-gate 			}
270*0Sstevel@tonic-gate 			free(save_ptp);
271*0Sstevel@tonic-gate 		}
272*0Sstevel@tonic-gate 	}
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	/* substitute read in window for the curscr */
275*0Sstevel@tonic-gate 	switch (type) {
276*0Sstevel@tonic-gate 		case 1:
277*0Sstevel@tonic-gate 		case 2:
278*0Sstevel@tonic-gate 			(void) delwin(_virtscr);
279*0Sstevel@tonic-gate 			hash = _VIRTHASH;
280*0Sstevel@tonic-gate 			if (type == 1) {
281*0Sstevel@tonic-gate 				SP->virt_scr = _virtscr = win;
282*0Sstevel@tonic-gate 				_VIRTTOP = 0;
283*0Sstevel@tonic-gate 				_VIRTBOT = curscr->_maxy - 1;
284*0Sstevel@tonic-gate 				break;
285*0Sstevel@tonic-gate 			}
286*0Sstevel@tonic-gate 			SP->virt_scr = _virtscr = win1;
287*0Sstevel@tonic-gate 			_VIRTTOP = curscr->_maxy;
288*0Sstevel@tonic-gate 			_VIRTBOT = -1;
289*0Sstevel@tonic-gate 			/* clear the hash table */
290*0Sstevel@tonic-gate 			for (y = curscr->_maxy; y > 0; --y)
291*0Sstevel@tonic-gate 				*hash++ = _NOHASH;
292*0Sstevel@tonic-gate 		/* LINTED */ /* Known fall-through on case statement. */
293*0Sstevel@tonic-gate 		case 0:
294*0Sstevel@tonic-gate 			{
295*0Sstevel@tonic-gate 			int	saveflag = curscr->_flags & _CANT_BE_IMMED;
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate 			(void) delwin(curscr);
298*0Sstevel@tonic-gate 			SP->cur_scr = curscr = win;
299*0Sstevel@tonic-gate 			curscr->_sync = TRUE;
300*0Sstevel@tonic-gate 			curscr->_flags |= saveflag;
301*0Sstevel@tonic-gate 			hash = _CURHASH;
302*0Sstevel@tonic-gate 		}
303*0Sstevel@tonic-gate 	}
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	/* clear the hash table */
306*0Sstevel@tonic-gate 	for (y = curscr->_maxy; y > 0; --y)
307*0Sstevel@tonic-gate 		*hash++ = _NOHASH;
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate 	curscr->_clear = clearit;
310*0Sstevel@tonic-gate 	return (OK);
311*0Sstevel@tonic-gate }
312