1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer 2009 *
35 ****************************************************************************/
40 ** The routine set_term().
44 #include <curses.priv.h>
49 /* use dynamic loader to avoid linkage dependency */
55 #define CUR SP_TERMTYPE
57 MODULE_ID("$Id: lib_set_term.c,v 1.179 2021/05/08 21:48:34 tom Exp $")
59 #ifdef USE_TERM_DRIVER
60 #define MaxColors InfoOf(sp).maxcolors
61 #define NumLabels InfoOf(sp).numlabels
63 #define MaxColors max_colors
64 #define NumLabels num_labels
67 NCURSES_EXPORT(SCREEN *)
68 set_term(SCREEN *screenp)
73 T((T_CALLED("set_term(%p)"), (void *) screenp));
75 _nc_lock_global(curses);
77 oldSP = CURRENT_SCREEN;
78 _nc_set_screen(screenp);
82 TINFO_SET_CURTERM(newSP, newSP->_term);
84 curscr = CurScreen(newSP);
85 newscr = NewScreen(newSP);
86 stdscr = StdScreen(newSP);
87 COLORS = newSP->_color_count;
88 COLOR_PAIRS = newSP->_pair_count;
91 TINFO_SET_CURTERM(oldSP, 0);
101 _nc_unlock_global(curses);
103 T((T_RETURN("%p"), (void *) oldSP));
108 _nc_free_keytry(TRIES * kt)
111 _nc_free_keytry(kt->child);
112 _nc_free_keytry(kt->sibling);
118 delink_screen(SCREEN *sp)
124 for (each_screen(temp)) {
127 last->_next_screen = sp->_next_screen;
129 _nc_screen_chain = sp->_next_screen;
139 * Free the storage associated with the given SCREEN sp.
142 delscreen(SCREEN *sp)
145 T((T_CALLED("delscreen(%p)"), (void *) sp));
147 _nc_lock_global(curses);
148 if (delink_screen(sp)) {
150 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
152 for (rop = safe_ripoff_stack;
153 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
156 (void) delwin(rop->win);
163 (void) _nc_freewin(CurScreen(sp));
164 (void) _nc_freewin(NewScreen(sp));
165 (void) _nc_freewin(StdScreen(sp));
169 if (sp->_slk->ent != 0) {
172 for (i = 0; i < sp->_slk->labcnt; ++i) {
173 FreeIfNeeded(sp->_slk->ent[i].ent_text);
174 FreeIfNeeded(sp->_slk->ent[i].form_text);
182 _nc_free_keytry(sp->_keytry);
185 _nc_free_keytry(sp->_key_ok);
188 FreeIfNeeded(sp->_current_attr);
190 FreeIfNeeded(sp->_color_table);
191 FreeIfNeeded(sp->_color_pairs);
193 FreeIfNeeded(sp->_oldnum_list);
194 FreeIfNeeded(sp->oldhash);
195 FreeIfNeeded(sp->newhash);
196 FreeIfNeeded(sp->hashtab);
198 FreeIfNeeded(sp->_acs_map);
199 FreeIfNeeded(sp->_screen_acs_map);
201 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
202 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx sp->_term);
203 FreeIfNeeded(sp->out_buffer);
204 if (_nc_find_prescr() == sp) {
209 if (sp->_dlopen_gpm != 0) {
210 dlclose(sp->_dlopen_gpm);
214 #endif /* USE_GPM_SUPPORT */
218 * If this was the current screen, reset everything that the
219 * application might try to use (except cur_term, which may have
220 * multiple references in different screens).
222 if (sp == CURRENT_SCREEN) {
231 #if USE_WIDEC_SUPPORT
233 FreeIfNeeded(_nc_wacs);
239 _nc_unlock_global(curses);
245 no_mouse_event(SCREEN *sp GCC_UNUSED)
251 no_mouse_inline(SCREEN *sp GCC_UNUSED)
257 no_mouse_parse(SCREEN *sp GCC_UNUSED, int code GCC_UNUSED)
263 no_mouse_resume(SCREEN *sp GCC_UNUSED)
268 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
272 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
274 extract_fgbg(const char *src, int *result)
278 long value = strtol(src, &tmp, 0);
280 if ((dst = tmp) == 0) {
282 } else if (value >= 0) {
283 *result = (int) value;
285 while (*dst != 0 && *dst != ';')
293 #define ReturnScreenError() do { _nc_set_screen(0); \
294 returnCode(ERR); } while (0)
296 /* OS-independent screen initializations */
298 NCURSES_SP_NAME(_nc_setupscreen) (
308 #ifndef USE_TERM_DRIVER
309 static const TTY null_TTY; /* all zeros iff uninitialized */
312 int bottom_stolen = 0;
314 #ifndef USE_TERM_DRIVER
315 bool support_cookies = USE_XMC_SUPPORT;
318 T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
319 slines, scolumns, (void *) output, filtered, slk_format));
321 assert(CURRENT_SCREEN == 0); /* has been reset in newterm() ! */
328 sp = _nc_alloc_screen_sp();
329 T(("_nc_alloc_screen_sp %p", (void *) sp));
335 if ((sp->_acs_map = typeCalloc(chtype, ACS_LEN)) == NULL) {
338 if ((sp->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == NULL) {
343 T(("created SP %p", (void *) sp));
344 sp->_next_screen = _nc_screen_chain;
345 _nc_screen_chain = sp;
347 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
351 if (!_nc_alloc_screen()
352 || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
353 || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
357 T(("created SP %p", (void *) SP));
359 sp = SP; /* fixup so SET_LINES and SET_COLS works */
360 sp->_next_screen = _nc_screen_chain;
361 _nc_screen_chain = sp;
363 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
369 * We should always check the screensize, just in case.
372 sp->_term = cur_term;
373 #ifdef USE_TERM_DRIVER
375 _nc_get_screensize(sp, sp->_term, &slines, &scolumns);
377 _nc_get_screensize(sp, &slines, &scolumns);
382 T((T_CREATE("screen %s %dx%d"),
383 NCURSES_SP_NAME(termname) (NCURSES_SP_ARG), slines, scolumns));
385 sp->_filtered = filtered;
387 /* implement filter mode */
391 #ifdef USE_TERM_DRIVER
392 CallDriver(sp, td_setfilter);
395 clear_screen = ABSENT_STRING;
396 cursor_address = ABSENT_STRING;
397 cursor_down = ABSENT_STRING;
398 cursor_up = ABSENT_STRING;
399 parm_down_cursor = ABSENT_STRING;
400 parm_up_cursor = ABSENT_STRING;
401 row_address = ABSENT_STRING;
402 cursor_home = carriage_return;
404 if (back_color_erase)
405 clr_eos = ABSENT_STRING;
408 T(("filter screensize %dx%d", slines, scolumns));
411 T(("setting output mode to binary"));
413 setmode(output, O_BINARY);
415 #if defined(EXP_WIN32_DRIVER)
416 T(("setting output mode to binary"));
418 _setmode(fileno(output), _O_BINARY);
420 sp->_lines = (NCURSES_SIZE_T) slines;
421 sp->_lines_avail = (NCURSES_SIZE_T) slines;
422 sp->_columns = (NCURSES_SIZE_T) scolumns;
425 sp->_ofd = output ? fileno(output) : -1;
427 #if defined(EXP_WIN32_DRIVER)
429 _setmode(fileno(output), _O_BINARY);
431 sp->out_limit = (size_t) ((2 + slines) * (6 + scolumns));
432 if ((sp->out_buffer = malloc(sp->out_limit)) == 0)
439 #if NCURSES_EXT_FUNCS
440 sp->_default_color = FALSE;
441 sp->_has_sgr_39_49 = FALSE;
444 * Set our assumption of the terminal's default foreground and background
445 * colors. The curs_color man-page states that we can assume that the
446 * background is black. The origin of this assumption appears to be
447 * terminals that displayed colored text, but no colored backgrounds, e.g.,
448 * the first colored terminals around 1980. More recent ones with better
449 * technology can display not only colored backgrounds, but all
450 * combinations. So a terminal might be something other than "white" on
451 * black (green/black looks monochrome too), but black on white or even
454 * White-on-black is the simplest thing to use for monochrome. Almost
455 * all applications that use color paint both text and background, so
456 * the distinction is moot. But a few do not - which is why we leave this
457 * configurable (a better solution is to use assume_default_colors() for
458 * the rare applications that do require that sort of appearance, since
459 * is appears that more users expect to be able to make a white-on-black
460 * or black-on-white display under control of the application than not).
462 #ifdef USE_ASSUMED_COLOR
463 sp->_default_fg = COLOR_WHITE;
464 sp->_default_bg = COLOR_BLACK;
466 sp->_default_fg = COLOR_DEFAULT;
467 sp->_default_bg = COLOR_DEFAULT;
471 * Allow those assumed/default color assumptions to be overridden at
474 if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
477 int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
479 sp->_default_fg = ((fg >= 0 && fg < MaxColors) ? fg : COLOR_DEFAULT);
481 sp->_default_bg = ((bg >= 0 && bg < MaxColors) ? bg : COLOR_DEFAULT);
483 TR(TRACE_CHARPUT | TRACE_MOVE,
484 ("from environment assumed fg=%d, bg=%d",
491 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
492 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
493 * color to that value plus 8. We'll only use the non-bold color for now -
494 * decide later if it is worth having default attributes as well.
496 if (getenv("COLORFGBG") != 0) {
497 const char *p = getenv("COLORFGBG");
498 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
499 p = extract_fgbg(p, &(sp->_default_fg));
500 p = extract_fgbg(p, &(sp->_default_bg));
501 if (*p) /* assume rxvt was compiled with xpm support */
502 extract_fgbg(p, &(sp->_default_bg));
503 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
504 sp->_default_fg, sp->_default_bg));
505 if (sp->_default_fg >= MaxColors) {
506 if (set_a_foreground != ABSENT_STRING
507 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
508 set_a_foreground = strdup("\033[3%?%p1%{8}%>%t9%e%p1%d%;m");
510 sp->_default_fg %= MaxColors;
513 if (sp->_default_bg >= MaxColors) {
514 if (set_a_background != ABSENT_STRING
515 && !strcmp(set_a_background, "\033[4%p1%dm")) {
516 set_a_background = strdup("\033[4%?%p1%{8}%>%t9%e%p1%d%;m");
518 sp->_default_bg %= MaxColors;
523 #endif /* NCURSES_EXT_FUNCS */
525 sp->_maxclick = DEFAULT_MAXCLICK;
526 sp->_mouse_event = no_mouse_event;
527 sp->_mouse_inline = no_mouse_inline;
528 sp->_mouse_parse = no_mouse_parse;
529 sp->_mouse_resume = no_mouse_resume;
530 sp->_mouse_wrap = no_mouse_wrap;
534 * If we've no magic cookie support, we suppress attributes that xmc would
535 * affect, i.e., the attributes that affect the rendition of a space.
537 sp->_ok_attributes = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
538 if (NCURSES_SP_NAME(has_colors) (NCURSES_SP_ARG)) {
539 sp->_ok_attributes |= A_COLOR;
541 #ifdef USE_TERM_DRIVER
546 * If we have no magic-cookie support compiled-in, or if it is suppressed
547 * in the environment, reset the support-flag.
549 if (magic_cookie_glitch >= 0) {
550 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
551 support_cookies = FALSE;
556 if (!support_cookies && magic_cookie_glitch >= 0) {
557 T(("will disable attributes to work w/o magic cookies"));
560 if (magic_cookie_glitch > 0) { /* tvi, wyse */
562 sp->_xmc_triggers = sp->_ok_attributes & XMC_CONFLICT;
565 * We "should" treat colors as an attribute. The wyse350 (and its
566 * clones) appear to be the only ones that have both colors and magic
570 sp->_xmc_triggers |= A_COLOR;
573 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
575 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
577 * Supporting line-drawing may be possible. But make the regular
578 * video attributes work first.
580 acs_chars = ABSENT_STRING;
581 ena_acs = ABSENT_STRING;
582 enter_alt_charset_mode = ABSENT_STRING;
583 exit_alt_charset_mode = ABSENT_STRING;
586 * To keep the cookie support simple, suppress all of the optimization
587 * hooks except for clear_screen and the cursor addressing.
589 if (support_cookies) {
590 clr_eol = ABSENT_STRING;
591 clr_eos = ABSENT_STRING;
592 set_attributes = ABSENT_STRING;
595 } else if (magic_cookie_glitch == 0) { /* hpterm */
599 * If magic cookies are not supported, cancel the strings that set
602 if (!support_cookies && magic_cookie_glitch >= 0) {
603 magic_cookie_glitch = ABSENT_NUMERIC;
604 set_attributes = ABSENT_STRING;
605 enter_blink_mode = ABSENT_STRING;
606 enter_bold_mode = ABSENT_STRING;
607 enter_dim_mode = ABSENT_STRING;
608 enter_reverse_mode = ABSENT_STRING;
609 enter_standout_mode = ABSENT_STRING;
610 enter_underline_mode = ABSENT_STRING;
613 /* initialize normal acs before wide, since we use mapping in the latter */
614 #if !USE_WIDEC_SUPPORT
615 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
618 enter_alt_charset_mode = NULL;
619 exit_alt_charset_mode = NULL;
620 set_attributes = NULL;
625 NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_ARG);
626 #if USE_WIDEC_SUPPORT
627 sp->_screen_unicode = _nc_unicode_locale();
635 sp->_screen_acs_fix = (sp->_screen_unicode
636 && _nc_locale_breaks_acs(sp->_term));
638 env = _nc_get_locale();
639 sp->_legacy_coding = ((env == 0)
641 || !strcmp(env, "POSIX"));
642 T(("legacy-coding %d", sp->_legacy_coding));
644 sp->_nc_sp_idcok = TRUE;
645 sp->_nc_sp_idlok = FALSE;
650 T(("creating newscr"));
651 NewScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
653 if (NewScreen(sp) == 0) {
656 T(("creating curscr"));
657 CurScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
659 if (CurScreen(sp) == 0) {
663 newscr = NewScreen(sp);
664 curscr = CurScreen(sp);
667 sp->_resize = NCURSES_SP_NAME(resizeterm);
668 sp->_ungetch = safe_ungetch;
671 NewScreen(sp)->_clear = TRUE;
672 CurScreen(sp)->_clear = FALSE;
675 * Get the current tty-modes. setupterm() may already have done this,
676 * unless we use the term-driver.
678 #ifndef USE_TERM_DRIVER
680 !memcmp(&cur_term->Ottyb, &null_TTY, sizeof(TTY)))
683 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
684 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
687 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
690 for (rop = safe_ripoff_stack;
691 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
694 /* If we must simulate soft labels, grab off the line to be used.
695 We assume that we must simulate, if it is none of the standard
696 formats (4-4 or 3-2-3) for which there may be some hardware
698 if (rop->hook == _nc_slk_initialize) {
699 if (!TerminalOf(sp)) {
702 if (!(NumLabels <= 0 || !SLK_STDFMT(slk_format))) {
710 count = (rop->line < 0) ? -rop->line : rop->line;
711 T(("ripping off %i lines at %s", count,
716 w = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
719 ? sp->_lines_avail - count
724 rop->hook(w, scolumns);
729 bottom_stolen += count;
731 sp->_topstolen = (NCURSES_SIZE_T) (sp->_topstolen + count);
733 sp->_lines_avail = (NCURSES_SIZE_T) (sp->_lines_avail - count);
736 /* reset the stack */
737 safe_ripoff_sp = safe_ripoff_stack;
740 T(("creating stdscr"));
741 assert((sp->_lines_avail + sp->_topstolen + bottom_stolen) == slines);
742 if ((StdScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
744 scolumns, 0, 0)) == 0) {
747 SET_LINES(sp->_lines_avail);
749 stdscr = StdScreen(sp);
751 sp->_prescreen = FALSE;
757 _nc_setupscreen(int slines GCC_UNUSED,
758 int scolumns GCC_UNUSED,
764 int rc = NCURSES_SP_NAME(_nc_setupscreen) (&sp,
777 * The internal implementation interprets line as the number of lines to rip
778 * off from the top or bottom.
781 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
783 int (*init) (WINDOW *, int))
789 T((T_CALLED("ripoffline(%p,%d,%s)"),
790 (void *) SP_PARM, line,
791 TR_FUNC_ARG(0, init)));
794 if (SP_PARM != 0 && SP_PARM->_prescreen)
800 if (safe_ripoff_sp == 0) {
801 safe_ripoff_sp = safe_ripoff_stack;
803 if (safe_ripoff_sp < safe_ripoff_stack + N_RIPS) {
804 safe_ripoff_sp->line = line;
805 safe_ripoff_sp->hook = init;
807 T(("ripped-off %d:%d chunks",
808 (int) (safe_ripoff_sp - safe_ripoff_stack), N_RIPS));
819 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
822 _nc_lock_global(prescreen);
824 rc = NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN_PRE, line, init);
825 _nc_unlock_global(prescreen);
831 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
833 int (*init) (WINDOW *, int))
836 return NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
843 ripoffline(int line, int (*init) (WINDOW *, int))
846 _nc_lock_global(prescreen);
848 rc = NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN_PRE, line, init);
849 _nc_unlock_global(prescreen);