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 |
}; |
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 |
bcopy(&comm_default, &comm_driver, sizeof(comm_driver)); |
84 |
/* verify & get resolution */ |
85 |
putw(COM_SENDM, devout); |
86 |
fflush(devout); |
87 |
if (getw(devin) != COM_RECVM) |
88 |
return(NULL); |
89 |
comm_driver.xsiz = getw(devin); |
90 |
comm_driver.ysiz = getw(devin); |
91 |
fread(comm_driver.pixaspect, sizeof(comm_driver.pixaspect), 1, devin); |
92 |
/* input handling */ |
93 |
signal(SIGIO, onsigio); |
94 |
/* set error vectors */ |
95 |
cmdvec = comm_comout; |
96 |
if (wrnvec != NULL) |
97 |
wrnvec = comm_comout; |
98 |
return(&comm_driver); |
99 |
syserr: |
100 |
perror(dname); |
101 |
return(NULL); |
102 |
} |
103 |
|
104 |
|
105 |
static |
106 |
comm_close() /* done with driver */ |
107 |
{ |
108 |
int pid; |
109 |
|
110 |
cmdvec = NULL; /* reset error vectors */ |
111 |
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 |
putw(xres, devout); |
127 |
putw(yres, 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 |
putw(xmin, devout); |
143 |
putw(ymin, devout); |
144 |
putw(xmax, devout); |
145 |
putw(ymax, 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 |
*xp = getw(devin); |
165 |
*yp = getw(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 |
fflush(devout); |
177 |
} |
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 |
if (comm_driver.inpready > 0) |
190 |
comm_driver.inpready--; |
191 |
} |
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 |
} |