--- ray/src/rt/devcomm.c 1989/11/01 17:33:03 1.7 +++ ray/src/rt/devcomm.c 1993/04/05 15:16:47 2.3 @@ -10,37 +10,24 @@ static char SCCSid[] = "$SunId$ LBL"; * 10/5/88 */ -#include +#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, - MAXRES, MAXRES, 0 + comm_comout, comm_comin, comm_flush }; FILE *devin, *devout; @@ -48,7 +35,35 @@ 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; { @@ -56,7 +71,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); @@ -81,21 +96,7 @@ char *dname, *id; goto syserr; if ((devin = fdopen(p2[0], "r")) == NULL) goto syserr; - bcopy(&comm_default, &comm_driver, sizeof(comm_driver)); - /* verify & get resolution */ - putw(COM_SENDM, devout); - fflush(devout); - if (getw(devin) != COM_RECVM) - return(NULL); - 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); @@ -110,9 +111,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) ; } @@ -123,8 +125,8 @@ comm_clear(xres, yres) /* clear screen */ int xres, yres; { putc(COM_CLEAR, devout); - fwrite(&xres, sizeof(xres), 1, devout); - fwrite(&yres, sizeof(yres), 1, devout); + putw(xres, devout); + putw(yres, devout); fflush(devout); } @@ -134,22 +136,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(col, sizeof(COLOR), 1, devout); - fwrite(&xmin, sizeof(xmin), 1, devout); - fwrite(&ymin, sizeof(ymin), 1, devout); - fwrite(&xmax, sizeof(xmax), 1, devout); - fwrite(&ymax, sizeof(ymax), 1, devout); - if (nrays - lastflush >= WFLUSH) { - fflush(devout); - lastflush = nrays; - } + fwrite((char *)col, sizeof(COLOR), 1, devout); + putw(xmin, devout); + putw(ymin, devout); + putw(xmax, devout); + putw(ymax, devout); } +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; @@ -161,8 +167,8 @@ int *xp, *yp; if (getc(devin) != COM_GETCUR) reply_error("getcur"); c = getc(devin); - fread(xp, sizeof(*xp), 1, devin); - fread(yp, sizeof(*yp), 1, devin); + *xp = getw(devin); + *yp = getw(devin); return(c); } @@ -173,34 +179,32 @@ char *str; { putc(COM_COMOUT, devout); myputs(str, devout); - fflush(devout); + if (str[strlen(str)-1] == '\n') + fflush(devout); } static -comm_comin(buf) /* read string from command line */ +comm_comin(buf, prompt) /* read string from command line */ char *buf; +char *prompt; { putc(COM_COMIN, devout); + if (prompt == NULL) + putc(0, devout); + else { + putc(1, devout); + myputs(prompt, devout); + } fflush(devout); 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; @@ -236,7 +240,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); }