]> ncurses.scripts.mit.edu Git - ncurses.git/blob - man/curs_initscr.3x
ncurses 6.4 - patch 20230617
[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.41 2023/06/10 16:50:22 tom Exp $
31 .TH curs_initscr 3X 2023-06-10 "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 .na
41 .hy 0
42 .SH NAME
43 \fBinitscr\fP,
44 \fBnewterm\fP,
45 \fBendwin\fP,
46 \fBisendwin\fP,
47 \fBset_term\fP,
48 \fBdelscreen\fP \- \fBcurses\fP screen initialization and manipulation routines
49 .ad
50 .hy
51 .SH SYNOPSIS
52 \fB#include <curses.h>\fP
53 .sp
54 \fBWINDOW *initscr(void);\fP
55 .br
56 \fBint endwin(void);\fP
57 .sp
58 \fBbool isendwin(void);\fP
59 .sp
60 \fBSCREEN *newterm(const char *\fItype\fB, FILE *\fIoutf\fB, FILE *\fIinf\fB);\fR
61 .br
62 \fBSCREEN *set_term(SCREEN *\fInew\fB);\fR
63 .br
64 \fBvoid delscreen(SCREEN* \fIsp\fB);\fR
65 .br
66 .SH DESCRIPTION
67 .SS initscr
68 \fBinitscr\fP is normally the first \fBcurses\fP routine to call when
69 initializing a program.
70 A few special routines sometimes need to be called before it;
71 these are \fBslk_init\fP(3X), \fBfilter\fP, \fBripoffline\fP,
72 \fBuse_env\fP.
73 For multiple-terminal applications,
74 \fBnewterm\fP may be called before \fBinitscr\fP.
75 .PP
76 The initscr code determines the terminal type and initializes all \fBcurses\fP
77 data structures.
78 \fBinitscr\fP also causes the first call to \fBrefresh\fP(3X)
79 to clear the screen.
80 If errors occur, \fBinitscr\fP writes an appropriate error
81 message to standard error and exits;
82 otherwise, a pointer is returned to \fBstdscr\fP.
83 .SS newterm
84 .PP
85 A program that outputs to more than one terminal should use the \fBnewterm\fP
86 routine for each terminal instead of \fBinitscr\fP.
87 A program that needs to inspect capabilities,
88 so it can continue to run in a line-oriented mode if the
89 terminal cannot support a screen-oriented program, would also use
90 \fBnewterm\fP.
91 .PP
92 The routine \fBnewterm\fP should be called once for each terminal.
93 It returns a variable of type \fBSCREEN *\fP which should be saved
94 as a reference to that terminal.
95 \fBnewterm\fP's arguments are
96 .bP
97 the \fItype\fP of the terminal to be used in place of \fB$TERM\fP,
98 .bP
99 an output stream connected to the terminal, and
100 .bP
101 an input stream connected to the terminal
102 .PP
103 If the \fItype\fP parameter is \fBNULL\fP, \fB$TERM\fP will be used.
104 .PP
105 The file descriptor of the output stream is passed to \fBsetupterm\fP(3X),
106 which returns a pointer to a \fBTERMINAL\fP structure.
107 \fBnewterm\fP's return value holds a pointer to the \fBTERMINAL\fP structure.
108 .SS endwin
109 .PP
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 .PP
135 The \fBisendwin\fP routine returns \fBTRUE\fP if \fBendwin\fP has been
136 called without any subsequent calls to \fBwrefresh\fP,
137 and \fBFALSE\fP otherwise.
138 .SS set_term
139 .PP
140 The \fBset_term\fP routine is used to switch between different terminals.
141 The screen reference \fInew\fP becomes the new current terminal.
142 The previous terminal is returned by the routine.
143 This is the only routine which manipulates \fBSCREEN\fP pointers;
144 all other routines affect only the current terminal.
145 .SS delscreen
146 .PP
147 The \fBdelscreen\fP routine frees storage associated with the
148 \fBSCREEN\fP data structure.
149 The \fBendwin\fP routine does not do
150 this, so \fBdelscreen\fP should be called after \fBendwin\fP if a
151 particular \fBSCREEN\fP is no longer needed.
152 .SH RETURN VALUE
153 \fBendwin\fP returns the integer \fBERR\fP upon failure and \fBOK\fP
154 upon successful completion.
155 .PP
156 Routines that return pointers always return \fBNULL\fP on error.
157 .PP
158 X/Open defines no error conditions.
159 In this implementation
160 .bP
161 \fBendwin\fP returns an error if the terminal was not initialized.
162 .bP
163 \fBnewterm\fP
164 returns an error if it cannot allocate the data structures for the screen,
165 or for the top-level windows within the screen,
166 i.e.,
167 \fBcurscr\fP, \fBnewscr\fP, or \fBstdscr\fP.
168 .bP
169 \fBset_term\fP
170 returns no error.
171 .SH PORTABILITY
172 These functions were described in the XSI Curses standard, Issue 4.
173 As of 2015, the current document is X/Open Curses, Issue 7.
174 .SS Differences
175 X/Open specifies that portable applications must not
176 call \fBinitscr\fP more than once:
177 .bP
178 The portable way to use \fBinitscr\fP is once only,
179 using \fBrefresh\fP (see curs_refresh(3X))
180 to restore the screen after \fBendwin\fP.
181 .bP
182 This implementation allows using \fBinitscr\fP after \fBendwin\fP.
183 .PP
184 Old versions of curses, e.g., BSD 4.4, would return a null pointer
185 from \fBinitscr\fP when an error is detected, rather than exiting.
186 It is safe but redundant to check the return value of \fBinitscr\fP
187 in XSI Curses.
188 .PP
189 Calling \fBendwin\fP does not dispose of the memory allocated in \fBinitscr\fP
190 or \fBnewterm\fP.
191 Deleting a \fBSCREEN\fP provides a way to do this:
192 .bP
193 X/Open Curses does not say what happens to \fBWINDOW\fPs when \fBdelscreen\fP
194 \*(``frees storage associated with the \fBSCREEN\fP\*(''
195 nor does the SVr4 documentation help,
196 adding that it should be called after \fBendwin\fP if a \fBSCREEN\fP
197 is no longer needed.
198 .bP
199 However, \fBWINDOW\fPs are implicitly associated with a \fBSCREEN\fP.
200 so that it is reasonable to expect \fBdelscreen\fP to deal with these.
201 .bP
202 SVr4 curses deletes the standard \fBWINDOW\fP structures
203 \fBstdscr\fP and \fBcurscr\fP as well as a work area \fBnewscr\fP.
204 SVr4 curses ignores other windows.
205 .bP
206 Since version 4.0 (1996), ncurses has maintained a list of all windows
207 for each screen,
208 using that information to delete those windows when \fBdelscreen\fP is called.
209 .bP
210 NetBSD copied this feature of ncurses in 2001.
211 PDCurses follows the SVr4 model,
212 deleting only the standard \fBWINDOW\fP structures.
213 .SS High-level versus low-level
214 Different implementations may disagree regarding the level of some functions.
215 For example, \fBSCREEN\fP (returned by \fBnewterm\fP) and
216 \fBTERMINAL\fP (returned by \fBsetupterm\fP(3X)) hold file descriptors for
217 the output stream.
218 If an application switches screens using \fBset_term\fR,
219 or switches terminals using \fBset_curterm\fP(3X),
220 applications which use the output file descriptor can have different
221 behavior depending on which structure holds the corresponding descriptor.
222 .PP
223 For example
224 .bP
225 NetBSD's \fBbaudrate\fP(3X) function uses the descriptor in \fBTERMINAL\fP.
226 \fBncurses\fP and SVr4 use the descriptor in \fBSCREEN\fP.
227 .bP
228 NetBSD and \fBncurses\fP use the descriptor in \fBTERMINAL\fP for terminal I/O modes,
229 e.g.,
230 \fBdef_shell_mode\fP(3X),
231 \fBdef_prog_mode\fP(3X).
232 SVr4 curses uses the descriptor in \fBSCREEN\fP.
233 .SS Unset TERM Variable
234 .PP
235 If the TERM variable is missing or empty, \fBinitscr\fP uses the
236 value \*(``unknown\*('',
237 which normally corresponds to a terminal entry with the \fIgeneric\fP
238 (\fIgn\fP) capability.
239 Generic entries are detected by \fBsetupterm\fP(3X)
240 and cannot be used for full-screen operation.
241 Other implementations may handle a missing/empty TERM variable differently.
242 .SS Signal Handlers
243 .PP
244 Quoting from X/Open Curses, section 3.1.1:
245 .RS 5
246 .hy 0
247 .PP
248 .I Curses implementations may provide for special handling of the
249 .I \fBSIGINT\fP,
250 .I \fBSIGQUIT\fP and
251 .I \fBSIGTSTP\fP signals
252 .I if their disposition is \fBSIG_DFL\fP at the time
253 \fBinitscr\fI is called \fR...
254 .PP
255 .I Any special handling for these signals may remain in effect for the
256 .I life of the process or until the process changes the disposition of
257 .I the signal.
258 .PP
259 .I None of the Curses functions are required to be safe
260 .I with respect to signals \fP...
261 .RE
262 .hy
263 .PP
264 This implementation establishes signal handlers during initialization,
265 e.g., \fBinitscr\fP or \fBnewterm\fP.
266 Applications which must handle these signals should set up the corresponding
267 handlers \fIafter\fP initializing the library:
268 .TP 5
269 .B SIGINT
270 The handler \fIattempts\fP to cleanup the screen on exit.
271 Although it \fIusually\fP works as expected, there are limitations:
272 .RS 5
273 .bP
274 Walking the \fBSCREEN\fP list is unsafe, since all list management
275 is done without any signal blocking.
276 .bP
277 On systems which have \fBREENTRANT\fP turned on, \fBset_term\fP uses
278 functions which could deadlock or misbehave in other ways.
279 .bP
280 \fBendwin\fP calls other functions, many of which use stdio or
281 other library functions which are clearly unsafe.
282 .RE
283 .TP 5
284 .B SIGTERM
285 This uses the same handler as \fBSIGINT\fP, with the same limitations.
286 It is not mentioned in X/Open Curses, but is more suitable for this
287 purpose than \fBSIGQUIT\fP (which is used in debugging).
288 .TP 5
289 .B SIGTSTP
290 This handles the \fIstop\fP signal, used in job control.
291 When resuming the process, this implementation discards pending
292 input with \fBflushinput\fP (see curs_util(3X)), and repaints the screen
293 assuming that it has been completely altered.
294 It also updates the saved terminal modes with \fBdef_shell_mode\fP
295 (see \fBcurs_kernel\fP(3X)).
296 .TP 5
297 .B SIGWINCH
298 This handles the window-size changes which were ignored in
299 the standardization efforts.
300 The handler sets a (signal-safe) variable
301 which is later tested in \fBwgetch\fP (see curs_getch(3X)).
302 If \fBkeypad\fP has been enabled for the corresponding window,
303 \fBwgetch\fP returns the key symbol \fBKEY_RESIZE\fP.
304 At the same time, \fBwgetch\fP calls \fBresizeterm\fP to adjust the
305 standard screen \fBstdscr\fP,
306 and update other data such as \fBLINES\fP and \fBCOLS\fP.
307 .SH SEE ALSO
308 \fBcurses\fP(3X),
309 \fBcurs_kernel\fP(3X),
310 \fBcurs_refresh\fP(3X),
311 \fBcurs_slk\fP(3X),
312 \fBcurs_terminfo\fP(3X),
313 \fBcurs_util\fP(3X),
314 \fBcurs_variables\fP(3X).