From cf5ab7bbee963f1cc5c0c4dd99c0c7e3a271bcf0 Mon Sep 17 00:00:00 2001 From: Faisal Abbas <90.abbasfaisal@gmail.com> Date: Mon, 17 Aug 2015 17:35:06 +0500 Subject: [PATCH] curses: some small test programs --- Applications/curses-tests/cbreak_echo.c | 59 +++++++++ Applications/curses-tests/curs_tools.c | 125 +++++++++++++++++++ Applications/curses-tests/multiple_windows.c | 81 ++++++++++++ Applications/curses-tests/overwrite.c | 30 +++++ Applications/curses-tests/scroll.c | 24 ++++ Applications/curses-tests/subwindow.c | 48 +++++++ 6 files changed, 367 insertions(+) create mode 100644 Applications/curses-tests/cbreak_echo.c create mode 100644 Applications/curses-tests/curs_tools.c create mode 100644 Applications/curses-tests/multiple_windows.c create mode 100644 Applications/curses-tests/overwrite.c create mode 100644 Applications/curses-tests/scroll.c create mode 100644 Applications/curses-tests/subwindow.c diff --git a/Applications/curses-tests/cbreak_echo.c b/Applications/curses-tests/cbreak_echo.c new file mode 100644 index 00000000..7d2a6598 --- /dev/null +++ b/Applications/curses-tests/cbreak_echo.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +#define PW_LEN 256 +#define NAME_LEN 256 + +int main() { + + char name[NAME_LEN]; + char password[PW_LEN]; + const char *real_password = "abcxyz"; + + int i = 0; + + initscr(); + move(5, 10); + printw("%s", "Please login:"); + move(7, 10); + printw("%s", "User name: "); + refresh(); + getstr(name); + move(8, 10); + printw("%s", "Password: "); + refresh(); + + cbreak(); + noecho(); + + _tty.c_cc[VMIN] = 1; + tcsetattr(0, TCSANOW, &_tty); + + memset(password, '\0', sizeof(password)); + while (i < PW_LEN) { + password[i] = getch(); + if (password[i] == '\r'| password[i] == '\n') break; + move(8, 20 + i); + addch('*'); + refresh(); + i++; + } + + + echo(); + nocbreak(); + move(11, 10); + + if (strncmp(real_password, password, strlen(real_password)) == 0) + printw("%s", "Correct"); + else printw("%s", "Wrong"); + printw("%s", " password"); + refresh(); + + sleep(2); + endwin(); + exit(EXIT_SUCCESS); +} + diff --git a/Applications/curses-tests/curs_tools.c b/Applications/curses-tests/curs_tools.c new file mode 100644 index 00000000..28fe2aac --- /dev/null +++ b/Applications/curses-tests/curs_tools.c @@ -0,0 +1,125 @@ +#include +#include +#include +#include + +#define Y 18 +#define gotosleep() sleep(5) + +void main() +{ + WINDOW *main_window_ptr; + WINDOW *status_window_ptr; + char c; + int i = 0; + + initscr(); + + main_window_ptr = newwin(20, 80, 0, 0); + status_window_ptr = newwin(1, 80, 21, 0); + + while (i++ < (Y - 1)) + wprintw(main_window_ptr, "testing curses functions.\n"); + + touchwin(main_window_ptr); wrefresh(main_window_ptr); + + gotosleep(); + + wmove(main_window_ptr, 0 , 0); + + c = (char)winch(main_window_ptr); + + werase(status_window_ptr); wrefresh(status_window_ptr); + mvwprintw(status_window_ptr, 0, 0, "winch: char at (%d, %d) is %c\n", main_window_ptr->_curx, main_window_ptr->_cury, c); + touchwin(status_window_ptr); wrefresh(status_window_ptr); + + gotosleep(); + + c = (char)mvwinch(main_window_ptr, 9, 2); + werase(status_window_ptr); wrefresh(status_window_ptr); + mvwprintw(status_window_ptr, 0, 0, "winch: char at (%d, %d) is %c\n", main_window_ptr->_curx, main_window_ptr->_cury, c); + touchwin(status_window_ptr); wrefresh(status_window_ptr); + gotosleep(); + + werase(status_window_ptr); wrefresh(status_window_ptr); + mvwprintw(status_window_ptr, 0, 0, "wclrtoeol"); + touchwin(status_window_ptr); wrefresh(status_window_ptr); + + gotosleep(); + + wmove(main_window_ptr, 0, 7); + wclrtoeol(main_window_ptr); + touchwin(main_window_ptr); + wrefresh(main_window_ptr); + + gotosleep(); + + werase(status_window_ptr); wrefresh(status_window_ptr); + mvwprintw(status_window_ptr, 0, 0, "winsch"); + touchwin(status_window_ptr); wrefresh(status_window_ptr); + + gotosleep(); + + wmove(main_window_ptr, 6, 0); + winsch(main_window_ptr, 't'); + touchwin(main_window_ptr); + wrefresh(main_window_ptr); + + gotosleep(); + + werase(status_window_ptr); wrefresh(status_window_ptr); + mvwprintw(status_window_ptr, 0, 0, "wdelch"); + touchwin(status_window_ptr); wrefresh(status_window_ptr); + + gotosleep(); + + wmove(main_window_ptr, 6, 0); + wdelch(main_window_ptr); + touchwin(main_window_ptr); + wrefresh(main_window_ptr); + + gotosleep(); + + werase(status_window_ptr); wrefresh(status_window_ptr); + mvwprintw(status_window_ptr, 0, 0, "wdeleteln"); + touchwin(status_window_ptr); wrefresh(status_window_ptr); + + gotosleep(); + + wmove(main_window_ptr, 7, 0); + wdeleteln(main_window_ptr); + touchwin(main_window_ptr); + wrefresh(main_window_ptr); + + gotosleep(); + + werase(status_window_ptr); wrefresh(status_window_ptr); + mvwprintw(status_window_ptr, 0, 0, "winsertln"); + touchwin(status_window_ptr); wrefresh(status_window_ptr); + + gotosleep(); + + wmove(main_window_ptr, 9, 0); + winsertln(main_window_ptr); + touchwin(main_window_ptr); + wrefresh(main_window_ptr); + + gotosleep(); + + werase(status_window_ptr); wrefresh(status_window_ptr); + mvwprintw(status_window_ptr, 0, 0, "wclrtobot"); + touchwin(status_window_ptr); wrefresh(status_window_ptr); + + gotosleep(); + + wmove(main_window_ptr, 9, 0); + wclrtobot(main_window_ptr); + touchwin(main_window_ptr); + wrefresh(main_window_ptr); + + gotosleep(); + + delwin(main_window_ptr); + delwin(status_window_ptr); + endwin(); +} diff --git a/Applications/curses-tests/multiple_windows.c b/Applications/curses-tests/multiple_windows.c new file mode 100644 index 00000000..6e6b0113 --- /dev/null +++ b/Applications/curses-tests/multiple_windows.c @@ -0,0 +1,81 @@ +#include +#include +#include + +int main() +{ + WINDOW *new_window_ptr; + WINDOW *popup_window_ptr; + int x_loop; + int y_loop; + char a_letter = 'a'; + + initscr(); + move(5, 5); + printw("%s", "Testing multiple windows"); + refresh(); + for (y_loop = 0; y_loop < LINES - 1; y_loop++) { + for (x_loop = 0; x_loop < COLS - 1; x_loop++) { + mvwaddch(stdscr, y_loop, x_loop, a_letter); + a_letter++; + if (a_letter > 'z') a_letter = 'a'; + } + } + + /* Update the screen */ + refresh(); + sleep(2); + + new_window_ptr = newwin(10, 20,5, 5); + mvwprintw(new_window_ptr, 2, 2,"%s", "Hello World"); + mvwprintw(new_window_ptr, 5, 2, "%s", "Notice how very long lines wrap inside the window"); + wrefresh(new_window_ptr); + sleep(2); + + for (y_loop = 0; y_loop < LINES -1; y_loop++) { + for (x_loop = 0; x_loop < COLS - 1; x_loop++) { + mvwaddch(stdscr, y_loop, x_loop, a_letter); + a_letter++; + if (a_letter > '9') + a_letter = '0'; + } + } + + refresh(); + sleep(2); + + wrefresh(new_window_ptr); + sleep(2); + + touchwin(new_window_ptr); + wrefresh(new_window_ptr); + + sleep(2); + popup_window_ptr = newwin(10, 20, 8, 8); + box(popup_window_ptr, '|', '-'); + mvwprintw(popup_window_ptr, 5, 2, "%s", "Pop Up Window!"); + wrefresh(popup_window_ptr); + sleep(2); + + touchwin(new_window_ptr); + wrefresh(new_window_ptr); + + sleep(2); + wclear(new_window_ptr); + wrefresh(new_window_ptr); + + sleep(2); + delwin(new_window_ptr); + touchwin(popup_window_ptr); + wrefresh(popup_window_ptr); + sleep(2); + + delwin(popup_window_ptr); + touchwin(stdscr); + refresh(); + + sleep(2); + + endwin(); + exit(EXIT_SUCCESS); +} diff --git a/Applications/curses-tests/overwrite.c b/Applications/curses-tests/overwrite.c new file mode 100644 index 00000000..cc90cabe --- /dev/null +++ b/Applications/curses-tests/overwrite.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +void main() +{ + WINDOW *w = newwin(5, 20, 5, 5); + int i = 0; + + initscr(); + + while (i++ < (LINES * COLS)/2) + printw("a "); + + refresh(); + sleep(5); + + wprintw(w, "b b b b b b b b b b \nb b b b b b b "); + + touchwin(w); + + overwrite(w, stdscr); + + refresh(); + + sleep(5); + delwin(w); + endwin(); +} diff --git a/Applications/curses-tests/scroll.c b/Applications/curses-tests/scroll.c new file mode 100644 index 00000000..47e8e535 --- /dev/null +++ b/Applications/curses-tests/scroll.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include + +int main(void) +{ + int i = 0; + + initscr(); + + scrollok(stdscr,TRUE); + + while(i < 250) + { + printw("%d - lots and lots of lines flowing down the terminal\n", i); + ++i; + refresh(); + } + + getch(); + endwin(); + return 0; +} diff --git a/Applications/curses-tests/subwindow.c b/Applications/curses-tests/subwindow.c new file mode 100644 index 00000000..8d755680 --- /dev/null +++ b/Applications/curses-tests/subwindow.c @@ -0,0 +1,48 @@ +#include +#include +#include + +int main() +{ + WINDOW *sub_window_ptr; + int x_loop; + int y_loop; + int counter; + char a_letter = '1'; + + initscr(); + for (y_loop = 0; y_loop < LINES - 1; y_loop++) { + for (x_loop = 0; x_loop < COLS - 1; x_loop++) { + mvwaddch(stdscr, y_loop, x_loop, a_letter); + a_letter++; + if (a_letter > '9') a_letter = '1'; + } + } + + sub_window_ptr = subwin(stdscr, 10, 20, 10, 10); + scrollok(sub_window_ptr, 1); + touchwin(stdscr); + refresh(); + sleep(1); + + werase(sub_window_ptr); + mvwprintw(sub_window_ptr, 2, 0, "%s", "This window will now scroll"); + wrefresh(sub_window_ptr); + sleep(1); + + for (counter = 1; counter < 10; counter++) { + wprintw(sub_window_ptr, "%s", "This text is both wrapping and \ + scrolling."); + wrefresh(sub_window_ptr); + sleep(1); + } + + delwin(sub_window_ptr); + touchwin(stdscr); + refresh(); + sleep(1); + endwin(); + + exit(EXIT_SUCCESS); +} + -- 2.34.1