1 /* $OpenBSD: lib_set_term.c,v 1.12 2001/01/22 18:01:44 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /* 37 ** lib_set_term.c 38 ** 39 ** The routine set_term(). 40 ** 41 */ 42 43 #include <curses.priv.h> 44 45 #include <term.h> /* cur_term */ 46 #include <tic.h> 47 48 MODULE_ID("$From: lib_set_term.c,v 1.61 2000/12/10 02:43:27 tom Exp $") 49 50 NCURSES_EXPORT(SCREEN *) 51 set_term(SCREEN * screenp) 52 { 53 SCREEN *oldSP; 54 55 T((T_CALLED("set_term(%p)"), screenp)); 56 57 oldSP = SP; 58 _nc_set_screen(screenp); 59 60 set_curterm(SP->_term); 61 curscr = SP->_curscr; 62 newscr = SP->_newscr; 63 stdscr = SP->_stdscr; 64 COLORS = SP->_color_count; 65 COLOR_PAIRS = SP->_pair_count; 66 memcpy(acs_map, SP->_acs_map, sizeof(chtype) * ACS_LEN); 67 68 T((T_RETURN("%p"), oldSP)); 69 return (oldSP); 70 } 71 72 static void 73 _nc_free_keytry(struct tries *kt) 74 { 75 if (kt != 0) { 76 _nc_free_keytry(kt->child); 77 _nc_free_keytry(kt->sibling); 78 free(kt); 79 } 80 } 81 82 /* 83 * Free the storage associated with the given SCREEN sp. 84 */ 85 NCURSES_EXPORT(void) 86 delscreen(SCREEN * sp) 87 { 88 SCREEN **scan = &_nc_screen_chain; 89 90 T((T_CALLED("delscreen(%p)"), sp)); 91 92 while (*scan) { 93 if (*scan == sp) { 94 *scan = sp->_next_screen; 95 break; 96 } 97 scan = &(*scan)->_next_screen; 98 } 99 100 (void) _nc_freewin(sp->_curscr); 101 (void) _nc_freewin(sp->_newscr); 102 (void) _nc_freewin(sp->_stdscr); 103 _nc_free_keytry(sp->_keytry); 104 _nc_free_keytry(sp->_key_ok); 105 106 FreeIfNeeded(sp->_color_table); 107 FreeIfNeeded(sp->_color_pairs); 108 109 FreeIfNeeded(sp->oldhash); 110 FreeIfNeeded(sp->newhash); 111 112 del_curterm(sp->_term); 113 114 /* 115 * If the associated output stream has been closed, we can discard the 116 * set-buffer. Limit the error check to EBADF, since fflush may fail 117 * for other reasons than trying to operate upon a closed stream. 118 */ 119 if (sp->_ofp != 0 120 && sp->_setbuf != 0 121 && fflush(sp->_ofp) != 0 122 && errno == EBADF) { 123 free(sp->_setbuf); 124 } 125 126 free(sp); 127 128 /* 129 * If this was the current screen, reset everything that the 130 * application might try to use (except cur_term, which may have 131 * multiple references in different screens). 132 */ 133 if (sp == SP) { 134 curscr = 0; 135 newscr = 0; 136 stdscr = 0; 137 COLORS = 0; 138 COLOR_PAIRS = 0; 139 _nc_set_screen(0); 140 } 141 returnVoid; 142 } 143 144 static ripoff_t rippedoff[5]; 145 static ripoff_t *rsp = rippedoff; 146 #define N_RIPS SIZEOF(rippedoff) 147 148 static bool 149 no_mouse_event(SCREEN * sp GCC_UNUSED) 150 { 151 return FALSE; 152 } 153 154 static bool 155 no_mouse_inline(SCREEN * sp GCC_UNUSED) 156 { 157 return FALSE; 158 } 159 160 static bool 161 no_mouse_parse(int code GCC_UNUSED) 162 { 163 return TRUE; 164 } 165 166 static void 167 no_mouse_resume(SCREEN * sp GCC_UNUSED) 168 { 169 } 170 171 static void 172 no_mouse_wrap(SCREEN * sp GCC_UNUSED) 173 { 174 } 175 176 #if NCURSES_EXT_FUNCS && USE_COLORFGBG 177 static char * 178 extract_fgbg(char *src, int *result) 179 { 180 char *dst = 0; 181 long value = strtol(src, &dst, 0); 182 183 if (dst == 0) { 184 dst = src; 185 } else if (value >= 0) { 186 *result = value % max_colors; 187 } 188 while (*dst != 0 && *dst != ';') 189 dst++; 190 if (*dst == ';') 191 dst++; 192 return dst; 193 } 194 #endif 195 196 NCURSES_EXPORT(int) 197 _nc_setupscreen 198 (short slines, short const scolumns, FILE * output) 199 /* OS-independent screen initializations */ 200 { 201 int bottom_stolen = 0; 202 size_t i; 203 204 assert(SP == 0); /* has been reset in newterm() ! */ 205 if (!_nc_alloc_screen()) 206 return ERR; 207 208 SP->_next_screen = _nc_screen_chain; 209 _nc_screen_chain = SP; 210 211 _nc_set_buffer(output, TRUE); 212 SP->_term = cur_term; 213 SP->_lines = slines; 214 SP->_lines_avail = slines; 215 SP->_columns = scolumns; 216 SP->_cursrow = -1; 217 SP->_curscol = -1; 218 SP->_nl = TRUE; 219 SP->_raw = FALSE; 220 SP->_cbreak = 0; 221 SP->_echo = TRUE; 222 SP->_fifohead = -1; 223 SP->_endwin = TRUE; 224 SP->_ofp = output; 225 SP->_cursor = -1; /* cannot know real cursor shape */ 226 227 #if NCURSES_NO_PADDING 228 SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0; 229 TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used", 230 SP->_no_padding ? " not" : "")); 231 #endif 232 233 #if NCURSES_EXT_FUNCS 234 SP->_default_color = FALSE; 235 SP->_has_sgr_39_49 = FALSE; 236 237 /* 238 * Set our assumption of the terminal's default foreground and background 239 * colors. The curs_color man-page states that we can assume that the 240 * background is black. The origin of this assumption appears to be 241 * terminals that displayed colored text, but no colored backgrounds, e.g., 242 * the first colored terminals around 1980. More recent ones with better 243 * technology can display not only colored backgrounds, but all 244 * combinations. So a terminal might be something other than "white" on 245 * black (green/black looks monochrome too), but black on white or even 246 * on ivory. 247 * 248 * White-on-black is the simplest thing to use for monochrome. Almost 249 * all applications that use color paint both text and background, so 250 * the distinction is moot. But a few do not - which is why we leave this 251 * configurable (a better solution is to use assume_default_colors() for 252 * the rare applications that do require that sort of appearance, since 253 * is appears that more users expect to be able to make a white-on-black 254 * or black-on-white display under control of the application than not). 255 */ 256 #ifdef USE_ASSUMED_COLOR 257 SP->_default_fg = COLOR_WHITE; 258 SP->_default_bg = COLOR_BLACK; 259 #else 260 SP->_default_fg = C_MASK; 261 SP->_default_bg = C_MASK; 262 #endif 263 264 #if USE_COLORFGBG 265 /* 266 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed 267 * default colors. Note that rxvt (mis)uses bold colors, equating a bold 268 * color to that value plus 8. We'll only use the non-bold color for now - 269 * decide later if it is worth having default attributes as well. 270 */ 271 if (getenv("COLORFGBG") != 0) { 272 char *p = getenv("COLORFGBG"); 273 p = extract_fgbg(p, &(SP->_default_fg)); 274 p = extract_fgbg(p, &(SP->_default_bg)); 275 } 276 #endif 277 #endif /* NCURSES_EXT_FUNCS */ 278 279 SP->_maxclick = DEFAULT_MAXCLICK; 280 SP->_mouse_event = no_mouse_event; 281 SP->_mouse_inline = no_mouse_inline; 282 SP->_mouse_parse = no_mouse_parse; 283 SP->_mouse_resume = no_mouse_resume; 284 SP->_mouse_wrap = no_mouse_wrap; 285 SP->_mouse_fd = -1; 286 287 /* initialize the panel hooks */ 288 SP->_panelHook.top_panel = (struct panel *) 0; 289 SP->_panelHook.bottom_panel = (struct panel *) 0; 290 SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0; 291 292 /* 293 * If we've no magic cookie support, we suppress attributes that xmc 294 * would affect, i.e., the attributes that affect the rendition of a 295 * space. Note that this impacts the alternate character set mapping 296 * as well. 297 */ 298 if (magic_cookie_glitch > 0) { 299 300 SP->_xmc_triggers = termattrs() & ( 301 A_ALTCHARSET | 302 A_BLINK | 303 A_BOLD | 304 A_REVERSE | 305 A_STANDOUT | 306 A_UNDERLINE 307 ); 308 SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~ (A_BOLD); 309 310 T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress))); 311 #if USE_XMC_SUPPORT 312 /* 313 * To keep this simple, suppress all of the optimization hooks 314 * except for clear_screen and the cursor addressing. 315 */ 316 clr_eol = 0; 317 clr_eos = 0; 318 set_attributes = 0; 319 #else 320 magic_cookie_glitch = ABSENT_NUMERIC; 321 acs_chars = 0; 322 #endif 323 } 324 _nc_init_acs(); 325 memcpy(SP->_acs_map, acs_map, sizeof(chtype) * ACS_LEN); 326 327 _nc_idcok = TRUE; 328 _nc_idlok = FALSE; 329 330 _nc_windows = 0; /* no windows yet */ 331 332 SP->oldhash = 0; 333 SP->newhash = 0; 334 335 T(("creating newscr")); 336 if ((newscr = newwin(slines, scolumns, 0, 0)) == 0) 337 return ERR; 338 339 T(("creating curscr")); 340 if ((curscr = newwin(slines, scolumns, 0, 0)) == 0) 341 return ERR; 342 343 SP->_newscr = newscr; 344 SP->_curscr = curscr; 345 #if USE_SIZECHANGE 346 SP->_resize = resizeterm; 347 #endif 348 349 newscr->_clear = TRUE; 350 curscr->_clear = FALSE; 351 352 for (i = 0, rsp = rippedoff; rsp->line && (i < N_RIPS); rsp++, i++) { 353 if (rsp->hook) { 354 WINDOW *w; 355 int count = (rsp->line < 0) ? -rsp->line : rsp->line; 356 357 if (rsp->line < 0) { 358 w = newwin(count, scolumns, SP->_lines_avail - count, 0); 359 if (w) { 360 rsp->w = w; 361 rsp->hook(w, scolumns); 362 bottom_stolen += count; 363 } else 364 return ERR; 365 } else { 366 w = newwin(count, scolumns, 0, 0); 367 if (w) { 368 rsp->w = w; 369 rsp->hook(w, scolumns); 370 SP->_topstolen += count; 371 } else 372 return ERR; 373 } 374 SP->_lines_avail -= count; 375 } 376 rsp->line = 0; 377 } 378 /* reset the stack */ 379 rsp = rippedoff; 380 381 T(("creating stdscr")); 382 assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines); 383 if ((stdscr = newwin(LINES = SP->_lines_avail, scolumns, 0, 0)) == 0) 384 return ERR; 385 SP->_stdscr = stdscr; 386 387 def_shell_mode(); 388 def_prog_mode(); 389 390 return OK; 391 } 392 393 /* The internal implementation interprets line as the number of 394 lines to rip off from the top or bottom. 395 */ 396 NCURSES_EXPORT(int) 397 _nc_ripoffline(int line, int (*init) (WINDOW *, int)) 398 { 399 if (line == 0) 400 return (OK); 401 402 if (rsp >= rippedoff + N_RIPS) 403 return (ERR); 404 405 rsp->line = line; 406 rsp->hook = init; 407 rsp->w = 0; 408 rsp++; 409 410 return (OK); 411 } 412 413 NCURSES_EXPORT(int) 414 ripoffline(int line, int (*init) (WINDOW *, int)) 415 { 416 T((T_CALLED("ripoffline(%d,%p)"), line, init)); 417 418 if (line == 0) 419 returnCode(OK); 420 421 returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init)); 422 } 423