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$");
32186681Sed
33186681Sed#include "opt_syscons.h"
34197118Sed#include "opt_teken.h"
35186681Sed
36186681Sed#include <sys/param.h>
37186681Sed#include <sys/systm.h>
38186681Sed#include <sys/kernel.h>
39186681Sed#include <sys/module.h>
40186681Sed#include <sys/consio.h>
41199171Sed#include <sys/kbio.h>
42186681Sed
43239696Sgonzo#if defined(__arm__) || defined(__mips__) || \
44239696Sgonzo	defined(__powerpc__) || defined(__sparc64__)
45186681Sed#include <machine/sc_machdep.h>
46186681Sed#else
47186681Sed#include <machine/pc/display.h>
48186681Sed#endif
49186681Sed
50186681Sed#include <dev/syscons/syscons.h>
51186681Sed
52196775Sed#include <teken/teken.h>
53186681Sed
54186681Sedstatic void scteken_revattr(unsigned char, teken_attr_t *);
55188391Sedstatic unsigned int scteken_attr(const teken_attr_t *);
56186681Sed
57199171Sedstatic sc_term_init_t		scteken_init;
58199171Sedstatic sc_term_term_t		scteken_term;
59199171Sedstatic sc_term_puts_t		scteken_puts;
60199171Sedstatic sc_term_ioctl_t		scteken_ioctl;
61199171Sedstatic sc_term_default_attr_t	scteken_default_attr;
62199171Sedstatic sc_term_clear_t		scteken_clear;
63199171Sedstatic sc_term_input_t		scteken_input;
64199171Sedstatic sc_term_fkeystr_t	scteken_fkeystr;
65199171Sedstatic void			scteken_nop(void);
66186681Sed
67186681Sedtypedef struct {
68186681Sed	teken_t		ts_teken;
69186681Sed	int		ts_busy;
70186681Sed} teken_stat;
71186681Sed
72186681Sedstatic teken_stat	reserved_teken_stat;
73186681Sed
74186681Sedstatic sc_term_sw_t sc_term_scteken = {
75186681Sed	{ NULL, NULL },
76186681Sed	"scteken",			/* emulator name */
77186681Sed	"teken terminal",		/* description */
78186681Sed	"*",				/* matching renderer */
79186681Sed	sizeof(teken_stat),		/* softc size */
80186681Sed	0,
81186681Sed	scteken_init,
82186681Sed	scteken_term,
83186681Sed	scteken_puts,
84186681Sed	scteken_ioctl,
85186681Sed	(sc_term_reset_t *)scteken_nop,
86186681Sed	scteken_default_attr,
87186681Sed	scteken_clear,
88186681Sed	(sc_term_notify_t *)scteken_nop,
89186681Sed	scteken_input,
90199171Sed	scteken_fkeystr,
91186681Sed};
92186681Sed
93186681SedSCTERM_MODULE(scteken, sc_term_scteken);
94186681Sed
95186681Sedstatic tf_bell_t	scteken_bell;
96186681Sedstatic tf_cursor_t	scteken_cursor;
97186681Sedstatic tf_putchar_t	scteken_putchar;
98186681Sedstatic tf_fill_t	scteken_fill;
99186681Sedstatic tf_copy_t	scteken_copy;
100186681Sedstatic tf_param_t	scteken_param;
101186681Sedstatic tf_respond_t	scteken_respond;
102186681Sed
103186681Sedstatic const teken_funcs_t scteken_funcs = {
104186681Sed	.tf_bell	= scteken_bell,
105186681Sed	.tf_cursor	= scteken_cursor,
106186681Sed	.tf_putchar	= scteken_putchar,
107186681Sed	.tf_fill	= scteken_fill,
108186681Sed	.tf_copy	= scteken_copy,
109186681Sed	.tf_param	= scteken_param,
110186681Sed	.tf_respond	= scteken_respond,
111186681Sed};
112186681Sed
113186681Sedstatic int
114186681Sedscteken_init(scr_stat *scp, void **softc, int code)
115186681Sed{
116189064Sed	teken_stat *ts;
117186681Sed	teken_pos_t tp;
118186681Sed
119186681Sed	if (*softc == NULL) {
120186681Sed		if (reserved_teken_stat.ts_busy)
121186681Sed			return (EINVAL);
122186681Sed		*softc = &reserved_teken_stat;
123186681Sed	}
124186681Sed	ts = *softc;
125186681Sed
126186681Sed	switch (code) {
127186681Sed	case SC_TE_COLD_INIT:
128186681Sed		++sc_term_scteken.te_refcount;
129186681Sed		ts->ts_busy = 1;
130186681Sed		/* FALLTHROUGH */
131186681Sed	case SC_TE_WARM_INIT:
132186681Sed		teken_init(&ts->ts_teken, &scteken_funcs, scp);
133197115Sed#ifndef TEKEN_UTF8
134197115Sed		teken_set_8bit(&ts->ts_teken);
135197115Sed#endif /* !TEKEN_UTF8 */
136199243Sed#ifdef TEKEN_CONS25
137197118Sed		teken_set_cons25(&ts->ts_teken);
138199243Sed#endif /* TEKEN_CONS25 */
139186681Sed
140186681Sed		tp.tp_row = scp->ysize;
141186681Sed		tp.tp_col = scp->xsize;
142186681Sed		teken_set_winsize(&ts->ts_teken, &tp);
143186681Sed
144197174Sed		if (scp->cursor_pos < scp->ysize * scp->xsize) {
145197174Sed			/* Valid old cursor position. */
146197174Sed			tp.tp_row = scp->cursor_pos / scp->xsize;
147197174Sed			tp.tp_col = scp->cursor_pos % scp->xsize;
148197174Sed			teken_set_cursor(&ts->ts_teken, &tp);
149197174Sed		}
150186681Sed		break;
151186681Sed	}
152186681Sed
153186681Sed	return (0);
154186681Sed}
155186681Sed
156186681Sedstatic int
157186681Sedscteken_term(scr_stat *scp, void **softc)
158186681Sed{
159186681Sed
160186681Sed	if (*softc == &reserved_teken_stat) {
161186681Sed		*softc = NULL;
162186681Sed		reserved_teken_stat.ts_busy = 0;
163186681Sed	}
164186681Sed	--sc_term_scteken.te_refcount;
165186681Sed
166186681Sed	return (0);
167186681Sed}
168186681Sed
169186681Sedstatic void
170189617Sedscteken_puts(scr_stat *scp, u_char *buf, int len, int kernel)
171186681Sed{
172186681Sed	teken_stat *ts = scp->ts;
173189617Sed	teken_attr_t backup, kattr;
174186681Sed
175186681Sed	scp->sc->write_in_progress++;
176189617Sed	if (kernel) {
177189617Sed		/* Use special colors for kernel messages. */
178189617Sed		backup = *teken_get_curattr(&ts->ts_teken);
179189617Sed		scteken_revattr(SC_KERNEL_CONS_ATTR, &kattr);
180189617Sed		teken_set_curattr(&ts->ts_teken, &kattr);
181189617Sed		teken_input(&ts->ts_teken, buf, len);
182189617Sed		teken_set_curattr(&ts->ts_teken, &backup);
183189617Sed	} else {
184189617Sed		/* Print user messages with regular colors. */
185189617Sed		teken_input(&ts->ts_teken, buf, len);
186189617Sed	}
187186681Sed	scp->sc->write_in_progress--;
188186681Sed}
189186681Sed
190186681Sedstatic int
191186681Sedscteken_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
192186681Sed	     struct thread *td)
193186681Sed{
194188391Sed	teken_stat *ts = scp->ts;
195186681Sed	vid_info_t *vi;
196188391Sed	unsigned int attr;
197186681Sed
198186681Sed	switch (cmd) {
199186681Sed	case GIO_ATTR:      	/* get current attributes */
200188391Sed		*(int*)data =
201188391Sed		    scteken_attr(teken_get_curattr(&ts->ts_teken));
202186681Sed		return (0);
203186681Sed	case CONS_GETINFO:  	/* get current (virtual) console info */
204186681Sed		vi = (vid_info_t *)data;
205186681Sed		if (vi->size != sizeof(struct vid_info))
206186681Sed			return EINVAL;
207188391Sed
208188391Sed		attr = scteken_attr(teken_get_defattr(&ts->ts_teken));
209188391Sed		vi->mv_norm.fore = attr & 0x0f;
210188391Sed		vi->mv_norm.back = (attr >> 4) & 0x0f;
211188391Sed		vi->mv_rev.fore = vi->mv_norm.back;
212188391Sed		vi->mv_rev.back = vi->mv_norm.fore;
213186681Sed		/*
214186681Sed		 * The other fields are filled by the upper routine. XXX
215186681Sed		 */
216186681Sed		return (ENOIOCTL);
217186681Sed	}
218186681Sed
219186681Sed	return (ENOIOCTL);
220186681Sed}
221186681Sed
222186681Sedstatic void
223186681Sedscteken_default_attr(scr_stat *scp, int color, int rev_color)
224186681Sed{
225186681Sed	teken_stat *ts = scp->ts;
226186681Sed	teken_attr_t ta;
227186681Sed
228186681Sed	scteken_revattr(color, &ta);
229186681Sed	teken_set_defattr(&ts->ts_teken, &ta);
230186681Sed}
231186681Sed
232186681Sedstatic void
233186681Sedscteken_clear(scr_stat *scp)
234186681Sed{
235186681Sed
236186681Sed	sc_move_cursor(scp, 0, 0);
237186681Sed	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], SC_NORM_ATTR << 8);
238186681Sed	mark_all(scp);
239186681Sed}
240186681Sed
241186681Sedstatic int
242186681Sedscteken_input(scr_stat *scp, int c, struct tty *tp)
243186681Sed{
244186681Sed
245186681Sed	return FALSE;
246186681Sed}
247186681Sed
248199171Sedstatic const char *
249199171Sedscteken_fkeystr(scr_stat *scp, int c)
250199171Sed{
251199171Sed	teken_stat *ts = scp->ts;
252199171Sed	unsigned int k;
253199171Sed
254199171Sed	switch (c) {
255199171Sed	case FKEY | F(1):  case FKEY | F(2):  case FKEY | F(3):
256199171Sed	case FKEY | F(4):  case FKEY | F(5):  case FKEY | F(6):
257199171Sed	case FKEY | F(7):  case FKEY | F(8):  case FKEY | F(9):
258199171Sed	case FKEY | F(10): case FKEY | F(11): case FKEY | F(12):
259199171Sed		k = TKEY_F1 + c - (FKEY | F(1));
260199171Sed		break;
261199171Sed	case FKEY | F(49):
262199171Sed		k = TKEY_HOME;
263199171Sed		break;
264199171Sed	case FKEY | F(50):
265199171Sed		k = TKEY_UP;
266199171Sed		break;
267199171Sed	case FKEY | F(51):
268199171Sed		k = TKEY_PAGE_UP;
269199171Sed		break;
270199171Sed	case FKEY | F(53):
271199171Sed		k = TKEY_LEFT;
272199171Sed		break;
273199171Sed	case FKEY | F(55):
274199171Sed		k = TKEY_RIGHT;
275199171Sed		break;
276199171Sed	case FKEY | F(57):
277199171Sed		k = TKEY_END;
278199171Sed		break;
279199171Sed	case FKEY | F(58):
280199171Sed		k = TKEY_DOWN;
281199171Sed		break;
282199171Sed	case FKEY | F(59):
283199171Sed		k = TKEY_PAGE_DOWN;
284199171Sed		break;
285199171Sed	case FKEY | F(60):
286199171Sed		k = TKEY_INSERT;
287199171Sed		break;
288199171Sed	case FKEY | F(61):
289199171Sed		k = TKEY_DELETE;
290199171Sed		break;
291199171Sed	default:
292199171Sed		return (NULL);
293199171Sed	}
294199171Sed
295199171Sed	return (teken_get_sequence(&ts->ts_teken, k));
296199171Sed}
297199171Sed
298186681Sedstatic void
299186681Sedscteken_nop(void)
300186681Sed{
301186681Sed
302186681Sed}
303186681Sed
304186681Sed/*
305186681Sed * libteken routines.
306186681Sed */
307186681Sed
308186681Sedstatic const unsigned char fgcolors_normal[TC_NCOLORS] = {
309186681Sed	FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
310186681Sed	FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
311186681Sed};
312186681Sed
313186681Sedstatic const unsigned char fgcolors_bold[TC_NCOLORS] = {
314186681Sed	FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
315186681Sed	FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE,
316186681Sed};
317186681Sed
318186681Sedstatic const unsigned char bgcolors[TC_NCOLORS] = {
319186681Sed	BG_BLACK,     BG_RED,          BG_GREEN,      BG_BROWN,
320186681Sed	BG_BLUE,      BG_MAGENTA,      BG_CYAN,       BG_LIGHTGREY,
321186681Sed};
322186681Sed
323186681Sedstatic void
324186681Sedscteken_revattr(unsigned char color, teken_attr_t *a)
325186681Sed{
326186681Sed	teken_color_t fg, bg;
327186681Sed
328186681Sed	/*
329186681Sed	 * XXX: Reverse conversion of syscons to teken attributes. Not
330186681Sed	 * realiable. Maybe we should turn it into a 1:1 mapping one of
331186681Sed	 * these days?
332186681Sed	 */
333186681Sed
334186681Sed	a->ta_format = 0;
335186681Sed	a->ta_fgcolor = TC_WHITE;
336186681Sed	a->ta_bgcolor = TC_BLACK;
337186681Sed
338186681Sed#ifdef FG_BLINK
339186681Sed	if (color & FG_BLINK) {
340186681Sed		a->ta_format |= TF_BLINK;
341186681Sed		color &= ~FG_BLINK;
342186681Sed	}
343186681Sed#endif /* FG_BLINK */
344186681Sed
345186681Sed	for (fg = 0; fg < TC_NCOLORS; fg++) {
346186681Sed		for (bg = 0; bg < TC_NCOLORS; bg++) {
347186681Sed			if ((fgcolors_normal[fg] | bgcolors[bg]) == color) {
348186681Sed				a->ta_fgcolor = fg;
349186681Sed				a->ta_bgcolor = bg;
350186681Sed				return;
351186681Sed			}
352186681Sed
353186681Sed			if ((fgcolors_bold[fg] | bgcolors[bg]) == color) {
354186681Sed				a->ta_fgcolor = fg;
355186681Sed				a->ta_bgcolor = bg;
356186681Sed				a->ta_format |= TF_BOLD;
357186681Sed				return;
358186681Sed			}
359186681Sed		}
360186681Sed	}
361186681Sed}
362186681Sed
363188391Sedstatic unsigned int
364186681Sedscteken_attr(const teken_attr_t *a)
365186681Sed{
366186681Sed	unsigned int attr = 0;
367196786Sed	teken_color_t fg, bg;
368186681Sed
369196786Sed	if (a->ta_format & TF_REVERSE) {
370197522Sed		fg = teken_256to8(a->ta_bgcolor);
371197522Sed		bg = teken_256to8(a->ta_fgcolor);
372196786Sed	} else {
373197522Sed		fg = teken_256to8(a->ta_fgcolor);
374197522Sed		bg = teken_256to8(a->ta_bgcolor);
375196786Sed	}
376186681Sed	if (a->ta_format & TF_BOLD)
377196786Sed		attr |= fgcolors_bold[fg];
378186681Sed	else
379196786Sed		attr |= fgcolors_normal[fg];
380196786Sed	attr |= bgcolors[bg];
381186681Sed
382186681Sed#ifdef FG_UNDERLINE
383186681Sed	if (a->ta_format & TF_UNDERLINE)
384186681Sed		attr |= FG_UNDERLINE;
385186681Sed#endif /* FG_UNDERLINE */
386186681Sed#ifdef FG_BLINK
387186681Sed	if (a->ta_format & TF_BLINK)
388186681Sed		attr |= FG_BLINK;
389186681Sed#endif /* FG_BLINK */
390186681Sed
391188391Sed	return (attr);
392186681Sed}
393186681Sed
394186681Sedstatic void
395186681Sedscteken_bell(void *arg)
396186681Sed{
397186681Sed	scr_stat *scp = arg;
398186681Sed
399186681Sed	sc_bell(scp, scp->bell_pitch, scp->bell_duration);
400186681Sed}
401186681Sed
402186681Sedstatic void
403186681Sedscteken_cursor(void *arg, const teken_pos_t *p)
404186681Sed{
405186681Sed	scr_stat *scp = arg;
406186681Sed
407186681Sed	sc_move_cursor(scp, p->tp_col, p->tp_row);
408186681Sed}
409186681Sed
410194103Sed#ifdef TEKEN_UTF8
411194103Sedstruct unicp437 {
412194103Sed	uint16_t	unicode_base;
413194103Sed	uint8_t		cp437_base;
414194103Sed	uint8_t		length;
415194103Sed};
416194103Sed
417194103Sedstatic const struct unicp437 cp437table[] = {
418194293Sed	{ 0x0020, 0x20, 0x5e }, { 0x00a0, 0x20, 0x00 },
419194293Sed	{ 0x00a1, 0xad, 0x00 }, { 0x00a2, 0x9b, 0x00 },
420194293Sed	{ 0x00a3, 0x9c, 0x00 }, { 0x00a5, 0x9d, 0x00 },
421194293Sed	{ 0x00a7, 0x15, 0x00 }, { 0x00aa, 0xa6, 0x00 },
422194293Sed	{ 0x00ab, 0xae, 0x00 }, { 0x00ac, 0xaa, 0x00 },
423194293Sed	{ 0x00b0, 0xf8, 0x00 }, { 0x00b1, 0xf1, 0x00 },
424194293Sed	{ 0x00b2, 0xfd, 0x00 }, { 0x00b5, 0xe6, 0x00 },
425194293Sed	{ 0x00b6, 0x14, 0x00 }, { 0x00b7, 0xfa, 0x00 },
426194293Sed	{ 0x00ba, 0xa7, 0x00 }, { 0x00bb, 0xaf, 0x00 },
427194293Sed	{ 0x00bc, 0xac, 0x00 }, { 0x00bd, 0xab, 0x00 },
428226888Srmh	{ 0x00bf, 0xa8, 0x00 }, { 0x00c0, 0x41, 0x00 },
429226888Srmh	{ 0x00c1, 0x41, 0x00 }, { 0x00c2, 0x41, 0x00 },
430226888Srmh	{ 0x00c4, 0x8e, 0x01 }, { 0x00c6, 0x92, 0x00 },
431226888Srmh	{ 0x00c7, 0x80, 0x00 }, { 0x00c8, 0x45, 0x00 },
432226888Srmh	{ 0x00c9, 0x90, 0x00 }, { 0x00ca, 0x45, 0x00 },
433226888Srmh	{ 0x00cb, 0x45, 0x00 }, { 0x00cc, 0x49, 0x00 },
434226888Srmh	{ 0x00cd, 0x49, 0x00 }, { 0x00ce, 0x49, 0x00 },
435226888Srmh	{ 0x00cf, 0x49, 0x00 }, { 0x00d1, 0xa5, 0x00 },
436226888Srmh	{ 0x00d2, 0x4f, 0x00 }, { 0x00d3, 0x4f, 0x00 },
437226888Srmh	{ 0x00d4, 0x4f, 0x00 }, { 0x00d6, 0x99, 0x00 },
438226888Srmh	{ 0x00d9, 0x55, 0x00 }, { 0x00da, 0x55, 0x00 },
439226888Srmh	{ 0x00db, 0x55, 0x00 }, { 0x00dc, 0x9a, 0x00 },
440194293Sed	{ 0x00df, 0xe1, 0x00 }, { 0x00e0, 0x85, 0x00 },
441194293Sed	{ 0x00e1, 0xa0, 0x00 }, { 0x00e2, 0x83, 0x00 },
442194293Sed	{ 0x00e4, 0x84, 0x00 }, { 0x00e5, 0x86, 0x00 },
443194293Sed	{ 0x00e6, 0x91, 0x00 }, { 0x00e7, 0x87, 0x00 },
444194293Sed	{ 0x00e8, 0x8a, 0x00 }, { 0x00e9, 0x82, 0x00 },
445194293Sed	{ 0x00ea, 0x88, 0x01 }, { 0x00ec, 0x8d, 0x00 },
446194293Sed	{ 0x00ed, 0xa1, 0x00 }, { 0x00ee, 0x8c, 0x00 },
447194293Sed	{ 0x00ef, 0x8b, 0x00 }, { 0x00f0, 0xeb, 0x00 },
448194103Sed	{ 0x00f1, 0xa4, 0x00 }, { 0x00f2, 0x95, 0x00 },
449194103Sed	{ 0x00f3, 0xa2, 0x00 }, { 0x00f4, 0x93, 0x00 },
450194103Sed	{ 0x00f6, 0x94, 0x00 }, { 0x00f7, 0xf6, 0x00 },
451194293Sed	{ 0x00f8, 0xed, 0x00 }, { 0x00f9, 0x97, 0x00 },
452194293Sed	{ 0x00fa, 0xa3, 0x00 }, { 0x00fb, 0x96, 0x00 },
453194293Sed	{ 0x00fc, 0x81, 0x00 }, { 0x00ff, 0x98, 0x00 },
454226888Srmh	{ 0x013f, 0x4c, 0x00 }, { 0x0140, 0x6c, 0x00 },
455194293Sed	{ 0x0192, 0x9f, 0x00 }, { 0x0393, 0xe2, 0x00 },
456194293Sed	{ 0x0398, 0xe9, 0x00 }, { 0x03a3, 0xe4, 0x00 },
457194293Sed	{ 0x03a6, 0xe8, 0x00 }, { 0x03a9, 0xea, 0x00 },
458194293Sed	{ 0x03b1, 0xe0, 0x01 }, { 0x03b4, 0xeb, 0x00 },
459194293Sed	{ 0x03b5, 0xee, 0x00 }, { 0x03bc, 0xe6, 0x00 },
460194103Sed	{ 0x03c0, 0xe3, 0x00 }, { 0x03c3, 0xe5, 0x00 },
461194103Sed	{ 0x03c4, 0xe7, 0x00 }, { 0x03c6, 0xed, 0x00 },
462194293Sed	{ 0x03d5, 0xed, 0x00 }, { 0x2010, 0x2d, 0x00 },
463194293Sed	{ 0x2014, 0x2d, 0x00 }, { 0x2018, 0x60, 0x00 },
464194293Sed	{ 0x2019, 0x27, 0x00 }, { 0x201c, 0x22, 0x00 },
465194293Sed	{ 0x201d, 0x22, 0x00 }, { 0x2022, 0x07, 0x00 },
466194293Sed	{ 0x203c, 0x13, 0x00 }, { 0x207f, 0xfc, 0x00 },
467194293Sed	{ 0x20a7, 0x9e, 0x00 }, { 0x20ac, 0xee, 0x00 },
468194293Sed	{ 0x2126, 0xea, 0x00 }, { 0x2190, 0x1b, 0x00 },
469194293Sed	{ 0x2191, 0x18, 0x00 }, { 0x2192, 0x1a, 0x00 },
470194293Sed	{ 0x2193, 0x19, 0x00 }, { 0x2194, 0x1d, 0x00 },
471194293Sed	{ 0x2195, 0x12, 0x00 }, { 0x21a8, 0x17, 0x00 },
472194293Sed	{ 0x2202, 0xeb, 0x00 }, { 0x2208, 0xee, 0x00 },
473194293Sed	{ 0x2211, 0xe4, 0x00 }, { 0x2212, 0x2d, 0x00 },
474194103Sed	{ 0x2219, 0xf9, 0x00 }, { 0x221a, 0xfb, 0x00 },
475194103Sed	{ 0x221e, 0xec, 0x00 }, { 0x221f, 0x1c, 0x00 },
476194103Sed	{ 0x2229, 0xef, 0x00 }, { 0x2248, 0xf7, 0x00 },
477194103Sed	{ 0x2261, 0xf0, 0x00 }, { 0x2264, 0xf3, 0x00 },
478194103Sed	{ 0x2265, 0xf2, 0x00 }, { 0x2302, 0x7f, 0x00 },
479194103Sed	{ 0x2310, 0xa9, 0x00 }, { 0x2320, 0xf4, 0x00 },
480194103Sed	{ 0x2321, 0xf5, 0x00 }, { 0x2500, 0xc4, 0x00 },
481194103Sed	{ 0x2502, 0xb3, 0x00 }, { 0x250c, 0xda, 0x00 },
482194103Sed	{ 0x2510, 0xbf, 0x00 }, { 0x2514, 0xc0, 0x00 },
483194103Sed	{ 0x2518, 0xd9, 0x00 }, { 0x251c, 0xc3, 0x00 },
484194103Sed	{ 0x2524, 0xb4, 0x00 }, { 0x252c, 0xc2, 0x00 },
485194103Sed	{ 0x2534, 0xc1, 0x00 }, { 0x253c, 0xc5, 0x00 },
486194103Sed	{ 0x2550, 0xcd, 0x00 }, { 0x2551, 0xba, 0x00 },
487194103Sed	{ 0x2552, 0xd5, 0x00 }, { 0x2553, 0xd6, 0x00 },
488194103Sed	{ 0x2554, 0xc9, 0x00 }, { 0x2555, 0xb8, 0x00 },
489194103Sed	{ 0x2556, 0xb7, 0x00 }, { 0x2557, 0xbb, 0x00 },
490194103Sed	{ 0x2558, 0xd4, 0x00 }, { 0x2559, 0xd3, 0x00 },
491194103Sed	{ 0x255a, 0xc8, 0x00 }, { 0x255b, 0xbe, 0x00 },
492194103Sed	{ 0x255c, 0xbd, 0x00 }, { 0x255d, 0xbc, 0x00 },
493194103Sed	{ 0x255e, 0xc6, 0x01 }, { 0x2560, 0xcc, 0x00 },
494194103Sed	{ 0x2561, 0xb5, 0x00 }, { 0x2562, 0xb6, 0x00 },
495194103Sed	{ 0x2563, 0xb9, 0x00 }, { 0x2564, 0xd1, 0x01 },
496194103Sed	{ 0x2566, 0xcb, 0x00 }, { 0x2567, 0xcf, 0x00 },
497194103Sed	{ 0x2568, 0xd0, 0x00 }, { 0x2569, 0xca, 0x00 },
498194103Sed	{ 0x256a, 0xd8, 0x00 }, { 0x256b, 0xd7, 0x00 },
499194103Sed	{ 0x256c, 0xce, 0x00 }, { 0x2580, 0xdf, 0x00 },
500194103Sed	{ 0x2584, 0xdc, 0x00 }, { 0x2588, 0xdb, 0x00 },
501194103Sed	{ 0x258c, 0xdd, 0x00 }, { 0x2590, 0xde, 0x00 },
502194103Sed	{ 0x2591, 0xb0, 0x02 }, { 0x25a0, 0xfe, 0x00 },
503226888Srmh	{ 0x25ac, 0x16, 0x00 },
504226888Srmh	{ 0x25ae, 0xdb, 0x00 }, { 0x25b2, 0x1e, 0x00 },
505194103Sed	{ 0x25ba, 0x10, 0x00 }, { 0x25bc, 0x1f, 0x00 },
506194184Sed	{ 0x25c4, 0x11, 0x00 }, { 0x25cb, 0x09, 0x00 },
507194184Sed	{ 0x25d8, 0x08, 0x00 }, { 0x25d9, 0x0a, 0x00 },
508194184Sed	{ 0x263a, 0x01, 0x01 }, { 0x263c, 0x0f, 0x00 },
509194184Sed	{ 0x2640, 0x0c, 0x00 }, { 0x2642, 0x0b, 0x00 },
510194184Sed	{ 0x2660, 0x06, 0x00 }, { 0x2663, 0x05, 0x00 },
511194184Sed	{ 0x2665, 0x03, 0x01 }, { 0x266a, 0x0d, 0x01 },
512194103Sed};
513194103Sed
514186681Sedstatic void
515194103Sedscteken_get_cp437(teken_char_t *c, int *attr)
516194103Sed{
517194103Sed	int min, mid, max;
518194103Sed
519194103Sed	min = 0;
520194103Sed	max = (sizeof(cp437table) / sizeof(struct unicp437)) - 1;
521194103Sed
522194103Sed	if (*c < cp437table[0].unicode_base ||
523194103Sed	    *c > cp437table[max].unicode_base + cp437table[max].length)
524194103Sed		goto bad;
525194103Sed
526194103Sed	while (max >= min) {
527194103Sed		mid = (min + max) / 2;
528194103Sed		if (*c < cp437table[mid].unicode_base) {
529194103Sed			max = mid - 1;
530194103Sed		} else if (*c > cp437table[mid].unicode_base +
531194103Sed		    cp437table[mid].length) {
532194103Sed			min = mid + 1;
533194103Sed		} else {
534194103Sed			*c -= cp437table[mid].unicode_base;
535194103Sed			*c += cp437table[mid].cp437_base;
536194103Sed			return;
537194103Sed		}
538194103Sed	}
539194103Sedbad:
540194103Sed	/* Character not present in CP437. */
541194103Sed	*attr = (FG_RED|BG_BLACK) << 8;
542194103Sed	*c = '?';
543194103Sed}
544194103Sed#endif /* TEKEN_UTF8 */
545194103Sed
546194103Sedstatic void
547186681Sedscteken_putchar(void *arg, const teken_pos_t *tp, teken_char_t c,
548186681Sed    const teken_attr_t *a)
549186681Sed{
550186681Sed	scr_stat *scp = arg;
551186681Sed	u_char *map;
552186681Sed	u_char ch;
553186681Sed	vm_offset_t p;
554186681Sed	int cursor, attr;
555186681Sed
556262861Sjhb	/*
557262861Sjhb	 * No support for printing right hand sides for CJK fullwidth
558262861Sjhb	 * characters. Simply print a space and assume that the left
559262861Sjhb	 * hand side describes the entire character.
560262861Sjhb	 */
561194103Sed	attr = scteken_attr(a) << 8;
562262861Sjhb	if (a->ta_format & TF_CJK_RIGHT)
563262861Sjhb		c = ' ';
564186681Sed#ifdef TEKEN_UTF8
565194103Sed	scteken_get_cp437(&c, &attr);
566186681Sed#endif /* TEKEN_UTF8 */
567194103Sed	ch = c;
568186681Sed
569186681Sed	map = scp->sc->scr_map;
570186681Sed
571186681Sed	cursor = tp->tp_row * scp->xsize + tp->tp_col;
572186681Sed	p = sc_vtb_pointer(&scp->vtb, cursor);
573186681Sed	sc_vtb_putchar(&scp->vtb, p, map[ch], attr);
574186681Sed
575186681Sed	mark_for_update(scp, cursor);
576186681Sed	/*
577186681Sed	 * XXX: Why do we need this? Only marking `cursor' should be
578186681Sed	 * enough. Without this line, we get artifacts.
579186681Sed	 */
580186681Sed	mark_for_update(scp, imin(cursor + 1, scp->xsize * scp->ysize - 1));
581186681Sed}
582186681Sed
583186681Sedstatic void
584186681Sedscteken_fill(void *arg, const teken_rect_t *r, teken_char_t c,
585186681Sed    const teken_attr_t *a)
586186681Sed{
587186681Sed	scr_stat *scp = arg;
588186681Sed	u_char *map;
589186681Sed	u_char ch;
590186681Sed	unsigned int width;
591186681Sed	int attr, row;
592186681Sed
593194103Sed	attr = scteken_attr(a) << 8;
594186681Sed#ifdef TEKEN_UTF8
595194103Sed	scteken_get_cp437(&c, &attr);
596186681Sed#endif /* TEKEN_UTF8 */
597194103Sed	ch = c;
598186681Sed
599186681Sed	map = scp->sc->scr_map;
600186681Sed
601186681Sed	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
602186681Sed		/* Single contiguous region to fill. */
603186681Sed		sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
604186681Sed		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize,
605186681Sed		    map[ch], attr);
606186681Sed	} else {
607186681Sed		/* Fill display line by line. */
608186681Sed		width = r->tr_end.tp_col - r->tr_begin.tp_col;
609186681Sed
610186681Sed		for (row = r->tr_begin.tp_row; row < r->tr_end.tp_row; row++) {
611186681Sed			sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row *
612186681Sed			    scp->xsize + r->tr_begin.tp_col,
613186681Sed			    width, map[ch], attr);
614186681Sed		}
615186681Sed	}
616186681Sed
617186681Sed	/* Mark begin and end positions to be refreshed. */
618186681Sed	mark_for_update(scp,
619186681Sed	    r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col);
620186681Sed	mark_for_update(scp,
621186681Sed	    (r->tr_end.tp_row - 1) * scp->xsize + (r->tr_end.tp_col - 1));
622186681Sed	sc_remove_cutmarking(scp);
623186681Sed}
624186681Sed
625186681Sedstatic void
626186681Sedscteken_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p)
627186681Sed{
628186681Sed	scr_stat *scp = arg;
629186681Sed	unsigned int width;
630186681Sed	int src, dst, end;
631186681Sed
632186681Sed#ifndef SC_NO_HISTORY
633186681Sed	/*
634186681Sed	 * We count a line of input as history if we perform a copy of
635186681Sed	 * one whole line upward. In other words: if a line of text gets
636186681Sed	 * overwritten by a rectangle that's right below it.
637186681Sed	 */
638186681Sed	if (scp->history != NULL &&
639186681Sed	    r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize &&
640186681Sed	    r->tr_begin.tp_row == p->tp_row + 1) {
641186681Sed		sc_hist_save_one_line(scp, p->tp_row);
642186681Sed	}
643186681Sed#endif
644186681Sed
645186681Sed	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
646186681Sed		/* Single contiguous region to copy. */
647186681Sed		sc_vtb_move(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
648186681Sed		    p->tp_row * scp->xsize,
649186681Sed		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize);
650186681Sed	} else {
651186681Sed		/* Copy line by line. */
652186681Sed		width = r->tr_end.tp_col - r->tr_begin.tp_col;
653186681Sed
654186681Sed		if (p->tp_row < r->tr_begin.tp_row) {
655186681Sed			/* Copy from top to bottom. */
656186681Sed			src = r->tr_begin.tp_row * scp->xsize +
657186681Sed			    r->tr_begin.tp_col;
658186681Sed			end = r->tr_end.tp_row * scp->xsize +
659186681Sed			    r->tr_end.tp_col;
660186681Sed			dst = p->tp_row * scp->xsize + p->tp_col;
661186681Sed
662186681Sed			while (src < end) {
663186681Sed				sc_vtb_move(&scp->vtb, src, dst, width);
664223575Sed
665186681Sed				src += scp->xsize;
666186681Sed				dst += scp->xsize;
667186681Sed			}
668186681Sed		} else {
669186681Sed			/* Copy from bottom to top. */
670186681Sed			src = (r->tr_end.tp_row - 1) * scp->xsize +
671186681Sed			    r->tr_begin.tp_col;
672186681Sed			end = r->tr_begin.tp_row * scp->xsize +
673186681Sed			    r->tr_begin.tp_col;
674186681Sed			dst = (p->tp_row + r->tr_end.tp_row -
675186681Sed			    r->tr_begin.tp_row - 1) * scp->xsize + p->tp_col;
676186681Sed
677186681Sed			while (src >= end) {
678186681Sed				sc_vtb_move(&scp->vtb, src, dst, width);
679223575Sed
680186681Sed				src -= scp->xsize;
681186681Sed				dst -= scp->xsize;
682186681Sed			}
683186681Sed		}
684186681Sed	}
685186681Sed
686186681Sed	/* Mark begin and end positions to be refreshed. */
687186681Sed	mark_for_update(scp,
688186681Sed	    p->tp_row * scp->xsize + p->tp_col);
689186681Sed	mark_for_update(scp,
690186681Sed	    (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) *
691186681Sed	    scp->xsize +
692186681Sed	    (p->tp_col + r->tr_end.tp_col - r->tr_begin.tp_col - 1));
693186681Sed	sc_remove_cutmarking(scp);
694186681Sed}
695186681Sed
696186681Sedstatic void
697193184Sedscteken_param(void *arg, int cmd, unsigned int value)
698186681Sed{
699186681Sed	scr_stat *scp = arg;
700186681Sed
701186681Sed	switch (cmd) {
702186681Sed	case TP_SHOWCURSOR:
703186681Sed		if (value) {
704186681Sed			sc_change_cursor_shape(scp,
705186681Sed			    CONS_RESET_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
706186681Sed		} else {
707186681Sed			sc_change_cursor_shape(scp,
708186681Sed			    CONS_HIDDEN_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
709186681Sed		}
710186681Sed		break;
711186681Sed	case TP_SWITCHVT:
712186681Sed		sc_switch_scr(scp->sc, value);
713186681Sed		break;
714193184Sed	case TP_SETBELLPD:
715193184Sed		scp->bell_pitch = TP_SETBELLPD_PITCH(value);
716193184Sed		scp->bell_duration = TP_SETBELLPD_DURATION(value);
717193184Sed		break;
718197539Sed	case TP_MOUSE:
719197539Sed		scp->mouse_level = value;
720197539Sed		break;
721186681Sed	}
722186681Sed}
723186681Sed
724186681Sedstatic void
725186681Sedscteken_respond(void *arg, const void *buf, size_t len)
726186681Sed{
727186681Sed	scr_stat *scp = arg;
728186681Sed
729197539Sed	sc_respond(scp, buf, len, 0);
730186681Sed}
731