ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
Revision: 2.9
Committed: Thu Jul 3 15:00:19 2003 UTC (20 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +3 -1 lines
Log Message:
Added -N option to rad for parallel rendering (preliminary w/o using -PP)

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.9 static const char RCSid[] = "$Id: devcomm.c,v 2.8 2003/07/02 01:15:20 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * devcomm.c - communication routines for separate drivers.
6     *
7 greg 2.6 * External symbols declared in driver.h
8     */
9    
10 greg 2.7 #include "copyright.h"
11 greg 2.9
12     #include "platform.h"
13 greg 1.1
14 greg 1.10 #include "standard.h"
15 greg 1.1
16     #include "color.h"
17    
18     #include "driver.h"
19    
20 greg 2.2 #include "vfork.h"
21    
22 greg 1.1 #ifndef DEVPATH
23     #define DEVPATH getenv("PATH") /* device search path */
24     #endif
25    
26 greg 2.6 static int comm_getcur();
27     static void comm_close(), comm_clear(), comm_paintr(),
28     comm_comin(), comm_comout(), comm_flush();
29 greg 1.1
30 greg 1.12 struct driver comm_driver = {
31 greg 1.1 comm_close, comm_clear, comm_paintr, comm_getcur,
32 greg 1.12 comm_comout, comm_comin, comm_flush
33 greg 1.1 };
34    
35 greg 2.6 static void mygets(), myputs(), reply_error(), getstate();
36 greg 2.4
37 greg 1.1 FILE *devin, *devout;
38    
39     int devchild;
40    
41    
42 greg 2.3 static struct driver *
43     final_connect() /* verify and initialize connection */
44     {
45     putw(COM_SENDM, devout);
46     fflush(devout);
47     if (getw(devin) != COM_RECVM)
48     return(NULL);
49     /* get driver parameters */
50     getstate();
51     /* set error vectors */
52 gregl 2.5 erract[COMMAND].pf = comm_comout;
53     if (erract[WARNING].pf != NULL)
54     erract[WARNING].pf = comm_comout;
55 greg 2.3 return(&comm_driver);
56     }
57    
58    
59 greg 1.1 struct driver *
60 greg 2.3 slave_init(dname, id) /* run rview in slave mode */
61     char *dname, *id;
62     {
63     devchild = -1; /* we're the slave here */
64     devout = stdout; /* use standard input */
65     devin = stdin; /* and standard output */
66     return(final_connect()); /* verify initialization */
67     }
68    
69    
70     struct driver *
71 greg 1.7 comm_init(dname, id) /* set up and execute driver */
72     char *dname, *id;
73 greg 1.1 {
74 greg 2.6 char *dvcname;
75 greg 1.1 int p1[2], p2[2];
76 greg 1.7 char pin[16], pout[16];
77     /* find driver program */
78 greg 2.6 if ((dvcname = getpath(dname, DEVPATH, X_OK)) == NULL) {
79 gregl 2.5 eputs(dname);
80     eputs(": not found\n");
81 greg 1.1 return(NULL);
82     }
83 greg 2.8 #ifdef RHAS_FORK_EXEC
84 greg 1.7 /* open communication pipes */
85 greg 1.1 if (pipe(p1) == -1 || pipe(p2) == -1)
86     goto syserr;
87 greg 1.7 if ((devchild = vfork()) == 0) { /* fork driver process */
88 greg 1.1 close(p1[1]);
89     close(p2[0]);
90 greg 1.7 sprintf(pin, "%d", p1[0]);
91     sprintf(pout, "%d", p2[1]);
92 greg 2.6 execl(dvcname, dname, pin, pout, id, 0);
93     perror(dvcname);
94 greg 1.1 _exit(127);
95     }
96     if (devchild == -1)
97     goto syserr;
98     close(p1[0]);
99     close(p2[1]);
100     if ((devout = fdopen(p1[1], "w")) == NULL)
101     goto syserr;
102     if ((devin = fdopen(p2[0], "r")) == NULL)
103     goto syserr;
104 greg 2.3 return(final_connect()); /* verify initialization */
105 greg 1.1 syserr:
106 greg 1.7 perror(dname);
107 greg 1.1 return(NULL);
108 greg 2.8
109     #else /* ! RHAS_FORK_EXEC */
110    
111     eputs(dname);
112     eputs(": no fork/exec\n");
113     return(NULL);
114    
115     #endif /* ! RHAS_FORK_EXEC */
116 greg 1.1 }
117    
118    
119 greg 2.6 static void
120 greg 1.1 comm_close() /* done with driver */
121     {
122     int pid;
123    
124 gregl 2.5 erract[COMMAND].pf = NULL; /* reset error vectors */
125     if (erract[WARNING].pf != NULL)
126     erract[WARNING].pf = wputs;
127 greg 1.1 fclose(devout);
128     fclose(devin);
129 greg 2.3 if (devchild < 0)
130     return;
131 greg 1.1 while ((pid = wait(0)) != -1 && pid != devchild)
132     ;
133     }
134    
135    
136 greg 2.6 static void
137 greg 1.1 comm_clear(xres, yres) /* clear screen */
138     int xres, yres;
139     {
140     putc(COM_CLEAR, devout);
141 greg 1.8 putw(xres, devout);
142     putw(yres, devout);
143 greg 1.1 fflush(devout);
144     }
145    
146    
147 greg 2.6 static void
148 greg 1.1 comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */
149     COLOR col;
150     int xmin, ymin, xmax, ymax;
151     {
152     putc(COM_PAINTR, devout);
153 greg 1.10 fwrite((char *)col, sizeof(COLOR), 1, devout);
154 greg 1.8 putw(xmin, devout);
155     putw(ymin, devout);
156     putw(xmax, devout);
157     putw(ymax, devout);
158 greg 1.12 }
159    
160    
161 greg 2.6 static void
162 greg 1.12 comm_flush() /* flush output to driver */
163     {
164     putc(COM_FLUSH, devout);
165     fflush(devout);
166 greg 1.13 if (getc(devin) != COM_FLUSH)
167     reply_error("flush");
168 greg 1.17 getstate();
169 greg 1.1 }
170    
171    
172     static int
173     comm_getcur(xp, yp) /* get and return cursor position */
174     int *xp, *yp;
175     {
176     int c;
177    
178     putc(COM_GETCUR, devout);
179     fflush(devout);
180     if (getc(devin) != COM_GETCUR)
181     reply_error("getcur");
182     c = getc(devin);
183 greg 1.8 *xp = getw(devin);
184     *yp = getw(devin);
185 greg 1.1 return(c);
186     }
187    
188    
189 greg 2.6 static void
190 greg 1.1 comm_comout(str) /* print string to command line */
191     char *str;
192     {
193     putc(COM_COMOUT, devout);
194     myputs(str, devout);
195 greg 1.18 if (str[strlen(str)-1] == '\n')
196     fflush(devout);
197 greg 1.1 }
198    
199    
200 greg 2.6 static void
201 greg 1.11 comm_comin(buf, prompt) /* read string from command line */
202 greg 1.1 char *buf;
203 greg 1.11 char *prompt;
204 greg 1.1 {
205     putc(COM_COMIN, devout);
206 greg 1.11 if (prompt == NULL)
207     putc(0, devout);
208     else {
209     putc(1, devout);
210     myputs(prompt, devout);
211     }
212 greg 1.1 fflush(devout);
213     if (getc(devin) != COM_COMIN)
214     reply_error("comin");
215     mygets(buf, devin);
216 greg 1.17 getstate();
217 greg 1.1 }
218    
219    
220 greg 2.6 static void
221 greg 1.1 mygets(s, fp) /* get string from file (with nul) */
222     register char *s;
223     register FILE *fp;
224     {
225     register int c;
226    
227     while ((c = getc(fp)) != EOF)
228     if ((*s++ = c) == '\0')
229     return;
230     *s = '\0';
231     }
232    
233    
234 greg 2.6 static void
235 greg 1.1 myputs(s, fp) /* put string to file (with nul) */
236     register char *s;
237     register FILE *fp;
238     {
239     do
240     putc(*s, fp);
241     while (*s++);
242     }
243    
244    
245 greg 2.6 static void
246 greg 1.1 reply_error(routine) /* what should we do here? */
247     char *routine;
248     {
249 gregl 2.5 eputs(routine);
250     eputs(": driver reply error\n");
251 greg 1.1 quit(1);
252 greg 1.17 }
253    
254    
255 greg 2.6 static void
256 greg 1.17 getstate() /* get driver state variables */
257     {
258     fread((char *)&comm_driver.pixaspect,
259     sizeof(comm_driver.pixaspect), 1, devin);
260     comm_driver.xsiz = getw(devin);
261     comm_driver.ysiz = getw(devin);
262     comm_driver.inpready = getw(devin);
263 greg 1.1 }