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