From 7a8eff3fa31d7087248dc5bc9fbcf6e57e5dc3e2 Mon Sep 17 00:00:00 2001 From: Serge Vakulenko Date: Sun, 13 Sep 2015 22:19:53 -0700 Subject: [PATCH] Add a few curses examples. --- rootfs.manifest | 10 +++ share/examples/curses/Makefile | 20 ++++++ share/examples/curses/flip.c | 57 +++++++++++++++++ share/examples/curses/jump.c | 57 +++++++++++++++++ share/examples/curses/timer.c | 81 ++++++++++++++++++++++++ share/examples/curses/typetext.c | 103 +++++++++++++++++++++++++++++++ 6 files changed, 328 insertions(+) create mode 100644 share/examples/curses/Makefile create mode 100644 share/examples/curses/flip.c create mode 100644 share/examples/curses/jump.c create mode 100644 share/examples/curses/timer.c create mode 100644 share/examples/curses/typetext.c diff --git a/rootfs.manifest b/rootfs.manifest index e5efb3e..5ea717c 100644 --- a/rootfs.manifest +++ b/rootfs.manifest @@ -1055,6 +1055,16 @@ file /share/examples/cube/demo.c file /share/examples/cube/fubarino.c file /share/examples/cube/duinomite.c +# +# Files: /share/examples/curses +# +dir /share/examples/curses +file /share/examples/curses/Makefile +file /share/examples/curses/flip.c +file /share/examples/curses/jump.c +file /share/examples/curses/timer.c +file /share/examples/curses/typetext.c + # # Files: /share/examples/dhrystone # diff --git a/share/examples/curses/Makefile b/share/examples/curses/Makefile new file mode 100644 index 0000000..ee97857 --- /dev/null +++ b/share/examples/curses/Makefile @@ -0,0 +1,20 @@ +PROG = flip jump timer typetext +#CFLAGS = -DNCURSES_OPAQUE=0 +LIBS = -lcurses -ltermlib + +all: $(PROG) + +clean: + rm -f *.o *~ $(PROG) + +flip: flip.c + cc $(CFLAGS) -o $@ $@.c $(LIBS) + +jump: jump.c + cc $(CFLAGS) -o $@ $@.c $(LIBS) + +timer: timer.c + cc $(CFLAGS) -o $@ $@.c $(LIBS) + +typetext: typetext.c + cc $(CFLAGS) -o $@ $@.c $(LIBS) diff --git a/share/examples/curses/flip.c b/share/examples/curses/flip.c new file mode 100644 index 0000000..959834a --- /dev/null +++ b/share/examples/curses/flip.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +#define W_LINES 10 +#define W_COLS 30 + +#define center(WIND,LN,TEXT) \ + mvwaddstr((WIND), (LN), (W_COLS - strlen(TEXT))/2, (TEXT)) + +void die(sig) +{ + signal(sig, SIG_IGN); + clear(); + refresh(); + endwin(); + exit(0); +} + +int main() +{ + WINDOW *win1, *win2; + + if (! initscr()) { + fprintf(stderr, "Sorry, unknown terminal.\n"); + exit(1); + } + + signal(SIGINT, die); + signal(SIGQUIT, die); + signal(SIGHUP, die); + + noecho(); + + win1 = newwin(W_LINES, W_COLS, ((LINES - W_LINES)/2), ((COLS - W_COLS)/2)); + win2 = newwin(W_LINES, W_COLS, ((LINES - W_LINES)/2 + 4), ((COLS - W_COLS)/2 + 10)); + + scrollok(win1, FALSE); + scrollok(win2, FALSE); + + box(win1, '*', '*'); + box(win2, '*', '*'); + + center(win1, 3, "This is window 1"); + center(win2, 3, "This is window 2"); + + for (;;) { + wrefresh(win1); + touchwin(win1); + usleep(200000); + wrefresh(win2); + touchwin(win2); + usleep(200000); + } +} diff --git a/share/examples/curses/jump.c b/share/examples/curses/jump.c new file mode 100644 index 0000000..1593307 --- /dev/null +++ b/share/examples/curses/jump.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include +#include + +#define W_LINES 7 +#define W_COLS 11 + +void die(sig) +{ + signal(sig, SIG_IGN); + werase(curscr); + wmove(curscr, 0, 0); + wrefresh(curscr); + endwin(); + exit(0); +} + +int main() +{ + WINDOW *win, *blank; + int x, y; + + if (! initscr()) { + fprintf(stderr, "Sorry, unknown terminal.\n"); + exit(1); + } + + signal(SIGINT, die); + signal(SIGQUIT, die); + signal(SIGHUP, die); + + noecho(); + + //delwin(stdscr); + + win = newwin(W_LINES, W_COLS, 0, 0); + blank = newwin(W_LINES, W_COLS, 0, 0); + + box(win, '*', '*'); + mvwaddstr(win, W_LINES/2, (W_COLS - strlen("RetroBSD"))/2, "RetroBSD"); + + srand(time(0)); + + for (;;) { + x = rand() % (COLS - W_COLS); + y = rand() % (LINES - W_LINES); + wrefresh(blank); + if (mvwin(win, y, x) == OK) { + wrefresh(win); + usleep(500000); + mvwin(blank, y, x); + } + } +} diff --git a/share/examples/curses/timer.c b/share/examples/curses/timer.c new file mode 100644 index 0000000..d108a6a --- /dev/null +++ b/share/examples/curses/timer.c @@ -0,0 +1,81 @@ +#include +#include +#include +#include +#include +#include + +#define centered(y,str) mvaddstr(y, (COLS - strlen(str))/2, str) + +void die() +{ + alarm(0); + signal(SIGINT, SIG_IGN); + signal(SIGQUIT, SIG_IGN); + signal(SIGHUP, SIG_IGN); + signal(SIGTERM, SIG_IGN); + clear(); + refresh(); + endwin(); + exit(0); +} + +int main(int ac, char **av) +{ + int move_col, + msec; + int time_line, + move_line; + long num_start_time, + num_now_time; + char str_start_time[60], + str_now_time[60]; + + msec = 0; + if (ac > 1) { + msec = atoi(av[1]); + if (msec < 10) + msec = 10; + else if (msec > 1000) + msec = 1000; + } + if (! initscr()) + exit(1); + + signal(SIGINT, die); + signal(SIGQUIT, die); + signal(SIGHUP, die); + signal(SIGTERM, die); + + box(stdscr, '*', '*'); + + time(&num_start_time); + strcpy(str_start_time, ctime(&num_start_time)); + *(index(str_start_time, '\n')) = '\0'; + + centered(3, "*** Test started at: ***"); + centered(4, str_start_time); + + move_col = 1; + move_line = (LINES - 2) / 2; + time_line = move_line + (LINES - move_line - 1) / 2; + + while (TRUE) { + time(&num_now_time); + strcpy(str_now_time, ctime(&num_now_time)); + *(index(str_now_time, '\n')) = '\0'; + + centered(time_line, str_now_time); + + mvaddstr(move_line, move_col, "<-*->"); + + refresh(); + + mvaddstr(move_line, move_col, " "); + if (++move_col >= (COLS - 6)) + move_col = 1; + + if (msec) + usleep(msec * 1000); + } +} diff --git a/share/examples/curses/typetext.c b/share/examples/curses/typetext.c new file mode 100644 index 0000000..274c72d --- /dev/null +++ b/share/examples/curses/typetext.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include + +#define CNTRL(c) ((c) &037) + +void die(int sig) +{ + signal(sig, SIG_IGN); + move((LINES - 1), 0); + refresh(); + endwin(); + exit(0); +} + +int main() +{ + WINDOW *win, *boxing; + int c; + int x, y; + + initscr(); + + signal(SIGINT, die); + noecho(); + crmode(); + + win = subwin(stdscr, + LINES / 2, + COLS / 2, + LINES / 4, + COLS / 4); + scrollok(win, TRUE); + + boxing = subwin(stdscr, + LINES / 2 + 2, + COLS / 2 + 2, + LINES / 4 - 1, + COLS / 4 - 1); + + box(boxing, '!', '-'); + refresh(); + + wmove(win, 0, 0); + wrefresh(win); + + while ((c = wgetch(win)) != '#') { + if (iscntrl(c)) { + switch (c) { + case CNTRL('E'): + werase(win); + wrefresh(win); + continue; + + case CNTRL('R'): + wrefresh(curscr); + continue; + + case CNTRL('['): + getyx(win, y, x); + c = wgetch(win); + if (c == '[' || c == 'O') + c = wgetch(win); + switch (c) { + case 'H': + x = 0; + y = 0; + goto change; + case 'A': + y--; + goto change; + case 'B': + y++; + goto change; + case 'C': + x++; + goto change; + case 'D': + x--; +change: + if (x >= win->_maxx) { + x = 0; + y++; + } + if (y >= win->_maxy) + y = 0; + wmove(win, y, x); + wrefresh(win); + continue; + default: + break; + } + break; + default: + continue; + } + } + waddch(win, c); + wrefresh(win); + } + die(SIGINT); +}