1 |
/* Copyright (c) 1989 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* devmain.c - main for independent drivers. |
9 |
* |
10 |
* Redefine your initialization routine to dinit. |
11 |
* |
12 |
* 10/25/89 |
13 |
*/ |
14 |
|
15 |
#include <stdio.h> |
16 |
|
17 |
#include <signal.h> |
18 |
|
19 |
#include "color.h" |
20 |
|
21 |
#include "driver.h" |
22 |
|
23 |
int (*wrnvec)(), (*errvec)(), (*cmdvec)(); /* error vectors, unused */ |
24 |
|
25 |
long nrays = 0; /* fake it */ |
26 |
|
27 |
struct driver *dev = NULL; /* output device */ |
28 |
|
29 |
FILE *devin, *devout; /* communications channels */ |
30 |
|
31 |
int notified = 0; /* notified parent of input? */ |
32 |
|
33 |
char *progname; /* driver name */ |
34 |
|
35 |
int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(), r_mycomin(); |
36 |
|
37 |
int (*dev_func[NREQUESTS])() = { /* request handlers */ |
38 |
r_clear, r_paintr, |
39 |
r_getcur, r_comout, r_comin |
40 |
}; |
41 |
|
42 |
char mybuf[512] = ""; |
43 |
|
44 |
char *mybufp(); |
45 |
|
46 |
|
47 |
main(argc, argv) /* set up communications and main loop */ |
48 |
int argc; |
49 |
char *argv[]; |
50 |
{ |
51 |
extern struct driver *dinit(); |
52 |
int com; |
53 |
/* set up I/O */ |
54 |
progname = argv[0]; |
55 |
if (argc < 3) { |
56 |
stderr_v("arg count\n"); |
57 |
quit(1); |
58 |
} |
59 |
devin = fdopen(atoi(argv[1]), "r"); |
60 |
devout = fdopen(atoi(argv[2]), "w"); |
61 |
if (devin == NULL || devout == NULL || getw(devin) != COM_SENDM) { |
62 |
stderr_v("connection failure\n"); |
63 |
quit(1); |
64 |
} |
65 |
/* open device */ |
66 |
if ((dev = dinit(argv[0], argv[3])) == NULL) |
67 |
quit(1); |
68 |
putw(COM_RECVM, devout); /* verify initialization */ |
69 |
putw(dev->xsiz, devout); |
70 |
putw(dev->ysiz, devout); |
71 |
fflush(devout); |
72 |
/* loop on requests */ |
73 |
while ((com = getc(devin)) != EOF) { |
74 |
if (com >= NREQUESTS || dev_func[com] == NULL) { |
75 |
stderr_v("invalid request\n"); |
76 |
quit(1); |
77 |
} |
78 |
(*dev_func[com])(); /* process request */ |
79 |
} |
80 |
quit(0); /* all done, clean up and exit */ |
81 |
} |
82 |
|
83 |
|
84 |
quit(code) /* close device and exit */ |
85 |
int code; |
86 |
{ |
87 |
if (dev != NULL) |
88 |
(*dev->close)(); |
89 |
exit(code); |
90 |
} |
91 |
|
92 |
|
93 |
r_clear() /* clear screen */ |
94 |
{ |
95 |
int xres, yres; |
96 |
|
97 |
xres = getw(devin); |
98 |
yres = getw(devin); |
99 |
(*dev->clear)(xres, yres); |
100 |
} |
101 |
|
102 |
|
103 |
r_paintr() /* paint a rectangle */ |
104 |
{ |
105 |
COLOR col; |
106 |
int xmin, ymin, xmax, ymax; |
107 |
|
108 |
nrays += 5; /* pretend */ |
109 |
fread(col, sizeof(COLOR), 1, devin); |
110 |
xmin = getw(devin); ymin = getw(devin); |
111 |
xmax = getw(devin); ymax = getw(devin); |
112 |
(*dev->paintr)(col, xmin, ymin, xmax, ymax); |
113 |
/* check for input */ |
114 |
if (!notified && dev->inpready > 0) { |
115 |
notified = 1; |
116 |
kill(getppid(), SIGIO); |
117 |
} |
118 |
} |
119 |
|
120 |
|
121 |
r_getcur() /* get and return cursor position */ |
122 |
{ |
123 |
int c; |
124 |
int x, y; |
125 |
/* get it if we can */ |
126 |
if (dev->getcur == NULL) { |
127 |
c = ABORT; |
128 |
x = y = 0; |
129 |
} else |
130 |
c = (*dev->getcur)(&x, &y); |
131 |
/* reply */ |
132 |
putc(COM_GETCUR, devout); |
133 |
putc(c, devout); |
134 |
putw(x, devout); |
135 |
putw(y, devout); |
136 |
fflush(devout); |
137 |
} |
138 |
|
139 |
|
140 |
r_comout() /* print string to command line */ |
141 |
{ |
142 |
char str[256]; |
143 |
|
144 |
mygets(str, devin); |
145 |
(*dev->comout)(str); |
146 |
} |
147 |
|
148 |
|
149 |
r_comin() /* read string from command line */ |
150 |
{ |
151 |
char buf[256]; |
152 |
/* get string */ |
153 |
(*dev->comin)(buf); |
154 |
/* reply */ |
155 |
putc(COM_COMIN, devout); |
156 |
myputs(buf, devout); |
157 |
fflush(devout); |
158 |
/* reset notify */ |
159 |
notified = 0; |
160 |
} |
161 |
|
162 |
|
163 |
mygets(s, fp) /* get string from file (with nul) */ |
164 |
register char *s; |
165 |
register FILE *fp; |
166 |
{ |
167 |
register int c; |
168 |
|
169 |
while ((c = getc(fp)) != EOF) |
170 |
if ((*s++ = c) == '\0') |
171 |
return; |
172 |
*s = '\0'; |
173 |
} |
174 |
|
175 |
|
176 |
myputs(s, fp) /* put string to file (with nul) */ |
177 |
register char *s; |
178 |
register FILE *fp; |
179 |
{ |
180 |
do |
181 |
putc(*s, fp); |
182 |
while (*s++); |
183 |
} |
184 |
|
185 |
|
186 |
r_mycomin() /* get command from my buffer */ |
187 |
{ |
188 |
register char *cp; |
189 |
/* get next command */ |
190 |
for (cp = mybuf; *cp != '\n'; cp++) |
191 |
; |
192 |
*cp++ = '\0'; |
193 |
(*dev->comout)(mybuf); /* echo my command */ |
194 |
/* send it as reply */ |
195 |
putc(COM_COMIN, devout); |
196 |
myputs(mybuf, devout); |
197 |
fflush(devout); |
198 |
/* get next command */ |
199 |
(*dev->comout)("\n"); |
200 |
strcpy(mybuf, cp); |
201 |
if (mybuf[0]) |
202 |
kill(getppid(), SIGIO); /* signal more */ |
203 |
else |
204 |
dev_func[COM_COMIN] = r_comin; /* else reset */ |
205 |
} |
206 |
|
207 |
|
208 |
stderr_v(s) /* put string to stderr */ |
209 |
register char *s; |
210 |
{ |
211 |
static int inline = 0; |
212 |
|
213 |
if (!inline++) { |
214 |
fputs(progname, stderr); |
215 |
fputs(": ", stderr); |
216 |
} |
217 |
fputs(s, stderr); |
218 |
if (*s && s[strlen(s)-1] == '\n') { |
219 |
fflush(stderr); |
220 |
inline = 0; |
221 |
} |
222 |
} |
223 |
|
224 |
|
225 |
char * |
226 |
mybufp() /* return buffer for my command */ |
227 |
{ |
228 |
if (dev_func[COM_COMIN] != r_mycomin) { |
229 |
dev_func[COM_COMIN] = r_mycomin; |
230 |
kill(getppid(), SIGIO); |
231 |
} |
232 |
return(mybuf+strlen(mybuf)); |
233 |
} |
234 |
|
235 |
|
236 |
repaint(xmin, ymin, xmax, ymax) /* repaint section of display */ |
237 |
int xmin, ymin, xmax, ymax; |
238 |
{ |
239 |
sprintf(mybufp(), "repaint %d %d %d %d\n", |
240 |
xmin, ymin, xmax, ymax); |
241 |
} |