]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_set_term.c
ncurses 5.1
[ncurses.git] / ncurses / base / lib_set_term.c
1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000 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  ****************************************************************************/
33
34 /*
35 **      lib_set_term.c
36 **
37 **      The routine set_term().
38 **
39 */
40
41 #include <curses.priv.h>
42
43 #include <term.h>               /* cur_term */
44 #include <tic.h>
45
46 MODULE_ID("$Id: lib_set_term.c,v 1.55 2000/07/02 00:22:18 tom Exp $")
47
48 SCREEN *
49 set_term(SCREEN * screenp)
50 {
51     SCREEN *oldSP;
52
53     T((T_CALLED("set_term(%p)"), screenp));
54
55     oldSP = SP;
56     _nc_set_screen(screenp);
57
58     set_curterm(SP->_term);
59     curscr = SP->_curscr;
60     newscr = SP->_newscr;
61     stdscr = SP->_stdscr;
62     COLORS = SP->_color_count;
63     COLOR_PAIRS = SP->_pair_count;
64     memcpy(acs_map, SP->_acs_map, sizeof(chtype) * ACS_LEN);
65
66     T((T_RETURN("%p"), oldSP));
67     return (oldSP);
68 }
69
70 static 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  */
83 void
84 delscreen(SCREEN * sp)
85 {
86     SCREEN **scan = &_nc_screen_chain;
87
88     T((T_CALLED("delscreen(%p)"), sp));
89
90     while (*scan) {
91         if (*scan == sp) {
92             *scan = sp->_next_screen;
93             break;
94         }
95         scan = &(*scan)->_next_screen;
96     }
97
98     _nc_freewin(sp->_curscr);
99     _nc_freewin(sp->_newscr);
100     _nc_freewin(sp->_stdscr);
101     _nc_free_keytry(sp->_keytry);
102     _nc_free_keytry(sp->_key_ok);
103
104     FreeIfNeeded(sp->_color_table);
105     FreeIfNeeded(sp->_color_pairs);
106
107     FreeIfNeeded(sp->oldhash);
108     FreeIfNeeded(sp->newhash);
109
110     del_curterm(sp->_term);
111
112     /*
113      * If the associated output stream has been closed, we can discard the
114      * set-buffer.  Limit the error check to EBADF, since fflush may fail
115      * for other reasons than trying to operate upon a closed stream.
116      */
117     if (sp->_ofp != 0
118         && sp->_setbuf != 0
119         && fflush(sp->_ofp) != 0
120         && errno == EBADF) {
121         free(sp->_setbuf);
122     }
123
124     free(sp);
125
126     /*
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
142 static ripoff_t rippedoff[5];
143 static ripoff_t *rsp = rippedoff;
144 #define N_RIPS SIZEOF(rippedoff)
145
146 static bool
147 no_mouse_event(SCREEN * sp GCC_UNUSED)
148 {
149     return FALSE;
150 }
151
152 static bool
153 no_mouse_inline(SCREEN * sp GCC_UNUSED)
154 {
155     return FALSE;
156 }
157
158 static bool
159 no_mouse_parse(int code GCC_UNUSED)
160 {
161     return TRUE;
162 }
163
164 static void
165 no_mouse_resume(SCREEN * sp GCC_UNUSED)
166 {
167 }
168
169 static void
170 no_mouse_wrap(SCREEN * sp GCC_UNUSED)
171 {
172 }
173
174 #if defined(NCURSES_EXT_FUNCS) && defined(USE_COLORFGBG)
175 static char *
176 extract_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
194 int
195 _nc_setupscreen(short slines, short const scolumns, FILE * output)
196 /* OS-independent screen initializations */
197 {
198     int bottom_stolen = 0;
199     size_t i;
200
201     assert(SP == 0);            /* has been reset in newterm() ! */
202     if (!_nc_alloc_screen())
203         return ERR;
204
205     SP->_next_screen = _nc_screen_chain;
206     _nc_screen_chain = SP;
207
208     _nc_set_buffer(output, TRUE);
209     SP->_term = cur_term;
210     SP->_lines = slines;
211     SP->_lines_avail = slines;
212     SP->_columns = scolumns;
213     SP->_cursrow = -1;
214     SP->_curscol = -1;
215     SP->_nl = TRUE;
216     SP->_raw = FALSE;
217     SP->_cbreak = 0;
218     SP->_echo = TRUE;
219     SP->_fifohead = -1;
220     SP->_endwin = TRUE;
221     SP->_ofp = output;
222     SP->_cursor = -1;           /* cannot know real cursor shape */
223 #ifdef NCURSES_NO_PADDING
224     SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
225     TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
226             SP->_no_padding ? " not" : ""));
227 #endif
228 #ifdef NCURSES_EXT_FUNCS
229     SP->_default_color = FALSE;
230     SP->_has_sgr_39_49 = FALSE;
231     SP->_default_fg = COLOR_WHITE;
232     SP->_default_bg = COLOR_BLACK;
233 #ifdef USE_COLORFGBG
234     /*
235      * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
236      * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
237      * color to that value plus 8.  We'll only use the non-bold color for now -
238      * decide later if it is worth having default attributes as well.
239      */
240     if (getenv("COLORFGBG") != 0) {
241         char *p = getenv("COLORFGBG");
242         p = extract_fgbg(p, &(SP->_default_fg));
243         p = extract_fgbg(p, &(SP->_default_bg));
244     }
245 #endif
246 #endif /* NCURSES_EXT_FUNCS */
247
248     SP->_maxclick = DEFAULT_MAXCLICK;
249     SP->_mouse_event = no_mouse_event;
250     SP->_mouse_inline = no_mouse_inline;
251     SP->_mouse_parse = no_mouse_parse;
252     SP->_mouse_resume = no_mouse_resume;
253     SP->_mouse_wrap = no_mouse_wrap;
254     SP->_mouse_fd = -1;
255
256     /* initialize the panel hooks */
257     SP->_panelHook.top_panel = (struct panel *) 0;
258     SP->_panelHook.bottom_panel = (struct panel *) 0;
259     SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
260
261     /*
262      * If we've no magic cookie support, we suppress attributes that xmc
263      * would affect, i.e., the attributes that affect the rendition of a
264      * space.  Note that this impacts the alternate character set mapping
265      * as well.
266      */
267     if (magic_cookie_glitch > 0) {
268
269         SP->_xmc_triggers = termattrs() & (
270             A_ALTCHARSET |
271             A_BLINK |
272             A_BOLD |
273             A_REVERSE |
274             A_STANDOUT |
275             A_UNDERLINE
276             );
277         SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~ (A_BOLD);
278
279         T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
280 #if USE_XMC_SUPPORT
281         /*
282          * To keep this simple, suppress all of the optimization hooks
283          * except for clear_screen and the cursor addressing.
284          */
285         clr_eol = 0;
286         clr_eos = 0;
287         set_attributes = 0;
288 #else
289         magic_cookie_glitch = ABSENT_NUMERIC;
290         acs_chars = 0;
291 #endif
292     }
293     _nc_init_acs();
294     memcpy(SP->_acs_map, acs_map, sizeof(chtype) * ACS_LEN);
295
296     _nc_idcok = TRUE;
297     _nc_idlok = FALSE;
298
299     _nc_windows = 0;            /* no windows yet */
300
301     SP->oldhash = 0;
302     SP->newhash = 0;
303
304     T(("creating newscr"));
305     if ((newscr = newwin(slines, scolumns, 0, 0)) == 0)
306         return ERR;
307
308     T(("creating curscr"));
309     if ((curscr = newwin(slines, scolumns, 0, 0)) == 0)
310         return ERR;
311
312     SP->_newscr = newscr;
313     SP->_curscr = curscr;
314 #if USE_SIZECHANGE
315     SP->_resize = resizeterm;
316 #endif
317
318     newscr->_clear = TRUE;
319     curscr->_clear = FALSE;
320
321     for (i = 0, rsp = rippedoff; rsp->line && (i < N_RIPS); rsp++, i++) {
322         if (rsp->hook) {
323             WINDOW *w;
324             int count = (rsp->line < 0) ? -rsp->line : rsp->line;
325
326             if (rsp->line < 0) {
327                 w = newwin(count, scolumns, SP->_lines_avail - count, 0);
328                 if (w) {
329                     rsp->w = w;
330                     rsp->hook(w, scolumns);
331                     bottom_stolen += count;
332                 } else
333                     return ERR;
334             } else {
335                 w = newwin(count, scolumns, 0, 0);
336                 if (w) {
337                     rsp->w = w;
338                     rsp->hook(w, scolumns);
339                     SP->_topstolen += count;
340                 } else
341                     return ERR;
342             }
343             SP->_lines_avail -= count;
344         }
345         rsp->line = 0;
346     }
347     /* reset the stack */
348     rsp = rippedoff;
349
350     T(("creating stdscr"));
351     assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
352     if ((stdscr = newwin(LINES = SP->_lines_avail, scolumns, 0, 0)) == 0)
353         return ERR;
354     SP->_stdscr = stdscr;
355
356     def_shell_mode();
357     def_prog_mode();
358
359     return OK;
360 }
361
362 /* The internal implementation interprets line as the number of
363    lines to rip off from the top or bottom.
364    */
365 int
366 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
367 {
368     if (line == 0)
369         return (OK);
370
371     if (rsp >= rippedoff + N_RIPS)
372         return (ERR);
373
374     rsp->line = line;
375     rsp->hook = init;
376     rsp->w = 0;
377     rsp++;
378
379     return (OK);
380 }
381
382 int
383 ripoffline(int line, int (*init) (WINDOW *, int))
384 {
385     T((T_CALLED("ripoffline(%d,%p)"), line, init));
386
387     if (line == 0)
388         returnCode(OK);
389
390     returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
391 }