COHERENT manpages
This page displays the COHERENT manpage for libcurses [Library of screen-handling functions].
List of available manpages
Index
libcurses -- Library
Library of screen-handling functions
libcurses is the library that holds the curses screen-handling functions.
With curses, you can perform rudimentary graphics, even on dumb terminals;
the range of routines includes mapping portions of the screen, drawing pop-
up windows, creating forms with fields for data entry, and highlighting
portions of text.
Implementations of curses
COHERENT uses the Cornell edition of curses. This implementation of curses
reads the terminfo data base. It uses eight-bit characters; thus, the
COHERENT edition of curses can display characters with accents and
diacritical marks. Library libcurses contains the functions needed to read
terminfo capability codes; thus, to compile the program curses_ex.c, use
the following command line:
cc curses_ex.c -lcurses
Programs that wish to use curses must not link in both libcurses and
libterm; doing so will cause collisions among library routines. Rather,
these programs must link in only libcurses. On the other hand, if you wish
to use the functions that read terminfo descriptions, you must link library
libcurses into your program, even if you are not using any curses routines.
If you have special terminal descriptions under termcap, the command
captoinfo converts a termcap description into its terminfo analogue.
See the Lexicon entries for termcap and terminfo for more information on
this rather confusing topic.
How curses Works
curses organizes the screen into a two-dimensional array of cells, one cell
for every character that the device can display. It maintains in memory an
image of the screen, called the curscr. A second image, called the stdcur,
is manipulated by the user; when the user has finished a given
manipulation, curses copies the changes from the stdcur to the curscr,
which results in their being displayed on the physical screen. This act of
copying from the stdscr to the curscr is called refreshing the screen.
curses keeps track of where all changes have begun and ended between one
refresh and the next; this lets it rewrite only the portions of the curscr
that the user has changed, and so speed up rewriting of the screen.
curses records the position of a ``logical cursor'', which points to the
position in the stdscr that is being manipulated by the user, and also
records the position of the physical cursor. Note that the two are not
necessarily identical: it is possible to manipulate the logical cursor
without repositioning the physical cursor, and vice versa, depending on the
task you wish to perform.
Most curses routines work by manipulating a WINDOW object. WINDOW is
defined in the header curses.h.
curses defines WINDOW as follows:
#define WINDOW _win_st
struct _win_st {
short _cury, _curx;
short _maxy, _maxx;
short _begy, _begx;
short _flags;
chtype _attrs;
bool _clear;
bool _leave;
bool _scroll;
bool _idlok;
bool _use_keypad;/* 0=no, 1=yes, 2=yes/timeout */
bool _use_meta;/* T=use the meta key */
bool _nodelay;/* T=don't wait for tty input */
chtype **_line;
short _firstchar;/* First changed character in the line */
short *_lastchar;/* Last changed character in the line */
short *_numchngd;/* Number of changes made in the line */
short _regtop;/* Top and bottom of scrolling region */
short _regbottom;
};
Type bool is defined in curses.h; an object of this type can hold the value
of true (nonzero) or false (zero).
The following describes selected WINDOW fields in detail.
_cury, _curx
Give the Y and X positions of the logical cursor. The upper left
corner of the window is, by definition, position 0,0. Note that
curses by convention gives positions as Y/X (row/column) rather
than X/Y, as is usual elsewhere.
_maxy, _maxx
Width and height of the window.
_begy, _begx
Position of the upper left corner of the window relative to the
upper left corner of the physical screen. For example, if the
window's upper left corner is five rows from the top of the
screen and ten columns from the left, then _begy and _begx will
be set to ten and five, respectively.
_flags One or more of the following flags, logically OR'd together:
_SUBWIN -- Window is a sub-window
_ENDLINE -- Right edge of window touches edge of the screen
_FULLWIN -- Window fills the physical screen
_SCROLLWIN -- Window touches lower right corner of physical screen
_FULLINE -- Window extends across entire physical screen
_STANDOUT -- Write text in reverse video
_INSL -- Line has been inserted into window
_DELL -- Line has been deleted from window
_ch_off Character offset.
_clear Clear the physical screen before next refresh of the screen.
_leave Do not move the physical cursor after refreshing the screen.
_scroll Enable scrolling for this window.
_y Pointer to an array of pointers to the character arrays that hold
the window's text.
_firstch Pointer to an array of integers, one for each line in the window,
whose value is the first character in the line to have been
altered by the user. If a line has not been changed, then its
corresponding entry in the array is set to _NOCHANGE.
_lastch Same as _firstch, except that it indicates the last character to
have been changed on the line.
_nextp Point to next window.
_orig Point to parent window.
When curses is first invoked, it defines the entire screen as being one
large window. The programmer has the choice of subdividing an existing
window or creating new windows; when a window is subdivided, it shares the
same curscr as its parent window, whereas a new window has its own stdscr.
Multiple Terminals
Some applications need to display text on more than one terminal,
controlled by the same process. curses can handle this, even if the
terminals are of different types. The following describes how curses
output to multiple terminals.
All information about the current terminal is kept in a global variable
struct screen *SP. Although the screen structure is hidden from the user,
the C compiler will accept declarations of variables which are pointers.
The user program should declare one screen pointer variable for each
terminal it wishes to handle.
The function newterm() sets up a new terminal of the given terminal type
that is accessed via file-descriptor fp. To use more than one terminal,
call newterm() for each terminal and save the value returned as a reference
to that terminal.
To switch to a different terminal, call set_term(). It returns the current
contents of SP. Do not assign directly to SP because certain other global
variables must also be changed.
All curses routines always affect the current terminal. To handle several
terminals, switch to each one in turn with set_term(), and then access it.
Each terminal must first be set up with newterm(), and closed down with
endwin().
Video Attributes
curses lets you display any combination of video attributes on any
terminal. Each character position on the screen has 16 bits of information
associated with it. Seven bits are the character to be displayed, leaving
bits for nine video attributes. These bits are used for the following
modes respectively: standout, underline, reverse video, blink, dim, bold,
blank, protect, and alternate-character set. Standout is whatever
highlighting works best on the terminal, and should be used by any program
that does not need specific or combined attributes. Underlining, reverse
video, blink, dim, and bold are the usual video attributes. Blank means
that the character is displayed as a space, for security reasons.
Protected and alternate character set depend on the terminal. The use of
these last three bits is subject to change and not recommended.
The routines to use these attributes include attron(), attroff(),
attrset(), standend(), standout(), wattroff(), wattron(), wattrset(),
wstandend(), and wstandout(). All are described below.
Attributes, if given, can be any combination of A_STANDOUT, A_UNDERLINE,
A_REVERSE, A_BLINK, A_DIM, A_BOLD, A_INVIS, A_PROTECT, and A_ALTCHARSET,
OR'd together. These constants are defined in curses.h. If the particular
terminal does not have the particular attribute or combination requested,
curses will attempt to use some other attribute in its place. If the
terminal has no highlighting, all attributes are ignored.
Function Keys
Many terminals have special keys, such as arrow keys, keys to erase the
screen, insert or delete text, and keys intended for user functions. The
particular sequences these terminals send differs from terminal to
terminal. curses lets you handle these keys.
A program using function keys should turn on the keypad by calling keypad()
at initialization. This causes special characters to be passed through to
the program by the function getch(). These keys have constants that are
defined in curses.h. They have values starting at 0401, so they should not
be stored in a char variable, as significant bits will be lost.
A program that uses function keys should avoid using the <esc> key:
most sequences start with <esc>, so an ambiguity will occur. curses
sets a one-second alarm to deal with this ambiguity, which will cause
delayed response to the <esc> key. It is a good idea to avoid
<esc> in any case, because there is eventually pressure for nearly
any screen-oriented program to accept arrow-key input.
Scrolling Region
Most terminals have a user-accessible scrolling region. Normally, this is
set to the entire window, but the calls setscrreg() and wsetscrreg() set
the scrolling region for stdscr or the given window to any combination of
top and bottom margins. If scrolling has been enabled with scrollok(),
scrolling takes place only within that window.
TTY Mode Functions
In addition to the save/restore routines savetty() and resetty(), curses
contains routines for going into and out of normal tty mode.
The normal routines are resetterm(), which puts the terminal back in the
mode it was in when curses was started, and fixterm(), which undoes the
effects of resetterm(), that is, restores the ``current curses mode''.
endwin() automatically calls resetterm(). These routines are also
available at the terminfo level.
No-Delay Mode
curses offers the call nodelay(), which puts the terminal into ``no-delay
mode''. While in no-delay mode, any call to getch() returns -1 if nothing
is waiting to be read. This is useful for programs that require real-time
behavior, where the user watches action on the screen and presses a key
when he wants something to happen. For example, the cursor can be moving
across the screen, and the user can press an arrow key to change direction.
This mode is especially useful for games.
Portability
curses contains several routines to improve portability. Because these
routines do not directly relate to terminal handling, their implementation
differs from system to system, and the differences can be isolated from the
user program by including them in curses.
Functions erasechar() and killchar() return the characters that,
respectively, erase one character and kill the entire input line. The
function baudrate() returns the current baud rate, as an integer. (For
example, at 9600 baud, baudrate() returns the integer 9600, not the value
B9600 from <sgtty.h>.) The routine flushinp() throws away all
typeahead. call resetterm() to restore the tty modes. After the shell
escape, fixterm() can be called to set the tty modes back to their internal
settings. These calls are now required, because they perform system-
dependent processing.
curses Routines
The following table summarizes the functions and macros that comprise the
curses library. These routines are declared and defined in the header file
curses.h.
addch(ch) char ch;
Insert a character into stdscr.
addstr(str) char *str;
Insert a string into stdscr.
attroff(at) int at;
Turn off video attributes on stdscr.
attron(at) int at;
Turn on video attributes on stdscr.
attrset(at) int at;
Set video attributes on stdscr.
baudrate()
Return the baud rate of the current terminal.
beep()
Sound the audible bell.
box(win, vert, hor) WINDOW *win; char vert, hor;
Draw a box. vert is the character used to draw the vertical lines,
and hor is used to draw the horizontal lines. The call
box(win, 0, 0)
draws a box with solid lines. The call
box(win, '|', '-');
draws a box around window win, using `|' to draw the vertical lines
and `-' to draw the horizontal lines. Do not use non-ASCII characters
unless you are very sure of the output terminal's identity.
cbreak()
Turn on cbreak mode.
clear()
Clear the stdscr.
clearok(win,bf) WINDOW *win; bool bf;
Set the clear flag for window win. This will clear the screen at the
next refresh, but not reset the window.
clrtobot()
Clear from the position of the logical cursor to the bottom of the
window.
clrtoeol()
Clear from the logical cursor to the end of the line.
crmode()
Turn on control-character mode; i.e., force terminal to receive cooked
input.
delch()
Delete a character from stdscr; shift the rest of the characters on
the line one position to the left.
deleteln()
Delete all of the current line; shift up the rest of the lines in the
window.
delwin(win) WINDOW *win;
Delete window win.
doupdate()
Update the physical screen.
echo()
Turn on both physical and logical echoing; i.e., characters are
automatically inserted into the current window and onto the physical
screen.
endwin()
Terminate text processing with curses.
erase()
Erase a window; do not clear the screen.
char *erasechar()
Return the erase character of the current terminal.
flash()
Execute the visual bell.
flushinp()
Flush input from the current terminal.
getch()
Read a character from the terminal.
getstr(str) char *str;
Read a string from the terminal.
getyx(win,y,x) WINDOW *win; short y,x;
Read the position of the logical cursor in win and store it in y,x.
Note that this is a macro, and due to its construction the variables y
and x must be integers, not pointers to integers.
idlok(win, flag) WINDOW *win; int flag;
Enable insert/delete line operations for window win. flag must
contain the OR'd operations you desire.
inch()
Read the character pointed to by the stdscr's logical cursor.
WINDOW *initscr()
Initialize curses. Among other things, this function initializes the
global variables LINES and COLS, which give, respectively, the number
of lines and the number of columns on your screen.
This is of most use under X Windows. When you change the size of an
xterm or xvt window, the command
eval `resize`
resets these variables. The next time you invoke a curses-based
program, its size will reflect new the dimensions of window.
insch(ch) char ch;
Insert character ch into the stdscr.
insertln()
Insert a blank line into stdscr, above the current line.
keypad(win,flag) WINDOW *win; int flag;
Enable keypad-sequence mapping.
char *killchar()
Return the kill character for the current terminal.
leaveok(win,bf) WINDOW *win; bool bf;
Set win->_leave to bf. If set to TRUE, refresh will leave the
cursor after the last character changed by refresh. This makes sense
if you want to minimize the commands sent to the screen and it does
not matter where the cursor is.
char *longname(termbuf, name) char *termbuf, *name;
Copy the long name for the terminal from termbuf into name.
meta(win, flag) WINDOW *win; int flag;
Enable use of the meta key.
move(y,x) short y,x;
Move logical cursor to position y,x in stdscr.
mvaddbytes(y,x,da,count) int y,x; char *da; int count;
Move to position y,x and print count bytes from the string pointed to
by da.
mvaddch(y,x,ch) short y,x; char ch;
Move the logical cursor to position y,x and insert character ch.
mvaddstr(y,x,str) short y,x; char *str;
Move the logical cursor to position y,x and insert string str.
mvcur(y_cur,x_cur,y_new,x_new) int y_cur, x_cur, y_new, x_new;
Move cursor from position y_cur,x_cur to position y_new,x_new.
mvdelch(y,x) short y,x;
Move to position y,x and delete the character found there.
mvgetch(y,x) short y,x;
Move to position y,x and get a character through stdscr.
mvgetstr(y,x,str) short y,x; char *str;
Move to position y,x, get a string through stdscr, and copy it into
string.
mvinch(y,x) short y,x;
Move to position y,x and get the character found there.
mvinsch(y,x,ch) short y,x; char ch;
Move to position y,x and insert a character into stdscr.
mvwaddbytes(win,y,x,da,count) WINDOW *win; int y,x; char *da; int count;
Move to position y,x and print count bytes from the string pointed to
by da into window win.
mvwaddch(win,y,x,ch) WINDOW *win; int y,x; char ch;
Move to position y,x and insert character ch into window win.
mvwaddstr(win,y,x,str) WINDOW *win; short y,x; char *str;
Move to position y,x and insert string str.
mvwdelch(win,y,x) WINDOW *win; int y,x;
Move to position y,x and delete character ch from window win.
mvwgetch(win,y,x) WINDOW *win; short y,x;
Move to position y,x and get a character.
mvwgetstr(win,y,x,str) WINDOW *win; short y,x; char *str;
Move to position y,x, get a string, and write it into str.
mvwin(win,y,x) WINDOW *win; int y,x;
Move window win to position y,x.
mvwinch(win,y,x) WINDOW *win; short y,x;
Move to position y,x and get character found there.
mvwinsch(win,y,x,ch) WINDOW *win; short y,x; char ch;
Move to position y,x and insert character ch there.
struct screen *newterm(type, fd) char *type; int fd;
Initialize the new terminal type, which is accessed via file-
descriptor fd.
WINDOW *newwin(lines, cols, y1, x1)
int lines, cols, y1, x1;
Create a new window. The new window is lines lines high, cols columns
wide, with the upper-left corner at position y1,x1. It returns a
pointer to the WINDOW structure that defines the newly created window.
nl() Turn on newline mode; i.e., force terminal to output <newline>
after <linefeed>.
nocbreak()
Turn off cbreak mode.
nocrmode()
Turn off control-character mode; i.e., force terminal to accept raw
input.
nodelay(win, flag) WINDOW *win; int flag;
Make getch() non-blocking.
noecho()
Turn off echo mode.
nonl()
Turn off newline mode.
noraw()
Turn off raw mode.
overlay(win1,win2) WINDOW *win1, win2;
Copy all characters, except spaces, from their current positions in
win1 to identical positions in win2.
overwrite(win1,win2) WINDOW *win1, win2;
Copy all characters, including spaces, from win1 to their identical
positions in win2.
printw(format[,arg1,...argN]) char *format; [data type] arg1,..argN;
Print formatted text on the standard screen.
raw()
Turn on raw mode; i.e., kernel does not process what is typed at the
keyboard, but passes it directly to curses. In normal (or cooked)
mode, the kernel intercepts and processes the control characters
<ctrl-C>, <ctrl-S>, <ctrl-Q>, and <ctrl-Y>.
See the entry for stty for more information.
refresh()
Copy the contents of stdscr to the physical screen.
resetty()
Reset the terminal flags to values stored by earlier call to savetty.
saveterm()
Save the current state of the terminal.
savetty()
Save the current terminal settings.
scanw(format[,arg1,...argN]) char *format; [data type] arg1,..argN;
Read the standard input; translate what is read into the appropriate
data type.
scroll(win) WINDOW *win;
Scroll win up by one line.
scrollok(win,bf) WINDOW *win; bool bf;
Permit or forbid scrolling of window win, depending upon whether bf is
set to true or false.
setscrreg(top, bottom) int top, bottom;
Set the scrolling region on stdscr.
setterm(name) char *name;
Set term variables for name.
struct screen *set_term(new) struct screen *new;
Switch output to terminal new. It returns a pointer to the previously
used terminal.
standend()
Turn off standout mode.
standout()
Turn on standout mode for text. Usually, this means that text will be
displayed in reverse video.
WINDOW *subwin(win, lines, cols, y1, x1)
int win,lines,cols,y1,x1;
Create a sub-window in window win. The new sub-window is lines lines
high, cols columns wide, and is fixed at position y1,x1. Note that the
position is relative to the upper-left corner of the physical screen.
This function returns a pointer to the WINDOW structure that defines
the newly created sub-window.
touchwin(win) WINDOW *win;
Copy all characters in window win to the screen.
traceoff()
Turn off debugging output.
traceon()
Turn on debugging output
unctrl(ch) char ch;
Output a printable version of the control-character ch.
waddch(win,ch) WINDOW *win; char ch;
Add character ch to window win.
waddstr(win,str) WINDOW *win; char *str;
Add the string pointed to by str to window win.
wattroff(win,att) WINDOW *win; int att;
Turn off video attributes att for the window pointed to by win.
wattron(win,att) WINDOW *win; int att;
Turn on video attributes att for the window pointed to by win.
wattrset(win,at) WINDOW *win; int att;
Set the video attributes att for the window pointed to by win.
wclear(win) WINDOW *win;
Clear window win. Move cursor to position 0,0 and set the screen's
clear flag.
wclrtobot(win) WINDOW *win;
Clear window win from current position to the bottom.
wclrtoeol(win) WINDOW *win;
Clear window win from the current position to the end of the line.
wdelch(win) WINDOW *win;
Delete the character at the current position in window win; shift all
remaining characters to the right of the current position one position
left.
wdeleteln(win) WINDOW *win;
Delete the current line and shift all lines below it one line up.
werase(win) WINDOW *win;
Clear window win. Move the cursor to position 0,0 but do not set the
screen's clear flag.
wgetch(win) WINDOW *win;
Read one character from the standard input.
wgetstr(win,str) WINDOW *win; char *str;
Read a string from the standard input; write it in the area pointed to
by str.
winch(win) WINDOW *win;
Force the next call to refresh() to rewrite the entire screen.
winsch(win,ch) WINDOW *win; char ch;
Insert character ch into window win at the current position. Shift
all existing characters one position to the right.
winsertln(win) WINDOW *win;
Insert a blank line into window win at the current position. Move all
lines down by one position.
wmove(win,y,x) WINDOW *win; int y, x;
Move current position in the window win to position y,x.
wnoutrefresh(win) WINDOW *win;
Copy the window pointed to by win to the virtual screen; do not
refresh the real screen.
wprintw(win,format[,arg1,...argN])
WINDOW *win; char *format;
[data type] arg1,..argN;
Format text and print it to the current position in window win.
wrefresh(win) WINDOW *win;
Refresh a window.
wscanw(win,format[,arg1,...argN])
WINDOW *win; char *format;
[data type] arg1,..argN;
Read standard input from the current position in window win, format
it, and store it in the indicated places.
wstandend(win) WINDOW *win;
Turn off standout (reverse video) mode for window win.
wstandout(win) WINDOW *win;
Turn on standout (reverse video) mode for window win.
wsetscrreg(win,top,bottom) WINDOW *win; int top, bottom;
Set the scrolling region on the window pointed to by win.
Color Support
Beginning with release 4.2, COHERENT's implementation of curses supports
color. curses defines colors as a video attribute, like any other. It
actually handles pairs of colors -- one for the foreground and one for the
background. You must initialize a color pair and given it a unique
identifying number; then pass the identifier of the color pair to the
function wattron() to turn on, like any other video attribute.
The header file <terminfo.h> defines the following colors, which
curses recognizes:
COLOR_BLACK
COLOR_RED
COLOR_GREEN
COLOR_YELLOW
COLOR_BLUE
COLOR_MAGENTA
COLOR_CYAN
COLOR_WHITE
Header file <curses.h> defines the variables COLORS, which holds the
maximum number of colors that your console or terminal recognizes; and
COLOR_PAIRS, which holds the maximum number of color pairs that your
console or terminal recognizes. The function start_color() initializes
both variables.
The following gives the functions and macros with which you can manipulate
colors:
can_change_colors()
This function returns TRUE if you can change the definition of a color
on your device, and FALSE if you cannot. You should call this
function before you invoke the function init_color(), described below.
color_content(color,r,g,b); int color,*r,*g,*b;
Read the RGB settings for color and write them at the addresses given
by r, g, and b.
COLOR_PAIR(pairnum); int pairnum;
Return the definition of the color pair identified by pairnum. The
color pair must have been initialized by a call to init_pair().
has_colors()
Return TRUE if your console or terminal supports color and FALSE if it
does not.
init_color(color,r,g,b); int color,r,g,b;
Initialize color to the RGB values r, g, and b. color must be greater
than zero and less than COLORS. r,' g, and b must each be between zero
and 1,000. Not every console or terminal permits you to reset its
colors. Call can_change_colors() to see if you can alter your
device's colors.
init_pair(pairnum,fc,bc) int pairnum, fc, bc;
Initialize the color pair pairnum to the foreground color fc and the
background color bc. pairnum must be greater than zero and less than
COLOR_PAIRS. fc and bc must be greater than -1 and less than COLORS.
pair_content(pairnum,fc,bc) int pairnum,*fc,*bc;
Read the foreground and background colors represented by color pair
pairnum and write them into the areas pointed to by fg and bg.
start_color()
Turn on color processing. This function must precede all other color
routines; usually, it immediately follows the function initscr().
A brief example of how to colors appears in the Examples section, below.
terminfo Routines
As noted above, curses reads terminal descriptions from terminfo rather
than termcap. The library libcurses also holds the following functions,
with which you can read a terminfo description:
fixterm()......Set the terminal into program mode
putp().........Write a string into stdwin
resetterm()....Reset the terminal into a saved mode
setupterm()....Initialize terminal capabilities
tparm()........Output a parameterized string
tputs()........Process a capability string
vidattr()......Set the terminal's video attributes
vidputs()......Set video attributes into a function
For more information on these routines, see the Lexicon entry terminfo, or
see each routine's entry in the Lexicon.
If you define the environment variable TERMINFO, curses checks for the
terminal definition in the directory that TERMINFO names rather than in the
standard directory /usr/lib/terminfo. For example, if you set the
environmental variable TERM is set to vt100, then the compiled terminfo
definition is kept in directory /usr/lib/terminfo/v/vt100. However, if you
define TERMINFO to be $HOME/testterm, curses first checks
$HOME/testterm/v/vt100; if that fails, it then checks
/usr/lib/terminfo/v/vt100. This is useful when you are debugging a terminfo
entry.
Structure of a curses Program
To use the curses routines, a program must include the header file
curses.h, which declares and defines the functions and macros that comprise
the curses library.
Before a program can perform any screen operations, it must call the
function initscr() to initialize the curses environment.
As noted above, curses manipulates text in a copy of the screen that it
maintains in memory. After a program has manipulated text, it must call
refresh() to copy these alterations from memory to the physical screen.
(This is done because writing to the screen is slow; this scheme permits
mass alterations to be made to copy in memory, then written to the screen
in a batch.)
Finally, when the program has finished working with curses, it must call
the function endwin(). This frees memory allocated by curses, and generally
closes down the curses environment gracefully.
Examples
The first example shows what modes and characters are visible on your
system.
#include <curses.h>
#define A_ETX 0x03
main()
{
int c, y = 0, x = 0, attr = A_NORMAL, mask, state = 0, hibit = 0;
initscr();
noecho();
raw();
erase();
move(y, 0);
addstr(
"+ sets - clears Normal Bold Underline blInk Reverse Standout Altmode");
move(++y, 0);
refresh();
for (;;) {
if (!state) {
switch (c = getch()) {
case '+':
state = 1;
break;
case '-':
state = 2;
break;
case '\b':
if (!x)
break;
move(y, --x);
addch(' ');
move(y, x);
refresh();
break;
case '\r':
case '\n':
move(++y, x = 0);
refresh();
break;
case A_ETX:
noraw();
echo();
endwin();
exit(0);
default:
x++;
addch(c | hibit);
refresh();
}
} else {
switch (c = getch()) {
case 'A': /* turn on high bit of input */
hibit = (state & 1) << 7;
state = 0;
continue;
case 'B':
mask = A_BOLD;
break;
case 'U':
mask = A_UNDERLINE;
break;
case 'I':
mask = A_BLINK;
break;
case 'R':
mask = A_REVERSE;
break;
case 'S':
mask = A_STANDOUT;
break;
case 'N': /* normal is an absence of bits */
if (state == 1) {
attr = A_NORMAL;
hibit = state = 0;
continue;
}
default:
beep();
continue;
}
if (state == 1)
attr |= mask;
else
attr &= ~mask;
attrset(attr);
state = 0;
}
}
}
The next example demonstrates how to use colors in a curses program. It
selects colors randomly, builds color pairs, and displays a 40-character
text string to demonstrate the newly build color pair.
#include <curses.h>
#include <stdlib.h>
void goodbye()
{
move(23, 0);
noraw();
echo();
endwin();
exit(EXIT_SUCCESS);
}
main()
{
int x, y, i;
initscr();
start_color();
noecho();
raw();
srand(time(NULL));
erase();
if (!has_colors())
goodbye();
for (x = 0, y = 0, i = 1; i < COLOR_PAIRS; i++, y++) {
if (y == 23) {
x = 40;
y = 0;
}
move(y, x);
init_pair(i, (rand() % COLORS), (rand() % COLORS));
attrset(COLOR_PAIR(i));
addstr("Pack my box with five dozen liquor jugs.");
}
refresh();
goodbye();
}
The next example shows how to read function keys under curses:
#include <curses.h>
void text();
main()
{
int input;
initscr();
raw();
noecho();
keypad(stdscr, TRUE);
refresh();
while (TRUE) {
input = wgetch(stdscr);
switch (input) {
case 'q':
case 'Q':
endwin();
exit(0);
case KEY_UP:
text("cursor up");
break;
case KEY_DOWN:
text("cursor down");
break;
case KEY_LEFT:
text("cursor left");
break;
case KEY_RIGHT:
text("cursor right");
break;
case KEY_F(1):
text("function key 1");
break;
case KEY_F(2):
text("function key 2");
break;
default:
text("some other key");
break;
}
}
}
void text(s)
register char *s;
{
move(0, 0);
clrtoeol();
printw("Your input was: %s", s);
refresh();
}
See Also
curses.h,
libraries,
termcap,
terminfo
Strang J: Programming with curses. Sebastopol, Calif, O'Reilly &
Associates Inc., 1986.
Notes
The implementation of curses used by the COHERENT system was written by
Pavel Curtis of Cornell University. It was ported to COHERENT by Udo Munk.









