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