ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
(Generate patch)

Comparing ray/src/rt/devcomm.c (file contents):
Revision 1.3 by greg, Fri May 26 10:27:16 1989 UTC vs.
Revision 2.3 by greg, Mon Apr 5 15:16:47 1993 UTC

# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10   *      10/5/88
11   */
12  
13 < #include <stdio.h>
13 > #include "standard.h"
14  
15 #include <signal.h>
16
15   #include "color.h"
16  
17   #include "driver.h"
18  
19 + #include "vfork.h"
20 +
21   #ifndef DEVPATH
22   #define DEVPATH         getenv("PATH")  /* device search path */
23   #endif
24  
25 < #ifndef BSD
26 < #define vfork           fork
27 < #endif
25 > static int      comm_close(), comm_clear(), comm_paintr(),
26 >                comm_getcur(), comm_comout(), comm_comin(), comm_flush();
27  
28 < #ifndef WFLUSH
30 < #define WFLUSH          30              /* flush after this many rays */
31 < #endif
32 <
33 < extern char     *getpath(), *getenv();
34 <
35 < int     onsigio();
36 <
37 < int     comm_close(), comm_clear(), comm_paintr(), comm_errout(),
38 <                comm_getcur(), comm_comout(), comm_comin();
39 <
40 < struct driver   comm_driver, comm_default = {
28 > struct driver   comm_driver = {
29          comm_close, comm_clear, comm_paintr, comm_getcur,
30 <        comm_comout, comm_comin,
43 <        MAXRES, MAXRES, 0
30 >        comm_comout, comm_comin, comm_flush
31   };
32  
33   FILE    *devin, *devout;
# Line 48 | Line 35 | FILE   *devin, *devout;
35   int     devchild;
36  
37  
38 + static struct driver *
39 + final_connect()                         /* verify and initialize connection */
40 + {
41 +        putw(COM_SENDM, devout);
42 +        fflush(devout);
43 +        if (getw(devin) != COM_RECVM)
44 +                return(NULL);
45 +                                                /* get driver parameters */
46 +        getstate();
47 +                                                /* set error vectors */
48 +        cmdvec = comm_comout;
49 +        if (wrnvec != NULL)
50 +                wrnvec = comm_comout;
51 +        return(&comm_driver);
52 + }
53 +
54 +
55   struct driver *
56 < comm_init(argv)                 /* set up and execute driver */
57 < char    *argv[];
56 > slave_init(dname, id)                   /* run rview in slave mode */
57 > char    *dname, *id;
58   {
59 +        devchild = -1;                          /* we're the slave here */
60 +        devout = stdout;                        /* use standard input */
61 +        devin = stdin;                          /* and standard output */
62 +        return(final_connect());                /* verify initialization */
63 + }
64 +
65 +
66 + struct driver *
67 + comm_init(dname, id)                    /* set up and execute driver */
68 + char    *dname, *id;
69 + {
70          char    *devname;
71          int     p1[2], p2[2];
72 <
73 <        if ((devname = getpath(argv[0], DEVPATH)) == NULL) {
74 <                stderr_v(argv[0]);
72 >        char    pin[16], pout[16];
73 >                                                /* find driver program */
74 >        if ((devname = getpath(dname, DEVPATH, X_OK)) == NULL) {
75 >                stderr_v(dname);
76                  stderr_v(": not found\n");
77                  return(NULL);
78          }
79 +                                                /* open communication pipes */
80          if (pipe(p1) == -1 || pipe(p2) == -1)
81                  goto syserr;
82 <        if ((devchild = vfork()) == 0) {
82 >        if ((devchild = vfork()) == 0) {        /* fork driver process */
83                  close(p1[1]);
84                  close(p2[0]);
85 <                if (p1[0] != 0) {
86 <                        dup2(p1[0], 0);
87 <                        close(p1[0]);
88 <                }
72 <                if (p2[1] != 1) {
73 <                        dup2(p2[1], 1);
74 <                        close(p2[1]);
75 <                }
76 <                execv(devname, argv);
77 <                stderr_v(devname);
78 <                stderr_v(": cannot execute\n");
85 >                sprintf(pin, "%d", p1[0]);
86 >                sprintf(pout, "%d", p2[1]);
87 >                execl(devname, dname, pin, pout, id, 0);
88 >                perror(devname);
89                  _exit(127);
90          }
91          if (devchild == -1)
# Line 86 | Line 96 | char   *argv[];
96                  goto syserr;
97          if ((devin = fdopen(p2[0], "r")) == NULL)
98                  goto syserr;
99 <        bcopy(&comm_default, &comm_driver, sizeof(comm_driver));
90 <        signal(SIGIO, onsigio);
91 <        cmdvec = comm_comout;                   /* set error vectors */
92 <        if (wrnvec != NULL)
93 <                wrnvec = comm_comout;
94 <        return(&comm_driver);
99 >        return(final_connect());                /* verify initialization */
100   syserr:
101 <        perror(argv[0]);
101 >        perror(dname);
102          return(NULL);
103   }
104  
# Line 106 | Line 111 | comm_close()                   /* done with driver */
111          cmdvec = NULL;                          /* reset error vectors */
112          if (wrnvec != NULL)
113                  wrnvec = stderr_v;
109        signal(SIGIO, SIG_DFL);
114          fclose(devout);
115          fclose(devin);
116 +        if (devchild < 0)
117 +                return;
118          while ((pid = wait(0)) != -1 && pid != devchild)
119                  ;
120   }
# Line 119 | Line 125 | comm_clear(xres, yres)                         /* clear screen */
125   int     xres, yres;
126   {
127          putc(COM_CLEAR, devout);
128 <        fwrite(&xres, sizeof(xres), 1, devout);
129 <        fwrite(&yres, sizeof(yres), 1, devout);
128 >        putw(xres, devout);
129 >        putw(yres, devout);
130          fflush(devout);
131   }
132  
# Line 130 | Line 136 | comm_paintr(col, xmin, ymin, xmax, ymax)       /* paint a re
136   COLOR   col;
137   int     xmin, ymin, xmax, ymax;
138   {
133        extern long     nrays;          /* number of rays traced */
134        static long     lastflush = 0;  /* ray count at last flush */
135
139          putc(COM_PAINTR, devout);
140 <        fwrite(col, sizeof(COLOR), 1, devout);
141 <        fwrite(&xmin, sizeof(xmin), 1, devout);
142 <        fwrite(&ymin, sizeof(ymin), 1, devout);
143 <        fwrite(&xmax, sizeof(xmax), 1, devout);
144 <        fwrite(&ymax, sizeof(ymax), 1, devout);
142 <        if (nrays - lastflush >= WFLUSH) {
143 <                fflush(devout);
144 <                lastflush = nrays;
145 <        }
140 >        fwrite((char *)col, sizeof(COLOR), 1, devout);
141 >        putw(xmin, devout);
142 >        putw(ymin, devout);
143 >        putw(xmax, devout);
144 >        putw(ymax, devout);
145   }
146  
147  
148 + static
149 + comm_flush()                            /* flush output to driver */
150 + {
151 +        putc(COM_FLUSH, devout);
152 +        fflush(devout);
153 +        if (getc(devin) != COM_FLUSH)
154 +                reply_error("flush");
155 +        getstate();
156 + }
157 +
158 +
159   static int
160   comm_getcur(xp, yp)                     /* get and return cursor position */
161   int     *xp, *yp;
# Line 157 | Line 167 | int    *xp, *yp;
167          if (getc(devin) != COM_GETCUR)
168                  reply_error("getcur");
169          c = getc(devin);
170 <        fread(xp, sizeof(*xp), 1, devin);
171 <        fread(yp, sizeof(*yp), 1, devin);
170 >        *xp = getw(devin);
171 >        *yp = getw(devin);
172          return(c);
173   }
174  
# Line 169 | Line 179 | char   *str;
179   {
180          putc(COM_COMOUT, devout);
181          myputs(str, devout);
182 <        fflush(devout);
182 >        if (str[strlen(str)-1] == '\n')
183 >                fflush(devout);
184   }
185  
186  
187   static
188 < comm_comin(buf)                         /* read string from command line */
188 > comm_comin(buf, prompt)                 /* read string from command line */
189   char    *buf;
190 + char    *prompt;
191   {
192          putc(COM_COMIN, devout);
193 +        if (prompt == NULL)
194 +                putc(0, devout);
195 +        else {
196 +                putc(1, devout);
197 +                myputs(prompt, devout);
198 +        }
199          fflush(devout);
200          if (getc(devin) != COM_COMIN)
201                  reply_error("comin");
202          mygets(buf, devin);
203 <        comm_driver.inpready = 0;
203 >        getstate();
204   }
205  
206  
207   static
190 comm_errout(str)                        /* display an error message */
191 char    *str;
192 {
193        comm_comout(str);
194        stderr_v(str);                  /* send to standard error also */
195 }
196
197
198 static
208   mygets(s, fp)                           /* get string from file (with nul) */
209   register char   *s;
210   register FILE   *fp;
# Line 231 | Line 240 | char   *routine;
240  
241  
242   static
243 < onsigio()                               /* input ready */
243 > getstate()                              /* get driver state variables */
244   {
245 <        comm_driver.inpready++;
245 >        fread((char *)&comm_driver.pixaspect,
246 >                        sizeof(comm_driver.pixaspect), 1, devin);
247 >        comm_driver.xsiz = getw(devin);
248 >        comm_driver.ysiz = getw(devin);
249 >        comm_driver.inpready = getw(devin);
250   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines