UNIX C graphical programming language -curses.h Library

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Believe in your network must be used as tin,elm Tools such as, These software have common characteristics, They can use the up and down direction keys to control the cursor position. In addition, These programs picture is beautiful. Yes Programming Interested friends must be curious, Maybe he can PC With TurboC Write a similar program easily, However, But when he will be the same code word for word to workstation compile time, There is a pile of grasping also endless errors. In fact, The reason is very simple, Library he used in UNIX Is not defined. Some in the Turbo-C Some functions are widely used, May be in the UNIX Is not defined.

Due to the Internet every kind of terminal morphology (terminal), UNIX specially developed a library, designed to handle the UNIX cursor movement and screen display. The curses.h library which is this article to introduce you. Use this library, you can also write
Like elm use the arrow keys to move the bar position program. (CCCA recently provided online courses, and program service interface, that is by using the curses evolved)

■ The history and the version of curses

Cureses is the first by the University of California at Berkeley's Bill Joy and Ken Arnold development.
The main reason for such a library is to improve the compatibility of different terminal program.
Meter. Therefore, using the curses development program will not and you are using a terminal.
That is to say, you don't have to worry about your program cannot be used because of the change in a terminal and the process.
Type designers, especially writing network programming, is a very important thing.
Curses is on more than 100 kinds of terminal work, because it will all terminals.
Material, stored in a termcap database, and in the System V system (version second.
, The new version of the curses terminfo to replace the original termcap). With these records, process
Type can know what kind of terminal, to send what characters can move the cursor position,
Send what character to clear the whole screen removed. (* Note 1)

In addition, this paper introduces the System V curses version.

■ How to use curses in your program ?

In the header of your C program <curses.h> include come in. When you import curses.h
The library, the system will automatically <stdio.h> and <unctl.h> together with include
To. In addition, in the System version V, <terminfo.h> the library will also be
Include came in.

#include <curses.h>

: :
: :

Of course, the curses.h library are put to your system.

■ How to compile(compile)

When you edit your program at the UNIX prompt, type:

% /usr/5bin/cc [file.c] -lcurses
The introduction of the curses.h Library

Or % /usr/5bin/cc [file.c] -lcurses -ltermlib

(note two)

■ How to start the first curses program I?

Before all the command to start using curses, you must use the initscr () function
Open the curses mode.

Relative, at the end of the curses model (usually in your program before the end) must also be
endwin()To close the curses mode.

#include <curses.h>

: :
: :
: :

This is a general curses program standard mode.

In addition, you can have your program must be, and do different settings. Of course, you can't do is set, and the
Just call initscr().

You can write a function to store all the desired setting. Regular use, as long as the call
Call this function to start the curses and complete all the settings.

The following example, which is the usual some of the more commonly used set on a call initial () function


void initial()

The functions are introduced as follows:

□ initscr()

initscr() Is a function of general curses program must first call, but this function
Called after, according to the system of the terminal morphology and start the curses mode.

□ endwin()

Curses usually call endwin () to end the program (endwin) can be used to shut down.
Curses mode, or temporary jump from curses mode. If you need in a program.
Call shell (such as call system () function) or need to do system call,
We must first to endwin (temporary) jumped off the curses model. Then the
wrefresh() doupdate() To return to the curses mode.

□ cbreak()

When the cbreak mode is activated, in addition to the DELETE or CTRL is still seen as a special
Control characters all input characters will immediately be one one read. When in nocbreak mode
Type, from the keyboard input character will be stored in the buffer until the input RETURN
Or NEWLINE. must call crmode in the older version of the curses (), nocrmode ().
Replace the cbreak(),nocbreak()

□ nl()

Used to determine when the input data, press the RETURN key is corresponding to the NEWLINE word
Element (such as \n).
While the output data, NEWLINE character is corresponding to RETURN and LINDFEED
The system is turned on by default.

□ echo()

This function is used to control the keyboard input from the character is the character display on the terminal.
Is turned on by default.

□ intrflush(win,bf)

Call intrflush to be introduced into the two value:
Win is a WINDOW type index, usually into the standard input and output screen stdscr

When BF is true, when the input interrupt character (such as break), interrupt response
Will be more quickly. But it may cause a screen disorder.

□ keypad(win,bf)

Call keypad to be introduced into the two value:
Win is a WINDOW type index, usually into the standard input and output screen stdscr

When opening a keypad, can use some special characters on the keyboard, such as left and right
The direction key, curses will these special characters into a curses.h defined within a
Some special key. Special keys of these definitions are usually begin with KEY_.

□ refresh()

refresh() As a function of curses most often call.

Curses in order to make the screen output to reach optimum, when you call a function to change the screen output
Change the screen images, curses did not immediately on the screen do change, but wait until the
refresh() After the call, they will just do change once completed. The rest of the data will be
Remain unchanged. As much as possible to give the least character to the screen. Reduce screen redraw time.
If it is initscr () after the first call refresh (), curses will make clear fireflies
Screen work.

■ Cursor control

move(y,x) Move the cursor to the X, y position
getyx(win,y,x) Get the current cursor position
(note y, X instead! &y,&x )

■ Function related to clear the screen.

erase() The entire screen cleaning
(note) with refresh (use)

■ How to display the characters on the screen

echochar(ch) Display a character

addch(ch) Display a character
mvaddch(y,x,ch) In the (x, y) shows a character
Equivalent to calling move(y,x);addch(ch);

addstr(str) Display a string of string
mvaddstr(y,x,str) In the (x, y) display a string of string
Equivalent to calling move(y,x);addstr(str);

printw(format,str) Similar to printf (), in a certain format output to the screen
mvprintw(y,x,format,str) In the (x, y) position to work on Printw.
Equivalent to calling move(y,x);printw(format,str);

■ How to read a character from the keyboard.

getch() Read a character from the keyboard (attention! Is the return of
Integer value)
getstr() Read a character from the keyboard
scanw(format,&arg1,&arg2...) As with the scanf, read the string of characters from the keyboard

□An example:

int ch;
char string1[80]; /* Attention please! Don't be declared char *string1; */
char string2[80];

echo(); /* Echo mode, the input immediately displayed on the screen */

■ How to use the direction keys

Curses will like the direction key and other special control characters, beginning with KEY_ are defined in the curses.h

This file inside, such as KEY_UP represents the direction key " ↑ ". But, if you want to use

Curses.h for these special keys that you define it, you must set the keypad for

TRUE. otherwise, you must define the special keys for all.

Curses.h defined for some special keys are as follows:

KEY_UP 0403 ↑
KEY_DOWN 0402 ↓
KEY_LEFT 0404 ←
KEY_RIGHT 0405 →
KEY_HOME 0406 Home key (upward+left arrow)
KEY_BACKSPACE 0407 backspace (unreliable)
KEY_F0 0410 Function keys.
KEY_F(n) (KEY_F0+(n)) formula for f .
KEY_NPAGE 0522 Next page
KEY_PPAGE 0523 Previous page

The above only lists some of the more commonly used control keys, defined for other control key, please.

Refer to man curses (note three)

Also provide you a list of other commonly used some special characters

[TAB] /t
[ENTER] /r
[ESC] 27

■ How to change the screen display character attributes

In order to make the output screen is more lively and beautiful, we often have to do things like white on the screen,

Scintillation changes. Curses defines some special properties, through these definitions, we can

In the output change of curses program - control screen.

attron(mod) Open property
attroff(mod) Close the property

Curses.h, define some properties, such as:

A_UNDERLINE underline
A_BLINK flash
A_BOLD Gao Liangdu
A_NORMAL standard mode (only with attrset) (use)

When using attron () to open a specific attribute model, then the screen output will be to

This kind of attribute appears. Until you call attroff () will turn this mode.

Please note, when you want to attron () to open another property, please remember to use attroff () the first turn

Close the original property, or directly to the attrset (A_NORMAL) will be closed. Otherwise, all special attribute,

Curses will do overlap processing will be two kinds of property.

□An example:

attrset(A_NORMAL); /* The first property is set to the normal mode */

attron(A_UNDERLINE); /* And the bottom line */

mvaddstr(9,10,"Underline "); / * underline the output string of characters */

attroff(A_UNDERLINE); /* To close a line mode, return to normal mode */

attron(A_REVERSE); /* Open the visual mode */

mvaddstr(10,10,"White "); / * the output string of white character */

attroff(A_REVERSE); /* Close the visual mode, return to normal mode */

attron(A_BLINK); /* Open flash mode */

mvaddstr(11,10,"Flashing "); / * the output string of scintillation character */

attroff(A_BLINK); /* Close the flash mode, return to normal mode */

attron(A_BOLD); /* Open high brightness mode */

mvaddstr(12,10,"High brightness "); / * output a string of high brightness character */

attroff(A_BOLD); /* Off the high brightness mode, return to normal mode */

■ Other commonly used some function

beep() A loud beep
box(win,ch1,ch2) Since animation box ch1: if drawn perpendicular to the direction of the character
The character used by the ch2: if drawn horizontally

example: box(stdscr,'|','-');
The | and - round into a box

■ Application of full - Cases

The following example, namely complete with type just introduced to complete. This program can be the key

Disk read characters displayed on the screen, and can be up and down arrow keys to control the cursor position

, When you press [ESC], the program is finished.

You have not found, this is not a simple prototype it full screen editor?

#include <curses.h> /* The introduction of curses.h, and automatic introduction
stdio.h */

#define StartX 1 /* Determine the initial position the cursor */
#define StartY 1

void initial();

int x=StartX; /* Declaration of X, y and set its initial value

int y=StartY;
int ch; /* Dim ch as integer, getch()
Use */

initial(); /* Call initial (), curses
Pattern, */

/* And complete other setting

box(stdscr,'|','-'); /* Drawing box

attron(A_REVERSE); /* Open the visual mode

mvaddstr(0,20,"Curses Program"); /* In (20,0) at the output level character

attroff(A_REVERSE); /* Close the visual mode

move(x,y); /* The cursor is moved to the initial position

do { /* In order to infinite loop constantly waiting for input

ch=getch(); /* Wait for a keyboard input character
switch(ch) { /* Determine whether the input character why

case KEY_UP: --y; /* To determine whether the " key is pressed; ↑ "

case KEY_DOWN: ++y; /* To determine whether the " key is pressed down; "

case KEY_RIGHT: ++x; /* To determine whether the " key is pressed, ";

case KEY_LEFT: --x; /* To determine whether the " key is pressed; ← "

case '\r': /* To determine whether the ENTER key is pressed

case '\t': /* To determine whether the TAB key is pressed

case 127: /* To determine whether the BACKSPACE key is pressed

mvaddch(y,--x,' ');/* Delete a character


case 27: endwin(); /* To determine whether the [ESC] key is pressed

exit(1); /* The end of curses model

/* The end of the program

addch(ch); /* If no special characters, the characters printed
Out */

move(y,x); /* Move the cursor to the present position

} while (1);

void initial() /* Since the opening of curses function


■ Postscript

After some command above, do not believe you have to write a beautiful full screen editing
Device? In fact, function provided by curses is not 200, but the author thinks, after all
Function of complex can be combined with some changes in the form, the function is too much, only
Add to their troubles. Of course, if you are interested in other functions, can refer to
Curses description file. (method:% man curses) in this paper, but for this effect, also
Hope to have more students create their own application future.

* Any questions and suggestions, welcome to e-mail. Thank you. ! *

Note 1:
Please refer to /usr/share/lib/termcup

Note two:
1 if the version of BSD, need to use
cc [file.c] -lcurses -ltermcap to do compile.
2 meter workstation somehow the original change /usr/5bin/cc to /usr/5bin/cc.org

Therefore, if you want to in the meter workstation compile curses program. To /usr/5bin/cc.
Replace the /usr/5bin/cc, or compile might be wrong.
The 3 older version of the curses needs to introduce curses and termlib two Library,
Therefore, you must use the /usr/5bin/cc [file.c] -lcurses -ltermlib

Note three:
According to the author's experience, the left and right arrow keys on the normal use and should not occur, but the other

Such as PgUp, PgDn, Home, End function keys, and other special keys, very easily due to different machines, without keyboard

Method of use, therefore, if your program needs to use on different machines, suggest you use the arrow keys to

Control, special keys other little use for it.
As for PgUp, PgDn control method of some special keys, it is so complicated, interested students can
Some of the test methods used in the tin source curses.c.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Jesse at December 24, 2013 - 2:29 PM