]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_newterm.c
ncurses 5.7 - patch 20090906
[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 <tic.h>
54
55 MODULE_ID("$Id: lib_newterm.c,v 1.79 2009/09/06 15:13:41 tom Exp $")
56
57 #ifdef USE_TERM_DRIVER
58 #define NumLabels      InfoOf(SP_PARM).numlabels
59 #else
60 #define NumLabels      num_labels
61 #endif
62
63 #ifndef ONLCR                   /* Allows compilation under the QNX 4.2 OS */
64 #define ONLCR 0
65 #endif
66
67 /*
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).
71  *
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.
74  */
75 static NCURSES_INLINE int
76 _nc_initscr(NCURSES_SP_DCL0)
77 {
78     int result = ERR;
79     TERMINAL *term = TerminalOf(SP_PARM);
80
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) {
84         TTY buf;
85
86         buf = term->Nttyb;
87 #ifdef TERMIOS
88         buf.c_lflag &= ~(ECHO | ECHONL);
89         buf.c_iflag &= ~(ICRNL | INLCR | IGNCR);
90         buf.c_oflag &= ~(ONLCR);
91 #elif HAVE_SGTTY_H
92         buf.sg_flags &= ~(ECHO | CRMOD);
93 #else
94         memset(&buf, 0, sizeof(buf));
95 #endif
96         result = NCURSES_SP_NAME(_nc_set_tty_mode) (NCURSES_SP_ARGx &buf);
97         if (result == OK)
98             term->Nttyb = buf;
99     }
100     return result;
101 }
102
103 /*
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
107  * initialized.
108  */
109 NCURSES_EXPORT(void)
110 NCURSES_SP_NAME(filter) (NCURSES_SP_DCL0)
111 {
112     START_TRACE();
113     T((T_CALLED("filter(%p)"), SP_PARM));
114 #if NCURSES_SP_FUNCS
115     if (IsPreScreen(SP_PARM)) {
116         SP_PARM->_filtered = TRUE;
117     }
118 #else
119     _nc_prescreen.filter_mode = TRUE;
120 #endif
121     returnVoid;
122 }
123
124 #if NCURSES_SP_FUNCS
125 NCURSES_EXPORT(void)
126 filter(void)
127 {
128     START_TRACE();
129     T((T_CALLED("filter()")));
130     _nc_prescreen.filter_mode = TRUE;
131     returnVoid;
132 }
133 #endif
134
135 #if NCURSES_EXT_FUNCS
136 /*
137  * An extension, allowing the application to open a new screen without
138  * requiring it to also be filtered.
139  */
140 NCURSES_EXPORT(void)
141 NCURSES_SP_NAME(nofilter) (NCURSES_SP_DCL0)
142 {
143     START_TRACE();
144     T((T_CALLED("nofilter(%p)"), SP_PARM));
145 #if NCURSES_SP_FUNCS
146     if (IsPreScreen(SP_PARM)) {
147         SP_PARM->_filtered = FALSE;
148     }
149 #else
150     _nc_prescreen.filter_mode = FALSE;
151 #endif
152     returnVoid;
153 }
154
155 #if NCURSES_SP_FUNCS
156 NCURSES_EXPORT(void)
157 nofilter(void)
158 {
159     START_TRACE();
160     T((T_CALLED("nofilter()")));
161     _nc_prescreen.filter_mode = FALSE;
162     returnVoid;
163 }
164 #endif
165 #endif /* NCURSES_EXT_FUNCS */
166
167 NCURSES_EXPORT(SCREEN *)
168 NCURSES_SP_NAME(newterm) (NCURSES_SP_DCLx
169                           NCURSES_CONST char *name,
170                           FILE *ofp,
171                           FILE *ifp)
172 {
173     int value;
174     int errret;
175     SCREEN *result = 0;
176     SCREEN *current;
177     TERMINAL *its_term;
178     FILE *_ofp = ofp ? ofp : stdout;
179     FILE *_ifp = ifp ? ifp : stdin;
180     int cols;
181     int numlab;
182 #ifdef USE_TERM_DRIVER
183     TERMINAL *new_term;
184 #endif
185
186     START_TRACE();
187     T((T_CALLED("newterm(%p, \"%s\", %p,%p)"), SP_PARM, name, ofp, ifp));
188
189     _nc_init_pthreads();
190     _nc_lock_global(curses);
191
192     current = SP_PARM;
193     its_term = (SP_PARM ? SP_PARM->_term : 0);
194
195     /* this loads the capability entry, then sets LINES and COLS */
196     if (setupterm(name, fileno(_ofp), &errret) != ERR) {
197         int slk_format = _nc_globals.slk_format;
198
199         _nc_set_screen(0);
200 #ifdef USE_TERM_DRIVER
201         assert(new_term != 0);
202 #endif
203
204         /* allow user to set maximum escape delay from the environment */
205         if ((value = _nc_getenv_num("ESCDELAY")) >= 0) {
206             set_escdelay(value);
207         }
208
209         /*
210          * This actually allocates the screen structure, and saves the original
211          * terminal settings.
212          */
213         if (_nc_setupscreen(LINES,
214                             COLS,
215                             _ofp,
216                             _nc_prescreen.filter_mode,
217                             slk_format) == ERR) {
218             _nc_set_screen(current);
219             result = 0;
220         } else {
221 #ifdef USE_TERM_DRIVER
222             TERMINAL_CONTROL_BLOCK *TCB;
223 #else
224             SP_PARM = CURRENT_SCREEN;
225 #endif
226             assert(SP_PARM != 0);
227             cols = *(ptrCols(SP_PARM));
228 #ifdef USE_TERM_DRIVER
229             TCB = (TERMINAL_CONTROL_BLOCK *) new_term;
230             TCB->csp = SP_PARM;
231 #endif
232             numlab = NumLabels;
233
234             /*
235              * In setupterm() we did a set_curterm(), but it was before we set
236              * CURRENT_SCREEN.  So the "current" screen's terminal pointer was
237              * overwritten with a different terminal.  Later, in
238              * _nc_setupscreen(), we set CURRENT_SCREEN and the terminal
239              * pointer in the new screen.
240              *
241              * Restore the terminal-pointer for the pre-existing screen, if
242              * any.
243              */
244             if (current)
245                 current->_term = its_term;
246
247             /* if the terminal type has real soft labels, set those up */
248             if (slk_format && NumLabels > 0 && SLK_STDFMT(slk_format))
249                 _nc_slk_initialize(stdscr, COLS);
250
251             SP_PARM->_ifd = fileno(_ifp);
252             NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
253 #ifdef TERMIOS
254             SP_PARM->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
255                                   !(cur_term->Ottyb.c_iflag & ISTRIP));
256 #else
257             SP_PARM->_use_meta = FALSE;
258 #endif
259             SP_PARM->_endwin = FALSE;
260 #ifndef USE_TERM_DRIVER
261             /*
262              * Check whether we can optimize scrolling under dumb terminals in
263              * case we do not have any of these capabilities, scrolling
264              * optimization will be useless.
265              */
266             SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
267                                    ((parm_rindex ||
268                                      parm_insert_line ||
269                                      insert_line) &&
270                                     (parm_index ||
271                                      parm_delete_line ||
272                                      delete_line)));
273 #endif
274
275             NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);         /* sets a field in the screen structure */
276
277             SP_PARM->_keytry = 0;
278
279             /* compute movement costs so we can do better move optimization */
280 #ifdef USE_TERM_DRIVER
281             TCBOf(SP_PARM)->drv->scinit(SP_PARM);
282 #else
283             /*
284              * Check for mismatched graphic-rendition capabilities.  Most SVr4
285              * terminfo trees contain entries that have rmul or rmso equated to
286              * sgr0 (Solaris curses copes with those entries).  We do this only
287              * for curses, since many termcap applications assume that
288              * smso/rmso and smul/rmul are paired, and will not function
289              * properly if we remove rmso or rmul.  Curses applications
290              * shouldn't be looking at this detail.
291              */
292 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
293             SP_PARM->_use_rmso = SGR0_TEST(exit_standout_mode);
294             SP_PARM->_use_rmul = SGR0_TEST(exit_underline_mode);
295
296             /* compute movement costs so we can do better move optimization */
297             _nc_mvcur_init();
298
299             /* initialize terminal to a sane state */
300             _nc_screen_init();
301 #endif
302
303             /* Initialize the terminal line settings. */
304             _nc_initscr(NCURSES_SP_ARG);
305
306             _nc_signal_handler(TRUE);
307             result = SP_PARM;
308         }
309     }
310     _nc_unlock_global(curses);
311     returnSP(result);
312 }
313
314 #if NCURSES_SP_FUNCS
315 NCURSES_EXPORT(SCREEN *)
316 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
317 {
318     return NCURSES_SP_NAME(newterm) (CURRENT_SCREEN, name, ofp, ifp);
319 }
320 #endif