kernel(3NCURSES)

💖 kernel — low-level curses routines (from the 3NCURSES manual, bestie) 💖

okay so. this is the kernel(3NCURSES) page from the curses library docs, and it's literally the backstage-pass section. these are the lil gremlins that do the actual terminal wrangling so the pretty stuff upstairs can vibe. let's get into it 🫶


✨ NAME ✨

def_prog_mode, def_shell_mode, reset_prog_mode, reset_shell_mode, resetty, savetty, getsyx, setsyx, curs_set, mvcur, napms, ripoffline — the low-level curses routines, periodt 💅


✨ SYNOPSIS ✨

#include <curses.h>

int def_prog_mode(void);
int def_shell_mode(void);

int reset_prog_mode(void);
int reset_shell_mode(void);

int resetty(void);
int savetty(void);

void getsyx(int y, int x);
void setsyx(int y, int x);

int curs_set(int visibility);
int mvcur(int oldrow, int oldcol, int newrow, int newcol);
int napms(int ms);
int ripoffline(int line, int (*init)(WINDOW *, int));

✨ DESCRIPTION ✨

so these routines give you low-level access to a whole bunch of curses capabilities. and when I say low-level I mean it — these are the ones that typically get used inside library routines. they're the stage crew, not the main characters. but they ate, fr fr. 🔥

🌸 def_prog_mode, def_shell_mode

def_prog_mode and def_shell_mode save the current terminal modes as the “program” (in curses) or “shell” (not in curses) state, so that reset_prog_mode and reset_shell_mode can use them later. this happens automatically via initscr. and bestie, listen — there's one such save area for each screen context allocated by newterm. one per context. no sharing. 🎀

🌸 reset_prog_mode, reset_shell_mode

reset_prog_mode and reset_shell_mode restore the terminal to “program” (in curses) or “shell” (out of curses) state. these get done automatically by endwin(3NCURSES) and, after an endwin, by doupdate, so like... you normally are not calling these. they're already handled. 😌

🌸 resetty, savetty

resetty and savetty save and restore the state of the terminal modes. savetty saves the current state in a buffer, and resetty restores the state to whatever it was at the last call to savetty. it's giving save-point energy 🦋

🌸 getsyx

getsyx stores the coordinates of the virtual screen (newscr) cursor in y and x. if newscr's leaveok(3NCURSES) output option is TRUE, then getsyx stores -1 in both y and x. and if lines have been removed from the top of the screen using ripoffline, then y includes these lines; therefore, the y and x that getsyx populates should be used only as arguments for setsyx. okay? only for that. don't be doing extra with them.

few applications use this feature; most call getyx(3NCURSES) instead.

🌸 setsyx

setsyx sets the virtual screen (newscr) cursor location to (y, x). and setsyx(-1, -1) is equivalent to leaveok(newscr, TRUE).

getsyx and setsyx are designed to be used by a function that manipulates curses windows but is trying to avoid changing the cursor position. such a function would first call getsyx, modify its windows' content, call wnoutrefresh(3NCURSES) on them, call setsyx, and then call doupdate(3NCURSES). like a lil choreography, honestly iconic 💅

few applications use this feature; most call wmove(3NCURSES) instead.

🌸 curs_set

curs_set adjusts the cursor visibility to “invisible”, “visible”, or “very visible”, depending on whether its argument is 0, 1, or 2, respectively. it returns the previous visibility if the requested one is supported, and ERR otherwise. simple as 🎀

🌸 mvcur

mvcur provides low-level cursor motion. and it takes effect immediately, rather than at the next refresh. unlike the other low-level output functions, which either write to the standard output stream or get passed a function pointer to perform output, mvcur uses a file descriptor derived from the output stream parameter of newterm(3NCURSES).

one application of mvcur accompanies the temporary use of another program to write to the terminal screen. for example, like:

  1. first call refresh(3NCURSES) to make sure the screen and the library's model of it are up to date;
  2. then call reset_shell_mode;
  3. write to the screen with the external application;
  4. call reset_prog_mode;
  5. and finally call mvcur(..., ..., -1, -1) to move the terminal cursor to where curses thinks it is — because the library has no knowledge of how the external application moved it. the audacity of that external app, honestly 😭

🌸 napms

napms sleeps for ms milliseconds. if ms exceeds 30,000 (thirty seconds), ncurses caps it at that value. lil nap, but not too much nap 😴

🌸 ripoffline

ripoffline provides access to the same facility that slk_init(3NCURSES) uses to reduce the size of the screen. the application must call ripoffline before initscr(3NCURSES) or newterm(3NCURSES) so that the latter functions prepare a stdscr of the correct size. timing matters bestie ⏰

  • if line is positive, ripoffline removes a line from the top of what will become stdscr.
  • if line is negative, ripoffline removes a line from the bottom of what will become stdscr.

when initscr initializes curses, it calls the init function supplied to ripoffline by the application with two arguments:

  • a pointer to the one-line WINDOW that it allocates, and
  • an integer with the number of columns in the window.

inside this init function, the values of the integer variables LINES and COLS (see curses_variables(3NCURSES)) are not guaranteed to be reliable; it must not call wrefresh(3NCURSES) or doupdate(3NCURSES). a wnoutrefresh(3NCURSES) call is permissible. (only that one!! 🫶)

ripoffline can be called up to five times before initscr or newterm.


✨ RETURN VALUE ✨

except for curs_set, these functions return OK on success and ERR on failure.

curs_set returns the previous cursor visibility, and returns ERR if the terminal type does not support the requested visibility.

napms always succeeds. slay ✨

mvcur fails if the position (newrow, newcol) is outside the screen boundaries.

in ncurses:

  • def_prog_mode, def_shell_mode, reset_prog_mode, and reset_shell_mode return ERR if the terminal was not initialized, or if the operating system's function for obtaining terminal settings fails.
  • ripoffline returns ERR if the accumulated quantity of ripped-off lines would exceed the maximum (5).

✨ NOTES ✨

getsyx is a macro; use of the & operator before its arguments is unnecessary. like, don't. 🙈

the endwin function of both ncurses and SVr4 curses calls curs_set if the latter has previously been called to set the cursor visibility to a value other than normal; that is, either invisible or very visible. there is no way for ncurses to determine the initial cursor visibility to restore it. it simply does not know. tragic, but true 💀

ncurses imposes a limit of 30 seconds on a delay requested of napms.

while the init function called by ripoffline is specified to return an int, ncurses pays no attention to its return value. do with that what you will.

if ripoffline cannot allocate memory for the required WINDOW structure backing the ripped-off line, it stores a null pointer to the WINDOW pointer argument supplied by the init function the application specifies. the application must check this argument for validity after calling initscr and prior to performing curses operations on that window. check it!! 🫶


✨ EXTENSIONS ✨

in ncurses, mvcur accepts -1 for either or both old coordinates. this value tells ncurses that the old location is unknown, and that it must use only absolute motion, as with the cursor_address (cup) capability, rather than the least costly combination of absolute and relative motion.


✨ PORTABILITY ✨

applications employing ncurses extensions should condition their use on the visibility of the NCURSES_VERSION preprocessor macro.

the virtual screen functions setsyx and getsyx are not described in X/Open Curses Issue 4. SVr4 documents each of them as returning an int. this is misleading, as they are macros with no documented semantics for returning values. 😭

all other functions are as described in X/Open Curses. it specifies no error conditions for them, except as described for curs_set in section “RETURN VALUE” above.

the System V Interface Definition, Version 4 (1995), specified all of these functions except curs_set as returning OK.

older SVr4 man pages warn that the return value of curs_set “is currently incorrect”. this implementation gets it right, but counting on its correctness anywhere else may be unwise. iykyk 💅

X/Open Curses specifies ripoffline as returning OK with no possibility of failure (“[c]alls to ripoffline above this limit [five lines] have no effect but report success”).

X/Open Curses notes:

after use of mvcur(), the model Curses maintains of the state of the terminal might not match the actual state of the terminal. An application should touch and refresh the window before resuming conventional use of Curses.

both ncurses and SVr4 curses implement mvcur using the SCREEN object allocated in either initscr(3NCURSES) or newterm(3NCURSES). X/Open Curses states that the old location must be given for mvcur to accommodate terminals that lack absolute cursor positioning.

if interrupted by a signal, ncurses restarts napms. that, and the limitation to 30 seconds, differ from other implementations.


✨ SEE ALSO ✨

  • ncurses(3NCURSES)
  • initscr(3NCURSES)
  • outopts(3NCURSES)
  • refresh(3NCURSES)
  • scr_dump(3NCURSES)
  • slk(3NCURSES)
  • curses_variables(3NCURSES)

and that's the whole page bestie!! 🎀 this was ncurses version 6.6, dated 2025-08-16, from the kernel(3NCURSES) manual. low-level routines, high-level drama, absolutely iconic. go forth and rip off some lines responsibly 👑💖

📦 where this page comes from