xref: /freebsd/sys/dev/syscons/scterm-teken.c (revision da5069e1f7daaef1e7157876d6044de6f3a08ce2)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * All rights reserved.
6  *
7  * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer as
15  *    the first lines of this file unmodified.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_syscons.h"
36 #include "opt_teken.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/consio.h>
43 #include <sys/kbio.h>
44 
45 #if defined(__arm__) || defined(__mips__) || \
46 	defined(__powerpc__) || defined(__sparc64__)
47 #include <machine/sc_machdep.h>
48 #else
49 #include <machine/pc/display.h>
50 #endif
51 
52 #include <dev/syscons/syscons.h>
53 
54 #include <teken/teken.h>
55 
56 static void scteken_sc_to_te_attr(unsigned char, teken_attr_t *);
57 static int scteken_te_to_sc_attr(const teken_attr_t *);
58 
59 static sc_term_init_t		scteken_init;
60 static sc_term_term_t		scteken_term;
61 static sc_term_puts_t		scteken_puts;
62 static sc_term_ioctl_t		scteken_ioctl;
63 static sc_term_default_attr_t	scteken_default_attr;
64 static sc_term_clear_t		scteken_clear;
65 static sc_term_input_t		scteken_input;
66 static sc_term_fkeystr_t	scteken_fkeystr;
67 static sc_term_sync_t		scteken_sync;
68 static void			scteken_nop(void);
69 
70 typedef struct {
71 	teken_t		ts_teken;
72 	int		ts_busy;
73 } teken_stat;
74 
75 static teken_stat	reserved_teken_stat;
76 
77 static void scteken_sync_internal(scr_stat *, teken_stat *);
78 
79 static sc_term_sw_t sc_term_scteken = {
80 	{ NULL, NULL },
81 	"scteken",			/* emulator name */
82 	"teken terminal",		/* description */
83 	"*",				/* matching renderer */
84 	sizeof(teken_stat),		/* softc size */
85 	0,
86 	scteken_init,
87 	scteken_term,
88 	scteken_puts,
89 	scteken_ioctl,
90 	(sc_term_reset_t *)scteken_nop,
91 	scteken_default_attr,
92 	scteken_clear,
93 	(sc_term_notify_t *)scteken_nop,
94 	scteken_input,
95 	scteken_fkeystr,
96 	scteken_sync,
97 };
98 
99 SCTERM_MODULE(scteken, sc_term_scteken);
100 
101 static tf_bell_t	scteken_bell;
102 static tf_cursor_t	scteken_cursor;
103 static tf_putchar_t	scteken_putchar;
104 static tf_fill_t	scteken_fill;
105 static tf_copy_t	scteken_copy;
106 static tf_param_t	scteken_param;
107 static tf_respond_t	scteken_respond;
108 
109 static const teken_funcs_t scteken_funcs = {
110 	.tf_bell	= scteken_bell,
111 	.tf_cursor	= scteken_cursor,
112 	.tf_putchar	= scteken_putchar,
113 	.tf_fill	= scteken_fill,
114 	.tf_copy	= scteken_copy,
115 	.tf_param	= scteken_param,
116 	.tf_respond	= scteken_respond,
117 };
118 
119 static int
120 scteken_init(scr_stat *scp, void **softc, int code)
121 {
122 	teken_stat *ts;
123 	teken_attr_t ta;
124 
125 	if (*softc == NULL) {
126 		if (reserved_teken_stat.ts_busy)
127 			return (EINVAL);
128 		*softc = &reserved_teken_stat;
129 	}
130 	ts = *softc;
131 
132 	switch (code) {
133 	case SC_TE_COLD_INIT:
134 		++sc_term_scteken.te_refcount;
135 		ts->ts_busy = 1;
136 		/* FALLTHROUGH */
137 	case SC_TE_WARM_INIT:
138 		ta = *teken_get_defattr(&ts->ts_teken);
139 		teken_init(&ts->ts_teken, &scteken_funcs, scp);
140 		teken_set_defattr(&ts->ts_teken, &ta);
141 #ifndef TEKEN_UTF8
142 		teken_set_8bit(&ts->ts_teken);
143 #endif /* !TEKEN_UTF8 */
144 #ifdef TEKEN_CONS25
145 		teken_set_cons25(&ts->ts_teken);
146 #endif /* TEKEN_CONS25 */
147 		teken_set_cons25keys(&ts->ts_teken);
148 		scteken_sync_internal(scp, ts);
149 		break;
150 	}
151 
152 	return (0);
153 }
154 
155 static int
156 scteken_term(scr_stat *scp, void **softc)
157 {
158 
159 	if (*softc == &reserved_teken_stat) {
160 		*softc = NULL;
161 		reserved_teken_stat.ts_busy = 0;
162 	}
163 	--sc_term_scteken.te_refcount;
164 
165 	return (0);
166 }
167 
168 static void
169 scteken_puts(scr_stat *scp, u_char *buf, int len)
170 {
171 	teken_stat *ts = scp->ts;
172 
173 	scp->sc->write_in_progress++;
174 	teken_input(&ts->ts_teken, buf, len);
175 	scp->sc->write_in_progress--;
176 }
177 
178 static int
179 scteken_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
180 	     struct thread *td)
181 {
182 	teken_stat *ts = scp->ts;
183 	vid_info_t *vi;
184 	int attr;
185 
186 	switch (cmd) {
187 	case GIO_ATTR:      	/* get current attributes */
188 		*(int*)data =
189 		    scteken_te_to_sc_attr(teken_get_curattr(&ts->ts_teken));
190 		return (0);
191 	case CONS_GETINFO:  	/* get current (virtual) console info */
192 		vi = (vid_info_t *)data;
193 		if (vi->size != sizeof(struct vid_info))
194 			return EINVAL;
195 
196 		attr = scteken_te_to_sc_attr(teken_get_defattr(&ts->ts_teken));
197 		vi->mv_norm.fore = attr & 0x0f;
198 		vi->mv_norm.back = (attr >> 4) & 0x0f;
199 		vi->mv_rev.fore = vi->mv_norm.back;
200 		vi->mv_rev.back = vi->mv_norm.fore;
201 		/*
202 		 * The other fields are filled by the upper routine. XXX
203 		 */
204 		return (ENOIOCTL);
205 	}
206 
207 	return (ENOIOCTL);
208 }
209 
210 static void
211 scteken_default_attr(scr_stat *scp, int color, int rev_color)
212 {
213 	teken_stat *ts = scp->ts;
214 	teken_attr_t ta;
215 
216 	scteken_sc_to_te_attr(color, &ta);
217 	teken_set_defattr(&ts->ts_teken, &ta);
218 }
219 
220 static void
221 scteken_clear(scr_stat *scp)
222 {
223 	teken_stat *ts = scp->ts;
224 
225 	sc_move_cursor(scp, 0, 0);
226 	scteken_sync_internal(scp, ts);
227 	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20],
228 		     scteken_te_to_sc_attr(teken_get_curattr(&ts->ts_teken))
229 		     << 8);
230 	mark_all(scp);
231 }
232 
233 static int
234 scteken_input(scr_stat *scp, int c, struct tty *tp)
235 {
236 
237 	return FALSE;
238 }
239 
240 static const char *
241 scteken_fkeystr(scr_stat *scp, int c)
242 {
243 	teken_stat *ts = scp->ts;
244 	unsigned int k;
245 
246 	switch (c) {
247 	case FKEY | F(1):  case FKEY | F(2):  case FKEY | F(3):
248 	case FKEY | F(4):  case FKEY | F(5):  case FKEY | F(6):
249 	case FKEY | F(7):  case FKEY | F(8):  case FKEY | F(9):
250 	case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
251 		k = TKEY_F1 + c - (FKEY | F(1));
252 		break;
253 	case FKEY | F(49):
254 		k = TKEY_HOME;
255 		break;
256 	case FKEY | F(50):
257 		k = TKEY_UP;
258 		break;
259 	case FKEY | F(51):
260 		k = TKEY_PAGE_UP;
261 		break;
262 	case FKEY | F(53):
263 		k = TKEY_LEFT;
264 		break;
265 	case FKEY | F(55):
266 		k = TKEY_RIGHT;
267 		break;
268 	case FKEY | F(57):
269 		k = TKEY_END;
270 		break;
271 	case FKEY | F(58):
272 		k = TKEY_DOWN;
273 		break;
274 	case FKEY | F(59):
275 		k = TKEY_PAGE_DOWN;
276 		break;
277 	case FKEY | F(60):
278 		k = TKEY_INSERT;
279 		break;
280 	case FKEY | F(61):
281 		k = TKEY_DELETE;
282 		break;
283 	default:
284 		return (NULL);
285 	}
286 
287 	return (teken_get_sequence(&ts->ts_teken, k));
288 }
289 
290 static void
291 scteken_sync_internal(scr_stat *scp, teken_stat *ts)
292 {
293 	teken_pos_t tp;
294 
295 	tp.tp_col = scp->xsize;
296 	tp.tp_row = scp->ysize;
297 	teken_set_winsize_noreset(&ts->ts_teken, &tp);
298 	tp.tp_col = scp->xpos;
299 	tp.tp_row = scp->ypos;
300 	teken_set_cursor(&ts->ts_teken, &tp);
301 }
302 
303 static void
304 scteken_sync(scr_stat *scp)
305 {
306 	scteken_sync_internal(scp, scp->ts);
307 }
308 
309 static void
310 scteken_nop(void)
311 {
312 
313 }
314 
315 /*
316  * libteken routines.
317  */
318 
319 static const teken_color_t sc_to_te_color[] = {
320 	TC_BLACK,     TC_BLUE,         TC_GREEN,     TC_CYAN,
321 	TC_RED,       TC_MAGENTA,      TC_BROWN,     TC_WHITE,
322 };
323 
324 static const unsigned char te_to_sc_color[] = {
325 	FG_BLACK,     FG_RED,          FG_GREEN,     FG_BROWN,
326 	FG_BLUE,      FG_MAGENTA,      FG_CYAN,      FG_LIGHTGREY,
327 };
328 
329 static void
330 scteken_sc_to_te_attr(unsigned char color, teken_attr_t *a)
331 {
332 
333 	/*
334 	 * Conversions of attrs are not reversible.  Since sc attrs are
335 	 * pure colors in the simplest mode (16-color graphics) and the
336 	 * API is too deficient to tell us the mode, always convert to
337 	 * pure colors.  The conversion is essentially the identity except
338 	 * for reordering the non-brightness bits in the 2 color numbers.
339 	 */
340 	a->ta_format = 0;
341 	a->ta_fgcolor = sc_to_te_color[color & 7] | (color & 8);
342 	a->ta_bgcolor = sc_to_te_color[(color >> 4) & 7] | ((color >> 4) & 8);
343 }
344 
345 static int
346 scteken_te_to_sc_attr(const teken_attr_t *a)
347 {
348 	int attr;
349 	teken_color_t fg, bg;
350 
351 	if (a->ta_format & TF_REVERSE) {
352 		fg = a->ta_bgcolor;
353 		bg = a->ta_fgcolor;
354 	} else {
355 		fg = a->ta_fgcolor;
356 		bg = a->ta_bgcolor;
357 	}
358 	if (fg >= 16)
359 		fg = teken_256to16(fg);
360 	if (bg >= 16)
361 		bg = teken_256to16(bg);
362 	attr = te_to_sc_color[fg & 7] | (fg & 8) |
363 	    ((te_to_sc_color[bg & 7] | (bg & 8)) << 4);
364 
365 	/* XXX: underline mapping for Hercules adapter can be better. */
366 	if (a->ta_format & (TF_BOLD | TF_UNDERLINE))
367 		attr ^= 8;
368 	if (a->ta_format & TF_BLINK)
369 		attr ^= 0x80;
370 
371 	return (attr);
372 }
373 
374 static void
375 scteken_bell(void *arg)
376 {
377 	scr_stat *scp = arg;
378 
379 	sc_bell(scp, scp->bell_pitch, scp->bell_duration);
380 }
381 
382 static void
383 scteken_cursor(void *arg, const teken_pos_t *p)
384 {
385 	scr_stat *scp = arg;
386 
387 	sc_move_cursor(scp, p->tp_col, p->tp_row);
388 }
389 
390 #ifdef TEKEN_UTF8
391 struct unicp437 {
392 	uint16_t	unicode_base;
393 	uint8_t		cp437_base;
394 	uint8_t		length;
395 };
396 
397 static const struct unicp437 cp437table[] = {
398 	{ 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 },
399 	{ 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 },
400 	{ 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
401 	{ 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 },
402 	{ 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 },
403 	{ 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
404 	{ 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 },
405 	{ 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 },
406 	{ 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
407 	{ 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 },
408 	{ 0x00bf, 0xa8, 0x00 }, { 0x00c0, 0x41, 0x00 },
409 	{ 0x00c1, 0x41, 0x00 }, { 0x00c2, 0x41, 0x00 },
410 	{ 0x00c4, 0x8e, 0x01 }, { 0x00c6, 0x92, 0x00 },
411 	{ 0x00c7, 0x80, 0x00 }, { 0x00c8, 0x45, 0x00 },
412 	{ 0x00c9, 0x90, 0x00 }, { 0x00ca, 0x45, 0x00 },
413 	{ 0x00cb, 0x45, 0x00 }, { 0x00cc, 0x49, 0x00 },
414 	{ 0x00cd, 0x49, 0x00 }, { 0x00ce, 0x49, 0x00 },
415 	{ 0x00cf, 0x49, 0x00 }, { 0x00d1, 0xa5, 0x00 },
416 	{ 0x00d2, 0x4f, 0x00 }, { 0x00d3, 0x4f, 0x00 },
417 	{ 0x00d4, 0x4f, 0x00 }, { 0x00d6, 0x99, 0x00 },
418 	{ 0x00d9, 0x55, 0x00 }, { 0x00da, 0x55, 0x00 },
419 	{ 0x00db, 0x55, 0x00 }, { 0x00dc, 0x9a, 0x00 },
420 	{ 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
421 	{ 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 },
422 	{ 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 },
423 	{ 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
424 	{ 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 },
425 	{ 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 },
426 	{ 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
427 	{ 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 },
428 	{ 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 },
429 	{ 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
430 	{ 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 },
431 	{ 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 },
432 	{ 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
433 	{ 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 },
434 	{ 0x013f, 0x4c, 0x00 }, { 0x0140, 0x6c, 0x00 },
435 	{ 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 },
436 	{ 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
437 	{ 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 },
438 	{ 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 },
439 	{ 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
440 	{ 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 },
441 	{ 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 },
442 	{ 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
443 	{ 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 },
444 	{ 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 },
445 	{ 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
446 	{ 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 },
447 	{ 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 },
448 	{ 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
449 	{ 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 },
450 	{ 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 },
451 	{ 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
452 	{ 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 },
453 	{ 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 },
454 	{ 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
455 	{ 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 },
456 	{ 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 },
457 	{ 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
458 	{ 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 },
459 	{ 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 },
460 	{ 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
461 	{ 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 },
462 	{ 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 },
463 	{ 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
464 	{ 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 },
465 	{ 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 },
466 	{ 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
467 	{ 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 },
468 	{ 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 },
469 	{ 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
470 	{ 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 },
471 	{ 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 },
472 	{ 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
473 	{ 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 },
474 	{ 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 },
475 	{ 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
476 	{ 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 },
477 	{ 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 },
478 	{ 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
479 	{ 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 },
480 	{ 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 },
481 	{ 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
482 	{ 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 },
483 	{ 0x25ac, 0x16, 0x00 },
484 	{ 0x25ae, 0xdb, 0x00 }, { 0x25b2, 0x1e, 0x00 },
485 	{ 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
486 	{ 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 },
487 	{ 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 },
488 	{ 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
489 	{ 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 },
490 	{ 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 },
491 	{ 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x01 },
492 };
493 
494 static void
495 scteken_get_cp437(teken_char_t *c, int *attr)
496 {
497 	int min, mid, max;
498 
499 	min = 0;
500 	max = (sizeof(cp437table) / sizeof(struct unicp437)) - 1;
501 
502 	if (*c < cp437table[0].unicode_base ||
503 	    *c > cp437table[max].unicode_base + cp437table[max].length)
504 		goto bad;
505 
506 	while (max >= min) {
507 		mid = (min + max) / 2;
508 		if (*c < cp437table[mid].unicode_base) {
509 			max = mid - 1;
510 		} else if (*c > cp437table[mid].unicode_base +
511 		    cp437table[mid].length) {
512 			min = mid + 1;
513 		} else {
514 			*c -= cp437table[mid].unicode_base;
515 			*c += cp437table[mid].cp437_base;
516 			return;
517 		}
518 	}
519 bad:
520 	/* Character not present in CP437. */
521 	*attr = (FG_RED|BG_BLACK) << 8;
522 	*c = '?';
523 }
524 #endif /* TEKEN_UTF8 */
525 
526 static void
527 scteken_putchar(void *arg, const teken_pos_t *tp, teken_char_t c,
528     const teken_attr_t *a)
529 {
530 	scr_stat *scp = arg;
531 	u_char *map;
532 	u_char ch;
533 	vm_offset_t p;
534 	int cursor, attr;
535 
536 	/*
537 	 * No support for printing right hand sides for CJK fullwidth
538 	 * characters. Simply print a space and assume that the left
539 	 * hand side describes the entire character.
540 	 */
541 	attr = scteken_te_to_sc_attr(a) << 8;
542 	if (a->ta_format & TF_CJK_RIGHT)
543 		c = ' ';
544 #ifdef TEKEN_UTF8
545 	scteken_get_cp437(&c, &attr);
546 #endif /* TEKEN_UTF8 */
547 	ch = c;
548 
549 	map = scp->sc->scr_map;
550 
551 	cursor = tp->tp_row * scp->xsize + tp->tp_col;
552 	p = sc_vtb_pointer(&scp->vtb, cursor);
553 	sc_vtb_putchar(&scp->vtb, p, map[ch], attr);
554 
555 	mark_for_update(scp, cursor);
556 	/*
557 	 * XXX: Why do we need this? Only marking `cursor' should be
558 	 * enough. Without this line, we get artifacts.
559 	 */
560 	mark_for_update(scp, imin(cursor + 1, scp->xsize * scp->ysize - 1));
561 }
562 
563 static void
564 scteken_fill(void *arg, const teken_rect_t *r, teken_char_t c,
565     const teken_attr_t *a)
566 {
567 	scr_stat *scp = arg;
568 	u_char *map;
569 	u_char ch;
570 	unsigned int width;
571 	int attr, row;
572 
573 	attr = scteken_te_to_sc_attr(a) << 8;
574 #ifdef TEKEN_UTF8
575 	scteken_get_cp437(&c, &attr);
576 #endif /* TEKEN_UTF8 */
577 	ch = c;
578 
579 	map = scp->sc->scr_map;
580 
581 	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
582 		/* Single contiguous region to fill. */
583 		sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
584 		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize,
585 		    map[ch], attr);
586 	} else {
587 		/* Fill display line by line. */
588 		width = r->tr_end.tp_col - r->tr_begin.tp_col;
589 
590 		for (row = r->tr_begin.tp_row; row < r->tr_end.tp_row; row++) {
591 			sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row *
592 			    scp->xsize + r->tr_begin.tp_col,
593 			    width, map[ch], attr);
594 		}
595 	}
596 
597 	/* Mark begin and end positions to be refreshed. */
598 	mark_for_update(scp,
599 	    r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col);
600 	mark_for_update(scp,
601 	    (r->tr_end.tp_row - 1) * scp->xsize + (r->tr_end.tp_col - 1));
602 	sc_remove_cutmarking(scp);
603 }
604 
605 static void
606 scteken_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p)
607 {
608 	scr_stat *scp = arg;
609 	unsigned int width;
610 	int src, dst, end;
611 
612 #ifndef SC_NO_HISTORY
613 	/*
614 	 * We count a line of input as history if we perform a copy of
615 	 * one whole line upward. In other words: if a line of text gets
616 	 * overwritten by a rectangle that's right below it.
617 	 */
618 	if (scp->history != NULL &&
619 	    r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize &&
620 	    r->tr_begin.tp_row == p->tp_row + 1) {
621 		sc_hist_save_one_line(scp, p->tp_row);
622 	}
623 #endif
624 
625 	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
626 		/* Single contiguous region to copy. */
627 		sc_vtb_move(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
628 		    p->tp_row * scp->xsize,
629 		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize);
630 	} else {
631 		/* Copy line by line. */
632 		width = r->tr_end.tp_col - r->tr_begin.tp_col;
633 
634 		if (p->tp_row < r->tr_begin.tp_row) {
635 			/* Copy from top to bottom. */
636 			src = r->tr_begin.tp_row * scp->xsize +
637 			    r->tr_begin.tp_col;
638 			end = r->tr_end.tp_row * scp->xsize +
639 			    r->tr_end.tp_col;
640 			dst = p->tp_row * scp->xsize + p->tp_col;
641 
642 			while (src < end) {
643 				sc_vtb_move(&scp->vtb, src, dst, width);
644 
645 				src += scp->xsize;
646 				dst += scp->xsize;
647 			}
648 		} else {
649 			/* Copy from bottom to top. */
650 			src = (r->tr_end.tp_row - 1) * scp->xsize +
651 			    r->tr_begin.tp_col;
652 			end = r->tr_begin.tp_row * scp->xsize +
653 			    r->tr_begin.tp_col;
654 			dst = (p->tp_row + r->tr_end.tp_row -
655 			    r->tr_begin.tp_row - 1) * scp->xsize + p->tp_col;
656 
657 			while (src >= end) {
658 				sc_vtb_move(&scp->vtb, src, dst, width);
659 
660 				src -= scp->xsize;
661 				dst -= scp->xsize;
662 			}
663 		}
664 	}
665 
666 	/* Mark begin and end positions to be refreshed. */
667 	mark_for_update(scp,
668 	    p->tp_row * scp->xsize + p->tp_col);
669 	mark_for_update(scp,
670 	    (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) *
671 	    scp->xsize +
672 	    (p->tp_col + r->tr_end.tp_col - r->tr_begin.tp_col - 1));
673 	sc_remove_cutmarking(scp);
674 }
675 
676 static void
677 scteken_param(void *arg, int cmd, unsigned int value)
678 {
679 	static int cattrs[] = {
680 		0,					/* block */
681 		CONS_BLINK_CURSOR,			/* blinking block */
682 		CONS_CHAR_CURSOR,			/* underline */
683 		CONS_CHAR_CURSOR | CONS_BLINK_CURSOR,	/* blinking underline */
684 		CONS_RESET_CURSOR,			/* reset to default */
685 		CONS_HIDDEN_CURSOR,			/* hide cursor */
686 	};
687 	static int tcattrs[] = {
688 		CONS_RESET_CURSOR | CONS_LOCAL_CURSOR,	/* normal */
689 		CONS_HIDDEN_CURSOR | CONS_LOCAL_CURSOR,	/* invisible */
690 	};
691 	scr_stat *scp = arg;
692 	int flags, n, v0, v1, v2;
693 
694 	switch (cmd) {
695 	case TP_SETBORDER:
696 		scp->border = value & 0xff;
697 		if (scp == scp->sc->cur_scp)
698 			sc_set_border(scp, scp->border);
699 		break;
700 	case TP_SETGLOBALCURSOR:
701 		n = value & 0xff;
702 		v0 = (value >> 8) & 0xff;
703 		v1 = (value >> 16) & 0xff;
704 		v2 = (value >> 24) & 0xff;
705 		switch (n) {
706 		case 1:	/* flags only */
707 			if (v0 < sizeof(cattrs) / sizeof(cattrs[0]))
708 				v0 = cattrs[v0];
709 			else /* backward compatibility */
710 				v0 = cattrs[v0 & 0x3];
711 			sc_change_cursor_shape(scp, v0, -1, -1);
712 			break;
713 		case 2:
714 			v2 = 0;
715 			v0 &= 0x1f;	/* backward compatibility */
716 			v1 &= 0x1f;
717 			/* FALL THROUGH */
718 		case 3:	/* base and height */
719 			if (v2 == 0)	/* count from top */
720 				sc_change_cursor_shape(scp, -1,
721 				    scp->font_size - v1 - 1,
722 				    v1 - v0 + 1);
723 			else if (v2 == 1) /* count from bottom */
724 				sc_change_cursor_shape(scp, -1,
725 				    v0, v1 - v0 + 1);
726 			break;
727 		}
728 		break;
729 	case TP_SETLOCALCURSOR:
730 		if (value < sizeof(tcattrs) / sizeof(tcattrs[0]))
731 			sc_change_cursor_shape(scp, tcattrs[value], -1, -1);
732 		else if (value == 2) {
733 			sc_change_cursor_shape(scp,
734 			    CONS_RESET_CURSOR | CONS_LOCAL_CURSOR, -1, -1);
735 			flags = scp->base_curs_attr.flags ^ CONS_BLINK_CURSOR;
736 			sc_change_cursor_shape(scp,
737 			    flags | CONS_LOCAL_CURSOR, -1, -1);
738 		}
739 		break;
740 	case TP_SHOWCURSOR:
741 		if (value != 0)
742 			flags = scp->base_curs_attr.flags & ~CONS_HIDDEN_CURSOR;
743 		else
744 			flags = scp->base_curs_attr.flags | CONS_HIDDEN_CURSOR;
745 		sc_change_cursor_shape(scp, flags | CONS_LOCAL_CURSOR, -1, -1);
746 		break;
747 	case TP_SWITCHVT:
748 		sc_switch_scr(scp->sc, value);
749 		break;
750 	case TP_SETBELLPD:
751 		scp->bell_pitch = TP_SETBELLPD_PITCH(value);
752 		scp->bell_duration = TP_SETBELLPD_DURATION(value);
753 		break;
754 	case TP_MOUSE:
755 		scp->mouse_level = value;
756 		break;
757 	}
758 }
759 
760 static void
761 scteken_respond(void *arg, const void *buf, size_t len)
762 {
763 	scr_stat *scp = arg;
764 
765 	sc_respond(scp, buf, len, 0);
766 }
767