X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=demo%2Fboxshell.c;h=43c007a2a90ab95281e4c9d48f2f76b6fee2c38a;hb=880886ae6dca338d2d57e4b85cf984f4022ec520;hp=63780e37ebfc7e0305f63c6eb2da75c12a88cfae;hpb=0bc01873b2eaadc8074443abecd8ef50119bcc9e;p=apps%2Fmadtty.git diff --git a/demo/boxshell.c b/demo/boxshell.c index 63780e3..43c007a 100644 --- a/demo/boxshell.c +++ b/demo/boxshell.c @@ -21,46 +21,22 @@ void sigchld(int signo __attribute__((unused))) getout = 1; } -int my_custom_handler(RoteTerm *rt __attribute__((unused)), const char *es) -{ - int color; - int i, j; - - /* if the escape sequence does not begin with '{', we give up */ - if (*es != '{') return ROTE_HANDLERESULT_NOWAY; - - /* ok, now we know it begins with '{'. Now, if it does not end with '}', - * it is not yet complete */ - if (es[strlen(es)-1] != '}') return ROTE_HANDLERESULT_NOTYET; - - /* ok, the sequence is complete */ - color = atoi(es + 1); - if (color < 0 || color > 7) return false; /* don't recognize it */ - - /* paint the background with that color */ - attrset(COLOR_PAIR(color * 8)); - move(0, 0); - for (i = 0; i < screen_h; i++) for (j = 0; j < screen_w; j++) addch(' '); - touchwin(stdscr); - refresh(); - - /* touch term_win to force it to do a full redraw next time */ - touchwin(term_win); - - /* and redraw the terminal window */ - wrefresh(term_win); - - /* escape sequence was handled ok */ - return ROTE_HANDLERESULT_OK; -} - -int main() +int main(int argc, char *argv[]) { RoteTerm *rt; - int i, j, ch; + int i, j, ch, w, h; 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(); @@ -88,36 +64,53 @@ int main() /* paint the screen blue */ attrset(COLOR_PAIR(32)); - for (i = 0; i < screen_h; i++) for (j = 0; j < screen_w; j++) addch(' '); + 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(50, 82, 2, 4); + 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); + 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(48, 80); - rote_vt_forkpty(rt, "/bin/bash --login"); + rt = rote_vt_create(h, w); + { + const char *path = "/bin/bash"; + const char *args[] = {"/bin/bash", "--login", NULL}; - /* add a sample custom escape sequence handler... say we want to handle - * the sequence, say, \e{N}, which will change the application's background - * color to N (where N is a number between 0 and 7). */ - rote_vt_install_handler(rt, my_custom_handler); + rote_vt_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); + fd_set rfds; + struct timeval tv = { 0 , 1000 }; + + FD_ZERO(&rfds); + FD_SET(rt->pty, &rfds); - ch = getch(); - if (ch != ERR) + 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); + } + + + 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); } endwin();