]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_newterm.c
41bbc4ee660e20be91513b11dec016ed96152d68
[ncurses.git] / ncurses / base / lib_newterm.c
1 /****************************************************************************
2  * Copyright (c) 1998-2001,2002 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  ****************************************************************************/
33
34 /*
35 **      lib_newterm.c
36 **
37 **      The newterm() function.
38 **
39 */
40
41 #include <curses.priv.h>
42
43 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
44 #define _POSIX_SOURCE
45 #endif
46
47 #include <term.h>               /* clear_screen, cup & friends, cur_term */
48 #include <tic.h>
49
50 MODULE_ID("$Id: lib_newterm.c,v 1.57 2002/10/20 00:10:56 Philippe.Blain Exp $")
51
52 #ifndef ONLCR                   /* Allows compilation under the QNX 4.2 OS */
53 #define ONLCR 0
54 #endif
55
56 /*
57  * SVr4/XSI Curses specify that hardware echo is turned off in initscr, and not
58  * restored during the curses session.  The library simulates echo in software.
59  * (The behavior is unspecified if the application enables hardware echo).
60  *
61  * The newterm function also initializes terminal settings, and since initscr
62  * is supposed to behave as if it calls newterm, we do it here.
63  */
64 static inline int
65 _nc_initscr(void)
66 {
67     int result = ERR;
68
69     /* for extended XPG4 conformance requires cbreak() at this point */
70     /* (SVr4 curses does this anyway) */
71     if (cbreak() == OK) {
72         TTY buf;
73
74         buf = cur_term->Nttyb;
75 #ifdef TERMIOS
76         buf.c_lflag &= ~(ECHO | ECHONL);
77         buf.c_iflag &= ~(ICRNL | INLCR | IGNCR);
78         buf.c_oflag &= ~(ONLCR);
79 #else
80         buf.sg_flags &= ~(ECHO | CRMOD);
81 #endif
82         if ((result = _nc_set_tty_mode(&buf)) == OK)
83             cur_term->Nttyb = buf;
84     }
85     return result;
86 }
87
88 /*
89  * filter() has to be called before either initscr() or newterm(), so there is
90  * apparently no way to make this flag apply to some terminals and not others,
91  * aside from possibly delaying a filter() call until some terminals have been
92  * initialized.
93  */
94 static int filter_mode = FALSE;
95
96 NCURSES_EXPORT(void)
97 filter(void)
98 {
99     T((T_CALLED("filter")));
100     filter_mode = TRUE;
101     returnVoid;
102 }
103
104 NCURSES_EXPORT(SCREEN *)
105 newterm(NCURSES_CONST char *name, FILE * ofp, FILE * ifp)
106 {
107     int errret;
108     int slk_format = _nc_slk_format;
109     SCREEN *current;
110
111     START_TRACE();
112     T((T_CALLED("newterm(\"%s\",%p,%p)"), name, ofp, ifp));
113
114     /* this loads the capability entry, then sets LINES and COLS */
115     if (setupterm(name, fileno(ofp), &errret) == ERR)
116         returnSP(0);
117
118     /* implement filter mode */
119     if (filter_mode) {
120         LINES = 1;
121
122         clear_screen = 0;
123         cursor_down = parm_down_cursor = 0;
124         cursor_address = 0;
125         cursor_up = parm_up_cursor = 0;
126         row_address = 0;
127
128         cursor_home = carriage_return;
129     }
130
131     /* If we must simulate soft labels, grab off the line to be used.
132        We assume that we must simulate, if it is none of the standard
133        formats (4-4  or 3-2-3) for which there may be some hardware
134        support. */
135     if (num_labels <= 0 || !SLK_STDFMT(slk_format))
136         if (slk_format) {
137             if (ERR == _nc_ripoffline(-SLK_LINES(slk_format),
138                                       _nc_slk_initialize))
139                 returnSP(0);
140         }
141     /* this actually allocates the screen structure, and saves the
142      * original terminal settings.
143      */
144     current = SP;
145     _nc_set_screen(0);
146     if (_nc_setupscreen(LINES, COLS, ofp) == ERR) {
147         _nc_set_screen(current);
148         returnSP(0);
149     }
150
151     /* if the terminal type has real soft labels, set those up */
152     if (slk_format && num_labels > 0 && SLK_STDFMT(slk_format))
153         _nc_slk_initialize(stdscr, COLS);
154
155     SP->_ifd = fileno(ifp);
156     typeahead(fileno(ifp));
157 #ifdef TERMIOS
158     SP->_use_meta = ((cur_term->Ottyb.c_cflag & CSIZE) == CS8 &&
159                      !(cur_term->Ottyb.c_iflag & ISTRIP));
160 #else
161     SP->_use_meta = FALSE;
162 #endif
163     SP->_endwin = FALSE;
164
165     /* Check whether we can optimize scrolling under dumb terminals in case
166      * we do not have any of these capabilities, scrolling optimization
167      * will be useless.
168      */
169     SP->_scrolling = ((scroll_forward && scroll_reverse) ||
170                       ((parm_rindex || parm_insert_line || insert_line) &&
171                        (parm_index || parm_delete_line || delete_line)));
172
173     baudrate();                 /* sets a field in the SP structure */
174
175     SP->_keytry = 0;
176
177     /*
178      * Check for mismatched graphic-rendition capabilities.  Most SVr4
179      * terminfo trees contain entries that have rmul or rmso equated to
180      * sgr0 (Solaris curses copes with those entries).  We do this only for
181      * curses, since many termcap applications assume that smso/rmso and
182      * smul/rmul are paired, and will not function properly if we remove
183      * rmso or rmul.  Curses applications shouldn't be looking at this
184      * detail.
185      */
186 #define SGR0_TEST(mode) (mode != 0) && (exit_attribute_mode == 0 || strcmp(mode, exit_attribute_mode))
187     SP->_use_rmso = SGR0_TEST(exit_standout_mode);
188     SP->_use_rmul = SGR0_TEST(exit_underline_mode);
189
190     /* compute movement costs so we can do better move optimization */
191     _nc_mvcur_init();
192
193     /* initialize terminal to a sane state */
194     _nc_screen_init();
195
196     /* Initialize the terminal line settings. */
197     _nc_initscr();
198
199     _nc_signal_handler(TRUE);
200
201     returnSP(SP);
202 }