1 |
– |
/* Copyright (c) 1989 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* devmain.c - main for independent drivers. |
6 |
|
* |
7 |
|
* Redefine your initialization routine to dinit. |
11 |
– |
* |
12 |
– |
* 10/25/89 |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include <signal.h> |
12 |
> |
#include "standard.h" |
13 |
|
|
14 |
|
#include "color.h" |
15 |
|
|
16 |
|
#include "driver.h" |
17 |
|
|
23 |
– |
int (*wrnvec)(), (*errvec)(), (*cmdvec)(); /* error vectors, unused */ |
24 |
– |
|
18 |
|
struct driver *dev = NULL; /* output device */ |
19 |
|
|
20 |
|
FILE *devin, *devout; /* communications channels */ |
21 |
|
|
29 |
– |
int notified = 0; /* notified parent of input? */ |
30 |
– |
|
22 |
|
char *progname; /* driver name */ |
23 |
|
|
24 |
< |
int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(); |
24 |
> |
int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(), r_flush(); |
25 |
|
|
26 |
|
int (*dev_func[NREQUESTS])() = { /* request handlers */ |
27 |
|
r_clear, r_paintr, |
28 |
< |
r_getcur, r_comout, r_comin |
28 |
> |
r_getcur, r_comout, |
29 |
> |
r_comin, r_flush |
30 |
|
}; |
31 |
|
|
32 |
|
|
39 |
|
/* set up I/O */ |
40 |
|
progname = argv[0]; |
41 |
|
if (argc < 3) { |
42 |
< |
stderr_v("arg count\n"); |
42 |
> |
eputs("arg count\n"); |
43 |
|
quit(1); |
44 |
|
} |
45 |
|
devin = fdopen(atoi(argv[1]), "r"); |
46 |
|
devout = fdopen(atoi(argv[2]), "w"); |
47 |
|
if (devin == NULL || devout == NULL || getw(devin) != COM_SENDM) { |
48 |
< |
stderr_v("connection failure\n"); |
48 |
> |
eputs("connection failure\n"); |
49 |
|
quit(1); |
50 |
|
} |
51 |
|
/* open device */ |
52 |
< |
if ((dev = dinit(argv[0], argv[3])) == NULL) { |
61 |
< |
stderr_v("initialization failure\n"); |
52 |
> |
if ((dev = dinit(argv[0], argv[3])) == NULL) |
53 |
|
quit(1); |
63 |
– |
} |
54 |
|
putw(COM_RECVM, devout); /* verify initialization */ |
55 |
< |
putw(dev->xsiz, devout); |
66 |
< |
putw(dev->ysiz, devout); |
55 |
> |
sendstate(); |
56 |
|
fflush(devout); |
57 |
|
/* loop on requests */ |
58 |
|
while ((com = getc(devin)) != EOF) { |
59 |
|
if (com >= NREQUESTS || dev_func[com] == NULL) { |
60 |
< |
stderr_v("invalid request\n"); |
60 |
> |
eputs("invalid request\n"); |
61 |
|
quit(1); |
62 |
|
} |
63 |
|
(*dev_func[com])(); /* process request */ |
66 |
|
} |
67 |
|
|
68 |
|
|
69 |
+ |
void |
70 |
|
quit(code) /* close device and exit */ |
71 |
|
int code; |
72 |
|
{ |
91 |
|
COLOR col; |
92 |
|
int xmin, ymin, xmax, ymax; |
93 |
|
|
94 |
< |
fread(col, sizeof(COLOR), 1, devin); |
94 |
> |
getbinary((char *)col, sizeof(COLOR), 1, devin); |
95 |
|
xmin = getw(devin); ymin = getw(devin); |
96 |
|
xmax = getw(devin); ymax = getw(devin); |
97 |
|
(*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 |
– |
} |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
+ |
r_flush() /* flush output */ |
102 |
+ |
{ |
103 |
+ |
if (dev->flush != NULL) |
104 |
+ |
(*dev->flush)(); |
105 |
+ |
putc(COM_FLUSH, devout); |
106 |
+ |
sendstate(); |
107 |
+ |
fflush(devout); |
108 |
+ |
} |
109 |
+ |
|
110 |
+ |
|
111 |
|
r_getcur() /* get and return cursor position */ |
112 |
|
{ |
113 |
|
int c; |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
< |
r_comout() /* print string to command line */ |
130 |
> |
void |
131 |
> |
mygets(char *s, FILE *fp) /* get string from file (with nul) */ |
132 |
|
{ |
133 |
< |
char str[256]; |
133 |
> |
int c; |
134 |
|
|
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 |
– |
|
135 |
|
while ((c = getc(fp)) != EOF) |
136 |
|
if ((*s++ = c) == '\0') |
137 |
|
return; |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
< |
myputs(s, fp) /* put string to file (with nul) */ |
143 |
< |
register char *s; |
173 |
< |
register FILE *fp; |
142 |
> |
void |
143 |
> |
myputs(const char *s, FILE *fp) /* put string to file (with nul) */ |
144 |
|
{ |
145 |
|
do |
146 |
|
putc(*s, fp); |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
< |
repaint(xmin, ymin, xmax, ymax) /* repaint section of display */ |
182 |
< |
int xmin, ymin, xmax, ymax; |
151 |
> |
r_comout() /* print string to command line */ |
152 |
|
{ |
153 |
< |
stderr_v("repaint called!\n"); /* no can do! */ |
153 |
> |
char str[256]; |
154 |
> |
|
155 |
> |
mygets(str, devin); |
156 |
> |
(*dev->comout)(str); |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
< |
stderr_v(s) /* put string to stderr */ |
189 |
< |
register char *s; |
160 |
> |
r_comin() /* read string from command line */ |
161 |
|
{ |
162 |
< |
static int inline = 0; |
162 |
> |
char buf[256], *prompt; |
163 |
> |
/* get prompt */ |
164 |
> |
if (getc(devin)) { |
165 |
> |
mygets(buf, devin); |
166 |
> |
prompt = buf; |
167 |
> |
} else |
168 |
> |
prompt = NULL; |
169 |
> |
/* get string */ |
170 |
> |
(*dev->comin)(buf, prompt); |
171 |
> |
/* reply */ |
172 |
> |
putc(COM_COMIN, devout); |
173 |
> |
myputs(buf, devout); |
174 |
> |
sendstate(); |
175 |
> |
fflush(devout); |
176 |
> |
} |
177 |
|
|
178 |
< |
if (!inline++) { |
178 |
> |
|
179 |
> |
void |
180 |
> |
eputs(const char *s) /* put string to stderr */ |
181 |
> |
{ |
182 |
> |
static int midline = 0; |
183 |
> |
|
184 |
> |
if (!*s) |
185 |
> |
return; |
186 |
> |
if (!midline++) { |
187 |
|
fputs(progname, stderr); |
188 |
|
fputs(": ", stderr); |
189 |
|
} |
190 |
|
fputs(s, stderr); |
191 |
< |
if (*s && s[strlen(s)-1] == '\n') { |
191 |
> |
if (s[strlen(s)-1] == '\n') { |
192 |
|
fflush(stderr); |
193 |
< |
inline = 0; |
193 |
> |
midline = 0; |
194 |
|
} |
195 |
+ |
} |
196 |
+ |
|
197 |
+ |
|
198 |
+ |
sendstate() /* send driver state variables */ |
199 |
+ |
{ |
200 |
+ |
putbinary(&dev->pixaspect, sizeof(dev->pixaspect), 1, devout); |
201 |
+ |
putw(dev->xsiz, devout); |
202 |
+ |
putw(dev->ysiz, devout); |
203 |
+ |
putw(dev->inpready, devout); |
204 |
|
} |