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