--- ray/src/rt/devcomm.c 1990/01/30 11:37:36 1.11 +++ ray/src/rt/devcomm.c 1993/11/18 09:42:58 2.4 @@ -12,42 +12,60 @@ static char SCCSid[] = "$SunId$ LBL"; #include "standard.h" -#include - #include "color.h" #include "driver.h" +#include "vfork.h" + #ifndef DEVPATH #define DEVPATH getenv("PATH") /* device search path */ #endif -#ifndef BSD -#define vfork fork -#endif +static int comm_close(), comm_clear(), comm_paintr(), + comm_getcur(), comm_comout(), comm_comin(), comm_flush(); -#ifndef WFLUSH -#define WFLUSH 30 /* flush after this many rays */ -#endif - -extern char *getpath(), *getenv(); - -int onsigio(); - -int comm_close(), comm_clear(), comm_paintr(), comm_errout(), - comm_getcur(), comm_comout(), comm_comin(); - -struct driver comm_driver, comm_default = { +struct driver comm_driver = { comm_close, comm_clear, comm_paintr, comm_getcur, - comm_comout, comm_comin + comm_comout, comm_comin, comm_flush }; +static int mygets(), myputs(), reply_error(), getstate(); + FILE *devin, *devout; int devchild; +static struct driver * +final_connect() /* verify and initialize connection */ +{ + putw(COM_SENDM, devout); + fflush(devout); + if (getw(devin) != COM_RECVM) + return(NULL); + /* get driver parameters */ + getstate(); + /* set error vectors */ + cmdvec = comm_comout; + if (wrnvec != NULL) + wrnvec = comm_comout; + return(&comm_driver); +} + + struct driver * +slave_init(dname, id) /* run rview in slave mode */ +char *dname, *id; +{ + devchild = -1; /* we're the slave here */ + devout = stdout; /* use standard input */ + devin = stdin; /* and standard output */ + return(final_connect()); /* verify initialization */ +} + + +struct driver * comm_init(dname, id) /* set up and execute driver */ char *dname, *id; { @@ -55,7 +73,7 @@ char *dname, *id; int p1[2], p2[2]; char pin[16], pout[16]; /* find driver program */ - if ((devname = getpath(dname, DEVPATH, 1)) == NULL) { + if ((devname = getpath(dname, DEVPATH, X_OK)) == NULL) { stderr_v(dname); stderr_v(": not found\n"); return(NULL); @@ -80,23 +98,7 @@ char *dname, *id; goto syserr; if ((devin = fdopen(p2[0], "r")) == NULL) goto syserr; - copystruct(&comm_driver, &comm_default); - /* verify & get resolution */ - putw(COM_SENDM, devout); - fflush(devout); - if (getw(devin) != COM_RECVM) - return(NULL); - fread((char *)&comm_driver.pixaspect, - sizeof(comm_driver.pixaspect), 1, devin); - comm_driver.xsiz = getw(devin); - comm_driver.ysiz = getw(devin); - /* input handling */ - signal(SIGIO, onsigio); - /* set error vectors */ - cmdvec = comm_comout; - if (wrnvec != NULL) - wrnvec = comm_comout; - return(&comm_driver); + return(final_connect()); /* verify initialization */ syserr: perror(dname); return(NULL); @@ -111,9 +113,10 @@ comm_close() /* done with driver */ cmdvec = NULL; /* reset error vectors */ if (wrnvec != NULL) wrnvec = stderr_v; - signal(SIGIO, SIG_DFL); fclose(devout); fclose(devin); + if (devchild < 0) + return; while ((pid = wait(0)) != -1 && pid != devchild) ; } @@ -135,22 +138,26 @@ comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a re COLOR col; int xmin, ymin, xmax, ymax; { - extern long nrays; /* number of rays traced */ - static long lastflush = 0; /* ray count at last flush */ - putc(COM_PAINTR, devout); fwrite((char *)col, sizeof(COLOR), 1, devout); putw(xmin, devout); putw(ymin, devout); putw(xmax, devout); putw(ymax, devout); - if (nrays - lastflush >= WFLUSH) { - fflush(devout); - lastflush = nrays; - } } +static +comm_flush() /* flush output to driver */ +{ + putc(COM_FLUSH, devout); + fflush(devout); + if (getc(devin) != COM_FLUSH) + reply_error("flush"); + getstate(); +} + + static int comm_getcur(xp, yp) /* get and return cursor position */ int *xp, *yp; @@ -174,7 +181,8 @@ char *str; { putc(COM_COMOUT, devout); myputs(str, devout); - fflush(devout); + if (str[strlen(str)-1] == '\n') + fflush(devout); } @@ -194,21 +202,11 @@ char *prompt; if (getc(devin) != COM_COMIN) reply_error("comin"); mygets(buf, devin); - if (comm_driver.inpready > 0) - comm_driver.inpready--; + getstate(); } static -comm_errout(str) /* display an error message */ -char *str; -{ - comm_comout(str); - stderr_v(str); /* send to standard error also */ -} - - -static mygets(s, fp) /* get string from file (with nul) */ register char *s; register FILE *fp; @@ -244,7 +242,11 @@ char *routine; static -onsigio() /* input ready */ +getstate() /* get driver state variables */ { - comm_driver.inpready++; + fread((char *)&comm_driver.pixaspect, + sizeof(comm_driver.pixaspect), 1, devin); + comm_driver.xsiz = getw(devin); + comm_driver.ysiz = getw(devin); + comm_driver.inpready = getw(devin); }