]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_initscr.3x
ncurses 6.4 - patch 20240420
[ncurses.git] / man / curs_initscr.3x
1 .\"***************************************************************************
2 .\" Copyright 2018-2023,2024 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.69 2024/04/20 21:24:19 tom Exp $
31 .TH curs_initscr 3X 2024-04-20 "ncurses @NCURSES_MAJOR@.@NCURSES_MINOR@" "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 \fISCREEN *\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 \fI\%TERMINAL\fP structure.
108 \fBnewterm\fP's return value holds a pointer to the \fI\%TERMINAL\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 \fISCREEN\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 \fISCREEN\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 \fISCREEN\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
159 .RS
160 .bP
161 the terminal was not initialized, or
162 .bP
163 \fBendwin\fP is called more than once without updating the screen, or
164 .bP
165 \fBreset_shell_mode\fP(3X) returns an error.
166 .RE
167 .bP
168 \fBnewterm\fP
169 returns an error if it cannot allocate the data structures for the screen,
170 or for the top-level windows within the screen,
171 i.e.,
172 \fBcurscr\fP, \fBnewscr\fP, or \fBstdscr\fP.
173 .bP
174 \fBset_term\fP
175 returns no error.
176 .SH PORTABILITY
177 These functions were described in X/Open Curses, Issue 4.
178 As of 2015, the current document is X/Open Curses, Issue 7.
179 .SS Differences
180 X/Open Curses specifies that portable applications must not
181 call \fBinitscr\fP more than once:
182 .bP
183 The portable way to use \fBinitscr\fP is once only,
184 using \fB\%refresh\fP(3X)
185 to restore the screen after \fBendwin\fP.
186 .bP
187 This implementation allows using \fBinitscr\fP after \fBendwin\fP.
188 .PP
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
192 in X/Open Curses.
193 .PP
194 Calling \fBendwin\fP does not dispose of the memory allocated in \fBinitscr\fP
195 or \fBnewterm\fP.
196 Deleting a \fISCREEN\fP provides a way to do this:
197 .bP
198 X/Open Curses does not say what happens to \fI\%WINDOW\fPs when \fBdelscreen\fP
199 \*(``frees storage associated with the \fISCREEN\fP\*(''
200 nor does the SVr4 documentation help,
201 adding that it should be called after \fBendwin\fP if a \fISCREEN\fP
202 is no longer needed.
203 .bP
204 However, \fI\%WINDOW\fPs are implicitly associated with a \fISCREEN\fP.
205 so that it is reasonable to expect \fBdelscreen\fP to deal with these.
206 .bP
207 SVr4 curses deletes the standard \fI\%WINDOW\fP structures
208 \fBstdscr\fP and \fBcurscr\fP as well as a work area \fBnewscr\fP.
209 SVr4 curses ignores other windows.
210 .bP
211 Since version 4.0 (1996),
212 \fI\%ncurses\fP has maintained a list of all windows for each screen,
213 using that information to delete those windows when \fBdelscreen\fP is called.
214 .bP
215 NetBSD copied this feature of \fI\%ncurses\fP in 2001.
216 PDCurses follows the SVr4 model,
217 deleting only the standard \fI\%WINDOW\fP structures.
218 .SS "High-level versus Low-level"
219 Different implementations may disagree regarding the level of some functions.
220 For example, \fISCREEN\fP (returned by \fBnewterm\fP) and
221 \fI\%TERMINAL\fP (returned by \fBsetupterm\fP(3X)) hold file descriptors for
222 the output stream.
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.
227 .PP
228 For example
229 .bP
230 NetBSD's \fBbaudrate\fP(3X) function uses the descriptor in \fI\%TERMINAL\fP.
231 \fI\%ncurses\fP and SVr4 use the descriptor in \fISCREEN\fP.
232 .bP
233 NetBSD and \fI\%ncurses\fP use the descriptor
234 in \fI\%TERMINAL\fP
235 for terminal I/O modes,
236 e.g.,
237 \fBdef_shell_mode\fP(3X),
238 \fBdef_prog_mode\fP(3X).
239 SVr4 curses uses the descriptor in \fISCREEN\fP.
240 .SS "Unset \fITERM\fP Variable"
241 If the \fITERM\fP 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
248 a missing/empty \fITERM\fP variable differently.
249 .SS "Signal Handlers"
250 Quoting from X/Open Curses Issue 7, section 3.1.1:
251 .RS 5
252 .PP
253 Curses implementations may provide for special handling of the
254 \%SIGINT,
255 \%SIGQUIT,
256 and \%SIGTSTP signals if their disposition is \%SIG_DFL at the time
257 .I \%initscr
258 is called.\|.\|.
259 .PP
260 Any special handling for these signals may remain in effect for the
261 life of the process or until the process changes the disposition of
262 the signal.
263 .PP
264 None of the Curses functions are required to be safe
265 with respect to signals.\|.\|.
266 .RE
267 .PP
268 This implementation establishes signal handlers during initialization,
269 e.g., \fBinitscr\fP or \fBnewterm\fP.
270 Applications which must handle these signals should set up the corresponding
271 handlers \fIafter\fP initializing the library:
272 .TP 5
273 .B SIGINT
274 The handler \fIattempts\fP to clean up the screen on exit.
275 Although it \fIusually\fP works as expected, there are limitations:
276 .RS 5
277 .bP
278 Walking the \fISCREEN\fP list is unsafe, since all list management
279 is done without any signal blocking.
280 .bP
281 On systems which have \fBREENTRANT\fP turned on, \fBset_term\fP uses
282 functions which could deadlock or misbehave in other ways.
283 .bP
284 \fBendwin\fP calls other functions,
285 many of which use \fI\%stdio\fP(3) or other library functions which are
286 clearly unsafe.
287 .RE
288 .TP 5
289 .B SIGTERM
290 This uses the same handler as \fBSIGINT\fP, with the same limitations.
291 It is not mentioned in X/Open Curses, but is more suitable for this
292 purpose than \fBSIGQUIT\fP (which is used in debugging).
293 .TP 5
294 .B SIGTSTP
295 This handles the \fIstop\fP signal, used in job control.
296 When resuming the process, this implementation discards pending
297 input with \fB\%flushinp\fP(3X), and repaints the screen
298 assuming that it has been completely altered.
299 It also updates the saved terminal modes with
300 \fB\%def_shell_mode\fP(3X).
301 .TP 5
302 .B SIGWINCH
303 This handles the window-size changes which were ignored in
304 the standardization efforts.
305 The handler sets a (signal-safe) variable
306 which is later tested in \fB\%wgetch\fP(3X).
307 If \fBkeypad\fP has been enabled for the corresponding window,
308 \fBwgetch\fP returns the key symbol \fBKEY_RESIZE\fP.
309 At the same time, \fBwgetch\fP calls \fBresizeterm\fP to adjust the
310 standard screen \fBstdscr\fP,
311 and update other data such as \fBLINES\fP and \fBCOLS\fP.
312 .SH SEE ALSO
313 \fB\%curses\fP(3X),
314 \fB\%curs_kernel\fP(3X),
315 \fB\%curs_refresh\fP(3X),
316 \fB\%curs_slk\fP(3X),
317 \fB\%curs_terminfo\fP(3X),
318 \fB\%curs_util\fP(3X),
319 \fB\%curs_variables\fP(3X)