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 |
int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(); |
34 |
|
35 |
int (*dev_func[NREQUESTS])() = { /* request handlers */ |
36 |
r_clear, r_paintr, |
37 |
r_getcur, r_comout, r_comin |
38 |
}; |
39 |
|
40 |
|
41 |
main(argc, argv) /* set up communications and main loop */ |
42 |
int argc; |
43 |
char *argv[]; |
44 |
{ |
45 |
extern struct driver *dinit(); |
46 |
int com; |
47 |
/* set up I/O */ |
48 |
progname = argv[0]; |
49 |
if (argc < 3) { |
50 |
stderr_v("arg count\n"); |
51 |
quit(1); |
52 |
} |
53 |
devin = fdopen(atoi(argv[1]), "r"); |
54 |
devout = fdopen(atoi(argv[2]), "w"); |
55 |
if (devin == NULL || devout == NULL || getw(devin) != COM_SENDM) { |
56 |
stderr_v("connection failure\n"); |
57 |
quit(1); |
58 |
} |
59 |
/* open device */ |
60 |
if ((dev = dinit(argv[0], argv[3])) == NULL) { |
61 |
stderr_v("initialization failure\n"); |
62 |
quit(1); |
63 |
} |
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 |
fread(col, sizeof(COLOR), 1, devin); |
105 |
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 |
if (dev->inpready > notified) { |
110 |
kill(getppid(), SIGIO); |
111 |
notified = dev->inpready; |
112 |
} |
113 |
} |
114 |
|
115 |
|
116 |
r_getcur() /* get and return cursor position */ |
117 |
{ |
118 |
int c; |
119 |
int x, y; |
120 |
/* get it if we can */ |
121 |
if (dev->getcur == NULL) { |
122 |
c = ABORT; |
123 |
x = y = 0; |
124 |
} else |
125 |
c = (*dev->getcur)(&x, &y); |
126 |
/* reply */ |
127 |
putc(COM_GETCUR, devout); |
128 |
putc(c, devout); |
129 |
putw(x, devout); |
130 |
putw(y, devout); |
131 |
fflush(devout); |
132 |
} |
133 |
|
134 |
|
135 |
r_comout() /* print string to command line */ |
136 |
{ |
137 |
char str[256]; |
138 |
|
139 |
mygets(str, devin); |
140 |
(*dev->comout)(str); |
141 |
} |
142 |
|
143 |
|
144 |
r_comin() /* read string from command line */ |
145 |
{ |
146 |
char buf[256]; |
147 |
/* get string */ |
148 |
(*dev->comin)(buf); |
149 |
/* reply */ |
150 |
putc(COM_COMIN, devout); |
151 |
myputs(buf, devout); |
152 |
fflush(devout); |
153 |
/* reset notify */ |
154 |
notified = 0; |
155 |
} |
156 |
|
157 |
|
158 |
mygets(s, fp) /* get string from file (with nul) */ |
159 |
register char *s; |
160 |
register FILE *fp; |
161 |
{ |
162 |
register int c; |
163 |
|
164 |
while ((c = getc(fp)) != EOF) |
165 |
if ((*s++ = c) == '\0') |
166 |
return; |
167 |
*s = '\0'; |
168 |
} |
169 |
|
170 |
|
171 |
myputs(s, fp) /* put string to file (with nul) */ |
172 |
register char *s; |
173 |
register FILE *fp; |
174 |
{ |
175 |
do |
176 |
putc(*s, fp); |
177 |
while (*s++); |
178 |
} |
179 |
|
180 |
|
181 |
repaint(xmin, ymin, xmax, ymax) /* repaint section of display */ |
182 |
int xmin, ymin, xmax, ymax; |
183 |
{ |
184 |
stderr_v("repaint called!\n"); /* no can do! */ |
185 |
} |
186 |
|
187 |
|
188 |
stderr_v(s) /* put string to stderr */ |
189 |
register char *s; |
190 |
{ |
191 |
static int inline = 0; |
192 |
|
193 |
if (!inline++) { |
194 |
fputs(progname, stderr); |
195 |
fputs(": ", stderr); |
196 |
} |
197 |
fputs(s, stderr); |
198 |
if (*s && s[strlen(s)-1] == '\n') { |
199 |
fflush(stderr); |
200 |
inline = 0; |
201 |
} |
202 |
} |