ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
Revision: 1.7
Committed: Wed Nov 1 17:33:03 1989 UTC (34 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +25 -20 lines
Log Message:
modified interface to independent drivers

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1988 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * devcomm.c - communication routines for separate drivers.
9     *
10     * 10/5/88
11     */
12    
13     #include <stdio.h>
14    
15     #include <signal.h>
16    
17     #include "color.h"
18    
19     #include "driver.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
28    
29     #ifndef WFLUSH
30 greg 1.5 #define WFLUSH 30 /* flush after this many rays */
31 greg 1.1 #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 = {
41     comm_close, comm_clear, comm_paintr, comm_getcur,
42     comm_comout, comm_comin,
43     MAXRES, MAXRES, 0
44     };
45    
46     FILE *devin, *devout;
47    
48     int devchild;
49    
50    
51     struct driver *
52 greg 1.7 comm_init(dname, id) /* set up and execute driver */
53     char *dname, *id;
54 greg 1.1 {
55     char *devname;
56     int p1[2], p2[2];
57 greg 1.7 char pin[16], pout[16];
58     /* find driver program */
59     if ((devname = getpath(dname, DEVPATH, 1)) == NULL) {
60     stderr_v(dname);
61 greg 1.1 stderr_v(": not found\n");
62     return(NULL);
63     }
64 greg 1.7 /* open communication pipes */
65 greg 1.1 if (pipe(p1) == -1 || pipe(p2) == -1)
66     goto syserr;
67 greg 1.7 if ((devchild = vfork()) == 0) { /* fork driver process */
68 greg 1.1 close(p1[1]);
69     close(p2[0]);
70 greg 1.7 sprintf(pin, "%d", p1[0]);
71     sprintf(pout, "%d", p2[1]);
72     execl(devname, dname, pin, pout, id, 0);
73     perror(devname);
74 greg 1.1 _exit(127);
75     }
76     if (devchild == -1)
77     goto syserr;
78     close(p1[0]);
79     close(p2[1]);
80     if ((devout = fdopen(p1[1], "w")) == NULL)
81     goto syserr;
82     if ((devin = fdopen(p2[0], "r")) == NULL)
83     goto syserr;
84     bcopy(&comm_default, &comm_driver, sizeof(comm_driver));
85 greg 1.7 /* verify & get resolution */
86     putw(COM_SENDM, devout);
87     fflush(devout);
88     if (getw(devin) != COM_RECVM)
89     return(NULL);
90     comm_driver.xsiz = getw(devin);
91     comm_driver.ysiz = getw(devin);
92     /* input handling */
93 greg 1.1 signal(SIGIO, onsigio);
94 greg 1.7 /* set error vectors */
95     cmdvec = comm_comout;
96 greg 1.1 if (wrnvec != NULL)
97     wrnvec = comm_comout;
98     return(&comm_driver);
99     syserr:
100 greg 1.7 perror(dname);
101 greg 1.1 return(NULL);
102     }
103    
104    
105     static
106     comm_close() /* done with driver */
107     {
108     int pid;
109    
110 greg 1.2 cmdvec = NULL; /* reset error vectors */
111 greg 1.1 if (wrnvec != NULL)
112     wrnvec = stderr_v;
113     signal(SIGIO, SIG_DFL);
114     fclose(devout);
115     fclose(devin);
116     while ((pid = wait(0)) != -1 && pid != devchild)
117     ;
118     }
119    
120    
121     static
122     comm_clear(xres, yres) /* clear screen */
123     int xres, yres;
124     {
125     putc(COM_CLEAR, devout);
126     fwrite(&xres, sizeof(xres), 1, devout);
127     fwrite(&yres, sizeof(yres), 1, devout);
128     fflush(devout);
129     }
130    
131    
132     static
133     comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */
134     COLOR col;
135     int xmin, ymin, xmax, ymax;
136     {
137     extern long nrays; /* number of rays traced */
138     static long lastflush = 0; /* ray count at last flush */
139    
140     putc(COM_PAINTR, devout);
141     fwrite(col, sizeof(COLOR), 1, devout);
142     fwrite(&xmin, sizeof(xmin), 1, devout);
143     fwrite(&ymin, sizeof(ymin), 1, devout);
144     fwrite(&xmax, sizeof(xmax), 1, devout);
145     fwrite(&ymax, sizeof(ymax), 1, devout);
146     if (nrays - lastflush >= WFLUSH) {
147     fflush(devout);
148     lastflush = nrays;
149     }
150     }
151    
152    
153     static int
154     comm_getcur(xp, yp) /* get and return cursor position */
155     int *xp, *yp;
156     {
157     int c;
158    
159     putc(COM_GETCUR, devout);
160     fflush(devout);
161     if (getc(devin) != COM_GETCUR)
162     reply_error("getcur");
163     c = getc(devin);
164     fread(xp, sizeof(*xp), 1, devin);
165     fread(yp, sizeof(*yp), 1, devin);
166     return(c);
167     }
168    
169    
170     static
171     comm_comout(str) /* print string to command line */
172     char *str;
173     {
174     putc(COM_COMOUT, devout);
175     myputs(str, devout);
176 greg 1.3 fflush(devout);
177 greg 1.1 }
178    
179    
180     static
181     comm_comin(buf) /* read string from command line */
182     char *buf;
183     {
184     putc(COM_COMIN, devout);
185     fflush(devout);
186     if (getc(devin) != COM_COMIN)
187     reply_error("comin");
188     mygets(buf, devin);
189 greg 1.7 if (comm_driver.inpready > 0)
190     comm_driver.inpready--;
191 greg 1.1 }
192    
193    
194     static
195     comm_errout(str) /* display an error message */
196     char *str;
197     {
198     comm_comout(str);
199     stderr_v(str); /* send to standard error also */
200     }
201    
202    
203     static
204     mygets(s, fp) /* get string from file (with nul) */
205     register char *s;
206     register FILE *fp;
207     {
208     register int c;
209    
210     while ((c = getc(fp)) != EOF)
211     if ((*s++ = c) == '\0')
212     return;
213     *s = '\0';
214     }
215    
216    
217     static
218     myputs(s, fp) /* put string to file (with nul) */
219     register char *s;
220     register FILE *fp;
221     {
222     do
223     putc(*s, fp);
224     while (*s++);
225     }
226    
227    
228     static
229     reply_error(routine) /* what should we do here? */
230     char *routine;
231     {
232     stderr_v(routine);
233     stderr_v(": driver reply error\n");
234     quit(1);
235     }
236    
237    
238     static
239     onsigio() /* input ready */
240     {
241     comm_driver.inpready++;
242     }