ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
Revision: 1.10
Committed: Fri Jan 19 00:00:08 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +5 -4 lines
Log Message:
improved portability and speed of bcopy()

File Contents

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