X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=demo%2Fboxshell.c;h=b3c8f2d9d033c20c1357f901962c1492e55ef7c3;hb=5c99ba24e74e451d0c256b46134d38ee17054565;hp=a1d1fa88abf49592b3490344e62f89b4d8ef3001;hpb=bf09a79a00c99c715e7fbc65bf50634704c4233b;p=apps%2Fmadtty.git diff --git a/demo/boxshell.c b/demo/boxshell.c index a1d1fa8..b3c8f2d 100644 --- a/demo/boxshell.c +++ b/demo/boxshell.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include @@ -21,81 +23,56 @@ void sigchld(int signo __attribute__((unused))) getout = 1; } -int main(int argc, char *argv[]) +int main(void) { - RoteTerm *rt; - int i, j, ch, w, h; + struct timeval next = { 0, 0 }; + madtty_t *rt; signal(SIGCHLD, sigchld); - w = 80; - h = 50; - if (argc > 1) { - char *p = argv[1]; - w = strtol(p, &p, 10); - if (*p++ == 'x') - h = strtol(p, &p, 10); - } - - initscr(); - noecho(); - start_color(); - raw(); - nodelay(stdscr, TRUE); /* prevents getch() from blocking; rather - * it will return ERR when there is no - * keypress available */ - - keypad(stdscr, TRUE); /* necessary to use rote_vt_keypress */ + madtty_initialize(); getmaxyx(stdscr, screen_h, screen_w); - /* initialize the color pairs the way rote_vt_draw expects it. You might - * initialize them differently, but in that case you would need - * to supply a custom conversion function for rote_vt_draw to - * call when setting attributes. The idea of this "default" mapping - * is to map (fg, bg) to the color pair bg * 8 + 7 - fg. This way, - * the pair (white, black) ends up mapped to 0, which means that - * it does not need a color pair (since it is the default). Since - * there are only 63 available color pairs (and 64 possible fg/bg - * combinations), we really have to save 1 pair by assigning no pair - * to the combination white/black. */ - for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) - if (i != 7 || j != 0) - init_pair(j*8+7-i, i, j); - - /* paint the screen blue */ - attrset(COLOR_PAIR(32)); - for (i = 0; i < screen_h; i++) - for (j = 0; j < screen_w; j++) - addch(' '); - 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 */ - 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); + term_win = newwin(screen_h - 2, screen_w - 2, 1, 1); + rt = madtty_create(screen_h - 2, screen_w - 2); { - 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); + madtty_forkpty(rt, path, args); } /* keep reading keypresses from the user and passing them to the terminal; * also, redraw the terminal to the window at each iteration */ - ch = '\0'; while (!getout) { - usleep(10000); - rote_vt_draw(rt, term_win, 1, 1, NULL); - wrefresh(term_win); - - ch = getch(); - if (ch != ERR) - rote_vt_keypress(rt, ch); /* pass the keypress for handling */ + fd_set rfds; + struct timeval tv = { 0 , 1000 }, t; + int ch; + + FD_ZERO(&rfds); + FD_SET(rt->pty, &rfds); + + if (select(rt->pty + 1, &rfds, NULL, NULL, &tv) > 0) { + if (madtty_process(rt)) + break; + } + + while ((ch = getch()) != ERR) { + madtty_keypress(rt, ch); /* pass the keypress for handling */ + } + + gettimeofday(&t, NULL); + if (timercmp(&t, &next, >=)) { + madtty_draw(rt, term_win, 0, 0); + wrefresh(term_win); + gettimeofday(&next, NULL); + next.tv_usec += 1000 * 1000 / 50; + if (next.tv_usec > 1000 * 1000) { + next.tv_usec -= 1000 * 1000; + next.tv_sec++; + } + } } endwin();