scterm-teken.c revision 197115
1186681Sed/*-
2186681Sed * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
3186681Sed * All rights reserved.
4186681Sed *
5186681Sed * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
6186681Sed * All rights reserved.
7186681Sed *
8186681Sed * Redistribution and use in source and binary forms, with or without
9186681Sed * modification, are permitted provided that the following conditions
10186681Sed * are met:
11186681Sed * 1. Redistributions of source code must retain the above copyright
12186681Sed *    notice, this list of conditions and the following disclaimer as
13186681Sed *    the first lines of this file unmodified.
14186681Sed * 2. Redistributions in binary form must reproduce the above copyright
15186681Sed *    notice, this list of conditions and the following disclaimer in the
16186681Sed *    documentation and/or other materials provided with the distribution.
17186681Sed *
18186681Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19186681Sed * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20186681Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21186681Sed * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22186681Sed * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23186681Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24186681Sed * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25186681Sed * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26186681Sed * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27186681Sed * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28186681Sed */
29186681Sed
30186681Sed#include <sys/cdefs.h>
31186681Sed__FBSDID("$FreeBSD: head/sys/dev/syscons/scterm-teken.c 197115 2009-09-12 10:34:34Z ed $");
32186681Sed
33186681Sed#include "opt_syscons.h"
34186681Sed
35186681Sed#include <sys/param.h>
36186681Sed#include <sys/systm.h>
37186681Sed#include <sys/kernel.h>
38186681Sed#include <sys/module.h>
39186681Sed#include <sys/consio.h>
40186681Sed
41186681Sed#if defined(__sparc64__) || defined(__powerpc__)
42186681Sed#include <machine/sc_machdep.h>
43186681Sed#else
44186681Sed#include <machine/pc/display.h>
45186681Sed#endif
46186681Sed
47186681Sed#include <dev/syscons/syscons.h>
48186681Sed
49196775Sed#include <teken/teken.h>
50186681Sed
51186681Sedstatic void scteken_revattr(unsigned char, teken_attr_t *);
52188391Sedstatic unsigned int scteken_attr(const teken_attr_t *);
53186681Sed
54186681Sedstatic sc_term_init_t	scteken_init;
55186681Sedstatic sc_term_term_t	scteken_term;
56186681Sedstatic sc_term_puts_t	scteken_puts;
57186681Sedstatic sc_term_ioctl_t	scteken_ioctl;
58186681Sedstatic sc_term_default_attr_t scteken_default_attr;
59186681Sedstatic sc_term_clear_t	scteken_clear;
60186681Sedstatic sc_term_input_t	scteken_input;
61186681Sedstatic void		scteken_nop(void);
62186681Sed
63186681Sedtypedef struct {
64186681Sed	teken_t		ts_teken;
65186681Sed	int		ts_busy;
66186681Sed} teken_stat;
67186681Sed
68186681Sedstatic teken_stat	reserved_teken_stat;
69186681Sed
70186681Sedstatic sc_term_sw_t sc_term_scteken = {
71186681Sed	{ NULL, NULL },
72186681Sed	"scteken",			/* emulator name */
73186681Sed	"teken terminal",		/* description */
74186681Sed	"*",				/* matching renderer */
75186681Sed	sizeof(teken_stat),		/* softc size */
76186681Sed	0,
77186681Sed	scteken_init,
78186681Sed	scteken_term,
79186681Sed	scteken_puts,
80186681Sed	scteken_ioctl,
81186681Sed	(sc_term_reset_t *)scteken_nop,
82186681Sed	scteken_default_attr,
83186681Sed	scteken_clear,
84186681Sed	(sc_term_notify_t *)scteken_nop,
85186681Sed	scteken_input,
86186681Sed};
87186681Sed
88186681SedSCTERM_MODULE(scteken, sc_term_scteken);
89186681Sed
90186681Sedstatic tf_bell_t	scteken_bell;
91186681Sedstatic tf_cursor_t	scteken_cursor;
92186681Sedstatic tf_putchar_t	scteken_putchar;
93186681Sedstatic tf_fill_t	scteken_fill;
94186681Sedstatic tf_copy_t	scteken_copy;
95186681Sedstatic tf_param_t	scteken_param;
96186681Sedstatic tf_respond_t	scteken_respond;
97186681Sed
98186681Sedstatic const teken_funcs_t scteken_funcs = {
99186681Sed	.tf_bell	= scteken_bell,
100186681Sed	.tf_cursor	= scteken_cursor,
101186681Sed	.tf_putchar	= scteken_putchar,
102186681Sed	.tf_fill	= scteken_fill,
103186681Sed	.tf_copy	= scteken_copy,
104186681Sed	.tf_param	= scteken_param,
105186681Sed	.tf_respond	= scteken_respond,
106186681Sed};
107186681Sed
108186681Sedstatic int
109186681Sedscteken_init(scr_stat *scp, void **softc, int code)
110186681Sed{
111189064Sed	teken_stat *ts;
112186681Sed	teken_pos_t tp;
113186681Sed
114186681Sed	if (*softc == NULL) {
115186681Sed		if (reserved_teken_stat.ts_busy)
116186681Sed			return (EINVAL);
117186681Sed		*softc = &reserved_teken_stat;
118186681Sed	}
119186681Sed	ts = *softc;
120186681Sed
121186681Sed	switch (code) {
122186681Sed	case SC_TE_COLD_INIT:
123186681Sed		++sc_term_scteken.te_refcount;
124186681Sed		ts->ts_busy = 1;
125186681Sed		/* FALLTHROUGH */
126186681Sed	case SC_TE_WARM_INIT:
127186681Sed		teken_init(&ts->ts_teken, &scteken_funcs, scp);
128197115Sed#ifndef TEKEN_UTF8
129197115Sed		teken_set_8bit(&ts->ts_teken);
130197115Sed#endif /* !TEKEN_UTF8 */
131186681Sed
132186681Sed		tp.tp_row = scp->ysize;
133186681Sed		tp.tp_col = scp->xsize;
134186681Sed		teken_set_winsize(&ts->ts_teken, &tp);
135186681Sed
136186681Sed		tp.tp_row = scp->cursor_pos / scp->xsize;
137186681Sed		tp.tp_col = scp->cursor_pos % scp->xsize;
138186681Sed		teken_set_cursor(&ts->ts_teken, &tp);
139186681Sed		break;
140186681Sed	}
141186681Sed
142186681Sed	return (0);
143186681Sed}
144186681Sed
145186681Sedstatic int
146186681Sedscteken_term(scr_stat *scp, void **softc)
147186681Sed{
148186681Sed
149186681Sed	if (*softc == &reserved_teken_stat) {
150186681Sed		*softc = NULL;
151186681Sed		reserved_teken_stat.ts_busy = 0;
152186681Sed	}
153186681Sed	--sc_term_scteken.te_refcount;
154186681Sed
155186681Sed	return (0);
156186681Sed}
157186681Sed
158186681Sedstatic void
159189617Sedscteken_puts(scr_stat *scp, u_char *buf, int len, int kernel)
160186681Sed{
161186681Sed	teken_stat *ts = scp->ts;
162189617Sed	teken_attr_t backup, kattr;
163186681Sed
164186681Sed	scp->sc->write_in_progress++;
165189617Sed	if (kernel) {
166189617Sed		/* Use special colors for kernel messages. */
167189617Sed		backup = *teken_get_curattr(&ts->ts_teken);
168189617Sed		scteken_revattr(SC_KERNEL_CONS_ATTR, &kattr);
169189617Sed		teken_set_curattr(&ts->ts_teken, &kattr);
170189617Sed		teken_input(&ts->ts_teken, buf, len);
171189617Sed		teken_set_curattr(&ts->ts_teken, &backup);
172189617Sed	} else {
173189617Sed		/* Print user messages with regular colors. */
174189617Sed		teken_input(&ts->ts_teken, buf, len);
175189617Sed	}
176186681Sed	scp->sc->write_in_progress--;
177186681Sed}
178186681Sed
179186681Sedstatic int
180186681Sedscteken_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
181186681Sed	     struct thread *td)
182186681Sed{
183188391Sed	teken_stat *ts = scp->ts;
184186681Sed	vid_info_t *vi;
185188391Sed	unsigned int attr;
186186681Sed
187186681Sed	switch (cmd) {
188186681Sed	case GIO_ATTR:      	/* get current attributes */
189188391Sed		*(int*)data =
190188391Sed		    scteken_attr(teken_get_curattr(&ts->ts_teken));
191186681Sed		return (0);
192186681Sed	case CONS_GETINFO:  	/* get current (virtual) console info */
193186681Sed		vi = (vid_info_t *)data;
194186681Sed		if (vi->size != sizeof(struct vid_info))
195186681Sed			return EINVAL;
196188391Sed
197188391Sed		attr = scteken_attr(teken_get_defattr(&ts->ts_teken));
198188391Sed		vi->mv_norm.fore = attr & 0x0f;
199188391Sed		vi->mv_norm.back = (attr >> 4) & 0x0f;
200188391Sed		vi->mv_rev.fore = vi->mv_norm.back;
201188391Sed		vi->mv_rev.back = vi->mv_norm.fore;
202186681Sed		/*
203186681Sed		 * The other fields are filled by the upper routine. XXX
204186681Sed		 */
205186681Sed		return (ENOIOCTL);
206186681Sed	}
207186681Sed
208186681Sed	return (ENOIOCTL);
209186681Sed}
210186681Sed
211186681Sedstatic void
212186681Sedscteken_default_attr(scr_stat *scp, int color, int rev_color)
213186681Sed{
214186681Sed	teken_stat *ts = scp->ts;
215186681Sed	teken_attr_t ta;
216186681Sed
217186681Sed	scteken_revattr(color, &ta);
218186681Sed	teken_set_defattr(&ts->ts_teken, &ta);
219186681Sed}
220186681Sed
221186681Sedstatic void
222186681Sedscteken_clear(scr_stat *scp)
223186681Sed{
224186681Sed
225186681Sed	sc_move_cursor(scp, 0, 0);
226186681Sed	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], SC_NORM_ATTR << 8);
227186681Sed	mark_all(scp);
228186681Sed}
229186681Sed
230186681Sedstatic int
231186681Sedscteken_input(scr_stat *scp, int c, struct tty *tp)
232186681Sed{
233186681Sed
234186681Sed	return FALSE;
235186681Sed}
236186681Sed
237186681Sedstatic void
238186681Sedscteken_nop(void)
239186681Sed{
240186681Sed
241186681Sed}
242186681Sed
243186681Sed/*
244186681Sed * libteken routines.
245186681Sed */
246186681Sed
247186681Sedstatic const unsigned char fgcolors_normal[TC_NCOLORS] = {
248186681Sed	FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
249186681Sed	FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
250186681Sed};
251186681Sed
252186681Sedstatic const unsigned char fgcolors_bold[TC_NCOLORS] = {
253186681Sed	FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
254186681Sed	FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE,
255186681Sed};
256186681Sed
257186681Sedstatic const unsigned char bgcolors[TC_NCOLORS] = {
258186681Sed	BG_BLACK,     BG_RED,          BG_GREEN,      BG_BROWN,
259186681Sed	BG_BLUE,      BG_MAGENTA,      BG_CYAN,       BG_LIGHTGREY,
260186681Sed};
261186681Sed
262186681Sedstatic void
263186681Sedscteken_revattr(unsigned char color, teken_attr_t *a)
264186681Sed{
265186681Sed	teken_color_t fg, bg;
266186681Sed
267186681Sed	/*
268186681Sed	 * XXX: Reverse conversion of syscons to teken attributes. Not
269186681Sed	 * realiable. Maybe we should turn it into a 1:1 mapping one of
270186681Sed	 * these days?
271186681Sed	 */
272186681Sed
273186681Sed	a->ta_format = 0;
274186681Sed	a->ta_fgcolor = TC_WHITE;
275186681Sed	a->ta_bgcolor = TC_BLACK;
276186681Sed
277186681Sed#ifdef FG_BLINK
278186681Sed	if (color & FG_BLINK) {
279186681Sed		a->ta_format |= TF_BLINK;
280186681Sed		color &= ~FG_BLINK;
281186681Sed	}
282186681Sed#endif /* FG_BLINK */
283186681Sed
284186681Sed	for (fg = 0; fg < TC_NCOLORS; fg++) {
285186681Sed		for (bg = 0; bg < TC_NCOLORS; bg++) {
286186681Sed			if ((fgcolors_normal[fg] | bgcolors[bg]) == color) {
287186681Sed				a->ta_fgcolor = fg;
288186681Sed				a->ta_bgcolor = bg;
289186681Sed				return;
290186681Sed			}
291186681Sed
292186681Sed			if ((fgcolors_bold[fg] | bgcolors[bg]) == color) {
293186681Sed				a->ta_fgcolor = fg;
294186681Sed				a->ta_bgcolor = bg;
295186681Sed				a->ta_format |= TF_BOLD;
296186681Sed				return;
297186681Sed			}
298186681Sed		}
299186681Sed	}
300186681Sed}
301186681Sed
302188391Sedstatic unsigned int
303186681Sedscteken_attr(const teken_attr_t *a)
304186681Sed{
305186681Sed	unsigned int attr = 0;
306196786Sed	teken_color_t fg, bg;
307186681Sed
308196786Sed	if (a->ta_format & TF_REVERSE) {
309196786Sed		fg = a->ta_bgcolor;
310196786Sed		bg = a->ta_fgcolor;
311196786Sed	} else {
312196786Sed		fg = a->ta_fgcolor;
313196786Sed		bg = a->ta_bgcolor;
314196786Sed	}
315186681Sed	if (a->ta_format & TF_BOLD)
316196786Sed		attr |= fgcolors_bold[fg];
317186681Sed	else
318196786Sed		attr |= fgcolors_normal[fg];
319196786Sed	attr |= bgcolors[bg];
320186681Sed
321186681Sed#ifdef FG_UNDERLINE
322186681Sed	if (a->ta_format & TF_UNDERLINE)
323186681Sed		attr |= FG_UNDERLINE;
324186681Sed#endif /* FG_UNDERLINE */
325186681Sed#ifdef FG_BLINK
326186681Sed	if (a->ta_format & TF_BLINK)
327186681Sed		attr |= FG_BLINK;
328186681Sed#endif /* FG_BLINK */
329186681Sed
330188391Sed	return (attr);
331186681Sed}
332186681Sed
333186681Sedstatic void
334186681Sedscteken_bell(void *arg)
335186681Sed{
336186681Sed	scr_stat *scp = arg;
337186681Sed
338186681Sed	sc_bell(scp, scp->bell_pitch, scp->bell_duration);
339186681Sed}
340186681Sed
341186681Sedstatic void
342186681Sedscteken_cursor(void *arg, const teken_pos_t *p)
343186681Sed{
344186681Sed	scr_stat *scp = arg;
345186681Sed
346186681Sed	sc_move_cursor(scp, p->tp_col, p->tp_row);
347186681Sed}
348186681Sed
349194103Sed#ifdef TEKEN_UTF8
350194103Sedstruct unicp437 {
351194103Sed	uint16_t	unicode_base;
352194103Sed	uint8_t		cp437_base;
353194103Sed	uint8_t		length;
354194103Sed};
355194103Sed
356194103Sedstatic const struct unicp437 cp437table[] = {
357194293Sed	{ 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 },
358194293Sed	{ 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 },
359194293Sed	{ 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
360194293Sed	{ 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 },
361194293Sed	{ 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 },
362194293Sed	{ 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
363194293Sed	{ 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 },
364194293Sed	{ 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 },
365194293Sed	{ 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
366194293Sed	{ 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 },
367194293Sed	{ 0x00bf, 0xa8, 0x00 }, { 0x00c4, 0x8e, 0x01 },
368194293Sed	{ 0x00c6, 0x92, 0x00 }, { 0x00c7, 0x80, 0x00 },
369194293Sed	{ 0x00c9, 0x90, 0x00 }, { 0x00d1, 0xa5, 0x00 },
370194293Sed	{ 0x00d6, 0x99, 0x00 }, { 0x00dc, 0x9a, 0x00 },
371194293Sed	{ 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
372194293Sed	{ 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 },
373194293Sed	{ 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 },
374194293Sed	{ 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
375194293Sed	{ 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 },
376194293Sed	{ 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 },
377194293Sed	{ 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
378194293Sed	{ 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 },
379194103Sed	{ 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 },
380194103Sed	{ 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
381194103Sed	{ 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 },
382194293Sed	{ 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 },
383194293Sed	{ 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
384194293Sed	{ 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 },
385194293Sed	{ 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 },
386194293Sed	{ 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
387194293Sed	{ 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 },
388194293Sed	{ 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 },
389194293Sed	{ 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
390194103Sed	{ 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 },
391194103Sed	{ 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 },
392194293Sed	{ 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
393194293Sed	{ 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 },
394194293Sed	{ 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 },
395194293Sed	{ 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
396194293Sed	{ 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 },
397194293Sed	{ 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 },
398194293Sed	{ 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
399194293Sed	{ 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 },
400194293Sed	{ 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 },
401194293Sed	{ 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
402194293Sed	{ 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 },
403194293Sed	{ 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 },
404194103Sed	{ 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
405194103Sed	{ 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 },
406194103Sed	{ 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 },
407194103Sed	{ 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
408194103Sed	{ 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 },
409194103Sed	{ 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 },
410194103Sed	{ 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
411194103Sed	{ 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 },
412194103Sed	{ 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 },
413194103Sed	{ 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
414194103Sed	{ 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 },
415194103Sed	{ 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 },
416194103Sed	{ 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
417194103Sed	{ 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 },
418194103Sed	{ 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 },
419194103Sed	{ 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
420194103Sed	{ 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 },
421194103Sed	{ 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 },
422194103Sed	{ 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
423194103Sed	{ 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 },
424194103Sed	{ 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 },
425194103Sed	{ 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
426194103Sed	{ 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 },
427194103Sed	{ 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 },
428194103Sed	{ 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
429194103Sed	{ 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 },
430194103Sed	{ 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 },
431194103Sed	{ 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
432194103Sed	{ 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 },
433194103Sed	{ 0x25ac, 0x16, 0x00 }, { 0x25b2, 0x1e, 0x00 },
434194103Sed	{ 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
435194184Sed	{ 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 },
436194184Sed	{ 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 },
437194184Sed	{ 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
438194184Sed	{ 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 },
439194184Sed	{ 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 },
440194184Sed	{ 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x01 },
441194103Sed};
442194103Sed
443186681Sedstatic void
444194103Sedscteken_get_cp437(teken_char_t *c, int *attr)
445194103Sed{
446194103Sed	int min, mid, max;
447194103Sed
448194103Sed	min = 0;
449194103Sed	max = (sizeof(cp437table) / sizeof(struct unicp437)) - 1;
450194103Sed
451194103Sed	if (*c < cp437table[0].unicode_base ||
452194103Sed	    *c > cp437table[max].unicode_base + cp437table[max].length)
453194103Sed		goto bad;
454194103Sed
455194103Sed	while (max >= min) {
456194103Sed		mid = (min + max) / 2;
457194103Sed		if (*c < cp437table[mid].unicode_base) {
458194103Sed			max = mid - 1;
459194103Sed		} else if (*c > cp437table[mid].unicode_base +
460194103Sed		    cp437table[mid].length) {
461194103Sed			min = mid + 1;
462194103Sed		} else {
463194103Sed			*c -= cp437table[mid].unicode_base;
464194103Sed			*c += cp437table[mid].cp437_base;
465194103Sed			return;
466194103Sed		}
467194103Sed	}
468194103Sedbad:
469194103Sed	/* Character not present in CP437. */
470194103Sed	*attr = (FG_RED|BG_BLACK) << 8;
471194103Sed	*c = '?';
472194103Sed}
473194103Sed#endif /* TEKEN_UTF8 */
474194103Sed
475194103Sedstatic void
476186681Sedscteken_putchar(void *arg, const teken_pos_t *tp, teken_char_t c,
477186681Sed    const teken_attr_t *a)
478186681Sed{
479186681Sed	scr_stat *scp = arg;
480186681Sed	u_char *map;
481186681Sed	u_char ch;
482186681Sed	vm_offset_t p;
483186681Sed	int cursor, attr;
484186681Sed
485194103Sed	attr = scteken_attr(a) << 8;
486186681Sed#ifdef TEKEN_UTF8
487194103Sed	scteken_get_cp437(&c, &attr);
488186681Sed#endif /* TEKEN_UTF8 */
489194103Sed	ch = c;
490186681Sed
491186681Sed	map = scp->sc->scr_map;
492186681Sed
493186681Sed	cursor = tp->tp_row * scp->xsize + tp->tp_col;
494186681Sed	p = sc_vtb_pointer(&scp->vtb, cursor);
495186681Sed	sc_vtb_putchar(&scp->vtb, p, map[ch], attr);
496186681Sed
497186681Sed	mark_for_update(scp, cursor);
498186681Sed	/*
499186681Sed	 * XXX: Why do we need this? Only marking `cursor' should be
500186681Sed	 * enough. Without this line, we get artifacts.
501186681Sed	 */
502186681Sed	mark_for_update(scp, imin(cursor + 1, scp->xsize * scp->ysize - 1));
503186681Sed}
504186681Sed
505186681Sedstatic void
506186681Sedscteken_fill(void *arg, const teken_rect_t *r, teken_char_t c,
507186681Sed    const teken_attr_t *a)
508186681Sed{
509186681Sed	scr_stat *scp = arg;
510186681Sed	u_char *map;
511186681Sed	u_char ch;
512186681Sed	unsigned int width;
513186681Sed	int attr, row;
514186681Sed
515194103Sed	attr = scteken_attr(a) << 8;
516186681Sed#ifdef TEKEN_UTF8
517194103Sed	scteken_get_cp437(&c, &attr);
518186681Sed#endif /* TEKEN_UTF8 */
519194103Sed	ch = c;
520186681Sed
521186681Sed	map = scp->sc->scr_map;
522186681Sed
523186681Sed	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
524186681Sed		/* Single contiguous region to fill. */
525186681Sed		sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
526186681Sed		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize,
527186681Sed		    map[ch], attr);
528186681Sed	} else {
529186681Sed		/* Fill display line by line. */
530186681Sed		width = r->tr_end.tp_col - r->tr_begin.tp_col;
531186681Sed
532186681Sed		for (row = r->tr_begin.tp_row; row < r->tr_end.tp_row; row++) {
533186681Sed			sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row *
534186681Sed			    scp->xsize + r->tr_begin.tp_col,
535186681Sed			    width, map[ch], attr);
536186681Sed		}
537186681Sed	}
538186681Sed
539186681Sed	/* Mark begin and end positions to be refreshed. */
540186681Sed	mark_for_update(scp,
541186681Sed	    r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col);
542186681Sed	mark_for_update(scp,
543186681Sed	    (r->tr_end.tp_row - 1) * scp->xsize + (r->tr_end.tp_col - 1));
544186681Sed	sc_remove_cutmarking(scp);
545186681Sed}
546186681Sed
547186681Sedstatic void
548186681Sedscteken_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p)
549186681Sed{
550186681Sed	scr_stat *scp = arg;
551186681Sed	unsigned int width;
552186681Sed	int src, dst, end;
553186681Sed
554186681Sed#ifndef SC_NO_HISTORY
555186681Sed	/*
556186681Sed	 * We count a line of input as history if we perform a copy of
557186681Sed	 * one whole line upward. In other words: if a line of text gets
558186681Sed	 * overwritten by a rectangle that's right below it.
559186681Sed	 */
560186681Sed	if (scp->history != NULL &&
561186681Sed	    r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize &&
562186681Sed	    r->tr_begin.tp_row == p->tp_row + 1) {
563186681Sed		sc_hist_save_one_line(scp, p->tp_row);
564186681Sed	}
565186681Sed#endif
566186681Sed
567186681Sed	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
568186681Sed		/* Single contiguous region to copy. */
569186681Sed		sc_vtb_move(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
570186681Sed		    p->tp_row * scp->xsize,
571186681Sed		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize);
572186681Sed	} else {
573186681Sed		/* Copy line by line. */
574186681Sed		width = r->tr_end.tp_col - r->tr_begin.tp_col;
575186681Sed
576186681Sed		if (p->tp_row < r->tr_begin.tp_row) {
577186681Sed			/* Copy from top to bottom. */
578186681Sed			src = r->tr_begin.tp_row * scp->xsize +
579186681Sed			    r->tr_begin.tp_col;
580186681Sed			end = r->tr_end.tp_row * scp->xsize +
581186681Sed			    r->tr_end.tp_col;
582186681Sed			dst = p->tp_row * scp->xsize + p->tp_col;
583186681Sed
584186681Sed			while (src < end) {
585186681Sed				sc_vtb_move(&scp->vtb, src, dst, width);
586186681Sed
587186681Sed				src += scp->xsize;
588186681Sed				dst += scp->xsize;
589186681Sed			}
590186681Sed		} else {
591186681Sed			/* Copy from bottom to top. */
592186681Sed			src = (r->tr_end.tp_row - 1) * scp->xsize +
593186681Sed			    r->tr_begin.tp_col;
594186681Sed			end = r->tr_begin.tp_row * scp->xsize +
595186681Sed			    r->tr_begin.tp_col;
596186681Sed			dst = (p->tp_row + r->tr_end.tp_row -
597186681Sed			    r->tr_begin.tp_row - 1) * scp->xsize + p->tp_col;
598186681Sed
599186681Sed			while (src >= end) {
600186681Sed				sc_vtb_move(&scp->vtb, src, dst, width);
601186681Sed
602186681Sed				src -= scp->xsize;
603186681Sed				dst -= scp->xsize;
604186681Sed			}
605186681Sed		}
606186681Sed	}
607186681Sed
608186681Sed	/* Mark begin and end positions to be refreshed. */
609186681Sed	mark_for_update(scp,
610186681Sed	    p->tp_row * scp->xsize + p->tp_col);
611186681Sed	mark_for_update(scp,
612186681Sed	    (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) *
613186681Sed	    scp->xsize +
614186681Sed	    (p->tp_col + r->tr_end.tp_col - r->tr_begin.tp_col - 1));
615186681Sed	sc_remove_cutmarking(scp);
616186681Sed}
617186681Sed
618186681Sedstatic void
619193184Sedscteken_param(void *arg, int cmd, unsigned int value)
620186681Sed{
621186681Sed	scr_stat *scp = arg;
622186681Sed
623186681Sed	switch (cmd) {
624186681Sed	case TP_SHOWCURSOR:
625186681Sed		if (value) {
626186681Sed			sc_change_cursor_shape(scp,
627186681Sed			    CONS_RESET_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
628186681Sed		} else {
629186681Sed			sc_change_cursor_shape(scp,
630186681Sed			    CONS_HIDDEN_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
631186681Sed		}
632186681Sed		break;
633186681Sed	case TP_SWITCHVT:
634186681Sed		sc_switch_scr(scp->sc, value);
635186681Sed		break;
636193184Sed	case TP_SETBELLPD:
637193184Sed		scp->bell_pitch = TP_SETBELLPD_PITCH(value);
638193184Sed		scp->bell_duration = TP_SETBELLPD_DURATION(value);
639193184Sed		break;
640186681Sed	}
641186681Sed}
642186681Sed
643186681Sedstatic void
644186681Sedscteken_respond(void *arg, const void *buf, size_t len)
645186681Sed{
646186681Sed	scr_stat *scp = arg;
647186681Sed
648186681Sed	sc_respond(scp, buf, len);
649186681Sed}
650