1 /****************************************************************************
2 * Copyright (c) 1998-2015,2016 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
50 MODULE_ID("$Id: lib_set_term.c,v 1.155 2016/05/28 21:33:38 tom Exp $")
52 #ifdef USE_TERM_DRIVER
53 #define MaxColors InfoOf(sp).maxcolors
54 #define NumLabels InfoOf(sp).numlabels
56 #define MaxColors max_colors
57 #define NumLabels num_labels
60 NCURSES_EXPORT(SCREEN *)
61 set_term(SCREEN *screenp)
66 T((T_CALLED("set_term(%p)"), (void *) screenp));
68 _nc_lock_global(curses);
70 oldSP = CURRENT_SCREEN;
71 _nc_set_screen(screenp);
75 TINFO_SET_CURTERM(newSP, newSP->_term);
77 curscr = CurScreen(newSP);
78 newscr = NewScreen(newSP);
79 stdscr = StdScreen(newSP);
80 COLORS = newSP->_color_count;
81 COLOR_PAIRS = newSP->_pair_count;
84 TINFO_SET_CURTERM(oldSP, 0);
94 _nc_unlock_global(curses);
96 T((T_RETURN("%p"), (void *) oldSP));
101 _nc_free_keytry(TRIES * kt)
104 _nc_free_keytry(kt->child);
105 _nc_free_keytry(kt->sibling);
111 delink_screen(SCREEN *sp)
117 for (each_screen(temp)) {
120 last->_next_screen = sp->_next_screen;
122 _nc_screen_chain = sp->_next_screen;
132 * Free the storage associated with the given SCREEN sp.
135 delscreen(SCREEN *sp)
138 T((T_CALLED("delscreen(%p)"), (void *) sp));
140 _nc_lock_global(curses);
141 if (delink_screen(sp)) {
144 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
145 for (rop = safe_ripoff_stack;
146 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
149 (void) delwin(rop->win);
156 (void) _nc_freewin(CurScreen(sp));
157 (void) _nc_freewin(NewScreen(sp));
158 (void) _nc_freewin(StdScreen(sp));
162 if (sp->_slk->ent != 0) {
165 for (i = 0; i < sp->_slk->labcnt; ++i) {
166 FreeIfNeeded(sp->_slk->ent[i].ent_text);
167 FreeIfNeeded(sp->_slk->ent[i].form_text);
175 _nc_free_keytry(sp->_keytry);
178 _nc_free_keytry(sp->_key_ok);
181 FreeIfNeeded(sp->_current_attr);
183 FreeIfNeeded(sp->_color_table);
184 FreeIfNeeded(sp->_color_pairs);
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);
199 * If this was the current screen, reset everything that the
200 * application might try to use (except cur_term, which may have
201 * multiple references in different screens).
203 if (sp == CURRENT_SCREEN) {
212 #if USE_WIDEC_SUPPORT
214 FreeIfNeeded(_nc_wacs);
220 _nc_unlock_global(curses);
226 no_mouse_event(SCREEN *sp GCC_UNUSED)
232 no_mouse_inline(SCREEN *sp GCC_UNUSED)
238 no_mouse_parse(SCREEN *sp GCC_UNUSED, int code GCC_UNUSED)
244 no_mouse_resume(SCREEN *sp GCC_UNUSED)
249 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
253 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
255 extract_fgbg(const char *src, int *result)
259 long value = strtol(src, &tmp, 0);
261 if ((dst = tmp) == 0) {
263 } else if (value >= 0) {
266 while (*dst != 0 && *dst != ';')
274 #define ReturnScreenError() do { _nc_set_screen(0); \
275 returnCode(ERR); } while (0)
277 /* OS-independent screen initializations */
279 NCURSES_SP_NAME(_nc_setupscreen) (
290 int bottom_stolen = 0;
292 #ifndef USE_TERM_DRIVER
293 bool support_cookies = USE_XMC_SUPPORT;
296 T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
297 slines, scolumns, (void *) output, filtered, slk_format));
299 assert(CURRENT_SCREEN == 0); /* has been reset in newterm() ! */
306 sp = _nc_alloc_screen_sp();
310 || ((sp->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
311 || ((sp->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
315 T(("created SP %p", (void *) sp));
316 sp->_next_screen = _nc_screen_chain;
317 _nc_screen_chain = sp;
319 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
323 if (!_nc_alloc_screen()
324 || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
325 || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
329 T(("created SP %p", (void *) SP));
331 sp = SP; /* fixup so SET_LINES and SET_COLS works */
332 sp->_next_screen = _nc_screen_chain;
333 _nc_screen_chain = sp;
335 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
340 * We should always check the screensize, just in case.
343 sp->_term = cur_term;
344 #ifdef USE_TERM_DRIVER
346 _nc_get_screensize(sp, sp->_term, &slines, &scolumns);
348 _nc_get_screensize(sp, &slines, &scolumns);
353 T((T_CREATE("screen %s %dx%d"),
354 NCURSES_SP_NAME(termname) (NCURSES_SP_ARG), slines, scolumns));
356 sp->_filtered = filtered;
358 /* implement filter mode */
362 #ifdef USE_TERM_DRIVER
363 CallDriver(sp, td_setfilter);
366 cursor_down = parm_down_cursor = 0;
368 cursor_up = parm_up_cursor = 0;
371 cursor_home = carriage_return;
373 T(("filter screensize %dx%d", slines, scolumns));
376 T(("setting output mode to binary"));
378 setmode(output, O_BINARY);
380 NCURSES_SP_NAME(_nc_set_buffer) (NCURSES_SP_ARGx output, TRUE);
381 sp->_lines = (NCURSES_SIZE_T) slines;
382 sp->_lines_avail = (NCURSES_SIZE_T) slines;
383 sp->_columns = (NCURSES_SIZE_T) scolumns;
386 sp->_ofd = output ? fileno(output) : -1;
388 sp->out_limit = (size_t) ((2 + slines) * (6 + scolumns));
389 if ((sp->out_buffer = malloc(sp->out_limit)) == 0)
396 #if NCURSES_EXT_FUNCS
397 sp->_default_color = FALSE;
398 sp->_has_sgr_39_49 = FALSE;
401 * Set our assumption of the terminal's default foreground and background
402 * colors. The curs_color man-page states that we can assume that the
403 * background is black. The origin of this assumption appears to be
404 * terminals that displayed colored text, but no colored backgrounds, e.g.,
405 * the first colored terminals around 1980. More recent ones with better
406 * technology can display not only colored backgrounds, but all
407 * combinations. So a terminal might be something other than "white" on
408 * black (green/black looks monochrome too), but black on white or even
411 * White-on-black is the simplest thing to use for monochrome. Almost
412 * all applications that use color paint both text and background, so
413 * the distinction is moot. But a few do not - which is why we leave this
414 * configurable (a better solution is to use assume_default_colors() for
415 * the rare applications that do require that sort of appearance, since
416 * is appears that more users expect to be able to make a white-on-black
417 * or black-on-white display under control of the application than not).
419 #ifdef USE_ASSUMED_COLOR
420 sp->_default_fg = COLOR_WHITE;
421 sp->_default_bg = COLOR_BLACK;
423 sp->_default_fg = C_MASK;
424 sp->_default_bg = C_MASK;
428 * Allow those assumed/default color assumptions to be overridden at
431 if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
434 int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
436 sp->_default_fg = ((fg >= 0 && fg < MaxColors) ? fg : C_MASK);
438 sp->_default_bg = ((bg >= 0 && bg < MaxColors) ? bg : C_MASK);
440 TR(TRACE_CHARPUT | TRACE_MOVE,
441 ("from environment assumed fg=%d, bg=%d",
448 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
449 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
450 * color to that value plus 8. We'll only use the non-bold color for now -
451 * decide later if it is worth having default attributes as well.
453 if (getenv("COLORFGBG") != 0) {
454 const char *p = getenv("COLORFGBG");
455 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
456 p = extract_fgbg(p, &(sp->_default_fg));
457 p = extract_fgbg(p, &(sp->_default_bg));
458 if (*p) /* assume rxvt was compiled with xpm support */
459 p = extract_fgbg(p, &(sp->_default_bg));
460 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
461 sp->_default_fg, sp->_default_bg));
462 if (sp->_default_fg >= MaxColors) {
463 if (set_a_foreground != ABSENT_STRING
464 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
465 set_a_foreground = strdup("\033[3%?%p1%{8}%>%t9%e%p1%d%;m");
467 sp->_default_fg %= MaxColors;
470 if (sp->_default_bg >= MaxColors) {
471 if (set_a_background != ABSENT_STRING
472 && !strcmp(set_a_background, "\033[4%p1%dm")) {
473 set_a_background = strdup("\033[4%?%p1%{8}%>%t9%e%p1%d%;m");
475 sp->_default_bg %= MaxColors;
480 #endif /* NCURSES_EXT_FUNCS */
482 sp->_maxclick = DEFAULT_MAXCLICK;
483 sp->_mouse_event = no_mouse_event;
484 sp->_mouse_inline = no_mouse_inline;
485 sp->_mouse_parse = no_mouse_parse;
486 sp->_mouse_resume = no_mouse_resume;
487 sp->_mouse_wrap = no_mouse_wrap;
491 * If we've no magic cookie support, we suppress attributes that xmc would
492 * affect, i.e., the attributes that affect the rendition of a space.
494 sp->_ok_attributes = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
495 if (NCURSES_SP_NAME(has_colors) (NCURSES_SP_ARG)) {
496 sp->_ok_attributes |= A_COLOR;
498 #ifdef USE_TERM_DRIVER
503 * If we have no magic-cookie support compiled-in, or if it is suppressed
504 * in the environment, reset the support-flag.
506 if (magic_cookie_glitch >= 0) {
507 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
508 support_cookies = FALSE;
513 if (!support_cookies && magic_cookie_glitch >= 0) {
514 T(("will disable attributes to work w/o magic cookies"));
517 if (magic_cookie_glitch > 0) { /* tvi, wyse */
519 sp->_xmc_triggers = sp->_ok_attributes & XMC_CONFLICT;
522 * We "should" treat colors as an attribute. The wyse350 (and its
523 * clones) appear to be the only ones that have both colors and magic
527 sp->_xmc_triggers |= A_COLOR;
530 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
532 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
534 * Supporting line-drawing may be possible. But make the regular
535 * video attributes work first.
537 acs_chars = ABSENT_STRING;
538 ena_acs = ABSENT_STRING;
539 enter_alt_charset_mode = ABSENT_STRING;
540 exit_alt_charset_mode = ABSENT_STRING;
543 * To keep the cookie support simple, suppress all of the optimization
544 * hooks except for clear_screen and the cursor addressing.
546 if (support_cookies) {
547 clr_eol = ABSENT_STRING;
548 clr_eos = ABSENT_STRING;
549 set_attributes = ABSENT_STRING;
552 } else if (magic_cookie_glitch == 0) { /* hpterm */
556 * If magic cookies are not supported, cancel the strings that set
559 if (!support_cookies && magic_cookie_glitch >= 0) {
560 magic_cookie_glitch = ABSENT_NUMERIC;
561 set_attributes = ABSENT_STRING;
562 enter_blink_mode = ABSENT_STRING;
563 enter_bold_mode = ABSENT_STRING;
564 enter_dim_mode = ABSENT_STRING;
565 enter_reverse_mode = ABSENT_STRING;
566 enter_standout_mode = ABSENT_STRING;
567 enter_underline_mode = ABSENT_STRING;
570 /* initialize normal acs before wide, since we use mapping in the latter */
571 #if !USE_WIDEC_SUPPORT
572 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
575 enter_alt_charset_mode = NULL;
576 exit_alt_charset_mode = NULL;
577 set_attributes = NULL;
582 NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_ARG);
583 #if USE_WIDEC_SUPPORT
584 sp->_screen_unicode = _nc_unicode_locale();
592 sp->_screen_acs_fix = (sp->_screen_unicode
593 && _nc_locale_breaks_acs(sp->_term));
595 env = _nc_get_locale();
596 sp->_legacy_coding = ((env == 0)
598 || !strcmp(env, "POSIX"));
599 T(("legacy-coding %d", sp->_legacy_coding));
601 sp->_nc_sp_idcok = TRUE;
602 sp->_nc_sp_idlok = FALSE;
607 T(("creating newscr"));
608 NewScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
610 if (NewScreen(sp) == 0) {
613 T(("creating curscr"));
614 CurScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
616 if (CurScreen(sp) == 0) {
620 newscr = NewScreen(sp);
621 curscr = CurScreen(sp);
624 sp->_resize = NCURSES_SP_NAME(resizeterm);
625 sp->_ungetch = safe_ungetch;
628 NewScreen(sp)->_clear = TRUE;
629 CurScreen(sp)->_clear = FALSE;
631 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
632 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
634 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
637 for (rop = safe_ripoff_stack;
638 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
641 /* If we must simulate soft labels, grab off the line to be used.
642 We assume that we must simulate, if it is none of the standard
643 formats (4-4 or 3-2-3) for which there may be some hardware
645 if (rop->hook == _nc_slk_initialize) {
646 if (!(NumLabels <= 0 || !SLK_STDFMT(slk_format))) {
654 count = (rop->line < 0) ? -rop->line : rop->line;
655 T(("ripping off %i lines at %s", count,
660 w = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
663 ? sp->_lines_avail - count
668 rop->hook(w, scolumns);
673 bottom_stolen += count;
675 sp->_topstolen = (NCURSES_SIZE_T) (sp->_topstolen + count);
677 sp->_lines_avail = (NCURSES_SIZE_T) (sp->_lines_avail - count);
680 /* reset the stack */
681 safe_ripoff_sp = safe_ripoff_stack;
684 T(("creating stdscr"));
685 assert((sp->_lines_avail + sp->_topstolen + bottom_stolen) == slines);
686 if ((StdScreen(sp) = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
688 scolumns, 0, 0)) == 0) {
691 SET_LINES(sp->_lines_avail);
693 stdscr = StdScreen(sp);
695 sp->_prescreen = FALSE;
701 _nc_setupscreen(int slines GCC_UNUSED,
702 int scolumns GCC_UNUSED,
708 int rc = NCURSES_SP_NAME(_nc_setupscreen) (&sp,
721 * The internal implementation interprets line as the number of lines to rip
722 * off from the top or bottom.
725 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
727 int (*init) (WINDOW *, int))
732 T((T_CALLED("ripoffline(%p,%d,%p)"), (void *) SP_PARM, line, TR_FUNC(init)));
735 if (SP_PARM != 0 && SP_PARM->_prescreen)
741 if (safe_ripoff_sp == 0)
742 safe_ripoff_sp = safe_ripoff_stack;
743 if (safe_ripoff_sp < safe_ripoff_stack + N_RIPS) {
744 safe_ripoff_sp->line = line;
745 safe_ripoff_sp->hook = init;
757 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
759 return NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN_PRE, line, init);
764 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
766 int (*init) (WINDOW *, int))
769 return NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
776 ripoffline(int line, int (*init) (WINDOW *, int))
778 return NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN_PRE, line, init);