]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_set_term.c
ncurses 5.5
[ncurses.git] / ncurses / base / lib_set_term.c
1 /****************************************************************************
2  * Copyright (c) 1998-2004,2005 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
47 MODULE_ID("$Id: lib_set_term.c,v 1.85 2005/01/22 17:36:01 tom Exp $")
48
49 NCURSES_EXPORT(SCREEN *)
50 set_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
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 NCURSES_EXPORT(void)
84 delscreen(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     del_curterm(sp->_term);
131
132     /*
133      * If the associated output stream has been closed, we can discard the
134      * set-buffer.  Limit the error check to EBADF, since fflush may fail
135      * for other reasons than trying to operate upon a closed stream.
136      */
137     if (sp->_ofp != 0
138         && sp->_setbuf != 0
139         && fflush(sp->_ofp) != 0
140         && errno == EBADF) {
141         free(sp->_setbuf);
142     }
143
144     free(sp);
145
146     /*
147      * If this was the current screen, reset everything that the
148      * application might try to use (except cur_term, which may have
149      * multiple references in different screens).
150      */
151     if (sp == SP) {
152         curscr = 0;
153         newscr = 0;
154         stdscr = 0;
155         COLORS = 0;
156         COLOR_PAIRS = 0;
157         _nc_set_screen(0);
158     }
159     returnVoid;
160 }
161
162 static ripoff_t rippedoff[5];
163 static ripoff_t *rsp = rippedoff;
164 #define N_RIPS SIZEOF(SP->_rippedoff)
165
166 static bool
167 no_mouse_event(SCREEN *sp GCC_UNUSED)
168 {
169     return FALSE;
170 }
171
172 static bool
173 no_mouse_inline(SCREEN *sp GCC_UNUSED)
174 {
175     return FALSE;
176 }
177
178 static bool
179 no_mouse_parse(int code GCC_UNUSED)
180 {
181     return TRUE;
182 }
183
184 static void
185 no_mouse_resume(SCREEN *sp GCC_UNUSED)
186 {
187 }
188
189 static void
190 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
191 {
192 }
193
194 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
195 static char *
196 extract_fgbg(char *src, int *result)
197 {
198     char *dst = 0;
199     long value = strtol(src, &dst, 0);
200
201     if (dst == 0) {
202         dst = src;
203     } else if (value >= 0) {
204         *result = value;
205     }
206     while (*dst != 0 && *dst != ';')
207         dst++;
208     if (*dst == ';')
209         dst++;
210     return dst;
211 }
212 #endif
213
214 NCURSES_EXPORT(int)
215 _nc_setupscreen(short slines, short const scolumns, FILE *output)
216 /* OS-independent screen initializations */
217 {
218     int bottom_stolen = 0;
219     int i;
220
221     T((T_CALLED("_nc_setupscreen(%d, %d, %p)"), slines, scolumns, output));
222     assert(SP == 0);            /* has been reset in newterm() ! */
223     if (!_nc_alloc_screen())
224         returnCode(ERR);
225
226     T(("created SP %p", SP));
227     SP->_next_screen = _nc_screen_chain;
228     _nc_screen_chain = SP;
229
230     if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
231         returnCode(ERR);
232
233 #ifdef __DJGPP__
234     T(("setting output mode to binary"));
235     fflush(output);
236     setmode(output, O_BINARY);
237 #endif
238     _nc_set_buffer(output, TRUE);
239     SP->_term = cur_term;
240     SP->_lines = slines;
241     SP->_lines_avail = slines;
242     SP->_columns = scolumns;
243     SP->_cursrow = -1;
244     SP->_curscol = -1;
245     SP->_nl = TRUE;
246     SP->_raw = FALSE;
247     SP->_cbreak = 0;
248     SP->_echo = TRUE;
249     SP->_fifohead = -1;
250     SP->_endwin = TRUE;
251     SP->_ofp = output;
252     SP->_cursor = -1;           /* cannot know real cursor shape */
253
254 #if NCURSES_NO_PADDING
255     SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
256     TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
257                                     SP->_no_padding ? " not" : ""));
258 #endif
259
260 #if NCURSES_EXT_FUNCS
261     SP->_default_color = FALSE;
262     SP->_has_sgr_39_49 = FALSE;
263
264     /*
265      * Set our assumption of the terminal's default foreground and background
266      * colors.  The curs_color man-page states that we can assume that the
267      * background is black.  The origin of this assumption appears to be
268      * terminals that displayed colored text, but no colored backgrounds, e.g.,
269      * the first colored terminals around 1980.  More recent ones with better
270      * technology can display not only colored backgrounds, but all
271      * combinations.  So a terminal might be something other than "white" on
272      * black (green/black looks monochrome too), but black on white or even
273      * on ivory.
274      *
275      * White-on-black is the simplest thing to use for monochrome.  Almost
276      * all applications that use color paint both text and background, so
277      * the distinction is moot.  But a few do not - which is why we leave this
278      * configurable (a better solution is to use assume_default_colors() for
279      * the rare applications that do require that sort of appearance, since
280      * is appears that more users expect to be able to make a white-on-black
281      * or black-on-white display under control of the application than not).
282      */
283 #ifdef USE_ASSUMED_COLOR
284     SP->_default_fg = COLOR_WHITE;
285     SP->_default_bg = COLOR_BLACK;
286 #else
287     SP->_default_fg = C_MASK;
288     SP->_default_bg = C_MASK;
289 #endif
290
291     /*
292      * Allow those assumed/default color assumptions to be overridden at
293      * runtime:
294      */
295     if (getenv("NCURSES_ASSUMED_COLORS") != 0) {
296         char *p = getenv("NCURSES_ASSUMED_COLORS");
297         int fg, bg;
298         char sep1, sep2;
299         int count = sscanf(p, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
300         if (count >= 1) {
301             SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
302             if (count >= 3) {
303                 SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
304             }
305             TR(TRACE_CHARPUT | TRACE_MOVE,
306                ("from environment assumed fg=%d, bg=%d",
307                 SP->_default_fg,
308                 SP->_default_bg));
309         }
310     }
311 #if USE_COLORFGBG
312     /*
313      * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
314      * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
315      * color to that value plus 8.  We'll only use the non-bold color for now -
316      * decide later if it is worth having default attributes as well.
317      */
318     if (getenv("COLORFGBG") != 0) {
319         char *p = getenv("COLORFGBG");
320         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
321         p = extract_fgbg(p, &(SP->_default_fg));
322         p = extract_fgbg(p, &(SP->_default_bg));
323         if (*p)                 /* assume rxvt was compiled with xpm support */
324             p = extract_fgbg(p, &(SP->_default_bg));
325         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
326                                         SP->_default_fg, SP->_default_bg));
327         if (SP->_default_fg >= max_colors) {
328             if (set_a_foreground != ABSENT_STRING
329                 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
330                 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
331             } else {
332                 SP->_default_fg %= max_colors;
333             }
334         }
335         if (SP->_default_bg >= max_colors) {
336             if (set_a_background != ABSENT_STRING
337                 && !strcmp(set_a_background, "\033[4%p1%dm")) {
338                 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
339             } else {
340                 SP->_default_bg %= max_colors;
341             }
342         }
343     }
344 #endif
345 #endif /* NCURSES_EXT_FUNCS */
346
347     SP->_maxclick = DEFAULT_MAXCLICK;
348     SP->_mouse_event = no_mouse_event;
349     SP->_mouse_inline = no_mouse_inline;
350     SP->_mouse_parse = no_mouse_parse;
351     SP->_mouse_resume = no_mouse_resume;
352     SP->_mouse_wrap = no_mouse_wrap;
353     SP->_mouse_fd = -1;
354
355     /* initialize the panel hooks */
356     SP->_panelHook.top_panel = (struct panel *) 0;
357     SP->_panelHook.bottom_panel = (struct panel *) 0;
358     SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
359
360     /*
361      * If we've no magic cookie support, we suppress attributes that xmc
362      * would affect, i.e., the attributes that affect the rendition of a
363      * space.  Note that this impacts the alternate character set mapping
364      * as well.
365      */
366     if (magic_cookie_glitch > 0) {
367
368         SP->_xmc_triggers = termattrs() & (
369                                               A_ALTCHARSET |
370                                               A_BLINK |
371                                               A_BOLD |
372                                               A_REVERSE |
373                                               A_STANDOUT |
374                                               A_UNDERLINE
375             );
376         SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
377
378         T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
379 #if USE_XMC_SUPPORT
380         /*
381          * To keep this simple, suppress all of the optimization hooks
382          * except for clear_screen and the cursor addressing.
383          */
384         clr_eol = 0;
385         clr_eos = 0;
386         set_attributes = 0;
387 #else
388         magic_cookie_glitch = ABSENT_NUMERIC;
389         acs_chars = 0;
390 #endif
391     }
392
393     /* initialize normal acs before wide, since we use mapping in the latter */
394     _nc_init_acs();
395 #if USE_WIDEC_SUPPORT
396     _nc_init_wacs();
397
398     SP->_screen_acs_fix = (_nc_unicode_locale() && _nc_locale_breaks_acs());
399     {
400         char *env = _nc_get_locale();
401         SP->_legacy_coding = ((env == 0)
402                               || !strcmp(env, "C")
403                               || !strcmp(env, "POSIX"));
404     }
405 #endif
406
407     _nc_idcok = TRUE;
408     _nc_idlok = FALSE;
409
410     _nc_windows = 0;            /* no windows yet */
411
412     SP->oldhash = 0;
413     SP->newhash = 0;
414
415     T(("creating newscr"));
416     if ((newscr = newwin(slines, scolumns, 0, 0)) == 0)
417         returnCode(ERR);
418
419     T(("creating curscr"));
420     if ((curscr = newwin(slines, scolumns, 0, 0)) == 0)
421         returnCode(ERR);
422
423     SP->_newscr = newscr;
424     SP->_curscr = curscr;
425 #if USE_SIZECHANGE
426     SP->_resize = resizeterm;
427 #endif
428
429     newscr->_clear = TRUE;
430     curscr->_clear = FALSE;
431
432     def_shell_mode();
433     def_prog_mode();
434
435     for (i = 0, rsp = rippedoff; rsp->line && (i < (int) N_RIPS); rsp++, i++) {
436         T(("ripping off line %d at %s", i, rsp->line < 0 ? "bottom" : "top"));
437         SP->_rippedoff[i] = rippedoff[i];
438         if (rsp->hook) {
439             int count = (rsp->line < 0) ? -rsp->line : rsp->line;
440
441             SP->_rippedoff[i].w = newwin(count,
442                                          scolumns,
443                                          ((rsp->line < 0)
444                                           ? SP->_lines_avail - count
445                                           : 0),
446                                          0);
447             if (SP->_rippedoff[i].w != 0)
448                 SP->_rippedoff[i].hook(SP->_rippedoff[i].w, scolumns);
449             else
450                 returnCode(ERR);
451             if (rsp->line < 0)
452                 bottom_stolen += count;
453             else
454                 SP->_topstolen += count;
455             SP->_lines_avail -= count;
456         }
457         rsp->line = 0;
458     }
459     SP->_rip_count = i;
460     /* reset the stack */
461     rsp = rippedoff;
462
463     T(("creating stdscr"));
464     assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
465     if ((stdscr = newwin(LINES = SP->_lines_avail, scolumns, 0, 0)) == 0)
466         returnCode(ERR);
467     SP->_stdscr = stdscr;
468
469     returnCode(OK);
470 }
471
472 /* The internal implementation interprets line as the number of
473    lines to rip off from the top or bottom.
474    */
475 NCURSES_EXPORT(int)
476 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
477 {
478     T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
479
480     if (line != 0) {
481
482         if (rsp >= rippedoff + N_RIPS)
483             returnCode(ERR);
484
485         rsp->line = line;
486         rsp->hook = init;
487         rsp->w = 0;
488         rsp++;
489     }
490
491     returnCode(OK);
492 }
493
494 NCURSES_EXPORT(int)
495 ripoffline(int line, int (*init) (WINDOW *, int))
496 {
497     T((T_CALLED("ripoffline(%d,%p)"), line, init));
498
499     if (line == 0)
500         returnCode(OK);
501
502     returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
503 }