]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_set_term.c
ncurses 5.6 - patch 20070721
[ncurses.git] / ncurses / base / lib_set_term.c
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 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.98 2007/05/12 19:37:04 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 #if !USE_REENTRANT
61     curscr = SP->_curscr;
62     newscr = SP->_newscr;
63     stdscr = SP->_stdscr;
64     COLORS = SP->_color_count;
65     COLOR_PAIRS = SP->_pair_count;
66 #endif
67
68     T((T_RETURN("%p"), oldSP));
69     return (oldSP);
70 }
71
72 static void
73 _nc_free_keytry(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 NCURSES_EXPORT(void)
86 delscreen(SCREEN *sp)
87 {
88     SCREEN **scan = &_nc_screen_chain;
89     int i;
90
91     T((T_CALLED("delscreen(%p)"), sp));
92
93     while (*scan) {
94         if (*scan == sp) {
95             *scan = sp->_next_screen;
96             break;
97         }
98         scan = &(*scan)->_next_screen;
99     }
100
101     (void) _nc_freewin(sp->_curscr);
102     (void) _nc_freewin(sp->_newscr);
103     (void) _nc_freewin(sp->_stdscr);
104
105     if (sp->_slk != 0) {
106         if (sp->_slk->ent != 0) {
107             for (i = 0; i < sp->_slk->labcnt; ++i) {
108                 FreeIfNeeded(sp->_slk->ent[i].ent_text);
109                 FreeIfNeeded(sp->_slk->ent[i].form_text);
110             }
111             free(sp->_slk->ent);
112         }
113         free(sp->_slk);
114         sp->_slk = 0;
115     }
116
117     _nc_free_keytry(sp->_keytry);
118     sp->_keytry = 0;
119
120     _nc_free_keytry(sp->_key_ok);
121     sp->_key_ok = 0;
122
123     FreeIfNeeded(sp->_current_attr);
124
125     FreeIfNeeded(sp->_color_table);
126     FreeIfNeeded(sp->_color_pairs);
127
128     FreeIfNeeded(sp->oldhash);
129     FreeIfNeeded(sp->newhash);
130     FreeIfNeeded(sp->hashtab);
131
132     FreeIfNeeded(sp->_acs_map);
133     FreeIfNeeded(sp->_screen_acs_map);
134
135     del_curterm(sp->_term);
136
137     /*
138      * If the associated output stream has been closed, we can discard the
139      * set-buffer.  Limit the error check to EBADF, since fflush may fail
140      * for other reasons than trying to operate upon a closed stream.
141      */
142     if (sp->_ofp != 0
143         && sp->_setbuf != 0
144         && fflush(sp->_ofp) != 0
145         && errno == EBADF) {
146         free(sp->_setbuf);
147     }
148
149     free(sp);
150
151     /*
152      * If this was the current screen, reset everything that the
153      * application might try to use (except cur_term, which may have
154      * multiple references in different screens).
155      */
156     if (sp == SP) {
157 #if !USE_REENTRANT
158         curscr = 0;
159         newscr = 0;
160         stdscr = 0;
161         COLORS = 0;
162         COLOR_PAIRS = 0;
163 #endif
164         _nc_set_screen(0);
165     }
166     returnVoid;
167 }
168
169 static bool
170 no_mouse_event(SCREEN *sp GCC_UNUSED)
171 {
172     return FALSE;
173 }
174
175 static bool
176 no_mouse_inline(SCREEN *sp GCC_UNUSED)
177 {
178     return FALSE;
179 }
180
181 static bool
182 no_mouse_parse(int code GCC_UNUSED)
183 {
184     return TRUE;
185 }
186
187 static void
188 no_mouse_resume(SCREEN *sp GCC_UNUSED)
189 {
190 }
191
192 static void
193 no_mouse_wrap(SCREEN *sp GCC_UNUSED)
194 {
195 }
196
197 #if NCURSES_EXT_FUNCS && USE_COLORFGBG
198 static char *
199 extract_fgbg(char *src, int *result)
200 {
201     char *dst = 0;
202     long value = strtol(src, &dst, 0);
203
204     if (dst == 0) {
205         dst = src;
206     } else if (value >= 0) {
207         *result = value;
208     }
209     while (*dst != 0 && *dst != ';')
210         dst++;
211     if (*dst == ';')
212         dst++;
213     return dst;
214 }
215 #endif
216
217 #define ripoff_sp       _nc_prescreen.rsp
218 #define ripoff_stack    _nc_prescreen.rippedoff
219
220 /* OS-independent screen initializations */
221 NCURSES_EXPORT(int)
222 _nc_setupscreen(int slines GCC_UNUSED,
223                 int scolumns GCC_UNUSED,
224                 FILE *output,
225                 bool filtered,
226                 int slk_format)
227 {
228     int bottom_stolen = 0;
229     bool support_cookies = USE_XMC_SUPPORT;
230     ripoff_t *rop;
231
232     T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
233        slines, scolumns, output, filtered, slk_format));
234
235     assert(SP == 0);            /* has been reset in newterm() ! */
236     if (!_nc_alloc_screen()
237         || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
238         || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
239         returnCode(ERR);
240     }
241
242     T(("created SP %p", SP));
243     SP->_next_screen = _nc_screen_chain;
244     _nc_screen_chain = SP;
245
246     if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
247         returnCode(ERR);
248
249     /*
250      * We should always check the screensize, just in case.
251      */
252     _nc_get_screensize(&slines, &scolumns);
253     SET_LINES(slines);
254     SET_COLS(scolumns);
255     T((T_CREATE("screen %s %dx%d"), termname(), LINES, COLS));
256
257     SP->_filtered = filtered;
258
259     /* implement filter mode */
260     if (filtered) {
261         slines = 1;
262         SET_LINES(slines);
263         clear_screen = 0;
264         cursor_down = parm_down_cursor = 0;
265         cursor_address = 0;
266         cursor_up = parm_up_cursor = 0;
267         row_address = 0;
268
269         cursor_home = carriage_return;
270         T(("filter screensize %dx%d", LINES, COLS));
271     }
272 #ifdef __DJGPP__
273     T(("setting output mode to binary"));
274     fflush(output);
275     setmode(output, O_BINARY);
276 #endif
277     _nc_set_buffer(output, TRUE);
278     SP->_term = cur_term;
279     SP->_lines = slines;
280     SP->_lines_avail = slines;
281     SP->_columns = scolumns;
282     SP->_cursrow = -1;
283     SP->_curscol = -1;
284     SP->_nl = TRUE;
285     SP->_raw = FALSE;
286     SP->_cbreak = 0;
287     SP->_echo = TRUE;
288     SP->_fifohead = -1;
289     SP->_endwin = TRUE;
290     SP->_ofp = output;
291     SP->_cursor = -1;           /* cannot know real cursor shape */
292
293 #if NCURSES_NO_PADDING
294     SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
295     TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
296                                     SP->_no_padding ? " not" : ""));
297 #endif
298
299 #if NCURSES_EXT_FUNCS
300     SP->_default_color = FALSE;
301     SP->_has_sgr_39_49 = FALSE;
302
303     /*
304      * Set our assumption of the terminal's default foreground and background
305      * colors.  The curs_color man-page states that we can assume that the
306      * background is black.  The origin of this assumption appears to be
307      * terminals that displayed colored text, but no colored backgrounds, e.g.,
308      * the first colored terminals around 1980.  More recent ones with better
309      * technology can display not only colored backgrounds, but all
310      * combinations.  So a terminal might be something other than "white" on
311      * black (green/black looks monochrome too), but black on white or even
312      * on ivory.
313      *
314      * White-on-black is the simplest thing to use for monochrome.  Almost
315      * all applications that use color paint both text and background, so
316      * the distinction is moot.  But a few do not - which is why we leave this
317      * configurable (a better solution is to use assume_default_colors() for
318      * the rare applications that do require that sort of appearance, since
319      * is appears that more users expect to be able to make a white-on-black
320      * or black-on-white display under control of the application than not).
321      */
322 #ifdef USE_ASSUMED_COLOR
323     SP->_default_fg = COLOR_WHITE;
324     SP->_default_bg = COLOR_BLACK;
325 #else
326     SP->_default_fg = C_MASK;
327     SP->_default_bg = C_MASK;
328 #endif
329
330     /*
331      * Allow those assumed/default color assumptions to be overridden at
332      * runtime:
333      */
334     if (getenv("NCURSES_ASSUMED_COLORS") != 0) {
335         char *p = getenv("NCURSES_ASSUMED_COLORS");
336         int fg, bg;
337         char sep1, sep2;
338         int count = sscanf(p, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
339         if (count >= 1) {
340             SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
341             if (count >= 3) {
342                 SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
343             }
344             TR(TRACE_CHARPUT | TRACE_MOVE,
345                ("from environment assumed fg=%d, bg=%d",
346                 SP->_default_fg,
347                 SP->_default_bg));
348         }
349     }
350 #if USE_COLORFGBG
351     /*
352      * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
353      * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
354      * color to that value plus 8.  We'll only use the non-bold color for now -
355      * decide later if it is worth having default attributes as well.
356      */
357     if (getenv("COLORFGBG") != 0) {
358         char *p = getenv("COLORFGBG");
359         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
360         p = extract_fgbg(p, &(SP->_default_fg));
361         p = extract_fgbg(p, &(SP->_default_bg));
362         if (*p)                 /* assume rxvt was compiled with xpm support */
363             p = extract_fgbg(p, &(SP->_default_bg));
364         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
365                                         SP->_default_fg, SP->_default_bg));
366         if (SP->_default_fg >= max_colors) {
367             if (set_a_foreground != ABSENT_STRING
368                 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
369                 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
370             } else {
371                 SP->_default_fg %= max_colors;
372             }
373         }
374         if (SP->_default_bg >= max_colors) {
375             if (set_a_background != ABSENT_STRING
376                 && !strcmp(set_a_background, "\033[4%p1%dm")) {
377                 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
378             } else {
379                 SP->_default_bg %= max_colors;
380             }
381         }
382     }
383 #endif
384 #endif /* NCURSES_EXT_FUNCS */
385
386     SP->_maxclick = DEFAULT_MAXCLICK;
387     SP->_mouse_event = no_mouse_event;
388     SP->_mouse_inline = no_mouse_inline;
389     SP->_mouse_parse = no_mouse_parse;
390     SP->_mouse_resume = no_mouse_resume;
391     SP->_mouse_wrap = no_mouse_wrap;
392     SP->_mouse_fd = -1;
393
394     /* initialize the panel hooks */
395     SP->_panelHook.top_panel = (struct panel *) 0;
396     SP->_panelHook.bottom_panel = (struct panel *) 0;
397     SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
398
399     /*
400      * If we've no magic cookie support, we suppress attributes that xmc would
401      * affect, i.e., the attributes that affect the rendition of a space.
402      */
403     SP->_ok_attributes = termattrs();
404     if (has_colors()) {
405         SP->_ok_attributes |= A_COLOR;
406     }
407 #if USE_XMC_SUPPORT
408     /*
409      * If we have no magic-cookie support compiled-in, or if it is suppressed
410      * in the environment, reset the support-flag.
411      */
412     if (magic_cookie_glitch >= 0) {
413         if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
414             support_cookies = FALSE;
415         }
416     }
417 #endif
418
419     if (!support_cookies && magic_cookie_glitch >= 0) {
420         T(("will disable attributes to work w/o magic cookies"));
421     }
422
423     if (magic_cookie_glitch > 0) {      /* tvi, wyse */
424
425         SP->_xmc_triggers = SP->_ok_attributes & (
426                                                      A_STANDOUT |
427                                                      A_UNDERLINE |
428                                                      A_REVERSE |
429                                                      A_BLINK |
430                                                      A_DIM |
431                                                      A_BOLD |
432                                                      A_INVIS |
433                                                      A_PROTECT
434             );
435 #if 0
436         /*
437          * We "should" treat colors as an attribute.  The wyse350 (and its
438          * clones) appear to be the only ones that have both colors and magic
439          * cookies.
440          */
441         if (has_colors()) {
442             SP->_xmc_triggers |= A_COLOR;
443         }
444 #endif
445         SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
446
447         T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
448         /*
449          * Supporting line-drawing may be possible.  But make the regular
450          * video attributes work first.
451          */
452         acs_chars = ABSENT_STRING;
453         ena_acs = ABSENT_STRING;
454         enter_alt_charset_mode = ABSENT_STRING;
455         exit_alt_charset_mode = ABSENT_STRING;
456 #if USE_XMC_SUPPORT
457         /*
458          * To keep the cookie support simple, suppress all of the optimization
459          * hooks except for clear_screen and the cursor addressing.
460          */
461         if (support_cookies) {
462             clr_eol = ABSENT_STRING;
463             clr_eos = ABSENT_STRING;
464             set_attributes = ABSENT_STRING;
465         }
466 #endif
467     } else if (magic_cookie_glitch == 0) {      /* hpterm */
468     }
469
470     /*
471      * If magic cookies are not supported, cancel the strings that set
472      * video attributes.
473      */
474     if (!support_cookies && magic_cookie_glitch >= 0) {
475         magic_cookie_glitch = ABSENT_NUMERIC;
476         set_attributes = ABSENT_STRING;
477         enter_blink_mode = ABSENT_STRING;
478         enter_bold_mode = ABSENT_STRING;
479         enter_dim_mode = ABSENT_STRING;
480         enter_reverse_mode = ABSENT_STRING;
481         enter_standout_mode = ABSENT_STRING;
482         enter_underline_mode = ABSENT_STRING;
483     }
484
485     /* initialize normal acs before wide, since we use mapping in the latter */
486 #if !USE_WIDEC_SUPPORT
487     if (_nc_unicode_locale() && _nc_locale_breaks_acs()) {
488         acs_chars = NULL;
489         ena_acs = NULL;
490         enter_alt_charset_mode = NULL;
491         exit_alt_charset_mode = NULL;
492         set_attributes = NULL;
493     }
494 #endif
495     _nc_init_acs();
496 #if USE_WIDEC_SUPPORT
497     _nc_init_wacs();
498
499     SP->_screen_acs_fix = (_nc_unicode_locale() && _nc_locale_breaks_acs());
500     {
501         char *env = _nc_get_locale();
502         SP->_legacy_coding = ((env == 0)
503                               || !strcmp(env, "C")
504                               || !strcmp(env, "POSIX"));
505     }
506 #endif
507
508     _nc_idcok = TRUE;
509     _nc_idlok = FALSE;
510
511     _nc_windows = 0;            /* no windows yet */
512
513     SP->oldhash = 0;
514     SP->newhash = 0;
515
516     T(("creating newscr"));
517     if ((SP->_newscr = newwin(slines, scolumns, 0, 0)) == 0)
518         returnCode(ERR);
519
520     T(("creating curscr"));
521     if ((SP->_curscr = newwin(slines, scolumns, 0, 0)) == 0)
522         returnCode(ERR);
523
524 #if !USE_REENTRANT
525     newscr = SP->_newscr;
526     curscr = SP->_curscr;
527 #endif
528 #if USE_SIZECHANGE
529     SP->_resize = resizeterm;
530 #endif
531
532     newscr->_clear = TRUE;
533     curscr->_clear = FALSE;
534
535     def_shell_mode();
536     def_prog_mode();
537
538     for (rop = ripoff_stack;
539          rop != ripoff_sp && (rop - ripoff_stack) < N_RIPS;
540          rop++) {
541
542         /* If we must simulate soft labels, grab off the line to be used.
543            We assume that we must simulate, if it is none of the standard
544            formats (4-4 or 3-2-3) for which there may be some hardware
545            support. */
546         if (rop->hook == _nc_slk_initialize)
547             if (!(num_labels <= 0 || !SLK_STDFMT(slk_format)))
548                 continue;
549         if (rop->hook) {
550             int count;
551             WINDOW *w;
552
553             count = (rop->line < 0) ? -rop->line : rop->line;
554             T(("ripping off %i lines at %s", count,
555                ((rop->line < 0)
556                 ? "bottom"
557                 : "top")));
558
559             w = newwin(count, scolumns,
560                        ((rop->line < 0)
561                         ? SP->_lines_avail - count
562                         : 0),
563                        0);
564             if (w)
565                 rop->hook(w, scolumns);
566             else
567                 returnCode(ERR);
568             if (rop->line < 0)
569                 bottom_stolen += count;
570             else
571                 SP->_topstolen += count;
572             SP->_lines_avail -= count;
573         }
574     }
575     /* reset the stack */
576     ripoff_sp = ripoff_stack;
577
578     T(("creating stdscr"));
579     assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
580     if ((SP->_stdscr = newwin(SP->_lines_avail, scolumns, 0, 0)) == 0)
581         returnCode(ERR);
582
583     SET_LINES(SP->_lines_avail);
584 #if !USE_REENTRANT
585     stdscr = SP->_stdscr;
586 #endif
587
588     returnCode(OK);
589 }
590
591 /*
592  * The internal implementation interprets line as the number of lines to rip
593  * off from the top or bottom.
594  */
595 NCURSES_EXPORT(int)
596 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
597 {
598     T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
599
600     if (line != 0) {
601
602         if (ripoff_sp == 0)
603             ripoff_sp = ripoff_stack;
604         if (ripoff_sp >= ripoff_stack + N_RIPS)
605             returnCode(ERR);
606
607         ripoff_sp->line = line;
608         ripoff_sp->hook = init;
609         ripoff_sp++;
610     }
611
612     returnCode(OK);
613 }
614
615 NCURSES_EXPORT(int)
616 ripoffline(int line, int (*init) (WINDOW *, int))
617 {
618     T((T_CALLED("ripoffline(%d,%p)"), line, init));
619
620     if (line == 0)
621         returnCode(OK);
622
623     returnCode(_nc_ripoffline((line < 0) ? -1 : 1, init));
624 }