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