1 .\"***************************************************************************
2 .\" Copyright 2018-2022,2023 Thomas E. Dickey *
3 .\" Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 .\" Permission is hereby granted, free of charge, to any person obtaining a *
6 .\" copy of this software and associated documentation files (the *
7 .\" "Software"), to deal in the Software without restriction, including *
8 .\" without limitation the rights to use, copy, modify, merge, publish, *
9 .\" distribute, distribute with modifications, sublicense, and/or sell *
10 .\" copies of the Software, and to permit persons to whom the Software is *
11 .\" furnished to do so, subject to the following conditions: *
13 .\" The above copyright notice and this permission notice shall be included *
14 .\" in all copies or substantial portions of the Software. *
16 .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 .\" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 .\" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 .\" IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 .\" DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 .\" OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 .\" THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 .\" Except as contained in this notice, the name(s) of the above copyright *
25 .\" holders shall not be used in advertising or otherwise to promote the *
26 .\" sale, use or other dealings in this Software without prior written *
28 .\"***************************************************************************
30 .\" $Id: curs_initscr.3x,v 1.52 2023/09/30 21:38:11 tom Exp $
31 .TH curs_initscr 3X 2023-09-30 "ncurses 6.4" "Library calls"
47 initialize, manipulate, or tear down \fIcurses\fR terminal interface
49 \fB#include <curses.h>\fP
51 \fBWINDOW *initscr(void);\fP
53 \fBint endwin(void);\fP
55 \fBbool isendwin(void);\fP
57 \fBSCREEN *newterm(const char *\fItype\fB, FILE *\fIoutf\fB, FILE *\fIinf\fB);\fR
59 \fBSCREEN *set_term(SCREEN *\fInew\fB);\fR
61 \fBvoid delscreen(SCREEN* \fIsp\fB);\fR
64 \fBinitscr\fP is normally the first \fBcurses\fP routine to call when
65 initializing a program.
66 A few special routines sometimes need to be called before it;
67 these are \fBslk_init\fP(3X), \fBfilter\fP, \fBripoffline\fP,
69 For multiple-terminal applications,
70 \fBnewterm\fP may be called before \fBinitscr\fP.
72 The initscr code determines the terminal type and initializes all \fBcurses\fP
74 \fBinitscr\fP also causes the first call to \fBrefresh\fP(3X)
76 If errors occur, \fBinitscr\fP writes an appropriate error
77 message to standard error and exits;
78 otherwise, a pointer is returned to \fBstdscr\fP.
80 A program that outputs to more than one terminal should use the \fBnewterm\fP
81 routine for each terminal instead of \fBinitscr\fP.
82 A program that needs to inspect capabilities,
83 so it can continue to run in a line-oriented mode if the
84 terminal cannot support a screen-oriented program, would also use
87 The routine \fBnewterm\fP should be called once for each terminal.
88 It returns a variable of type \fBSCREEN *\fP which should be saved
89 as a reference to that terminal.
90 \fBnewterm\fP's arguments are
92 the \fItype\fP of the terminal to be used in place of \fB$TERM\fP,
94 an output stream connected to the terminal, and
96 an input stream connected to the terminal
98 If the \fItype\fP parameter is \fBNULL\fP, \fB$TERM\fP will be used.
100 The file descriptor of the output stream is passed to \fBsetupterm\fP(3X),
101 which returns a pointer to a \fBTERMINAL\fP structure.
102 \fBnewterm\fP's return value holds a pointer to the \fBTERMINAL\fP structure.
104 The program must also call
105 \fBendwin\fP for each terminal being used before exiting from \fBcurses\fP.
106 If \fBnewterm\fP is called more than once for the same terminal, the first
107 terminal referred to must be the last one for which \fBendwin\fP is called.
109 A program should always call \fBendwin\fP before exiting or escaping from
110 \fBcurses\fP mode temporarily.
113 resets colors to correspond with the default color pair 0,
115 moves the cursor to the lower left-hand corner of the screen,
117 clears the remainder of the line so that it uses the default colors,
119 sets the cursor to normal visibility (see \fBcurs_set\fP(3X)),
121 stops cursor-addressing mode using the \fIexit_ca_mode\fP terminal capability,
123 restores tty modes (see \fBreset_shell_mode\fP(3X)).
125 Calling \fBrefresh\fP(3X) or \fBdoupdate\fP(3X) after a
126 temporary escape causes the program to resume visual mode.
128 The \fBisendwin\fP routine returns \fBTRUE\fP if \fBendwin\fP has been
129 called without any subsequent calls to \fBwrefresh\fP,
130 and \fBFALSE\fP otherwise.
132 The \fBset_term\fP routine is used to switch between different terminals.
133 The screen reference \fInew\fP becomes the new current terminal.
134 The previous terminal is returned by the routine.
135 This is the only routine which manipulates \fBSCREEN\fP pointers;
136 all other routines affect only the current terminal.
138 The \fBdelscreen\fP routine frees storage associated with the
139 \fBSCREEN\fP data structure.
140 The \fBendwin\fP routine does not do
141 this, so \fBdelscreen\fP should be called after \fBendwin\fP if a
142 particular \fBSCREEN\fP is no longer needed.
144 \fBendwin\fP returns the integer \fBERR\fP upon failure and \fBOK\fP
145 upon successful completion.
147 Routines that return pointers always return \fBNULL\fP on error.
149 X/Open defines no error conditions.
150 In this implementation
152 \fBendwin\fP returns an error if the terminal was not initialized.
155 returns an error if it cannot allocate the data structures for the screen,
156 or for the top-level windows within the screen,
158 \fBcurscr\fP, \fBnewscr\fP, or \fBstdscr\fP.
163 These functions were described in the XSI Curses standard, Issue 4.
164 As of 2015, the current document is X/Open Curses, Issue 7.
166 X/Open specifies that portable applications must not
167 call \fBinitscr\fP more than once:
169 The portable way to use \fBinitscr\fP is once only,
170 using \fBrefresh\fP (see curs_refresh(3X))
171 to restore the screen after \fBendwin\fP.
173 This implementation allows using \fBinitscr\fP after \fBendwin\fP.
175 Old versions of curses, e.g., BSD 4.4, would return a null pointer
176 from \fBinitscr\fP when an error is detected, rather than exiting.
177 It is safe but redundant to check the return value of \fBinitscr\fP
180 Calling \fBendwin\fP does not dispose of the memory allocated in \fBinitscr\fP
182 Deleting a \fBSCREEN\fP provides a way to do this:
184 X/Open Curses does not say what happens to \fBWINDOW\fPs when \fBdelscreen\fP
185 \*(``frees storage associated with the \fBSCREEN\fP\*(''
186 nor does the SVr4 documentation help,
187 adding that it should be called after \fBendwin\fP if a \fBSCREEN\fP
190 However, \fBWINDOW\fPs are implicitly associated with a \fBSCREEN\fP.
191 so that it is reasonable to expect \fBdelscreen\fP to deal with these.
193 SVr4 curses deletes the standard \fBWINDOW\fP structures
194 \fBstdscr\fP and \fBcurscr\fP as well as a work area \fBnewscr\fP.
195 SVr4 curses ignores other windows.
197 Since version 4.0 (1996), ncurses has maintained a list of all windows
199 using that information to delete those windows when \fBdelscreen\fP is called.
201 NetBSD copied this feature of ncurses in 2001.
202 PDCurses follows the SVr4 model,
203 deleting only the standard \fBWINDOW\fP structures.
204 .SS High-level versus low-level
205 Different implementations may disagree regarding the level of some functions.
206 For example, \fBSCREEN\fP (returned by \fBnewterm\fP) and
207 \fBTERMINAL\fP (returned by \fBsetupterm\fP(3X)) hold file descriptors for
209 If an application switches screens using \fBset_term\fR,
210 or switches terminals using \fBset_curterm\fP(3X),
211 applications which use the output file descriptor can have different
212 behavior depending on which structure holds the corresponding descriptor.
216 NetBSD's \fBbaudrate\fP(3X) function uses the descriptor in \fBTERMINAL\fP.
217 \fBncurses\fP and SVr4 use the descriptor in \fBSCREEN\fP.
219 NetBSD and \fBncurses\fP use the descriptor
221 for terminal I/O modes,
223 \fBdef_shell_mode\fP(3X),
224 \fBdef_prog_mode\fP(3X).
225 SVr4 curses uses the descriptor in \fBSCREEN\fP.
226 .SS Unset TERM Variable
227 If the TERM variable is missing or empty, \fBinitscr\fP uses the
228 value \*(``unknown\*('',
229 which normally corresponds to a terminal entry with the \fIgeneric\fP
230 (\fIgn\fP) capability.
231 Generic entries are detected by \fBsetupterm\fP(3X)
232 and cannot be used for full-screen operation.
233 Other implementations may handle a missing/empty TERM variable differently.
235 Quoting from X/Open Curses Issue 7, section 3.1.1:
238 Curses implementations may provide for special handling of the
241 and \%SIGTSTP signals if their disposition is \%SIG_DFL at the time
245 Any special handling for these signals may remain in effect for the
246 life of the process or until the process changes the disposition of
249 None of the Curses functions are required to be safe
250 with respect to signals.\|.\|.
253 This implementation establishes signal handlers during initialization,
254 e.g., \fBinitscr\fP or \fBnewterm\fP.
255 Applications which must handle these signals should set up the corresponding
256 handlers \fIafter\fP initializing the library:
259 The handler \fIattempts\fP to cleanup the screen on exit.
260 Although it \fIusually\fP works as expected, there are limitations:
263 Walking the \fBSCREEN\fP list is unsafe, since all list management
264 is done without any signal blocking.
266 On systems which have \fBREENTRANT\fP turned on, \fBset_term\fP uses
267 functions which could deadlock or misbehave in other ways.
269 \fBendwin\fP calls other functions, many of which use stdio or
270 other library functions which are clearly unsafe.
274 This uses the same handler as \fBSIGINT\fP, with the same limitations.
275 It is not mentioned in X/Open Curses, but is more suitable for this
276 purpose than \fBSIGQUIT\fP (which is used in debugging).
279 This handles the \fIstop\fP signal, used in job control.
280 When resuming the process, this implementation discards pending
281 input with \fBflushinput\fP (see curs_util(3X)), and repaints the screen
282 assuming that it has been completely altered.
283 It also updates the saved terminal modes with \fBdef_shell_mode\fP
284 (see \fBcurs_kernel\fP(3X)).
287 This handles the window-size changes which were ignored in
288 the standardization efforts.
289 The handler sets a (signal-safe) variable
290 which is later tested in \fBwgetch\fP (see curs_getch(3X)).
291 If \fBkeypad\fP has been enabled for the corresponding window,
292 \fBwgetch\fP returns the key symbol \fBKEY_RESIZE\fP.
293 At the same time, \fBwgetch\fP calls \fBresizeterm\fP to adjust the
294 standard screen \fBstdscr\fP,
295 and update other data such as \fBLINES\fP and \fBCOLS\fP.
298 \fB\%curs_kernel\fP(3X),
299 \fB\%curs_refresh\fP(3X),
300 \fB\%curs_slk\fP(3X),
301 \fB\%curs_terminfo\fP(3X),
302 \fB\%curs_util\fP(3X),
303 \fB\%curs_variables\fP(3X)