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