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