initscr(3NCURSES)

✨ initscr(3NCURSES) β€” Library calls ✨

okay bestie, welcome to the curses terminal-init group chat πŸ’– This is initscr(3NCURSES) and friends, and they are literally the main characters who get your terminal interface set up, switched around, and cleaned up. No cap. 🫢

🌸 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);

πŸŽ€ DESCRIPTION πŸŽ€

🌟 initscr 🌟

okay so initscr is the moment your program walks in, does a vibe check, and sets everything up. It determines the terminal type and initializes the library's SCREEN, WINDOW, and other data structures. It is normally the first curses function call a program performs. But β€” and this is the plot twist β€” an application with unusual needs might employ a few other curses functions beforehand:

  • slk_init(3NCURSES) β€” to set up soft-label keys; πŸŽ€
  • filter(3NCURSES) β€” if the program is designed to operate in a process pipeline;
  • ripoffline(3NCURSES) β€” to reserve up to five lines at the top and/or bottom of the screen from management by stdscr, the standard curses window; and
  • use_env(3NCURSES) and/or use_tioctl(3NCURSES) β€” to configure use of the process environment and operating system's terminal driver, respectively, when determining the dimensions of the terminal display.

Further, a curses program might call newterm prior to or instead of initscr in two specialized cases described in its subsection below. iykyk. 😌

initscr causes the first refresh(3NCURSES) call to clear the screen. If errors occur, initscr writes an appropriate diagnostic message to the standard error stream and exits; otherwise, it returns a pointer to stdscr. So if it works, you get stdscr; if not, it's giving dramatic exit energy. 😭

🌈 newterm 🌈

An application that manages multiple terminals should call newterm once for each such device instead of initscr. newterm's arguments are:

  • the type of the associated terminal, or a null pointer to use the TERM environment variable;
  • an output stream outf connected to the terminal; and
  • an input stream inf connected to the terminal.

newterm returns a variable of pointer-to-SCREEN type, which should be saved for later use with set_term and delscreen. Save that pointer, bestie β€” future you will need it. πŸ’–

The way that newterm passes the file descriptor of the output stream to the terminfo function setupterm(3NCURSES), 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 that it can continue to run in a line-oriented mode if the terminal type does not support 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 in which to operate, then call delscreen on the pointer returned by newterm, and proceed with either initscr or a non-curses interface. It's giving "test the vibes first, then commit." ✨

πŸ’– endwin πŸ’–

The program must also call endwin for each terminal being used before exiting from curses. 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. Highkey this order matters, okay? Don't skip it. 🚨

A program should always call endwin before exiting the application or temporarily suspending curses's management of the terminal. endwin:

  • (if start_color(3NCURSES) 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(3NCURSES) has been called) restores the default color pair,
  • clears the line,
  • sets the cursor to normal visibility (see curs_set(3NCURSES)),
  • if applicable, stops cursor-addressing mode using the exit_ca_mode (rmcup) terminal capability, and
  • restores terminal modes (see reset_shell_mode(3NCURSES)).

Calling refresh(3NCURSES) or doupdate(3NCURSES) after a temporary suspension causes curses to resume managing the terminal. So it's not goodbye forever, it's just a soft launch. πŸ’…

✨ isendwin ✨

isendwin returns TRUE if wrefresh(3NCURSES) has not been called since the most recent endwin call, and FALSE otherwise. Basically a "are we still on break?" check. 🫢

πŸ¦‹ 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. set_term is the only standard curses API function that manipulates SCREEN pointers; all others affect only the current terminal (but see sp_funcs(3NCURSES)). Not me being the only one allowed to touch the SCREEN pointers β€” iconic behavior, ate. πŸ‘‘

🌸 delscreen 🌸

delscreen frees the storage backing the supplied SCREEN pointer argument. endwin does not, so that an application can resume managing a terminal with curses after a (possibly conditional or temporary) suspension; see kernel(3NCURSES). Use delscreen after endwin when the application has no more need of a terminal device but will not soon exit. Clean up your room, bestie. delscreen is the final declutter. 🧹✨

πŸ’… 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,

  • endwin returns ERR if
    • the terminal was not initialized,
    • it is called more than once without updating the screen, or
    • its call of reset_shell_mode(3NCURSES) returns ERR; and
  • newterm returns ERR if it cannot allocate storage for the SCREEN structure or the WINDOW structures automatically associated with it: curscr, newscr, and stdscr.

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. So initscr is dramatic: succeed or exit, no in-between. πŸ’€

πŸ‘‘ 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. Timing is everything, fr fr. ⏰

SIGINT ncurses's handler attempts to clean up the screen on exit. Although it usually works as expected, there are limitations.

  • Walking the SCREEN list is unsafe, since all list management is done without any signal blocking.
  • When an application has been built with the _REENTRANT macro defined (and corresponding system support), set_term uses functions that could deadlock or misbehave in other ways.
  • endwin calls other functions, many of which use stdio(3) or other library functions that are clearly unsafe.

So like, the SIGINT cleanup is lowkey not always safe. No cap. 😬

SIGTERM ncurses uses the same handler as for SIGINT, with the same limitations. It is not mentioned in X/Open Curses, but is more suitable for this purpose than SIGQUIT (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 with flushinp(3NCURSES) and repaints the screen, assuming that it has been completely altered. It also updates the saved terminal modes with def_shell_mode(3NCURSES).

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 by wgetch(3NCURSES) and wget_wch(3NCURSES).

  • wgetch returns the key code KEY_RESIZE.
  • wget_wch returns KEY_CODE_YES and sets its wch parameter to KEY_RESIZE.

At the same time, ncurses calls resizeterm(3NCURSES) to adjust the standard screen stdscr and update global variables such as LINES and COLS. Resize the terminal and watch the whole vibe update. πŸ’…βœ¨

🌈 PORTABILITY 🌈

X/Open Curses Issue 4 describes these functions, bestie. And it specifies no error conditions for them. That's it, that's the whole vibe. ✨


πŸ’… Differences πŸ’…

X/Open Curses says that portable applications must not call initscr more than once. Like, once and done. No double-dipping. πŸ™…β€β™€οΈ

  • The portable way to use initscr is once only, using refresh to restore the screen after endwin.
  • ncurses permits use of initscr after endwin. (She's flexible like that. πŸ’–)

initscr in BSD, from its 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) switched it up and returned a null pointer instead. Neither one exited the application. So it's safe but redundant to check the return value of initscr in X/Open Curses. Check it if you want, she won't be mad, it's just extra. 😌

Calling endwin does not dispose of the memory allocated by initscr or newterm. Deleting a SCREEN is the way to actually do that.

  • X/Open Curses does not say what happens to WINDOWs when delscreen β€œfrees storage associated with the SCREEN”, and the SVr4 documentation does not help either β€” it just adds that delscreen should be called after endwin if a SCREEN is no longer needed. Not the docs being vague, I can't. 😭
  • However! Every WINDOW is implicitly associated with a SCREEN, so it is reasonable to expect delscreen to dispose of them. Makes sense, right? ✨
  • SVr4 deletes the standard WINDOW structures stdscr and curscr, as well as a work area newscr. It ignores other windows. Rude, honestly. πŸ’€
  • Since version 4.0 (1996), ncurses has maintained a list of all windows for each screen, using that information to delete those windows when delscreen is called. Organized queen. πŸ‘‘
  • NetBSD copied this feature of ncurses in 2001. PDCurses follows the SVr4 model, deleting only the standard WINDOW structures and newscr.

πŸ¦‹ High-level versus Low-level Functions πŸ¦‹

Implementations disagree regarding the level of abstraction applicable to a function or property. For example, SCREEN (returned by newterm) and TERMINAL (returned by setupterm(3NCURSES)) hold file descriptors for the output stream. So if an application switches screens using set_term, or switches terminals using set_curterm(3NCURSES), applications using the output file descriptor can behave differently depending on which structure is holding the corresponding descriptor. It's giving chaos, lowkey. πŸ™ˆ

  • NetBSD's baudrate function uses the descriptor in TERMINAL. ncurses and SVr4 use the descriptor in SCREEN.
  • NetBSD and ncurses use the descriptor in TERMINAL for terminal I/O modes, e.g., def_shell_mode(3NCURSES), def_prog_mode(3NCURSES). SVr4 uses the descriptor in SCREEN.

🧚 Unset TERM Environment Variable 🧚

If the TERM variable is not set in the environment, or 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(3NCURSES) and cannot be used for full-screen operation. 🚫

Other implementations may handle a missing or empty TERM variable differently. So don't come for ncurses, everyone's got their own thing going on. πŸ’…


πŸ’€ Signal Handlers πŸ’€

Quoting X/Open Curses Issue 7, section 3.1.1, and I'm putting this in a little quote box because it's the moment:

Curses implementations may provide for special handling of the SIGINT, SIGQUIT, and SIGTSTP signals if their disposition is SIG_DFL at the time initscr() 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...

The section β€œNOTES” above discusses ncurses's signal handlers. Go read that, bestie, it's up there. 🫢


πŸ‘‘ HISTORY πŸ‘‘

  • 4BSD (1980) introduced initscr and endwin. The originals. The icons. ⭐
  • SVr2 (1984) added newterm and set_term.
  • SVr3.1 (1987) supplied delscreen and isendwin.

Look at that glow-up over the decades. 🌸


🌟 SEE ALSO 🌟

  • ncurses(3NCURSES)
  • kernel(3NCURSES)
  • refresh(3NCURSES)
  • slk(3NCURSES)
  • terminfo(3NCURSES)
  • util(3NCURSES)
  • curses_variables(3NCURSES)

And that's the page, bestie!! πŸŽ€ This whole thing is from ncurses 6.6, dated 2025-08-23, and it lives at initscr(3NCURSES). Go forth and initialize your screens responsibly. Mwah. πŸ’–βœ¨

πŸ“¦ where this page comes from