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(), r_mycomin(); |
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 |
fwrite(dev->pixaspect, sizeof(dev->pixaspect), 1, 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 |
nrays += 5; /* pretend */ |
106 |
fread(col, sizeof(COLOR), 1, devin); |
107 |
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 |
if (!notified && dev->inpready > 0) { |
112 |
notified = 1; |
113 |
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 |
char buf[256]; |
149 |
/* get string */ |
150 |
(*dev->comin)(buf); |
151 |
/* reply */ |
152 |
putc(COM_COMIN, devout); |
153 |
myputs(buf, devout); |
154 |
fflush(devout); |
155 |
/* reset notify */ |
156 |
notified = 0; |
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 |
stderr_v(s) /* put string to stderr */ |
184 |
register char *s; |
185 |
{ |
186 |
static int inline = 0; |
187 |
|
188 |
if (!inline++) { |
189 |
fputs(progname, stderr); |
190 |
fputs(": ", stderr); |
191 |
} |
192 |
fputs(s, stderr); |
193 |
if (*s && s[strlen(s)-1] == '\n') { |
194 |
fflush(stderr); |
195 |
inline = 0; |
196 |
} |
197 |
} |