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