1 /****************************************************************************
2 * Copyright 2018-2021,2022 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 #define NEW_PAIR_INTERNAL 1
46 #include <curses.priv.h>
52 /* use dynamic loader to avoid linkage dependency */
58 #define CUR SP_TERMTYPE
60 MODULE_ID("$Id: lib_set_term.c,v 1.184 2022/12/10 21:34:12 tom Exp $")
62 #ifdef USE_TERM_DRIVER
63 #define MaxColors InfoOf(sp).maxcolors
64 #define NumLabels InfoOf(sp).numlabels
66 #define MaxColors max_colors
67 #define NumLabels num_labels
70 NCURSES_EXPORT(SCREEN *)
71 set_term(SCREEN *screenp)
76 T((T_CALLED("set_term(%p)"), (void *) screenp));
78 _nc_lock_global(curses);
80 oldSP = CURRENT_SCREEN;
81 _nc_set_screen(screenp);
85 TINFO_SET_CURTERM(newSP, newSP->_term);
87 curscr = CurScreen(newSP);
88 newscr = NewScreen(newSP);
89 stdscr = StdScreen(newSP);
90 COLORS = newSP->_color_count;
91 COLOR_PAIRS = newSP->_pair_count;
94 TINFO_SET_CURTERM(oldSP, 0);
104 _nc_unlock_global(curses);
106 T((T_RETURN("%p"), (void *) oldSP));
111 _nc_free_keytry(TRIES * kt)
114 _nc_free_keytry(kt->child);
115 _nc_free_keytry(kt->sibling);
121 delink_screen(SCREEN *sp)
127 for (each_screen(temp)) {
130 last->_next_screen = sp->_next_screen;
132 _nc_screen_chain = sp->_next_screen;
142 * Free the storage associated with the given SCREEN sp.
145 delscreen(SCREEN *sp)
148 T((T_CALLED("delscreen(%p)"), (void *) sp));
150 _nc_lock_global(curses);
151 if (delink_screen(sp)) {
153 bool is_current = (sp == CURRENT_SCREEN);
156 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
158 for (rop = safe_ripoff_stack;
159 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
162 (void) delwin(rop->win);
169 /* delete all of the windows in this screen */
171 for (each_window(sp, wl)) {
172 if (_nc_freewin(&(wl->win)) == OK) {
179 if (sp->_slk->ent != 0) {
182 for (i = 0; i < sp->_slk->labcnt; ++i) {
183 FreeIfNeeded(sp->_slk->ent[i].ent_text);
184 FreeIfNeeded(sp->_slk->ent[i].form_text);
192 _nc_free_keytry(sp->_keytry);
195 _nc_free_keytry(sp->_key_ok);
198 FreeIfNeeded(sp->_current_attr);
200 _nc_free_ordered_pairs(sp);
201 FreeIfNeeded(sp->_color_table);
202 FreeIfNeeded(sp->_color_pairs);
204 FreeIfNeeded(sp->_oldnum_list);
205 FreeIfNeeded(sp->oldhash);
206 FreeIfNeeded(sp->newhash);
207 FreeIfNeeded(sp->hashtab);
209 FreeIfNeeded(sp->_acs_map);
210 FreeIfNeeded(sp->_screen_acs_map);
212 NCURSES_SP_NAME(_nc_flush) (NCURSES_SP_ARG);
213 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx sp->_term);
214 FreeIfNeeded(sp->out_buffer);
215 if (_nc_find_prescr() == sp) {
220 if (sp->_dlopen_gpm != 0) {
221 dlclose(sp->_dlopen_gpm);
225 #endif /* USE_GPM_SUPPORT */
229 * If this was the current screen, reset everything that the
230 * application might try to use (except cur_term, which may have
231 * multiple references in different screens).
242 #if USE_WIDEC_SUPPORT
244 FreeIfNeeded(_nc_wacs);
249 set_term(CURRENT_SCREEN);
252 _nc_unlock_global(curses);
258 no_mouse_event(SCREEN *sp GCC_UNUSED)
264 no_mouse_inline(SCREEN *sp GCC_UNUSED)
270 no_mouse_parse(SCREEN *sp GCC_UNUSED, int code GCC_UNUSED)
276 no_mouse_resume(SCREEN *sp GCC_UNUSED)
281 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
285 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
287 extract_fgbg(const char *src, int *result)
291 long value = strtol(src, &tmp, 0);
293 if ((dst = tmp) == 0) {
295 } else if (value >= 0) {
296 *result = (int) value;
298 while (*dst != 0 && *dst != ';')
306 #define ReturnScreenError() do { _nc_set_screen(0); \
307 returnCode(ERR); } while (0)
309 /* OS-independent screen initializations */
311 NCURSES_SP_NAME(_nc_setupscreen) (
321 #ifndef USE_TERM_DRIVER
322 static const TTY null_TTY; /* all zeros iff uninitialized */
325 int bottom_stolen = 0;
327 #ifndef USE_TERM_DRIVER
328 bool support_cookies = USE_XMC_SUPPORT;
331 T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
332 slines, scolumns, (void *) output, filtered, slk_format));
334 assert(CURRENT_SCREEN == 0); /* has been reset in newterm() ! */
341 sp = _nc_alloc_screen_sp();
342 T(("_nc_alloc_screen_sp %p", (void *) sp));
348 if ((sp->_acs_map = typeCalloc(chtype, ACS_LEN)) == NULL) {
351 if ((sp->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == NULL) {
356 T(("created SP %p", (void *) sp));
357 sp->_next_screen = _nc_screen_chain;
358 _nc_screen_chain = sp;
360 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
364 if (!_nc_alloc_screen()
365 || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
366 || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
370 T(("created SP %p", (void *) SP));
372 sp = SP; /* fixup so SET_LINES and SET_COLS works */
373 sp->_next_screen = _nc_screen_chain;
374 _nc_screen_chain = sp;
376 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
382 * We should always check the screensize, just in case.
385 sp->_term = cur_term;
386 #ifdef USE_TERM_DRIVER
388 _nc_get_screensize(sp, sp->_term, &slines, &scolumns);
390 _nc_get_screensize(sp, &slines, &scolumns);
395 T((T_CREATE("screen %s %dx%d"),
396 NCURSES_SP_NAME(termname) (NCURSES_SP_ARG), slines, scolumns));
398 sp->_filtered = filtered;
400 /* implement filter mode */
404 #ifdef USE_TERM_DRIVER
405 CallDriver(sp, td_setfilter);
408 clear_screen = ABSENT_STRING;
409 cursor_address = ABSENT_STRING;
410 cursor_down = ABSENT_STRING;
411 cursor_up = ABSENT_STRING;
412 parm_down_cursor = ABSENT_STRING;
413 parm_up_cursor = ABSENT_STRING;
414 row_address = ABSENT_STRING;
415 cursor_home = carriage_return;
417 if (back_color_erase)
418 clr_eos = ABSENT_STRING;
421 T(("filter screensize %dx%d", slines, scolumns));
424 T(("setting output mode to binary"));
426 setmode(output, O_BINARY);
428 #if defined(EXP_WIN32_DRIVER)
429 T(("setting output mode to binary"));
431 _setmode(fileno(output), _O_BINARY);
433 sp->_lines = (NCURSES_SIZE_T) slines;
434 sp->_lines_avail = (NCURSES_SIZE_T) slines;
435 sp->_columns = (NCURSES_SIZE_T) scolumns;
438 sp->_ofd = output ? fileno(output) : -1;
440 #if defined(EXP_WIN32_DRIVER)
442 _setmode(fileno(output), _O_BINARY);
444 sp->out_limit = (size_t) ((2 + slines) * (6 + scolumns));
445 if ((sp->out_buffer = malloc(sp->out_limit)) == 0)
452 #if NCURSES_EXT_FUNCS
453 sp->_default_color = FALSE;
454 sp->_has_sgr_39_49 = FALSE;
457 * Set our assumption of the terminal's default foreground and background
458 * colors. The curs_color man-page states that we can assume that the
459 * background is black. The origin of this assumption appears to be
460 * terminals that displayed colored text, but no colored backgrounds, e.g.,
461 * the first colored terminals around 1980. More recent ones with better
462 * technology can display not only colored backgrounds, but all
463 * combinations. So a terminal might be something other than "white" on
464 * black (green/black looks monochrome too), but black on white or even
467 * White-on-black is the simplest thing to use for monochrome. Almost
468 * all applications that use color paint both text and background, so
469 * the distinction is moot. But a few do not - which is why we leave this
470 * configurable (a better solution is to use assume_default_colors() for
471 * the rare applications that do require that sort of appearance, since
472 * is appears that more users expect to be able to make a white-on-black
473 * or black-on-white display under control of the application than not).
475 #ifdef USE_ASSUMED_COLOR
476 sp->_default_fg = COLOR_WHITE;
477 sp->_default_bg = COLOR_BLACK;
479 sp->_default_fg = COLOR_DEFAULT;
480 sp->_default_bg = COLOR_DEFAULT;
484 * Allow those assumed/default color assumptions to be overridden at
487 if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
490 int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
492 sp->_default_fg = ((fg >= 0 && fg < MaxColors) ? fg : COLOR_DEFAULT);
494 sp->_default_bg = ((bg >= 0 && bg < MaxColors) ? bg : COLOR_DEFAULT);
496 TR(TRACE_CHARPUT | TRACE_MOVE,
497 ("from environment assumed fg=%d, bg=%d",
504 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
505 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
506 * color to that value plus 8. We'll only use the non-bold color for now -
507 * decide later if it is worth having default attributes as well.
509 if (getenv("COLORFGBG") != 0) {
510 const char *p = getenv("COLORFGBG");
511 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
512 p = extract_fgbg(p, &(sp->_default_fg));
513 p = extract_fgbg(p, &(sp->_default_bg));
514 if (*p) /* assume rxvt was compiled with xpm support */
515 extract_fgbg(p, &(sp->_default_bg));
516 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
517 sp->_default_fg, sp->_default_bg));
518 if (sp->_default_fg >= MaxColors) {
519 if (set_a_foreground != ABSENT_STRING
520 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
521 set_a_foreground = strdup("\033[3%?%p1%{8}%>%t9%e%p1%d%;m");
523 sp->_default_fg %= MaxColors;
526 if (sp->_default_bg >= MaxColors) {
527 if (set_a_background != ABSENT_STRING
528 && !strcmp(set_a_background, "\033[4%p1%dm")) {
529 set_a_background = strdup("\033[4%?%p1%{8}%>%t9%e%p1%d%;m");
531 sp->_default_bg %= MaxColors;
536 #endif /* NCURSES_EXT_FUNCS */
538 sp->_maxclick = DEFAULT_MAXCLICK;
539 sp->_mouse_event = no_mouse_event;
540 sp->_mouse_inline = no_mouse_inline;
541 sp->_mouse_parse = no_mouse_parse;
542 sp->_mouse_resume = no_mouse_resume;
543 sp->_mouse_wrap = no_mouse_wrap;
547 * If we've no magic cookie support, we suppress attributes that xmc would
548 * affect, i.e., the attributes that affect the rendition of a space.
550 sp->_ok_attributes = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
551 if (NCURSES_SP_NAME(has_colors) (NCURSES_SP_ARG)) {
552 sp->_ok_attributes |= A_COLOR;
554 #ifdef USE_TERM_DRIVER
559 * If we have no magic-cookie support compiled-in, or if it is suppressed
560 * in the environment, reset the support-flag.
562 if (magic_cookie_glitch >= 0) {
563 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
564 support_cookies = FALSE;
569 if (!support_cookies && magic_cookie_glitch >= 0) {
570 T(("will disable attributes to work w/o magic cookies"));
573 if (magic_cookie_glitch > 0) { /* tvi, wyse */
575 sp->_xmc_triggers = sp->_ok_attributes & XMC_CONFLICT;
578 * We "should" treat colors as an attribute. The wyse350 (and its
579 * clones) appear to be the only ones that have both colors and magic
583 sp->_xmc_triggers |= A_COLOR;
586 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
588 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
590 * Supporting line-drawing may be possible. But make the regular
591 * video attributes work first.
593 acs_chars = ABSENT_STRING;
594 ena_acs = ABSENT_STRING;
595 enter_alt_charset_mode = ABSENT_STRING;
596 exit_alt_charset_mode = ABSENT_STRING;
599 * To keep the cookie support simple, suppress all of the optimization
600 * hooks except for clear_screen and the cursor addressing.
602 if (support_cookies) {
603 clr_eol = ABSENT_STRING;
604 clr_eos = ABSENT_STRING;
605 set_attributes = ABSENT_STRING;
608 } else if (magic_cookie_glitch == 0) { /* hpterm */
612 * If magic cookies are not supported, cancel the strings that set
615 if (!support_cookies && magic_cookie_glitch >= 0) {
616 magic_cookie_glitch = ABSENT_NUMERIC;
617 set_attributes = ABSENT_STRING;
618 enter_blink_mode = ABSENT_STRING;
619 enter_bold_mode = ABSENT_STRING;
620 enter_dim_mode = ABSENT_STRING;
621 enter_reverse_mode = ABSENT_STRING;
622 enter_standout_mode = ABSENT_STRING;
623 enter_underline_mode = ABSENT_STRING;
626 /* initialize normal acs before wide, since we use mapping in the latter */
627 #if !USE_WIDEC_SUPPORT
628 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
631 enter_alt_charset_mode = NULL;
632 exit_alt_charset_mode = NULL;
633 set_attributes = NULL;
638 NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_ARG);
639 #if USE_WIDEC_SUPPORT
640 sp->_screen_unicode = _nc_unicode_locale();
648 sp->_screen_acs_fix = (sp->_screen_unicode
649 && _nc_locale_breaks_acs(sp->_term));
651 env = _nc_get_locale();
652 sp->_legacy_coding = ((env == 0)
654 || !strcmp(env, "POSIX"));
655 T(("legacy-coding %d", sp->_legacy_coding));
657 sp->_nc_sp_idcok = TRUE;
658 sp->_nc_sp_idlok = FALSE;
663 T(("creating newscr"));
664 NewScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
666 if (NewScreen(sp) == 0) {
669 T(("creating curscr"));
670 CurScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
672 if (CurScreen(sp) == 0) {
676 newscr = NewScreen(sp);
677 curscr = CurScreen(sp);
680 sp->_resize = NCURSES_SP_NAME(resizeterm);
681 sp->_ungetch = safe_ungetch;
684 NewScreen(sp)->_clear = TRUE;
685 CurScreen(sp)->_clear = FALSE;
688 * Get the current tty-modes. setupterm() may already have done this,
689 * unless we use the term-driver.
691 #ifndef USE_TERM_DRIVER
693 !memcmp(&cur_term->Ottyb, &null_TTY, sizeof(TTY)))
696 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
697 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
700 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
703 for (rop = safe_ripoff_stack;
704 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
707 /* If we must simulate soft labels, grab off the line to be used.
708 We assume that we must simulate, if it is none of the standard
709 formats (4-4 or 3-2-3) for which there may be some hardware
711 if (rop->hook == _nc_slk_initialize) {
712 if (!TerminalOf(sp)) {
715 if (!(NumLabels <= 0 || !SLK_STDFMT(slk_format))) {
723 count = (rop->line < 0) ? -rop->line : rop->line;
724 T(("ripping off %i lines at %s", count,
729 w = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
732 ? sp->_lines_avail - count
737 rop->hook(w, scolumns);
742 bottom_stolen += count;
744 sp->_topstolen = (NCURSES_SIZE_T) (sp->_topstolen + count);
746 sp->_lines_avail = (NCURSES_SIZE_T) (sp->_lines_avail - count);
749 /* reset the stack */
750 safe_ripoff_sp = safe_ripoff_stack;
753 T(("creating stdscr"));
754 (void) bottom_stolen;
755 assert((sp->_lines_avail + sp->_topstolen + bottom_stolen) == slines);
756 if ((StdScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
758 scolumns, 0, 0)) == 0) {
761 SET_LINES(sp->_lines_avail);
763 stdscr = StdScreen(sp);
765 sp->_prescreen = FALSE;
771 _nc_setupscreen(int slines GCC_UNUSED,
772 int scolumns GCC_UNUSED,
778 int rc = NCURSES_SP_NAME(_nc_setupscreen) (&sp,
791 * The internal implementation interprets line as the number of lines to rip
792 * off from the top or bottom.
795 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
797 int (*init) (WINDOW *, int))
803 T((T_CALLED("ripoffline(%p,%d,%s)"),
804 (void *) SP_PARM, line,
805 TR_FUNC_ARG(0, init)));
808 if (SP_PARM != 0 && SP_PARM->_prescreen)
814 if (safe_ripoff_sp == 0) {
815 safe_ripoff_sp = safe_ripoff_stack;
817 if (safe_ripoff_sp < safe_ripoff_stack + N_RIPS) {
818 safe_ripoff_sp->line = line;
819 safe_ripoff_sp->hook = init;
821 T(("ripped-off %d:%d chunks",
822 (int) (safe_ripoff_sp - safe_ripoff_stack), N_RIPS));
833 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
838 _nc_lock_global(prescreen);
840 rc = NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN_PRE, line, init);
841 _nc_unlock_global(prescreen);
848 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
850 int (*init) (WINDOW *, int))
853 return NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
860 ripoffline(int line, int (*init) (WINDOW *, int))
865 _nc_lock_global(prescreen);
867 rc = NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN_PRE, line, init);
868 _nc_unlock_global(prescreen);