--- ray/src/rt/devcomm.c 1989/06/02 17:21:24 1.4 +++ ray/src/rt/devcomm.c 1990/03/21 09:46:55 1.17 @@ -10,10 +10,8 @@ static char SCCSid[] = "$SunId$ LBL"; * 10/5/88 */ -#include +#include "standard.h" -#include - #include "color.h" #include "driver.h" @@ -22,25 +20,22 @@ static char SCCSid[] = "$SunId$ LBL"; #define DEVPATH getenv("PATH") /* device search path */ #endif +#ifndef DELAY +#define DELAY 20 /* seconds to wait for response */ +#endif + #ifndef BSD #define vfork fork #endif -#ifndef WFLUSH -#define WFLUSH 50 /* flush after this many rays */ -#endif - extern char *getpath(), *getenv(); -int onsigio(); +static int comm_close(), comm_clear(), comm_paintr(), comm_errout(), + comm_getcur(), comm_comout(), comm_comin(), comm_flush(); -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; @@ -49,33 +44,28 @@ int devchild; struct driver * -comm_init(argv) /* set up and execute driver */ -char *argv[]; +comm_init(dname, id) /* set up and execute driver */ +char *dname, *id; { char *devname; int p1[2], p2[2]; - - if ((devname = getpath(argv[0], DEVPATH)) == NULL) { - stderr_v(argv[0]); + char pin[16], pout[16]; + /* find driver program */ + if ((devname = getpath(dname, DEVPATH, 1)) == NULL) { + stderr_v(dname); stderr_v(": not found\n"); return(NULL); } + /* open communication pipes */ if (pipe(p1) == -1 || pipe(p2) == -1) goto syserr; - if ((devchild = vfork()) == 0) { + if ((devchild = vfork()) == 0) { /* fork driver process */ close(p1[1]); close(p2[0]); - if (p1[0] != 0) { - dup2(p1[0], 0); - close(p1[0]); - } - if (p2[1] != 1) { - dup2(p2[1], 1); - close(p2[1]); - } - execv(devname, argv); - stderr_v(devname); - stderr_v(": cannot execute\n"); + sprintf(pin, "%d", p1[0]); + sprintf(pout, "%d", p2[1]); + execl(devname, dname, pin, pout, id, 0); + perror(devname); _exit(127); } if (devchild == -1) @@ -86,14 +76,20 @@ char *argv[]; goto syserr; if ((devin = fdopen(p2[0], "r")) == NULL) goto syserr; - bcopy(&comm_default, &comm_driver, sizeof(comm_driver)); - signal(SIGIO, onsigio); - cmdvec = comm_comout; /* set error vectors */ + /* verify initialization */ + 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); syserr: - perror(argv[0]); + perror(dname); return(NULL); } @@ -106,7 +102,6 @@ comm_close() /* done with driver */ cmdvec = NULL; /* reset error vectors */ if (wrnvec != NULL) wrnvec = stderr_v; - signal(SIGIO, SIG_DFL); fclose(devout); fclose(devin); while ((pid = wait(0)) != -1 && pid != devchild) @@ -119,8 +114,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); } @@ -130,22 +125,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; @@ -157,8 +156,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); } @@ -169,20 +168,26 @@ char *str; { putc(COM_COMOUT, devout); myputs(str, devout); - 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); - comm_driver.inpready = 0; + getstate(); } @@ -231,7 +236,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); }