1 /* $NetBSD: screen.c,v 1.40 2024/07/11 07:13:41 blymn 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.40 2024/07/11 07:13:41 blymn 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
filter(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 *
set_term(SCREEN * new)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 *
newterm(const char * type,FILE * outfd,FILE * infd)127 newterm(const char *type, FILE *outfd, FILE *infd)
128 {
129 SCREEN *new_screen;
130 const 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 precede each newterm() */
156 new_screen->nca = A_NORMAL;
157 new_screen->color_type = COLOR_NONE;
158 new_screen->COLOR_PAIRS = 0;
159 new_screen->curpair = -1;
160 new_screen->old_mode = 1;
161 new_screen->stdbuf = NULL;
162 new_screen->stdscr = NULL;
163 new_screen->curscr = NULL;
164 new_screen->__virtscr = NULL;
165 new_screen->curwin = 0;
166 new_screen->notty = FALSE;
167 new_screen->resized = 0;
168 new_screen->unget_len = 32;
169
170 if ((new_screen->unget_list =
171 malloc(sizeof(wchar_t) * new_screen->unget_len)) == NULL)
172 {
173 goto error_exit;
174 }
175 new_screen->unget_pos = 0;
176
177 if (_cursesi_gettmode(new_screen) == ERR)
178 goto error_exit;
179
180 if (_cursesi_setterm(sp, new_screen) == ERR)
181 goto error_exit;
182
183 /* Need either homing or cursor motion for refreshes */
184 if (!t_cursor_home(new_screen->term) &&
185 !t_cursor_address(new_screen->term))
186 goto error_exit;
187
188 new_screen->winlistp = NULL;
189
190 if ((new_screen->curscr = __newwin(new_screen, 0,
191 0, 0, 0, FALSE, FALSE)) == NULL)
192 goto error_exit;
193
194 if ((new_screen->__virtscr = __newwin(new_screen, 0,
195 0, 0, 0, FALSE, FALSE)) == NULL)
196 goto error_exit;
197
198 /* If Soft Label Keys are setup, they will ripoffline. */
199 if (__slk_init(new_screen) == ERR)
200 goto error_exit;
201
202 if (__ripoffscreen(new_screen) == ERR)
203 goto error_exit;
204
205 new_screen->stdscr = __newwin(new_screen, 0, 0, 0, 0, FALSE, TRUE);
206 if (new_screen->stdscr == NULL)
207 goto error_exit;
208
209 clearok(new_screen->stdscr, 1);
210
211 __init_getch(new_screen);
212 __init_acs(new_screen);
213 #ifdef HAVE_WCHAR
214 __init_get_wch(new_screen);
215 __init_wacs(new_screen);
216 #endif /* HAVE_WCHAR */
217
218 __set_stophandler();
219 __set_winchhandler();
220
221 /*
222 * bleh - it seems that apps expect the first newterm to set
223 * up the curses screen.... emulate this.
224 */
225 if (_cursesi_screen == NULL || _cursesi_screen->endwin) {
226 set_term(new_screen);
227 }
228
229 __CTRACE(__CTRACE_SCREEN, "newterm: LINES = %d, COLS = %d\n",
230 LINES, COLS);
231 __startwin(new_screen);
232
233 return new_screen;
234
235 error_exit:
236 __delscreen(new_screen);
237 free(new_screen->unget_list);
238
239 free(new_screen);
240 return NULL;
241 }
242
243 /*
244 * delscreen --
245 * Free resources used by the given screen and destroy it.
246 *
247 */
248 void
delscreen(SCREEN * screen)249 delscreen(SCREEN *screen)
250 {
251
252 __CTRACE(__CTRACE_SCREEN, "delscreen(%p)\n", screen);
253
254 __delscreen(screen);
255
256 /* free the storage of the keymaps */
257 _cursesi_free_keymap(screen->base_keymap);
258
259 /* free the Soft Label Keys */
260 __slk_free(screen);
261
262 free(screen->stdbuf);
263 free(screen->unget_list);
264 if (_cursesi_screen == screen)
265 _cursesi_screen = NULL;
266 free(screen);
267 }
268
269 static void
__delscreen(SCREEN * screen)270 __delscreen(SCREEN *screen)
271 {
272 struct __winlist *list;
273
274 /* free up the terminfo stuff */
275 if (screen->term != NULL)
276 del_curterm(screen->term);
277
278 /* walk the window list and kill all the parent windows */
279 while ((list = screen->winlistp) != NULL) {
280 delwin(list->winp);
281 if (list == screen->winlistp)
282 /* sanity - abort if window didn't remove itself */
283 break;
284 }
285 }
286