scterm-teken.c revision 188391
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 188391 2009-02-09 15:55:21Z 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
49186681Sed#include <dev/syscons/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{
111186681Sed	teken_stat *ts = scp->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);
128186681Sed
129186681Sed		tp.tp_row = scp->ysize;
130186681Sed		tp.tp_col = scp->xsize;
131186681Sed		teken_set_winsize(&ts->ts_teken, &tp);
132186681Sed
133186681Sed		tp.tp_row = scp->cursor_pos / scp->xsize;
134186681Sed		tp.tp_col = scp->cursor_pos % scp->xsize;
135186681Sed		teken_set_cursor(&ts->ts_teken, &tp);
136186681Sed		break;
137186681Sed	}
138186681Sed
139186681Sed	return (0);
140186681Sed}
141186681Sed
142186681Sedstatic int
143186681Sedscteken_term(scr_stat *scp, void **softc)
144186681Sed{
145186681Sed
146186681Sed	if (*softc == &reserved_teken_stat) {
147186681Sed		*softc = NULL;
148186681Sed		reserved_teken_stat.ts_busy = 0;
149186681Sed	}
150186681Sed	--sc_term_scteken.te_refcount;
151186681Sed
152186681Sed	return (0);
153186681Sed}
154186681Sed
155186681Sedstatic void
156186681Sedscteken_puts(scr_stat *scp, u_char *buf, int len)
157186681Sed{
158186681Sed	teken_stat *ts = scp->ts;
159186681Sed
160186681Sed	scp->sc->write_in_progress++;
161186681Sed	teken_input(&ts->ts_teken, buf, len);
162186681Sed	scp->sc->write_in_progress--;
163186681Sed}
164186681Sed
165186681Sedstatic int
166186681Sedscteken_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
167186681Sed	     struct thread *td)
168186681Sed{
169188391Sed	teken_stat *ts = scp->ts;
170186681Sed	vid_info_t *vi;
171188391Sed	unsigned int attr;
172186681Sed
173186681Sed	switch (cmd) {
174186681Sed	case GIO_ATTR:      	/* get current attributes */
175188391Sed		*(int*)data =
176188391Sed		    scteken_attr(teken_get_curattr(&ts->ts_teken));
177186681Sed		return (0);
178186681Sed	case CONS_GETINFO:  	/* get current (virtual) console info */
179186681Sed		vi = (vid_info_t *)data;
180186681Sed		if (vi->size != sizeof(struct vid_info))
181186681Sed			return EINVAL;
182188391Sed
183188391Sed		attr = scteken_attr(teken_get_defattr(&ts->ts_teken));
184188391Sed		vi->mv_norm.fore = attr & 0x0f;
185188391Sed		vi->mv_norm.back = (attr >> 4) & 0x0f;
186188391Sed		vi->mv_rev.fore = vi->mv_norm.back;
187188391Sed		vi->mv_rev.back = vi->mv_norm.fore;
188186681Sed		/*
189186681Sed		 * The other fields are filled by the upper routine. XXX
190186681Sed		 */
191186681Sed		return (ENOIOCTL);
192186681Sed	}
193186681Sed
194186681Sed	return (ENOIOCTL);
195186681Sed}
196186681Sed
197186681Sedstatic void
198186681Sedscteken_default_attr(scr_stat *scp, int color, int rev_color)
199186681Sed{
200186681Sed	teken_stat *ts = scp->ts;
201186681Sed	teken_attr_t ta;
202186681Sed
203186681Sed	scteken_revattr(color, &ta);
204186681Sed	teken_set_defattr(&ts->ts_teken, &ta);
205186681Sed}
206186681Sed
207186681Sedstatic void
208186681Sedscteken_clear(scr_stat *scp)
209186681Sed{
210186681Sed
211186681Sed	sc_move_cursor(scp, 0, 0);
212186681Sed	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], SC_NORM_ATTR << 8);
213186681Sed	mark_all(scp);
214186681Sed}
215186681Sed
216186681Sedstatic int
217186681Sedscteken_input(scr_stat *scp, int c, struct tty *tp)
218186681Sed{
219186681Sed
220186681Sed	return FALSE;
221186681Sed}
222186681Sed
223186681Sedstatic void
224186681Sedscteken_nop(void)
225186681Sed{
226186681Sed
227186681Sed}
228186681Sed
229186681Sed/*
230186681Sed * libteken routines.
231186681Sed */
232186681Sed
233186681Sedstatic const unsigned char fgcolors_normal[TC_NCOLORS] = {
234186681Sed	FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
235186681Sed	FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
236186681Sed};
237186681Sed
238186681Sedstatic const unsigned char fgcolors_bold[TC_NCOLORS] = {
239186681Sed	FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
240186681Sed	FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE,
241186681Sed};
242186681Sed
243186681Sedstatic const unsigned char bgcolors[TC_NCOLORS] = {
244186681Sed	BG_BLACK,     BG_RED,          BG_GREEN,      BG_BROWN,
245186681Sed	BG_BLUE,      BG_MAGENTA,      BG_CYAN,       BG_LIGHTGREY,
246186681Sed};
247186681Sed
248186681Sedstatic void
249186681Sedscteken_revattr(unsigned char color, teken_attr_t *a)
250186681Sed{
251186681Sed	teken_color_t fg, bg;
252186681Sed
253186681Sed	/*
254186681Sed	 * XXX: Reverse conversion of syscons to teken attributes. Not
255186681Sed	 * realiable. Maybe we should turn it into a 1:1 mapping one of
256186681Sed	 * these days?
257186681Sed	 */
258186681Sed
259186681Sed	a->ta_format = 0;
260186681Sed	a->ta_fgcolor = TC_WHITE;
261186681Sed	a->ta_bgcolor = TC_BLACK;
262186681Sed
263186681Sed#ifdef FG_BLINK
264186681Sed	if (color & FG_BLINK) {
265186681Sed		a->ta_format |= TF_BLINK;
266186681Sed		color &= ~FG_BLINK;
267186681Sed	}
268186681Sed#endif /* FG_BLINK */
269186681Sed
270186681Sed	for (fg = 0; fg < TC_NCOLORS; fg++) {
271186681Sed		for (bg = 0; bg < TC_NCOLORS; bg++) {
272186681Sed			if ((fgcolors_normal[fg] | bgcolors[bg]) == color) {
273186681Sed				a->ta_fgcolor = fg;
274186681Sed				a->ta_bgcolor = bg;
275186681Sed				return;
276186681Sed			}
277186681Sed
278186681Sed			if ((fgcolors_bold[fg] | bgcolors[bg]) == color) {
279186681Sed				a->ta_fgcolor = fg;
280186681Sed				a->ta_bgcolor = bg;
281186681Sed				a->ta_format |= TF_BOLD;
282186681Sed				return;
283186681Sed			}
284186681Sed		}
285186681Sed	}
286186681Sed}
287186681Sed
288188391Sedstatic unsigned int
289186681Sedscteken_attr(const teken_attr_t *a)
290186681Sed{
291186681Sed	unsigned int attr = 0;
292186681Sed
293186681Sed	if (a->ta_format & TF_BOLD)
294186681Sed		attr |= fgcolors_bold[a->ta_fgcolor];
295186681Sed	else
296186681Sed		attr |= fgcolors_normal[a->ta_fgcolor];
297186681Sed	attr |= bgcolors[a->ta_bgcolor];
298186681Sed
299186681Sed#ifdef FG_UNDERLINE
300186681Sed	if (a->ta_format & TF_UNDERLINE)
301186681Sed		attr |= FG_UNDERLINE;
302186681Sed#endif /* FG_UNDERLINE */
303186681Sed#ifdef FG_BLINK
304186681Sed	if (a->ta_format & TF_BLINK)
305186681Sed		attr |= FG_BLINK;
306186681Sed#endif /* FG_BLINK */
307186681Sed
308188391Sed	return (attr);
309186681Sed}
310186681Sed
311186681Sedstatic void
312186681Sedscteken_bell(void *arg)
313186681Sed{
314186681Sed	scr_stat *scp = arg;
315186681Sed
316186681Sed	sc_bell(scp, scp->bell_pitch, scp->bell_duration);
317186681Sed}
318186681Sed
319186681Sedstatic void
320186681Sedscteken_cursor(void *arg, const teken_pos_t *p)
321186681Sed{
322186681Sed	scr_stat *scp = arg;
323186681Sed
324186681Sed	sc_move_cursor(scp, p->tp_col, p->tp_row);
325186681Sed}
326186681Sed
327186681Sedstatic void
328186681Sedscteken_putchar(void *arg, const teken_pos_t *tp, teken_char_t c,
329186681Sed    const teken_attr_t *a)
330186681Sed{
331186681Sed	scr_stat *scp = arg;
332186681Sed	u_char *map;
333186681Sed	u_char ch;
334186681Sed	vm_offset_t p;
335186681Sed	int cursor, attr;
336186681Sed
337186681Sed#ifdef TEKEN_UTF8
338186681Sed	if (c >= 0x80) {
339186681Sed		/* XXX: Don't display UTF-8 yet. */
340186681Sed		attr = (FG_YELLOW|BG_RED) << 8;
341186681Sed		ch = '?';
342186681Sed	} else
343186681Sed#endif /* TEKEN_UTF8 */
344186681Sed	{
345188391Sed		attr = scteken_attr(a) << 8;
346186681Sed		ch = c;
347186681Sed	}
348186681Sed
349186681Sed	map = scp->sc->scr_map;
350186681Sed
351186681Sed	cursor = tp->tp_row * scp->xsize + tp->tp_col;
352186681Sed	p = sc_vtb_pointer(&scp->vtb, cursor);
353186681Sed	sc_vtb_putchar(&scp->vtb, p, map[ch], attr);
354186681Sed
355186681Sed	mark_for_update(scp, cursor);
356186681Sed	/*
357186681Sed	 * XXX: Why do we need this? Only marking `cursor' should be
358186681Sed	 * enough. Without this line, we get artifacts.
359186681Sed	 */
360186681Sed	mark_for_update(scp, imin(cursor + 1, scp->xsize * scp->ysize - 1));
361186681Sed}
362186681Sed
363186681Sedstatic void
364186681Sedscteken_fill(void *arg, const teken_rect_t *r, teken_char_t c,
365186681Sed    const teken_attr_t *a)
366186681Sed{
367186681Sed	scr_stat *scp = arg;
368186681Sed	u_char *map;
369186681Sed	u_char ch;
370186681Sed	unsigned int width;
371186681Sed	int attr, row;
372186681Sed
373186681Sed#ifdef TEKEN_UTF8
374186681Sed	if (c >= 0x80) {
375186681Sed		/* XXX: Don't display UTF-8 yet. */
376186681Sed		attr = (FG_YELLOW|BG_RED) << 8;
377186681Sed		ch = '?';
378186681Sed	} else
379186681Sed#endif /* TEKEN_UTF8 */
380186681Sed	{
381188391Sed		attr = scteken_attr(a) << 8;
382186681Sed		ch = c;
383186681Sed	}
384186681Sed
385186681Sed	map = scp->sc->scr_map;
386186681Sed
387186681Sed	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
388186681Sed		/* Single contiguous region to fill. */
389186681Sed		sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
390186681Sed		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize,
391186681Sed		    map[ch], attr);
392186681Sed	} else {
393186681Sed		/* Fill display line by line. */
394186681Sed		width = r->tr_end.tp_col - r->tr_begin.tp_col;
395186681Sed
396186681Sed		for (row = r->tr_begin.tp_row; row < r->tr_end.tp_row; row++) {
397186681Sed			sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row *
398186681Sed			    scp->xsize + r->tr_begin.tp_col,
399186681Sed			    width, map[ch], attr);
400186681Sed		}
401186681Sed	}
402186681Sed
403186681Sed	/* Mark begin and end positions to be refreshed. */
404186681Sed	mark_for_update(scp,
405186681Sed	    r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col);
406186681Sed	mark_for_update(scp,
407186681Sed	    (r->tr_end.tp_row - 1) * scp->xsize + (r->tr_end.tp_col - 1));
408186681Sed	sc_remove_cutmarking(scp);
409186681Sed}
410186681Sed
411186681Sedstatic void
412186681Sedscteken_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p)
413186681Sed{
414186681Sed	scr_stat *scp = arg;
415186681Sed	unsigned int width;
416186681Sed	int src, dst, end;
417186681Sed
418186681Sed#ifndef SC_NO_HISTORY
419186681Sed	/*
420186681Sed	 * We count a line of input as history if we perform a copy of
421186681Sed	 * one whole line upward. In other words: if a line of text gets
422186681Sed	 * overwritten by a rectangle that's right below it.
423186681Sed	 */
424186681Sed	if (scp->history != NULL &&
425186681Sed	    r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize &&
426186681Sed	    r->tr_begin.tp_row == p->tp_row + 1) {
427186681Sed		sc_hist_save_one_line(scp, p->tp_row);
428186681Sed	}
429186681Sed#endif
430186681Sed
431186681Sed	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
432186681Sed		/* Single contiguous region to copy. */
433186681Sed		sc_vtb_move(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
434186681Sed		    p->tp_row * scp->xsize,
435186681Sed		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize);
436186681Sed	} else {
437186681Sed		/* Copy line by line. */
438186681Sed		width = r->tr_end.tp_col - r->tr_begin.tp_col;
439186681Sed
440186681Sed		if (p->tp_row < r->tr_begin.tp_row) {
441186681Sed			/* Copy from top to bottom. */
442186681Sed			src = r->tr_begin.tp_row * scp->xsize +
443186681Sed			    r->tr_begin.tp_col;
444186681Sed			end = r->tr_end.tp_row * scp->xsize +
445186681Sed			    r->tr_end.tp_col;
446186681Sed			dst = p->tp_row * scp->xsize + p->tp_col;
447186681Sed
448186681Sed			while (src < end) {
449186681Sed				sc_vtb_move(&scp->vtb, src, dst, width);
450186681Sed
451186681Sed				src += scp->xsize;
452186681Sed				dst += scp->xsize;
453186681Sed			}
454186681Sed		} else {
455186681Sed			/* Copy from bottom to top. */
456186681Sed			src = (r->tr_end.tp_row - 1) * scp->xsize +
457186681Sed			    r->tr_begin.tp_col;
458186681Sed			end = r->tr_begin.tp_row * scp->xsize +
459186681Sed			    r->tr_begin.tp_col;
460186681Sed			dst = (p->tp_row + r->tr_end.tp_row -
461186681Sed			    r->tr_begin.tp_row - 1) * scp->xsize + p->tp_col;
462186681Sed
463186681Sed			while (src >= end) {
464186681Sed				sc_vtb_move(&scp->vtb, src, dst, width);
465186681Sed
466186681Sed				src -= scp->xsize;
467186681Sed				dst -= scp->xsize;
468186681Sed			}
469186681Sed		}
470186681Sed	}
471186681Sed
472186681Sed	/* Mark begin and end positions to be refreshed. */
473186681Sed	mark_for_update(scp,
474186681Sed	    p->tp_row * scp->xsize + p->tp_col);
475186681Sed	mark_for_update(scp,
476186681Sed	    (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) *
477186681Sed	    scp->xsize +
478186681Sed	    (p->tp_col + r->tr_end.tp_col - r->tr_begin.tp_col - 1));
479186681Sed	sc_remove_cutmarking(scp);
480186681Sed}
481186681Sed
482186681Sedstatic void
483186681Sedscteken_param(void *arg, int cmd, int value)
484186681Sed{
485186681Sed	scr_stat *scp = arg;
486186681Sed
487186681Sed	switch (cmd) {
488186681Sed	case TP_SHOWCURSOR:
489186681Sed		if (value) {
490186681Sed			sc_change_cursor_shape(scp,
491186681Sed			    CONS_RESET_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
492186681Sed		} else {
493186681Sed			sc_change_cursor_shape(scp,
494186681Sed			    CONS_HIDDEN_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
495186681Sed		}
496186681Sed		break;
497186681Sed	case TP_SWITCHVT:
498186681Sed		sc_switch_scr(scp->sc, value);
499186681Sed		break;
500186681Sed	}
501186681Sed}
502186681Sed
503186681Sedstatic void
504186681Sedscteken_respond(void *arg, const void *buf, size_t len)
505186681Sed{
506186681Sed	scr_stat *scp = arg;
507186681Sed
508186681Sed	sc_respond(scp, buf, len);
509186681Sed}
510