Add a few curses examples.

This commit is contained in:
Serge Vakulenko
2015-09-13 22:19:53 -07:00
parent 261f70aef6
commit 7a8eff3fa3
6 changed files with 328 additions and 0 deletions

View File

@@ -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
#

View File

@@ -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)

View File

@@ -0,0 +1,57 @@
#include <curses.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#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);
}
}

View File

@@ -0,0 +1,57 @@
#include <curses.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#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);
}
}
}

View File

@@ -0,0 +1,81 @@
#include <curses.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#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);
}
}

View File

@@ -0,0 +1,103 @@
#include <curses.h>
#include <signal.h>
#include <ctype.h>
#include <stdlib.h>
#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);
}