ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
Revision: 2.7
Committed: Tue Feb 25 02:47:22 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.6: +1 -56 lines
Log Message:
Replaced inline copyright notice with #include "copyright.h"

File Contents

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