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.167 2017/09/10 21:09:16 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 clear_screen = ABSENT_STRING;
374 cursor_address = ABSENT_STRING;
375 cursor_down = ABSENT_STRING;
376 cursor_up = ABSENT_STRING;
377 parm_down_cursor = ABSENT_STRING;
378 parm_up_cursor = ABSENT_STRING;
379 row_address = ABSENT_STRING;
380 cursor_home = carriage_return;
382 if (back_color_erase)
383 clr_eos = ABSENT_STRING;
386 T(("filter screensize %dx%d", slines, scolumns));
389 T(("setting output mode to binary"));
391 setmode(output, O_BINARY);
393 NCURSES_SP_NAME(_nc_set_buffer) (NCURSES_SP_ARGx output, TRUE);
394 sp->_lines = (NCURSES_SIZE_T) slines;
395 sp->_lines_avail = (NCURSES_SIZE_T) slines;
396 sp->_columns = (NCURSES_SIZE_T) scolumns;
399 sp->_ofd = output ? fileno(output) : -1;
401 sp->out_limit = (size_t) ((2 + slines) * (6 + scolumns));
402 if ((sp->out_buffer = malloc(sp->out_limit)) == 0)
409 #if NCURSES_EXT_FUNCS
410 sp->_default_color = FALSE;
411 sp->_has_sgr_39_49 = FALSE;
414 * Set our assumption of the terminal's default foreground and background
415 * colors. The curs_color man-page states that we can assume that the
416 * background is black. The origin of this assumption appears to be
417 * terminals that displayed colored text, but no colored backgrounds, e.g.,
418 * the first colored terminals around 1980. More recent ones with better
419 * technology can display not only colored backgrounds, but all
420 * combinations. So a terminal might be something other than "white" on
421 * black (green/black looks monochrome too), but black on white or even
424 * White-on-black is the simplest thing to use for monochrome. Almost
425 * all applications that use color paint both text and background, so
426 * the distinction is moot. But a few do not - which is why we leave this
427 * configurable (a better solution is to use assume_default_colors() for
428 * the rare applications that do require that sort of appearance, since
429 * is appears that more users expect to be able to make a white-on-black
430 * or black-on-white display under control of the application than not).
432 #ifdef USE_ASSUMED_COLOR
433 sp->_default_fg = COLOR_WHITE;
434 sp->_default_bg = COLOR_BLACK;
436 sp->_default_fg = COLOR_DEFAULT;
437 sp->_default_bg = COLOR_DEFAULT;
441 * Allow those assumed/default color assumptions to be overridden at
444 if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
447 int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
449 sp->_default_fg = ((fg >= 0 && fg < MaxColors) ? fg : COLOR_DEFAULT);
451 sp->_default_bg = ((bg >= 0 && bg < MaxColors) ? bg : COLOR_DEFAULT);
453 TR(TRACE_CHARPUT | TRACE_MOVE,
454 ("from environment assumed fg=%d, bg=%d",
461 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
462 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
463 * color to that value plus 8. We'll only use the non-bold color for now -
464 * decide later if it is worth having default attributes as well.
466 if (getenv("COLORFGBG") != 0) {
467 const char *p = getenv("COLORFGBG");
468 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
469 p = extract_fgbg(p, &(sp->_default_fg));
470 p = extract_fgbg(p, &(sp->_default_bg));
471 if (*p) /* assume rxvt was compiled with xpm support */
472 p = extract_fgbg(p, &(sp->_default_bg));
473 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
474 sp->_default_fg, sp->_default_bg));
475 if (sp->_default_fg >= MaxColors) {
476 if (set_a_foreground != ABSENT_STRING
477 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
478 set_a_foreground = strdup("\033[3%?%p1%{8}%>%t9%e%p1%d%;m");
480 sp->_default_fg %= MaxColors;
483 if (sp->_default_bg >= MaxColors) {
484 if (set_a_background != ABSENT_STRING
485 && !strcmp(set_a_background, "\033[4%p1%dm")) {
486 set_a_background = strdup("\033[4%?%p1%{8}%>%t9%e%p1%d%;m");
488 sp->_default_bg %= MaxColors;
493 #endif /* NCURSES_EXT_FUNCS */
495 sp->_maxclick = DEFAULT_MAXCLICK;
496 sp->_mouse_event = no_mouse_event;
497 sp->_mouse_inline = no_mouse_inline;
498 sp->_mouse_parse = no_mouse_parse;
499 sp->_mouse_resume = no_mouse_resume;
500 sp->_mouse_wrap = no_mouse_wrap;
504 * If we've no magic cookie support, we suppress attributes that xmc would
505 * affect, i.e., the attributes that affect the rendition of a space.
507 sp->_ok_attributes = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
508 if (NCURSES_SP_NAME(has_colors) (NCURSES_SP_ARG)) {
509 sp->_ok_attributes |= A_COLOR;
511 #ifdef USE_TERM_DRIVER
516 * If we have no magic-cookie support compiled-in, or if it is suppressed
517 * in the environment, reset the support-flag.
519 if (magic_cookie_glitch >= 0) {
520 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
521 support_cookies = FALSE;
526 if (!support_cookies && magic_cookie_glitch >= 0) {
527 T(("will disable attributes to work w/o magic cookies"));
530 if (magic_cookie_glitch > 0) { /* tvi, wyse */
532 sp->_xmc_triggers = sp->_ok_attributes & XMC_CONFLICT;
535 * We "should" treat colors as an attribute. The wyse350 (and its
536 * clones) appear to be the only ones that have both colors and magic
540 sp->_xmc_triggers |= A_COLOR;
543 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
545 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
547 * Supporting line-drawing may be possible. But make the regular
548 * video attributes work first.
550 acs_chars = ABSENT_STRING;
551 ena_acs = ABSENT_STRING;
552 enter_alt_charset_mode = ABSENT_STRING;
553 exit_alt_charset_mode = ABSENT_STRING;
556 * To keep the cookie support simple, suppress all of the optimization
557 * hooks except for clear_screen and the cursor addressing.
559 if (support_cookies) {
560 clr_eol = ABSENT_STRING;
561 clr_eos = ABSENT_STRING;
562 set_attributes = ABSENT_STRING;
565 } else if (magic_cookie_glitch == 0) { /* hpterm */
569 * If magic cookies are not supported, cancel the strings that set
572 if (!support_cookies && magic_cookie_glitch >= 0) {
573 magic_cookie_glitch = ABSENT_NUMERIC;
574 set_attributes = ABSENT_STRING;
575 enter_blink_mode = ABSENT_STRING;
576 enter_bold_mode = ABSENT_STRING;
577 enter_dim_mode = ABSENT_STRING;
578 enter_reverse_mode = ABSENT_STRING;
579 enter_standout_mode = ABSENT_STRING;
580 enter_underline_mode = ABSENT_STRING;
583 /* initialize normal acs before wide, since we use mapping in the latter */
584 #if !USE_WIDEC_SUPPORT
585 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
588 enter_alt_charset_mode = NULL;
589 exit_alt_charset_mode = NULL;
590 set_attributes = NULL;
595 NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_ARG);
596 #if USE_WIDEC_SUPPORT
597 sp->_screen_unicode = _nc_unicode_locale();
605 sp->_screen_acs_fix = (sp->_screen_unicode
606 && _nc_locale_breaks_acs(sp->_term));
608 env = _nc_get_locale();
609 sp->_legacy_coding = ((env == 0)
611 || !strcmp(env, "POSIX"));
612 T(("legacy-coding %d", sp->_legacy_coding));
614 sp->_nc_sp_idcok = TRUE;
615 sp->_nc_sp_idlok = FALSE;
620 T(("creating newscr"));
621 NewScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
623 if (NewScreen(sp) == 0) {
626 T(("creating curscr"));
627 CurScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
629 if (CurScreen(sp) == 0) {
633 newscr = NewScreen(sp);
634 curscr = CurScreen(sp);
637 sp->_resize = NCURSES_SP_NAME(resizeterm);
638 sp->_ungetch = safe_ungetch;
641 NewScreen(sp)->_clear = TRUE;
642 CurScreen(sp)->_clear = FALSE;
645 * Get the current tty-modes. setupterm() may already have done this,
646 * unless we use the term-driver.
648 #ifndef USE_TERM_DRIVER
650 !memcmp(&cur_term->Ottyb, &null_TTY, sizeof(TTY)))
653 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
654 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
657 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
660 for (rop = safe_ripoff_stack;
661 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
664 /* If we must simulate soft labels, grab off the line to be used.
665 We assume that we must simulate, if it is none of the standard
666 formats (4-4 or 3-2-3) for which there may be some hardware
668 if (rop->hook == _nc_slk_initialize) {
669 if (!(NumLabels <= 0 || !SLK_STDFMT(slk_format))) {
677 count = (rop->line < 0) ? -rop->line : rop->line;
678 T(("ripping off %i lines at %s", count,
683 w = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
686 ? sp->_lines_avail - count
691 rop->hook(w, scolumns);
696 bottom_stolen += count;
698 sp->_topstolen = (NCURSES_SIZE_T) (sp->_topstolen + count);
700 sp->_lines_avail = (NCURSES_SIZE_T) (sp->_lines_avail - count);
703 /* reset the stack */
704 safe_ripoff_sp = safe_ripoff_stack;
707 T(("creating stdscr"));
708 assert((sp->_lines_avail + sp->_topstolen + bottom_stolen) == slines);
709 if ((StdScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
711 scolumns, 0, 0)) == 0) {
714 SET_LINES(sp->_lines_avail);
716 stdscr = StdScreen(sp);
718 sp->_prescreen = FALSE;
724 _nc_setupscreen(int slines GCC_UNUSED,
725 int scolumns GCC_UNUSED,
731 int rc = NCURSES_SP_NAME(_nc_setupscreen) (&sp,
744 * The internal implementation interprets line as the number of lines to rip
745 * off from the top or bottom.
748 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
750 int (*init) (WINDOW *, int))
755 T((T_CALLED("ripoffline(%p,%d,%p)"), (void *) SP_PARM, line, TR_FUNC(init)));
758 if (SP_PARM != 0 && SP_PARM->_prescreen)
764 if (safe_ripoff_sp == 0) {
765 safe_ripoff_sp = safe_ripoff_stack;
767 if (safe_ripoff_sp < safe_ripoff_stack + N_RIPS) {
768 safe_ripoff_sp->line = line;
769 safe_ripoff_sp->hook = init;
771 T(("ripped-off %d:%d chunks",
772 (int) (safe_ripoff_sp - safe_ripoff_stack), N_RIPS));
783 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
786 _nc_lock_global(prescreen);
788 rc = NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN_PRE, line, init);
789 _nc_unlock_global(prescreen);
795 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
797 int (*init) (WINDOW *, int))
800 return NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
807 ripoffline(int line, int (*init) (WINDOW *, int))
810 _nc_lock_global(prescreen);
812 rc = NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN_PRE, line, init);
813 _nc_unlock_global(prescreen);