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