xref: /openbsd-src/lib/libcurses/base/lib_set_term.c (revision 1fe33145a0a9b1310a0413297a8deb871918b27f)
1 /*	$OpenBSD: lib_set_term.c,v 1.9 2000/06/19 03:53:46 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.52 2000/05/27 23:22:36 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 static bool
154 no_mouse_inline(SCREEN * sp GCC_UNUSED)
155 {
156     return FALSE;
157 }
158 static bool
159 no_mouse_parse(int code GCC_UNUSED)
160 {
161     return TRUE;
162 }
163 static void
164 no_mouse_resume(SCREEN * sp GCC_UNUSED)
165 {
166 }
167 static void
168 no_mouse_wrap(SCREEN * sp GCC_UNUSED)
169 {
170 }
171 
172 int
173 _nc_setupscreen(short slines, short const scolumns, FILE * output)
174 /* OS-independent screen initializations */
175 {
176     int bottom_stolen = 0;
177     size_t i;
178 
179     assert(SP == 0);		/* has been reset in newterm() ! */
180     if (!_nc_alloc_screen())
181 	return ERR;
182 
183     SP->_next_screen = _nc_screen_chain;
184     _nc_screen_chain = SP;
185 
186     _nc_set_buffer(output, TRUE);
187     SP->_term = cur_term;
188     SP->_lines = slines;
189     SP->_lines_avail = slines;
190     SP->_columns = scolumns;
191     SP->_cursrow = -1;
192     SP->_curscol = -1;
193     SP->_nl = TRUE;
194     SP->_raw = FALSE;
195     SP->_cbreak = 0;
196     SP->_echo = TRUE;
197     SP->_fifohead = -1;
198     SP->_endwin = TRUE;
199     SP->_ofp = output;
200     SP->_cursor = -1;		/* cannot know real cursor shape */
201 #ifdef NCURSES_NO_PADDING
202     SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
203 #endif
204 #ifdef NCURSES_EXT_FUNCS
205     SP->_default_color = FALSE;
206     SP->_has_sgr_39_49 = FALSE;
207     SP->_default_fg = COLOR_WHITE;
208     SP->_default_bg = COLOR_BLACK;
209 #endif
210 
211     SP->_maxclick = DEFAULT_MAXCLICK;
212     SP->_mouse_event = no_mouse_event;
213     SP->_mouse_inline = no_mouse_inline;
214     SP->_mouse_parse = no_mouse_parse;
215     SP->_mouse_resume = no_mouse_resume;
216     SP->_mouse_wrap = no_mouse_wrap;
217     SP->_mouse_fd = -1;
218 
219     /* initialize the panel hooks */
220     SP->_panelHook.top_panel = (struct panel *) 0;
221     SP->_panelHook.bottom_panel = (struct panel *) 0;
222     SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
223 
224     /*
225      * If we've no magic cookie support, we suppress attributes that xmc
226      * would affect, i.e., the attributes that affect the rendition of a
227      * space.  Note that this impacts the alternate character set mapping
228      * as well.
229      */
230     if (magic_cookie_glitch > 0) {
231 
232 	SP->_xmc_triggers = termattrs() & (
233 	    A_ALTCHARSET |
234 	    A_BLINK |
235 	    A_BOLD |
236 	    A_REVERSE |
237 	    A_STANDOUT |
238 	    A_UNDERLINE
239 	    );
240 	SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~ (A_BOLD);
241 
242 	T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
243 #if USE_XMC_SUPPORT
244 	/*
245 	 * To keep this simple, suppress all of the optimization hooks
246 	 * except for clear_screen and the cursor addressing.
247 	 */
248 	clr_eol = 0;
249 	clr_eos = 0;
250 	set_attributes = 0;
251 #else
252 	magic_cookie_glitch = ABSENT_NUMERIC;
253 	acs_chars = 0;
254 #endif
255     }
256     _nc_init_acs();
257     memcpy(SP->_acs_map, acs_map, sizeof(chtype) * ACS_LEN);
258 
259     _nc_idcok = TRUE;
260     _nc_idlok = FALSE;
261 
262     _nc_windows = 0;		/* no windows yet */
263 
264     SP->oldhash = 0;
265     SP->newhash = 0;
266 
267     T(("creating newscr"));
268     if ((newscr = newwin(slines, scolumns, 0, 0)) == 0)
269 	return ERR;
270 
271     T(("creating curscr"));
272     if ((curscr = newwin(slines, scolumns, 0, 0)) == 0)
273 	return ERR;
274 
275     SP->_newscr = newscr;
276     SP->_curscr = curscr;
277 #if USE_SIZECHANGE
278     SP->_resize = resizeterm;
279 #endif
280 
281     newscr->_clear = TRUE;
282     curscr->_clear = FALSE;
283 
284     for (i = 0, rsp = rippedoff; rsp->line && (i < N_RIPS); rsp++, i++) {
285 	if (rsp->hook) {
286 	    WINDOW *w;
287 	    int count = (rsp->line < 0) ? -rsp->line : rsp->line;
288 
289 	    if (rsp->line < 0) {
290 		w = newwin(count, scolumns, SP->_lines_avail - count, 0);
291 		if (w) {
292 		    rsp->w = w;
293 		    rsp->hook(w, scolumns);
294 		    bottom_stolen += count;
295 		} else
296 		    return ERR;
297 	    } else {
298 		w = newwin(count, scolumns, 0, 0);
299 		if (w) {
300 		    rsp->w = w;
301 		    rsp->hook(w, scolumns);
302 		    SP->_topstolen += count;
303 		} else
304 		    return ERR;
305 	    }
306 	    SP->_lines_avail -= count;
307 	}
308 	rsp->line = 0;
309     }
310     /* reset the stack */
311     rsp = rippedoff;
312 
313     T(("creating stdscr"));
314     assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
315     if ((stdscr = newwin(LINES = SP->_lines_avail, scolumns, 0, 0)) == 0)
316 	return ERR;
317     SP->_stdscr = stdscr;
318 
319     def_shell_mode();
320     def_prog_mode();
321 
322     return OK;
323 }
324 
325 /* The internal implementation interprets line as the number of
326    lines to rip off from the top or bottom.
327    */
328 int
329 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
330 {
331     if (line == 0)
332 	return (OK);
333 
334     if (rsp >= rippedoff + N_RIPS)
335 	return (ERR);
336 
337     rsp->line = line;
338     rsp->hook = init;
339     rsp->w = 0;
340     rsp++;
341 
342     return (OK);
343 }
344 
345 int
346 ripoffline(int line, int (*init) (WINDOW *, int))
347 {
348     T((T_CALLED("ripoffline(%d,%p)"), line, init));
349 
350     if (line == 0)
351 	returnCode(OK);
352 
353     returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
354 }
355