blob: 0f460af14eee2c2b7acea94491409a335d7d3a4b [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301.\"***************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002.\" Copyright 2018-2023,2024 Thomas E. Dickey *
3.\" Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304.\" *
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.\"
micky3879b9f5e72025-07-08 18:04:53 -040030.\" $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.
Steve Kondikae271bc2015-11-15 02:50:53 +010043.de bP
micky3879b9f5e72025-07-08 18:04:53 -040044.ie n .IP \(bu 4
45.el .IP \(bu 2
Steve Kondikae271bc2015-11-15 02:50:53 +010046..
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053047.SH NAME
micky3879b9f5e72025-07-08 18:04:53 -040048\fB\%initscr\fP,
49\fB\%newterm\fP,
50\fB\%endwin\fP,
51\fB\%isendwin\fP,
52\fB\%set_term\fP,
53\fB\%delscreen\fP \-
54initialize, manipulate, or tear down \fIcurses\fR terminal interface
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053055.SH SYNOPSIS
micky3879b9f5e72025-07-08 18:04:53 -040056.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
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053068.SH DESCRIPTION
Steve Kondikae271bc2015-11-15 02:50:53 +010069.SS initscr
micky3879b9f5e72025-07-08 18:04:53 -040070\fBinitscr\fP is normally the first \fBcurses\fP routine to call when
Steve Kondikae271bc2015-11-15 02:50:53 +010071initializing a program.
72A few special routines sometimes need to be called before it;
micky3879b9f5e72025-07-08 18:04:53 -040073these are \fBslk_init\fP(3X), \fBfilter\fP, \fBripoffline\fP,
74\fBuse_env\fP.
Steve Kondikae271bc2015-11-15 02:50:53 +010075For multiple-terminal applications,
micky3879b9f5e72025-07-08 18:04:53 -040076\fBnewterm\fP may be called before \fBinitscr\fP.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053077.PP
micky3879b9f5e72025-07-08 18:04:53 -040078The initscr code determines the terminal type and initializes all \fBcurses\fP
Steve Kondikae271bc2015-11-15 02:50:53 +010079data structures.
micky3879b9f5e72025-07-08 18:04:53 -040080\fBinitscr\fP also causes the first call to \fBrefresh\fP(3X)
81to clear the screen.
82If errors occur, \fBinitscr\fP writes an appropriate error
Steve Kondikae271bc2015-11-15 02:50:53 +010083message to standard error and exits;
micky3879b9f5e72025-07-08 18:04:53 -040084otherwise, a pointer is returned to \fBstdscr\fP.
Steve Kondikae271bc2015-11-15 02:50:53 +010085.SS newterm
micky3879b9f5e72025-07-08 18:04:53 -040086A program that outputs to more than one terminal should use the \fBnewterm\fP
87routine for each terminal instead of \fBinitscr\fP.
Steve Kondikae271bc2015-11-15 02:50:53 +010088A program that needs to inspect capabilities,
89so it can continue to run in a line-oriented mode if the
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053090terminal cannot support a screen-oriented program, would also use
micky3879b9f5e72025-07-08 18:04:53 -040091\fBnewterm\fP.
92.PP
93The routine \fBnewterm\fP should be called once for each terminal.
94It returns a variable of type \fISCREEN *\fP which should be saved
Steve Kondikae271bc2015-11-15 02:50:53 +010095as a reference to that terminal.
96\fBnewterm\fP's arguments are
97.bP
micky3879b9f5e72025-07-08 18:04:53 -040098the \fItype\fP of the terminal to be used in place of \fB$TERM\fP,
Steve Kondikae271bc2015-11-15 02:50:53 +010099.bP
micky3879b9f5e72025-07-08 18:04:53 -0400100an output stream connected to the terminal, and
Steve Kondikae271bc2015-11-15 02:50:53 +0100101.bP
micky3879b9f5e72025-07-08 18:04:53 -0400102an input stream connected to the terminal
Steve Kondikae271bc2015-11-15 02:50:53 +0100103.PP
micky3879b9f5e72025-07-08 18:04:53 -0400104If the \fItype\fP parameter is \fBNULL\fP, \fB$TERM\fP will be used.
105.PP
106The file descriptor of the output stream is passed to \fBsetupterm\fP(3X),
107which returns a pointer to a \fI\%TERMINAL\fP structure.
108\fBnewterm\fP's return value holds a pointer to the \fI\%TERMINAL\fP structure.
Steve Kondikae271bc2015-11-15 02:50:53 +0100109.SS endwin
Steve Kondikae271bc2015-11-15 02:50:53 +0100110The program must also call
micky3879b9f5e72025-07-08 18:04:53 -0400111\fBendwin\fP for each terminal being used before exiting from \fBcurses\fP.
112If \fBnewterm\fP is called more than once for the same terminal, the first
113terminal referred to must be the last one for which \fBendwin\fP is called.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530114.PP
micky3879b9f5e72025-07-08 18:04:53 -0400115A program should always call \fBendwin\fP before exiting or escaping from
116\fBcurses\fP mode temporarily.
Steve Kondikae271bc2015-11-15 02:50:53 +0100117This routine
118.bP
micky3879b9f5e72025-07-08 18:04:53 -0400119resets colors to correspond with the default color pair 0,
Steve Kondikae271bc2015-11-15 02:50:53 +0100120.bP
micky3879b9f5e72025-07-08 18:04:53 -0400121moves the cursor to the lower left-hand corner of the screen,
Steve Kondikae271bc2015-11-15 02:50:53 +0100122.bP
micky3879b9f5e72025-07-08 18:04:53 -0400123clears the remainder of the line so that it uses the default colors,
124.bP
125sets the cursor to normal visibility (see \fBcurs_set\fP(3X)),
126.bP
127stops cursor-addressing mode using the \fIexit_ca_mode\fP terminal capability,
128.bP
129restores tty modes (see \fBreset_shell_mode\fP(3X)).
Steve Kondikae271bc2015-11-15 02:50:53 +0100130.PP
micky3879b9f5e72025-07-08 18:04:53 -0400131Calling \fBrefresh\fP(3X) or \fBdoupdate\fP(3X) after a
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530132temporary escape causes the program to resume visual mode.
Steve Kondikae271bc2015-11-15 02:50:53 +0100133.SS isendwin
micky3879b9f5e72025-07-08 18:04:53 -0400134The \fBisendwin\fP routine returns \fBTRUE\fP if \fBendwin\fP has been
135called without any subsequent calls to \fBwrefresh\fP,
136and \fBFALSE\fP otherwise.
Steve Kondikae271bc2015-11-15 02:50:53 +0100137.SS set_term
micky3879b9f5e72025-07-08 18:04:53 -0400138The \fBset_term\fP routine is used to switch between different terminals.
139The screen reference \fInew\fP becomes the new current terminal.
Steve Kondikae271bc2015-11-15 02:50:53 +0100140The previous terminal is returned by the routine.
micky3879b9f5e72025-07-08 18:04:53 -0400141This is the only routine which manipulates \fISCREEN\fP pointers;
Steve Kondikae271bc2015-11-15 02:50:53 +0100142all other routines affect only the current terminal.
143.SS delscreen
micky3879b9f5e72025-07-08 18:04:53 -0400144The \fBdelscreen\fP routine frees storage associated with the
145\fISCREEN\fP data structure.
146The \fBendwin\fP routine does not do
147this, so \fBdelscreen\fP should be called after \fBendwin\fP if a
148particular \fISCREEN\fP is no longer needed.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530149.SH RETURN VALUE
micky3879b9f5e72025-07-08 18:04:53 -0400150\fBendwin\fP returns the integer \fBERR\fP upon failure and \fBOK\fP
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530151upon successful completion.
152.PP
micky3879b9f5e72025-07-08 18:04:53 -0400153Routines that return pointers always return \fBNULL\fP on error.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530154.PP
155X/Open defines no error conditions.
156In this implementation
Steve Kondikae271bc2015-11-15 02:50:53 +0100157.bP
micky3879b9f5e72025-07-08 18:04:53 -0400158\fBendwin\fP returns an error if
159.RS
160.bP
161the 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
Steve Kondikae271bc2015-11-15 02:50:53 +0100167.bP
168\fBnewterm\fP
169returns an error if it cannot allocate the data structures for the screen,
170or for the top-level windows within the screen,
171i.e.,
172\fBcurscr\fP, \fBnewscr\fP, or \fBstdscr\fP.
173.bP
174\fBset_term\fP
175returns no error.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530176.SH PORTABILITY
micky3879b9f5e72025-07-08 18:04:53 -0400177These functions were described in X/Open Curses, Issue 4.
Steve Kondikae271bc2015-11-15 02:50:53 +0100178As of 2015, the current document is X/Open Curses, Issue 7.
179.SS Differences
micky3879b9f5e72025-07-08 18:04:53 -0400180X/Open Curses specifies that portable applications must not
181call \fBinitscr\fP more than once:
Steve Kondikae271bc2015-11-15 02:50:53 +0100182.bP
183The portable way to use \fBinitscr\fP is once only,
micky3879b9f5e72025-07-08 18:04:53 -0400184using \fB\%refresh\fP(3X)
185to restore the screen after \fBendwin\fP.
Steve Kondikae271bc2015-11-15 02:50:53 +0100186.bP
187This implementation allows using \fBinitscr\fP after \fBendwin\fP.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530188.PP
micky3879b9f5e72025-07-08 18:04:53 -0400189Old versions of curses, e.g., BSD 4.4, would return a null pointer
190from \fBinitscr\fP when an error is detected, rather than exiting.
191It is safe but redundant to check the return value of \fBinitscr\fP
192in X/Open Curses.
Steve Kondikae271bc2015-11-15 02:50:53 +0100193.PP
micky3879b9f5e72025-07-08 18:04:53 -0400194Calling \fBendwin\fP does not dispose of the memory allocated in \fBinitscr\fP
195or \fBnewterm\fP.
196Deleting a \fISCREEN\fP provides a way to do this:
197.bP
198X/Open Curses does not say what happens to \fI\%WINDOW\fPs when \fBdelscreen\fP
199\*(``frees storage associated with the \fISCREEN\fP\*(''
200nor does the SVr4 documentation help,
201adding that it should be called after \fBendwin\fP if a \fISCREEN\fP
202is no longer needed.
203.bP
204However, \fI\%WINDOW\fPs are implicitly associated with a \fISCREEN\fP.
205so that it is reasonable to expect \fBdelscreen\fP to deal with these.
206.bP
207SVr4 curses deletes the standard \fI\%WINDOW\fP structures
208\fBstdscr\fP and \fBcurscr\fP as well as a work area \fBnewscr\fP.
209SVr4 curses ignores other windows.
210.bP
211Since version 4.0 (1996),
212\fI\%ncurses\fP has maintained a list of all windows for each screen,
213using that information to delete those windows when \fBdelscreen\fP is called.
214.bP
215NetBSD copied this feature of \fI\%ncurses\fP in 2001.
216PDCurses follows the SVr4 model,
217deleting only the standard \fI\%WINDOW\fP structures.
218.SS "High-level versus Low-level"
219Different implementations may disagree regarding the level of some functions.
220For example, \fISCREEN\fP (returned by \fBnewterm\fP) and
221\fI\%TERMINAL\fP (returned by \fBsetupterm\fP(3X)) hold file descriptors for
222the output stream.
223If an application switches screens using \fBset_term\fR,
224or switches terminals using \fBset_curterm\fP(3X),
225applications which use the output file descriptor can have different
226behavior depending on which structure holds the corresponding descriptor.
227.PP
228For example
229.bP
230NetBSD'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
233NetBSD and \fI\%ncurses\fP use the descriptor
234in \fI\%TERMINAL\fP
235for terminal I/O modes,
236e.g.,
237\fBdef_shell_mode\fP(3X),
238\fBdef_prog_mode\fP(3X).
239SVr4 curses uses the descriptor in \fISCREEN\fP.
240.SS "Unset \fITERM\fP Variable"
241If the \fITERM\fP variable is missing or empty, \fBinitscr\fP uses the
Steve Kondikae271bc2015-11-15 02:50:53 +0100242value \*(``unknown\*('',
243which normally corresponds to a terminal entry with the \fIgeneric\fP
244(\fIgn\fP) capability.
micky3879b9f5e72025-07-08 18:04:53 -0400245Generic entries are detected by \fBsetupterm\fP(3X)
246and cannot be used for full-screen operation.
247Other implementations may handle
248a missing/empty \fITERM\fP variable differently.
249.SS "Signal Handlers"
250Quoting from X/Open Curses Issue 7, section 3.1.1:
Steve Kondikae271bc2015-11-15 02:50:53 +0100251.RS 5
252.PP
micky3879b9f5e72025-07-08 18:04:53 -0400253Curses implementations may provide for special handling of the
254\%SIGINT,
255\%SIGQUIT,
256and \%SIGTSTP signals if their disposition is \%SIG_DFL at the time
257.I \%initscr
258is called.\|.\|.
Steve Kondikae271bc2015-11-15 02:50:53 +0100259.PP
micky3879b9f5e72025-07-08 18:04:53 -0400260Any special handling for these signals may remain in effect for the
Steve Kondikae271bc2015-11-15 02:50:53 +0100261life of the process or until the process changes the disposition of
micky3879b9f5e72025-07-08 18:04:53 -0400262the signal.
Steve Kondikae271bc2015-11-15 02:50:53 +0100263.PP
micky3879b9f5e72025-07-08 18:04:53 -0400264None of the Curses functions are required to be safe
265with respect to signals.\|.\|.
Steve Kondikae271bc2015-11-15 02:50:53 +0100266.RE
267.PP
268This implementation establishes signal handlers during initialization,
269e.g., \fBinitscr\fP or \fBnewterm\fP.
270Applications which must handle these signals should set up the corresponding
271handlers \fIafter\fP initializing the library:
272.TP 5
273.B SIGINT
micky3879b9f5e72025-07-08 18:04:53 -0400274The handler \fIattempts\fP to clean up the screen on exit.
Steve Kondikae271bc2015-11-15 02:50:53 +0100275Although it \fIusually\fP works as expected, there are limitations:
276.RS 5
277.bP
micky3879b9f5e72025-07-08 18:04:53 -0400278Walking the \fISCREEN\fP list is unsafe, since all list management
Steve Kondikae271bc2015-11-15 02:50:53 +0100279is done without any signal blocking.
280.bP
281On systems which have \fBREENTRANT\fP turned on, \fBset_term\fP uses
282functions which could deadlock or misbehave in other ways.
283.bP
micky3879b9f5e72025-07-08 18:04:53 -0400284\fBendwin\fP calls other functions,
285many of which use \fI\%stdio\fP(3) or other library functions which are
286clearly unsafe.
Steve Kondikae271bc2015-11-15 02:50:53 +0100287.RE
288.TP 5
289.B SIGTERM
290This uses the same handler as \fBSIGINT\fP, with the same limitations.
291It is not mentioned in X/Open Curses, but is more suitable for this
292purpose than \fBSIGQUIT\fP (which is used in debugging).
293.TP 5
294.B SIGTSTP
295This handles the \fIstop\fP signal, used in job control.
296When resuming the process, this implementation discards pending
micky3879b9f5e72025-07-08 18:04:53 -0400297input with \fB\%flushinp\fP(3X), and repaints the screen
Steve Kondikae271bc2015-11-15 02:50:53 +0100298assuming that it has been completely altered.
micky3879b9f5e72025-07-08 18:04:53 -0400299It also updates the saved terminal modes with
300\fB\%def_shell_mode\fP(3X).
Steve Kondikae271bc2015-11-15 02:50:53 +0100301.TP 5
302.B SIGWINCH
micky3879b9f5e72025-07-08 18:04:53 -0400303This handles the window-size changes which were ignored in
Steve Kondikae271bc2015-11-15 02:50:53 +0100304the standardization efforts.
305The handler sets a (signal-safe) variable
micky3879b9f5e72025-07-08 18:04:53 -0400306which is later tested in \fB\%wgetch\fP(3X).
Steve Kondikae271bc2015-11-15 02:50:53 +0100307If \fBkeypad\fP has been enabled for the corresponding window,
308\fBwgetch\fP returns the key symbol \fBKEY_RESIZE\fP.
309At the same time, \fBwgetch\fP calls \fBresizeterm\fP to adjust the
310standard screen \fBstdscr\fP,
311and update other data such as \fBLINES\fP and \fBCOLS\fP.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530312.SH SEE ALSO
micky3879b9f5e72025-07-08 18:04:53 -0400313\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)