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