260 likes | 599 Views
NCURSES. ncurses - CRT screen handling and optimization package. SPARCS 98’ 함명주. What is ncurses?. curses 는 터미널종류에 상관없이 상당한 최적화된 화면상의 문자조절방법을 제공하는 라이브러리입니다 . 이것의 구현이 “ new curses” - ncurses 입니다. ncurses 라이브러리 사용법. 소스화일내에 “ #include <ncurses.h>” 를 넣는다 .
E N D
NCURSES ncurses - CRT screen handling and optimization package SPARCS 98’ 함명주
What is ncurses? • curses는 터미널종류에 상관없이 상당한 최적화된 화면상의 문자조절방법을 제공하는 라이브러리입니다. • 이것의 구현이 “new curses” - ncurses입니다.
ncurses 라이브러리 사용법 • 소스화일내에 “#include <ncurses.h>”를 넣는다. • 컴파일 할때 gcc main.c -lncurses 와 같이 ncurses라이브러리를 넣어준다. (linker 옵션) • man ncurses • man curs_???????
#include<ncurses.h> void main() { int x; initscr(); cbreak(); noecho(); keypad(stdscr,TRUE); refresh(); while((x=getch()) != 'q') { printw("%d %x\n", x, x); refresh(); } endwin(); } “ncurses1.c” RESULT : #cat m.t abcq #./a.out < m.t 97 61 98 62 99 63 # ncurses 예제 1
WINDOW *initscr() 초기화 함수 (터미널 제어코드를 읽음) • int cbreak() - 입력한 내용을 buffering하지 않음 (nobreak()는 반대 역할을 함) • int noecho() - 입력한 내용이 화면에 보이지 않도록 함 (echo()와 반대) • int keypad(WINDOW*, int) - keypad(stdstr, TRUE)는 표준화면 (사용자화면)이고 TRUE는 특수문자를 허용하겠다는 뜻.
int refresh() - 출력한 내용을 실제로 화면에 뿌린다. • int printw(char*, ... ) - ncurses에서 쓰는 printf() • endwin() - 윈도우 작업을 종료할 때 호출하는 함수. initscr()에서 저장한 것 터미널 타입을 돌려주고 이전화면으로 돌아감.
#include <ncurses.h> main() { int i = 0; initscr(); noecho(); cbreak(); keypad(stdscr, TRUE); scrollok(stdscr, TRUE); while(getch() != 'q') { mvprintw(24, 0, "printing line # %d", i++); scroll(stdscr); refresh(); } endwin(); } ncurses 예제 2
Result • gcc ncurses2.c -lncurses • ./a.out printing line # 0 printing line # 1
int mvprintw(int y, int x, char *,...) - conio와는 달리 x,y가 아니라 y,x이고, 좌표의 시작은 0,0이다 • int scrollok(WINDOW *, int) - 커서가 라인의 끝이나 마지막 라인에 있을때 스크롤이 되게 할것인가 말것인가를 결정. scrollok(stdscr, TRUE) 는 표준화면이 스크롤 되도록 함. (TRUE == 1) • scroll(stdscr) - 한줄 내려감 • #define scroll(win) wscrl(win,1) cf) • #define gotoxy(x,y) move(y-1,x-1) • #define clrscr() clear()
ncurses 예제 3 #include <ncurses/ncurses.h> void main() { int i, j; initscr(); for (i = 0; i < LINES; i++) { printw("%d %d", LINES, COLS); j = mvaddch(i, COLS - 1, 'A' + i); getch(); } refresh(); endwin(); } // Default : // echo(); // keypad(stdscr, TRUE); // noecho() Default => // nocbreak();
Result • gcc ncurses3.c -lncurses • ./a.out 10 80 A 10 80 B 10 80 C 10 80 D 10 80 E 10 80 F 10 80 G 10 80 H 10 80 I 10 80 J
#define mvaddch(y,x,ch) mvwaddch(stdscr, y,x,ch) • stdscr의 y,x에 ch를 찍음 • #define mvaddch(win,y,x,ch) (wmove(win,y,x)==ERR? ERR: waddch(win,ch)) • win의 y,x에 커서를 이동시키고 에러가 없으면 ch를 찍는다. • int wmove(WINDOW *, int, int) - 해당 윈도의 y,x좌표로 이동 • int waddch(WINDOW *, chtype) - 현재위치에 문자 출력 • int LINES, COLS : 현재 터미널 (stdscr)의 크기를 나타냄 • #define ERR (-1) - 대부분의 함수의 리턴은 에러상태에서 -1을 보냄 • #define OK(0)
ncurses 예제 4 - 문자속성 #include <ncurses.h> void main() { initscr(); attron(A_STANDOUT); mvaddstr(2,8,"this is STANDOUT mode"); attroff(A_STANDOUT); attron(A_REVERSE); mvaddstr(4,8,"this is REVERSE mode"); attroff(A_REVERSE); attron(A_BOLD); mvaddstr(6,8,"this is BOLD mode"); attroff(A_BOLD); attron(A_UNDERLINE); mvaddstr(8,8,"this is UNDERLINE mode"); attroff(A_UNDERLINE);
attron(A_DIM); mvaddstr(10,8,"this is DIM mode"); attroff(A_DIM); mvaddstr(12,8,"this is NORMAL mode"); attron(A_BLINK); mvaddstr(14,8,"this is BLINK mode"); attroff(A_BLINK); attron(A_BOLD|A_BLINK|A_UNDERLINE); mvaddstr(16,8,"this is BOLD UNDERLINE BLINK mode"); attrset(A_NORMAL); mvaddstr(18,8,"this is NORMAL mode"); refresh(); getch(); endwin(); }
#define attroff(at) wattroff(stdscr, at) - 해당 속성을 끕니다. • #define attron(at) wattron(stdscr, at) - 해당 속성을 켭니다. • 비트 연산을 통해 속성을 관리합니다. • #define mvaddstr(y,x,str) mvwaddstr(stdscr, y, x, str) - 문자열을 표준화면의 y,x에 씁니다. • #define mvwaddstr(win,y,x,str) (wmove(win,y,x)==ERR?ERR:waddnstr(win,str,-1)) • 속성들은 32bit int으로 되어 있습니다. 예를들어 #define A_UNDERLINE 0x00020000 #define A_REVERSE 0x00040000 #define A_BLINK 0x00080000 • 이것을 | (논리or) 연산자로 여러개의 속성을 한번에 관리할 수 있습니다.
윈도우 다루기 #1 • extern WINDOW *stdscr, *curscr - 표준스크린 , 현재스크린 • extern WINDOW *newwin(int,int,int,int) - 세로크기,가로크기, y,x좌표 • typedef struct _win_st WINDOW; • struct _win_st { • short _cury, _curx; //Current Position • short _maxy, _maxx; // maxvalue of y.x (dimension - 1) • short _begy, _begx; // begging of y,x • short _attr; // 속성. • . . . . . • WINDOW *_parent; • };
윈도우다루기-예제1 #include <ncurses.h> void screen_info(WINDOW *mywin2, int i) { printw("-------- Window %d -------- \n", i); printw("Current (X, Y) Position : ( %d, %d )\n", mywin2->_curx, mywin2->_cury); printw("Max Colums, Max Lines : ( %d, %d )\n", mywin2->_maxx, mywin2->_maxy); refresh(); } void main() { WINDOW *mywin, *yourwin; mywin= initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); screen_info(mywin, 1); yourwin= newwin(10, 40, 6, 20); screen_info(yourwin, 2); endwin(); }
result.. • ./a.out ------- Window 1 ------ Current (X, Y) Position : ( 0, 1 ) Max Colums, Max Lines : ( 79, 24 ) ------- Window 2 ------ Current (X, Y) Position : ( 0, 0 ) Max Colums, Max Lines : ( 39, 9 )
윈도우 다루기 #2 • wmove(WINDOW *w , int y , int x) - w윈도에서 y,x로 이동 • whline(WINDOW *w, chtype, int xd) - w윈도 현재 위치에서 chtype속성으로 xd거리만큼 선을 그림 • refresh(yourwin) - wourwin에 써넣은것을 화면에 출력 • winsertin(mywin) - 현재위치에 빈 라인을 삽입하여 아래줄을 밀어냄 • #define winsertin(x) winsdelln(x,1) • scrollok(mywin, TRUE) - mywin에서 스크롤을 동작시킴
윈도우 다루기 예제2 #include <ncurses.h> main() { int i; WINDOW *win; initscr(); noecho(); nonl(); cbreak(); scrollok(stdscr, TRUE); for (i=0; i<25; i++) printw("This is in the background,\ this should be left alone!\n"); refresh(); win=newwin(10,50,6,20); scrollok(win,TRUE); wmove(win,0,0); whline(win,0,49); wmove(win,9,0); whline(win,0,49); wrefresh(win); getch(); wattrset(win,A_STANDOUT); mvwprintw(win, 0, 14, " Scrolling Demo! "); mvwprintw(win, 9, 14, " Scrolling Demo! ");
wattroff(win,A_STANDOUT); wsetscrreg(win, 1,8); mvwprintw(win, 0, 5, " DOWN "); wmove(win,1,0); wrefresh(win); getch(); for (i=0; i<25; i++){ wprintw(win, "This is window line test (%d).\n", i); if (i%2) wattrset(win,A_BOLD); else wattroff(win,A_BOLD); wrefresh(win); } mvwprintw(win, 0, 5, " DONE "); wrefresh(win); endwin(); }
result... • 전체화면윈도와 작은 윈도우가 생기고 작은 윈도우에 스크롤이 된다. • nl() , nonl() : newline이 전송된 때, 그것을 CR/LF로 출력하고 return이 전송됬을때 newline으로 출력한다. • nonl()은 이러한 전송을 불가능 하게 한다.
윈도우 다루기 #3 • WINDOW *newwin(int, int, int, int) • int delwin(WINDOW *win) • int mvwin(WINDOW *win, int y, int x) - win을 y,x로 옮기고 영역 밖일때는 에러가 발생함. • WINDOW *subwin(WINDOW *orig, int nlines, int ncols, int begin_y, int begin_x) - 본래의 box를 침입 안하고 subwindow를 만듬. begin_? 을 위치로 만듬 (절대좌표) • int wborder(WINDOW *win, chtype ls, rs, ts, bs, tl, tr, bl, br) - 테두리그리기 • int box(WINDOW *win, chtype verch, horch) - 역시 테두리 (모서리빠짐)
컬러 처리 #1 • int has_color() - 컬러가 지원되는가? • int start_color() - 컬러를 사용하고 싶을때나 전의 컬러처리루틴을 불러올때에 사용 (여러 색상변수를 초기화 함) • int init_pair(short pair, short fg, short bg) - pair : 바뀐뒤의 색상짝 번호 • int init_color(short, short r, short g, short b) - 색상의 정의 • int can_change_color() 색상변경가능? • int color_content(short color, short *r, short *g, short *b) - 색상내용 • int pair_content(short pair, short *fg, short *bg) - 색상짝 내용 열람.
컬러 처리 #2 • 컬러를 지정하기 • attron(COLOR_PAIR(2); • Example • initscr(); • init_pair(2, COLOR_WHITE, COLOR_BLUE); • attron(COLOR_PAIR(2)); • printw(“TEST...\n”); • refresh(); • endwin(); • TEST...이 파란바탕, 어두운 흰색으로 나타남.. 밝은 흰색은 A_BOLD를 켜주어야 함.
터미널 정보 • int baudrate() - 터미널의 출력속도 • char erasechar() - erase문자는? • int has_colors() • char killchar() - kill시키는 문자.. • char *longname() - 터미널 특성 • example • “Standard Linux Console” • int curs_set(int x) - 커서 감추기 • 0, 1, 2 세단계임. • 기능제공시 상태를 리턴, 아닐때에는 -1을 리턴 • chtype mvwinch(WINDOW*,int,int) - 해당윈도,위치의 문자와 그 속성을 리턴 (inch, winch, mvinch)