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