]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_set_term.c
e44850c6a15a2fb81e2eb728a76ca3c2da8e7edd
[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.103 2008/02/03 20:31:08 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     char *env;
233     int bottom_stolen = 0;
234     bool support_cookies = USE_XMC_SUPPORT;
235     ripoff_t *rop;
236
237     T((T_CALLED("_nc_setupscreen(%d, %d, %p, %d, %d)"),
238        slines, scolumns, output, filtered, slk_format));
239
240     assert(SP == 0);            /* has been reset in newterm() ! */
241     if (!_nc_alloc_screen()
242         || ((SP->_acs_map = typeCalloc(chtype, ACS_LEN)) == 0)
243         || ((SP->_screen_acs_map = typeCalloc(bool, ACS_LEN)) == 0)) {
244         returnCode(ERR);
245     }
246
247     T(("created SP %p", SP));
248     SP->_next_screen = _nc_screen_chain;
249     _nc_screen_chain = SP;
250
251     if ((SP->_current_attr = typeCalloc(NCURSES_CH_T, 1)) == 0)
252         returnCode(ERR);
253
254     /*
255      * We should always check the screensize, just in case.
256      */
257     _nc_get_screensize(&slines, &scolumns);
258     SET_LINES(slines);
259     SET_COLS(scolumns);
260     T((T_CREATE("screen %s %dx%d"), termname(), LINES, COLS));
261
262     SP->_filtered = filtered;
263
264     /* implement filter mode */
265     if (filtered) {
266         slines = 1;
267         SET_LINES(slines);
268         clear_screen = 0;
269         cursor_down = parm_down_cursor = 0;
270         cursor_address = 0;
271         cursor_up = parm_up_cursor = 0;
272         row_address = 0;
273
274         cursor_home = carriage_return;
275         T(("filter screensize %dx%d", LINES, COLS));
276     }
277 #ifdef __DJGPP__
278     T(("setting output mode to binary"));
279     fflush(output);
280     setmode(output, O_BINARY);
281 #endif
282     _nc_set_buffer(output, TRUE);
283     SP->_term = cur_term;
284     SP->_lines = slines;
285     SP->_lines_avail = slines;
286     SP->_columns = scolumns;
287     SP->_cursrow = -1;
288     SP->_curscol = -1;
289     SP->_nl = TRUE;
290     SP->_raw = FALSE;
291     SP->_cbreak = 0;
292     SP->_echo = TRUE;
293     SP->_fifohead = -1;
294     SP->_endwin = TRUE;
295     SP->_ofp = output;
296     SP->_cursor = -1;           /* cannot know real cursor shape */
297
298 #if NCURSES_NO_PADDING
299     SP->_no_padding = getenv("NCURSES_NO_PADDING") != 0;
300     TR(TRACE_CHARPUT | TRACE_MOVE, ("padding will%s be used",
301                                     SP->_no_padding ? " not" : ""));
302 #endif
303
304 #if NCURSES_EXT_FUNCS
305     SP->_default_color = FALSE;
306     SP->_has_sgr_39_49 = FALSE;
307
308     /*
309      * Set our assumption of the terminal's default foreground and background
310      * colors.  The curs_color man-page states that we can assume that the
311      * background is black.  The origin of this assumption appears to be
312      * terminals that displayed colored text, but no colored backgrounds, e.g.,
313      * the first colored terminals around 1980.  More recent ones with better
314      * technology can display not only colored backgrounds, but all
315      * combinations.  So a terminal might be something other than "white" on
316      * black (green/black looks monochrome too), but black on white or even
317      * on ivory.
318      *
319      * White-on-black is the simplest thing to use for monochrome.  Almost
320      * all applications that use color paint both text and background, so
321      * the distinction is moot.  But a few do not - which is why we leave this
322      * configurable (a better solution is to use assume_default_colors() for
323      * the rare applications that do require that sort of appearance, since
324      * is appears that more users expect to be able to make a white-on-black
325      * or black-on-white display under control of the application than not).
326      */
327 #ifdef USE_ASSUMED_COLOR
328     SP->_default_fg = COLOR_WHITE;
329     SP->_default_bg = COLOR_BLACK;
330 #else
331     SP->_default_fg = C_MASK;
332     SP->_default_bg = C_MASK;
333 #endif
334
335     /*
336      * Allow those assumed/default color assumptions to be overridden at
337      * runtime:
338      */
339     if (getenv("NCURSES_ASSUMED_COLORS") != 0) {
340         char *p = getenv("NCURSES_ASSUMED_COLORS");
341         int fg, bg;
342         char sep1, sep2;
343         int count = sscanf(p, "%d%c%d%c", &fg, &sep1, &bg, &sep2);
344         if (count >= 1) {
345             SP->_default_fg = (fg >= 0 && fg < max_colors) ? fg : C_MASK;
346             if (count >= 3) {
347                 SP->_default_bg = (bg >= 0 && bg < max_colors) ? bg : C_MASK;
348             }
349             TR(TRACE_CHARPUT | TRACE_MOVE,
350                ("from environment assumed fg=%d, bg=%d",
351                 SP->_default_fg,
352                 SP->_default_bg));
353         }
354     }
355 #if USE_COLORFGBG
356     /*
357      * If rxvt's $COLORFGBG variable is set, use it to specify the assumed
358      * default colors.  Note that rxvt (mis)uses bold colors, equating a bold
359      * color to that value plus 8.  We'll only use the non-bold color for now -
360      * decide later if it is worth having default attributes as well.
361      */
362     if (getenv("COLORFGBG") != 0) {
363         char *p = getenv("COLORFGBG");
364         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoding COLORFGBG %s", p));
365         p = extract_fgbg(p, &(SP->_default_fg));
366         p = extract_fgbg(p, &(SP->_default_bg));
367         if (*p)                 /* assume rxvt was compiled with xpm support */
368             p = extract_fgbg(p, &(SP->_default_bg));
369         TR(TRACE_CHARPUT | TRACE_MOVE, ("decoded fg=%d, bg=%d",
370                                         SP->_default_fg, SP->_default_bg));
371         if (SP->_default_fg >= max_colors) {
372             if (set_a_foreground != ABSENT_STRING
373                 && !strcmp(set_a_foreground, "\033[3%p1%dm")) {
374                 set_a_foreground = "\033[3%?%p1%{8}%>%t9%e%p1%d%;m";
375             } else {
376                 SP->_default_fg %= max_colors;
377             }
378         }
379         if (SP->_default_bg >= max_colors) {
380             if (set_a_background != ABSENT_STRING
381                 && !strcmp(set_a_background, "\033[4%p1%dm")) {
382                 set_a_background = "\033[4%?%p1%{8}%>%t9%e%p1%d%;m";
383             } else {
384                 SP->_default_bg %= max_colors;
385             }
386         }
387     }
388 #endif
389 #endif /* NCURSES_EXT_FUNCS */
390
391     SP->_maxclick = DEFAULT_MAXCLICK;
392     SP->_mouse_event = no_mouse_event;
393     SP->_mouse_inline = no_mouse_inline;
394     SP->_mouse_parse = no_mouse_parse;
395     SP->_mouse_resume = no_mouse_resume;
396     SP->_mouse_wrap = no_mouse_wrap;
397     SP->_mouse_fd = -1;
398
399     /* initialize the panel hooks */
400     SP->_panelHook.top_panel = (struct panel *) 0;
401     SP->_panelHook.bottom_panel = (struct panel *) 0;
402     SP->_panelHook.stdscr_pseudo_panel = (struct panel *) 0;
403
404     /*
405      * If we've no magic cookie support, we suppress attributes that xmc would
406      * affect, i.e., the attributes that affect the rendition of a space.
407      */
408     SP->_ok_attributes = termattrs();
409     if (has_colors()) {
410         SP->_ok_attributes |= A_COLOR;
411     }
412 #if USE_XMC_SUPPORT
413     /*
414      * If we have no magic-cookie support compiled-in, or if it is suppressed
415      * in the environment, reset the support-flag.
416      */
417     if (magic_cookie_glitch >= 0) {
418         if (getenv("NCURSES_NO_MAGIC_COOKIE") != 0) {
419             support_cookies = FALSE;
420         }
421     }
422 #endif
423
424     if (!support_cookies && magic_cookie_glitch >= 0) {
425         T(("will disable attributes to work w/o magic cookies"));
426     }
427
428     if (magic_cookie_glitch > 0) {      /* tvi, wyse */
429
430         SP->_xmc_triggers = SP->_ok_attributes & (
431                                                      A_STANDOUT |
432                                                      A_UNDERLINE |
433                                                      A_REVERSE |
434                                                      A_BLINK |
435                                                      A_DIM |
436                                                      A_BOLD |
437                                                      A_INVIS |
438                                                      A_PROTECT
439             );
440 #if 0
441         /*
442          * We "should" treat colors as an attribute.  The wyse350 (and its
443          * clones) appear to be the only ones that have both colors and magic
444          * cookies.
445          */
446         if (has_colors()) {
447             SP->_xmc_triggers |= A_COLOR;
448         }
449 #endif
450         SP->_xmc_suppress = SP->_xmc_triggers & (chtype) ~(A_BOLD);
451
452         T(("magic cookie attributes %s", _traceattr(SP->_xmc_suppress)));
453         /*
454          * Supporting line-drawing may be possible.  But make the regular
455          * video attributes work first.
456          */
457         acs_chars = ABSENT_STRING;
458         ena_acs = ABSENT_STRING;
459         enter_alt_charset_mode = ABSENT_STRING;
460         exit_alt_charset_mode = ABSENT_STRING;
461 #if USE_XMC_SUPPORT
462         /*
463          * To keep the cookie support simple, suppress all of the optimization
464          * hooks except for clear_screen and the cursor addressing.
465          */
466         if (support_cookies) {
467             clr_eol = ABSENT_STRING;
468             clr_eos = ABSENT_STRING;
469             set_attributes = ABSENT_STRING;
470         }
471 #endif
472     } else if (magic_cookie_glitch == 0) {      /* hpterm */
473     }
474
475     /*
476      * If magic cookies are not supported, cancel the strings that set
477      * video attributes.
478      */
479     if (!support_cookies && magic_cookie_glitch >= 0) {
480         magic_cookie_glitch = ABSENT_NUMERIC;
481         set_attributes = ABSENT_STRING;
482         enter_blink_mode = ABSENT_STRING;
483         enter_bold_mode = ABSENT_STRING;
484         enter_dim_mode = ABSENT_STRING;
485         enter_reverse_mode = ABSENT_STRING;
486         enter_standout_mode = ABSENT_STRING;
487         enter_underline_mode = ABSENT_STRING;
488     }
489
490     /* initialize normal acs before wide, since we use mapping in the latter */
491 #if !USE_WIDEC_SUPPORT
492     if (_nc_unicode_locale() && _nc_locale_breaks_acs()) {
493         acs_chars = NULL;
494         ena_acs = NULL;
495         enter_alt_charset_mode = NULL;
496         exit_alt_charset_mode = NULL;
497         set_attributes = NULL;
498     }
499 #endif
500     _nc_init_acs();
501 #if USE_WIDEC_SUPPORT
502     _nc_init_wacs();
503
504     SP->_screen_acs_fix = (_nc_unicode_locale() && _nc_locale_breaks_acs());
505 #endif
506     env = _nc_get_locale();
507     SP->_legacy_coding = ((env == 0)
508                           || !strcmp(env, "C")
509                           || !strcmp(env, "POSIX"));
510     T(("legacy-coding %d", SP->_legacy_coding));
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 }