Ncurses Manual
Ncurses Manual
If you have some beginning experience in Unix programming, you may have felt the need of some text
user interface operations, such as moving the cursor on the screen, editing user input, using colors, ...
Such terminal IO related operations are not portable and not defined in C language. You need to either
use the low-level termcap library or the curses library. Using curses/ncurses library is much easier and
more portable.
Contents
Using the library
Creating windows
Creating windows
Input
Output
To compile your C/C++ programs using ncurses/curses library you need to include the curses header file
<curses.h>. For ncurses, you may include either <curses.h> or <ncurses.h>. In some systems, you
must include <ncurses.h>.
#include <curses.h>
To link the programs you need to use the -lcurses or -lncurses option, like
gcc -lncurses prog.c
This way the program is dynamically linked to the ncurses library. To run it in another computer, the
system must have the ncurses library installed. If you want to avoid the trouble, you may have it
statically linked. To do that, find the file libncurses.a in /usr/lib and do
gcc prog.c libncurses.a
Most Unix systems have curses or ncurses installed as a default option. To find out if it's installed, you
can try man ncurses man curses or go to /usr/lib and /usr/include to list the files.
If you're installing Linux or FreeBSD on your own machine, be sure to install the ncurses-development
package in order to do ncurses programming.
Initialization
The very first thing to do: Before you use any other curses routines, the initscr() routine must be
called first.
initscr();
If your program is going to write to several terminals, you should call newterm instead, which is another
story.
One-character-a-time. To disable the buffering of typed characters by the TTY driver and get a
character-at-a-time input, you need to call
cbreak();
No echo. To suppress the automatic echoing of typed characters, you need to call
noecho();
Special keys. In order to capture special keystrokes like Backspace, Delete and the four arrow keys by
getch(), you need to call
keypad(stdscr, TRUE);
Before exiting. Before the program is terminated, endwin() must be called to restore the terminal
settings.
Windows
A window is a 2-dimensional array of characters representing all or part of a CRT screen. Character
input and output should pertain to a specific window.
The default window. A default window called stdscr, which is the size of the terminal screen, is
supplied. To use the stdscr window, you don't need to do any initializations. You can also divide the
screen to several parts and create a window to represent each part.
www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html#window 2/9
11/26/2020 Ncurses programming guide
Create a new window. The data structure of window is WINDOW, defined in ncurses.h. To declare and
create a new window, do
WINDOW * win = newwin(nlines, ncols, y0, x0);
All the 4 parameters are ints. Here nline is the height of the window -- number of lines, ncols is the
width -- number of columns of the window. y0 and x0 are the coordinates of the upper left corner of win
on the screen -- line y0 and columns x0. You should make sure that the area of the new window is inside
the screen.
Height and width of the window. The size of the whole screen can be determined by the two global
variables COLUMNS and LINES. y0 and x0 should satisfy
In X window system, the actual xterm size might be changed leaving these two variables obslete. In this
case you should use the macro void getmaxyx(WINDOW *, int y, int x) to get the size of the screen.
int h, w;
getmaxyx(stdscr, h, w);
No overlapping. Windows cannot overlap with each other. Therefore you have two options: only use
stdscr and no other windows, or create several non-overlapping windows but do not use stdscr.
Refresh. If you make some change to a window, such as printing something or moving the cursor, the
effect is not shown on the screen until you call the wrefresh() function
wrefresh(win);
Clear window. To erase everything written in the window win, call wrefresh(win). refresh() is
equivalent to wrefresh(stdscr).
Delete window. If a window win is no longer needed, and you're going to create new windows to
overlap it, you should call delwin(win) to delete the window (release the memory it is using).
www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html#window 3/9
11/26/2020 Ncurses programming guide
The position of the cursor on the screen is important because it is default beginning place for most output
functions. The cursor also shows the user where the input is expected.
To move the cursor to a new position on a window, use the function int wmove(WINDOW *win, int y,
int x)
wmove(win, y, x);
where (x, y) are the coordinates of the new position in the window. If the window has nlines lines and
ncolumns columns, then
Refresh. The actual cursor motion is not shown on the screen untill you do a wrefresh(win).
Input
To read a character from stdscr, use the function int getch(void).
int ch = getch();
No echoing. If you have called noecho(), the character ch will not be printed on the screen, otherwise it
will. Disabling automatic echoing gives you more control over the user interface.
No buffering. If you have called cbreak(void) each key the user hits is returned immediately by
getch(). Otherwise the keys hit by the user are queued until a newline is read. Then calls to getch()
take characters from the queue in FIFO manner until the queue is empty and the next whole line is read.
No delaying. Usually a call to getch() waits until a key is hit. If you have called nodelay(stdscr,
TRUE), then getch() will work in a non-blocking manner -- it will return ERR if the key input is not
ready. This is usually useful for writing game-like programs, where the promptness of user response
matters. For example
int ch;
nodelay(stdscr, TRUE);
for (;;) {
if ((ch = getch()) == ERR) {
/* user hasn't responded
...
*/
}
else {
/* user has pressed a key ch
...
*/
}
}
Special keys. If you have called keypad(stdstr, TRUE), then if the user hits a special key such as the
Delete key, the arrow keys, Ctrl combined keys and function keys, a single int value will be returned.
Here is the definition of several special keys
Catch special keys. To use these keys, you need to check the return value of getch(). For example
int ch = getch();
switch (ch) {
case KEY_BACKSPACE: /* user pressed backspace */
...
case KEY_UP: /* user pressed up arrow key */
...
case KEY_DOWN: /* user pressed up arrow key */
...
case 'A' .... /* user pressed key 'A' */
...
}
Read character from a window. The function int wgetch(WINDOW *win). reads a key from a window.
The user input of course comes from the keyboard and not the screen window. But the different windows
on the screen might have different delay modes and other properties, therefore affect the behavior of
wgetch().
Moving the cursor and read a character. There are also functions which combine cursor moving and
character reading together
Output
The function int waddch(WINDOW * win, chtype ch) adds a character on the window at the current
cursor position, and the cursor position is advanced then.
Wrap. If the new position of the cursor is out of the window, it wraps to the beginning of the next line.
Scroll. If the next line is out of the window, and you have called scrollok(win, TRUE) when the
window was created, the stuff in the window is scrolled up one line.
Character attribute. The parameter ch is of type chtype(), which is the ASCII value of the character
combined with some video attributes such as colors. The combination is through the logical OR of the
character value and the attribute, which I will talk about in the section.
Refresh. After a call to waddch, the screen is not updated until you call wrefresh(win).
www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html#window 5/9
11/26/2020 Ncurses programming guide
int waddstr(WINDOW *win, const char *str) and int addstr(const char *str) prints a null-
terminated string at the cursor position of the window, and advance the cursor position accordingly.
The functions int wprintw(WINDOW *win, char *fmt ...) and int printw(char *fmt ...) do
formatted output in the same fashion as the analogous standard library function printf.
Attribute
When characters are drawn on the screen some special video effects, like foreground and background
color, highlight, underline, blinking, ..., can be shown. Such video effects are represented by integers
called text attributes. Each significant bit of the attribute corresponds to one video effect.
Using attribute
There are two ways to use attribute. One is by passing waddch(win, ch) a character value combined
with attribute. The other is setting the global window attribute.
Character type. When calling waddch(win, ch) or addch(ch), logical OR the character value with the
attribute. For example, A_UNDERLINE is the predefined attribute for underlining. To print the character 'X'
with underlining, do
Using several attributes is of course possible. For example, to To print the character 'X' with highlight in
color pair 3
Setting window attribute. int wattron(WINDOW *win, int attr) function to turn on an attribute
attr. Then anything printed by subsequent calls to waddch, addstr and waddstr will have the attribute
attr, For example, to print a highlighted message on the screen
attron(A_STANDOUT);
addstr("I am highlighted!\n");
Using colors
www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html#window 6/9
11/26/2020 Ncurses programming guide
The combination of foreground and background color is an attribute. Unlike other attributes, before
using colors, you must call start_color().
When start_color() is called, a set of colors and color pairs are created which you can use. The
number of available colors and the number of the color pairs are stored in two global variables COLORS
and COLOR_PAIRS. To use an predefined color pair as an attribute, you need to cal the macro
COLOR_PAIR(n), where n must satisfy
Example. To give a window the color attribute defined by color pair #2, so that each subsequent
character printed in this window has the foreground and background color defined by color pair #2
wattron(win, COLOR_PAIR(2));
init_pair(1,2,0);
redefine the color pair #1 with foreground color #2 and background color #0. In the function int
init_pair(short n, short f, short b) the parameters must satisfy
COLOR_BLACK
COLOR_RED
COLOR_GREEN
COLOR_YELLOW
COLOR_BLUE
COLOR_MAGENTA
COLOR_CYAN
COLOR_WHITE
You can use these names in init_pair() for specifying foreground and background color.
To find out what foreground color and background color is used by a color pair, use the function int
pair_content(short pair, short *f, short *b). To find out the definition of a color use the
function int color_content(short color, short *r, short *g, short *b)
Color can also be redefined by int init_color(short n, short r, short g, short b), where n is
the index of color, must be less than COLORS. r, g, and b represent the intensity of red, green and blue.
Each value of r, g and b must be less than 1000.
Line graphics
Line graphics. Here are some special characters which can be used in addch and addstr routines as the
chtype.
ACS_BLOCK solid square block
ACS_BOARD board of squares
ACS_BTEE bottom tee
ACS_BULLET bullet
ACS_CKBOARD checker board (stipple)
ACS_DARROW arrow pointing down
ACS_DEGREE degree symbol
ACS_DIAMOND diamond
www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html#window 7/9
11/26/2020 Ncurses programming guide
ACS_GEQUAL greater-than-or-equal-to
ACS_HLINE horizontal line
ACS_LANTERN lantern symbol
ACS_LARROW arrow pointing left
ACS_LEQUAL less-than-or-equal-to
ACS_LLCORNER lower left-hand corner
ACS_LRCORNER lower right-hand corner
ACS_LTEE left tee
ACS_NEQUAL not-equal
ACS_PI greek pi
ACS_PLMINUS plus/minus
ACS_PLUS plus
ACS_RARROW arrow pointing right
ACS_RTEE right tee
ACS_S1 scan line 1
ACS_S3 scan line 3
ACS_S7 scan line 7
ACS_S9 scan line 9
ACS_STERLING pound-sterling symbol
ACS_TTEE top tee
ACS_UARROW arrow pointing up
ACS_ULCORNER upper left-hand corner
ACS_URCORNER upper right-hand corner
ACS_VLINE vertical line
Usually on terminals using these symbols can draw pretty windows and shapes. One place to use this is
the wborder function, which draws borders for a window. See the man page for details about the
parameters, but usually do it the following way
wborder(win, 0, 0, 0, 0, 0, 0, 0, 0);
X window
If an xterm is resized the contents on your text windows might be messed up. To handle this gracefully
you should redraw all the stuff based on the new height and width of the screen. When resizing happens,
your program is sent a SIGWINCH signal. You should catch this signal and do redrawing accordingly. Here
is some hint.
#include <signal.h>
void* resizeHandler(int);
int main(void) {
...
signal(SIGWINCH, resizeHandler);
...
}
www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html#window 8/9
11/26/2020 Ncurses programming guide
X. Li's Homepage
www.cs.ukzn.ac.za/~hughm/os/notes/ncurses.html#window 9/9