X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=demo%2Fboxshell.c;h=505f6b9c0307ec804799555361523938a54f75ae;hb=c3f780f9456ff7adde5b8041596e200a9b41c588;hp=fa7d2eeba098adf0373720c1a2db4d61beca7e8f;hpb=663accfc1b7feeb66176ed03b8c77d4adaa1bc21;p=apps%2Fmadtty.git diff --git a/demo/boxshell.c b/demo/boxshell.c index fa7d2ee..505f6b9 100644 --- a/demo/boxshell.c +++ b/demo/boxshell.c @@ -21,6 +21,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,10 +35,29 @@ 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); @@ -52,22 +72,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 +124,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); } }