1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
39 ** The newterm() function.
43 #include <curses.priv.h>
45 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
50 #define CUR SP_TERMTYPE
55 MODULE_ID("$Id: lib_newterm.c,v 1.82 2009/10/24 22:52:04 tom Exp $")
57 #ifdef USE_TERM_DRIVER
58 #define NumLabels InfoOf(SP_PARM).numlabels
60 #define NumLabels num_labels
63 #ifndef ONLCR /* Allows compilation under the QNX 4.2 OS */
68 * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
69 * restored during the curses session. The library simulates echo in software.
70 * (The behavior is unspecified if the application enables hardware echo).
72 * The newterm function also initializes terminal settings, and since initscr
73 * is supposed to behave as if it calls newterm, we do it here.
75 static NCURSES_INLINE int
76 _nc_initscr(NCURSES_SP_DCL0)
79 TERMINAL *term = TerminalOf(SP_PARM);
81 /* for extended XPG4 conformance requires cbreak() at this point */
82 /* (SVr4 curses does this anyway) */
83 if (NCURSES_SP_NAME(cbreak) (NCURSES_SP_ARG) == OK) {
88 buf.c_lflag &= ~(ECHO | ECHONL);
89 buf.c_iflag &= ~(ICRNL | INLCR | IGNCR);
90 buf.c_oflag &= ~(ONLCR);
92 buf.sg_flags &= ~(ECHO | CRMOD);
94 memset(&buf, 0, sizeof(buf));
96 result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
104 * filter() has to be called before either initscr() or newterm(), so there is
105 * apparently no way to make this flag apply to some terminals and not others,
106 * aside from possibly delaying a filter() call until some terminals have been
110 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
113 T((T_CALLED("filter(%p)"), (void *) SP_PARM));
115 if (IsPreScreen(SP_PARM)) {
116 SP_PARM->_filtered = TRUE;
119 _nc_prescreen.filter_mode = TRUE;
129 T((T_CALLED("filter()")));
130 _nc_prescreen.filter_mode = TRUE;
135 #if NCURSES_EXT_FUNCS
137 * An extension, allowing the application to open a new screen without
138 * requiring it to also be filtered.
141 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
144 T((T_CALLED("nofilter(%p)"), (void *) SP_PARM));
146 if (IsPreScreen(SP_PARM)) {
147 SP_PARM->_filtered = FALSE;
150 _nc_prescreen.filter_mode = FALSE;
160 T((T_CALLED("nofilter()")));
161 _nc_prescreen.filter_mode = FALSE;
165 #endif /* NCURSES_EXT_FUNCS */
167 NCURSES_EXPORT(SCREEN *)
168 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
169 NCURSES_CONST char *name,
178 FILE *_ofp = ofp ? ofp : stdout;
179 FILE *_ifp = ifp ? ifp : stdin;
183 TERMINAL *new_term = 0;
186 T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
193 assert(SP_PARM != 0);
199 _nc_lock_global(curses);
201 current = CURRENT_SCREEN;
202 its_term = (current ? current->_term : 0);
204 /* this loads the capability entry, then sets LINES and COLS */
207 SP_PARM->_prescreen &&
209 TINFO_SETUP_TERM(&new_term, name,
210 fileno(_ofp), &errret, FALSE) != ERR) {
213 #ifdef USE_TERM_DRIVER
214 assert(new_term != 0);
218 slk_format = SP_PARM->slk_format;
219 filter_mode = SP_PARM->_filtered;
221 slk_format = _nc_globals.slk_format;
222 filter_mode = _nc_prescreen.filter_mode;
226 * This actually allocates the screen structure, and saves the original
229 if (NCURSES_SP_NAME(_nc_setupscreen) (
233 *(ptrLines(SP_PARM)),
237 slk_format) == ERR) {
238 _nc_set_screen(current);
241 #ifdef USE_TERM_DRIVER
242 TERMINAL_CONTROL_BLOCK *TCB;
243 #elif !NCURSES_SP_FUNCS
244 SP_PARM = CURRENT_SCREEN;
246 assert(SP_PARM != 0);
247 cols = *(ptrCols(SP_PARM));
248 #ifdef USE_TERM_DRIVER
249 _nc_set_screen(SP_PARM);
250 TCB = (TERMINAL_CONTROL_BLOCK *) new_term;
254 * In setupterm() we did a set_curterm(), but it was before we set
255 * CURRENT_SCREEN. So the "current" screen's terminal pointer was
256 * overwritten with a different terminal. Later, in
257 * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal
258 * pointer in the new screen.
260 * Restore the terminal-pointer for the pre-existing screen, if
264 current->_term = its_term;
266 #ifdef USE_TERM_DRIVER
267 SP_PARM->_term = new_term;
269 new_term = SP_PARM->_term;
272 /* allow user to set maximum escape delay from the environment */
273 if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
274 NCURSES_SP_NAME(set_escdelay) (NCURSES_SP_ARGx value);
277 /* if the terminal type has real soft labels, set those up */
278 if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format))
279 _nc_slk_initialize(SP_PARM->_stdscr, cols);
281 SP_PARM->_ifd = fileno(_ifp);
282 NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
284 SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
285 !(new_term->Ottyb.c_iflag & ISTRIP));
287 SP_PARM->_use_meta = FALSE;
289 SP_PARM->_endwin = FALSE;
290 #ifndef USE_TERM_DRIVER
292 * Check whether we can optimize scrolling under dumb terminals in
293 * case we do not have any of these capabilities, scrolling
294 * optimization will be useless.
296 SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
305 NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG); /* sets a field in the screen structure */
307 SP_PARM->_keytry = 0;
309 /* compute movement costs so we can do better move optimization */
310 #ifdef USE_TERM_DRIVER
311 TCBOf(SP_PARM)->drv->scinit(SP_PARM);
314 * Check for mismatched graphic-rendition capabilities. Most SVr4
315 * terminfo trees contain entries that have rmul or rmso equated to
316 * sgr0 (Solaris curses copes with those entries). We do this only
317 * for curses, since many termcap applications assume that
318 * smso/rmso and smul/rmul are paired, and will not function
319 * properly if we remove rmso or rmul. Curses applications
320 * shouldn't be looking at this detail.
322 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
323 SP_PARM->_use_rmso = SGR0_TEST(exit_standout_mode);
324 SP_PARM->_use_rmul = SGR0_TEST(exit_underline_mode);
326 /* compute movement costs so we can do better move optimization */
329 /* initialize terminal to a sane state */
333 /* Initialize the terminal line settings. */
334 _nc_initscr(NCURSES_SP_ARG);
336 _nc_signal_handler(TRUE);
340 _nc_unlock_global(curses);
345 NCURSES_EXPORT(SCREEN *)
346 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
348 return NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);