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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 /* Clean things up before exiting. */
45 
46 #include	<stdlib.h>
47 #include	<sys/types.h>
48 #include	"curses_inc.h"
49 
50 #ifdef	_VR2_COMPAT_CODE
51 char	_endwin = 0;
52 #endif	/* _VR2_COMPAT_CODE */
53 
54 int
55 isendwin(void)
56 {
57 	/*
58 	 * The test below must stay at TRUE because the value of 2
59 	 * has special meaning to wrefresh but does not mean that
60 	 * endwin has been called.
61 	 */
62 	if (SP && (SP->fl_endwin == TRUE))
63 		return (TRUE);
64 	else
65 		return (FALSE);
66 }
67 
68 int
69 endwin(void)
70 {
71 	/* make sure we're in program mode */
72 	if (SP->fl_endwin) {
73 		/* If endwin is equal to 2 it means we just did a newscreen. */
74 		if (SP->fl_endwin == TRUE) {
75 			(void) reset_prog_mode();
76 			if (SP->kp_state)
77 				(void) tputs(keypad_xmit, 1, _outch);
78 			if (SP->fl_meta)
79 				(void) tputs(meta_on, 1, _outch);
80 			if (cur_term->_cursorstate != 1)
81 				_PUTS(cur_term->cursor_seq
82 				    [cur_term->_cursorstate], 0);
83 		}
84 		_PUTS(enter_ca_mode, 1);
85 		(void) tputs(ena_acs, 1, _outch);
86 		if (exit_attribute_mode)
87 			_PUTS(tparm_p0(exit_attribute_mode), 1);
88 		else
89 			/*
90 			 * If there is no exit_attribute mode, then vidupdate
91 			 * could only possibly turn off one of the below three
92 			 * so that's all we ask it turn off.
93 			 */
94 			vidupdate(A_NORMAL,
95 			    (A_ALTCHARSET | A_STANDOUT | A_UNDERLINE), _outch);
96 
97 		SP->fl_endwin = FALSE;
98 
99 #ifdef	_VR2_COMPAT_CODE
100 		_endwin = (char) FALSE;
101 #endif	/* _VR2_COMPAT_CODE */
102 	}
103 
104 	/* See comment above why this test is explicitly against TRUE. */
105 	if (SP->fl_endwin == TRUE)
106 		return (ERR);
107 
108 	/* Flush out any output not output due to typeahead. */
109 	if (_INPUTPENDING)
110 		(void) force_doupdate();
111 
112 	/* Close things down. */
113 	(void) ttimeout(-1);
114 	if (SP->fl_meta)
115 		(void) tputs(meta_off, 1, _outch);
116 	(void) mvcur(curscr->_cury, curscr->_curx, curscr->_maxy - 1, 0);
117 
118 	if (SP->kp_state)
119 		(void) tputs(keypad_local, 1, _outch);
120 	if (cur_term->_cursorstate != 1)
121 		(void) tputs(cursor_normal, 0, _outch);
122 
123 	/* don't bother turning off colors: it will be done later anyway */
124 	curscr->_attrs &= ~A_COLOR;		/* SS: colors */
125 
126 	if ((curscr->_attrs != A_NORMAL) &&
127 	    (magic_cookie_glitch < 0) && (!ceol_standout_glitch))
128 		_VIDS(A_NORMAL, curscr->_attrs);
129 
130 	if (SP->phys_irm)
131 		_OFFINSERT();
132 
133 	SP->fl_endwin = TRUE;
134 
135 #ifdef	_VR2_COMPAT_CODE
136 	_endwin = TRUE;
137 #endif	/* _VR2_COMPAT_CODE */
138 
139 	curscr->_clear = TRUE;
140 	(void) reset_shell_mode();
141 	(void) tputs(exit_ca_mode, 0, _outch);
142 
143 	/* restore colors and default color pair. SS: colors	*/
144 	if (orig_colors)
145 		(void) tputs(orig_colors, 1, _outch);
146 	if (orig_pair)
147 		(void) tputs(tparm_p0(orig_pair), 1, _outch);
148 
149 	/* SS-mouse: free the mouse. */
150 
151 	if (get_mouse)
152 		(void) tputs(tparm_p1(get_mouse, 0), 1, _outch);
153 
154 	(void) fflush(SP->term_file);
155 
156 	return (OK);
157 }
158 
159 int
160 force_doupdate(void)
161 {
162 	char	chars_onQ = cur_term->_chars_on_queue;
163 	int	ret;
164 
165 	/*
166 	 * This will cause _chkinput to return FALSE which will force wrefresh
167 	 * to think there is no input waiting and it will finish its refresh.
168 	 */
169 
170 	cur_term->_chars_on_queue = INP_QSIZE;
171 	ret = doupdate();
172 	cur_term->_chars_on_queue = chars_onQ;
173 	return (ret);
174 }
175