X-Git-Url: http://git.madism.org/?p=apps%2Fmadtty.git;a=blobdiff_plain;f=demo%2Fboxshell.c;h=15fbc41865b28eeba3bb28fe3fba7356eae49bc0;hp=c8ef22aeae47d70ddf8e0c339361304b3c8a5190;hb=HEAD;hpb=7a634f01490ab34dcddffdde5b5a3d5dd10f6de5 diff --git a/demo/boxshell.c b/demo/boxshell.c index c8ef22a..15fbc41 100644 --- a/demo/boxshell.c +++ b/demo/boxshell.c @@ -7,6 +7,8 @@ #include +#include +#include #include #include #include @@ -17,9 +19,10 @@ #include -static unsigned char getout = 0; +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) { @@ -28,69 +31,116 @@ void handler(int signo) getout = 1; break; case SIGWINCH: + sigwinch = 1; break; } } +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_vt100_graphics(); + madtty_init_colors(); getmaxyx(stdscr, screen_h, screen_w); /* create a window with a frame */ term_win = newwin(screen_h - 2, screen_w - 2, 1, 1); - rt = madtty_create(screen_h - 2, screen_w - 2); + rt = madtty_create(screen_h - 2, screen_w -2); { 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(rt->pty, &rfds); + FD_SET(0, &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; } } - while ((ch = getch()) != ERR) { -#if 0 - if (ch == KEY_F(1)) { - struct winsize ws = { - .ws_row = --rt->rows, - .ws_col = --rt->cols, - }; - - erase(); - ioctl(rt->pty, TIOCSWINSZ, &ws); - wresize(term_win, rt->rows, rt->cols); + if (sigwinch) { + int fd, cols = -1, rows = -1; + struct winsize w; + + if ((fd = open("/dev/tty", O_RDONLY)) != -1) { + if (ioctl(fd, TIOCGWINSZ, &w) != -1) { + rows = w.ws_row; + cols = w.ws_col; + } + close(fd); + } + if (rows <= 0) { + rows = atoi(getenv("LINES") ?: "24"); } -#endif + if (cols <= 0) { + cols = atoi(getenv("COLUMNS") ?: "80"); + } + + resizeterm(rows, cols); + madtty_resize(rt, rows - 2, cols - 2); + wresize(term_win, rows - 2, cols - 2); + sigwinch = 0; + erase(); + } + + while ((ch = getch()) != ERR) { madtty_keypress(rt, ch); /* pass the keypress for handling */ 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); } }