scterm-teken.c revision 186681
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 186681 2009-01-01 13:26:53Z 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 *);
52186681Sed
53186681Sedstatic sc_term_init_t	scteken_init;
54186681Sedstatic sc_term_term_t	scteken_term;
55186681Sedstatic sc_term_puts_t	scteken_puts;
56186681Sedstatic sc_term_ioctl_t	scteken_ioctl;
57186681Sedstatic sc_term_default_attr_t scteken_default_attr;
58186681Sedstatic sc_term_clear_t	scteken_clear;
59186681Sedstatic sc_term_input_t	scteken_input;
60186681Sedstatic void		scteken_nop(void);
61186681Sed
62186681Sedtypedef struct {
63186681Sed	teken_t		ts_teken;
64186681Sed	int		ts_busy;
65186681Sed} teken_stat;
66186681Sed
67186681Sedstatic teken_stat	reserved_teken_stat;
68186681Sed
69186681Sedstatic sc_term_sw_t sc_term_scteken = {
70186681Sed	{ NULL, NULL },
71186681Sed	"scteken",			/* emulator name */
72186681Sed	"teken terminal",		/* description */
73186681Sed	"*",				/* matching renderer */
74186681Sed	sizeof(teken_stat),		/* softc size */
75186681Sed	0,
76186681Sed	scteken_init,
77186681Sed	scteken_term,
78186681Sed	scteken_puts,
79186681Sed	scteken_ioctl,
80186681Sed	(sc_term_reset_t *)scteken_nop,
81186681Sed	scteken_default_attr,
82186681Sed	scteken_clear,
83186681Sed	(sc_term_notify_t *)scteken_nop,
84186681Sed	scteken_input,
85186681Sed};
86186681Sed
87186681SedSCTERM_MODULE(scteken, sc_term_scteken);
88186681Sed
89186681Sedstatic tf_bell_t	scteken_bell;
90186681Sedstatic tf_cursor_t	scteken_cursor;
91186681Sedstatic tf_putchar_t	scteken_putchar;
92186681Sedstatic tf_fill_t	scteken_fill;
93186681Sedstatic tf_copy_t	scteken_copy;
94186681Sedstatic tf_param_t	scteken_param;
95186681Sedstatic tf_respond_t	scteken_respond;
96186681Sed
97186681Sedstatic const teken_funcs_t scteken_funcs = {
98186681Sed	.tf_bell	= scteken_bell,
99186681Sed	.tf_cursor	= scteken_cursor,
100186681Sed	.tf_putchar	= scteken_putchar,
101186681Sed	.tf_fill	= scteken_fill,
102186681Sed	.tf_copy	= scteken_copy,
103186681Sed	.tf_param	= scteken_param,
104186681Sed	.tf_respond	= scteken_respond,
105186681Sed};
106186681Sed
107186681Sedstatic int
108186681Sedscteken_init(scr_stat *scp, void **softc, int code)
109186681Sed{
110186681Sed	teken_stat *ts = scp->ts;
111186681Sed	teken_pos_t tp;
112186681Sed
113186681Sed	if (*softc == NULL) {
114186681Sed		if (reserved_teken_stat.ts_busy)
115186681Sed			return (EINVAL);
116186681Sed		*softc = &reserved_teken_stat;
117186681Sed	}
118186681Sed	ts = *softc;
119186681Sed
120186681Sed	switch (code) {
121186681Sed	case SC_TE_COLD_INIT:
122186681Sed		++sc_term_scteken.te_refcount;
123186681Sed		ts->ts_busy = 1;
124186681Sed		/* FALLTHROUGH */
125186681Sed	case SC_TE_WARM_INIT:
126186681Sed		teken_init(&ts->ts_teken, &scteken_funcs, scp);
127186681Sed
128186681Sed		tp.tp_row = scp->ysize;
129186681Sed		tp.tp_col = scp->xsize;
130186681Sed		teken_set_winsize(&ts->ts_teken, &tp);
131186681Sed
132186681Sed		tp.tp_row = scp->cursor_pos / scp->xsize;
133186681Sed		tp.tp_col = scp->cursor_pos % scp->xsize;
134186681Sed		teken_set_cursor(&ts->ts_teken, &tp);
135186681Sed		break;
136186681Sed	}
137186681Sed
138186681Sed	return (0);
139186681Sed}
140186681Sed
141186681Sedstatic int
142186681Sedscteken_term(scr_stat *scp, void **softc)
143186681Sed{
144186681Sed
145186681Sed	if (*softc == &reserved_teken_stat) {
146186681Sed		*softc = NULL;
147186681Sed		reserved_teken_stat.ts_busy = 0;
148186681Sed	}
149186681Sed	--sc_term_scteken.te_refcount;
150186681Sed
151186681Sed	return (0);
152186681Sed}
153186681Sed
154186681Sedstatic void
155186681Sedscteken_puts(scr_stat *scp, u_char *buf, int len)
156186681Sed{
157186681Sed	teken_stat *ts = scp->ts;
158186681Sed
159186681Sed	scp->sc->write_in_progress++;
160186681Sed	teken_input(&ts->ts_teken, buf, len);
161186681Sed	scp->sc->write_in_progress--;
162186681Sed}
163186681Sed
164186681Sedstatic int
165186681Sedscteken_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
166186681Sed	     struct thread *td)
167186681Sed{
168186681Sed	vid_info_t *vi;
169186681Sed
170186681Sed	switch (cmd) {
171186681Sed	case GIO_ATTR:      	/* get current attributes */
172186681Sed		*(int*)data = SC_NORM_ATTR;
173186681Sed		return (0);
174186681Sed	case CONS_GETINFO:  	/* get current (virtual) console info */
175186681Sed		/* XXX: INCORRECT! */
176186681Sed		vi = (vid_info_t *)data;
177186681Sed		if (vi->size != sizeof(struct vid_info))
178186681Sed			return EINVAL;
179186681Sed		vi->mv_norm.fore = SC_NORM_ATTR & 0x0f;
180186681Sed		vi->mv_norm.back = (SC_NORM_ATTR >> 4) & 0x0f;
181186681Sed		vi->mv_rev.fore = SC_NORM_ATTR & 0x0f;
182186681Sed		vi->mv_rev.back = (SC_NORM_ATTR >> 4) & 0x0f;
183186681Sed		/*
184186681Sed		 * The other fields are filled by the upper routine. XXX
185186681Sed		 */
186186681Sed		return (ENOIOCTL);
187186681Sed	}
188186681Sed
189186681Sed	return (ENOIOCTL);
190186681Sed}
191186681Sed
192186681Sedstatic void
193186681Sedscteken_default_attr(scr_stat *scp, int color, int rev_color)
194186681Sed{
195186681Sed	teken_stat *ts = scp->ts;
196186681Sed	teken_attr_t ta;
197186681Sed
198186681Sed	scteken_revattr(color, &ta);
199186681Sed	teken_set_defattr(&ts->ts_teken, &ta);
200186681Sed}
201186681Sed
202186681Sedstatic void
203186681Sedscteken_clear(scr_stat *scp)
204186681Sed{
205186681Sed
206186681Sed	sc_move_cursor(scp, 0, 0);
207186681Sed	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], SC_NORM_ATTR << 8);
208186681Sed	mark_all(scp);
209186681Sed}
210186681Sed
211186681Sedstatic int
212186681Sedscteken_input(scr_stat *scp, int c, struct tty *tp)
213186681Sed{
214186681Sed
215186681Sed	return FALSE;
216186681Sed}
217186681Sed
218186681Sedstatic void
219186681Sedscteken_nop(void)
220186681Sed{
221186681Sed
222186681Sed}
223186681Sed
224186681Sed/*
225186681Sed * libteken routines.
226186681Sed */
227186681Sed
228186681Sedstatic const unsigned char fgcolors_normal[TC_NCOLORS] = {
229186681Sed	FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
230186681Sed	FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
231186681Sed};
232186681Sed
233186681Sedstatic const unsigned char fgcolors_bold[TC_NCOLORS] = {
234186681Sed	FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
235186681Sed	FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE,
236186681Sed};
237186681Sed
238186681Sedstatic const unsigned char bgcolors[TC_NCOLORS] = {
239186681Sed	BG_BLACK,     BG_RED,          BG_GREEN,      BG_BROWN,
240186681Sed	BG_BLUE,      BG_MAGENTA,      BG_CYAN,       BG_LIGHTGREY,
241186681Sed};
242186681Sed
243186681Sedstatic void
244186681Sedscteken_revattr(unsigned char color, teken_attr_t *a)
245186681Sed{
246186681Sed	teken_color_t fg, bg;
247186681Sed
248186681Sed	/*
249186681Sed	 * XXX: Reverse conversion of syscons to teken attributes. Not
250186681Sed	 * realiable. Maybe we should turn it into a 1:1 mapping one of
251186681Sed	 * these days?
252186681Sed	 */
253186681Sed
254186681Sed	a->ta_format = 0;
255186681Sed	a->ta_fgcolor = TC_WHITE;
256186681Sed	a->ta_bgcolor = TC_BLACK;
257186681Sed
258186681Sed#ifdef FG_BLINK
259186681Sed	if (color & FG_BLINK) {
260186681Sed		a->ta_format |= TF_BLINK;
261186681Sed		color &= ~FG_BLINK;
262186681Sed	}
263186681Sed#endif /* FG_BLINK */
264186681Sed
265186681Sed	for (fg = 0; fg < TC_NCOLORS; fg++) {
266186681Sed		for (bg = 0; bg < TC_NCOLORS; bg++) {
267186681Sed			if ((fgcolors_normal[fg] | bgcolors[bg]) == color) {
268186681Sed				a->ta_fgcolor = fg;
269186681Sed				a->ta_bgcolor = bg;
270186681Sed				return;
271186681Sed			}
272186681Sed
273186681Sed			if ((fgcolors_bold[fg] | bgcolors[bg]) == color) {
274186681Sed				a->ta_fgcolor = fg;
275186681Sed				a->ta_bgcolor = bg;
276186681Sed				a->ta_format |= TF_BOLD;
277186681Sed				return;
278186681Sed			}
279186681Sed		}
280186681Sed	}
281186681Sed}
282186681Sed
283186681Sedstatic inline unsigned int
284186681Sedscteken_attr(const teken_attr_t *a)
285186681Sed{
286186681Sed	unsigned int attr = 0;
287186681Sed
288186681Sed	if (a->ta_format & TF_BOLD)
289186681Sed		attr |= fgcolors_bold[a->ta_fgcolor];
290186681Sed	else
291186681Sed		attr |= fgcolors_normal[a->ta_fgcolor];
292186681Sed	attr |= bgcolors[a->ta_bgcolor];
293186681Sed
294186681Sed#ifdef FG_UNDERLINE
295186681Sed	if (a->ta_format & TF_UNDERLINE)
296186681Sed		attr |= FG_UNDERLINE;
297186681Sed#endif /* FG_UNDERLINE */
298186681Sed#ifdef FG_BLINK
299186681Sed	if (a->ta_format & TF_BLINK)
300186681Sed		attr |= FG_BLINK;
301186681Sed#endif /* FG_BLINK */
302186681Sed
303186681Sed	return (attr << 8);
304186681Sed}
305186681Sed
306186681Sedstatic void
307186681Sedscteken_bell(void *arg)
308186681Sed{
309186681Sed	scr_stat *scp = arg;
310186681Sed
311186681Sed	sc_bell(scp, scp->bell_pitch, scp->bell_duration);
312186681Sed}
313186681Sed
314186681Sedstatic void
315186681Sedscteken_cursor(void *arg, const teken_pos_t *p)
316186681Sed{
317186681Sed	scr_stat *scp = arg;
318186681Sed
319186681Sed	sc_move_cursor(scp, p->tp_col, p->tp_row);
320186681Sed}
321186681Sed
322186681Sedstatic void
323186681Sedscteken_putchar(void *arg, const teken_pos_t *tp, teken_char_t c,
324186681Sed    const teken_attr_t *a)
325186681Sed{
326186681Sed	scr_stat *scp = arg;
327186681Sed	u_char *map;
328186681Sed	u_char ch;
329186681Sed	vm_offset_t p;
330186681Sed	int cursor, attr;
331186681Sed
332186681Sed#ifdef TEKEN_UTF8
333186681Sed	if (c >= 0x80) {
334186681Sed		/* XXX: Don't display UTF-8 yet. */
335186681Sed		attr = (FG_YELLOW|BG_RED) << 8;
336186681Sed		ch = '?';
337186681Sed	} else
338186681Sed#endif /* TEKEN_UTF8 */
339186681Sed	{
340186681Sed		attr = scteken_attr(a);
341186681Sed		ch = c;
342186681Sed	}
343186681Sed
344186681Sed	map = scp->sc->scr_map;
345186681Sed
346186681Sed	cursor = tp->tp_row * scp->xsize + tp->tp_col;
347186681Sed	p = sc_vtb_pointer(&scp->vtb, cursor);
348186681Sed	sc_vtb_putchar(&scp->vtb, p, map[ch], attr);
349186681Sed
350186681Sed	mark_for_update(scp, cursor);
351186681Sed	/*
352186681Sed	 * XXX: Why do we need this? Only marking `cursor' should be
353186681Sed	 * enough. Without this line, we get artifacts.
354186681Sed	 */
355186681Sed	mark_for_update(scp, imin(cursor + 1, scp->xsize * scp->ysize - 1));
356186681Sed}
357186681Sed
358186681Sedstatic void
359186681Sedscteken_fill(void *arg, const teken_rect_t *r, teken_char_t c,
360186681Sed    const teken_attr_t *a)
361186681Sed{
362186681Sed	scr_stat *scp = arg;
363186681Sed	u_char *map;
364186681Sed	u_char ch;
365186681Sed	unsigned int width;
366186681Sed	int attr, row;
367186681Sed
368186681Sed#ifdef TEKEN_UTF8
369186681Sed	if (c >= 0x80) {
370186681Sed		/* XXX: Don't display UTF-8 yet. */
371186681Sed		attr = (FG_YELLOW|BG_RED) << 8;
372186681Sed		ch = '?';
373186681Sed	} else
374186681Sed#endif /* TEKEN_UTF8 */
375186681Sed	{
376186681Sed		attr = scteken_attr(a);
377186681Sed		ch = c;
378186681Sed	}
379186681Sed
380186681Sed	map = scp->sc->scr_map;
381186681Sed
382186681Sed	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
383186681Sed		/* Single contiguous region to fill. */
384186681Sed		sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
385186681Sed		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize,
386186681Sed		    map[ch], attr);
387186681Sed	} else {
388186681Sed		/* Fill display line by line. */
389186681Sed		width = r->tr_end.tp_col - r->tr_begin.tp_col;
390186681Sed
391186681Sed		for (row = r->tr_begin.tp_row; row < r->tr_end.tp_row; row++) {
392186681Sed			sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row *
393186681Sed			    scp->xsize + r->tr_begin.tp_col,
394186681Sed			    width, map[ch], attr);
395186681Sed		}
396186681Sed	}
397186681Sed
398186681Sed	/* Mark begin and end positions to be refreshed. */
399186681Sed	mark_for_update(scp,
400186681Sed	    r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col);
401186681Sed	mark_for_update(scp,
402186681Sed	    (r->tr_end.tp_row - 1) * scp->xsize + (r->tr_end.tp_col - 1));
403186681Sed	sc_remove_cutmarking(scp);
404186681Sed}
405186681Sed
406186681Sedstatic void
407186681Sedscteken_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p)
408186681Sed{
409186681Sed	scr_stat *scp = arg;
410186681Sed	unsigned int width;
411186681Sed	int src, dst, end;
412186681Sed
413186681Sed#ifndef SC_NO_HISTORY
414186681Sed	/*
415186681Sed	 * We count a line of input as history if we perform a copy of
416186681Sed	 * one whole line upward. In other words: if a line of text gets
417186681Sed	 * overwritten by a rectangle that's right below it.
418186681Sed	 */
419186681Sed	if (scp->history != NULL &&
420186681Sed	    r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize &&
421186681Sed	    r->tr_begin.tp_row == p->tp_row + 1) {
422186681Sed		sc_hist_save_one_line(scp, p->tp_row);
423186681Sed	}
424186681Sed#endif
425186681Sed
426186681Sed	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
427186681Sed		/* Single contiguous region to copy. */
428186681Sed		sc_vtb_move(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
429186681Sed		    p->tp_row * scp->xsize,
430186681Sed		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize);
431186681Sed	} else {
432186681Sed		/* Copy line by line. */
433186681Sed		width = r->tr_end.tp_col - r->tr_begin.tp_col;
434186681Sed
435186681Sed		if (p->tp_row < r->tr_begin.tp_row) {
436186681Sed			/* Copy from top to bottom. */
437186681Sed			src = r->tr_begin.tp_row * scp->xsize +
438186681Sed			    r->tr_begin.tp_col;
439186681Sed			end = r->tr_end.tp_row * scp->xsize +
440186681Sed			    r->tr_end.tp_col;
441186681Sed			dst = p->tp_row * scp->xsize + p->tp_col;
442186681Sed
443186681Sed			while (src < end) {
444186681Sed				sc_vtb_move(&scp->vtb, src, dst, width);
445186681Sed
446186681Sed				src += scp->xsize;
447186681Sed				dst += scp->xsize;
448186681Sed			}
449186681Sed		} else {
450186681Sed			/* Copy from bottom to top. */
451186681Sed			src = (r->tr_end.tp_row - 1) * scp->xsize +
452186681Sed			    r->tr_begin.tp_col;
453186681Sed			end = r->tr_begin.tp_row * scp->xsize +
454186681Sed			    r->tr_begin.tp_col;
455186681Sed			dst = (p->tp_row + r->tr_end.tp_row -
456186681Sed			    r->tr_begin.tp_row - 1) * scp->xsize + p->tp_col;
457186681Sed
458186681Sed			while (src >= end) {
459186681Sed				sc_vtb_move(&scp->vtb, src, dst, width);
460186681Sed
461186681Sed				src -= scp->xsize;
462186681Sed				dst -= scp->xsize;
463186681Sed			}
464186681Sed		}
465186681Sed	}
466186681Sed
467186681Sed	/* Mark begin and end positions to be refreshed. */
468186681Sed	mark_for_update(scp,
469186681Sed	    p->tp_row * scp->xsize + p->tp_col);
470186681Sed	mark_for_update(scp,
471186681Sed	    (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) *
472186681Sed	    scp->xsize +
473186681Sed	    (p->tp_col + r->tr_end.tp_col - r->tr_begin.tp_col - 1));
474186681Sed	sc_remove_cutmarking(scp);
475186681Sed}
476186681Sed
477186681Sedstatic void
478186681Sedscteken_param(void *arg, int cmd, int value)
479186681Sed{
480186681Sed	scr_stat *scp = arg;
481186681Sed
482186681Sed	switch (cmd) {
483186681Sed	case TP_SHOWCURSOR:
484186681Sed		if (value) {
485186681Sed			sc_change_cursor_shape(scp,
486186681Sed			    CONS_RESET_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
487186681Sed		} else {
488186681Sed			sc_change_cursor_shape(scp,
489186681Sed			    CONS_HIDDEN_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
490186681Sed		}
491186681Sed		break;
492186681Sed	case TP_SWITCHVT:
493186681Sed		sc_switch_scr(scp->sc, value);
494186681Sed		break;
495186681Sed	}
496186681Sed}
497186681Sed
498186681Sedstatic void
499186681Sedscteken_respond(void *arg, const void *buf, size_t len)
500186681Sed{
501186681Sed	scr_stat *scp = arg;
502186681Sed
503186681Sed	sc_respond(scp, buf, len);
504186681Sed}
505