1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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 ****************************************************************************/
38 ** The routine set_term().
42 #include <curses.priv.h>
44 #include <term.h> /* cur_term */
47 MODULE_ID("$Id: lib_set_term.c,v 1.99 2007/09/01 21:36:25 tom Exp $")
49 NCURSES_EXPORT(SCREEN *)
50 set_term(SCREEN *screenp)
54 T((T_CALLED("set_term(%p)"), screenp));
56 _nc_lock_mutex(set_SP);
59 _nc_set_screen(screenp);
61 set_curterm(SP->_term);
66 COLORS = SP->_color_count;
67 COLOR_PAIRS = SP->_pair_count;
70 _nc_unlock_mutex(set_SP);
72 T((T_RETURN("%p"), oldSP));
77 _nc_free_keytry(TRIES * kt)
80 _nc_free_keytry(kt->child);
81 _nc_free_keytry(kt->sibling);
87 * Free the storage associated with the given SCREEN sp.
92 SCREEN **scan = &_nc_screen_chain;
95 T((T_CALLED("delscreen(%p)"), sp));
97 _nc_lock_mutex(set_SP);
100 *scan = sp->_next_screen;
103 scan = &(*scan)->_next_screen;
106 (void) _nc_freewin(sp->_curscr);
107 (void) _nc_freewin(sp->_newscr);
108 (void) _nc_freewin(sp->_stdscr);
111 if (sp->_slk->ent != 0) {
112 for (i = 0; i < sp->_slk->labcnt; ++i) {
113 FreeIfNeeded(sp->_slk->ent[i].ent_text);
114 FreeIfNeeded(sp->_slk->ent[i].form_text);
122 _nc_free_keytry(sp->_keytry);
125 _nc_free_keytry(sp->_key_ok);
128 FreeIfNeeded(sp->_current_attr);
130 FreeIfNeeded(sp->_color_table);
131 FreeIfNeeded(sp->_color_pairs);
133 FreeIfNeeded(sp->oldhash);
134 FreeIfNeeded(sp->newhash);
135 FreeIfNeeded(sp->hashtab);
137 FreeIfNeeded(sp->_acs_map);
138 FreeIfNeeded(sp->_screen_acs_map);
140 del_curterm(sp->_term);
143 * If the associated output stream has been closed, we can discard the
144 * set-buffer. Limit the error check to EBADF, since fflush may fail
145 * for other reasons than trying to operate upon a closed stream.
149 && fflush(sp->_ofp) != 0
157 * If this was the current screen, reset everything that the
158 * application might try to use (except cur_term, which may have
159 * multiple references in different screens).
171 _nc_unlock_mutex(set_SP);
177 no_mouse_event(SCREEN *sp GCC_UNUSED)
183 no_mouse_inline(SCREEN *sp GCC_UNUSED)
189 no_mouse_parse(int code GCC_UNUSED)
195 no_mouse_resume(SCREEN *sp GCC_UNUSED)
200 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
204 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
206 extract_fgbg(char *src, int *result)
209 long value = strtol(src, &dst, 0);
213 } else if (value >= 0) {
216 while (*dst != 0 && *dst != ';')
224 #define ripoff_sp _nc_prescreen.rsp
225 #define ripoff_stack _nc_prescreen.rippedoff
227 /* OS-independent screen initializations */
229 _nc_setupscreen(int slines GCC_UNUSED,
230 int scolumns GCC_UNUSED,
235 int bottom_stolen = 0;
236 bool support_cookies = USE_XMC_SUPPORT;
239 T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
240 slines, scolumns, output, filtered, slk_format));
242 assert(SP == 0); /* has been reset in newterm() ! */
243 if (!_nc_alloc_screen()
244 || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
245 || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
249 T(("created SP %p", SP));
250 SP->_next_screen = _nc_screen_chain;
251 _nc_screen_chain = SP;
253 if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
257 * We should always check the screensize, just in case.
259 _nc_get_screensize(&slines, &scolumns);
262 T((T_CREATE("screen %s %dx%d"), termname(), LINES, COLS));
264 SP->_filtered = filtered;
266 /* implement filter mode */
271 cursor_down = parm_down_cursor = 0;
273 cursor_up = parm_up_cursor = 0;
276 cursor_home = carriage_return;
277 T(("filter screensize %dx%d", LINES, COLS));
280 T(("setting output mode to binary"));
282 setmode(output, O_BINARY);
284 _nc_set_buffer(output, TRUE);
285 SP->_term = cur_term;
287 SP->_lines_avail = slines;
288 SP->_columns = scolumns;
298 SP->_cursor = -1; /* cannot know real cursor shape */
300 #if NCURSES_NO_PADDING
301 SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
302 TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
303 SP->_no_padding ? " not" : ""));
306 #if NCURSES_EXT_FUNCS
307 SP->_default_color = FALSE;
308 SP->_has_sgr_39_49 = FALSE;
311 * Set our assumption of the terminal's default foreground and background
312 * colors. The curs_color man-page states that we can assume that the
313 * background is black. The origin of this assumption appears to be
314 * terminals that displayed colored text, but no colored backgrounds, e.g.,
315 * the first colored terminals around 1980. More recent ones with better
316 * technology can display not only colored backgrounds, but all
317 * combinations. So a terminal might be something other than "white" on
318 * black (green/black looks monochrome too), but black on white or even
321 * White-on-black is the simplest thing to use for monochrome. Almost
322 * all applications that use color paint both text and background, so
323 * the distinction is moot. But a few do not - which is why we leave this
324 * configurable (a better solution is to use assume_default_colors() for
325 * the rare applications that do require that sort of appearance, since
326 * is appears that more users expect to be able to make a white-on-black
327 * or black-on-white display under control of the application than not).
329 #ifdef USE_ASSUMED_COLOR
330 SP->_default_fg = COLOR_WHITE;
331 SP->_default_bg = COLOR_BLACK;
333 SP->_default_fg = C_MASK;
334 SP->_default_bg = C_MASK;
338 * Allow those assumed/default color assumptions to be overridden at
341 if (getenv("NCURSES_ASSUMED_COLORS") != 0) {
342 char *p = getenv("NCURSES_ASSUMED_COLORS");
345 int count = sscanf(p, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
347 SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
349 SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
351 TR(TRACE_CHARPUT | TRACE_MOVE,
352 ("from environment assumed fg=%d, bg=%d",
359 * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
360 * default colors. Note that rxvt (mis)uses bold colors, equating a bold
361 * color to that value plus 8. We'll only use the non-bold color for now -
362 * decide later if it is worth having default attributes as well.
364 if (getenv("COLORFGBG") != 0) {
365 char *p = getenv("COLORFGBG");
366 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
367 p = extract_fgbg(p, &(SP->_default_fg));
368 p = extract_fgbg(p, &(SP->_default_bg));
369 if (*p) /* assume rxvt was compiled with xpm support */
370 p = extract_fgbg(p, &(SP->_default_bg));
371 TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
372 SP->_default_fg, SP->_default_bg));
373 if (SP->_default_fg >= max_colors) {
374 if (set_a_foreground != ABSENT_STRING
375 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
376 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
378 SP->_default_fg %= max_colors;
381 if (SP->_default_bg >= max_colors) {
382 if (set_a_background != ABSENT_STRING
383 && !strcmp(set_a_background, "\033[4%p1%dm")) {
384 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
386 SP->_default_bg %= max_colors;
391 #endif /* NCURSES_EXT_FUNCS */
393 SP->_maxclick = DEFAULT_MAXCLICK;
394 SP->_mouse_event = no_mouse_event;
395 SP->_mouse_inline = no_mouse_inline;
396 SP->_mouse_parse = no_mouse_parse;
397 SP->_mouse_resume = no_mouse_resume;
398 SP->_mouse_wrap = no_mouse_wrap;
401 /* initialize the panel hooks */
402 SP->_panelHook.top_panel = (struct panel *) 0;
403 SP->_panelHook.bottom_panel = (struct panel *) 0;
404 SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
407 * If we've no magic cookie support, we suppress attributes that xmc would
408 * affect, i.e., the attributes that affect the rendition of a space.
410 SP->_ok_attributes = termattrs();
412 SP->_ok_attributes |= A_COLOR;
416 * If we have no magic-cookie support compiled-in, or if it is suppressed
417 * in the environment, reset the support-flag.
419 if (magic_cookie_glitch >= 0) {
420 if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
421 support_cookies = FALSE;
426 if (!support_cookies && magic_cookie_glitch >= 0) {
427 T(("will disable attributes to work w/o magic cookies"));
430 if (magic_cookie_glitch > 0) { /* tvi, wyse */
432 SP->_xmc_triggers = SP->_ok_attributes & (
444 * We "should" treat colors as an attribute. The wyse350 (and its
445 * clones) appear to be the only ones that have both colors and magic
449 SP->_xmc_triggers |= A_COLOR;
452 SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
454 T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
456 * Supporting line-drawing may be possible. But make the regular
457 * video attributes work first.
459 acs_chars = ABSENT_STRING;
460 ena_acs = ABSENT_STRING;
461 enter_alt_charset_mode = ABSENT_STRING;
462 exit_alt_charset_mode = ABSENT_STRING;
465 * To keep the cookie support simple, suppress all of the optimization
466 * hooks except for clear_screen and the cursor addressing.
468 if (support_cookies) {
469 clr_eol = ABSENT_STRING;
470 clr_eos = ABSENT_STRING;
471 set_attributes = ABSENT_STRING;
474 } else if (magic_cookie_glitch == 0) { /* hpterm */
478 * If magic cookies are not supported, cancel the strings that set
481 if (!support_cookies && magic_cookie_glitch >= 0) {
482 magic_cookie_glitch = ABSENT_NUMERIC;
483 set_attributes = ABSENT_STRING;
484 enter_blink_mode = ABSENT_STRING;
485 enter_bold_mode = ABSENT_STRING;
486 enter_dim_mode = ABSENT_STRING;
487 enter_reverse_mode = ABSENT_STRING;
488 enter_standout_mode = ABSENT_STRING;
489 enter_underline_mode = ABSENT_STRING;
492 /* initialize normal acs before wide, since we use mapping in the latter */
493 #if !USE_WIDEC_SUPPORT
494 if (_nc_unicode_locale() && _nc_locale_breaks_acs()) {
497 enter_alt_charset_mode = NULL;
498 exit_alt_charset_mode = NULL;
499 set_attributes = NULL;
503 #if USE_WIDEC_SUPPORT
506 SP->_screen_acs_fix = (_nc_unicode_locale() && _nc_locale_breaks_acs());
508 char *env = _nc_get_locale();
509 SP->_legacy_coding = ((env == 0)
511 || !strcmp(env, "POSIX"));
518 _nc_windows = 0; /* no windows yet */
523 T(("creating newscr"));
524 if ((SP->_newscr = newwin(slines, scolumns, 0, 0)) == 0)
527 T(("creating curscr"));
528 if ((SP->_curscr = newwin(slines, scolumns, 0, 0)) == 0)
532 newscr = SP->_newscr;
533 curscr = SP->_curscr;
536 SP->_resize = resizeterm;
539 newscr->_clear = TRUE;
540 curscr->_clear = FALSE;
545 for (rop = ripoff_stack;
546 rop != ripoff_sp && (rop - ripoff_stack) < N_RIPS;
549 /* If we must simulate soft labels, grab off the line to be used.
550 We assume that we must simulate, if it is none of the standard
551 formats (4-4 or 3-2-3) for which there may be some hardware
553 if (rop->hook == _nc_slk_initialize)
554 if (!(num_labels <= 0 || !SLK_STDFMT(slk_format)))
560 count = (rop->line < 0) ? -rop->line : rop->line;
561 T(("ripping off %i lines at %s", count,
566 w = newwin(count, scolumns,
568 ? SP->_lines_avail - count
572 rop->hook(w, scolumns);
576 bottom_stolen += count;
578 SP->_topstolen += count;
579 SP->_lines_avail -= count;
582 /* reset the stack */
583 ripoff_sp = ripoff_stack;
585 T(("creating stdscr"));
586 assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
587 if ((SP->_stdscr = newwin(SP->_lines_avail, scolumns, 0, 0)) == 0)
590 SET_LINES(SP->_lines_avail);
592 stdscr = SP->_stdscr;
599 * The internal implementation interprets line as the number of lines to rip
600 * off from the top or bottom.
603 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
605 T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
610 ripoff_sp = ripoff_stack;
611 if (ripoff_sp >= ripoff_stack + N_RIPS)
614 ripoff_sp->line = line;
615 ripoff_sp->hook = init;
623 ripoffline(int line, int (*init) (WINDOW *, int))
625 T((T_CALLED("ripoffline(%d,%p)"), line, init));
630 returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));