The Single UNIX ® Specification, Version 2
Copyright © 1997 The Open Group

 Interface Overview


A Curses initialisation function, usually initscr(), determines the terminal model in use, by reference to either an argument or an environment variable. If that model is defined in terminfo, then the same terminfo entry tells Curses exactly how to operate the terminal.

In this case, a comprehensive API lets the application perform terminal operations. The Curses run-time system receives each terminal request and sends appropriate commands to the terminal to achieve the desired effect.

 Relationship to the XBD Specification
Applications using Curses should not also control the terminal using capabilities of the general terminal interface defined in the XBD specification, General Terminal Interface  .

There is no requirement that the paradigms that exist while in Curses mode be carried over outside the Curses environment (see def_prog_mode ()).

 Relationship to Signals
Curses implementations may provide for special handling of the SIGINT, SIGQUIT and SIGTSTP signals if their disposition is SIGDFL at the time initscr() is called (see initscr ()).

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 (see sigaction() in the XSH specification).

The behaviour of Curses with respect to signals not defined by the XSH specification is unspecified.

 Screens, Windows and Terminals

A screen is the physical output device of the terminal. In Curses, a SCREEN data type is an opaque data type associated with a terminal. Each window (see below) is associated with a SCREEN.
The Curses functions permit manipulation of window objects, which can be thought of as two-dimensional arrays of characters and their renditions. A default window called stdscr, which is the size of the terminal screen, is supplied. Others may be created with newwin().

Variables declared as WINDOW * refer to windows (and to subwindows, derived windows, and pads, as described below). These data structures are manipulated with functions described on the reference manual pages in Terminfo Source Format (ENHANCED CURSES) . Among the most basic functions are move() and addch(). More general versions of these functions are included that allow a process to specify a window.

After using functions to manipulate a window, refresh() is called, telling Curses to make the CRT screen look like stdscr.

Line drawing characters may be specified to be output. On input, Curses is also able to translate arrow and function keys that transmit escape sequences into single values. The line drawing characters and input values use names defined in <curses.h>.

Each window has a flag that indicates that the information in the window could differ from the information displayed on the terminal device. Making any change to the contents of the window, moving or modifying the window, or setting the window's cursor position, sets this flag (touches the window). Refreshing the window clears this flag. (For further information, see Synchronous and Networked Asynchronous Terminals .)

A subwindow is a window, created within another window (called the parent window), and positioned relative to the parent window. A subwindow can be created by calling derwin(), newpad() or subwin().

Subwindows can be created from a parent window by calling subwin(). The position and size of subwindows on the screen must be identical to or totally within the parent window. Changes to either the parent window or the subwindow affect both. Window clipping is not a property of subwindows.

The term ancestor refers to a window's parent, or its parent, and so on.
 Derived Windows
Derived windows are subwindows whose position is defined by reference to the parent window rather than in absolute screen coordinates. Derived windows are otherwise no different from subwindows.
A pad is a specialised case of subwindow that is not necessarily associated with a viewable part of a screen. Functions that deal with pads are all discussed in Synchronous and Networked Asynchronous Terminals .
A terminal is the logical input and output device through which character-based applications interact with the user. TERMINAL is an opaque data type associated with a terminal. A TERMINAL data structure primarily contains information about the capabilities of the terminal, as defined by terminfo. A TERMINAL also contains information about the terminal modes and current state for input and output operations. Each screen (see above) is associated with a TERMINAL.


 Character Storage Size

Historically, a position on the screen has corresponded to a single stored byte. This correspondence is no longer true for several reasons:

The internal storage format of characters and renditions is unspecified. There is no implied correspondence between the internal storage format and the external representation of characters and renditions in objects of type chtype and cchar_t.

 Multi-column Characters

Some character sets define multi-column characters that occupy more than one column position when displayed on the screen.

Writing a character whose width is greater than the width of the destination window is an error.


Each character can be displayed with attributes such as underlining, reverse video or colour on terminals that support such display enhancements. Current attributes of a window are applied to all characters that are written into the window with waddch(), wadd_wch(), waddstr(), waddchstr(), waddwstr(), wadd_wchstr() and wprintw(). Attributes can be combined.

Attributes can be specified using constants with the A_ prefix specified in <curses.h>. The A_ constants manipulate attributes in objects of type chtype.  Additional attributes can be specified using constants with the WA_ prefix. The WA_ constants manipulate attributes in objects of type attr_t.

Two constants that begin with A_ and WA_ and that represent the same terminal capability refer to the same attribute in the terminfo database and in the window data structure. The effect on a window does not differ depending on whether the application specifies A_ or WA_ constants. For example, when an application updates window attributes using the interfaces that support the A_ values, a query of the window attribute using the function that returns WA_ values reflects this update. When it updates window attributes using the interfaces that support the WA_ values, for which corresponding A_ values exist, a query of the window attribute using the function that returns A_ values reflects this update.


The rendition of a character displayed on the screen is its attributes  and a colour pair.

The rendition of a character written to the screen becomes a property of the character and moves with the character through any scrolling and insert/delete line/character operations. To the extent possible on a particular terminal, a character's rendition corresponds to the graphic rendition of the character put on the screen.

If a given terminal does not support a rendition that an application program is trying to use, Curses may substitute a different rendition for it.

Colours are always used in pairs (referred to as colour-pairs). A colour-pair consists of a foreground colour (for characters) and a background colour (for the field on which the characters are displayed).

 Non-spacing Characters

The requirements in this section are in effect only for implementations that claim Enhanced Curses compliance.

Some character sets may contain non-spacing characters. (Non-spacing characters are those, other than the '\0' character, for which wcwidth() returns a width of zero.) The application may write non-spacing characters to a window. Every non-spacing character in a window is associated with a spacing character and modifies the spacing character. Non-spacing characters in a window cannot be addressed separately. A non-spacing character is implicitly addressed whenever a Curses operation affects the spacing character with which the non-spacing character is associated.

Non-spacing characters do not support attributes. For interfaces that use wide characters and attributes, the attributes are ignored if the wide character is a non-spacing character. Multi-column characters have a single set of attributes for all columns. The association of non-spacing characters with spacing characters can be controlled by the application using the wide character interfaces. The wide character string functions provide codeset-dependent association.

Two typical effects of a non-spacing character associated with a spacing character called c, are as follows:

Implementations may limit the number of non-spacing characters that can be associated with a spacing character, provided any limit is at least 5.

 Complex Characters
A complex character is a set of associated characters, which may include a spacing character and may include any non-spacing characters associated with it. A spacing complex character is a spacing character followed by any non-spacing characters associated with it. That is, a spacing complex character is a complex character that includes one spacing character. An example of a code set that has complex characters is ISO/IEC 10646-1:1993.

A complex character can be written to the screen; if it does not include a spacing character, any non-spacing characters are associated with the spacing complex character that exists at the specified screen position. When the application reads information back from the screen, it obtains spacing complex characters.

The cchar_t data type represents a complex character and its rendition. When a cchar_t represents a non-spacing complex character (that is, when there is no spacing character within the complex character), then its rendition is not used; when it is written to the screen, it uses the rendition specified by the spacing character already displayed.

An object of type cchar_t can be initialised using setcchar() and its contents can be extracted using getcchar(). The behaviour of functions that take a cchar_t input argument is undefined if the application provides a cchar_t value that was not initialised in this way or obtained from a Curses function that has a cchar_t output argument.

 Window Properties

Associated with each window are the following properties that affect the placing of characters into the window (see Rendition of Characters Placed into a Window ).
 Window Rendition
Each window has a rendition, which is combined with the rendition component of the window's background property described below.
 Window Background
Each window has a background property. The background property specifies:

 Conceptual Operations

 Screen Addressing

Many Curses functions use a coordinate pair. In the DESCRIPTION, coordinate locations are represented as (y, x) since the y argument always precedes the x argument in the function call. These coordinates denote a line/column position, not a character position.

The coordinate y always refers to the row (of the window), and x always refers to the column. The first row and the first column is number 0, not 1. The position (0, 0) is the window's origin.

For example, for terminals that display the ISO 8859-1 character set (with left-to-right writing), (0, 0) represents the upper left-hand corner of the screen.

Functions that start with mv take arguments that specify a (y, x) position and move the cursor (as though move() were called) before performing the requested action. As part of the requested action, further cursor movement may occur, specified on the respective reference manual page.

 Basic Character Operations

 Adding (Overwriting)
The Curses functions that contain the word add, such as addch(), actually specify one or more characters to replace (overwrite) characters already in the window. If these functions specify only non-spacing characters, they are appended to a spacing character already in the window; see also Non-spacing Characters .

When replacing a multi-column character with a character that requires fewer columns, the new character is added starting at the specified or implied column position. All columns that the former multi-column character occupied that the new character does not require are orphaned columns, which are filled using the background character and rendition.

Replacing a character with a character that requires more columns also replaces one or more subsequent characters on the line. This process may also produce orphaned columns.

 Truncation, Wrapping and Scrolling

If the application specifies a character or a string of characters such that writing them to a window would extend beyond the end of the line (for example, if the application tries to deposit any multi-column character at the last column in a line),  the behaviour depends on whether the function supports line wrapping:

Some add functions move the cursor just beyond the end of the last character added. If this position is beyond the end of a line, it causes wrapping and scrolling under the conditions specified in the second bullet above.

Insertion functions (such as insch()) insert characters immediately before the character at the specified or implied cursor position.

The insertion shifts all characters that were formerly at or beyond the cursor position on the cursor line toward the end of that line. The disposition of the characters that would thus extend beyond the end of the line depends on whether the function supports wrapping:

If multi-column characters are displayed, some cursor positions are within a multi-column character but not at the beginning of a character. Any request to insert data at a position that is not the beginning of a multi-column character will be adjusted so that the actual cursor position is at the beginning of the multi-column character in which the requested position occurs.

There are no warning indications relative to cursor relocation. The application should not maintain an image of the cursor position, since this constitutes placing terminal-specific information in the application and defeats the purpose of using Curses.

Portable applications cannot assume that a cursor position specified in an insert function is a reusable indication of the actual cursor position.

Deletion functions (such as delch()) delete the simple  or complex  character at the specified or implied cursor position,  no matter which column of the character this is.  All column positions are replaced by the background character and rendition and the cursor is not relocated. If a character-deletion operation would cause a previous wrapping operation to be undone, then the results are unspecified.
 Window Operations
Overlapping a window (that is, placing one window on top of another) and overwriting a window (that is, copying the contents of one window into another) follows the operation of overwriting  multi-column  glyphs around its edge. Any orphaned columns are handled as in the character operations.
 Characters that Straddle the Subwindow Border
A subwindow can be defined such that multi-column characters straddle the subwindow border. The character operations deal with these straddling characters as follows:

If the application calls a function such as border(), the above situations do not occur because writing the border on the subwindow deletes any straddling characters.

In the above cases involving multi-column characters, operations confined to a subwindow can modify the screen outside the subwindow. Therefore, saving a subwindow, performing operations within the subwindow, and then restoring the subwindow may disturb the appearance of the screen. To overcome these effects (for example, for pop-up windows), the application should refresh the entire screen.

 Special Characters

Some functions process special characters as specified below.

In functions that do not move the cursor based on the information placed in the window, these special characters would only be used within a string in order to affect the placement of subsequent characters; the cursor movement specified below does not persist in the visible cursor beyond the end of the operation. In functions that do move the cursor, these special characters can be used to affect the placement of subsequent characters and to achieve movement of the visible cursor.

Unless the cursor was already in column 0, <backspace> moves the cursor one column toward the start of the current line and any characters after the <backspace> are added or inserted starting there.
<carriage return>

Unless the cursor was already in column 0, <carriage return> moves the cursor to the start of the current line. Any characters after the <carriage return> are added or inserted starting there.
In an add operation, Curses adds the background character into successive columns until reaching the end of the line. Scrolling occurs as described in Truncation, Wrapping and Scrolling . Any characters after the <newline> character are added, starting at the start of the new line. In an insert operation, <newline> erases the remainder of the current line with the background character, effectively a wclrtoeol(), and moves the cursor to the start of a new line. When scrolling is enabled, advancing the cursor to a new line may cause scrolling as described in Truncation, Wrapping and Scrolling . Any characters after the <newline> character are inserted at the start of the new line. The filter() function may inhibit this processing.
Tab characters in text move subsequent characters to the next horizontal tab stop. By default, tab stops are in column 0, 8, 16, and so on. In an insert or add operation, Curses inserts or adds, respectively, the background character into successive columns until reaching the next tab stop. If there are no more tab stops in the current line, wrapping and scrolling occur as described in Truncation, Wrapping and Scrolling .
 Control Characters
The Curses functions that perform special-character processing conceptually convert control characters to the caret ('^') character followed by a second character (which is an upper-case letter if it is alphabetic) and write this string to the window in place of the control character. The functions that retrieve text from the window will not retrieve the original control character.

 Rendition of Characters Placed into a Window

When the application adds or inserts characters into a window, the effect is as follows:

If the character is not the space character, then the window receives:

If the character is the space character, then the window receives:

 Input Processing

The Curses input model provides a variety of ways to obtain input from the keyboard.

 Keypad Processing

The application can enable or disable keypad translation by calling keypad(). When translation is enabled, Curses attempts to translate a sequence of terminal input that represents the pressing of a function key into a single key code. When translation is disabled, Curses passes terminal input to the application without such translation, and any interpretation of the input as representing the pressing of a keypad key must be done by the application.

The complete set of key codes for keypad keys that Curses can process is specified by the constants defined in <curses.h> whose names begin with "KEY_".
Each terminal type described in the terminfo database may support some or all of these key codes. The terminfo database specifies the sequence of input characters from the terminal type that correspond to each key code (see ).

The Curses implementation cannot translate keypad keys on terminals where pressing the keys does not transmit a unique sequence.

When translation is enabled and a character that could be the beginning of a function key (such as escape) is received, Curses notes the time and begins accumulating characters. If Curses receives additional characters that represent the pressing of a keypad key, within an unspecified interval from the time the first character was received, then Curses converts this input to a key code for presentation to the application. If such characters are not received during this interval, translation of this input does not occur and the individual characters are presented to the application separately. (Because Curses waits for this interval to accumulate a key code, many terminals experience a delay between the time a user presses the escape key and the time the escape is returned to the application.)

In addition, No Timeout Mode provides that in any case where Curses has received part of a function key sequence, it waits indefinitely for the complete key sequence. The "unspecified interval" in the previous paragraph becomes infinite in No Timeout Mode. No Timeout Mode allows the use of function keys over slow communication lines. No Timeout Mode lets the user type the individual characters of a function key sequence, but also delays application response when the user types a character (not a function key) that begins a function key sequence. For this reason, in No Timeout Mode many terminals will appear to hang between the time a user presses the escape key and the time another key is pressed. No Timeout Mode is switchable by calling notimeout().

If any special characters (see Special Characters ) are defined or redefined to be characters that are members of a function key sequence, then Curses will be unable to recognise and translate those function keys.

Several of the modes discussed below are described in terms of availability of input. If keypad translation is enabled, then input is not available once Curses has begun receiving a keypad sequence until the sequence is completely received or the interval has elapsed.

 Input Mode

The XBD specification (Special Characters) defines flow-control characters, the interrupt character, the erase character, and the kill character. Four mutually-exclusive Curses modes let the application control the effect of these input characters:

Input Mode Effect
Cooked Mode This achieves normal line-at-a-time processing with all special characters handled outside the application. This achieves the same effect as canonical-mode input processing as specified in the XBD specification. The state of the ISIG and IXON flags are not changed upon entering this mode by calling and are set upon entering this mode by calling
  The implementation supports erase and kill characters from any supported locale, no matter what the width of the character is.
cbreak Mode Characters typed by the user are immediately available to the application and Curses does not perform special processing on either the erase character or the kill character. An application can select cbreak mode to do its own line editing but to let the abort character be used to abort the task. This mode achieves the same effect as non-canonical-mode, Case B input processing (with MIN set to 1 and ICRNL cleared) as specified in the XBD specification. The state of the ISIG and IXON flags are not changed upon entering this mode.
Half-Delay Mode The effect is the same as cbreak, except that input functions wait until a character is available or an interval defined by the application elapses, whichever comes first. This mode achieves the same effect as non-canonical-mode, Case C input processing (with TIME set to the value specified by the application) as specified in the XBD specification. The state of the ISIG and IXON flags are not changed upon entering this mode.
Raw Mode Raw mode gives the application maximum control over terminal input. The application sees each character as it is typed. This achieves the same effect as non-canonical mode, Case D input processing as specified in the XBD specification. The ISIG and IXON flags are cleared upon entering this mode.

The terminal interface settings are recorded when the process calls initscr() or newterm() to initialise Curses and restores these settings when endwin() is called. The initial input mode for Curses operations is unspecified  unless the implementation supports Enhanced Curses compliance, in which the initial input mode is cbreak mode.

The behaviour of the BREAK key depends on other bits in the display driver that are not set by Curses.

 Delay Mode

Two mutually-exclusive delay modes specify how quickly certain Curses functions return to the application when there is no terminal input waiting when the function is called:
No Delay
The function fails.
The application waits until the implementation passes text through to the application. If cbreak or Raw Mode is set, this is after one character. Otherwise, this is after the first <newline> character, end-of-line character, or end-of-file character.

The effect of No Delay Mode on function key processing is unspecified.

 Echo Processing

Echo mode determines whether Curses echoes typed characters to the screen. The effect of Echo mode is analogous to the effect of the ECHO flag in the local mode field of the termios structure associated with the terminal device connected to the window. However, Curses always clears the ECHO flag when invoked, to inhibit the operating system from performing echoing. The method of echoing characters is not identical to the operating system's method of echoing characters, because Curses performs additional processing of terminal input.

If in Echo mode, Curses performs its own echoing: Any visible input character is stored in the current or specified window by the input function that the application called, at that window's cursor position, as though addch() were called, with all consequent effects such as cursor movement and wrapping.

If not in Echo mode, any echoing of input must be performed by the application. Applications often perform their own echoing in a controlled area of the screen, or do not echo at all, so they disable Echo mode.

It may not be possible to turn off echo processing for synchronous and networked asynchronous terminals because echo processing is done directly by the terminals. Applications running on such terminals should be aware that any characters typed will appear on the screen at wherever the cursor is positioned.

 The Set of Curses Functions

The Curses functions allow: overall screen, window and pad manipulation; output to windows and pads; reading terminal input; control over terminal and Curses input and output options; environment query functions; colour manipulation; use of soft label keys; access to the terminfo database of terminal capabilities; and access to low-level functions.

 Function Name Conventions

The reference manual pages in present families of multiple Curses functions. Most function families have different functions that give the programmer the following options:

 Function Families Provided

Function Names Description s w c Refer to
  Add (Overwrite)        
[mv][w]addch() add a character Y Y Y addch()
[mv][w]addch[n]str() add a character string N N N addchstr()
[mv][w]add[n]str() add a string Y Y Y addnstr()
[mv][w]add[n]wstr() add a wide character string Y Y Y addnwstr()
[mv][w]add_wch() add a wide character and rendition Y Y Y add_wch()
[mv][w]add_wch[n]str() add an array of wide characters and renditions ? N N add_wchnstr()
  Change Renditions        
[mv][w]chgat() change renditions of characters in a window - N N chgat()
[mv][w]delch() delete a character - - N delch()
  Get (Input from Keyboard to Window)        
[mv][w]getch() get a character Y Y Y getch()
[mv][w]get[n]str() get a character string Y Y Y getnstr()
[mv][w]get_wch() get a wide character Y Y Y get_wch()
[mv][w]get[n]_wstr() get an array of wide characters and key codes Y Y Y get_wstr()
  Explicit Cursor Movement        
[w]move() move the cursor - - - move()
  Input (Read Back from Window)        
[mv][w]inch() input a character - - - inch()
[mv][w]inch[n]str() input an array of characters and attributes - - - inchnstr()
[mv][w]in[n]str() input a string - - - innstr()
[mv][w]in[n]wstr() input a string of wide characters - - - innwstr()
[mv][w]in_wch() input a wide character and rendition - - - in_wch()
[mv][w]in_wch[n]str() input an array of wide characters and renditions - - - inchnstr()
[mv][w]insch() insert a character Y N N insch()
[mv][w]ins[n]str() insert a character string Y N N insnstr()
[mv][w]ins_[n]wstr() insert a wide-character string Y N N ins_nwstr()
[mv][w]ins_wch() insert a wide character Y N N ins_wch()
  Print and Scan        
[mv][w]printw() print formatted output - - - mvprintw()
[mv][w]scanw() convert formatted output - - - mvscanw()

The following notation indicates the effect when characters are moved to the screen. (For the Get functions, this applies only when echoing is enabled.)
Y means these functions perform special-character processing (see Special Characters ). N means they do not. ? means the results are unspecified when these functions are applied to special characters.
Y means these functions perform wrapping (see Truncation, Wrapping and Scrolling ). N means they do not.
Y means these functions advance the cursor (see Truncation, Wrapping and Scrolling ). N means they do not.
The attribute specified by this column does not apply to these functions.

 Interfaces Implemented as Macros

The following interfaces with arguments must be implemented as macros. The relevance to the application programmer is that the `&' character cannot be used before the arguments.

Macros Chapter 4 Entry
getbegyx(), getmaxyx(), getparyx(), getyx() getbegyx()

The header file reference manual pages list other macros, like COLOR_BLACK, that do not take arguments.

 Initialised Curses Environment

Before executing an application that uses Curses, the terminal must be prepared as follows:

The resulting state of the terminal must be compatible with the model of the terminal that Curses has, as reflected in the terminal's entry in the terminfo database (see Terminfo Source Format (ENHANCED CURSES) ).

To initialise Curses, the application must call initscr() or newterm() before calling any of the other functions that deal with windows and screens, and it must call endwin() before exiting. To get character-at-a-time input without echoing (most interactive, screen-oriented programs want this), the following sequence should be used:

initscr() cbreak() noecho()

Most programs would additionally use the sequence:

nonl() intrflush(stdscr, FALSE) keypad(stdscr, TRUE)

 Synchronous and Networked Asynchronous Terminals

This section indicates to the application writer some considerations to be borne in mind when driving synchronous, networked asynchronous (NWA) or non-standard directly-connected asynchronous terminals.

Such terminals are often used in a mainframe environment and communicate to the host in block mode. That is, the user types characters at the terminal then presses a special key to initiate transmission of the characters to the host.

Frequently, although it may be possible to send arbitrary sized blocks to the host, it is not possible or desirable to cause a character to be transmitted with only a single keystroke.

This can cause severe problems to an application wishing to make use of single-character input; see Input Processing .

The Curses interface can be used in the normal way for all operations pertaining to output to the terminal, with the possible exception that on some terminals the refresh() routine may have to redraw the entire screen contents in order to perform any update.

If it is additionally necessary to clear the screen before each such operation, the result could be undesirable.

Because of the nature of operation of synchronous (block-mode) and NWA terminals, it might not be possible to support all or any of the Curses input functions. In particular, the following points should be noted:

UNIX ® is a registered Trademark of The Open Group.
Copyright © 1997 The Open Group
[ Main Index | XSH | XCU | XBD | XCURSES | XNS ]