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