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