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