lib_set_term.c revision 166124
1/****************************************************************************
2 * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
3 *                                                                          *
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:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
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.                               *
22 *                                                                          *
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       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
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 ****************************************************************************/
34
35/*
36**	lib_set_term.c
37**
38**	The routine set_term().
39**
40*/
41
42#include <curses.priv.h>
43
44#include <term.h>		/* cur_term */
45#include <tic.h>
46
47MODULE_ID("$Id: lib_set_term.c,v 1.91 2006/05/20 14:58:02 tom Exp $")
48
49NCURSES_EXPORT(SCREEN *)
50set_term(SCREEN *screenp)
51{
52    SCREEN *oldSP;
53
54    T((T_CALLED("set_term(%p)"), screenp));
55
56    oldSP = SP;
57    _nc_set_screen(screenp);
58
59    set_curterm(SP->_term);
60    curscr = SP->_curscr;
61    newscr = SP->_newscr;
62    stdscr = SP->_stdscr;
63    COLORS = SP->_color_count;
64    COLOR_PAIRS = SP->_pair_count;
65
66    T((T_RETURN("%p"), oldSP));
67    return (oldSP);
68}
69
70static void
71_nc_free_keytry(struct tries *kt)
72{
73    if (kt != 0) {
74	_nc_free_keytry(kt->child);
75	_nc_free_keytry(kt->sibling);
76	free(kt);
77    }
78}
79
80/*
81 * Free the storage associated with the given SCREEN sp.
82 */
83NCURSES_EXPORT(void)
84delscreen(SCREEN *sp)
85{
86    SCREEN **scan = &_nc_screen_chain;
87    int i;
88
89    T((T_CALLED("delscreen(%p)"), sp));
90
91    while (*scan) {
92	if (*scan == sp) {
93	    *scan = sp->_next_screen;
94	    break;
95	}
96	scan = &(*scan)->_next_screen;
97    }
98
99    (void) _nc_freewin(sp->_curscr);
100    (void) _nc_freewin(sp->_newscr);
101    (void) _nc_freewin(sp->_stdscr);
102
103    if (sp->_slk != 0) {
104	if (sp->_slk->ent != 0) {
105	    for (i = 0; i < sp->_slk->labcnt; ++i) {
106		FreeIfNeeded(sp->_slk->ent[i].ent_text);
107		FreeIfNeeded(sp->_slk->ent[i].form_text);
108	    }
109	    free(sp->_slk->ent);
110	}
111	free(sp->_slk);
112	sp->_slk = 0;
113    }
114
115    _nc_free_keytry(sp->_keytry);
116    sp->_keytry = 0;
117
118    _nc_free_keytry(sp->_key_ok);
119    sp->_key_ok = 0;
120
121    FreeIfNeeded(sp->_current_attr);
122
123    FreeIfNeeded(sp->_color_table);
124    FreeIfNeeded(sp->_color_pairs);
125
126    FreeIfNeeded(sp->oldhash);
127    FreeIfNeeded(sp->newhash);
128    FreeIfNeeded(sp->hashtab);
129
130    FreeIfNeeded(sp->_acs_map);
131    FreeIfNeeded(sp->_screen_acs_map);
132
133    del_curterm(sp->_term);
134
135    /*
136     * If the associated output stream has been closed, we can discard the
137     * set-buffer.  Limit the error check to EBADF, since fflush may fail
138     * for other reasons than trying to operate upon a closed stream.
139     */
140    if (sp->_ofp != 0
141	&& sp->_setbuf != 0
142	&& fflush(sp->_ofp) != 0
143	&& errno == EBADF) {
144	free(sp->_setbuf);
145    }
146
147    free(sp);
148
149    /*
150     * If this was the current screen, reset everything that the
151     * application might try to use (except cur_term, which may have
152     * multiple references in different screens).
153     */
154    if (sp == SP) {
155	curscr = 0;
156	newscr = 0;
157	stdscr = 0;
158	COLORS = 0;
159	COLOR_PAIRS = 0;
160	_nc_set_screen(0);
161    }
162    returnVoid;
163}
164
165static ripoff_t rippedoff[5];
166static ripoff_t *rsp = rippedoff;
167#define N_RIPS SIZEOF(SP->_rippedoff)
168
169static bool
170no_mouse_event(SCREEN *sp GCC_UNUSED)
171{
172    return FALSE;
173}
174
175static bool
176no_mouse_inline(SCREEN *sp GCC_UNUSED)
177{
178    return FALSE;
179}
180
181static bool
182no_mouse_parse(int code GCC_UNUSED)
183{
184    return TRUE;
185}
186
187static void
188no_mouse_resume(SCREEN *sp GCC_UNUSED)
189{
190}
191
192static void
193no_mouse_wrap(SCREEN *sp GCC_UNUSED)
194{
195}
196
197#if NCURSES_EXT_FUNCS && USE_COLORFGBG
198static char *
199extract_fgbg(char *src, int *result)
200{
201    char *dst = 0;
202    long value = strtol(src, &dst, 0);
203
204    if (dst == 0) {
205	dst = src;
206    } else if (value >= 0) {
207	*result = value;
208    }
209    while (*dst != 0 && *dst != ';')
210	dst++;
211    if (*dst == ';')
212	dst++;
213    return dst;
214}
215#endif
216
217/* OS-independent screen initializations */
218NCURSES_EXPORT(int)
219_nc_setupscreen(int slines,
220		int scolumns,
221		FILE *output,
222		bool filtered,
223		int slk_format)
224{
225    int bottom_stolen = 0;
226    int i;
227    bool support_cookies = USE_XMC_SUPPORT;
228
229    T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
230       slines, scolumns, output, filtered, slk_format));
231
232    assert(SP == 0);		/* has been reset in newterm() ! */
233    if (!_nc_alloc_screen()
234	|| ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
235	|| ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
236	returnCode(ERR);
237    }
238
239    T(("created SP %p", SP));
240    SP->_next_screen = _nc_screen_chain;
241    _nc_screen_chain = SP;
242
243    if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
244	returnCode(ERR);
245
246    SP->_filtered = filtered;
247
248    /* implement filter mode */
249    if (filtered) {
250	slines = LINES = 1;
251
252	clear_screen = 0;
253	cursor_down = parm_down_cursor = 0;
254	cursor_address = 0;
255	cursor_up = parm_up_cursor = 0;
256	row_address = 0;
257
258	cursor_home = carriage_return;
259	T(("filter screensize %dx%d", LINES, COLS));
260    }
261
262    /* If we must simulate soft labels, grab off the line to be used.
263       We assume that we must simulate, if it is none of the standard
264       formats (4-4  or 3-2-3) for which there may be some hardware
265       support. */
266    if (num_labels <= 0 || !SLK_STDFMT(slk_format)) {
267	if (slk_format) {
268	    if (ERR == _nc_ripoffline(-SLK_LINES(slk_format),
269				      _nc_slk_initialize))
270		returnCode(ERR);
271	}
272    }
273#ifdef __DJGPP__
274    T(("setting output mode to binary"));
275    fflush(output);
276    setmode(output, O_BINARY);
277#endif
278    _nc_set_buffer(output, TRUE);
279    SP->_term = cur_term;
280    SP->_lines = slines;
281    SP->_lines_avail = slines;
282    SP->_columns = scolumns;
283    SP->_cursrow = -1;
284    SP->_curscol = -1;
285    SP->_nl = TRUE;
286    SP->_raw = FALSE;
287    SP->_cbreak = 0;
288    SP->_echo = TRUE;
289    SP->_fifohead = -1;
290    SP->_endwin = TRUE;
291    SP->_ofp = output;
292    SP->_cursor = -1;		/* cannot know real cursor shape */
293
294#if NCURSES_NO_PADDING
295    SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
296    TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
297				    SP->_no_padding ? " not" : ""));
298#endif
299
300#if NCURSES_EXT_FUNCS
301    SP->_default_color = FALSE;
302    SP->_has_sgr_39_49 = FALSE;
303
304    /*
305     * Set our assumption of the terminal's default foreground and background
306     * colors.  The curs_color man-page states that we can assume that the
307     * background is black.  The origin of this assumption appears to be
308     * terminals that displayed colored text, but no colored backgrounds, e.g.,
309     * the first colored terminals around 1980.  More recent ones with better
310     * technology can display not only colored backgrounds, but all
311     * combinations.  So a terminal might be something other than "white" on
312     * black (green/black looks monochrome too), but black on white or even
313     * on ivory.
314     *
315     * White-on-black is the simplest thing to use for monochrome.  Almost
316     * all applications that use color paint both text and background, so
317     * the distinction is moot.  But a few do not - which is why we leave this
318     * configurable (a better solution is to use assume_default_colors() for
319     * the rare applications that do require that sort of appearance, since
320     * is appears that more users expect to be able to make a white-on-black
321     * or black-on-white display under control of the application than not).
322     */
323#ifdef USE_ASSUMED_COLOR
324    SP->_default_fg = COLOR_WHITE;
325    SP->_default_bg = COLOR_BLACK;
326#else
327    SP->_default_fg = C_MASK;
328    SP->_default_bg = C_MASK;
329#endif
330
331    /*
332     * Allow those assumed/default color assumptions to be overridden at
333     * runtime:
334     */
335    if (getenv("NCURSES_ASSUMED_COLORS") != 0) {
336	char *p = getenv("NCURSES_ASSUMED_COLORS");
337	int fg, bg;
338	char sep1, sep2;
339	int count = sscanf(p, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
340	if (count >= 1) {
341	    SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
342	    if (count >= 3) {
343		SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
344	    }
345	    TR(TRACE_CHARPUT | TRACE_MOVE,
346	       ("from environment assumed fg=%d, bg=%d",
347		SP->_default_fg,
348		SP->_default_bg));
349	}
350    }
351#if USE_COLORFGBG
352    /*
353     * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
354     * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
355     * color to that value plus 8.  We'll only use the non-bold color for now -
356     * decide later if it is worth having default attributes as well.
357     */
358    if (getenv("COLORFGBG") != 0) {
359	char *p = getenv("COLORFGBG");
360	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
361	p = extract_fgbg(p, &(SP->_default_fg));
362	p = extract_fgbg(p, &(SP->_default_bg));
363	if (*p)			/* assume rxvt was compiled with xpm support */
364	    p = extract_fgbg(p, &(SP->_default_bg));
365	TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
366					SP->_default_fg, SP->_default_bg));
367	if (SP->_default_fg >= max_colors) {
368	    if (set_a_foreground != ABSENT_STRING
369		&& !strcmp(set_a_foreground, "\033[3%p1%dm")) {
370		set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
371	    } else {
372		SP->_default_fg %= max_colors;
373	    }
374	}
375	if (SP->_default_bg >= max_colors) {
376	    if (set_a_background != ABSENT_STRING
377		&& !strcmp(set_a_background, "\033[4%p1%dm")) {
378		set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
379	    } else {
380		SP->_default_bg %= max_colors;
381	    }
382	}
383    }
384#endif
385#endif /* NCURSES_EXT_FUNCS */
386
387    SP->_maxclick = DEFAULT_MAXCLICK;
388    SP->_mouse_event = no_mouse_event;
389    SP->_mouse_inline = no_mouse_inline;
390    SP->_mouse_parse = no_mouse_parse;
391    SP->_mouse_resume = no_mouse_resume;
392    SP->_mouse_wrap = no_mouse_wrap;
393    SP->_mouse_fd = -1;
394
395    /* initialize the panel hooks */
396    SP->_panelHook.top_panel = (struct panel *) 0;
397    SP->_panelHook.bottom_panel = (struct panel *) 0;
398    SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
399
400    /*
401     * If we've no magic cookie support, we suppress attributes that xmc would
402     * affect, i.e., the attributes that affect the rendition of a space.
403     */
404    SP->_ok_attributes = termattrs();
405    if (has_colors()) {
406	SP->_ok_attributes |= A_COLOR;
407    }
408#if USE_XMC_SUPPORT
409    /*
410     * If we have no magic-cookie support compiled-in, or if it is suppressed
411     * in the environment, reset the support-flag.
412     */
413    if (magic_cookie_glitch >= 0) {
414	if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
415	    support_cookies = FALSE;
416	}
417    }
418#endif
419
420    if (!support_cookies && magic_cookie_glitch >= 0) {
421	T(("will disable attributes to work w/o magic cookies"));
422    }
423
424    if (magic_cookie_glitch > 0) {	/* tvi, wyse */
425
426	SP->_xmc_triggers = SP->_ok_attributes & (
427						     A_STANDOUT |
428						     A_UNDERLINE |
429						     A_REVERSE |
430						     A_BLINK |
431						     A_DIM |
432						     A_BOLD |
433						     A_INVIS |
434						     A_PROTECT
435	    );
436#if 0
437	/*
438	 * We "should" treat colors as an attribute.  The wyse350 (and its
439	 * clones) appear to be the only ones that have both colors and magic
440	 * cookies.
441	 */
442	if (has_colors()) {
443	    SP->_xmc_triggers |= A_COLOR;
444	}
445#endif
446	SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
447
448	T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
449	/*
450	 * Supporting line-drawing may be possible.  But make the regular
451	 * video attributes work first.
452	 */
453	acs_chars = ABSENT_STRING;
454	ena_acs = ABSENT_STRING;
455	enter_alt_charset_mode = ABSENT_STRING;
456	exit_alt_charset_mode = ABSENT_STRING;
457#if USE_XMC_SUPPORT
458	/*
459	 * To keep the cookie support simple, suppress all of the optimization
460	 * hooks except for clear_screen and the cursor addressing.
461	 */
462	if (support_cookies) {
463	    clr_eol = ABSENT_STRING;
464	    clr_eos = ABSENT_STRING;
465	    set_attributes = ABSENT_STRING;
466	}
467#endif
468    } else if (magic_cookie_glitch == 0) {	/* hpterm */
469    }
470
471    /*
472     * If magic cookies are not supported, cancel the strings that set
473     * video attributes.
474     */
475    if (!support_cookies && magic_cookie_glitch >= 0) {
476	magic_cookie_glitch = ABSENT_NUMERIC;
477	set_attributes = ABSENT_STRING;
478	enter_blink_mode = ABSENT_STRING;
479	enter_bold_mode = ABSENT_STRING;
480	enter_dim_mode = ABSENT_STRING;
481	enter_reverse_mode = ABSENT_STRING;
482	enter_standout_mode = ABSENT_STRING;
483	enter_underline_mode = ABSENT_STRING;
484    }
485
486    /* initialize normal acs before wide, since we use mapping in the latter */
487    _nc_init_acs();
488#if USE_WIDEC_SUPPORT
489    _nc_init_wacs();
490
491    SP->_screen_acs_fix = (_nc_unicode_locale() && _nc_locale_breaks_acs());
492    {
493	char *env = _nc_get_locale();
494	SP->_legacy_coding = ((env == 0)
495			      || !strcmp(env, "C")
496			      || !strcmp(env, "POSIX"));
497    }
498#endif
499
500    _nc_idcok = TRUE;
501    _nc_idlok = FALSE;
502
503    _nc_windows = 0;		/* no windows yet */
504
505    SP->oldhash = 0;
506    SP->newhash = 0;
507
508    T(("creating newscr"));
509    if ((newscr = newwin(slines, scolumns, 0, 0)) == 0)
510	returnCode(ERR);
511
512    T(("creating curscr"));
513    if ((curscr = newwin(slines, scolumns, 0, 0)) == 0)
514	returnCode(ERR);
515
516    SP->_newscr = newscr;
517    SP->_curscr = curscr;
518#if USE_SIZECHANGE
519    SP->_resize = resizeterm;
520#endif
521
522    newscr->_clear = TRUE;
523    curscr->_clear = FALSE;
524
525    def_shell_mode();
526    def_prog_mode();
527
528    for (i = 0, rsp = rippedoff; rsp->line && (i < (int) N_RIPS); rsp++, i++) {
529	T(("ripping off line %d at %s", i, rsp->line < 0 ? "bottom" : "top"));
530	SP->_rippedoff[i] = rippedoff[i];
531	if (rsp->hook) {
532	    int count = (rsp->line < 0) ? -rsp->line : rsp->line;
533
534	    SP->_rippedoff[i].w = newwin(count,
535					 scolumns,
536					 ((rsp->line < 0)
537					  ? SP->_lines_avail - count
538					  : 0),
539					 0);
540	    if (SP->_rippedoff[i].w != 0)
541		SP->_rippedoff[i].hook(SP->_rippedoff[i].w, scolumns);
542	    else
543		returnCode(ERR);
544	    if (rsp->line < 0)
545		bottom_stolen += count;
546	    else
547		SP->_topstolen += count;
548	    SP->_lines_avail -= count;
549	}
550	rsp->line = 0;
551    }
552    SP->_rip_count = i;
553    /* reset the stack */
554    rsp = rippedoff;
555
556    T(("creating stdscr"));
557    assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
558    if ((stdscr = newwin(LINES = SP->_lines_avail, scolumns, 0, 0)) == 0)
559	returnCode(ERR);
560    SP->_stdscr = stdscr;
561
562    returnCode(OK);
563}
564
565/* The internal implementation interprets line as the number of
566   lines to rip off from the top or bottom.
567   */
568NCURSES_EXPORT(int)
569_nc_ripoffline(int line, int (*init) (WINDOW *, int))
570{
571    T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
572
573    if (line != 0) {
574
575	if (rsp >= rippedoff + N_RIPS)
576	    returnCode(ERR);
577
578	rsp->line = line;
579	rsp->hook = init;
580	rsp->w = 0;
581	rsp++;
582    }
583
584    returnCode(OK);
585}
586
587NCURSES_EXPORT(int)
588ripoffline(int line, int (*init) (WINDOW *, int))
589{
590    T((T_CALLED("ripoffline(%d,%p)"), line, init));
591
592    if (line == 0)
593	returnCode(OK);
594
595    returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
596}
597