Summary of the curses Manual - just the screen handling stuff NAME curses - machine independent screen handling and optimization package RULES You must include a spcial header in your programs #include You must link in a special library: cc [ flag ... ] file ... -lcurses To initialize the routines, the routine initscr() must be called before any of the other routines. The routine endwin() must be called before exiting. To get character-at-a-time input without echoing the following sequence should be used to initalize the curses routines: initscr(); cbreak(); noecho(); See `man curs_initscr`. The basic routines to set up the window are move(x,y) to move the internal cursor, and addch(ch). There are many more. The coordinate y always refers to the row (of the window), and x always refers to the column. The upper left-hand corner is always (0,0), not (1,1). Nothing happens to the real screen until you 'refresh()' it. After using routines to manipulate a window, refresh() is called, telling curses to make the user's CRT screen look like the internal screen (`man curs_refresh`). The characters in the screen are actually of type chtype, (character + attribute data) so that video attributes may be included, causing the characters to show up in such modes as underlined, in reverse video for example: addch('*' | A_REVERSE); Some Attribute symbols A_STANDOUT Best highlighting mode of the terminal. A_UNDERLINE Underlining A_REVERSE Reverse video A_BLINK Blinking A_DIM Half bright A_BOLD Extra bright or bold Line drawing characters may be specified to be output. The video attributes, line drawing characters, and input values use names, defined in , such as A_REVERSE and ACS_HLINE. On input, curses is also able to translate arrow and function keys that transmit escape sequences into single values, for example KEY_LEFT. For such special characters: See `man curs_addch` The integer variables LINES and COLS are defined in and will be filled in by initscr() with the size of the screen. Routines prefixed with `mv` require an x and y coordinate to move to before performing the appropriate action. The mv routines imply a call to move(x,y) before the call to the other routine. See `man curs_move` for more about `move`. Option setting routines require a Boolean flag bf with the value TRUE or FALSE; bf is always of type bool. The constants TRUE and FALSE have the values 1 and 0 in the type bool, respectively in . The value ERR is returned by routines when things go wrong. The types SCREEN, bool, and chtype are defined in etc. The header automatically includes the headers and Some Functions Putting characters|attributes onto the screen -- see man curs_addch int addch(chtype ch) Add ch to screen at current position also mvaddch(x,y,ch) -- Move and add character. echochar(ch) -- Add the character and then refresh the user's screen. Putting strings ofcharacters onto the screen -- see man curs_addstr int addstr(char *str); int addnstr(char *str, int n); also mvaddstr(x,y,str), mvnaddstr(x,y,str,n), Clear screen -- see curs_clear int erase(void); -- put blanks everywhere (will be written when refresh is called) int clear(void); -- put blanks and let terminal be cleared next time Character attribute control routines for whole screen -- see curs_attr int attroff(int attributes) int attron(int attributes) Input a character from the user -- man curs_getch and curs_inopts int getch(void); void echo(void) / void noecho(void) -- show the input to the user void cbreak(void) / void nocbreak(void) -- line input vs character/character input void raw(void) / void noraw(void) -- backsapce kept etc void nodelay(void) -- if no character is ready then getch() returns ERR int halfdelay(int tenths); -- error returned by getch() if no input for tenths sec. int keypad(stdscr, TRUE); int keypad(stdscr, FALSE); Function Keys (IMHO: you can't trust these in UNIX :-( ) The following might be returned by getch() if keypad() has been enabled. Name Key name KEY_BREAK Break key KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT The four arrow keys ... KEY_HOME Home key KEY_BACKSPACE Backspace KEY_F(n) For 0 < n < 63 KEY_DL Delete line KEY_IL Insert line KEY_DC Delete character KEY_IC Insert char or enter insert mode KEY_EIC Exit insert char mode KEY_CLEAR Clear screen KEY_NPAGE Next page KEY_PPAGE Previous page KEY_ENTER Enter or send Utility routines -- see curs_util int flushinp() -- throws away any typeahead char * unctrl(chtype) --translate Control character into the ^X notation(1 or two chars result). void delay_output(int ms) -- inserts an ms millisecond pause in output. This routine should not be used extensively because padding characters are used rather than a CPU pause. Sample programs. /* R J Botting. CS 202 Thu Oct 27 15:34:27 PDT 1994 screen.c*/ /* Purpose: A simple cursor movement program*/ /* WARNING: Must compile with option '-lcurses' */ #include #include int main( void ) { char caption[255]; /* declare variables*/ initscr(); clear(); move(LINES/2, COLS/2-8); addstr("Welcome to Curses!"); sprintf(caption, "Screen is %d rows by %d cols", LINES, COLS); move(2+LINES/2, COLS/2-strlen(caption)/2); addstr(caption); refresh(); endwin(); return(0); } /* R J Botting. CS 202 Thu Oct 27 15:34:27 PDT 1994 wallpaper.c*/ /* Purpose: Another cursor movement program*/ /* WARNING: Must compile with options '-lm' and '-lcurses' */ #include #include int main( void ) { int i; /*number of iterations in bugs llife cycle*/ int ix, iy; int vx, vy; int midx, midy; initscr(); /*Intialize the screen... essential to make curses work*/ midx = LINES / 2; /* COLS and LINES are defined by initscr(), and not before*/ midy = COLS / 2; cbreak();/*allow CTRL/C interupts... man cbreak*/ clear(); /*clear screen*/ ix = midx; iy = midy; vx = 1; vy = -1; move(ix, iy); addch('*' ); refresh(); for (i = 0; i < 200; i++) { /*move(ix, iy); addch(' ' ); */ ix += vx; iy += vy; move(ix, iy); addch('*' ); refresh(); if (ix <= 1) vx = 1; else if (ix >= LINES - 2) vx = -1; if (iy <= 1) vy = 1; else if (iy >= COLS - 2) vy = -1; } endwin(); return(0); } /* R J Botting. CS 202 Thu Oct 27 15:34:27 PDT 1994 wallpaper.c*/ /* Purpose: Another cursor movement program*/ /* WARNING: Must compile with options '-lm' and '-lcurses' */ #include #include #include /* Things that can be changed to get different effects*/ #define SCALEX 1.0L #define SCALEY 1.0L #define SCALEZ 20.0L delay(){/*system("sleep 1")*/;} char pixmap[]={' ', ':', '+', 'o', 'O' , '0', '*', '@', '#'}; /* NOT a STRING */ #define PIXMAPLEN (sizeof(pixmap)/sizeof(pixmap[0])) double f(double x, double y) { return 7.0L*x*x+4.0*y*y ; } /* Main program that usesthe above things */ int main( void ) { int ix,iy; double z; int midx, midy; initscr(); /*Intialize the screen... essential to make curses work*/ midx=LINES/2; /* COLS and LINES are defined by initscr(), and not before*/ midy=COLS/2; cbreak();/*allow CTRL/C interupts... man cbreak*/ clear(); /*clear screen*/ for(z=0.0L; z<15.0L; z += 0.25L ) { for(ix= -midx+1; ix