From: Pierre Habouzit Date: Sat, 11 Nov 2006 13:37:54 +0000 (+0100) Subject: alright, purte rote_vt_update that was completely poorly designed, use a X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=714e9fd2445997a69dec855e59a2a93cb991f3dd;p=apps%2Fmadtty.git alright, purte rote_vt_update that was completely poorly designed, use a nonblocking API in the caller. boxdemo now rocks. Signed-off-by: Pierre Habouzit --- diff --git a/demo/boxshell.c b/demo/boxshell.c index a1d1fa8..43c007a 100644 --- a/demo/boxshell.c +++ b/demo/boxshell.c @@ -89,13 +89,28 @@ int main(int argc, char *argv[]) * 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); + + 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); + } - ch = getch(); - if (ch != ERR) + + 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(); diff --git a/madtty/madtty.c b/madtty/madtty.c index 8fbc469..af959ce 100644 --- a/madtty/madtty.c +++ b/madtty/madtty.c @@ -29,8 +29,6 @@ #include "madtty.h" #include "roteprivate.h" -#define ROTE_VT_UPDATE_ITERATIONS 5 - RoteTerm *rote_vt_create(int rows, int cols) { RoteTerm *rt; @@ -118,9 +116,10 @@ void rote_vt_draw(RoteTerm *rt, WINDOW *win, int srow, int scol, void (*cur_set_attr)(WINDOW*,unsigned char)) { int i, j; - rote_vt_update(rt); - if (!cur_set_attr) cur_set_attr = default_cur_set_attr; + if (!cur_set_attr) + cur_set_attr = default_cur_set_attr; + for (i = 0; i < rt->rows; i++) { wmove(win, srow + i, scol); for (j = 0; j < rt->cols; j++) { @@ -235,29 +234,6 @@ void rote_vt_forsake_child(RoteTerm *rt) rt->childpid = 0; } -void rote_vt_update(RoteTerm *rt) -{ - char buf[512]; - int nbread; - int n = ROTE_VT_UPDATE_ITERATIONS; - - while (n--) { /* iterate at most ROVE_VT_UPDATE_ITERATIONS times. - * As Phil Endecott pointed out, if we don't restrict this, - * a program that floods the terminal with output - * could cause this loop to iterate forever, never - * being able to catch up. So we'll rely on the client - * calling rote_vt_update often, as the documentation - * recommends :-) */ - - nbread = rote_vt_read(rt, buf, sizeof(buf)); - if (nbread <= 0) - return; - - /* inject the data into the terminal */ - rote_vt_inject(rt, buf, nbread); - } -} - int rote_vt_read(RoteTerm *rt, char *buf, int buflen) { if (rt->pty < 0) { diff --git a/madtty/madtty.h b/madtty/madtty.h index 69d7288..1017bc0 100644 --- a/madtty/madtty.h +++ b/madtty/madtty.h @@ -175,15 +175,6 @@ pid_t rote_vt_forkpty(RoteTerm *rt, const char *path, const char *argv[]); * certainly tidy. */ void rote_vt_forsake_child(RoteTerm *rt); -/* Does some data plumbing, that is, sees if the sub process has - * something to write to the terminal, and if so, write it. If you - * called rote_vt_fork to start a forked process, you must call - * this function regularly to update the terminal. - * - * This function will not block, that is, if there is no data to be - * read from the child process it will return immediately. */ -void rote_vt_update(RoteTerm *rt); - int rote_vt_read(RoteTerm *rt, char *buf, int buflen); /* Puts data into the terminal: if there is a forked process running, @@ -217,8 +208,6 @@ void rote_vt_inject(RoteTerm *rt, const char *data, int length); * cursor of the terminal is supposed to be. * * This function does not call wrefresh(win); you have to do that yourself. - * This function automatically calls rote_vt_update prior to drawing - * so that the drawn contents are accurate. */ void rote_vt_draw(RoteTerm *rt, WINDOW *win, int startrow, int startcol, void (*cur_set_attr)(WINDOW *win, unsigned char attr));