xref: /original-bsd/usr.bin/window/ttzapple.c (revision 07d71086)
1 /*
2  * Copyright (c) 1989 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)ttzapple.c	3.3 (Berkeley) 08/02/89";
20 #endif /* not lint */
21 
22 #include "ww.h"
23 #include "tt.h"
24 #include "char.h"
25 
26 /*
27 zz|zapple|unorthodox apple:\
28 	:am:pt:co#80:li#24:le=^H:nd=^F:up=^K:do=^J:\
29 	:ho=^[0:ll=^[1:cm=^]%+ %+ =:ch=^\%+ <:cv=^\%+ >:\
30 	:cl=^[4:ce=^[2:cd=^[3:rp=^]%.%+ @:\
31 	:so=^[+:se=^[-:\
32 	:dc=^[c:DC=^\%+ C:ic=^[i:IC=^\%+ I:\
33 	:al=^[a:AL=^\%+ A:dl=^[d:DL=^\%+ D:\
34 	:sf=^[f:SF=^\%+ F:sr=^[r:SR=^\%+ R:cs=^]%+ %+ ?:
35 */
36 
37 #define NCOL		80
38 #define NROW		24
39 #define TOKEN_MAX	32
40 
41 #define pc(c)	ttputc(c)
42 #define esc()	pc(ctrl('['))
43 #define esc1()	pc(ctrl('\\'))
44 #define esc2()	pc(ctrl(']'))
45 
46 extern short gen_frame[];
47 
48 zz_setmodes(new)
49 {
50 	if (new & WWM_REV) {
51 		if ((tt.tt_modes & WWM_REV) == 0) {
52 			esc();
53 			pc('+');
54 		}
55 	} else
56 		if (tt.tt_modes & WWM_REV) {
57 			esc();
58 			pc('-');
59 		}
60 	tt.tt_modes = new;
61 }
62 
63 zz_insline(n)
64 {
65 	if (n == 0) {
66 		esc();
67 		pc('a');
68 	} else {
69 		esc1();
70 		pc(n + ' ');
71 		pc('A');
72 	}
73 }
74 
75 zz_delline(n)
76 {
77 	if (n == 0) {
78 		esc();
79 		pc('d');
80 	} else {
81 		esc1();
82 		pc(n + ' ');
83 		pc('D');
84 	}
85 }
86 
87 zz_putc(c)
88 	char c;
89 {
90 	if (tt.tt_nmodes != tt.tt_modes)
91 		zz_setmodes(tt.tt_nmodes);
92 	ttputc(c);
93 	if (++tt.tt_col == NCOL)
94 		tt.tt_col = 0, tt.tt_row++;
95 }
96 
97 zz_write(p, n)
98 	register char *p;
99 	register n;
100 {
101 	if (tt.tt_nmodes != tt.tt_modes)
102 		zz_setmodes(tt.tt_nmodes);
103 	ttwrite(p, n);
104 	tt.tt_col += n;
105 	if (tt.tt_col == NCOL)
106 		tt.tt_col = 0, tt.tt_row++;
107 }
108 
109 zz_move(row, col)
110 	register row, col;
111 {
112 	register x;
113 
114 	if (tt.tt_row == row) {
115 		if ((x = col - tt.tt_col) == 0)
116 			return;
117 		if (col == 0) {
118 			pc('\r');
119 			goto out;
120 		}
121 		switch (x) {
122 		case 2:
123 			pc(ctrl('f'));
124 		case 1:
125 			pc(ctrl('f'));
126 			goto out;
127 		case -2:
128 			pc('\b');
129 		case -1:
130 			pc('\b');
131 			goto out;
132 		}
133 		if (col & 7 == 0 && x > 0 && x <= 16) {
134 			pc('\t');
135 			if (x > 8)
136 				pc('\t');
137 			goto out;
138 		}
139 		esc1();
140 		pc(col + ' ');
141 		pc('<');
142 		goto out;
143 	}
144 	if (tt.tt_col == col) {
145 		switch (row - tt.tt_row) {
146 		case 2:
147 			pc('\n');
148 		case 1:
149 			pc('\n');
150 			goto out;
151 		case -2:
152 			pc(ctrl('k'));
153 		case -1:
154 			pc(ctrl('k'));
155 			goto out;
156 		}
157 		if (col == 0) {
158 			if (row == 0)
159 				goto home;
160 			if (row == NROW - 1)
161 				goto ll;
162 		}
163 		esc1();
164 		pc(row + ' ');
165 		pc('>');
166 		goto out;
167 	}
168 	if (col == 0) {
169 		if (row == 0) {
170 home:
171 			esc();
172 			pc('0');
173 			goto out;
174 		}
175 		if (row == tt.tt_row + 1) {
176 			pc('\r');
177 			pc('\n');
178 			goto out;
179 		}
180 		if (row == NROW - 1) {
181 ll:
182 			esc();
183 			pc('1');
184 			goto out;
185 		}
186 	}
187 	esc2();
188 	pc(' ' + row);
189 	pc(' ' + col);
190 	pc('=');
191 out:
192 	tt.tt_col = col;
193 	tt.tt_row = row;
194 }
195 
196 zz_start()
197 {
198 	zz_setmodes(0);
199 	zz_setscroll(0, NROW - 1);
200 	zz_clear();
201 	esc1();
202 	pc(TOKEN_MAX + ' ');
203 	pc('T');
204 }
205 
206 zz_end()
207 {
208 	esc1();
209 	pc(' ');
210 	pc('T');
211 }
212 
213 zz_clreol()
214 {
215 	esc();
216 	pc('2');
217 }
218 
219 zz_clreos()
220 {
221 	esc();
222 	pc('3');
223 }
224 
225 zz_clear()
226 {
227 	esc();
228 	pc('4');
229 	tt.tt_col = tt.tt_row = 0;
230 }
231 
232 zz_inschar(n)
233 {
234 	if (n != 1) {
235 		esc1();
236 		pc(n + ' ');
237 		pc('I');
238 	} else {
239 		esc();
240 		pc('i');
241 	}
242 }
243 
244 zz_delchar(n)
245 {
246 	if (n != 1) {
247 		esc1();
248 		pc(n + ' ');
249 		pc('C');
250 	} else {
251 		esc();
252 		pc('c');
253 	}
254 }
255 
256 zz_scroll_down(n)
257 {
258 	if (n != 1) {
259 		esc1();
260 		pc(n + ' ');
261 		pc('F');
262 	} else if (tt.tt_row == NROW - 1)
263 		pc('\n');
264 	else {
265 		esc();
266 		pc('f');
267 	}
268 }
269 
270 zz_scroll_up(n)
271 {
272 	if (n == 1) {
273 		esc();
274 		pc('r');
275 	} else {
276 		esc1();
277 		pc(n + ' ');
278 		pc('R');
279 	}
280 }
281 
282 zz_setscroll(top, bot)
283 {
284 	esc2();
285 	pc(top + ' ');
286 	pc(bot + ' ');
287 	pc('?');
288 	tt.tt_scroll_top = top;
289 	tt.tt_scroll_bot = bot;
290 }
291 
292 int zz_debug = 0;
293 
294 zz_set_token(t, s, n)
295 	char *s;
296 {
297 	if (tt.tt_nmodes != tt.tt_modes)
298 		zz_setmodes(tt.tt_nmodes);
299 	if (zz_debug) {
300 		char buf[100];
301 		zz_setmodes(WWM_REV);
302 		(void) sprintf(buf, "%02x=", t);
303 		ttputs(buf);
304 		tt.tt_col += 3;
305 	}
306 	pc(ctrl('^'));
307 	pc(t);
308 	s[n - 1] |= 0x80;
309 	ttwrite(s, n);
310 	s[n - 1] &= ~0x80;
311 	tt.tt_col += n;
312 	if (tt.tt_col == NCOL)
313 		tt.tt_col = 0, tt.tt_row++;
314 }
315 
316 /*ARGSUSED*/
317 zz_put_token(t, s, n)
318 	char *s;
319 {
320 	if (tt.tt_nmodes != tt.tt_modes)
321 		zz_setmodes(tt.tt_nmodes);
322 	if (zz_debug) {
323 		char buf[100];
324 		zz_setmodes(WWM_REV);
325 		(void) sprintf(buf, "%02x>", t);
326 		ttputs(buf);
327 		tt.tt_col += 3;
328 	}
329 	pc(t | 0x80);
330 	tt.tt_col += n;
331 	if (tt.tt_col == NCOL)
332 		tt.tt_col = 0, tt.tt_row++;
333 }
334 
335 tt_zapple()
336 {
337 	tt.tt_inschar = zz_inschar;
338 	tt.tt_delchar = zz_delchar;
339 	tt.tt_insline = zz_insline;
340 	tt.tt_delline = zz_delline;
341 	tt.tt_clreol = zz_clreol;
342 	tt.tt_clreos = zz_clreos;
343 	tt.tt_scroll_down = zz_scroll_down;
344 	tt.tt_scroll_up = zz_scroll_up;
345 	tt.tt_setscroll = zz_setscroll;
346 	tt.tt_availmodes = WWM_REV;
347 	tt.tt_wrap = 1;
348 	tt.tt_retain = 0;
349 	tt.tt_ncol = NCOL;
350 	tt.tt_nrow = NROW;
351 	tt.tt_start = zz_start;
352 	tt.tt_end = zz_end;
353 	tt.tt_write = zz_write;
354 	tt.tt_putc = zz_putc;
355 	tt.tt_move = zz_move;
356 	tt.tt_clear = zz_clear;
357 	tt.tt_setmodes = zz_setmodes;
358 	tt.tt_frame = gen_frame;
359 	tt.tt_ntoken = 128;
360 	tt.tt_set_token = zz_set_token;
361 	tt.tt_put_token = zz_put_token;
362 	tt.tt_token_min = 1;
363 	tt.tt_token_max = TOKEN_MAX;
364 	tt.tt_set_token_cost = 2;
365 	tt.tt_put_token_cost = 1;
366 	return 0;
367 }
368