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