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