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.54 2023/11/11 23:20:20 tom Exp $
31 .TH curs_initscr 3X 2023-11-11 "ncurses 6.4" "Library calls"
54 initialize, manipulate, or tear down \fIcurses\fR terminal interface
57 \fB#include <curses.h>
59 \fBWINDOW *initscr(void);
62 \fBbool isendwin(void);
64 \fBSCREEN *newterm(const char *\fItype\fP, FILE *\fIoutf\fP, FILE *\fIinf\fP);
65 \fBSCREEN *set_term(SCREEN *\fInew\fP);
66 \fBvoid delscreen(SCREEN* \fIsp\fP);
70 \fBinitscr\fP is normally the first \fBcurses\fP routine to call when
71 initializing a program.
72 A few special routines sometimes need to be called before it;
73 these are \fBslk_init\fP(3X), \fBfilter\fP, \fBripoffline\fP,
75 For multiple-terminal applications,
76 \fBnewterm\fP may be called before \fBinitscr\fP.
78 The initscr code determines the terminal type and initializes all \fBcurses\fP
80 \fBinitscr\fP also causes the first call to \fBrefresh\fP(3X)
82 If errors occur, \fBinitscr\fP writes an appropriate error
83 message to standard error and exits;
84 otherwise, a pointer is returned to \fBstdscr\fP.
86 A program that outputs to more than one terminal should use the \fBnewterm\fP
87 routine for each terminal instead of \fBinitscr\fP.
88 A program that needs to inspect capabilities,
89 so it can continue to run in a line-oriented mode if the
90 terminal cannot support a screen-oriented program, would also use
93 The routine \fBnewterm\fP should be called once for each terminal.
94 It returns a variable of type \fBSCREEN *\fP which should be saved
95 as a reference to that terminal.
96 \fBnewterm\fP's arguments are
98 the \fItype\fP of the terminal to be used in place of \fB$TERM\fP,
100 an output stream connected to the terminal, and
102 an input stream connected to the terminal
104 If the \fItype\fP parameter is \fBNULL\fP, \fB$TERM\fP will be used.
106 The file descriptor of the output stream is passed to \fBsetupterm\fP(3X),
107 which returns a pointer to a \fBTERMINAL\fP structure.
108 \fBnewterm\fP's return value holds a pointer to the \fBTERMINAL\fP structure.
110 The program must also call
111 \fBendwin\fP for each terminal being used before exiting from \fBcurses\fP.
112 If \fBnewterm\fP is called more than once for the same terminal, the first
113 terminal referred to must be the last one for which \fBendwin\fP is called.
115 A program should always call \fBendwin\fP before exiting or escaping from
116 \fBcurses\fP mode temporarily.
119 resets colors to correspond with the default color pair 0,
121 moves the cursor to the lower left-hand corner of the screen,
123 clears the remainder of the line so that it uses the default colors,
125 sets the cursor to normal visibility (see \fBcurs_set\fP(3X)),
127 stops cursor-addressing mode using the \fIexit_ca_mode\fP terminal capability,
129 restores tty modes (see \fBreset_shell_mode\fP(3X)).
131 Calling \fBrefresh\fP(3X) or \fBdoupdate\fP(3X) after a
132 temporary escape causes the program to resume visual mode.
134 The \fBisendwin\fP routine returns \fBTRUE\fP if \fBendwin\fP has been
135 called without any subsequent calls to \fBwrefresh\fP,
136 and \fBFALSE\fP otherwise.
138 The \fBset_term\fP routine is used to switch between different terminals.
139 The screen reference \fInew\fP becomes the new current terminal.
140 The previous terminal is returned by the routine.
141 This is the only routine which manipulates \fBSCREEN\fP pointers;
142 all other routines affect only the current terminal.
144 The \fBdelscreen\fP routine frees storage associated with the
145 \fBSCREEN\fP data structure.
146 The \fBendwin\fP routine does not do
147 this, so \fBdelscreen\fP should be called after \fBendwin\fP if a
148 particular \fBSCREEN\fP is no longer needed.
150 \fBendwin\fP returns the integer \fBERR\fP upon failure and \fBOK\fP
151 upon successful completion.
153 Routines that return pointers always return \fBNULL\fP on error.
155 X/Open defines no error conditions.
156 In this implementation
158 \fBendwin\fP returns an error if
161 the terminal was not initialized, or
163 \fBendwin\fP is called more than once without updating the screen, or
165 \fBreset_shell_mode\fP(3X) returns an error.
169 returns an error if it cannot allocate the data structures for the screen,
170 or for the top-level windows within the screen,
172 \fBcurscr\fP, \fBnewscr\fP, or \fBstdscr\fP.
177 These functions were described in the XSI Curses standard, Issue 4.
178 As of 2015, the current document is X/Open Curses, Issue 7.
180 X/Open specifies that portable applications must not
181 call \fBinitscr\fP more than once:
183 The portable way to use \fBinitscr\fP is once only,
184 using \fBrefresh\fP (see curs_refresh(3X))
185 to restore the screen after \fBendwin\fP.
187 This implementation allows using \fBinitscr\fP after \fBendwin\fP.
189 Old versions of curses, e.g., BSD 4.4, would return a null pointer
190 from \fBinitscr\fP when an error is detected, rather than exiting.
191 It is safe but redundant to check the return value of \fBinitscr\fP
194 Calling \fBendwin\fP does not dispose of the memory allocated in \fBinitscr\fP
196 Deleting a \fBSCREEN\fP provides a way to do this:
198 X/Open Curses does not say what happens to \fBWINDOW\fPs when \fBdelscreen\fP
199 \*(``frees storage associated with the \fBSCREEN\fP\*(''
200 nor does the SVr4 documentation help,
201 adding that it should be called after \fBendwin\fP if a \fBSCREEN\fP
204 However, \fBWINDOW\fPs are implicitly associated with a \fBSCREEN\fP.
205 so that it is reasonable to expect \fBdelscreen\fP to deal with these.
207 SVr4 curses deletes the standard \fBWINDOW\fP structures
208 \fBstdscr\fP and \fBcurscr\fP as well as a work area \fBnewscr\fP.
209 SVr4 curses ignores other windows.
211 Since version 4.0 (1996), ncurses has maintained a list of all windows
213 using that information to delete those windows when \fBdelscreen\fP is called.
215 NetBSD copied this feature of ncurses in 2001.
216 PDCurses follows the SVr4 model,
217 deleting only the standard \fBWINDOW\fP structures.
218 .SS High-level versus low-level
219 Different implementations may disagree regarding the level of some functions.
220 For example, \fBSCREEN\fP (returned by \fBnewterm\fP) and
221 \fBTERMINAL\fP (returned by \fBsetupterm\fP(3X)) hold file descriptors for
223 If an application switches screens using \fBset_term\fR,
224 or switches terminals using \fBset_curterm\fP(3X),
225 applications which use the output file descriptor can have different
226 behavior depending on which structure holds the corresponding descriptor.
230 NetBSD's \fBbaudrate\fP(3X) function uses the descriptor in \fBTERMINAL\fP.
231 \fBncurses\fP and SVr4 use the descriptor in \fBSCREEN\fP.
233 NetBSD and \fBncurses\fP use the descriptor
235 for terminal I/O modes,
237 \fBdef_shell_mode\fP(3X),
238 \fBdef_prog_mode\fP(3X).
239 SVr4 curses uses the descriptor in \fBSCREEN\fP.
240 .SS Unset TERM Variable
241 If the TERM variable is missing or empty, \fBinitscr\fP uses the
242 value \*(``unknown\*('',
243 which normally corresponds to a terminal entry with the \fIgeneric\fP
244 (\fIgn\fP) capability.
245 Generic entries are detected by \fBsetupterm\fP(3X)
246 and cannot be used for full-screen operation.
247 Other implementations may handle a missing/empty TERM variable differently.
249 Quoting from X/Open Curses Issue 7, section 3.1.1:
252 Curses implementations may provide for special handling of the
255 and \%SIGTSTP signals if their disposition is \%SIG_DFL at the time
259 Any special handling for these signals may remain in effect for the
260 life of the process or until the process changes the disposition of
263 None of the Curses functions are required to be safe
264 with respect to signals.\|.\|.
267 This implementation establishes signal handlers during initialization,
268 e.g., \fBinitscr\fP or \fBnewterm\fP.
269 Applications which must handle these signals should set up the corresponding
270 handlers \fIafter\fP initializing the library:
273 The handler \fIattempts\fP to cleanup the screen on exit.
274 Although it \fIusually\fP works as expected, there are limitations:
277 Walking the \fBSCREEN\fP list is unsafe, since all list management
278 is done without any signal blocking.
280 On systems which have \fBREENTRANT\fP turned on, \fBset_term\fP uses
281 functions which could deadlock or misbehave in other ways.
283 \fBendwin\fP calls other functions, many of which use stdio or
284 other library functions which are clearly unsafe.
288 This uses the same handler as \fBSIGINT\fP, with the same limitations.
289 It is not mentioned in X/Open Curses, but is more suitable for this
290 purpose than \fBSIGQUIT\fP (which is used in debugging).
293 This handles the \fIstop\fP signal, used in job control.
294 When resuming the process, this implementation discards pending
295 input with \fBflushinput\fP (see curs_util(3X)), and repaints the screen
296 assuming that it has been completely altered.
297 It also updates the saved terminal modes with \fBdef_shell_mode\fP
298 (see \fBcurs_kernel\fP(3X)).
301 This handles the window-size changes which were ignored in
302 the standardization efforts.
303 The handler sets a (signal-safe) variable
304 which is later tested in \fBwgetch\fP (see curs_getch(3X)).
305 If \fBkeypad\fP has been enabled for the corresponding window,
306 \fBwgetch\fP returns the key symbol \fBKEY_RESIZE\fP.
307 At the same time, \fBwgetch\fP calls \fBresizeterm\fP to adjust the
308 standard screen \fBstdscr\fP,
309 and update other data such as \fBLINES\fP and \fBCOLS\fP.
312 \fB\%curs_kernel\fP(3X),
313 \fB\%curs_refresh\fP(3X),
314 \fB\%curs_slk\fP(3X),
315 \fB\%curs_terminfo\fP(3X),
316 \fB\%curs_util\fP(3X),
317 \fB\%curs_variables\fP(3X)