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