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 "standard.h" |
16 |
|
17 |
#include "color.h" |
18 |
|
19 |
#include "driver.h" |
20 |
|
21 |
struct driver *dev = NULL; /* output device */ |
22 |
|
23 |
FILE *devin, *devout; /* communications channels */ |
24 |
|
25 |
char *progname; /* driver name */ |
26 |
|
27 |
int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(), r_flush(); |
28 |
|
29 |
int (*dev_func[NREQUESTS])() = { /* request handlers */ |
30 |
r_clear, r_paintr, |
31 |
r_getcur, r_comout, |
32 |
r_comin, r_flush |
33 |
}; |
34 |
|
35 |
|
36 |
main(argc, argv) /* set up communications and main loop */ |
37 |
int argc; |
38 |
char *argv[]; |
39 |
{ |
40 |
extern struct driver *dinit(); |
41 |
int com; |
42 |
/* set up I/O */ |
43 |
progname = argv[0]; |
44 |
if (argc < 3) { |
45 |
eputs("arg count\n"); |
46 |
quit(1); |
47 |
} |
48 |
devin = fdopen(atoi(argv[1]), "r"); |
49 |
devout = fdopen(atoi(argv[2]), "w"); |
50 |
if (devin == NULL || devout == NULL || getw(devin) != COM_SENDM) { |
51 |
eputs("connection failure\n"); |
52 |
quit(1); |
53 |
} |
54 |
/* open device */ |
55 |
if ((dev = dinit(argv[0], argv[3])) == NULL) |
56 |
quit(1); |
57 |
putw(COM_RECVM, devout); /* verify initialization */ |
58 |
sendstate(); |
59 |
fflush(devout); |
60 |
/* loop on requests */ |
61 |
while ((com = getc(devin)) != EOF) { |
62 |
if (com >= NREQUESTS || dev_func[com] == NULL) { |
63 |
eputs("invalid request\n"); |
64 |
quit(1); |
65 |
} |
66 |
(*dev_func[com])(); /* process request */ |
67 |
} |
68 |
quit(0); /* all done, clean up and exit */ |
69 |
} |
70 |
|
71 |
|
72 |
quit(code) /* close device and exit */ |
73 |
int code; |
74 |
{ |
75 |
if (dev != NULL) |
76 |
(*dev->close)(); |
77 |
exit(code); |
78 |
} |
79 |
|
80 |
|
81 |
r_clear() /* clear screen */ |
82 |
{ |
83 |
int xres, yres; |
84 |
|
85 |
xres = getw(devin); |
86 |
yres = getw(devin); |
87 |
(*dev->clear)(xres, yres); |
88 |
} |
89 |
|
90 |
|
91 |
r_paintr() /* paint a rectangle */ |
92 |
{ |
93 |
COLOR col; |
94 |
int xmin, ymin, xmax, ymax; |
95 |
|
96 |
fread((char *)col, sizeof(COLOR), 1, devin); |
97 |
xmin = getw(devin); ymin = getw(devin); |
98 |
xmax = getw(devin); ymax = getw(devin); |
99 |
(*dev->paintr)(col, xmin, ymin, xmax, ymax); |
100 |
} |
101 |
|
102 |
|
103 |
r_flush() /* flush output */ |
104 |
{ |
105 |
if (dev->flush != NULL) |
106 |
(*dev->flush)(); |
107 |
putc(COM_FLUSH, devout); |
108 |
sendstate(); |
109 |
fflush(devout); |
110 |
} |
111 |
|
112 |
|
113 |
r_getcur() /* get and return cursor position */ |
114 |
{ |
115 |
int c; |
116 |
int x, y; |
117 |
/* get it if we can */ |
118 |
if (dev->getcur == NULL) { |
119 |
c = ABORT; |
120 |
x = y = 0; |
121 |
} else |
122 |
c = (*dev->getcur)(&x, &y); |
123 |
/* reply */ |
124 |
putc(COM_GETCUR, devout); |
125 |
putc(c, devout); |
126 |
putw(x, devout); |
127 |
putw(y, devout); |
128 |
fflush(devout); |
129 |
} |
130 |
|
131 |
|
132 |
r_comout() /* print string to command line */ |
133 |
{ |
134 |
char str[256]; |
135 |
|
136 |
mygets(str, devin); |
137 |
(*dev->comout)(str); |
138 |
} |
139 |
|
140 |
|
141 |
r_comin() /* read string from command line */ |
142 |
{ |
143 |
char buf[256], *prompt; |
144 |
/* get prompt */ |
145 |
if (getc(devin)) { |
146 |
mygets(buf, devin); |
147 |
prompt = buf; |
148 |
} else |
149 |
prompt = NULL; |
150 |
/* get string */ |
151 |
(*dev->comin)(buf, prompt); |
152 |
/* reply */ |
153 |
putc(COM_COMIN, devout); |
154 |
myputs(buf, devout); |
155 |
sendstate(); |
156 |
fflush(devout); |
157 |
} |
158 |
|
159 |
|
160 |
mygets(s, fp) /* get string from file (with nul) */ |
161 |
register char *s; |
162 |
register FILE *fp; |
163 |
{ |
164 |
register int c; |
165 |
|
166 |
while ((c = getc(fp)) != EOF) |
167 |
if ((*s++ = c) == '\0') |
168 |
return; |
169 |
*s = '\0'; |
170 |
} |
171 |
|
172 |
|
173 |
myputs(s, fp) /* put string to file (with nul) */ |
174 |
register char *s; |
175 |
register FILE *fp; |
176 |
{ |
177 |
do |
178 |
putc(*s, fp); |
179 |
while (*s++); |
180 |
} |
181 |
|
182 |
|
183 |
eputs(s) /* put string to stderr */ |
184 |
register char *s; |
185 |
{ |
186 |
static int midline = 0; |
187 |
|
188 |
if (!*s) |
189 |
return; |
190 |
if (!midline++) { |
191 |
fputs(progname, stderr); |
192 |
fputs(": ", stderr); |
193 |
} |
194 |
fputs(s, stderr); |
195 |
if (s[strlen(s)-1] == '\n') { |
196 |
fflush(stderr); |
197 |
midline = 0; |
198 |
} |
199 |
} |
200 |
|
201 |
|
202 |
sendstate() /* send driver state variables */ |
203 |
{ |
204 |
fwrite((char *)&dev->pixaspect, sizeof(dev->pixaspect), 1, devout); |
205 |
putw(dev->xsiz, devout); |
206 |
putw(dev->ysiz, devout); |
207 |
putw(dev->inpready, devout); |
208 |
} |