lib_set_term.c revision 76726
1205821Sedwin/****************************************************************************
2205872Sedwin * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
3205872Sedwin *                                                                          *
4205821Sedwin * Permission is hereby granted, free of charge, to any person obtaining a  *
5205821Sedwin * copy of this software and associated documentation files (the            *
6205821Sedwin * "Software"), to deal in the Software without restriction, including      *
7205821Sedwin * without limitation the rights to use, copy, modify, merge, publish,      *
8205821Sedwin * distribute, distribute with modifications, sublicense, and/or sell       *
9205821Sedwin * copies of the Software, and to permit persons to whom the Software is    *
10205821Sedwin * furnished to do so, subject to the following conditions:                 *
11205821Sedwin *                                                                          *
12205821Sedwin * The above copyright notice and this permission notice shall be included  *
13205821Sedwin * in all copies or substantial portions of the Software.                   *
14205821Sedwin *                                                                          *
15205821Sedwin * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16205821Sedwin * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17205821Sedwin * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18205821Sedwin * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19205821Sedwin * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20205821Sedwin * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21205821Sedwin * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22205821Sedwin *                                                                          *
23205821Sedwin * Except as contained in this notice, the name(s) of the above copyright   *
24205821Sedwin * holders shall not be used in advertising or otherwise to promote the     *
25205821Sedwin * sale, use or other dealings in this Software without prior written       *
26205821Sedwin * authorization.                                                           *
27205821Sedwin ****************************************************************************/
28205821Sedwin
29205821Sedwin/****************************************************************************
30205821Sedwin *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31205821Sedwin *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32205821Sedwin ****************************************************************************/
33205821Sedwin
34205821Sedwin/*
35205821Sedwin**	lib_set_term.c
36205821Sedwin**
37205821Sedwin**	The routine set_term().
38205821Sedwin**
39205821Sedwin*/
40205821Sedwin
41205821Sedwin#include <curses.priv.h>
42205821Sedwin
43205821Sedwin#include <term.h>		/* cur_term */
44205821Sedwin#include <tic.h>
45205821Sedwin
46205821SedwinMODULE_ID("$Id: lib_set_term.c,v 1.61 2000/12/10 02:43:27 tom Exp $")
47205821Sedwin
48205821SedwinNCURSES_EXPORT(SCREEN *)
49205821Sedwinset_term(SCREEN * screenp)
50205821Sedwin{
51205821Sedwin    SCREEN *oldSP;
52205821Sedwin
53205821Sedwin    T((T_CALLED("set_term(%p)"), screenp));
54205821Sedwin
55205821Sedwin    oldSP = SP;
56205821Sedwin    _nc_set_screen(screenp);
57205821Sedwin
58205821Sedwin    set_curterm(SP->_term);
59205821Sedwin    curscr = SP->_curscr;
60205821Sedwin    newscr = SP->_newscr;
61205821Sedwin    stdscr = SP->_stdscr;
62205821Sedwin    COLORS = SP->_color_count;
63205821Sedwin    COLOR_PAIRS = SP->_pair_count;
64205821Sedwin    memcpy(acs_map, SP->_acs_map, sizeof(chtype) * ACS_LEN);
65205821Sedwin
66205821Sedwin    T((T_RETURN("%p"), oldSP));
67205821Sedwin    return (oldSP);
68205821Sedwin}
69205821Sedwin
70205821Sedwinstatic void
71205821Sedwin_nc_free_keytry(struct tries *kt)
72205821Sedwin{
73205821Sedwin    if (kt != 0) {
74205821Sedwin	_nc_free_keytry(kt->child);
75205821Sedwin	_nc_free_keytry(kt->sibling);
76205821Sedwin	free(kt);
77205821Sedwin    }
78205821Sedwin}
79205821Sedwin
80205821Sedwin/*
81205821Sedwin * Free the storage associated with the given SCREEN sp.
82205821Sedwin */
83205821SedwinNCURSES_EXPORT(void)
84205821Sedwindelscreen(SCREEN * sp)
85205821Sedwin{
86205821Sedwin    SCREEN **scan = &_nc_screen_chain;
87205821Sedwin
88205821Sedwin    T((T_CALLED("delscreen(%p)"), sp));
89205821Sedwin
90205821Sedwin    while (*scan) {
91205821Sedwin	if (*scan == sp) {
92205821Sedwin	    *scan = sp->_next_screen;
93205821Sedwin	    break;
94205821Sedwin	}
95205821Sedwin	scan = &(*scan)->_next_screen;
96205821Sedwin    }
97205821Sedwin
98205821Sedwin    (void) _nc_freewin(sp->_curscr);
99205821Sedwin    (void) _nc_freewin(sp->_newscr);
100205821Sedwin    (void) _nc_freewin(sp->_stdscr);
101205821Sedwin    _nc_free_keytry(sp->_keytry);
102205821Sedwin    _nc_free_keytry(sp->_key_ok);
103205821Sedwin
104205821Sedwin    FreeIfNeeded(sp->_color_table);
105205821Sedwin    FreeIfNeeded(sp->_color_pairs);
106205821Sedwin
107205821Sedwin    FreeIfNeeded(sp->oldhash);
108205821Sedwin    FreeIfNeeded(sp->newhash);
109205821Sedwin
110205821Sedwin    del_curterm(sp->_term);
111205821Sedwin
112205821Sedwin    /*
113205821Sedwin     * If the associated output stream has been closed, we can discard the
114205821Sedwin     * set-buffer.  Limit the error check to EBADF, since fflush may fail
115205821Sedwin     * for other reasons than trying to operate upon a closed stream.
116205821Sedwin     */
117205821Sedwin    if (sp->_ofp != 0
118205821Sedwin	&& sp->_setbuf != 0
119205821Sedwin	&& fflush(sp->_ofp) != 0
120205821Sedwin	&& errno == EBADF) {
121205821Sedwin	free(sp->_setbuf);
122205821Sedwin    }
123205821Sedwin
124205821Sedwin    free(sp);
125205821Sedwin
126205821Sedwin    /*
127     * If this was the current screen, reset everything that the
128     * application might try to use (except cur_term, which may have
129     * multiple references in different screens).
130     */
131    if (sp == SP) {
132	curscr = 0;
133	newscr = 0;
134	stdscr = 0;
135	COLORS = 0;
136	COLOR_PAIRS = 0;
137	_nc_set_screen(0);
138    }
139    returnVoid;
140}
141
142static ripoff_t rippedoff[5];
143static ripoff_t *rsp = rippedoff;
144#define N_RIPS SIZEOF(rippedoff)
145
146static bool
147no_mouse_event(SCREEN * sp GCC_UNUSED)
148{
149    return FALSE;
150}
151
152static bool
153no_mouse_inline(SCREEN * sp GCC_UNUSED)
154{
155    return FALSE;
156}
157
158static bool
159no_mouse_parse(int code GCC_UNUSED)
160{
161    return TRUE;
162}
163
164static void
165no_mouse_resume(SCREEN * sp GCC_UNUSED)
166{
167}
168
169static void
170no_mouse_wrap(SCREEN * sp GCC_UNUSED)
171{
172}
173
174#if NCURSES_EXT_FUNCS && USE_COLORFGBG
175static char *
176extract_fgbg(char *src, int *result)
177{
178    char *dst = 0;
179    long value = strtol(src, &dst, 0);
180
181    if (dst == 0) {
182	dst = src;
183    } else if (value >= 0) {
184	*result = value % max_colors;
185    }
186    while (*dst != 0 && *dst != ';')
187	dst++;
188    if (*dst == ';')
189	dst++;
190    return dst;
191}
192#endif
193
194NCURSES_EXPORT(int)
195_nc_setupscreen
196(short slines, short const scolumns, FILE * output)
197/* OS-independent screen initializations */
198{
199    int bottom_stolen = 0;
200    size_t i;
201
202    assert(SP == 0);		/* has been reset in newterm() ! */
203    if (!_nc_alloc_screen())
204	return ERR;
205
206    SP->_next_screen = _nc_screen_chain;
207    _nc_screen_chain = SP;
208
209    _nc_set_buffer(output, TRUE);
210    SP->_term = cur_term;
211    SP->_lines = slines;
212    SP->_lines_avail = slines;
213    SP->_columns = scolumns;
214    SP->_cursrow = -1;
215    SP->_curscol = -1;
216    SP->_nl = TRUE;
217    SP->_raw = FALSE;
218    SP->_cbreak = 0;
219    SP->_echo = TRUE;
220    SP->_fifohead = -1;
221    SP->_endwin = TRUE;
222    SP->_ofp = output;
223    SP->_cursor = -1;		/* cannot know real cursor shape */
224
225#if NCURSES_NO_PADDING
226    SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
227    TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
228				    SP->_no_padding ? " not" : ""));
229#endif
230
231#if NCURSES_EXT_FUNCS
232    SP->_default_color = FALSE;
233    SP->_has_sgr_39_49 = FALSE;
234
235    /*
236     * Set our assumption of the terminal's default foreground and background
237     * colors.  The curs_color man-page states that we can assume that the
238     * background is black.  The origin of this assumption appears to be
239     * terminals that displayed colored text, but no colored backgrounds, e.g.,
240     * the first colored terminals around 1980.  More recent ones with better
241     * technology can display not only colored backgrounds, but all
242     * combinations.  So a terminal might be something other than "white" on
243     * black (green/black looks monochrome too), but black on white or even
244     * on ivory.
245     *
246     * White-on-black is the simplest thing to use for monochrome.  Almost
247     * all applications that use color paint both text and background, so
248     * the distinction is moot.  But a few do not - which is why we leave this
249     * configurable (a better solution is to use assume_default_colors() for
250     * the rare applications that do require that sort of appearance, since
251     * is appears that more users expect to be able to make a white-on-black
252     * or black-on-white display under control of the application than not).
253     */
254#ifdef USE_ASSUMED_COLOR
255    SP->_default_fg = COLOR_WHITE;
256    SP->_default_bg = COLOR_BLACK;
257#else
258    SP->_default_fg = C_MASK;
259    SP->_default_bg = C_MASK;
260#endif
261
262#if USE_COLORFGBG
263    /*
264     * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
265     * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
266     * color to that value plus 8.  We'll only use the non-bold color for now -
267     * decide later if it is worth having default attributes as well.
268     */
269    if (getenv("COLORFGBG") != 0) {
270	char *p = getenv("COLORFGBG");
271	p = extract_fgbg(p, &(SP->_default_fg));
272	p = extract_fgbg(p, &(SP->_default_bg));
273    }
274#endif
275#endif /* NCURSES_EXT_FUNCS */
276
277    SP->_maxclick = DEFAULT_MAXCLICK;
278    SP->_mouse_event = no_mouse_event;
279    SP->_mouse_inline = no_mouse_inline;
280    SP->_mouse_parse = no_mouse_parse;
281    SP->_mouse_resume = no_mouse_resume;
282    SP->_mouse_wrap = no_mouse_wrap;
283    SP->_mouse_fd = -1;
284
285    /* initialize the panel hooks */
286    SP->_panelHook.top_panel = (struct panel *) 0;
287    SP->_panelHook.bottom_panel = (struct panel *) 0;
288    SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
289
290    /*
291     * If we've no magic cookie support, we suppress attributes that xmc
292     * would affect, i.e., the attributes that affect the rendition of a
293     * space.  Note that this impacts the alternate character set mapping
294     * as well.
295     */
296    if (magic_cookie_glitch > 0) {
297
298	SP->_xmc_triggers = termattrs() & (
299					      A_ALTCHARSET |
300					      A_BLINK |
301					      A_BOLD |
302					      A_REVERSE |
303					      A_STANDOUT |
304					      A_UNDERLINE
305	    );
306	SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~ (A_BOLD);
307
308	T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
309#if USE_XMC_SUPPORT
310	/*
311	 * To keep this simple, suppress all of the optimization hooks
312	 * except for clear_screen and the cursor addressing.
313	 */
314	clr_eol = 0;
315	clr_eos = 0;
316	set_attributes = 0;
317#else
318	magic_cookie_glitch = ABSENT_NUMERIC;
319	acs_chars = 0;
320#endif
321    }
322    _nc_init_acs();
323    memcpy(SP->_acs_map, acs_map, sizeof(chtype) * ACS_LEN);
324
325    _nc_idcok = TRUE;
326    _nc_idlok = FALSE;
327
328    _nc_windows = 0;		/* no windows yet */
329
330    SP->oldhash = 0;
331    SP->newhash = 0;
332
333    T(("creating newscr"));
334    if ((newscr = newwin(slines, scolumns, 0, 0)) == 0)
335	return ERR;
336
337    T(("creating curscr"));
338    if ((curscr = newwin(slines, scolumns, 0, 0)) == 0)
339	return ERR;
340
341    SP->_newscr = newscr;
342    SP->_curscr = curscr;
343#if USE_SIZECHANGE
344    SP->_resize = resizeterm;
345#endif
346
347    newscr->_clear = TRUE;
348    curscr->_clear = FALSE;
349
350    for (i = 0, rsp = rippedoff; rsp->line && (i < N_RIPS); rsp++, i++) {
351	if (rsp->hook) {
352	    WINDOW *w;
353	    int count = (rsp->line < 0) ? -rsp->line : rsp->line;
354
355	    if (rsp->line < 0) {
356		w = newwin(count, scolumns, SP->_lines_avail - count, 0);
357		if (w) {
358		    rsp->w = w;
359		    rsp->hook(w, scolumns);
360		    bottom_stolen += count;
361		} else
362		    return ERR;
363	    } else {
364		w = newwin(count, scolumns, 0, 0);
365		if (w) {
366		    rsp->w = w;
367		    rsp->hook(w, scolumns);
368		    SP->_topstolen += count;
369		} else
370		    return ERR;
371	    }
372	    SP->_lines_avail -= count;
373	}
374	rsp->line = 0;
375    }
376    /* reset the stack */
377    rsp = rippedoff;
378
379    T(("creating stdscr"));
380    assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
381    if ((stdscr = newwin(LINES = SP->_lines_avail, scolumns, 0, 0)) == 0)
382	return ERR;
383    SP->_stdscr = stdscr;
384
385    def_shell_mode();
386    def_prog_mode();
387
388    return OK;
389}
390
391/* The internal implementation interprets line as the number of
392   lines to rip off from the top or bottom.
393   */
394NCURSES_EXPORT(int)
395_nc_ripoffline(int line, int (*init) (WINDOW *, int))
396{
397    if (line == 0)
398	return (OK);
399
400    if (rsp >= rippedoff + N_RIPS)
401	return (ERR);
402
403    rsp->line = line;
404    rsp->hook = init;
405    rsp->w = 0;
406    rsp++;
407
408    return (OK);
409}
410
411NCURSES_EXPORT(int)
412ripoffline(int line, int (*init) (WINDOW *, int))
413{
414    T((T_CALLED("ripoffline(%d,%p)"), line, init));
415
416    if (line == 0)
417	returnCode(OK);
418
419    returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
420}
421