]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_initscr.3x
ncurses 6.4 - patch 20231001
[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.52 2023/09/30 21:38:11 tom Exp $
31 .TH curs_initscr 3X 2023-09-30 "ncurses 6.4" "Library calls"
32 .de bP
33 .ie n  .IP \(bu 4
34 .el    .IP \(bu 2
35 ..
36 .ie \n(.g .ds `` \(lq
37 .el       .ds `` ``
38 .ie \n(.g .ds '' \(rq
39 .el       .ds '' ''
40 .SH NAME
41 \fB\%initscr\fP,
42 \fB\%newterm\fP,
43 \fB\%endwin\fP,
44 \fB\%isendwin\fP,
45 \fB\%set_term\fP,
46 \fB\%delscreen\fP \-
47 initialize, manipulate, or tear down \fIcurses\fR terminal interface
48 .SH SYNOPSIS
49 \fB#include <curses.h>\fP
50 .sp
51 \fBWINDOW *initscr(void);\fP
52 .br
53 \fBint endwin(void);\fP
54 .sp
55 \fBbool isendwin(void);\fP
56 .sp
57 \fBSCREEN *newterm(const char *\fItype\fB, FILE *\fIoutf\fB, FILE *\fIinf\fB);\fR
58 .br
59 \fBSCREEN *set_term(SCREEN *\fInew\fB);\fR
60 .br
61 \fBvoid delscreen(SCREEN* \fIsp\fB);\fR
62 .SH DESCRIPTION
63 .SS initscr
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,
68 \fBuse_env\fP.
69 For multiple-terminal applications,
70 \fBnewterm\fP may be called before \fBinitscr\fP.
71 .PP
72 The initscr code determines the terminal type and initializes all \fBcurses\fP
73 data structures.
74 \fBinitscr\fP also causes the first call to \fBrefresh\fP(3X)
75 to clear the screen.
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.
79 .SS newterm
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
85 \fBnewterm\fP.
86 .PP
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
91 .bP
92 the \fItype\fP of the terminal to be used in place of \fB$TERM\fP,
93 .bP
94 an output stream connected to the terminal, and
95 .bP
96 an input stream connected to the terminal
97 .PP
98 If the \fItype\fP parameter is \fBNULL\fP, \fB$TERM\fP will be used.
99 .PP
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.
103 .SS endwin
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.
108 .PP
109 A program should always call \fBendwin\fP before exiting or escaping from
110 \fBcurses\fP mode temporarily.
111 This routine
112 .bP
113 resets colors to correspond with the default color pair 0,
114 .bP
115 moves the cursor to the lower left-hand corner of the screen,
116 .bP
117 clears the remainder of the line so that it uses the default colors,
118 .bP
119 sets the cursor to normal visibility (see \fBcurs_set\fP(3X)),
120 .bP
121 stops cursor-addressing mode using the \fIexit_ca_mode\fP terminal capability,
122 .bP
123 restores tty modes (see \fBreset_shell_mode\fP(3X)).
124 .PP
125 Calling \fBrefresh\fP(3X) or \fBdoupdate\fP(3X) after a
126 temporary escape causes the program to resume visual mode.
127 .SS isendwin
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.
131 .SS set_term
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.
137 .SS delscreen
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.
143 .SH RETURN VALUE
144 \fBendwin\fP returns the integer \fBERR\fP upon failure and \fBOK\fP
145 upon successful completion.
146 .PP
147 Routines that return pointers always return \fBNULL\fP on error.
148 .PP
149 X/Open defines no error conditions.
150 In this implementation
151 .bP
152 \fBendwin\fP returns an error if the terminal was not initialized.
153 .bP
154 \fBnewterm\fP
155 returns an error if it cannot allocate the data structures for the screen,
156 or for the top-level windows within the screen,
157 i.e.,
158 \fBcurscr\fP, \fBnewscr\fP, or \fBstdscr\fP.
159 .bP
160 \fBset_term\fP
161 returns no error.
162 .SH PORTABILITY
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.
165 .SS Differences
166 X/Open specifies that portable applications must not
167 call \fBinitscr\fP more than once:
168 .bP
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.
172 .bP
173 This implementation allows using \fBinitscr\fP after \fBendwin\fP.
174 .PP
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
178 in XSI Curses.
179 .PP
180 Calling \fBendwin\fP does not dispose of the memory allocated in \fBinitscr\fP
181 or \fBnewterm\fP.
182 Deleting a \fBSCREEN\fP provides a way to do this:
183 .bP
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
188 is no longer needed.
189 .bP
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.
192 .bP
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.
196 .bP
197 Since version 4.0 (1996), ncurses has maintained a list of all windows
198 for each screen,
199 using that information to delete those windows when \fBdelscreen\fP is called.
200 .bP
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
208 the output stream.
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.
213 .PP
214 For example
215 .bP
216 NetBSD's \fBbaudrate\fP(3X) function uses the descriptor in \fBTERMINAL\fP.
217 \fBncurses\fP and SVr4 use the descriptor in \fBSCREEN\fP.
218 .bP
219 NetBSD and \fBncurses\fP use the descriptor
220 in \fBTERMINAL\fP
221 for terminal I/O modes,
222 e.g.,
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.
234 .SS Signal Handlers
235 Quoting from X/Open Curses Issue 7, section 3.1.1:
236 .RS 5
237 .PP
238 Curses implementations may provide for special handling of the
239 \%SIGINT,
240 \%SIGQUIT,
241 and \%SIGTSTP signals if their disposition is \%SIG_DFL at the time
242 .I \%initscr
243 is called.\|.\|.
244 .PP
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
247 the signal.
248 .PP
249 None of the Curses functions are required to be safe
250 with respect to signals.\|.\|.
251 .RE
252 .PP
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:
257 .TP 5
258 .B SIGINT
259 The handler \fIattempts\fP to cleanup the screen on exit.
260 Although it \fIusually\fP works as expected, there are limitations:
261 .RS 5
262 .bP
263 Walking the \fBSCREEN\fP list is unsafe, since all list management
264 is done without any signal blocking.
265 .bP
266 On systems which have \fBREENTRANT\fP turned on, \fBset_term\fP uses
267 functions which could deadlock or misbehave in other ways.
268 .bP
269 \fBendwin\fP calls other functions, many of which use stdio or
270 other library functions which are clearly unsafe.
271 .RE
272 .TP 5
273 .B SIGTERM
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).
277 .TP 5
278 .B SIGTSTP
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)).
285 .TP 5
286 .B SIGWINCH
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.
296 .SH SEE ALSO
297 \fB\%curses\fP(3X),
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)