ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
Revision: 1.1
Committed: Thu Feb 2 10:41:19 1989 UTC (35 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

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