]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_initscr.3x
ncurses 6.4 - patch 20231007
[ncurses.git] / man / curs_initscr.3x
1 .\"***************************************************************************
2 .\" Copyright 2018-2022,2023 Thomas E. Dickey                                *
3 .\" Copyright 1998-2016,2017 Free Software Foundation, Inc.                  *
4 .\"                                                                          *
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:                 *
12 .\"                                                                          *
13 .\" The above copyright notice and this permission notice shall be included  *
14 .\" in all copies or substantial portions of the Software.                   *
15 .\"                                                                          *
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.                               *
23 .\"                                                                          *
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       *
27 .\" authorization.                                                           *
28 .\"***************************************************************************
29 .\"
30 .\" $Id: curs_initscr.3x,v 1.53 2023/10/07 21:19:07 tom Exp $
31 .TH curs_initscr 3X 2023-10-07 "ncurses 6.4" "Library calls"
32 .ie \n(.g \{\
33 .ds `` \(lq
34 .ds '' \(rq
35 .\}
36 .el \{\
37 .ie t .ds `` ``
38 .el   .ds `` ""
39 .ie t .ds '' ''
40 .el   .ds '' ""
41 .\}
42 .
43 .de bP
44 .ie n  .IP \(bu 4
45 .el    .IP \(bu 2
46 ..
47 .SH NAME
48 \fB\%initscr\fP,
49 \fB\%newterm\fP,
50 \fB\%endwin\fP,
51 \fB\%isendwin\fP,
52 \fB\%set_term\fP,
53 \fB\%delscreen\fP \-
54 initialize, manipulate, or tear down \fIcurses\fR terminal interface
55 .SH SYNOPSIS
56 .nf
57 \fB#include <curses.h>
58 .PP
59 \fBWINDOW *initscr(void);
60 \fBint endwin(void);
61 .PP
62 \fBbool isendwin(void);
63 .PP
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);
67 .fi
68 .SH DESCRIPTION
69 .SS initscr
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,
74 \fBuse_env\fP.
75 For multiple-terminal applications,
76 \fBnewterm\fP may be called before \fBinitscr\fP.
77 .PP
78 The initscr code determines the terminal type and initializes all \fBcurses\fP
79 data structures.
80 \fBinitscr\fP also causes the first call to \fBrefresh\fP(3X)
81 to clear the screen.
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.
85 .SS newterm
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
91 \fBnewterm\fP.
92 .PP
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
97 .bP
98 the \fItype\fP of the terminal to be used in place of \fB$TERM\fP,
99 .bP
100 an output stream connected to the terminal, and
101 .bP
102 an input stream connected to the terminal
103 .PP
104 If the \fItype\fP parameter is \fBNULL\fP, \fB$TERM\fP will be used.
105 .PP
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.
109 .SS endwin
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.
114 .PP
115 A program should always call \fBendwin\fP before exiting or escaping from
116 \fBcurses\fP mode temporarily.
117 This routine
118 .bP
119 resets colors to correspond with the default color pair 0,
120 .bP
121 moves the cursor to the lower left-hand corner of the screen,
122 .bP
123 clears the remainder of the line so that it uses the default colors,
124 .bP
125 sets the cursor to normal visibility (see \fBcurs_set\fP(3X)),
126 .bP
127 stops cursor-addressing mode using the \fIexit_ca_mode\fP terminal capability,
128 .bP
129 restores tty modes (see \fBreset_shell_mode\fP(3X)).
130 .PP
131 Calling \fBrefresh\fP(3X) or \fBdoupdate\fP(3X) after a
132 temporary escape causes the program to resume visual mode.
133 .SS isendwin
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.
137 .SS set_term
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.
143 .SS delscreen
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.
149 .SH RETURN VALUE
150 \fBendwin\fP returns the integer \fBERR\fP upon failure and \fBOK\fP
151 upon successful completion.
152 .PP
153 Routines that return pointers always return \fBNULL\fP on error.
154 .PP
155 X/Open defines no error conditions.
156 In this implementation
157 .bP
158 \fBendwin\fP returns an error if the terminal was not initialized.
159 .bP
160 \fBnewterm\fP
161 returns an error if it cannot allocate the data structures for the screen,
162 or for the top-level windows within the screen,
163 i.e.,
164 \fBcurscr\fP, \fBnewscr\fP, or \fBstdscr\fP.
165 .bP
166 \fBset_term\fP
167 returns no error.
168 .SH PORTABILITY
169 These functions were described in the XSI Curses standard, Issue 4.
170 As of 2015, the current document is X/Open Curses, Issue 7.
171 .SS Differences
172 X/Open specifies that portable applications must not
173 call \fBinitscr\fP more than once:
174 .bP
175 The portable way to use \fBinitscr\fP is once only,
176 using \fBrefresh\fP (see curs_refresh(3X))
177 to restore the screen after \fBendwin\fP.
178 .bP
179 This implementation allows using \fBinitscr\fP after \fBendwin\fP.
180 .PP
181 Old versions of curses, e.g., BSD 4.4, would return a null pointer
182 from \fBinitscr\fP when an error is detected, rather than exiting.
183 It is safe but redundant to check the return value of \fBinitscr\fP
184 in XSI Curses.
185 .PP
186 Calling \fBendwin\fP does not dispose of the memory allocated in \fBinitscr\fP
187 or \fBnewterm\fP.
188 Deleting a \fBSCREEN\fP provides a way to do this:
189 .bP
190 X/Open Curses does not say what happens to \fBWINDOW\fPs when \fBdelscreen\fP
191 \*(``frees storage associated with the \fBSCREEN\fP\*(''
192 nor does the SVr4 documentation help,
193 adding that it should be called after \fBendwin\fP if a \fBSCREEN\fP
194 is no longer needed.
195 .bP
196 However, \fBWINDOW\fPs are implicitly associated with a \fBSCREEN\fP.
197 so that it is reasonable to expect \fBdelscreen\fP to deal with these.
198 .bP
199 SVr4 curses deletes the standard \fBWINDOW\fP structures
200 \fBstdscr\fP and \fBcurscr\fP as well as a work area \fBnewscr\fP.
201 SVr4 curses ignores other windows.
202 .bP
203 Since version 4.0 (1996), ncurses has maintained a list of all windows
204 for each screen,
205 using that information to delete those windows when \fBdelscreen\fP is called.
206 .bP
207 NetBSD copied this feature of ncurses in 2001.
208 PDCurses follows the SVr4 model,
209 deleting only the standard \fBWINDOW\fP structures.
210 .SS High-level versus low-level
211 Different implementations may disagree regarding the level of some functions.
212 For example, \fBSCREEN\fP (returned by \fBnewterm\fP) and
213 \fBTERMINAL\fP (returned by \fBsetupterm\fP(3X)) hold file descriptors for
214 the output stream.
215 If an application switches screens using \fBset_term\fR,
216 or switches terminals using \fBset_curterm\fP(3X),
217 applications which use the output file descriptor can have different
218 behavior depending on which structure holds the corresponding descriptor.
219 .PP
220 For example
221 .bP
222 NetBSD's \fBbaudrate\fP(3X) function uses the descriptor in \fBTERMINAL\fP.
223 \fBncurses\fP and SVr4 use the descriptor in \fBSCREEN\fP.
224 .bP
225 NetBSD and \fBncurses\fP use the descriptor
226 in \fBTERMINAL\fP
227 for terminal I/O modes,
228 e.g.,
229 \fBdef_shell_mode\fP(3X),
230 \fBdef_prog_mode\fP(3X).
231 SVr4 curses uses the descriptor in \fBSCREEN\fP.
232 .SS Unset TERM Variable
233 If the TERM variable is missing or empty, \fBinitscr\fP uses the
234 value \*(``unknown\*('',
235 which normally corresponds to a terminal entry with the \fIgeneric\fP
236 (\fIgn\fP) capability.
237 Generic entries are detected by \fBsetupterm\fP(3X)
238 and cannot be used for full-screen operation.
239 Other implementations may handle a missing/empty TERM variable differently.
240 .SS Signal Handlers
241 Quoting from X/Open Curses Issue 7, section 3.1.1:
242 .RS 5
243 .PP
244 Curses implementations may provide for special handling of the
245 \%SIGINT,
246 \%SIGQUIT,
247 and \%SIGTSTP signals if their disposition is \%SIG_DFL at the time
248 .I \%initscr
249 is called.\|.\|.
250 .PP
251 Any special handling for these signals may remain in effect for the
252 life of the process or until the process changes the disposition of
253 the signal.
254 .PP
255 None of the Curses functions are required to be safe
256 with respect to signals.\|.\|.
257 .RE
258 .PP
259 This implementation establishes signal handlers during initialization,
260 e.g., \fBinitscr\fP or \fBnewterm\fP.
261 Applications which must handle these signals should set up the corresponding
262 handlers \fIafter\fP initializing the library:
263 .TP 5
264 .B SIGINT
265 The handler \fIattempts\fP to cleanup the screen on exit.
266 Although it \fIusually\fP works as expected, there are limitations:
267 .RS 5
268 .bP
269 Walking the \fBSCREEN\fP list is unsafe, since all list management
270 is done without any signal blocking.
271 .bP
272 On systems which have \fBREENTRANT\fP turned on, \fBset_term\fP uses
273 functions which could deadlock or misbehave in other ways.
274 .bP
275 \fBendwin\fP calls other functions, many of which use stdio or
276 other library functions which are clearly unsafe.
277 .RE
278 .TP 5
279 .B SIGTERM
280 This uses the same handler as \fBSIGINT\fP, with the same limitations.
281 It is not mentioned in X/Open Curses, but is more suitable for this
282 purpose than \fBSIGQUIT\fP (which is used in debugging).
283 .TP 5
284 .B SIGTSTP
285 This handles the \fIstop\fP signal, used in job control.
286 When resuming the process, this implementation discards pending
287 input with \fBflushinput\fP (see curs_util(3X)), and repaints the screen
288 assuming that it has been completely altered.
289 It also updates the saved terminal modes with \fBdef_shell_mode\fP
290 (see \fBcurs_kernel\fP(3X)).
291 .TP 5
292 .B SIGWINCH
293 This handles the window-size changes which were ignored in
294 the standardization efforts.
295 The handler sets a (signal-safe) variable
296 which is later tested in \fBwgetch\fP (see curs_getch(3X)).
297 If \fBkeypad\fP has been enabled for the corresponding window,
298 \fBwgetch\fP returns the key symbol \fBKEY_RESIZE\fP.
299 At the same time, \fBwgetch\fP calls \fBresizeterm\fP to adjust the
300 standard screen \fBstdscr\fP,
301 and update other data such as \fBLINES\fP and \fBCOLS\fP.
302 .SH SEE ALSO
303 \fB\%curses\fP(3X),
304 \fB\%curs_kernel\fP(3X),
305 \fB\%curs_refresh\fP(3X),
306 \fB\%curs_slk\fP(3X),
307 \fB\%curs_terminfo\fP(3X),
308 \fB\%curs_util\fP(3X),
309 \fB\%curs_variables\fP(3X)