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