]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_newterm.c
ncurses 5.6 - patch 20080804
[ncurses.git] / ncurses / base / lib_newterm.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_newterm.c
37 **
38 **      The newterm() function.
39 **
40 */
41
42 #include <curses.priv.h>
43
44 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
45 #define _POSIX_SOURCE
46 #endif
47
48 #include <term.h>               /* clear_screen, cup & friends, cur_term */
49 #include <tic.h>
50
51 MODULE_ID("$Id: lib_newterm.c,v 1.72 2008/08/03 22:49:49 tom Exp $")
52
53 #ifndef ONLCR                   /* Allows compilation under the QNX 4.2 OS */
54 #define ONLCR 0
55 #endif
56
57 /*
58  * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
59  * restored during the curses session.  The library simulates echo in software.
60  * (The behavior is unspecified if the application enables hardware echo).
61  *
62  * The newterm function also initializes terminal settings, and since initscr
63  * is supposed to behave as if it calls newterm, we do it here.
64  */
65 static NCURSES_INLINE int
66 _nc_initscr(void)
67 {
68     int result = ERR;
69
70     /* for extended XPG4 conformance requires cbreak() at this point */
71     /* (SVr4 curses does this anyway) */
72     if (cbreak() == OK) {
73         TTY buf;
74
75         buf = cur_term->Nttyb;
76 #ifdef TERMIOS
77         buf.c_lflag &= ~(ECHO | ECHONL);
78         buf.c_iflag &= ~(ICRNL | INLCR | IGNCR);
79         buf.c_oflag &= ~(ONLCR);
80 #elif HAVE_SGTTY_H
81         buf.sg_flags &= ~(ECHO | CRMOD);
82 #else
83         memset(&buf, 0, sizeof(buf));
84 #endif
85         if ((result = _nc_set_tty_mode(&buf)) == OK)
86             cur_term->Nttyb = buf;
87     }
88     return result;
89 }
90
91 /*
92  * filter() has to be called before either initscr() or newterm(), so there is
93  * apparently no way to make this flag apply to some terminals and not others,
94  * aside from possibly delaying a filter() call until some terminals have been
95  * initialized.
96  */
97 NCURSES_EXPORT(void)
98 filter(void)
99 {
100     START_TRACE();
101     T((T_CALLED("filter")));
102     _nc_prescreen.filter_mode = TRUE;
103     returnVoid;
104 }
105
106 #if NCURSES_EXT_FUNCS
107 /*
108  * An extension, allowing the application to open a new screen without
109  * requiring it to also be filtered.
110  */
111 NCURSES_EXPORT(void)
112 nofilter(void)
113 {
114     START_TRACE();
115     T((T_CALLED("nofilter")));
116     _nc_prescreen.filter_mode = FALSE;
117     returnVoid;
118 }
119 #endif
120
121 NCURSES_EXPORT(SCREEN *)
122 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
123 {
124     int value;
125     int errret;
126     SCREEN *current;
127     SCREEN *result = 0;
128     TERMINAL *its_term;
129
130     START_TRACE();
131     T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp));
132
133     _nc_lock_global(curses);
134
135     current = SP;
136     its_term = (SP ? SP->_term : 0);
137
138     /* this loads the capability entry, then sets LINES and COLS */
139     if (setupterm(name, fileno(ofp), &errret) != ERR) {
140         int slk_format = _nc_globals.slk_format;
141
142         /*
143          * This actually allocates the screen structure, and saves the original
144          * terminal settings.
145          */
146         _nc_set_screen(0);
147
148         /* allow user to set maximum escape delay from the environment */
149         if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
150             set_escdelay(value);
151         }
152
153         if (_nc_setupscreen(LINES,
154                             COLS,
155                             ofp,
156                             _nc_prescreen.filter_mode,
157                             slk_format) == ERR) {
158             _nc_set_screen(current);
159             result = 0;
160         } else {
161             assert(SP != 0);
162             /*
163              * In setupterm() we did a set_curterm(), but it was before we set
164              * SP.  So the "current" screen's terminal pointer was overwritten
165              * with a different terminal.  Later, in _nc_setupscreen(), we set
166              * SP and the terminal pointer in the new screen.
167              *
168              * Restore the terminal-pointer for the pre-existing screen, if
169              * any.
170              */
171             if (current)
172                 current->_term = its_term;
173
174             /* if the terminal type has real soft labels, set those up */
175             if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
176                 _nc_slk_initialize(stdscr, COLS);
177
178             SP->_ifd = fileno(ifp);
179             typeahead(fileno(ifp));
180 #ifdef TERMIOS
181             SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
182                              !(cur_term->Ottyb.c_iflag & ISTRIP));
183 #else
184             SP->_use_meta = FALSE;
185 #endif
186             SP->_endwin = FALSE;
187
188             /*
189              * Check whether we can optimize scrolling under dumb terminals in
190              * case we do not have any of these capabilities, scrolling
191              * optimization will be useless.
192              */
193             SP->_scrolling = ((scroll_forward && scroll_reverse) ||
194                               ((parm_rindex ||
195                                 parm_insert_line ||
196                                 insert_line) &&
197                                (parm_index ||
198                                 parm_delete_line ||
199                                 delete_line)));
200
201             baudrate();         /* sets a field in the SP structure */
202
203             SP->_keytry = 0;
204
205             /*
206              * Check for mismatched graphic-rendition capabilities.  Most SVr4
207              * terminfo trees contain entries that have rmul or rmso equated to
208              * sgr0 (Solaris curses copes with those entries).  We do this only
209              * for curses, since many termcap applications assume that
210              * smso/rmso and smul/rmul are paired, and will not function
211              * properly if we remove rmso or rmul.  Curses applications
212              * shouldn't be looking at this detail.
213              */
214 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
215             SP->_use_rmso = SGR0_TEST(exit_standout_mode);
216             SP->_use_rmul = SGR0_TEST(exit_underline_mode);
217
218             /* compute movement costs so we can do better move optimization */
219             _nc_mvcur_init();
220
221             /* initialize terminal to a sane state */
222             _nc_screen_init();
223
224             /* Initialize the terminal line settings. */
225             _nc_initscr();
226
227             _nc_signal_handler(TRUE);
228
229             result = SP;
230         }
231     }
232     _nc_unlock_global(curses);
233     returnSP(result);
234 }