nonblocking API in the caller.
boxdemo now rocks.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
* 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();
#include "madtty.h"
#include "roteprivate.h"
-#define ROTE_VT_UPDATE_ITERATIONS 5
-
RoteTerm *rote_vt_create(int rows, int cols)
{
RoteTerm *rt;
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++) {
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) {
* 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,
* 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));