]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_set_term.c
ncurses 5.7 - patch 20090829
[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.124 2009/06/27 21:36:14 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 #if NCURSES_SP_FUNCS
284     sp = SP;                    /* fixup so SET_LINES and SET_COLS works */
285 #endif
286     SP->_next_screen = _nc_screen_chain;
287     _nc_screen_chain = SP;
288
289     if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
290         returnCode(ERR);
291
292     /*
293      * We should always check the screensize, just in case.
294      */
295     _nc_get_screensize(SP, &slines, &scolumns);
296     SET_LINES(slines);
297     SET_COLS(scolumns);
298     T((T_CREATE("screen %s %dx%d"), termname(), LINES, COLS));
299
300     SP->_filtered = filtered;
301
302     /* implement filter mode */
303     if (filtered) {
304         slines = 1;
305         SET_LINES(slines);
306         clear_screen = 0;
307         cursor_down = parm_down_cursor = 0;
308         cursor_address = 0;
309         cursor_up = parm_up_cursor = 0;
310         row_address = 0;
311
312         cursor_home = carriage_return;
313         T(("filter screensize %dx%d", LINES, COLS));
314     }
315 #ifdef __DJGPP__
316     T(("setting output mode to binary"));
317     fflush(output);
318     setmode(output, O_BINARY);
319 #endif
320     _nc_set_buffer(output, TRUE);
321     SP->_term = cur_term;
322     SP->_lines = slines;
323     SP->_lines_avail = slines;
324     SP->_columns = scolumns;
325     SP->_cursrow = -1;
326     SP->_curscol = -1;
327     SP->_nl = TRUE;
328     SP->_raw = FALSE;
329     SP->_cbreak = 0;
330     SP->_echo = TRUE;
331     SP->_fifohead = -1;
332     SP->_endwin = TRUE;
333     SP->_ofp = output;
334     SP->_cursor = -1;           /* cannot know real cursor shape */
335
336     SetNoPadding(SP);
337
338 #if NCURSES_EXT_FUNCS
339     SP->_default_color = FALSE;
340     SP->_has_sgr_39_49 = FALSE;
341
342     /*
343      * Set our assumption of the terminal's default foreground and background
344      * colors.  The curs_color man-page states that we can assume that the
345      * background is black.  The origin of this assumption appears to be
346      * terminals that displayed colored text, but no colored backgrounds, e.g.,
347      * the first colored terminals around 1980.  More recent ones with better
348      * technology can display not only colored backgrounds, but all
349      * combinations.  So a terminal might be something other than "white" on
350      * black (green/black looks monochrome too), but black on white or even
351      * on ivory.
352      *
353      * White-on-black is the simplest thing to use for monochrome.  Almost
354      * all applications that use color paint both text and background, so
355      * the distinction is moot.  But a few do not - which is why we leave this
356      * configurable (a better solution is to use assume_default_colors() for
357      * the rare applications that do require that sort of appearance, since
358      * is appears that more users expect to be able to make a white-on-black
359      * or black-on-white display under control of the application than not).
360      */
361 #ifdef USE_ASSUMED_COLOR
362     SP->_default_fg = COLOR_WHITE;
363     SP->_default_bg = COLOR_BLACK;
364 #else
365     SP->_default_fg = C_MASK;
366     SP->_default_bg = C_MASK;
367 #endif
368
369     /*
370      * Allow those assumed/default color assumptions to be overridden at
371      * runtime:
372      */
373     if ((env = getenv("NCURSES_ASSUMED_COLORS")) != 0) {
374         int fg, bg;
375         char sep1, sep2;
376         int count = sscanf(env, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
377         if (count >= 1) {
378             SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
379             if (count >= 3) {
380                 SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
381             }
382             TR(TRACE_CHARPUT | TRACE_MOVE,
383                ("from environment assumed fg=%d, bg=%d",
384                 SP->_default_fg,
385                 SP->_default_bg));
386         }
387     }
388 #if USE_COLORFGBG
389     /*
390      * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
391      * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
392      * color to that value plus 8.  We'll only use the non-bold color for now -
393      * decide later if it is worth having default attributes as well.
394      */
395     if (getenv("COLORFGBG") != 0) {
396         char *p = getenv("COLORFGBG");
397         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
398         p = extract_fgbg(p, &(SP->_default_fg));
399         p = extract_fgbg(p, &(SP->_default_bg));
400         if (*p)                 /* assume rxvt was compiled with xpm support */
401             p = extract_fgbg(p, &(SP->_default_bg));
402         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
403                                         SP->_default_fg, SP->_default_bg));
404         if (SP->_default_fg >= max_colors) {
405             if (set_a_foreground != ABSENT_STRING
406                 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
407                 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
408             } else {
409                 SP->_default_fg %= max_colors;
410             }
411         }
412         if (SP->_default_bg >= max_colors) {
413             if (set_a_background != ABSENT_STRING
414                 && !strcmp(set_a_background, "\033[4%p1%dm")) {
415                 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
416             } else {
417                 SP->_default_bg %= max_colors;
418             }
419         }
420     }
421 #endif
422 #endif /* NCURSES_EXT_FUNCS */
423
424     SP->_maxclick = DEFAULT_MAXCLICK;
425     SP->_mouse_event = no_mouse_event;
426     SP->_mouse_inline = no_mouse_inline;
427     SP->_mouse_parse = no_mouse_parse;
428     SP->_mouse_resume = no_mouse_resume;
429     SP->_mouse_wrap = no_mouse_wrap;
430     SP->_mouse_fd = -1;
431
432     /*
433      * If we've no magic cookie support, we suppress attributes that xmc would
434      * affect, i.e., the attributes that affect the rendition of a space.
435      */
436     SP->_ok_attributes = termattrs();
437     if (has_colors()) {
438         SP->_ok_attributes |= A_COLOR;
439     }
440 #if USE_XMC_SUPPORT
441     /*
442      * If we have no magic-cookie support compiled-in, or if it is suppressed
443      * in the environment, reset the support-flag.
444      */
445     if (magic_cookie_glitch >= 0) {
446         if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
447             support_cookies = FALSE;
448         }
449     }
450 #endif
451
452     if (!support_cookies && magic_cookie_glitch >= 0) {
453         T(("will disable attributes to work w/o magic cookies"));
454     }
455
456     if (magic_cookie_glitch > 0) {      /* tvi, wyse */
457
458         SP->_xmc_triggers = SP->_ok_attributes & (
459                                                      A_STANDOUT |
460                                                      A_UNDERLINE |
461                                                      A_REVERSE |
462                                                      A_BLINK |
463                                                      A_DIM |
464                                                      A_BOLD |
465                                                      A_INVIS |
466                                                      A_PROTECT
467             );
468 #if 0
469         /*
470          * We "should" treat colors as an attribute.  The wyse350 (and its
471          * clones) appear to be the only ones that have both colors and magic
472          * cookies.
473          */
474         if (has_colors()) {
475             SP->_xmc_triggers |= A_COLOR;
476         }
477 #endif
478         SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
479
480         T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
481         /*
482          * Supporting line-drawing may be possible.  But make the regular
483          * video attributes work first.
484          */
485         acs_chars = ABSENT_STRING;
486         ena_acs = ABSENT_STRING;
487         enter_alt_charset_mode = ABSENT_STRING;
488         exit_alt_charset_mode = ABSENT_STRING;
489 #if USE_XMC_SUPPORT
490         /*
491          * To keep the cookie support simple, suppress all of the optimization
492          * hooks except for clear_screen and the cursor addressing.
493          */
494         if (support_cookies) {
495             clr_eol = ABSENT_STRING;
496             clr_eos = ABSENT_STRING;
497             set_attributes = ABSENT_STRING;
498         }
499 #endif
500     } else if (magic_cookie_glitch == 0) {      /* hpterm */
501     }
502
503     /*
504      * If magic cookies are not supported, cancel the strings that set
505      * video attributes.
506      */
507     if (!support_cookies && magic_cookie_glitch >= 0) {
508         magic_cookie_glitch = ABSENT_NUMERIC;
509         set_attributes = ABSENT_STRING;
510         enter_blink_mode = ABSENT_STRING;
511         enter_bold_mode = ABSENT_STRING;
512         enter_dim_mode = ABSENT_STRING;
513         enter_reverse_mode = ABSENT_STRING;
514         enter_standout_mode = ABSENT_STRING;
515         enter_underline_mode = ABSENT_STRING;
516     }
517
518     /* initialize normal acs before wide, since we use mapping in the latter */
519 #if !USE_WIDEC_SUPPORT
520     if (_nc_unicode_locale() && _nc_locale_breaks_acs(cur_term)) {
521         acs_chars = NULL;
522         ena_acs = NULL;
523         enter_alt_charset_mode = NULL;
524         exit_alt_charset_mode = NULL;
525         set_attributes = NULL;
526     }
527 #endif
528     _nc_init_acs();
529 #if USE_WIDEC_SUPPORT
530     _nc_init_wacs();
531
532     SP->_screen_acs_fix = (_nc_unicode_locale()
533                            && _nc_locale_breaks_acs(cur_term));
534 #endif
535     env = _nc_get_locale();
536     SP->_legacy_coding = ((env == 0)
537                           || !strcmp(env, "C")
538                           || !strcmp(env, "POSIX"));
539     T(("legacy-coding %d", SP->_legacy_coding));
540
541     _nc_idcok = TRUE;
542     _nc_idlok = FALSE;
543
544     SP->oldhash = 0;
545     SP->newhash = 0;
546
547     T(("creating newscr"));
548     if ((SP->_newscr = newwin(slines, scolumns, 0, 0)) == 0)
549         returnCode(ERR);
550
551     T(("creating curscr"));
552     if ((SP->_curscr = newwin(slines, scolumns, 0, 0)) == 0)
553         returnCode(ERR);
554
555 #if !USE_REENTRANT
556     newscr = SP->_newscr;
557     curscr = SP->_curscr;
558 #endif
559 #if USE_SIZECHANGE
560     SP->_resize = NCURSES_SP_NAME(resizeterm);
561 #endif
562
563     newscr->_clear = TRUE;
564     curscr->_clear = FALSE;
565
566     def_shell_mode();
567     def_prog_mode();
568
569     for (rop = safe_ripoff_stack;
570          rop != safe_ripoff_sp && (rop - safe_ripoff_stack) < N_RIPS;
571          rop++) {
572
573         /* If we must simulate soft labels, grab off the line to be used.
574            We assume that we must simulate, if it is none of the standard
575            formats (4-4 or 3-2-3) for which there may be some hardware
576            support. */
577         if (rop->hook == _nc_slk_initialize)
578             if (!(num_labels <= 0 || !SLK_STDFMT(slk_format)))
579                 continue;
580         if (rop->hook) {
581             int count;
582             WINDOW *w;
583
584             count = (rop->line < 0) ? -rop->line : rop->line;
585             T(("ripping off %i lines at %s", count,
586                ((rop->line < 0)
587                 ? "bottom"
588                 : "top")));
589
590             w = newwin(count, scolumns,
591                        ((rop->line < 0)
592                         ? SP->_lines_avail - count
593                         : 0),
594                        0);
595             if (w) {
596                 rop->win = w;
597                 rop->hook(w, scolumns);
598             } else {
599                 returnCode(ERR);
600             }
601             if (rop->line < 0)
602                 bottom_stolen += count;
603             else
604                 SP->_topstolen += count;
605             SP->_lines_avail -= count;
606         }
607     }
608     /* reset the stack */
609     safe_ripoff_sp = safe_ripoff_stack;
610
611     T(("creating stdscr"));
612     assert((SP->_lines_avail + SP->_topstolen + bottom_stolen) == slines);
613     if ((SP->_stdscr = newwin(SP->_lines_avail, scolumns, 0, 0)) == 0)
614         returnCode(ERR);
615
616     SET_LINES(SP->_lines_avail);
617 #if !USE_REENTRANT
618     stdscr = SP->_stdscr;
619 #endif
620
621     returnCode(OK);
622 }
623
624 /*
625  * The internal implementation interprets line as the number of lines to rip
626  * off from the top or bottom.
627  */
628 NCURSES_EXPORT(int)
629 NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_DCLx
630                                  int line,
631                                  int (*init) (WINDOW *, int))
632 {
633     T((T_CALLED("_nc_ripoffline(%d, %p)"), line, init));
634
635     if (line != 0) {
636
637         if (safe_ripoff_sp == 0)
638             safe_ripoff_sp = safe_ripoff_stack;
639         if (safe_ripoff_sp >= safe_ripoff_stack + N_RIPS)
640             returnCode(ERR);
641
642         safe_ripoff_sp->line = line;
643         safe_ripoff_sp->hook = init;
644         safe_ripoff_sp++;
645     }
646
647     returnCode(OK);
648 }
649
650 #if NCURSES_SP_FUNCS
651 NCURSES_EXPORT(int)
652 _nc_ripoffline(int line, int (*init) (WINDOW *, int))
653 {
654     return NCURSES_SP_NAME(_nc_ripoffline) (CURRENT_SCREEN, line, init);
655 }
656 #endif
657
658 NCURSES_EXPORT(int)
659 NCURSES_SP_NAME(ripoffline) (NCURSES_SP_DCLx
660                              int line,
661                              int (*init) (WINDOW *, int))
662 {
663     START_TRACE();
664     T((T_CALLED("ripoffline(%d,%p)"), line, init));
665
666     if (line == 0)
667         returnCode(OK);
668
669     returnCode(NCURSES_SP_NAME(_nc_ripoffline) (NCURSES_SP_ARGx
670                                                 (line < 0) ? -1 : 1,
671                                                 init));
672 }
673
674 #if NCURSES_SP_FUNCS
675 NCURSES_EXPORT(int)
676 ripoffline(int line, int (*init) (WINDOW *, int))
677 {
678     return NCURSES_SP_NAME(ripoffline) (CURRENT_SCREEN, line, init);
679 }
680 #endif