xref: /openbsd-src/lib/libcurses/base/lib_set_term.c (revision 852c4eda9a667a259e9fed88dd1f9d35330b27e1)
1 /*	$OpenBSD: lib_set_term.c,v 1.10 2000/07/10 03:06:15 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37 **	lib_set_term.c
38 **
39 **	The routine set_term().
40 **
41 */
42 
43 #include <curses.priv.h>
44 
45 #include <term.h>		/* cur_term */
46 #include <tic.h>
47 
48 MODULE_ID("$From: lib_set_term.c,v 1.55 2000/07/02 00:22:18 tom Exp $")
49 
50 SCREEN *
51 set_term(SCREEN * screenp)
52 {
53     SCREEN *oldSP;
54 
55     T((T_CALLED("set_term(%p)"), screenp));
56 
57     oldSP = SP;
58     _nc_set_screen(screenp);
59 
60     set_curterm(SP->_term);
61     curscr = SP->_curscr;
62     newscr = SP->_newscr;
63     stdscr = SP->_stdscr;
64     COLORS = SP->_color_count;
65     COLOR_PAIRS = SP->_pair_count;
66     memcpy(acs_map, SP->_acs_map, sizeof(chtype) * ACS_LEN);
67 
68     T((T_RETURN("%p"), oldSP));
69     return (oldSP);
70 }
71 
72 static void
73 _nc_free_keytry(struct tries *kt)
74 {
75     if (kt != 0) {
76 	_nc_free_keytry(kt->child);
77 	_nc_free_keytry(kt->sibling);
78 	free(kt);
79     }
80 }
81 
82 /*
83  * Free the storage associated with the given SCREEN sp.
84  */
85 void
86 delscreen(SCREEN * sp)
87 {
88     SCREEN **scan = &_nc_screen_chain;
89 
90     T((T_CALLED("delscreen(%p)"), sp));
91 
92     while (*scan) {
93 	if (*scan == sp) {
94 	    *scan = sp->_next_screen;
95 	    break;
96 	}
97 	scan = &(*scan)->_next_screen;
98     }
99 
100     _nc_freewin(sp->_curscr);
101     _nc_freewin(sp->_newscr);
102     _nc_freewin(sp->_stdscr);
103     _nc_free_keytry(sp->_keytry);
104     _nc_free_keytry(sp->_key_ok);
105 
106     FreeIfNeeded(sp->_color_table);
107     FreeIfNeeded(sp->_color_pairs);
108 
109     FreeIfNeeded(sp->oldhash);
110     FreeIfNeeded(sp->newhash);
111 
112     del_curterm(sp->_term);
113 
114     /*
115      * If the associated output stream has been closed, we can discard the
116      * set-buffer.  Limit the error check to EBADF, since fflush may fail
117      * for other reasons than trying to operate upon a closed stream.
118      */
119     if (sp->_ofp != 0
120 	&& sp->_setbuf != 0
121 	&& fflush(sp->_ofp) != 0
122 	&& errno == EBADF) {
123 	free(sp->_setbuf);
124     }
125 
126     free(sp);
127 
128     /*
129      * If this was the current screen, reset everything that the
130      * application might try to use (except cur_term, which may have
131      * multiple references in different screens).
132      */
133     if (sp == SP) {
134 	curscr = 0;
135 	newscr = 0;
136 	stdscr = 0;
137 	COLORS = 0;
138 	COLOR_PAIRS = 0;
139 	_nc_set_screen(0);
140     }
141     returnVoid;
142 }
143 
144 static ripoff_t rippedoff[5];
145 static ripoff_t *rsp = rippedoff;
146 #define N_RIPS SIZEOF(rippedoff)
147 
148 static bool
149 no_mouse_event(SCREEN * sp GCC_UNUSED)
150 {
151     return FALSE;
152 }
153 
154 static bool
155 no_mouse_inline(SCREEN * sp GCC_UNUSED)
156 {
157     return FALSE;
158 }
159 
160 static bool
161 no_mouse_parse(int code GCC_UNUSED)
162 {
163     return TRUE;
164 }
165 
166 static void
167 no_mouse_resume(SCREEN * sp GCC_UNUSED)
168 {
169 }
170 
171 static void
172 no_mouse_wrap(SCREEN * sp GCC_UNUSED)
173 {
174 }
175 
176 #if defined(NCURSES_EXT_FUNCS) && defined(USE_COLORFGBG)
177 static char *
178 extract_fgbg(char *src, int *result)
179 {
180     char *dst = 0;
181     long value = strtol(src, &dst, 0);
182 
183     if (dst == 0) {
184 	dst = src;
185     } else if (value >= 0) {
186 	*result = value % max_colors;
187     }
188     while (*dst != 0 && *dst != ';')
189 	dst++;
190     if (*dst == ';')
191 	dst++;
192     return dst;
193 }
194 #endif
195 
196 int
197 _nc_setupscreen(short slines, short const scolumns, FILE * output)
198 /* OS-independent screen initializations */
199 {
200     int bottom_stolen = 0;
201     size_t i;
202 
203     assert(SP == 0);		/* has been reset in newterm() ! */
204     if (!_nc_alloc_screen())
205 	return ERR;
206 
207     SP->_next_screen = _nc_screen_chain;
208     _nc_screen_chain = SP;
209 
210     _nc_set_buffer(output, TRUE);
211     SP->_term = cur_term;
212     SP->_lines = slines;
213     SP->_lines_avail = slines;
214     SP->_columns = scolumns;
215     SP->_cursrow = -1;
216     SP->_curscol = -1;
217     SP->_nl = TRUE;
218     SP->_raw = FALSE;
219     SP->_cbreak = 0;
220     SP->_echo = TRUE;
221     SP->_fifohead = -1;
222     SP->_endwin = TRUE;
223     SP->_ofp = output;
224     SP->_cursor = -1;		/* cannot know real cursor shape */
225 #ifdef 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 #ifdef NCURSES_EXT_FUNCS
231     SP->_default_color = FALSE;
232     SP->_has_sgr_39_49 = FALSE;
233     SP->_default_fg = COLOR_WHITE;
234     SP->_default_bg = COLOR_BLACK;
235 #ifdef USE_COLORFGBG
236     /*
237      * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
238      * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
239      * color to that value plus 8.  We'll only use the non-bold color for now -
240      * decide later if it is worth having default attributes as well.
241      */
242     if (getenv("COLORFGBG") != 0) {
243 	char *p = getenv("COLORFGBG");
244 	p = extract_fgbg(p, &(SP->_default_fg));
245 	p = extract_fgbg(p, &(SP->_default_bg));
246     }
247 #endif
248 #endif /* NCURSES_EXT_FUNCS */
249 
250     SP->_maxclick = DEFAULT_MAXCLICK;
251     SP->_mouse_event = no_mouse_event;
252     SP->_mouse_inline = no_mouse_inline;
253     SP->_mouse_parse = no_mouse_parse;
254     SP->_mouse_resume = no_mouse_resume;
255     SP->_mouse_wrap = no_mouse_wrap;
256     SP->_mouse_fd = -1;
257 
258     /* initialize the panel hooks */
259     SP->_panelHook.top_panel = (struct panel *) 0;
260     SP->_panelHook.bottom_panel = (struct panel *) 0;
261     SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
262 
263     /*
264      * If we've no magic cookie support, we suppress attributes that xmc
265      * would affect, i.e., the attributes that affect the rendition of a
266      * space.  Note that this impacts the alternate character set mapping
267      * as well.
268      */
269     if (magic_cookie_glitch > 0) {
270 
271 	SP->_xmc_triggers = termattrs() & (
272 	    A_ALTCHARSET |
273 	    A_BLINK |
274 	    A_BOLD |
275 	    A_REVERSE |
276 	    A_STANDOUT |
277 	    A_UNDERLINE
278 	    );
279 	SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~ (A_BOLD);
280 
281 	T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
282 #if USE_XMC_SUPPORT
283 	/*
284 	 * To keep this simple, suppress all of the optimization hooks
285 	 * except for clear_screen and the cursor addressing.
286 	 */
287 	clr_eol = 0;
288 	clr_eos = 0;
289 	set_attributes = 0;
290 #else
291 	magic_cookie_glitch = ABSENT_NUMERIC;
292 	acs_chars = 0;
293 #endif
294     }
295     _nc_init_acs();
296     memcpy(SP->_acs_map, acs_map, sizeof(chtype) * ACS_LEN);
297 
298     _nc_idcok = TRUE;
299     _nc_idlok = FALSE;
300 
301     _nc_windows = 0;		/* no windows yet */
302 
303     SP->oldhash = 0;
304     SP->newhash = 0;
305 
306     T(("creating newscr"));
307     if ((newscr = newwin(slines, scolumns, 0, 0)) == 0)
308 	return ERR;
309 
310     T(("creating curscr"));
311     if ((curscr = newwin(slines, scolumns, 0, 0)) == 0)
312 	return ERR;
313 
314     SP->_newscr = newscr;
315     SP->_curscr = curscr;
316 #if USE_SIZECHANGE
317     SP->_resize = resizeterm;
318 #endif
319 
320     newscr->_clear = TRUE;
321     curscr->_clear = FALSE;
322 
323     for (i = 0, rsp = rippedoff; rsp->line && (i < N_RIPS); rsp++, i++) {
324 	if (rsp->hook) {
325 	    WINDOW *w;
326 	    int count = (rsp->line < 0) ? -rsp->line : rsp->line;
327 
328 	    if (rsp->line < 0) {
329 		w = newwin(count, scolumns, SP->_lines_avail - count, 0);
330 		if (w) {
331 		    rsp->w = w;
332 		    rsp->hook(w, scolumns);
333 		    bottom_stolen += count;
334 		} else
335 		    return ERR;
336 	    } else {
337 		w = newwin(count, scolumns, 0, 0);
338 		if (w) {
339 		    rsp->w = w;
340 		    rsp->hook(w, scolumns);
341 		    SP->_topstolen += count;
342 		} else
343 		    return ERR;
344 	    }
345 	    SP->_lines_avail -= count;
346 	}
347 	rsp->line = 0;
348     }
349     /* reset the stack */
350     rsp = rippedoff;
351 
352     T(("creating stdscr"));
353     assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
354     if ((stdscr = newwin(LINES = SP->_lines_avail, scolumns, 0, 0)) == 0)
355 	return ERR;
356     SP->_stdscr = stdscr;
357 
358     def_shell_mode();
359     def_prog_mode();
360 
361     return OK;
362 }
363 
364 /* The internal implementation interprets line as the number of
365    lines to rip off from the top or bottom.
366    */
367 int
368 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
369 {
370     if (line == 0)
371 	return (OK);
372 
373     if (rsp >= rippedoff + N_RIPS)
374 	return (ERR);
375 
376     rsp->line = line;
377     rsp->hook = init;
378     rsp->w = 0;
379     rsp++;
380 
381     return (OK);
382 }
383 
384 int
385 ripoffline(int line, int (*init) (WINDOW *, int))
386 {
387     T((T_CALLED("ripoffline(%d,%p)"), line, init));
388 
389     if (line == 0)
390 	returnCode(OK);
391 
392     returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
393 }
394