]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_newterm.c
ncurses 5.7 - patch 20091024
[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.82 2009/10/24 22:52:04 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)"), (void *) 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)"), (void *) 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 slk_format;
182     int filter_mode;
183     TERMINAL *new_term = 0;
184
185     START_TRACE();
186     T((T_CALLED("newterm(%p, \"%s\", %p,%p)"),
187        (void *) SP_PARM,
188        name,
189        (void *) ofp,
190        (void *) ifp));
191
192 #if NCURSES_SP_FUNCS
193     assert(SP_PARM != 0);
194     if (SP_PARM == 0)
195         returnSP(SP_PARM);
196 #endif
197
198     _nc_init_pthreads();
199     _nc_lock_global(curses);
200
201     current = CURRENT_SCREEN;
202     its_term = (current ? current->_term : 0);
203
204     /* this loads the capability entry, then sets LINES and COLS */
205     if (
206 #if NCURSES_SP_FUNCS
207            SP_PARM->_prescreen &&
208 #endif
209            TINFO_SETUP_TERM(&new_term, name,
210                             fileno(_ofp), &errret, FALSE) != ERR) {
211
212         _nc_set_screen(0);
213 #ifdef USE_TERM_DRIVER
214         assert(new_term != 0);
215 #endif
216
217 #if NCURSES_SP_FUNCS
218         slk_format = SP_PARM->slk_format;
219         filter_mode = SP_PARM->_filtered;
220 #else
221         slk_format = _nc_globals.slk_format;
222         filter_mode = _nc_prescreen.filter_mode;
223 #endif
224
225         /*
226          * This actually allocates the screen structure, and saves the original
227          * terminal settings.
228          */
229         if (NCURSES_SP_NAME(_nc_setupscreen) (
230 #if NCURSES_SP_FUNCS
231                                                  &SP_PARM,
232 #endif
233                                                  *(ptrLines(SP_PARM)),
234                                                  *(ptrCols(SP_PARM)),
235                                                  _ofp,
236                                                  filter_mode,
237                                                  slk_format) == ERR) {
238             _nc_set_screen(current);
239             result = 0;
240         } else {
241 #ifdef USE_TERM_DRIVER
242             TERMINAL_CONTROL_BLOCK *TCB;
243 #elif !NCURSES_SP_FUNCS
244             SP_PARM = CURRENT_SCREEN;
245 #endif
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;
251             TCB->csp = SP_PARM;
252 #endif
253             /*
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.
259              *
260              * Restore the terminal-pointer for the pre-existing screen, if
261              * any.
262              */
263             if (current)
264                 current->_term = its_term;
265
266 #ifdef USE_TERM_DRIVER
267             SP_PARM->_term = new_term;
268 #else
269             new_term = SP_PARM->_term;
270 #endif
271
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);
275             }
276
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);
280
281             SP_PARM->_ifd = fileno(_ifp);
282             NCURSES_SP_NAME(typeahead) (NCURSES_SP_ARGx fileno(_ifp));
283 #ifdef TERMIOS
284             SP_PARM->_use_meta = ((new_term->Ottyb.c_cflag & CSIZE) == CS8 &&
285                                   !(new_term->Ottyb.c_iflag & ISTRIP));
286 #else
287             SP_PARM->_use_meta = FALSE;
288 #endif
289             SP_PARM->_endwin = FALSE;
290 #ifndef USE_TERM_DRIVER
291             /*
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.
295              */
296             SP_PARM->_scrolling = ((scroll_forward && scroll_reverse) ||
297                                    ((parm_rindex ||
298                                      parm_insert_line ||
299                                      insert_line) &&
300                                     (parm_index ||
301                                      parm_delete_line ||
302                                      delete_line)));
303 #endif
304
305             NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);         /* sets a field in the screen structure */
306
307             SP_PARM->_keytry = 0;
308
309             /* compute movement costs so we can do better move optimization */
310 #ifdef USE_TERM_DRIVER
311             TCBOf(SP_PARM)->drv->scinit(SP_PARM);
312 #else
313             /*
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.
321              */
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);
325
326             /* compute movement costs so we can do better move optimization */
327             _nc_mvcur_init();
328
329             /* initialize terminal to a sane state */
330             _nc_screen_init();
331 #endif
332
333             /* Initialize the terminal line settings. */
334             _nc_initscr(NCURSES_SP_ARG);
335
336             _nc_signal_handler(TRUE);
337             result = SP_PARM;
338         }
339     }
340     _nc_unlock_global(curses);
341     returnSP(result);
342 }
343
344 #if NCURSES_SP_FUNCS
345 NCURSES_EXPORT(SCREEN *)
346 newterm(NCURSES_CONST char *name, FILE *ofp, FILE *ifp)
347 {
348     return NCURSES_SP_NAME(newterm) (CURRENT_SCREEN_PRE, name, ofp, ifp);
349 }
350 #endif