1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
38 ** The routine set_term().
42 #include <curses.priv.h>
44 #include <term.h> /* cur_term */
47 MODULE_ID("$Id: lib_set_term.c,v 1.93 2006/12/30 21:40:58 tom Exp $")
49 NCURSES_EXPORT(SCREEN *)
50 set_term(SCREEN *screenp)
54 T((T_CALLED("set_term(%p)"), screenp));
57 _nc_set_screen(screenp);
59 set_curterm(SP->_term);
63 COLORS = SP->_color_count;
64 COLOR_PAIRS = SP->_pair_count;
66 T((T_RETURN("%p"), oldSP));
71 _nc_free_keytry(TRIES * kt)
74 _nc_free_keytry(kt->child);
75 _nc_free_keytry(kt->sibling);
81 * Free the storage associated with the given SCREEN sp.
86 SCREEN **scan = &_nc_screen_chain;
89 T((T_CALLED("delscreen(%p)"), sp));
93 *scan = sp->_next_screen;
96 scan = &(*scan)->_next_screen;
99 (void) _nc_freewin(sp->_curscr);
100 (void) _nc_freewin(sp->_newscr);
101 (void) _nc_freewin(sp->_stdscr);
104 if (sp->_slk->ent != 0) {
105 for (i = 0; i < sp->_slk->labcnt; ++i) {
106 FreeIfNeeded(sp->_slk->ent[i].ent_text);
107 FreeIfNeeded(sp->_slk->ent[i].form_text);
115 _nc_free_keytry(sp->_keytry);
118 _nc_free_keytry(sp->_key_ok);
121 FreeIfNeeded(sp->_current_attr);
123 FreeIfNeeded(sp->_color_table);
124 FreeIfNeeded(sp->_color_pairs);
126 FreeIfNeeded(sp->oldhash);
127 FreeIfNeeded(sp->newhash);
128 FreeIfNeeded(sp->hashtab);
130 FreeIfNeeded(sp->_acs_map);
131 FreeIfNeeded(sp->_screen_acs_map);
133 del_curterm(sp->_term);
136 * If the associated output stream has been closed, we can discard the
137 * set-buffer. Limit the error check to EBADF, since fflush may fail
138 * for other reasons than trying to operate upon a closed stream.
142 && fflush(sp->_ofp) != 0
150 * If this was the current screen, reset everything that the
151 * application might try to use (except cur_term, which may have
152 * multiple references in different screens).
165 static ripoff_t rippedoff[5];
166 static ripoff_t *rsp = rippedoff;
167 #define N_RIPS SIZEOF(SP->_rippedoff)
170 no_mouse_event(SCREEN *sp GCC_UNUSED)
176 no_mouse_inline(SCREEN *sp GCC_UNUSED)
182 no_mouse_parse(int code GCC_UNUSED)
188 no_mouse_resume(SCREEN *sp GCC_UNUSED)
193 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
197 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
199 extract_fgbg(char *src, int *result)
202 long value = strtol(src, &dst, 0);
206 } else if (value >= 0) {
209 while (*dst != 0 && *dst != ';')
217 /* OS-independent screen initializations */
219 _nc_setupscreen(int slines,
225 int bottom_stolen = 0;
227 bool support_cookies = USE_XMC_SUPPORT;
229 T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
230 slines, scolumns, output, filtered, slk_format));
232 assert(SP == 0); /* has been reset in newterm() ! */
233 if (!_nc_alloc_screen()
234 || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
235 || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
239 T(("created SP %p", SP));
240 SP->_next_screen = _nc_screen_chain;
241 _nc_screen_chain = SP;
243 if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
246 SP->_filtered = filtered;
248 /* implement filter mode */
253 cursor_down = parm_down_cursor = 0;
255 cursor_up = parm_up_cursor = 0;
258 cursor_home = carriage_return;
259 T(("filter screensize %dx%d", LINES, COLS));
262 /* If we must simulate soft labels, grab off the line to be used.
263 We assume that we must simulate, if it is none of the standard
264 formats (4-4 or 3-2-3) for which there may be some hardware
266 if (num_labels <= 0 || !SLK_STDFMT(slk_format)) {
268 if (ERR == _nc_ripoffline(-SLK_LINES(slk_format),
274 T(("setting output mode to binary"));
276 setmode(output, O_BINARY);
278 _nc_set_buffer(output, TRUE);
279 SP->_term = cur_term;
281 SP->_lines_avail = slines;
282 SP->_columns = scolumns;
292 SP->_cursor = -1; /* cannot know real cursor shape */
294 #if NCURSES_NO_PADDING
295 SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
296 TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
297 SP->_no_padding ? " not" : ""));
300 #if NCURSES_EXT_FUNCS
301 SP->_default_color = FALSE;
302 SP->_has_sgr_39_49 = FALSE;
305 * Set our assumption of the terminal's default foreground and background
306 * colors. The curs_color man-page states that we can assume that the
307 * background is black. The origin of this assumption appears to be
308 * terminals that displayed colored text, but no colored backgrounds, e.g.,
309 * the first colored terminals around 1980. More recent ones with better
310 * technology can display not only colored backgrounds, but all
311 * combinations. So a terminal might be something other than "white" on
312 * black (green/black looks monochrome too), but black on white or even
315 * White-on-black is the simplest thing to use for monochrome. Almost
316 * all applications that use color paint both text and background, so
317 * the distinction is moot. But a few do not - which is why we leave this
318 * configurable (a better solution is to use assume_default_colors() for
319 * the rare applications that do require that sort of appearance, since
320 * is appears that more users expect to be able to make a white-on-black
321 * or black-on-white display under control of the application than not).
323 #ifdef USE_ASSUMED_COLOR
324 SP->_default_fg = COLOR_WHITE;
325 SP->_default_bg = COLOR_BLACK;
327 SP->_default_fg = C_MASK;
328 SP->_default_bg = C_MASK;
332 * Allow those assumed/default color assumptions to be overridden at
335 if (getenv("NCURSES_ASSUMED_COLORS") != 0) {
336 char *p = getenv("NCURSES_ASSUMED_COLORS");
339 int count = sscanf(p, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
341 SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
343 SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
345 TR(TRACE_CHARPUT | TRACE_MOVE,
346 ("from environment assumed fg=%d, bg=%d",
353 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
354 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
355 * color to that value plus 8. We'll only use the non-bold color for now -
356 * decide later if it is worth having default attributes as well.
358 if (getenv("COLORFGBG") != 0) {
359 char *p = getenv("COLORFGBG");
360 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
361 p = extract_fgbg(p, &(SP->_default_fg));
362 p = extract_fgbg(p, &(SP->_default_bg));
363 if (*p) /* assume rxvt was compiled with xpm support */
364 p = extract_fgbg(p, &(SP->_default_bg));
365 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
366 SP->_default_fg, SP->_default_bg));
367 if (SP->_default_fg >= max_colors) {
368 if (set_a_foreground != ABSENT_STRING
369 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
370 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
372 SP->_default_fg %= max_colors;
375 if (SP->_default_bg >= max_colors) {
376 if (set_a_background != ABSENT_STRING
377 && !strcmp(set_a_background, "\033[4%p1%dm")) {
378 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
380 SP->_default_bg %= max_colors;
385 #endif /* NCURSES_EXT_FUNCS */
387 SP->_maxclick = DEFAULT_MAXCLICK;
388 SP->_mouse_event = no_mouse_event;
389 SP->_mouse_inline = no_mouse_inline;
390 SP->_mouse_parse = no_mouse_parse;
391 SP->_mouse_resume = no_mouse_resume;
392 SP->_mouse_wrap = no_mouse_wrap;
395 /* initialize the panel hooks */
396 SP->_panelHook.top_panel = (struct panel *) 0;
397 SP->_panelHook.bottom_panel = (struct panel *) 0;
398 SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
401 * If we've no magic cookie support, we suppress attributes that xmc would
402 * affect, i.e., the attributes that affect the rendition of a space.
404 SP->_ok_attributes = termattrs();
406 SP->_ok_attributes |= A_COLOR;
410 * If we have no magic-cookie support compiled-in, or if it is suppressed
411 * in the environment, reset the support-flag.
413 if (magic_cookie_glitch >= 0) {
414 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
415 support_cookies = FALSE;
420 if (!support_cookies && magic_cookie_glitch >= 0) {
421 T(("will disable attributes to work w/o magic cookies"));
424 if (magic_cookie_glitch > 0) { /* tvi, wyse */
426 SP->_xmc_triggers = SP->_ok_attributes & (
438 * We "should" treat colors as an attribute. The wyse350 (and its
439 * clones) appear to be the only ones that have both colors and magic
443 SP->_xmc_triggers |= A_COLOR;
446 SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
448 T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
450 * Supporting line-drawing may be possible. But make the regular
451 * video attributes work first.
453 acs_chars = ABSENT_STRING;
454 ena_acs = ABSENT_STRING;
455 enter_alt_charset_mode = ABSENT_STRING;
456 exit_alt_charset_mode = ABSENT_STRING;
459 * To keep the cookie support simple, suppress all of the optimization
460 * hooks except for clear_screen and the cursor addressing.
462 if (support_cookies) {
463 clr_eol = ABSENT_STRING;
464 clr_eos = ABSENT_STRING;
465 set_attributes = ABSENT_STRING;
468 } else if (magic_cookie_glitch == 0) { /* hpterm */
472 * If magic cookies are not supported, cancel the strings that set
475 if (!support_cookies && magic_cookie_glitch >= 0) {
476 magic_cookie_glitch = ABSENT_NUMERIC;
477 set_attributes = ABSENT_STRING;
478 enter_blink_mode = ABSENT_STRING;
479 enter_bold_mode = ABSENT_STRING;
480 enter_dim_mode = ABSENT_STRING;
481 enter_reverse_mode = ABSENT_STRING;
482 enter_standout_mode = ABSENT_STRING;
483 enter_underline_mode = ABSENT_STRING;
486 /* initialize normal acs before wide, since we use mapping in the latter */
488 #if USE_WIDEC_SUPPORT
491 SP->_screen_acs_fix = (_nc_unicode_locale() && _nc_locale_breaks_acs());
493 char *env = _nc_get_locale();
494 SP->_legacy_coding = ((env == 0)
496 || !strcmp(env, "POSIX"));
503 _nc_windows = 0; /* no windows yet */
508 T(("creating newscr"));
509 if ((newscr = newwin(slines, scolumns, 0, 0)) == 0)
512 T(("creating curscr"));
513 if ((curscr = newwin(slines, scolumns, 0, 0)) == 0)
516 SP->_newscr = newscr;
517 SP->_curscr = curscr;
519 SP->_resize = resizeterm;
522 newscr->_clear = TRUE;
523 curscr->_clear = FALSE;
528 for (i = 0, rsp = rippedoff; rsp->line && (i < (int) N_RIPS); rsp++, i++) {
529 T(("ripping off line %d at %s", i, rsp->line < 0 ? "bottom" : "top"));
530 SP->_rippedoff[i] = rippedoff[i];
532 int count = (rsp->line < 0) ? -rsp->line : rsp->line;
534 SP->_rippedoff[i].w = newwin(count,
537 ? SP->_lines_avail - count
540 if (SP->_rippedoff[i].w != 0)
541 SP->_rippedoff[i].hook(SP->_rippedoff[i].w, scolumns);
545 bottom_stolen += count;
547 SP->_topstolen += count;
548 SP->_lines_avail -= count;
553 /* reset the stack */
556 T(("creating stdscr"));
557 assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
558 if ((stdscr = newwin(LINES = SP->_lines_avail, scolumns, 0, 0)) == 0)
560 SP->_stdscr = stdscr;
566 * The internal implementation interprets line as the number of lines to rip
567 * off from the top or bottom.
570 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
572 T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
576 if (rsp >= rippedoff + N_RIPS)
589 ripoffline(int line, int (*init) (WINDOW *, int))
591 T((T_CALLED("ripoffline(%d,%p)"), line, init));
596 returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));