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