1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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.128 2009/09/27 14:21:25 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)"), screenp));
68 _nc_lock_global(curses);
70 oldSP = CURRENT_SCREEN;
71 _nc_set_screen(screenp);
75 TINFO_SET_CURTERM(newSP, newSP->_term);
77 curscr = newSP->_curscr;
78 newscr = newSP->_newscr;
79 stdscr = newSP->_stdscr;
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"), 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 = sp->_next_screen;
122 _nc_screen_chain = sp->_next_screen;
132 * Free the storage associated with the given SCREEN sp.
135 delscreen(SCREEN *sp)
139 T((T_CALLED("delscreen(%p)"), sp));
141 _nc_lock_global(curses);
142 if (delink_screen(sp)) {
145 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
146 for (rop = safe_ripoff_stack;
147 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
150 (void) delwin(rop->win);
157 (void) _nc_freewin(sp->_curscr);
158 (void) _nc_freewin(sp->_newscr);
159 (void) _nc_freewin(sp->_stdscr);
162 if (sp->_slk->ent != 0) {
163 for (i = 0; i < sp->_slk->labcnt; ++i) {
164 FreeIfNeeded(sp->_slk->ent[i].ent_text);
165 FreeIfNeeded(sp->_slk->ent[i].form_text);
173 _nc_free_keytry(sp->_keytry);
176 _nc_free_keytry(sp->_key_ok);
179 FreeIfNeeded(sp->_current_attr);
181 FreeIfNeeded(sp->_color_table);
182 FreeIfNeeded(sp->_color_pairs);
184 FreeIfNeeded(sp->oldhash);
185 FreeIfNeeded(sp->newhash);
186 FreeIfNeeded(sp->hashtab);
188 FreeIfNeeded(sp->_acs_map);
189 FreeIfNeeded(sp->_screen_acs_map);
192 * If the associated output stream has been closed, we can discard the
193 * set-buffer. Limit the error check to EBADF, since fflush may fail
194 * for other reasons than trying to operate upon a closed stream.
198 && fflush(sp->_ofp) != 0
203 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx sp->_term);
207 * If this was the current screen, reset everything that the
208 * application might try to use (except cur_term, which may have
209 * multiple references in different screens).
211 if (sp == CURRENT_SCREEN) {
222 _nc_unlock_global(curses);
228 no_mouse_event(SCREEN *sp GCC_UNUSED)
234 no_mouse_inline(SCREEN *sp GCC_UNUSED)
240 no_mouse_parse(SCREEN *sp GCC_UNUSED, int code GCC_UNUSED)
246 no_mouse_resume(SCREEN *sp GCC_UNUSED)
251 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
255 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
257 extract_fgbg(char *src, int *result)
260 long value = strtol(src, &dst, 0);
264 } else if (value >= 0) {
267 while (*dst != 0 && *dst != ';')
277 * In case of handling multiple screens, we need to have a screen before
278 * initialization in setupscreen takes place. This is to extend the substitute
279 * for some of the stuff in _nc_prescreen, especially for slk and ripoff
280 * handling which should be done per screen.
282 NCURSES_EXPORT(SCREEN *)
285 SCREEN *sp = _nc_alloc_screen_sp();
287 sp->rsp = sp->rippedoff;
288 sp->_filtered = _nc_prescreen.filter_mode;
289 sp->_use_env = _nc_prescreen.use_env;
290 #if NCURSES_NO_PADDING
291 sp->_no_padding = _nc_prescreen._no_padding;
295 sp->_prescreen = TRUE;
298 sp->_TABSIZE = _nc_prescreen._TABSIZE;
299 sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
306 #define ReturnScreenError() _nc_set_screen(0); \
309 /* OS-independent screen initializations */
311 NCURSES_SP_NAME(_nc_setupscreen) (
322 int bottom_stolen = 0;
325 #ifndef USE_TERM_DRIVER
326 bool support_cookies = USE_XMC_SUPPORT;
329 T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
330 slines, scolumns, output, filtered, slk_format));
332 assert(CURRENT_SCREEN == 0); /* has been reset in newterm() ! */
339 sp = _nc_alloc_screen_sp();
343 || ((sp->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
344 || ((sp->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
348 T(("created SP %p", sp));
349 sp->_next_screen = _nc_screen_chain;
350 _nc_screen_chain = sp;
352 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0) {
356 if (!_nc_alloc_screen()
357 || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
358 || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
362 T(("created SP %p", SP));
364 sp = SP; /* fixup so SET_LINES and SET_COLS works */
365 sp->_next_screen = _nc_screen_chain;
366 _nc_screen_chain = sp;
368 if ((sp->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
373 * We should always check the screensize, just in case.
376 sp->_term = cur_term;
377 #ifdef USE_TERM_DRIVER
379 _nc_get_screensize(sp, sp->_term, &slines, &scolumns);
381 _nc_get_screensize(sp, &slines, &scolumns);
386 T((T_CREATE("screen %s %dx%d"),
387 NCURSES_SP_NAME(termname) (NCURSES_SP_ARG), slines, scolumns));
389 sp->_filtered = filtered;
391 /* implement filter mode */
395 #ifdef USE_TERM_DRIVER
396 CallDriver(sp, setfilter);
399 cursor_down = parm_down_cursor = 0;
401 cursor_up = parm_up_cursor = 0;
404 cursor_home = carriage_return;
406 T(("filter screensize %dx%d", slines, scolumns));
409 T(("setting output mode to binary"));
411 setmode(output, O_BINARY);
413 NCURSES_SP_NAME(_nc_set_buffer) (NCURSES_SP_ARGx output, TRUE);
415 sp->_lines_avail = slines;
416 sp->_columns = scolumns;
421 #if NCURSES_EXT_FUNCS
422 sp->_default_color = FALSE;
423 sp->_has_sgr_39_49 = FALSE;
426 * Set our assumption of the terminal's default foreground and background
427 * colors. The curs_color man-page states that we can assume that the
428 * background is black. The origin of this assumption appears to be
429 * terminals that displayed colored text, but no colored backgrounds, e.g.,
430 * the first colored terminals around 1980. More recent ones with better
431 * technology can display not only colored backgrounds, but all
432 * combinations. So a terminal might be something other than "white" on
433 * black (green/black looks monochrome too), but black on white or even
436 * White-on-black is the simplest thing to use for monochrome. Almost
437 * all applications that use color paint both text and background, so
438 * the distinction is moot. But a few do not - which is why we leave this
439 * configurable (a better solution is to use assume_default_colors() for
440 * the rare applications that do require that sort of appearance, since
441 * is appears that more users expect to be able to make a white-on-black
442 * or black-on-white display under control of the application than not).
444 #ifdef USE_ASSUMED_COLOR
445 sp->_default_fg = COLOR_WHITE;
446 sp->_default_bg = COLOR_BLACK;
448 sp->_default_fg = C_MASK;
449 sp->_default_bg = C_MASK;
453 * Allow those assumed/default color assumptions to be overridden at
456 if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
459 int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
461 sp->_default_fg = ((fg >= 0 && fg < MaxColors) ? fg : C_MASK);
463 sp->_default_bg = ((bg >= 0 && bg < MaxColors) ? bg : C_MASK);
465 TR(TRACE_CHARPUT | TRACE_MOVE,
466 ("from environment assumed fg=%d, bg=%d",
473 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
474 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
475 * color to that value plus 8. We'll only use the non-bold color for now -
476 * decide later if it is worth having default attributes as well.
478 if (getenv("COLORFGBG") != 0) {
479 char *p = getenv("COLORFGBG");
480 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
481 p = extract_fgbg(p, &(sp->_default_fg));
482 p = extract_fgbg(p, &(sp->_default_bg));
483 if (*p) /* assume rxvt was compiled with xpm support */
484 p = extract_fgbg(p, &(sp->_default_bg));
485 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
486 sp->_default_fg, sp->_default_bg));
487 if (sp->_default_fg >= MaxColors) {
488 if (set_a_foreground != ABSENT_STRING
489 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
490 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
492 sp->_default_fg %= MaxColors;
495 if (sp->_default_bg >= MaxColors) {
496 if (set_a_background != ABSENT_STRING
497 && !strcmp(set_a_background, "\033[4%p1%dm")) {
498 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
500 sp->_default_bg %= MaxColors;
505 #endif /* NCURSES_EXT_FUNCS */
507 sp->_maxclick = DEFAULT_MAXCLICK;
508 sp->_mouse_event = no_mouse_event;
509 sp->_mouse_inline = no_mouse_inline;
510 sp->_mouse_parse = no_mouse_parse;
511 sp->_mouse_resume = no_mouse_resume;
512 sp->_mouse_wrap = no_mouse_wrap;
516 * If we've no magic cookie support, we suppress attributes that xmc would
517 * affect, i.e., the attributes that affect the rendition of a space.
519 sp->_ok_attributes = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
520 if (NCURSES_SP_NAME(has_colors) (NCURSES_SP_ARG)) {
521 sp->_ok_attributes |= A_COLOR;
523 #ifdef USE_TERM_DRIVER
528 * If we have no magic-cookie support compiled-in, or if it is suppressed
529 * in the environment, reset the support-flag.
531 if (magic_cookie_glitch >= 0) {
532 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
533 support_cookies = FALSE;
538 if (!support_cookies && magic_cookie_glitch >= 0) {
539 T(("will disable attributes to work w/o magic cookies"));
542 if (magic_cookie_glitch > 0) { /* tvi, wyse */
544 sp->_xmc_triggers = sp->_ok_attributes & (
556 * We "should" treat colors as an attribute. The wyse350 (and its
557 * clones) appear to be the only ones that have both colors and magic
561 sp->_xmc_triggers |= A_COLOR;
564 sp->_xmc_suppress = sp->_xmc_triggers & (chtype) ~(A_BOLD);
566 T(("magic cookie attributes %s", _traceattr(sp->_xmc_suppress)));
568 * Supporting line-drawing may be possible. But make the regular
569 * video attributes work first.
571 acs_chars = ABSENT_STRING;
572 ena_acs = ABSENT_STRING;
573 enter_alt_charset_mode = ABSENT_STRING;
574 exit_alt_charset_mode = ABSENT_STRING;
577 * To keep the cookie support simple, suppress all of the optimization
578 * hooks except for clear_screen and the cursor addressing.
580 if (support_cookies) {
581 clr_eol = ABSENT_STRING;
582 clr_eos = ABSENT_STRING;
583 set_attributes = ABSENT_STRING;
586 } else if (magic_cookie_glitch == 0) { /* hpterm */
590 * If magic cookies are not supported, cancel the strings that set
593 if (!support_cookies && magic_cookie_glitch >= 0) {
594 magic_cookie_glitch = ABSENT_NUMERIC;
595 set_attributes = ABSENT_STRING;
596 enter_blink_mode = ABSENT_STRING;
597 enter_bold_mode = ABSENT_STRING;
598 enter_dim_mode = ABSENT_STRING;
599 enter_reverse_mode = ABSENT_STRING;
600 enter_standout_mode = ABSENT_STRING;
601 enter_underline_mode = ABSENT_STRING;
604 /* initialize normal acs before wide, since we use mapping in the latter */
605 #if !USE_WIDEC_SUPPORT
606 if (_nc_unicode_locale() && _nc_locale_breaks_acs(sp->_term)) {
609 enter_alt_charset_mode = NULL;
610 exit_alt_charset_mode = NULL;
611 set_attributes = NULL;
616 NCURSES_SP_NAME(_nc_init_acs) (NCURSES_SP_ARG);
617 #if USE_WIDEC_SUPPORT
620 sp->_screen_acs_fix = (_nc_unicode_locale()
621 && _nc_locale_breaks_acs(sp->_term));
623 env = _nc_get_locale();
624 sp->_legacy_coding = ((env == 0)
626 || !strcmp(env, "POSIX"));
627 T(("legacy-coding %d", sp->_legacy_coding));
629 sp->_nc_sp_idcok = TRUE;
630 sp->_nc_sp_idlok = FALSE;
635 T(("creating newscr"));
636 sp->_newscr = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
638 if (sp->_newscr == 0) {
641 T(("creating curscr"));
642 sp->_curscr = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx slines, scolumns,
644 if (sp->_curscr == 0) {
648 newscr = sp->_newscr;
649 curscr = sp->_curscr;
652 sp->_resize = NCURSES_SP_NAME(resizeterm);
655 sp->_newscr->_clear = TRUE;
656 sp->_curscr->_clear = FALSE;
658 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
659 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
661 if (safe_ripoff_sp && safe_ripoff_sp != safe_ripoff_stack) {
662 for (rop = safe_ripoff_stack;
663 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
666 /* If we must simulate soft labels, grab off the line to be used.
667 We assume that we must simulate, if it is none of the standard
668 formats (4-4 or 3-2-3) for which there may be some hardware
670 if (rop->hook == _nc_slk_initialize)
671 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 += count;
699 sp->_lines_avail -= count;
702 /* reset the stack */
703 safe_ripoff_sp = safe_ripoff_stack;
706 T(("creating stdscr"));
707 assert((sp->_lines_avail + sp->_topstolen + bottom_stolen) == slines);
708 if ((sp->_stdscr = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
710 scolumns, 0, 0)) == 0) {
713 SET_LINES(sp->_lines_avail);
715 stdscr = sp->_stdscr;
717 sp->_prescreen = FALSE;
723 _nc_setupscreen(int slines GCC_UNUSED,
724 int scolumns GCC_UNUSED,
730 int rc = NCURSES_SP_NAME(_nc_setupscreen) (&sp,
743 * The internal implementation interprets line as the number of lines to rip
744 * off from the top or bottom.
747 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
749 int (*init) (WINDOW *, int))
754 T((T_CALLED("ripoffline(%p,%d,%p)"), SP_PARM, line, init));
756 if (SP_PARM != 0 && SP_PARM->_prescreen) {
760 if (safe_ripoff_sp == 0)
761 safe_ripoff_sp = safe_ripoff_stack;
762 if (safe_ripoff_sp < safe_ripoff_stack + N_RIPS) {
763 safe_ripoff_sp->line = line;
764 safe_ripoff_sp->hook = init;
776 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
778 return NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN_PRE, line, init);
783 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
785 int (*init) (WINDOW *, int))
787 return NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
794 ripoffline(int line, int (*init) (WINDOW *, int))
796 return NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN_PRE, line, init);