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 |
long nrays = 0; /* fake it */ |
26 |
|
27 |
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 |
int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(); |
36 |
|
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 |
if ((dev = dinit(argv[0], argv[3])) == NULL) |
63 |
quit(1); |
64 |
putw(COM_RECVM, devout); /* verify initialization */ |
65 |
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 |
nrays += 5; /* pretend */ |
105 |
fread(col, sizeof(COLOR), 1, devin); |
106 |
xmin = getw(devin); ymin = getw(devin); |
107 |
xmax = getw(devin); ymax = getw(devin); |
108 |
(*dev->paintr)(col, xmin, ymin, xmax, ymax); |
109 |
/* check for input */ |
110 |
if (dev->inpready > notified) { |
111 |
kill(getppid(), SIGIO); |
112 |
notified = dev->inpready; |
113 |
} |
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]; |
148 |
/* get string */ |
149 |
(*dev->comin)(buf); |
150 |
/* reply */ |
151 |
putc(COM_COMIN, devout); |
152 |
myputs(buf, devout); |
153 |
fflush(devout); |
154 |
/* reset notify */ |
155 |
notified = 0; |
156 |
} |
157 |
|
158 |
|
159 |
mygets(s, fp) /* get string from file (with nul) */ |
160 |
register char *s; |
161 |
register FILE *fp; |
162 |
{ |
163 |
register int c; |
164 |
|
165 |
while ((c = getc(fp)) != EOF) |
166 |
if ((*s++ = c) == '\0') |
167 |
return; |
168 |
*s = '\0'; |
169 |
} |
170 |
|
171 |
|
172 |
myputs(s, fp) /* put string to file (with nul) */ |
173 |
register char *s; |
174 |
register FILE *fp; |
175 |
{ |
176 |
do |
177 |
putc(*s, fp); |
178 |
while (*s++); |
179 |
} |
180 |
|
181 |
|
182 |
repaint(xmin, ymin, xmax, ymax) /* repaint section of display */ |
183 |
int xmin, ymin, xmax, ymax; |
184 |
{ |
185 |
/* no can do! */ |
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 |
} |