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>
45 #include <term.h> /* cur_term */
49 #define CUR SP_TERMTYPE
52 MODULE_ID("$Id: lib_set_term.c,v 1.123 2009/06/06 20:26:17 tom Exp $")
54 NCURSES_EXPORT(SCREEN *)
55 set_term(SCREEN *screenp)
60 T((T_CALLED("set_term(%p)"), screenp));
62 _nc_lock_global(curses);
65 _nc_set_screen(screenp);
69 set_curterm(newSP->_term);
71 curscr = newSP->_curscr;
72 newscr = newSP->_newscr;
73 stdscr = newSP->_stdscr;
74 COLORS = newSP->_color_count;
75 COLOR_PAIRS = newSP->_pair_count;
88 _nc_unlock_global(curses);
90 T((T_RETURN("%p"), oldSP));
95 _nc_free_keytry(TRIES * kt)
98 _nc_free_keytry(kt->child);
99 _nc_free_keytry(kt->sibling);
105 delink_screen(SCREEN *sp)
111 for (each_screen(temp)) {
114 last = sp->_next_screen;
116 _nc_screen_chain = sp->_next_screen;
126 * Free the storage associated with the given SCREEN sp.
129 delscreen(SCREEN *sp)
133 T((T_CALLED("delscreen(%p)"), sp));
135 _nc_lock_global(curses);
136 if (delink_screen(sp)) {
138 (void) _nc_freewin(sp->_curscr);
139 (void) _nc_freewin(sp->_newscr);
140 (void) _nc_freewin(sp->_stdscr);
143 if (sp->_slk->ent != 0) {
144 for (i = 0; i < sp->_slk->labcnt; ++i) {
145 FreeIfNeeded(sp->_slk->ent[i].ent_text);
146 FreeIfNeeded(sp->_slk->ent[i].form_text);
154 _nc_free_keytry(sp->_keytry);
157 _nc_free_keytry(sp->_key_ok);
160 FreeIfNeeded(sp->_current_attr);
162 FreeIfNeeded(sp->_color_table);
163 FreeIfNeeded(sp->_color_pairs);
165 FreeIfNeeded(sp->oldhash);
166 FreeIfNeeded(sp->newhash);
167 FreeIfNeeded(sp->hashtab);
169 FreeIfNeeded(sp->_acs_map);
170 FreeIfNeeded(sp->_screen_acs_map);
173 * If the associated output stream has been closed, we can discard the
174 * set-buffer. Limit the error check to EBADF, since fflush may fail
175 * for other reasons than trying to operate upon a closed stream.
179 && fflush(sp->_ofp) != 0
184 del_curterm(sp->_term);
188 * If this was the current screen, reset everything that the
189 * application might try to use (except cur_term, which may have
190 * multiple references in different screens).
203 _nc_unlock_global(curses);
209 no_mouse_event(SCREEN *sp GCC_UNUSED)
215 no_mouse_inline(SCREEN *sp GCC_UNUSED)
221 no_mouse_parse(SCREEN *sp GCC_UNUSED, int code GCC_UNUSED)
227 no_mouse_resume(SCREEN *sp GCC_UNUSED)
232 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
236 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
238 extract_fgbg(char *src, int *result)
241 long value = strtol(src, &dst, 0);
245 } else if (value >= 0) {
248 while (*dst != 0 && *dst != ';')
256 /* OS-independent screen initializations */
258 _nc_setupscreen(int slines GCC_UNUSED,
259 int scolumns GCC_UNUSED,
265 SCREEN *sp = CURRENT_SCREEN;
268 int bottom_stolen = 0;
269 bool support_cookies = USE_XMC_SUPPORT;
272 T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
273 slines, scolumns, output, filtered, slk_format));
275 assert(SP == 0); /* has been reset in newterm() ! */
276 if (!_nc_alloc_screen()
277 || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
278 || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
282 T(("created SP %p", SP));
283 SP->_next_screen = _nc_screen_chain;
284 _nc_screen_chain = SP;
286 if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
290 * We should always check the screensize, just in case.
292 _nc_get_screensize(SP, &slines, &scolumns);
295 T((T_CREATE("screen %s %dx%d"), termname(), LINES, COLS));
297 SP->_filtered = filtered;
299 /* implement filter mode */
304 cursor_down = parm_down_cursor = 0;
306 cursor_up = parm_up_cursor = 0;
309 cursor_home = carriage_return;
310 T(("filter screensize %dx%d", LINES, COLS));
313 T(("setting output mode to binary"));
315 setmode(output, O_BINARY);
317 _nc_set_buffer(output, TRUE);
318 SP->_term = cur_term;
320 SP->_lines_avail = slines;
321 SP->_columns = scolumns;
331 SP->_cursor = -1; /* cannot know real cursor shape */
335 #if NCURSES_EXT_FUNCS
336 SP->_default_color = FALSE;
337 SP->_has_sgr_39_49 = FALSE;
340 * Set our assumption of the terminal's default foreground and background
341 * colors. The curs_color man-page states that we can assume that the
342 * background is black. The origin of this assumption appears to be
343 * terminals that displayed colored text, but no colored backgrounds, e.g.,
344 * the first colored terminals around 1980. More recent ones with better
345 * technology can display not only colored backgrounds, but all
346 * combinations. So a terminal might be something other than "white" on
347 * black (green/black looks monochrome too), but black on white or even
350 * White-on-black is the simplest thing to use for monochrome. Almost
351 * all applications that use color paint both text and background, so
352 * the distinction is moot. But a few do not - which is why we leave this
353 * configurable (a better solution is to use assume_default_colors() for
354 * the rare applications that do require that sort of appearance, since
355 * is appears that more users expect to be able to make a white-on-black
356 * or black-on-white display under control of the application than not).
358 #ifdef USE_ASSUMED_COLOR
359 SP->_default_fg = COLOR_WHITE;
360 SP->_default_bg = COLOR_BLACK;
362 SP->_default_fg = C_MASK;
363 SP->_default_bg = C_MASK;
367 * Allow those assumed/default color assumptions to be overridden at
370 if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
373 int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
375 SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
377 SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
379 TR(TRACE_CHARPUT | TRACE_MOVE,
380 ("from environment assumed fg=%d, bg=%d",
387 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
388 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
389 * color to that value plus 8. We'll only use the non-bold color for now -
390 * decide later if it is worth having default attributes as well.
392 if (getenv("COLORFGBG") != 0) {
393 char *p = getenv("COLORFGBG");
394 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
395 p = extract_fgbg(p, &(SP->_default_fg));
396 p = extract_fgbg(p, &(SP->_default_bg));
397 if (*p) /* assume rxvt was compiled with xpm support */
398 p = extract_fgbg(p, &(SP->_default_bg));
399 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
400 SP->_default_fg, SP->_default_bg));
401 if (SP->_default_fg >= max_colors) {
402 if (set_a_foreground != ABSENT_STRING
403 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
404 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
406 SP->_default_fg %= max_colors;
409 if (SP->_default_bg >= max_colors) {
410 if (set_a_background != ABSENT_STRING
411 && !strcmp(set_a_background, "\033[4%p1%dm")) {
412 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
414 SP->_default_bg %= max_colors;
419 #endif /* NCURSES_EXT_FUNCS */
421 SP->_maxclick = DEFAULT_MAXCLICK;
422 SP->_mouse_event = no_mouse_event;
423 SP->_mouse_inline = no_mouse_inline;
424 SP->_mouse_parse = no_mouse_parse;
425 SP->_mouse_resume = no_mouse_resume;
426 SP->_mouse_wrap = no_mouse_wrap;
430 * If we've no magic cookie support, we suppress attributes that xmc would
431 * affect, i.e., the attributes that affect the rendition of a space.
433 SP->_ok_attributes = termattrs();
435 SP->_ok_attributes |= A_COLOR;
439 * If we have no magic-cookie support compiled-in, or if it is suppressed
440 * in the environment, reset the support-flag.
442 if (magic_cookie_glitch >= 0) {
443 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
444 support_cookies = FALSE;
449 if (!support_cookies && magic_cookie_glitch >= 0) {
450 T(("will disable attributes to work w/o magic cookies"));
453 if (magic_cookie_glitch > 0) { /* tvi, wyse */
455 SP->_xmc_triggers = SP->_ok_attributes & (
467 * We "should" treat colors as an attribute. The wyse350 (and its
468 * clones) appear to be the only ones that have both colors and magic
472 SP->_xmc_triggers |= A_COLOR;
475 SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
477 T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
479 * Supporting line-drawing may be possible. But make the regular
480 * video attributes work first.
482 acs_chars = ABSENT_STRING;
483 ena_acs = ABSENT_STRING;
484 enter_alt_charset_mode = ABSENT_STRING;
485 exit_alt_charset_mode = ABSENT_STRING;
488 * To keep the cookie support simple, suppress all of the optimization
489 * hooks except for clear_screen and the cursor addressing.
491 if (support_cookies) {
492 clr_eol = ABSENT_STRING;
493 clr_eos = ABSENT_STRING;
494 set_attributes = ABSENT_STRING;
497 } else if (magic_cookie_glitch == 0) { /* hpterm */
501 * If magic cookies are not supported, cancel the strings that set
504 if (!support_cookies && magic_cookie_glitch >= 0) {
505 magic_cookie_glitch = ABSENT_NUMERIC;
506 set_attributes = ABSENT_STRING;
507 enter_blink_mode = ABSENT_STRING;
508 enter_bold_mode = ABSENT_STRING;
509 enter_dim_mode = ABSENT_STRING;
510 enter_reverse_mode = ABSENT_STRING;
511 enter_standout_mode = ABSENT_STRING;
512 enter_underline_mode = ABSENT_STRING;
515 /* initialize normal acs before wide, since we use mapping in the latter */
516 #if !USE_WIDEC_SUPPORT
517 if (_nc_unicode_locale() && _nc_locale_breaks_acs(cur_term)) {
520 enter_alt_charset_mode = NULL;
521 exit_alt_charset_mode = NULL;
522 set_attributes = NULL;
526 #if USE_WIDEC_SUPPORT
529 SP->_screen_acs_fix = (_nc_unicode_locale()
530 && _nc_locale_breaks_acs(cur_term));
532 env = _nc_get_locale();
533 SP->_legacy_coding = ((env == 0)
535 || !strcmp(env, "POSIX"));
536 T(("legacy-coding %d", SP->_legacy_coding));
544 T(("creating newscr"));
545 if ((SP->_newscr = newwin(slines, scolumns, 0, 0)) == 0)
548 T(("creating curscr"));
549 if ((SP->_curscr = newwin(slines, scolumns, 0, 0)) == 0)
553 newscr = SP->_newscr;
554 curscr = SP->_curscr;
557 SP->_resize = NCURSES_SP_NAME(resizeterm);
560 newscr->_clear = TRUE;
561 curscr->_clear = FALSE;
566 for (rop = safe_ripoff_stack;
567 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
570 /* If we must simulate soft labels, grab off the line to be used.
571 We assume that we must simulate, if it is none of the standard
572 formats (4-4 or 3-2-3) for which there may be some hardware
574 if (rop->hook == _nc_slk_initialize)
575 if (!(num_labels <= 0 || !SLK_STDFMT(slk_format)))
581 count = (rop->line < 0) ? -rop->line : rop->line;
582 T(("ripping off %i lines at %s", count,
587 w = newwin(count, scolumns,
589 ? SP->_lines_avail - count
594 rop->hook(w, scolumns);
599 bottom_stolen += count;
601 SP->_topstolen += count;
602 SP->_lines_avail -= count;
605 /* reset the stack */
606 safe_ripoff_sp = safe_ripoff_stack;
608 T(("creating stdscr"));
609 assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
610 if ((SP->_stdscr = newwin(SP->_lines_avail, scolumns, 0, 0)) == 0)
613 SET_LINES(SP->_lines_avail);
615 stdscr = SP->_stdscr;
622 * The internal implementation interprets line as the number of lines to rip
623 * off from the top or bottom.
626 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
628 int (*init) (WINDOW *, int))
630 T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
634 if (safe_ripoff_sp == 0)
635 safe_ripoff_sp = safe_ripoff_stack;
636 if (safe_ripoff_sp >= safe_ripoff_stack + N_RIPS)
639 safe_ripoff_sp->line = line;
640 safe_ripoff_sp->hook = init;
649 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
651 return NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN, line, init);
656 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
658 int (*init) (WINDOW *, int))
661 T((T_CALLED("ripoffline(%d,%p)"), line, init));
666 returnCode(NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
673 ripoffline(int line, int (*init) (WINDOW *, int))
675 return NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN, line, init);