1 /****************************************************************************
2 * Copyright (c) 1998-2016,2017 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 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
39 ** The routine set_term().
43 #include <curses.priv.h>
47 #define CUR SP_TERMTYPE
49 MODULE_ID("$Id: lib_set_term.c,v 1.166 2017/07/01 16:37:24 tom Exp $")
51 #ifdef USE_TERM_DRIVER
52 #define MaxColors InfoOf(sp).maxcolors
53 #define NumLabels InfoOf(sp).numlabels
55 #define MaxColors max_colors
56 #define NumLabels num_labels
59 NCURSES_EXPORT(SCREEN *)
60 set_term(SCREEN *screenp)
65 T((T_CALLED("set_term(%p)"), (void *) screenp));
67 _nc_lock_global(curses);
69 oldSP = CURRENT_SCREEN;
70 _nc_set_screen(screenp);
74 TINFO_SET_CURTERM(newSP, newSP->_term);
76 curscr = CurScreen(newSP);
77 newscr = NewScreen(newSP);
78 stdscr = StdScreen(newSP);
79 COLORS = newSP->_color_count;
80 COLOR_PAIRS = newSP->_pair_count;
83 TINFO_SET_CURTERM(oldSP, 0);
93 _nc_unlock_global(curses);
95 T((T_RETURN("%p"), (void *) oldSP));
100 _nc_free_keytry(TRIES * kt)
103 _nc_free_keytry(kt->child);
104 _nc_free_keytry(kt->sibling);
110 delink_screen(SCREEN *sp)
116 for (each_screen(temp)) {
119 last->_next_screen = sp->_next_screen;
121 _nc_screen_chain = sp->_next_screen;
131 * Free the storage associated with the given SCREEN sp.
134 delscreen(SCREEN *sp)
137 T((T_CALLED("delscreen(%p)"), (void *) sp));
139 _nc_lock_global(curses);
140 if (delink_screen(sp)) {
143 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
144 for (rop = safe_ripoff_stack;
145 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
148 (void) delwin(rop->win);
155 (void) _nc_freewin(CurScreen(sp));
156 (void) _nc_freewin(NewScreen(sp));
157 (void) _nc_freewin(StdScreen(sp));
161 if (sp->_slk->ent != 0) {
164 for (i = 0; i < sp->_slk->labcnt; ++i) {
165 FreeIfNeeded(sp->_slk->ent[i].ent_text);
166 FreeIfNeeded(sp->_slk->ent[i].form_text);
174 _nc_free_keytry(sp->_keytry);
177 _nc_free_keytry(sp->_key_ok);
180 FreeIfNeeded(sp->_current_attr);
182 FreeIfNeeded(sp->_color_table);
183 FreeIfNeeded(sp->_color_pairs);
185 FreeIfNeeded(sp->_oldnum_list);
186 FreeIfNeeded(sp->oldhash);
187 FreeIfNeeded(sp->newhash);
188 FreeIfNeeded(sp->hashtab);
190 FreeIfNeeded(sp->_acs_map);
191 FreeIfNeeded(sp->_screen_acs_map);
193 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
194 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx sp->_term);
195 FreeIfNeeded(sp->out_buffer);
196 if (_nc_find_prescr() == sp) {
202 * If this was the current screen, reset everything that the
203 * application might try to use (except cur_term, which may have
204 * multiple references in different screens).
206 if (sp == CURRENT_SCREEN) {
215 #if USE_WIDEC_SUPPORT
217 FreeIfNeeded(_nc_wacs);
223 _nc_unlock_global(curses);
229 no_mouse_event(SCREEN *sp GCC_UNUSED)
235 no_mouse_inline(SCREEN *sp GCC_UNUSED)
241 no_mouse_parse(SCREEN *sp GCC_UNUSED, int code GCC_UNUSED)
247 no_mouse_resume(SCREEN *sp GCC_UNUSED)
252 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
256 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
258 extract_fgbg(const char *src, int *result)
262 long value = strtol(src, &tmp, 0);
264 if ((dst = tmp) == 0) {
266 } else if (value >= 0) {
267 *result = (int) value;
269 while (*dst != 0 && *dst != ';')
277 #define ReturnScreenError() do { _nc_set_screen(0); \
278 returnCode(ERR); } while (0)
280 /* OS-independent screen initializations */
282 NCURSES_SP_NAME(_nc_setupscreen) (
292 #ifndef USE_TERM_DRIVER
293 static const TTY null_TTY; /* all zeros iff uninitialized */
296 int bottom_stolen = 0;
298 #ifndef USE_TERM_DRIVER
299 bool support_cookies = USE_XMC_SUPPORT;
302 T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
303 slines, scolumns, (void *) output, filtered, slk_format));
305 assert(CURRENT_SCREEN == 0); /* has been reset in newterm() ! */
312 sp = _nc_alloc_screen_sp();
313 T(("_nc_alloc_screen_sp %p", (void *) sp));
317 || ((sp->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
318 || ((sp->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
322 T(("created SP %p", (void *) sp));
323 sp->_next_screen = _nc_screen_chain;
324 _nc_screen_chain = sp;
326 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
330 if (!_nc_alloc_screen()
331 || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
332 || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
336 T(("created SP %p", (void *) SP));
338 sp = SP; /* fixup so SET_LINES and SET_COLS works */
339 sp->_next_screen = _nc_screen_chain;
340 _nc_screen_chain = sp;
342 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
347 * We should always check the screensize, just in case.
350 sp->_term = cur_term;
351 #ifdef USE_TERM_DRIVER
353 _nc_get_screensize(sp, sp->_term, &slines, &scolumns);
355 _nc_get_screensize(sp, &slines, &scolumns);
360 T((T_CREATE("screen %s %dx%d"),
361 NCURSES_SP_NAME(termname) (NCURSES_SP_ARG), slines, scolumns));
363 sp->_filtered = filtered;
365 /* implement filter mode */
369 #ifdef USE_TERM_DRIVER
370 CallDriver(sp, td_setfilter);
373 cursor_down = parm_down_cursor = 0;
375 cursor_up = parm_up_cursor = 0;
378 cursor_home = carriage_return;
380 T(("filter screensize %dx%d", slines, scolumns));
383 T(("setting output mode to binary"));
385 setmode(output, O_BINARY);
387 NCURSES_SP_NAME(_nc_set_buffer) (NCURSES_SP_ARGx output, TRUE);
388 sp->_lines = (NCURSES_SIZE_T) slines;
389 sp->_lines_avail = (NCURSES_SIZE_T) slines;
390 sp->_columns = (NCURSES_SIZE_T) scolumns;
393 sp->_ofd = output ? fileno(output) : -1;
395 sp->out_limit = (size_t) ((2 + slines) * (6 + scolumns));
396 if ((sp->out_buffer = malloc(sp->out_limit)) == 0)
403 #if NCURSES_EXT_FUNCS
404 sp->_default_color = FALSE;
405 sp->_has_sgr_39_49 = FALSE;
408 * Set our assumption of the terminal's default foreground and background
409 * colors. The curs_color man-page states that we can assume that the
410 * background is black. The origin of this assumption appears to be
411 * terminals that displayed colored text, but no colored backgrounds, e.g.,
412 * the first colored terminals around 1980. More recent ones with better
413 * technology can display not only colored backgrounds, but all
414 * combinations. So a terminal might be something other than "white" on
415 * black (green/black looks monochrome too), but black on white or even
418 * White-on-black is the simplest thing to use for monochrome. Almost
419 * all applications that use color paint both text and background, so
420 * the distinction is moot. But a few do not - which is why we leave this
421 * configurable (a better solution is to use assume_default_colors() for
422 * the rare applications that do require that sort of appearance, since
423 * is appears that more users expect to be able to make a white-on-black
424 * or black-on-white display under control of the application than not).
426 #ifdef USE_ASSUMED_COLOR
427 sp->_default_fg = COLOR_WHITE;
428 sp->_default_bg = COLOR_BLACK;
430 sp->_default_fg = COLOR_DEFAULT;
431 sp->_default_bg = COLOR_DEFAULT;
435 * Allow those assumed/default color assumptions to be overridden at
438 if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
441 int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
443 sp->_default_fg = ((fg >= 0 && fg < MaxColors) ? fg : COLOR_DEFAULT);
445 sp->_default_bg = ((bg >= 0 && bg < MaxColors) ? bg : COLOR_DEFAULT);
447 TR(TRACE_CHARPUT | TRACE_MOVE,
448 ("from environment assumed fg=%d, bg=%d",
455 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
456 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
457 * color to that value plus 8. We'll only use the non-bold color for now -
458 * decide later if it is worth having default attributes as well.
460 if (getenv("COLORFGBG") != 0) {
461 const char *p = getenv("COLORFGBG");
462 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
463 p = extract_fgbg(p, &(sp->_default_fg));
464 p = extract_fgbg(p, &(sp->_default_bg));
465 if (*p) /* assume rxvt was compiled with xpm support */
466 p = extract_fgbg(p, &(sp->_default_bg));
467 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
468 sp->_default_fg, sp->_default_bg));
469 if (sp->_default_fg >= MaxColors) {
470 if (set_a_foreground != ABSENT_STRING
471 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
472 set_a_foreground = strdup("\033[3%?%p1%{8}%>%t9%e%p1%d%;m");
474 sp->_default_fg %= MaxColors;
477 if (sp->_default_bg >= MaxColors) {
478 if (set_a_background != ABSENT_STRING
479 && !strcmp(set_a_background, "\033[4%p1%dm")) {
480 set_a_background = strdup("\033[4%?%p1%{8}%>%t9%e%p1%d%;m");
482 sp->_default_bg %= MaxColors;
487 #endif /* NCURSES_EXT_FUNCS */
489 sp->_maxclick = DEFAULT_MAXCLICK;
490 sp->_mouse_event = no_mouse_event;
491 sp->_mouse_inline = no_mouse_inline;
492 sp->_mouse_parse = no_mouse_parse;
493 sp->_mouse_resume = no_mouse_resume;
494 sp->_mouse_wrap = no_mouse_wrap;
498 * If we've no magic cookie support, we suppress attributes that xmc would
499 * affect, i.e., the attributes that affect the rendition of a space.
501 sp->_ok_attributes = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
502 if (NCURSES_SP_NAME(has_colors) (NCURSES_SP_ARG)) {
503 sp->_ok_attributes |= A_COLOR;
505 #ifdef USE_TERM_DRIVER
510 * If we have no magic-cookie support compiled-in, or if it is suppressed
511 * in the environment, reset the support-flag.
513 if (magic_cookie_glitch >= 0) {
514 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
515 support_cookies = FALSE;
520 if (!support_cookies && magic_cookie_glitch >= 0) {
521 T(("will disable attributes to work w/o magic cookies"));
524 if (magic_cookie_glitch > 0) { /* tvi, wyse */
526 sp->_xmc_triggers = sp->_ok_attributes & XMC_CONFLICT;
529 * We "should" treat colors as an attribute. The wyse350 (and its
530 * clones) appear to be the only ones that have both colors and magic
534 sp->_xmc_triggers |= A_COLOR;
537 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
539 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
541 * Supporting line-drawing may be possible. But make the regular
542 * video attributes work first.
544 acs_chars = ABSENT_STRING;
545 ena_acs = ABSENT_STRING;
546 enter_alt_charset_mode = ABSENT_STRING;
547 exit_alt_charset_mode = ABSENT_STRING;
550 * To keep the cookie support simple, suppress all of the optimization
551 * hooks except for clear_screen and the cursor addressing.
553 if (support_cookies) {
554 clr_eol = ABSENT_STRING;
555 clr_eos = ABSENT_STRING;
556 set_attributes = ABSENT_STRING;
559 } else if (magic_cookie_glitch == 0) { /* hpterm */
563 * If magic cookies are not supported, cancel the strings that set
566 if (!support_cookies && magic_cookie_glitch >= 0) {
567 magic_cookie_glitch = ABSENT_NUMERIC;
568 set_attributes = ABSENT_STRING;
569 enter_blink_mode = ABSENT_STRING;
570 enter_bold_mode = ABSENT_STRING;
571 enter_dim_mode = ABSENT_STRING;
572 enter_reverse_mode = ABSENT_STRING;
573 enter_standout_mode = ABSENT_STRING;
574 enter_underline_mode = ABSENT_STRING;
577 /* initialize normal acs before wide, since we use mapping in the latter */
578 #if !USE_WIDEC_SUPPORT
579 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
582 enter_alt_charset_mode = NULL;
583 exit_alt_charset_mode = NULL;
584 set_attributes = NULL;
589 NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_ARG);
590 #if USE_WIDEC_SUPPORT
591 sp->_screen_unicode = _nc_unicode_locale();
599 sp->_screen_acs_fix = (sp->_screen_unicode
600 && _nc_locale_breaks_acs(sp->_term));
602 env = _nc_get_locale();
603 sp->_legacy_coding = ((env == 0)
605 || !strcmp(env, "POSIX"));
606 T(("legacy-coding %d", sp->_legacy_coding));
608 sp->_nc_sp_idcok = TRUE;
609 sp->_nc_sp_idlok = FALSE;
614 T(("creating newscr"));
615 NewScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
617 if (NewScreen(sp) == 0) {
620 T(("creating curscr"));
621 CurScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
623 if (CurScreen(sp) == 0) {
627 newscr = NewScreen(sp);
628 curscr = CurScreen(sp);
631 sp->_resize = NCURSES_SP_NAME(resizeterm);
632 sp->_ungetch = safe_ungetch;
635 NewScreen(sp)->_clear = TRUE;
636 CurScreen(sp)->_clear = FALSE;
639 * Get the current tty-modes. setupterm() may already have done this,
640 * unless we use the term-driver.
642 #ifndef USE_TERM_DRIVER
644 !memcmp(&cur_term->Ottyb, &null_TTY, sizeof(TTY)))
647 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
648 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
651 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
654 for (rop = safe_ripoff_stack;
655 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
658 /* If we must simulate soft labels, grab off the line to be used.
659 We assume that we must simulate, if it is none of the standard
660 formats (4-4 or 3-2-3) for which there may be some hardware
662 if (rop->hook == _nc_slk_initialize) {
663 if (!(NumLabels <= 0 || !SLK_STDFMT(slk_format))) {
671 count = (rop->line < 0) ? -rop->line : rop->line;
672 T(("ripping off %i lines at %s", count,
677 w = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
680 ? sp->_lines_avail - count
685 rop->hook(w, scolumns);
690 bottom_stolen += count;
692 sp->_topstolen = (NCURSES_SIZE_T) (sp->_topstolen + count);
694 sp->_lines_avail = (NCURSES_SIZE_T) (sp->_lines_avail - count);
697 /* reset the stack */
698 safe_ripoff_sp = safe_ripoff_stack;
701 T(("creating stdscr"));
702 assert((sp->_lines_avail + sp->_topstolen + bottom_stolen) == slines);
703 if ((StdScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
705 scolumns, 0, 0)) == 0) {
708 SET_LINES(sp->_lines_avail);
710 stdscr = StdScreen(sp);
712 sp->_prescreen = FALSE;
718 _nc_setupscreen(int slines GCC_UNUSED,
719 int scolumns GCC_UNUSED,
725 int rc = NCURSES_SP_NAME(_nc_setupscreen) (&sp,
738 * The internal implementation interprets line as the number of lines to rip
739 * off from the top or bottom.
742 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
744 int (*init) (WINDOW *, int))
749 T((T_CALLED("ripoffline(%p,%d,%p)"), (void *) SP_PARM, line, TR_FUNC(init)));
752 if (SP_PARM != 0 && SP_PARM->_prescreen)
758 if (safe_ripoff_sp == 0) {
759 safe_ripoff_sp = safe_ripoff_stack;
761 if (safe_ripoff_sp < safe_ripoff_stack + N_RIPS) {
762 safe_ripoff_sp->line = line;
763 safe_ripoff_sp->hook = init;
765 T(("ripped-off %d:%d chunks",
766 (int) (safe_ripoff_sp - safe_ripoff_stack), N_RIPS));
777 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
780 _nc_lock_global(prescreen);
782 rc = NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN_PRE, line, init);
783 _nc_unlock_global(prescreen);
789 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
791 int (*init) (WINDOW *, int))
794 return NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
801 ripoffline(int line, int (*init) (WINDOW *, int))
804 _nc_lock_global(prescreen);
806 rc = NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN_PRE, line, init);
807 _nc_unlock_global(prescreen);