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.124 2009/06/27 21:36:14 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));
284 sp = SP; /* fixup so SET_LINES and SET_COLS works */
286 SP->_next_screen = _nc_screen_chain;
287 _nc_screen_chain = SP;
289 if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
293 * We should always check the screensize, just in case.
295 _nc_get_screensize(SP, &slines, &scolumns);
298 T((T_CREATE("screen %s %dx%d"), termname(), LINES, COLS));
300 SP->_filtered = filtered;
302 /* implement filter mode */
307 cursor_down = parm_down_cursor = 0;
309 cursor_up = parm_up_cursor = 0;
312 cursor_home = carriage_return;
313 T(("filter screensize %dx%d", LINES, COLS));
316 T(("setting output mode to binary"));
318 setmode(output, O_BINARY);
320 _nc_set_buffer(output, TRUE);
321 SP->_term = cur_term;
323 SP->_lines_avail = slines;
324 SP->_columns = scolumns;
334 SP->_cursor = -1; /* cannot know real cursor shape */
338 #if NCURSES_EXT_FUNCS
339 SP->_default_color = FALSE;
340 SP->_has_sgr_39_49 = FALSE;
343 * Set our assumption of the terminal's default foreground and background
344 * colors. The curs_color man-page states that we can assume that the
345 * background is black. The origin of this assumption appears to be
346 * terminals that displayed colored text, but no colored backgrounds, e.g.,
347 * the first colored terminals around 1980. More recent ones with better
348 * technology can display not only colored backgrounds, but all
349 * combinations. So a terminal might be something other than "white" on
350 * black (green/black looks monochrome too), but black on white or even
353 * White-on-black is the simplest thing to use for monochrome. Almost
354 * all applications that use color paint both text and background, so
355 * the distinction is moot. But a few do not - which is why we leave this
356 * configurable (a better solution is to use assume_default_colors() for
357 * the rare applications that do require that sort of appearance, since
358 * is appears that more users expect to be able to make a white-on-black
359 * or black-on-white display under control of the application than not).
361 #ifdef USE_ASSUMED_COLOR
362 SP->_default_fg = COLOR_WHITE;
363 SP->_default_bg = COLOR_BLACK;
365 SP->_default_fg = C_MASK;
366 SP->_default_bg = C_MASK;
370 * Allow those assumed/default color assumptions to be overridden at
373 if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
376 int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
378 SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
380 SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
382 TR(TRACE_CHARPUT | TRACE_MOVE,
383 ("from environment assumed fg=%d, bg=%d",
390 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
391 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
392 * color to that value plus 8. We'll only use the non-bold color for now -
393 * decide later if it is worth having default attributes as well.
395 if (getenv("COLORFGBG") != 0) {
396 char *p = getenv("COLORFGBG");
397 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
398 p = extract_fgbg(p, &(SP->_default_fg));
399 p = extract_fgbg(p, &(SP->_default_bg));
400 if (*p) /* assume rxvt was compiled with xpm support */
401 p = extract_fgbg(p, &(SP->_default_bg));
402 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
403 SP->_default_fg, SP->_default_bg));
404 if (SP->_default_fg >= max_colors) {
405 if (set_a_foreground != ABSENT_STRING
406 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
407 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
409 SP->_default_fg %= max_colors;
412 if (SP->_default_bg >= max_colors) {
413 if (set_a_background != ABSENT_STRING
414 && !strcmp(set_a_background, "\033[4%p1%dm")) {
415 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
417 SP->_default_bg %= max_colors;
422 #endif /* NCURSES_EXT_FUNCS */
424 SP->_maxclick = DEFAULT_MAXCLICK;
425 SP->_mouse_event = no_mouse_event;
426 SP->_mouse_inline = no_mouse_inline;
427 SP->_mouse_parse = no_mouse_parse;
428 SP->_mouse_resume = no_mouse_resume;
429 SP->_mouse_wrap = no_mouse_wrap;
433 * If we've no magic cookie support, we suppress attributes that xmc would
434 * affect, i.e., the attributes that affect the rendition of a space.
436 SP->_ok_attributes = termattrs();
438 SP->_ok_attributes |= A_COLOR;
442 * If we have no magic-cookie support compiled-in, or if it is suppressed
443 * in the environment, reset the support-flag.
445 if (magic_cookie_glitch >= 0) {
446 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
447 support_cookies = FALSE;
452 if (!support_cookies && magic_cookie_glitch >= 0) {
453 T(("will disable attributes to work w/o magic cookies"));
456 if (magic_cookie_glitch > 0) { /* tvi, wyse */
458 SP->_xmc_triggers = SP->_ok_attributes & (
470 * We "should" treat colors as an attribute. The wyse350 (and its
471 * clones) appear to be the only ones that have both colors and magic
475 SP->_xmc_triggers |= A_COLOR;
478 SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
480 T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
482 * Supporting line-drawing may be possible. But make the regular
483 * video attributes work first.
485 acs_chars = ABSENT_STRING;
486 ena_acs = ABSENT_STRING;
487 enter_alt_charset_mode = ABSENT_STRING;
488 exit_alt_charset_mode = ABSENT_STRING;
491 * To keep the cookie support simple, suppress all of the optimization
492 * hooks except for clear_screen and the cursor addressing.
494 if (support_cookies) {
495 clr_eol = ABSENT_STRING;
496 clr_eos = ABSENT_STRING;
497 set_attributes = ABSENT_STRING;
500 } else if (magic_cookie_glitch == 0) { /* hpterm */
504 * If magic cookies are not supported, cancel the strings that set
507 if (!support_cookies && magic_cookie_glitch >= 0) {
508 magic_cookie_glitch = ABSENT_NUMERIC;
509 set_attributes = ABSENT_STRING;
510 enter_blink_mode = ABSENT_STRING;
511 enter_bold_mode = ABSENT_STRING;
512 enter_dim_mode = ABSENT_STRING;
513 enter_reverse_mode = ABSENT_STRING;
514 enter_standout_mode = ABSENT_STRING;
515 enter_underline_mode = ABSENT_STRING;
518 /* initialize normal acs before wide, since we use mapping in the latter */
519 #if !USE_WIDEC_SUPPORT
520 if (_nc_unicode_locale() && _nc_locale_breaks_acs(cur_term)) {
523 enter_alt_charset_mode = NULL;
524 exit_alt_charset_mode = NULL;
525 set_attributes = NULL;
529 #if USE_WIDEC_SUPPORT
532 SP->_screen_acs_fix = (_nc_unicode_locale()
533 && _nc_locale_breaks_acs(cur_term));
535 env = _nc_get_locale();
536 SP->_legacy_coding = ((env == 0)
538 || !strcmp(env, "POSIX"));
539 T(("legacy-coding %d", SP->_legacy_coding));
547 T(("creating newscr"));
548 if ((SP->_newscr = newwin(slines, scolumns, 0, 0)) == 0)
551 T(("creating curscr"));
552 if ((SP->_curscr = newwin(slines, scolumns, 0, 0)) == 0)
556 newscr = SP->_newscr;
557 curscr = SP->_curscr;
560 SP->_resize = NCURSES_SP_NAME(resizeterm);
563 newscr->_clear = TRUE;
564 curscr->_clear = FALSE;
569 for (rop = safe_ripoff_stack;
570 rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
573 /* If we must simulate soft labels, grab off the line to be used.
574 We assume that we must simulate, if it is none of the standard
575 formats (4-4 or 3-2-3) for which there may be some hardware
577 if (rop->hook == _nc_slk_initialize)
578 if (!(num_labels <= 0 || !SLK_STDFMT(slk_format)))
584 count = (rop->line < 0) ? -rop->line : rop->line;
585 T(("ripping off %i lines at %s", count,
590 w = newwin(count, scolumns,
592 ? SP->_lines_avail - count
597 rop->hook(w, scolumns);
602 bottom_stolen += count;
604 SP->_topstolen += count;
605 SP->_lines_avail -= count;
608 /* reset the stack */
609 safe_ripoff_sp = safe_ripoff_stack;
611 T(("creating stdscr"));
612 assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
613 if ((SP->_stdscr = newwin(SP->_lines_avail, scolumns, 0, 0)) == 0)
616 SET_LINES(SP->_lines_avail);
618 stdscr = SP->_stdscr;
625 * The internal implementation interprets line as the number of lines to rip
626 * off from the top or bottom.
629 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
631 int (*init) (WINDOW *, int))
633 T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
637 if (safe_ripoff_sp == 0)
638 safe_ripoff_sp = safe_ripoff_stack;
639 if (safe_ripoff_sp >= safe_ripoff_stack + N_RIPS)
642 safe_ripoff_sp->line = line;
643 safe_ripoff_sp->hook = init;
652 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
654 return NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN, line, init);
659 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
661 int (*init) (WINDOW *, int))
664 T((T_CALLED("ripoffline(%d,%p)"), line, init));
669 returnCode(NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
676 ripoffline(int line, int (*init) (WINDOW *, int))
678 return NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN, line, init);