curs_initscr(3X)
π curs_initscr(3X) π
okay bestie, this is curs_initscr(3X) from the Library calls section and it's literally the glow-up moment for your whole curses era β it's all about initializing, manipulating, or tearing down the curses terminal interface. grab a strawberry drink π, this one's long but it's so worth it.
β¨ NAME β¨
initscr, newterm, endwin, isendwin, set_term, delscreen β initialize, manipulate, or tear down curses terminal interface. periodt. π
π¦ SYNOPSIS π¦
#include <curses.h>
WINDOW * initscr(void);
int endwin(void);
bool isendwin(void);
SCREEN * newterm(const char * type, FILE * outf, FILE * inf);
SCREEN * set_term(SCREEN * new);
void delscreen(SCREEN * sp);
no cap, that's the whole cast of characters.
πΈ DESCRIPTION πΈ
π initscr
initscr is the vibe-setter: it figures out what your terminal type is and initializes the library's SCREEN, WINDOW, and all the other data structures. it's normally the very first curses function call a program performs. like, first thing, main character energy.
buuut β an application with unusual needs might call a few other curses functions beforehand, and that's totally valid:
slk_init(3X)β to set up soft-label keysfilter(3X)β if the program is designed to operate in a process pipelineripoffline(3X)β to reserve up to five lines at the top and/or bottom of the screen from management bystdscr, the standard curses windowuse_env(3X)and/oruse_tioctl(3X)β to configure use of the process environment and the operating system's terminal driver, respectively, when determining the dimensions of the terminal display
also, a curses program might call newterm prior to or instead of initscr in two specialized cases, and those are described in its subsection below (iykyk, keep scrolling).
initscr causes the first refresh(3X) call to clear the screen. and if errors happen? initscr writes an appropriate diagnostic message to the standard error stream and exits. otherwise, it returns a pointer to stdscr. slay.
π newterm
an application that manages multiple terminals should call newterm once for each such device instead of initscr. here's what newterm's arguments are, bestie:
- the type of the associated terminal β or a null pointer to use the
TERMenvironment variable - an output stream
outfconnected to the terminal - an input stream
infconnected to the terminal
newterm returns a variable of pointer-to-SCREEN type, which you should save for later use with set_term and delscreen. don't lose it, it's your emotional support pointer.
newterm passes the file descriptor of the output stream to the terminfo function setupterm(3X), which returns a pointer to a TERMINAL structure that newterm stores in the SCREEN it returns to the application.
an application that needs to inspect a terminal type's capabilities β so it can keep running in a line-oriented mode if the terminal type doesn't support the capabilities the application demands β would also use newterm. if at most one terminal connection is needed, the programmer could perform such a capability test, decide the mode to operate in, then call delscreen on the pointer returned by newterm, and proceed with either initscr or a non-curses interface. flexible queen behavior honestly. π
π endwin
the program must also call endwin for each terminal being used before exiting from curses. and if newterm is called more than once for the same terminal, the first terminal referred to must be the last one for which endwin is called. first in, last out, it's giving stack energy.
a program should always call endwin before exiting the application or temporarily suspending curses's management of the terminal. here's what endwin does, in this exact order:
- (if
start_color(3X)has been called) resets the terminal's foreground and background colors to correspond with those of color pair 0 (the default pair) - moves the cursor to the lower left-hand corner of the screen
- (if
start_color(3X)has been called) restores the default color pair - clears the line
- sets the cursor to normal visibility (see
curs_set(3X)) - if applicable, stops cursor-addressing mode using the
exit_ca_mode(rmcup) terminal capability - restores terminal modes (see
reset_shell_mode(3X))
calling refresh(3X) or doupdate(3X) after a temporary suspension causes curses to resume managing the terminal. we love a comeback arc. β¨
β isendwin
isendwin returns TRUE if wrefresh(3X) has not been called since the most recent endwin call, and FALSE otherwise. simple. clean. iconic.
π set_term
set_term re-orients the curses library's operations to another terminal when the application has arranged to manage more than one with newterm. set_term expects a SCREEN pointer previously returned by newterm as an argument, and returns the previous one. it's the only standard curses API function that manipulates SCREEN pointers β all others affect only the current terminal (but see curs_sp_funcs(3X)). main character of the SCREEN-pointer world, fr fr.
π§ delscreen
delscreen frees the storage backing the supplied SCREEN pointer argument. note that endwin does not do this, and that's on purpose β so an application can resume managing a terminal with curses after a (possibly conditional or temporary) suspension; see curs_kernel(3X). use delscreen after endwin when the application has no more need of a terminal device but won't soon exit.
π«Ά RETURN VALUE π«Ά
delscreen returns no value. endwin returns OK on success and ERR on failure. isendwin returns TRUE or FALSE as described above.
in ncurses:
endwinreturnsERRif- the terminal was not initialized,
- it is called more than once without updating the screen, or
- its call of
reset_shell_mode(3X)returnsERR; and
newtermreturnsERRif it cannot allocate storage for theSCREENstructure or theWINDOWstructures automatically associated with it:curscr,newscr, andstdscr.
functions that return pointers return null pointers on error. in ncurses, set_term does not fail, and initscr exits the application if it does not operate successfully. π dramatic but true.
π₯ NOTES π₯
ncurses establishes signal handlers when a function that initializes a SCREEN β either initscr or newterm β is first called. applications that wish to handle the following signals themselves should set up their corresponding handlers after initializing the screen.
SIGINTβ ncurses's handler attempts to clean up the screen on exit. although it usually works as expected, there are limitations.- walking the
SCREENlist is unsafe, since all list management is done without any signal blocking. - when an application has been built with the
_REENTRANTmacro defined (and corresponding system support),set_termuses functions that could deadlock or misbehave in other ways. endwincalls other functions, many of which usestdio(3)or other library functions that are clearly unsafe.
- walking the
SIGTERMβ ncurses uses the same handler as forSIGINT, with the same limitations. it is not mentioned in X/Open Curses, but is more suitable for this purpose thanSIGQUIT(which is used in debugging).SIGTSTPβ ncurses's handler manages the terminal-generated stop signal, used in job control. when resuming the process, ncurses discards pending input withflushinp(3X)and repaints the screen, assuming that it has been completely altered. it also updates the saved terminal modes withdef_shell_mode(3X).SIGWINCHβ ncurses handles changes to the terminal's window size, a phenomenon ignored in standardization efforts. it sets a (signal-safe) variable that is later tested bywgetch(3X)andwget_wch(3X).wgetchreturns the key codeKEY_RESIZE.wget_wchreturnsKEY_CODE_YESand sets itswchparameter toKEY_RESIZE.
at the same time, ncurses calls resizeterm(3X) to adjust the standard screen stdscr and update global variables such as LINES and COLS. resize girlies stay winning. π¦
ngl bestie, that was a lot β but we're only halfway. more parts are coming, so don't go anywhere. π
π PORTABILITY π
X/Open Curses Issue 4 is out here describing these functions, bestie, and it specifies no error conditions for them. None. Zero. It said "not my problem." π
π« Differences π«
X/Open Curses says portable applications must not call initscr more than once. Like, once and done, periodt.
- The portable way to use
initscris once only, usingrefreshto restore the screen afterendwin. - ncurses is chill and permits use of
initscrafterendwin.
initscr in BSD, from its very inception (1980) all the way through the Net/2 release (1991), returned ERR cast to a WINDOW pointer when it detected an error. π Then 4.4BSD (1995) said "new era, new me" and instead returned a null pointer. Neither one exited the application. So it's safe but redundant to check the return value of initscr in X/Open Curses β do it if you want, it's just extra.
Calling endwin does not dispose of the memory allocated by initscr or newterm. Rude, honestly. Deleting a SCREEN is the way to actually do that.
- X/Open Curses does not say what happens to
WINDOWs whendelscreen"frees storage associated with theSCREEN", and the SVr4 documentation is no help either β it just adds thatdelscreenshould be called afterendwinif aSCREENis no longer needed. π« - However, every
WINDOWis implicitly associated with aSCREEN, so it's reasonable to expectdelscreento dispose of them. Makes sense, right? iykyk. - SVr4 deletes the standard
WINDOWstructuresstdscrandcurscras well as a work areanewscr. It ignores other windows. π€ not very demure. - Since version 4.0 (1996), ncurses has maintained a list of all windows for each screen, using that information to delete those windows when
delscreenis called. π main character behaviour. - NetBSD copied this feature of ncurses in 2001. PDCurses follows the SVr4 model, deleting only the standard
WINDOWstructures andnewscr.
π¦ High-level versus Low-level Functions π¦
Implementations disagree regarding the level of abstraction applicable to a function or property. It's giving drama. For example, SCREEN (returned by newterm) and TERMINAL (returned by setupterm(3X)) hold file descriptors for the output stream. So if an application switches screens using set_term, or switches terminals using set_curterm(3X), applications using the output file descriptor can behave differently depending on which structure is holding the corresponding descriptor. π
- NetBSD's
baudratefunction uses the descriptor inTERMINAL. ncurses and SVr4 use the descriptor inSCREEN. - NetBSD and ncurses use the descriptor in
TERMINALfor terminal I/O modes, e.g.,def_shell_mode(3X),def_prog_mode(3X). SVr4 uses the descriptor inSCREEN.
πΈ Unset TERM Environment Variable πΈ
If the TERM variable is not set in the environment, or it's set but has an empty value, initscr uses the value "unknown", which normally corresponds to a terminal entry with the generic (gn) capability. Generic entries are detected by setupterm(3X) and cannot be used for full-screen operation. π So likeβ¦ set your TERM, bestie. Other implementations may handle a missing or empty TERM variable differently.
β Signal Handlers β
Quoting X/Open Curses Issue 7, section 3.1.1, word for word because we don't play with quotes:
Curses implementations may provide for special handling of theSIGINT,SIGQUIT, andSIGTSTPsignals if their disposition isSIG_DFLat the timeinitscr()is called... Any special handling for these signals may remain in effect for the life of the process or until the process changes the disposition of the signal. None of the Curses functions are required to be safe with respect to signals...
Section "NOTES" above discusses ncurses's signal handlers. Go peek at it if you skipped it. π
π HISTORY π
- 4BSD (1980) introduced
initscrandendwin. the OGs fr fr π - SVr2 (1984) added
newtermandset_term. - SVr3.1 (1987) supplied
delscreenandisendwin.
π SEE ALSO π
curses(3X)curs_kernel(3X)curs_refresh(3X)curs_slk(3X)curs_terminfo(3X)curs_util(3X)curs_variables(3X)
and that's the whole page, bestie!! πβ¨ this absolute icon of a manpage is brought to you by ncurses 6.6, dated 2025-08-23, filed under curs_initscr(3X). go forth and call initscr exactly once, mwah π
π¦πΈ
π¦ where this page comes from
- alpine 3.24: ncurses-doc 6.6_p20260516-r0 Β· X11
- arch rolling: ncurses 6.6-2 Β· MIT-open-group
- fedora 44: ncurses-devel 6.6-1.fc44 Β· MIT-open-group
- fedora 44: ncurses-devel 6.6-1.fc44 Β· MIT-open-group
- Copyright 2018-2024,2025 Thomas E. Dickey
- Copyright 1998-2016,2017 Free Software Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms