X-Git-Url: http://git.madism.org/?p=apps%2Fmadtty.git;a=blobdiff_plain;f=demo%2Fboxshell.c;fp=demo%2Fboxshell.c;h=a1d746e2c0627c4d18ccb9615a076ca62a6e910c;hp=43c007a2a90ab95281e4c9d48f2f76b6fee2c38a;hb=1f877e6f07a48bc78f0b38d4fba4306d3a4688a2;hpb=f825f4ca53c05c78e52b7e007e07ad76587fb46c diff --git a/demo/boxshell.c b/demo/boxshell.c index 43c007a..a1d746e 100644 --- a/demo/boxshell.c +++ b/demo/boxshell.c @@ -6,9 +6,12 @@ */ #include +#include #include #include #include +#include +#include #include @@ -23,13 +26,15 @@ void sigchld(int signo __attribute__((unused))) int main(int argc, char *argv[]) { + struct timeval next = { 0, 0 }; RoteTerm *rt; - int i, j, ch, w, h; + int i, j, ch, w, h, pos; + char buf[BUFSIZ]; signal(SIGCHLD, sigchld); w = 80; - h = 50; + h = 40; if (argc > 1) { char *p = argv[1]; w = strtol(p, &p, 10); @@ -37,6 +42,7 @@ int main(int argc, char *argv[]) h = strtol(p, &p, 10); } + setlocale(LC_ALL, ""); initscr(); noecho(); start_color(); @@ -70,17 +76,15 @@ int main(int argc, char *argv[]) refresh(); /* create a window with a frame */ - term_win = newwin(h + 2, w + 2, 1, 2); - wattrset(term_win, COLOR_PAIR(7*8+7-0)); /* black over white */ + term_win = newwin(h + 2, w + 2, 2, 3); wborder(term_win, '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'); mvwprintw(term_win, 0, 27, " Term In a Box "); wrefresh(term_win); - /* create the terminal and have it run bash */ rt = rote_vt_create(h, w); { - const char *path = "/bin/bash"; - const char *args[] = {"/bin/bash", "--login", NULL}; + const char *path = getenv("SHELL") ?: "/bin/sh"; + const char *args[] = { path, "--login", NULL}; rote_vt_forkpty(rt, path, args); } @@ -88,29 +92,44 @@ int main(int argc, char *argv[]) /* keep reading keypresses from the user and passing them to the terminal; * also, redraw the terminal to the window at each iteration */ ch = '\0'; + pos = 0; while (!getout) { fd_set rfds; - struct timeval tv = { 0 , 1000 }; + struct timeval tv = { 0 , 1000 }, t; FD_ZERO(&rfds); FD_SET(rt->pty, &rfds); if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) { - char buf[512]; - int nbread; - - nbread = rote_vt_read(rt, buf, sizeof(buf)); - if (nbread > 0) - rote_vt_inject(rt, buf, nbread); + int nb; + + nb = rote_vt_read(rt, buf + pos, sizeof(buf) - pos); + if (nb <= 0) + continue; + pos += nb; + + nb = rote_vt_inject(rt, buf, pos); + if (nb <= 0) + continue; + memmove(buf, buf + nb, pos - nb); + pos -= nb; } - while ((ch = getch()) != ERR) { rote_vt_keypress(rt, ch); /* pass the keypress for handling */ } - rote_vt_draw(rt, term_win, 1, 1, NULL); - wrefresh(term_win); + gettimeofday(&t, NULL); + if (timercmp(&t, &next, >=)) { + rote_vt_draw(rt, term_win, 1, 1); + wrefresh(term_win); + gettimeofday(&next, NULL); + next.tv_usec += 1000 * 1000 / 100; + if (next.tv_usec > 1000 * 1000) { + next.tv_usec -= 1000 * 1000; + next.tv_sec++; + } + } } endwin();