1 |
– |
/* Copyright (c) 1989 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* devmain.c - main for independent drivers. |
6 |
|
* |
7 |
|
* Redefine your initialization routine to dinit. |
11 |
– |
* |
12 |
– |
* 10/25/89 |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include <signal.h> |
12 |
> |
#include "standard.h" |
13 |
|
|
14 |
|
#include "color.h" |
15 |
|
|
16 |
|
#include "driver.h" |
17 |
|
|
23 |
– |
int (*wrnvec)(), (*errvec)(), (*cmdvec)(); /* error vectors, unused */ |
24 |
– |
|
25 |
– |
long nrays = 0; /* fake it */ |
26 |
– |
|
18 |
|
struct driver *dev = NULL; /* output device */ |
19 |
|
|
20 |
|
FILE *devin, *devout; /* communications channels */ |
21 |
|
|
31 |
– |
int notified = 0; /* notified parent of input? */ |
32 |
– |
|
22 |
|
char *progname; /* driver name */ |
23 |
|
|
24 |
< |
int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(), r_mycomin(); |
24 |
> |
int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(), r_flush(); |
25 |
|
|
26 |
|
int (*dev_func[NREQUESTS])() = { /* request handlers */ |
27 |
|
r_clear, r_paintr, |
28 |
< |
r_getcur, r_comout, r_comin |
28 |
> |
r_getcur, r_comout, |
29 |
> |
r_comin, r_flush |
30 |
|
}; |
31 |
|
|
42 |
– |
char mybuf[512] = ""; |
32 |
|
|
44 |
– |
char *mybufp(); |
45 |
– |
|
46 |
– |
|
33 |
|
main(argc, argv) /* set up communications and main loop */ |
34 |
|
int argc; |
35 |
|
char *argv[]; |
39 |
|
/* set up I/O */ |
40 |
|
progname = argv[0]; |
41 |
|
if (argc < 3) { |
42 |
< |
stderr_v("arg count\n"); |
42 |
> |
eputs("arg count\n"); |
43 |
|
quit(1); |
44 |
|
} |
45 |
|
devin = fdopen(atoi(argv[1]), "r"); |
46 |
|
devout = fdopen(atoi(argv[2]), "w"); |
47 |
|
if (devin == NULL || devout == NULL || getw(devin) != COM_SENDM) { |
48 |
< |
stderr_v("connection failure\n"); |
48 |
> |
eputs("connection failure\n"); |
49 |
|
quit(1); |
50 |
|
} |
51 |
|
/* open device */ |
52 |
|
if ((dev = dinit(argv[0], argv[3])) == NULL) |
53 |
|
quit(1); |
54 |
|
putw(COM_RECVM, devout); /* verify initialization */ |
55 |
< |
putw(dev->xsiz, devout); |
70 |
< |
putw(dev->ysiz, devout); |
55 |
> |
sendstate(); |
56 |
|
fflush(devout); |
57 |
|
/* loop on requests */ |
58 |
|
while ((com = getc(devin)) != EOF) { |
59 |
|
if (com >= NREQUESTS || dev_func[com] == NULL) { |
60 |
< |
stderr_v("invalid request\n"); |
60 |
> |
eputs("invalid request\n"); |
61 |
|
quit(1); |
62 |
|
} |
63 |
|
(*dev_func[com])(); /* process request */ |
66 |
|
} |
67 |
|
|
68 |
|
|
69 |
+ |
void |
70 |
|
quit(code) /* close device and exit */ |
71 |
|
int code; |
72 |
|
{ |
91 |
|
COLOR col; |
92 |
|
int xmin, ymin, xmax, ymax; |
93 |
|
|
94 |
< |
nrays += 5; /* pretend */ |
109 |
< |
fread(col, sizeof(COLOR), 1, devin); |
94 |
> |
getbinary((char *)col, sizeof(COLOR), 1, devin); |
95 |
|
xmin = getw(devin); ymin = getw(devin); |
96 |
|
xmax = getw(devin); ymax = getw(devin); |
97 |
|
(*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 |
– |
} |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
+ |
r_flush() /* flush output */ |
102 |
+ |
{ |
103 |
+ |
if (dev->flush != NULL) |
104 |
+ |
(*dev->flush)(); |
105 |
+ |
putc(COM_FLUSH, devout); |
106 |
+ |
sendstate(); |
107 |
+ |
fflush(devout); |
108 |
+ |
} |
109 |
+ |
|
110 |
+ |
|
111 |
|
r_getcur() /* get and return cursor position */ |
112 |
|
{ |
113 |
|
int c; |
138 |
|
|
139 |
|
r_comin() /* read string from command line */ |
140 |
|
{ |
141 |
< |
char buf[256]; |
141 |
> |
char buf[256], *prompt; |
142 |
> |
/* get prompt */ |
143 |
> |
if (getc(devin)) { |
144 |
> |
mygets(buf, devin); |
145 |
> |
prompt = buf; |
146 |
> |
} else |
147 |
> |
prompt = NULL; |
148 |
|
/* get string */ |
149 |
< |
(*dev->comin)(buf); |
149 |
> |
(*dev->comin)(buf, prompt); |
150 |
|
/* reply */ |
151 |
|
putc(COM_COMIN, devout); |
152 |
|
myputs(buf, devout); |
153 |
+ |
sendstate(); |
154 |
|
fflush(devout); |
158 |
– |
/* reset notify */ |
159 |
– |
notified = 0; |
155 |
|
} |
156 |
|
|
157 |
|
|
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
< |
r_mycomin() /* get command from my buffer */ |
182 |
< |
{ |
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 */ |
181 |
> |
void |
182 |
> |
eputs(s) /* put string to stderr */ |
183 |
|
register char *s; |
184 |
|
{ |
185 |
< |
static int inline = 0; |
185 |
> |
static int midline = 0; |
186 |
|
|
187 |
< |
if (!inline++) { |
187 |
> |
if (!*s) |
188 |
> |
return; |
189 |
> |
if (!midline++) { |
190 |
|
fputs(progname, stderr); |
191 |
|
fputs(": ", stderr); |
192 |
|
} |
193 |
|
fputs(s, stderr); |
194 |
< |
if (*s && s[strlen(s)-1] == '\n') { |
194 |
> |
if (s[strlen(s)-1] == '\n') { |
195 |
|
fflush(stderr); |
196 |
< |
inline = 0; |
196 |
> |
midline = 0; |
197 |
|
} |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
< |
char * |
226 |
< |
mybufp() /* return buffer for my command */ |
201 |
> |
sendstate() /* send driver state variables */ |
202 |
|
{ |
203 |
< |
if (dev_func[COM_COMIN] != r_mycomin) { |
204 |
< |
dev_func[COM_COMIN] = r_mycomin; |
205 |
< |
kill(getppid(), SIGIO); |
206 |
< |
} |
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); |
203 |
> |
putbinary(&dev->pixaspect, sizeof(dev->pixaspect), 1, devout); |
204 |
> |
putw(dev->xsiz, devout); |
205 |
> |
putw(dev->ysiz, devout); |
206 |
> |
putw(dev->inpready, devout); |
207 |
|
} |