xref: /original-bsd/usr.bin/window/ttzapple.c (revision 860e07fc)
1 /*
2  * Copyright (c) 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)ttzapple.c	3.12 (Berkeley) 06/24/92";
13 #endif /* not lint */
14 
15 #include "ww.h"
16 #include "tt.h"
17 #include "char.h"
18 
19 /*
20 zz|zapple|perfect apple:\
21 	:am:pt:co#80:li#24:le=^H:nd=^F:up=^K:do=^J:\
22 	:ho=\E0:ll=\E1:cm=\E=%+ %+ :ch=\E<%+ :cv=\E>%+ :\
23 	:cl=\E4:ce=\E2:cd=\E3:rp=\E@%.%+ :\
24 	:so=\E+:se=\E-:\
25 	:dc=\Ec:DC=\EC%+ :ic=\Ei:IC=\EI%+ :\
26 	:al=\Ea:AL=\EA%+ :dl=\Ed:DL=\ED%+ :\
27 	:sf=\Ef:SF=\EF%+ :sr=\Er:SR=\ER%+ :cs=\E?%+ %+ :\
28 	:is=\E-\ET :
29 */
30 
31 #define NCOL		80
32 #define NROW		24
33 #define TOKEN_MAX	32
34 
35 extern short gen_frame[];
36 
37 	/* for error correction */
38 int zz_ecc;
39 
40 zz_setmodes(new)
41 {
42 	if (new & WWM_REV) {
43 		if ((tt.tt_modes & WWM_REV) == 0)
44 			ttesc('+');
45 	} else
46 		if (tt.tt_modes & WWM_REV)
47 			ttesc('-');
48 	tt.tt_modes = new;
49 }
50 
51 zz_insline(n)
52 {
53 	if (n == 1)
54 		ttesc('a');
55 	else {
56 		ttesc('A');
57 		ttputc(n + ' ');
58 	}
59 }
60 
61 zz_delline(n)
62 {
63 	if (n == 1)
64 		ttesc('d');
65 	else {
66 		ttesc('D');
67 		ttputc(n + ' ');
68 	}
69 }
70 
71 zz_putc(c)
72 	char c;
73 {
74 	if (tt.tt_nmodes != tt.tt_modes)
75 		zz_setmodes(tt.tt_nmodes);
76 	ttputc(c);
77 	if (++tt.tt_col == NCOL)
78 		tt.tt_col = 0, tt.tt_row++;
79 }
80 
81 zz_write(p, n)
82 	register char *p;
83 	register n;
84 {
85 	if (tt.tt_nmodes != tt.tt_modes)
86 		zz_setmodes(tt.tt_nmodes);
87 	ttwrite(p, n);
88 	tt.tt_col += n;
89 	if (tt.tt_col == NCOL)
90 		tt.tt_col = 0, tt.tt_row++;
91 }
92 
93 zz_move(row, col)
94 	register row, col;
95 {
96 	register x;
97 
98 	if (tt.tt_row == row) {
99 same_row:
100 		if ((x = col - tt.tt_col) == 0)
101 			return;
102 		if (col == 0) {
103 			ttctrl('m');
104 			goto out;
105 		}
106 		switch (x) {
107 		case 2:
108 			ttctrl('f');
109 		case 1:
110 			ttctrl('f');
111 			goto out;
112 		case -2:
113 			ttctrl('h');
114 		case -1:
115 			ttctrl('h');
116 			goto out;
117 		}
118 		if ((col & 7) == 0 && x > 0 && x <= 16) {
119 			ttctrl('i');
120 			if (x > 8)
121 				ttctrl('i');
122 			goto out;
123 		}
124 		ttesc('<');
125 		ttputc(col + ' ');
126 		goto out;
127 	}
128 	if (tt.tt_col == col) {
129 		switch (row - tt.tt_row) {
130 		case 2:
131 			ttctrl('j');
132 		case 1:
133 			ttctrl('j');
134 			goto out;
135 		case -2:
136 			ttctrl('k');
137 		case -1:
138 			ttctrl('k');
139 			goto out;
140 		}
141 		if (col == 0) {
142 			if (row == 0)
143 				goto home;
144 			if (row == NROW - 1)
145 				goto ll;
146 		}
147 		ttesc('>');
148 		ttputc(row + ' ');
149 		goto out;
150 	}
151 	if (col == 0) {
152 		if (row == 0) {
153 home:
154 			ttesc('0');
155 			goto out;
156 		}
157 		if (row == tt.tt_row + 1) {
158 			/*
159 			 * Do newline first to match the sequence
160 			 * for scroll down and return
161 			 */
162 			ttctrl('j');
163 			ttctrl('m');
164 			goto out;
165 		}
166 		if (row == NROW - 1) {
167 ll:
168 			ttesc('1');
169 			goto out;
170 		}
171 	}
172 	/* favor local motion for better compression */
173 	if (row == tt.tt_row + 1) {
174 		ttctrl('j');
175 		goto same_row;
176 	}
177 	if (row == tt.tt_row - 1) {
178 		ttctrl('k');
179 		goto same_row;
180 	}
181 	ttesc('=');
182 	ttputc(' ' + row);
183 	ttputc(' ' + col);
184 out:
185 	tt.tt_col = col;
186 	tt.tt_row = row;
187 }
188 
189 zz_start()
190 {
191 	zz_setmodes(0);
192 	zz_setscroll(0, NROW - 1);
193 	zz_clear();
194 	ttesc('T');
195 	ttputc(TOKEN_MAX + ' ');
196 	ttesc('U');
197 	ttputc('!');
198 	zz_ecc = 1;
199 }
200 
201 zz_end()
202 {
203 	ttesc('T');
204 	ttputc(' ');
205 	ttesc('U');
206 	ttputc(' ');
207 	zz_ecc = 0;
208 }
209 
210 zz_clreol()
211 {
212 	ttesc('2');
213 }
214 
215 zz_clreos()
216 {
217 	ttesc('3');
218 }
219 
220 zz_clear()
221 {
222 	ttesc('4');
223 	tt.tt_col = tt.tt_row = 0;
224 }
225 
226 zz_insspace(n)
227 {
228 	if (n == 1)
229 		ttesc('i');
230 	else {
231 		ttesc('I');
232 		ttputc(n + ' ');
233 	}
234 }
235 
236 zz_delchar(n)
237 {
238 	if (n == 1)
239 		ttesc('c');
240 	else {
241 		ttesc('C');
242 		ttputc(n + ' ');
243 	}
244 }
245 
246 zz_scroll_down(n)
247 {
248 	if (n == 1)
249 		if (tt.tt_row == NROW - 1)
250 			ttctrl('j');
251 		else
252 			ttesc('f');
253 	else {
254 		ttesc('F');
255 		ttputc(n + ' ');
256 	}
257 }
258 
259 zz_scroll_up(n)
260 {
261 	if (n == 1)
262 		ttesc('r');
263 	else {
264 		ttesc('R');
265 		ttputc(n + ' ');
266 	}
267 }
268 
269 zz_setscroll(top, bot)
270 {
271 	ttesc('?');
272 	ttputc(top + ' ');
273 	ttputc(bot + ' ');
274 	tt.tt_scroll_top = top;
275 	tt.tt_scroll_bot = bot;
276 }
277 
278 int zz_debug = 0;
279 
280 zz_set_token(t, s, n)
281 	char *s;
282 {
283 	if (tt.tt_nmodes != tt.tt_modes)
284 		zz_setmodes(tt.tt_nmodes);
285 	if (zz_debug) {
286 		char buf[100];
287 		zz_setmodes(WWM_REV);
288 		(void) sprintf(buf, "%02x=", t);
289 		ttputs(buf);
290 		tt.tt_col += 3;
291 	}
292 	ttputc(0x80);
293 	ttputc(t + 1);
294 	s[n - 1] |= 0x80;
295 	ttwrite(s, n);
296 	s[n - 1] &= ~0x80;
297 }
298 
299 /*ARGSUSED*/
300 zz_put_token(t, s, n)
301 	char *s;
302 {
303 	if (tt.tt_nmodes != tt.tt_modes)
304 		zz_setmodes(tt.tt_nmodes);
305 	if (zz_debug) {
306 		char buf[100];
307 		zz_setmodes(WWM_REV);
308 		(void) sprintf(buf, "%02x>", t);
309 		ttputs(buf);
310 		tt.tt_col += 3;
311 	}
312 	ttputc(t + 0x81);
313 }
314 
315 zz_rint(p, n)
316 	char *p;
317 {
318 	static lastc;
319 	register i;
320 	register char *q;
321 
322 	if (!zz_ecc)
323 		return n;
324 	for (i = n, q = p; --i >= 0;) {
325 		register c = (unsigned char) *p++;
326 		if (zz_ecc == 1) {
327 			if (c)
328 				*q++ = c;
329 			else {
330 				zz_ecc = 2;
331 				lastc = -1;
332 			}
333 		} else {
334 			if (lastc < 0) {
335 				lastc = c;
336 			} else if (lastc == c) {
337 				*q++ = lastc;
338 				lastc = -1;
339 			} else {
340 				wwnreadec++;
341 				lastc = c;
342 			}
343 		}
344 	}
345 	return q - (p - n);
346 }
347 
348 tt_zapple()
349 {
350 	tt.tt_insspace = zz_insspace;
351 	tt.tt_delchar = zz_delchar;
352 	tt.tt_insline = zz_insline;
353 	tt.tt_delline = zz_delline;
354 	tt.tt_clreol = zz_clreol;
355 	tt.tt_clreos = zz_clreos;
356 	tt.tt_scroll_down = zz_scroll_down;
357 	tt.tt_scroll_up = zz_scroll_up;
358 	tt.tt_setscroll = zz_setscroll;
359 	tt.tt_availmodes = WWM_REV;
360 	tt.tt_wrap = 1;
361 	tt.tt_retain = 0;
362 	tt.tt_ncol = NCOL;
363 	tt.tt_nrow = NROW;
364 	tt.tt_start = zz_start;
365 	tt.tt_end = zz_end;
366 	tt.tt_write = zz_write;
367 	tt.tt_putc = zz_putc;
368 	tt.tt_move = zz_move;
369 	tt.tt_clear = zz_clear;
370 	tt.tt_setmodes = zz_setmodes;
371 	tt.tt_frame = gen_frame;
372 	tt.tt_padc = TT_PADC_NONE;
373 	tt.tt_ntoken = 127;
374 	tt.tt_set_token = zz_set_token;
375 	tt.tt_put_token = zz_put_token;
376 	tt.tt_token_min = 1;
377 	tt.tt_token_max = TOKEN_MAX;
378 	tt.tt_set_token_cost = 2;
379 	tt.tt_put_token_cost = 1;
380 	tt.tt_rint = zz_rint;
381 	return 0;
382 }
383