X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=demo%2Fboxshell.c;h=806a1ff2394df7425546c764fc18d545df708dd0;hb=6c58525bf7665386fc72f1c5b1a217764c19f775;hp=fa7d2eeba098adf0373720c1a2db4d61beca7e8f;hpb=663accfc1b7feeb66176ed03b8c77d4adaa1bc21;p=apps%2Fmadtty.git diff --git a/demo/boxshell.c b/demo/boxshell.c index fa7d2ee..806a1ff 100644 --- a/demo/boxshell.c +++ b/demo/boxshell.c @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -21,6 +22,7 @@ static int getout = 0, sigwinch = 0; static int screen_w, screen_h; static WINDOW *term_win; +static struct timeval const slice = { 0, 1000 * 1000 / 100 }; void handler(int signo) { @@ -34,15 +36,43 @@ void handler(int signo) } } +static struct timeval timeval_add(struct timeval a, struct timeval b) +{ + int usec = a.tv_usec + b.tv_usec; + a.tv_sec += b.tv_sec; + while (usec > 1000 * 1000) { + a.tv_sec += 1; + usec -= 1000 * 1000; + } + a.tv_usec = usec; + return a; +} + +static int is_expired(struct timeval now, struct timeval expiry) +{ + return now.tv_sec > expiry.tv_sec + || (now.tv_sec == expiry.tv_sec && now.tv_usec > expiry.tv_usec); +} + int main(void) { madtty_t *rt; - int dirty = 0; + int dirty = 0, pty; + struct timeval next; signal(SIGCHLD, handler); signal(SIGWINCH, handler); - madtty_initialize(); + setlocale(LC_ALL, ""); + initscr(); + start_color(); + noecho(); + raw(); + nodelay(stdscr, TRUE); + keypad(stdscr, TRUE); + curs_set(0); + ESCDELAY=50; + madtty_init_colors(); getmaxyx(stdscr, screen_h, screen_w); /* create a window with a frame */ @@ -52,22 +82,23 @@ int main(void) const char *path = getenv("SHELL") ?: "/bin/sh"; const char *args[] = { path, "--login", NULL}; - madtty_forkpty(rt, path, args); + madtty_forkpty(rt, path, args, &pty); } /* keep reading keypresses from the user and passing them to the terminal; * also, redraw the terminal to the window at each iteration */ + gettimeofday(&next, NULL); while (!getout) { + struct timeval tv = { 0, 1000 * 1000 / 100 }; fd_set rfds; - struct timeval tv = { 0, 1000 * 1000 / 50 }; int ch; FD_ZERO(&rfds); FD_SET(0, &rfds); - FD_SET(rt->pty, &rfds); + FD_SET(pty, &rfds); - if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) { - if (FD_ISSET(rt->pty, &rfds)) { + if (select(pty + 1, &rfds, NULL, NULL, &tv) > 0) { + if (FD_ISSET(pty, &rfds)) { madtty_process(rt); dirty = 1; } @@ -103,10 +134,12 @@ int main(void) dirty = 1; } - if (dirty) { + gettimeofday(&tv, NULL); + if (dirty && is_expired(tv, next)) { madtty_draw(rt, term_win, 0, 0); wrefresh(term_win); dirty = 0; + next = timeval_add(tv, slice); } }