1 |
/* Copyright (c) 1987 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* aed.c - driver for AED 512 terminal. |
9 |
* |
10 |
* 2/2/87 |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
|
15 |
#include "driver.h" |
16 |
|
17 |
#include "color.h" |
18 |
|
19 |
|
20 |
/* AED command characters */ |
21 |
|
22 |
#define AEDFMT "1888N" /* Format string to send to AED */ |
23 |
#define CSTAT 0100 /* Console status: lower case */ |
24 |
#define SCT 75 /* Set color lookup table */ |
25 |
#define SEC 67 /* Set color for vector drawing */ |
26 |
#define MOV 81 /* Set CAP (current access pointer) to x, y */ |
27 |
#define OPT 40 /* Miscellaneous terminal control */ |
28 |
#define SEN 71 /* Set encoding types */ |
29 |
#define RST 48 /* Reset terminal */ |
30 |
#define FFD 12 /* Clear screen */ |
31 |
#define EJC 85 /* Enable Joystick cursor positioning */ |
32 |
#define DJC 100 /* Disable Joystick cursor positioning */ |
33 |
#define SCC 99 /* Set Cursor Colors */ |
34 |
#define SCP 93 /* Set Cursor Parameters */ |
35 |
#define DFR 111 /* Draw Filled Rectangle */ |
36 |
#define RCP 106 /* Read Cursor Position */ |
37 |
#define ESC 27 /* Escape starts a command sequence */ |
38 |
#define SCS 96 /* Set Console Status */ |
39 |
#define END 1 /* Ctrl-A used to terminate command mode */ |
40 |
|
41 |
#define BLK 0 /* color table entry for black */ |
42 |
#define WHT 7 /* color table entry for white */ |
43 |
|
44 |
#define command(c) (putc(ESC, stdout), putc(c, stdout)) |
45 |
#define byte(b) putc(b, stdout) |
46 |
#define string(s) fputs(s, stdout) |
47 |
#define flush() fflush(stdout) |
48 |
|
49 |
#define GAMMA 2.0 /* exponent for color correction */ |
50 |
|
51 |
#define MINCOLOR 8 /* start of device color table */ |
52 |
|
53 |
#define NCOLORS 241 /* our color table size (prime) */ |
54 |
|
55 |
#define hashcolr(c) ((67*(c)[RED]+59*(c)[GRN]+71*(c)[BLU])%NCOLORS) |
56 |
|
57 |
#define NCOLS 512 /* maximum # columns for output */ |
58 |
#define NROWS 512-COMHT /* maximum # rows for output */ |
59 |
#define COMHT 16 /* height of command line */ |
60 |
#define COMCW 63 /* maximum chars on command line */ |
61 |
|
62 |
static COLR colrmap[256]; /* our color map */ |
63 |
|
64 |
static COLR colrtbl[NCOLORS]; /* our color table */ |
65 |
|
66 |
static int colres = 64; /* color resolution */ |
67 |
|
68 |
int aed_close(), aed_clear(), aed_paintr(), |
69 |
aed_getcur(), aed_comout(), aed_errout(); |
70 |
|
71 |
static struct driver aed_driver = { |
72 |
aed_close, aed_clear, aed_paintr, aed_getcur, |
73 |
aed_comout, NULL, |
74 |
NCOLS, NROWS |
75 |
}; |
76 |
|
77 |
|
78 |
struct driver * |
79 |
aed_init(name) /* open AED */ |
80 |
char *name; |
81 |
{ |
82 |
if (ttyset(&aed_driver, fileno(stdin)) < 0) { /* set tty driver */ |
83 |
stderr_v("cannot access terminal\n"); |
84 |
return(NULL); |
85 |
} |
86 |
command(RST); /* reset AED */ |
87 |
longwait(2); |
88 |
command(OPT); |
89 |
byte(6); byte(1); /* AED command set */ |
90 |
command(SEN); |
91 |
string(AEDFMT); |
92 |
command(SCS); /* set console status */ |
93 |
byte(CSTAT); |
94 |
command(SCC); /* set cursor type */ |
95 |
byte(BLK); byte(WHT); byte(15); |
96 |
command(SCP); |
97 |
byte('+'); byte(0); byte(1); |
98 |
errvec = aed_errout; /* set error vector */ |
99 |
cmdvec = aed_errout; |
100 |
if (wrnvec != NULL) |
101 |
wrnvec = aed_errout; |
102 |
return(&aed_driver); |
103 |
} |
104 |
|
105 |
|
106 |
static |
107 |
aed_close() /* close AED */ |
108 |
{ |
109 |
errvec = stderr_v; /* reset error vector */ |
110 |
cmdvec = NULL; |
111 |
if (wrnvec != NULL) |
112 |
wrnvec = stderr_v; |
113 |
aedsetcap(0, 0); /* go to bottom */ |
114 |
command(SEC); |
115 |
byte(WHT); /* white text */ |
116 |
byte(END); |
117 |
byte('\n'); /* scroll */ |
118 |
flush(); |
119 |
ttydone(); |
120 |
} |
121 |
|
122 |
|
123 |
static |
124 |
aed_clear(x, y) /* clear AED */ |
125 |
int x, y; |
126 |
{ |
127 |
command(FFD); |
128 |
flush(); |
129 |
makemap(); /* init color map */ |
130 |
} |
131 |
|
132 |
|
133 |
static |
134 |
aed_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */ |
135 |
COLOR col; |
136 |
int xmin, ymin, xmax, ymax; |
137 |
{ |
138 |
int ndx; |
139 |
|
140 |
if ((ndx = colindex(col)) < 0) { /* full table? */ |
141 |
colres >>= 1; |
142 |
redraw(); |
143 |
return; |
144 |
} |
145 |
command(SEC); /* draw rectangle */ |
146 |
byte(ndx+MINCOLOR); |
147 |
aedsetcap(xmin, ymin+COMHT); |
148 |
command(DFR); |
149 |
aedcoord(xmax-1, ymax+(-1+COMHT)); |
150 |
flush(); |
151 |
} |
152 |
|
153 |
|
154 |
static int |
155 |
aed_getcur(xp, yp) /* get cursor position */ |
156 |
int *xp, *yp; |
157 |
{ |
158 |
int com; |
159 |
|
160 |
command(EJC); /* enable cursor */ |
161 |
com = getch(); /* get key */ |
162 |
command(DJC); /* disable cursor */ |
163 |
aedgetcap(xp, yp); /* get cursor position */ |
164 |
*yp -= COMHT; /* convert coordinates */ |
165 |
return(com); /* return key hit */ |
166 |
} |
167 |
|
168 |
|
169 |
static |
170 |
aed_comout(out) /* output to command line */ |
171 |
register char *out; |
172 |
{ |
173 |
static int newl = 1; |
174 |
static int inmsg = 0; |
175 |
|
176 |
while (*out) { |
177 |
if (newl) { /* new line */ |
178 |
command(SEC); |
179 |
byte(BLK); |
180 |
aedsetcap(0, 0); |
181 |
command(DFR); |
182 |
aedcoord(NCOLS-1, COMHT-1); |
183 |
aedsetcap(1, 4); |
184 |
command(SEC); /* white text */ |
185 |
byte(WHT); |
186 |
byte(END); |
187 |
newl = 0; |
188 |
} |
189 |
switch (*out) { |
190 |
case '\n': /* end of line */ |
191 |
newl = 1; |
192 |
/* fall through */ |
193 |
case '\r': |
194 |
inmsg = 0; |
195 |
byte('\r'); |
196 |
break; |
197 |
case '\b': /* back space */ |
198 |
if (inmsg > 0 && --inmsg < COMCW) |
199 |
byte('\b'); |
200 |
break; |
201 |
default: /* character */ |
202 |
if (inmsg++ < COMCW) |
203 |
byte(*out); |
204 |
break; |
205 |
} |
206 |
out++; |
207 |
} |
208 |
flush(); |
209 |
} |
210 |
|
211 |
|
212 |
static |
213 |
aed_errout(msg) /* print an error message */ |
214 |
char *msg; |
215 |
{ |
216 |
aed_comout(msg); |
217 |
if (*msg && msg[strlen(msg)-1] == '\n') |
218 |
longwait(2); |
219 |
} |
220 |
|
221 |
|
222 |
/* |
223 |
* aedsetcap - sets AED's current access pointer to (x, y). |
224 |
*/ |
225 |
|
226 |
static |
227 |
aedsetcap(x, y) |
228 |
register int x, y; |
229 |
{ |
230 |
command(MOV); |
231 |
aedcoord(x, y); |
232 |
} |
233 |
|
234 |
/* |
235 |
* aedcoord - puts out an (x, y) coordinate in AED 8 bit format. |
236 |
*/ |
237 |
|
238 |
static |
239 |
aedcoord(x, y) |
240 |
register int x, y; |
241 |
{ |
242 |
byte(((x >> 4) & 0x30) | ((y >> 8) & 0x3)); |
243 |
byte(x & 0xff); |
244 |
byte(y & 0xff); |
245 |
} |
246 |
|
247 |
|
248 |
static |
249 |
aedgetcap(xp, yp) /* get cursor postion */ |
250 |
int *xp, *yp; |
251 |
{ |
252 |
register int c; |
253 |
/* get cursor postition */ |
254 |
command(RCP); |
255 |
flush(); |
256 |
c = getch(); |
257 |
*xp = (c & 0x30) << 4; |
258 |
*yp = (c & 0x3) << 8; |
259 |
*xp |= getch(); |
260 |
*yp |= getch(); |
261 |
} |
262 |
|
263 |
|
264 |
static int |
265 |
colindex(col) /* return table index for color */ |
266 |
COLOR col; |
267 |
{ |
268 |
static COLR clr; |
269 |
int hval; |
270 |
register int ndx, i; |
271 |
|
272 |
mapcolor(clr, col); |
273 |
|
274 |
hval = ndx = hashcolr(clr); |
275 |
|
276 |
for (i = 1; i < NCOLORS; i++) { |
277 |
if (colrtbl[ndx][EXP] == 0) { |
278 |
colrtbl[ndx][RED] = clr[RED]; |
279 |
colrtbl[ndx][GRN] = clr[GRN]; |
280 |
colrtbl[ndx][BLU] = clr[BLU]; |
281 |
colrtbl[ndx][EXP] = COLXS; |
282 |
newcolr(ndx, clr); |
283 |
return(ndx); |
284 |
} |
285 |
if ( colrtbl[ndx][RED] == clr[RED] && |
286 |
colrtbl[ndx][GRN] == clr[GRN] && |
287 |
colrtbl[ndx][BLU] == clr[BLU] ) |
288 |
return(ndx); |
289 |
ndx = (hval + i*i) % NCOLORS; |
290 |
} |
291 |
return(-1); |
292 |
} |
293 |
|
294 |
|
295 |
static |
296 |
newcolr(index, clr) /* enter a color into our table */ |
297 |
int index; |
298 |
COLR clr; |
299 |
{ |
300 |
command(SCT); |
301 |
byte(index+MINCOLOR); |
302 |
byte(1); |
303 |
byte(clr[RED]); |
304 |
byte(clr[GRN]); |
305 |
byte(clr[BLU]); |
306 |
flush(); |
307 |
} |
308 |
|
309 |
|
310 |
static |
311 |
mapcolor(clr, col) /* map to our color space */ |
312 |
COLR clr; |
313 |
COLOR col; |
314 |
{ |
315 |
register int i, p; |
316 |
/* compute color table value */ |
317 |
for (i = 0; i < 3; i++) { |
318 |
p = colval(col,i) * 256.0; |
319 |
if (p < 0) p = 0; |
320 |
else if (p >= 256) p = 255; |
321 |
clr[i] = colrmap[p][i]; |
322 |
} |
323 |
clr[EXP] = COLXS; |
324 |
} |
325 |
|
326 |
|
327 |
static |
328 |
makemap() /* initialize the color map */ |
329 |
{ |
330 |
double pow(); |
331 |
int val; |
332 |
register int i; |
333 |
|
334 |
for (i = 0; i < 256; i++) { |
335 |
val = pow(i/256.0, 1.0/GAMMA) * colres; |
336 |
val = (val*256 + 128) / colres; |
337 |
colrmap[i][RED] = colrmap[i][GRN] = colrmap[i][BLU] = val; |
338 |
colrmap[i][EXP] = COLXS; |
339 |
} |
340 |
for (i = 0; i < NCOLORS; i++) |
341 |
colrtbl[i][EXP] = 0; |
342 |
} |
343 |
|
344 |
|
345 |
static |
346 |
longwait(t) /* longer wait */ |
347 |
int t; |
348 |
{ |
349 |
flush(); |
350 |
sleep(t); |
351 |
} |