xref: /netbsd-src/lib/libcurses/screen.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: screen.c,v 1.33 2017/03/20 10:20:16 roy 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.33 2017/03/20 10:20:16 roy 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 + __rippedlines(old_screen);
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 - __rippedlines(new);
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 #ifdef DEBUG
116 	__CTRACE(__CTRACE_SCREEN, "set_term: LINES = %d, COLS = %d\n",
117 	    LINES, COLS);
118 #endif
119 
120 	return old_screen;
121 }
122 
123 /*
124  * newterm --
125  *      Set up a new screen.
126  *
127  */
128 SCREEN *
129 newterm(char *type, FILE *outfd, FILE *infd)
130 {
131 	SCREEN *new_screen;
132 	char *sp;
133 	int rtop;
134 
135 	sp = type;
136 	if (type == NULL && (sp = getenv("TERM")) == NULL)
137 		return NULL;
138 
139 	if ((new_screen = calloc(1, sizeof(SCREEN))) == NULL)
140 		return NULL;
141 
142 #ifdef DEBUG
143 	__CTRACE(__CTRACE_INIT, "newterm\n");
144 #endif
145 
146 	new_screen->infd = infd;
147 	/*
148 	 * POSIX standard says this should be set to infd by default,
149 	 * but this seems to break nvi by leaving an unrefreshed screen.
150 	 * Also, the line breakout optimisation advertised in ncurses
151 	 * doesn't actually do anything, so explicitly disabling it here makes
152 	 * sense for the time being.
153 	 * A caller can always enable it by calling typeahead(3) anyway.
154 	 */
155 	new_screen->checkfd = -1; // fileno(infd);
156 	new_screen->outfd = outfd;
157 	new_screen->echoit = new_screen->nl = 1;
158 	new_screen->pfast = new_screen->rawmode = new_screen->noqch = 0;
159 	new_screen->filtered = filtered;
160 	filtered = FALSE; /* filter() must preceed each newterm() */
161 	new_screen->nca = A_NORMAL;
162 	new_screen->color_type = COLOR_NONE;
163 	new_screen->COLOR_PAIRS = 0;
164 	new_screen->old_mode = 2;
165 	new_screen->stdbuf = NULL;
166 	new_screen->stdscr = NULL;
167 	new_screen->curscr = NULL;
168 	new_screen->__virtscr = NULL;
169 	new_screen->curwin = 0;
170 	new_screen->notty = FALSE;
171 	new_screen->half_delay = FALSE;
172 	new_screen->resized = 0;
173 	new_screen->unget_len = 32;
174 
175 	if ((new_screen->unget_list =
176 	    malloc(sizeof(wchar_t) * new_screen->unget_len)) == NULL)
177 	{
178 		goto error_exit;
179 	}
180 	new_screen->unget_pos = 0;
181 
182 	if (_cursesi_gettmode(new_screen) == ERR)
183 		goto error_exit;
184 
185 	if (_cursesi_setterm((char *)sp, new_screen) == ERR)
186 		goto error_exit;
187 
188 	/* Need either homing or cursor motion for refreshes */
189 	if (!t_cursor_home(new_screen->term) &&
190 	    !t_cursor_address(new_screen->term))
191 		goto error_exit;
192 
193 	new_screen->winlistp = NULL;
194 
195 	if ((new_screen->curscr = __newwin(new_screen, 0,
196 	    0, 0, 0, FALSE)) == NULL)
197 		goto error_exit;
198 
199 	if ((new_screen->__virtscr = __newwin(new_screen, 0,
200 	    0, 0, 0, FALSE)) == NULL)
201 		goto error_exit;
202 
203 	/* If Soft Label Keys are setup, they will ripoffline. */
204 	if (__slk_init(new_screen) == ERR)
205 		goto error_exit;
206 
207 	if (__ripoffscreen(new_screen, &rtop) == ERR)
208 		goto error_exit;
209 
210 	new_screen->stdscr = __newwin(new_screen, LINES, 0, rtop, 0, FALSE);
211 	if (new_screen->stdscr == NULL)
212 		goto error_exit;
213 
214 	clearok(new_screen->stdscr, 1);
215 
216 	__init_getch(new_screen);
217 	__init_acs(new_screen);
218 #ifdef HAVE_WCHAR
219 	__init_get_wch(new_screen);
220 	__init_wacs(new_screen);
221 #endif /* HAVE_WCHAR */
222 
223 	__set_stophandler();
224 	__set_winchhandler();
225 
226 	  /*
227 	   * bleh - it seems that apps expect the first newterm to set
228 	   * up the curses screen.... emulate this.
229 	   */
230 	if (_cursesi_screen == NULL || _cursesi_screen->endwin) {
231 		set_term(new_screen);
232 	}
233 
234 #ifdef DEBUG
235 	__CTRACE(__CTRACE_SCREEN, "newterm: LINES = %d, COLS = %d\n",
236 	    LINES, COLS);
237 #endif
238 	__startwin(new_screen);
239 
240 	return new_screen;
241 
242   error_exit:
243 	__delscreen(new_screen);
244 	free(new_screen->unget_list);
245 
246 	free(new_screen);
247 	return NULL;
248 }
249 
250 /*
251  * delscreen --
252  *   Free resources used by the given screen and destroy it.
253  *
254  */
255 void
256 delscreen(SCREEN *screen)
257 {
258 
259 #ifdef DEBUG
260 	__CTRACE(__CTRACE_SCREEN, "delscreen(%p)\n", screen);
261 #endif
262 
263 	__delscreen(screen);
264 
265 	  /* free the storage of the keymaps */
266 	_cursesi_free_keymap(screen->base_keymap);
267 
268 	  /* free the Soft Label Keys */
269 	__slk_free(screen);
270 
271 	free(screen->stdbuf);
272 	free(screen->unget_list);
273 	if (_cursesi_screen == screen)
274 		_cursesi_screen = NULL;
275 	free(screen);
276 }
277 
278 static void
279 __delscreen(SCREEN *screen)
280 {
281         struct __winlist *list;
282 
283 	  /* free up the terminfo stuff */
284 	if (screen->term != NULL)
285 		del_curterm(screen->term);
286 
287 	  /* walk the window list and kill all the parent windows */
288 	while ((list = screen->winlistp) != NULL) {
289 		delwin(list->winp);
290 		if (list == screen->winlistp)
291 			/* sanity - abort if window didn't remove itself */
292 			break;
293 	}
294 }
295