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