xref: /netbsd-src/lib/libcurses/screen.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: screen.c,v 1.36 2021/09/06 07:03:50 rin Exp $	*/
2 
3 /*
4  * Copyright (c) 1981, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)screen.c	8.2 (blymn) 11/27/2001";
36 #else
37 __RCSID("$NetBSD: screen.c,v 1.36 2021/09/06 07:03:50 rin Exp $");
38 #endif
39 #endif					/* not lint */
40 
41 #include <stdlib.h>
42 
43 #include "curses.h"
44 #include "curses_private.h"
45 
46 static int filtered;
47 
48 static void	 __delscreen(SCREEN *);
49 
50 /*
51  * filter has to be called before either initscr or newterm.
52  */
53 void
54 filter(void)
55 {
56 
57 	filtered = TRUE;
58 }
59 
60 /*
61  * set_term --
62  *      Change the term to the given screen.
63  *
64  */
65 SCREEN *
66 set_term(SCREEN *new)
67 {
68 	SCREEN *old_screen = _cursesi_screen;
69 
70 	if (_cursesi_screen != NULL) {
71 		  /* save changes made to the current screen... */
72 		old_screen->echoit = __echoit;
73 		old_screen->pfast = __pfast;
74 		old_screen->rawmode = __rawmode;
75 		old_screen->noqch = __noqch;
76 		old_screen->COLS = COLS;
77 		old_screen->LINES = LINES;
78 		old_screen->ESCDELAY = ESCDELAY;
79 		old_screen->TABSIZE = TABSIZE;
80 		old_screen->COLORS = COLORS;
81 		old_screen->COLOR_PAIRS = COLOR_PAIRS;
82 		old_screen->GT = __GT;
83 		old_screen->NONL = __NONL;
84 		old_screen->UPPERCASE = __UPPERCASE;
85 	}
86 
87 	_cursesi_screen = new;
88 
89 	__echoit = new->echoit;
90         __pfast = new->pfast;
91 	__rawmode = new->rawmode;
92 	__noqch = new->noqch;
93 	COLS = new->COLS;
94 	LINES = new->LINES;
95 	ESCDELAY = new->ESCDELAY;
96 	TABSIZE = new->TABSIZE;
97 	COLORS = new->COLORS;
98 	COLOR_PAIRS = new->COLOR_PAIRS;
99 	__GT = new->GT;
100 	__NONL = new->NONL;
101 	__UPPERCASE = new->UPPERCASE;
102 
103 	_cursesi_resetterm(new);
104 
105 	curscr = new->curscr;
106 	clearok(curscr, new->clearok);
107 	stdscr = new->stdscr;
108 	__virtscr = new->__virtscr;
109 
110 	_cursesi_reset_acs(new);
111 #ifdef HAVE_WCHAR
112 	_cursesi_reset_wacs(new);
113 #endif /* HAVE_WCHAR */
114 
115 	__CTRACE(__CTRACE_SCREEN, "set_term: LINES = %d, COLS = %d\n",
116 	    LINES, COLS);
117 
118 	return old_screen;
119 }
120 
121 /*
122  * newterm --
123  *      Set up a new screen.
124  *
125  */
126 SCREEN *
127 newterm(char *type, FILE *outfd, FILE *infd)
128 {
129 	SCREEN *new_screen;
130 	char *sp;
131 
132 	sp = type;
133 	if (type == NULL && (sp = getenv("TERM")) == NULL)
134 		return NULL;
135 
136 	if ((new_screen = calloc(1, sizeof(SCREEN))) == NULL)
137 		return NULL;
138 
139 	__CTRACE(__CTRACE_INIT, "newterm\n");
140 
141 	new_screen->infd = infd;
142 	/*
143 	 * POSIX standard says this should be set to infd by default,
144 	 * but this seems to break nvi by leaving an unrefreshed screen.
145 	 * Also, the line breakout optimisation advertised in ncurses
146 	 * doesn't actually do anything, so explicitly disabling it here makes
147 	 * sense for the time being.
148 	 * A caller can always enable it by calling typeahead(3) anyway.
149 	 */
150 	new_screen->checkfd = -1; // fileno(infd);
151 	new_screen->outfd = outfd;
152 	new_screen->echoit = new_screen->nl = 1;
153 	new_screen->pfast = new_screen->rawmode = new_screen->noqch = 0;
154 	new_screen->filtered = filtered;
155 	filtered = FALSE; /* filter() must preceed each newterm() */
156 	new_screen->nca = A_NORMAL;
157 	new_screen->color_type = COLOR_NONE;
158 	new_screen->COLOR_PAIRS = 0;
159 	new_screen->old_mode = 2;
160 	new_screen->stdbuf = NULL;
161 	new_screen->stdscr = NULL;
162 	new_screen->curscr = NULL;
163 	new_screen->__virtscr = NULL;
164 	new_screen->curwin = 0;
165 	new_screen->notty = FALSE;
166 	new_screen->resized = 0;
167 	new_screen->unget_len = 32;
168 
169 	if ((new_screen->unget_list =
170 	    malloc(sizeof(wchar_t) * new_screen->unget_len)) == NULL)
171 	{
172 		goto error_exit;
173 	}
174 	new_screen->unget_pos = 0;
175 
176 	if (_cursesi_gettmode(new_screen) == ERR)
177 		goto error_exit;
178 
179 	if (_cursesi_setterm((char *)sp, new_screen) == ERR)
180 		goto error_exit;
181 
182 	/* Need either homing or cursor motion for refreshes */
183 	if (!t_cursor_home(new_screen->term) &&
184 	    !t_cursor_address(new_screen->term))
185 		goto error_exit;
186 
187 	new_screen->winlistp = NULL;
188 
189 	if ((new_screen->curscr = __newwin(new_screen, 0,
190 	    0, 0, 0, FALSE, FALSE)) == NULL)
191 		goto error_exit;
192 
193 	if ((new_screen->__virtscr = __newwin(new_screen, 0,
194 	    0, 0, 0, FALSE, FALSE)) == NULL)
195 		goto error_exit;
196 
197 	/* If Soft Label Keys are setup, they will ripoffline. */
198 	if (__slk_init(new_screen) == ERR)
199 		goto error_exit;
200 
201 	if (__ripoffscreen(new_screen) == ERR)
202 		goto error_exit;
203 
204 	new_screen->stdscr = __newwin(new_screen, 0, 0, 0, 0, FALSE, TRUE);
205 	if (new_screen->stdscr == NULL)
206 		goto error_exit;
207 
208 	clearok(new_screen->stdscr, 1);
209 
210 	__init_getch(new_screen);
211 	__init_acs(new_screen);
212 #ifdef HAVE_WCHAR
213 	__init_get_wch(new_screen);
214 	__init_wacs(new_screen);
215 #endif /* HAVE_WCHAR */
216 
217 	__set_stophandler();
218 	__set_winchhandler();
219 
220 	  /*
221 	   * bleh - it seems that apps expect the first newterm to set
222 	   * up the curses screen.... emulate this.
223 	   */
224 	if (_cursesi_screen == NULL || _cursesi_screen->endwin) {
225 		set_term(new_screen);
226 	}
227 
228 	__CTRACE(__CTRACE_SCREEN, "newterm: LINES = %d, COLS = %d\n",
229 	    LINES, COLS);
230 	__startwin(new_screen);
231 
232 	return new_screen;
233 
234   error_exit:
235 	__delscreen(new_screen);
236 	free(new_screen->unget_list);
237 
238 	free(new_screen);
239 	return NULL;
240 }
241 
242 /*
243  * delscreen --
244  *   Free resources used by the given screen and destroy it.
245  *
246  */
247 void
248 delscreen(SCREEN *screen)
249 {
250 
251 	__CTRACE(__CTRACE_SCREEN, "delscreen(%p)\n", screen);
252 
253 	__delscreen(screen);
254 
255 	  /* free the storage of the keymaps */
256 	_cursesi_free_keymap(screen->base_keymap);
257 
258 	  /* free the Soft Label Keys */
259 	__slk_free(screen);
260 
261 	free(screen->stdbuf);
262 	free(screen->unget_list);
263 	if (_cursesi_screen == screen)
264 		_cursesi_screen = NULL;
265 	free(screen);
266 }
267 
268 static void
269 __delscreen(SCREEN *screen)
270 {
271         struct __winlist *list;
272 
273 	  /* free up the terminfo stuff */
274 	if (screen->term != NULL)
275 		del_curterm(screen->term);
276 
277 	  /* walk the window list and kill all the parent windows */
278 	while ((list = screen->winlistp) != NULL) {
279 		delwin(list->winp);
280 		if (list == screen->winlistp)
281 			/* sanity - abort if window didn't remove itself */
282 			break;
283 	}
284 }
285