xref: /freebsd/sys/dev/syscons/scterm-teken.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
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 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/consio.h>
40 
41 #if defined(__sparc64__) || defined(__powerpc__)
42 #include <machine/sc_machdep.h>
43 #else
44 #include <machine/pc/display.h>
45 #endif
46 
47 #include <dev/syscons/syscons.h>
48 
49 #include <dev/syscons/teken/teken.h>
50 
51 static void scteken_revattr(unsigned char, teken_attr_t *);
52 static unsigned int scteken_attr(const teken_attr_t *);
53 
54 static sc_term_init_t	scteken_init;
55 static sc_term_term_t	scteken_term;
56 static sc_term_puts_t	scteken_puts;
57 static sc_term_ioctl_t	scteken_ioctl;
58 static sc_term_default_attr_t scteken_default_attr;
59 static sc_term_clear_t	scteken_clear;
60 static sc_term_input_t	scteken_input;
61 static void		scteken_nop(void);
62 
63 typedef struct {
64 	teken_t		ts_teken;
65 	int		ts_busy;
66 } teken_stat;
67 
68 static teken_stat	reserved_teken_stat;
69 
70 static sc_term_sw_t sc_term_scteken = {
71 	{ NULL, NULL },
72 	"scteken",			/* emulator name */
73 	"teken terminal",		/* description */
74 	"*",				/* matching renderer */
75 	sizeof(teken_stat),		/* softc size */
76 	0,
77 	scteken_init,
78 	scteken_term,
79 	scteken_puts,
80 	scteken_ioctl,
81 	(sc_term_reset_t *)scteken_nop,
82 	scteken_default_attr,
83 	scteken_clear,
84 	(sc_term_notify_t *)scteken_nop,
85 	scteken_input,
86 };
87 
88 SCTERM_MODULE(scteken, sc_term_scteken);
89 
90 static tf_bell_t	scteken_bell;
91 static tf_cursor_t	scteken_cursor;
92 static tf_putchar_t	scteken_putchar;
93 static tf_fill_t	scteken_fill;
94 static tf_copy_t	scteken_copy;
95 static tf_param_t	scteken_param;
96 static tf_respond_t	scteken_respond;
97 
98 static const teken_funcs_t scteken_funcs = {
99 	.tf_bell	= scteken_bell,
100 	.tf_cursor	= scteken_cursor,
101 	.tf_putchar	= scteken_putchar,
102 	.tf_fill	= scteken_fill,
103 	.tf_copy	= scteken_copy,
104 	.tf_param	= scteken_param,
105 	.tf_respond	= scteken_respond,
106 };
107 
108 static int
109 scteken_init(scr_stat *scp, void **softc, int code)
110 {
111 	teken_stat *ts;
112 	teken_pos_t tp;
113 
114 	if (*softc == NULL) {
115 		if (reserved_teken_stat.ts_busy)
116 			return (EINVAL);
117 		*softc = &reserved_teken_stat;
118 	}
119 	ts = *softc;
120 
121 	switch (code) {
122 	case SC_TE_COLD_INIT:
123 		++sc_term_scteken.te_refcount;
124 		ts->ts_busy = 1;
125 		/* FALLTHROUGH */
126 	case SC_TE_WARM_INIT:
127 		teken_init(&ts->ts_teken, &scteken_funcs, scp);
128 
129 		tp.tp_row = scp->ysize;
130 		tp.tp_col = scp->xsize;
131 		teken_set_winsize(&ts->ts_teken, &tp);
132 
133 		tp.tp_row = scp->cursor_pos / scp->xsize;
134 		tp.tp_col = scp->cursor_pos % scp->xsize;
135 		teken_set_cursor(&ts->ts_teken, &tp);
136 		break;
137 	}
138 
139 	return (0);
140 }
141 
142 static int
143 scteken_term(scr_stat *scp, void **softc)
144 {
145 
146 	if (*softc == &reserved_teken_stat) {
147 		*softc = NULL;
148 		reserved_teken_stat.ts_busy = 0;
149 	}
150 	--sc_term_scteken.te_refcount;
151 
152 	return (0);
153 }
154 
155 static void
156 scteken_puts(scr_stat *scp, u_char *buf, int len)
157 {
158 	teken_stat *ts = scp->ts;
159 
160 	scp->sc->write_in_progress++;
161 	teken_input(&ts->ts_teken, buf, len);
162 	scp->sc->write_in_progress--;
163 }
164 
165 static int
166 scteken_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
167 	     struct thread *td)
168 {
169 	teken_stat *ts = scp->ts;
170 	vid_info_t *vi;
171 	unsigned int attr;
172 
173 	switch (cmd) {
174 	case GIO_ATTR:      	/* get current attributes */
175 		*(int*)data =
176 		    scteken_attr(teken_get_curattr(&ts->ts_teken));
177 		return (0);
178 	case CONS_GETINFO:  	/* get current (virtual) console info */
179 		vi = (vid_info_t *)data;
180 		if (vi->size != sizeof(struct vid_info))
181 			return EINVAL;
182 
183 		attr = scteken_attr(teken_get_defattr(&ts->ts_teken));
184 		vi->mv_norm.fore = attr & 0x0f;
185 		vi->mv_norm.back = (attr >> 4) & 0x0f;
186 		vi->mv_rev.fore = vi->mv_norm.back;
187 		vi->mv_rev.back = vi->mv_norm.fore;
188 		/*
189 		 * The other fields are filled by the upper routine. XXX
190 		 */
191 		return (ENOIOCTL);
192 	}
193 
194 	return (ENOIOCTL);
195 }
196 
197 static void
198 scteken_default_attr(scr_stat *scp, int color, int rev_color)
199 {
200 	teken_stat *ts = scp->ts;
201 	teken_attr_t ta;
202 
203 	scteken_revattr(color, &ta);
204 	teken_set_defattr(&ts->ts_teken, &ta);
205 }
206 
207 static void
208 scteken_clear(scr_stat *scp)
209 {
210 
211 	sc_move_cursor(scp, 0, 0);
212 	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], SC_NORM_ATTR << 8);
213 	mark_all(scp);
214 }
215 
216 static int
217 scteken_input(scr_stat *scp, int c, struct tty *tp)
218 {
219 
220 	return FALSE;
221 }
222 
223 static void
224 scteken_nop(void)
225 {
226 
227 }
228 
229 /*
230  * libteken routines.
231  */
232 
233 static const unsigned char fgcolors_normal[TC_NCOLORS] = {
234 	FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
235 	FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
236 };
237 
238 static const unsigned char fgcolors_bold[TC_NCOLORS] = {
239 	FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
240 	FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE,
241 };
242 
243 static const unsigned char bgcolors[TC_NCOLORS] = {
244 	BG_BLACK,     BG_RED,          BG_GREEN,      BG_BROWN,
245 	BG_BLUE,      BG_MAGENTA,      BG_CYAN,       BG_LIGHTGREY,
246 };
247 
248 static void
249 scteken_revattr(unsigned char color, teken_attr_t *a)
250 {
251 	teken_color_t fg, bg;
252 
253 	/*
254 	 * XXX: Reverse conversion of syscons to teken attributes. Not
255 	 * realiable. Maybe we should turn it into a 1:1 mapping one of
256 	 * these days?
257 	 */
258 
259 	a->ta_format = 0;
260 	a->ta_fgcolor = TC_WHITE;
261 	a->ta_bgcolor = TC_BLACK;
262 
263 #ifdef FG_BLINK
264 	if (color & FG_BLINK) {
265 		a->ta_format |= TF_BLINK;
266 		color &= ~FG_BLINK;
267 	}
268 #endif /* FG_BLINK */
269 
270 	for (fg = 0; fg < TC_NCOLORS; fg++) {
271 		for (bg = 0; bg < TC_NCOLORS; bg++) {
272 			if ((fgcolors_normal[fg] | bgcolors[bg]) == color) {
273 				a->ta_fgcolor = fg;
274 				a->ta_bgcolor = bg;
275 				return;
276 			}
277 
278 			if ((fgcolors_bold[fg] | bgcolors[bg]) == color) {
279 				a->ta_fgcolor = fg;
280 				a->ta_bgcolor = bg;
281 				a->ta_format |= TF_BOLD;
282 				return;
283 			}
284 		}
285 	}
286 }
287 
288 static unsigned int
289 scteken_attr(const teken_attr_t *a)
290 {
291 	unsigned int attr = 0;
292 
293 	if (a->ta_format & TF_BOLD)
294 		attr |= fgcolors_bold[a->ta_fgcolor];
295 	else
296 		attr |= fgcolors_normal[a->ta_fgcolor];
297 	attr |= bgcolors[a->ta_bgcolor];
298 
299 #ifdef FG_UNDERLINE
300 	if (a->ta_format & TF_UNDERLINE)
301 		attr |= FG_UNDERLINE;
302 #endif /* FG_UNDERLINE */
303 #ifdef FG_BLINK
304 	if (a->ta_format & TF_BLINK)
305 		attr |= FG_BLINK;
306 #endif /* FG_BLINK */
307 
308 	return (attr);
309 }
310 
311 static void
312 scteken_bell(void *arg)
313 {
314 	scr_stat *scp = arg;
315 
316 	sc_bell(scp, scp->bell_pitch, scp->bell_duration);
317 }
318 
319 static void
320 scteken_cursor(void *arg, const teken_pos_t *p)
321 {
322 	scr_stat *scp = arg;
323 
324 	sc_move_cursor(scp, p->tp_col, p->tp_row);
325 }
326 
327 static void
328 scteken_putchar(void *arg, const teken_pos_t *tp, teken_char_t c,
329     const teken_attr_t *a)
330 {
331 	scr_stat *scp = arg;
332 	u_char *map;
333 	u_char ch;
334 	vm_offset_t p;
335 	int cursor, attr;
336 
337 #ifdef TEKEN_UTF8
338 	if (c >= 0x80) {
339 		/* XXX: Don't display UTF-8 yet. */
340 		attr = (FG_YELLOW|BG_RED) << 8;
341 		ch = '?';
342 	} else
343 #endif /* TEKEN_UTF8 */
344 	{
345 		attr = scteken_attr(a) << 8;
346 		ch = c;
347 	}
348 
349 	map = scp->sc->scr_map;
350 
351 	cursor = tp->tp_row * scp->xsize + tp->tp_col;
352 	p = sc_vtb_pointer(&scp->vtb, cursor);
353 	sc_vtb_putchar(&scp->vtb, p, map[ch], attr);
354 
355 	mark_for_update(scp, cursor);
356 	/*
357 	 * XXX: Why do we need this? Only marking `cursor' should be
358 	 * enough. Without this line, we get artifacts.
359 	 */
360 	mark_for_update(scp, imin(cursor + 1, scp->xsize * scp->ysize - 1));
361 }
362 
363 static void
364 scteken_fill(void *arg, const teken_rect_t *r, teken_char_t c,
365     const teken_attr_t *a)
366 {
367 	scr_stat *scp = arg;
368 	u_char *map;
369 	u_char ch;
370 	unsigned int width;
371 	int attr, row;
372 
373 #ifdef TEKEN_UTF8
374 	if (c >= 0x80) {
375 		/* XXX: Don't display UTF-8 yet. */
376 		attr = (FG_YELLOW|BG_RED) << 8;
377 		ch = '?';
378 	} else
379 #endif /* TEKEN_UTF8 */
380 	{
381 		attr = scteken_attr(a) << 8;
382 		ch = c;
383 	}
384 
385 	map = scp->sc->scr_map;
386 
387 	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
388 		/* Single contiguous region to fill. */
389 		sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
390 		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize,
391 		    map[ch], attr);
392 	} else {
393 		/* Fill display line by line. */
394 		width = r->tr_end.tp_col - r->tr_begin.tp_col;
395 
396 		for (row = r->tr_begin.tp_row; row < r->tr_end.tp_row; row++) {
397 			sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row *
398 			    scp->xsize + r->tr_begin.tp_col,
399 			    width, map[ch], attr);
400 		}
401 	}
402 
403 	/* Mark begin and end positions to be refreshed. */
404 	mark_for_update(scp,
405 	    r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col);
406 	mark_for_update(scp,
407 	    (r->tr_end.tp_row - 1) * scp->xsize + (r->tr_end.tp_col - 1));
408 	sc_remove_cutmarking(scp);
409 }
410 
411 static void
412 scteken_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p)
413 {
414 	scr_stat *scp = arg;
415 	unsigned int width;
416 	int src, dst, end;
417 
418 #ifndef SC_NO_HISTORY
419 	/*
420 	 * We count a line of input as history if we perform a copy of
421 	 * one whole line upward. In other words: if a line of text gets
422 	 * overwritten by a rectangle that's right below it.
423 	 */
424 	if (scp->history != NULL &&
425 	    r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize &&
426 	    r->tr_begin.tp_row == p->tp_row + 1) {
427 		sc_hist_save_one_line(scp, p->tp_row);
428 	}
429 #endif
430 
431 	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
432 		/* Single contiguous region to copy. */
433 		sc_vtb_move(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
434 		    p->tp_row * scp->xsize,
435 		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize);
436 	} else {
437 		/* Copy line by line. */
438 		width = r->tr_end.tp_col - r->tr_begin.tp_col;
439 
440 		if (p->tp_row < r->tr_begin.tp_row) {
441 			/* Copy from top to bottom. */
442 			src = r->tr_begin.tp_row * scp->xsize +
443 			    r->tr_begin.tp_col;
444 			end = r->tr_end.tp_row * scp->xsize +
445 			    r->tr_end.tp_col;
446 			dst = p->tp_row * scp->xsize + p->tp_col;
447 
448 			while (src < end) {
449 				sc_vtb_move(&scp->vtb, src, dst, width);
450 
451 				src += scp->xsize;
452 				dst += scp->xsize;
453 			}
454 		} else {
455 			/* Copy from bottom to top. */
456 			src = (r->tr_end.tp_row - 1) * scp->xsize +
457 			    r->tr_begin.tp_col;
458 			end = r->tr_begin.tp_row * scp->xsize +
459 			    r->tr_begin.tp_col;
460 			dst = (p->tp_row + r->tr_end.tp_row -
461 			    r->tr_begin.tp_row - 1) * scp->xsize + p->tp_col;
462 
463 			while (src >= end) {
464 				sc_vtb_move(&scp->vtb, src, dst, width);
465 
466 				src -= scp->xsize;
467 				dst -= scp->xsize;
468 			}
469 		}
470 	}
471 
472 	/* Mark begin and end positions to be refreshed. */
473 	mark_for_update(scp,
474 	    p->tp_row * scp->xsize + p->tp_col);
475 	mark_for_update(scp,
476 	    (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) *
477 	    scp->xsize +
478 	    (p->tp_col + r->tr_end.tp_col - r->tr_begin.tp_col - 1));
479 	sc_remove_cutmarking(scp);
480 }
481 
482 static void
483 scteken_param(void *arg, int cmd, int value)
484 {
485 	scr_stat *scp = arg;
486 
487 	switch (cmd) {
488 	case TP_SHOWCURSOR:
489 		if (value) {
490 			sc_change_cursor_shape(scp,
491 			    CONS_RESET_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
492 		} else {
493 			sc_change_cursor_shape(scp,
494 			    CONS_HIDDEN_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
495 		}
496 		break;
497 	case TP_SWITCHVT:
498 		sc_switch_scr(scp->sc, value);
499 		break;
500 	}
501 }
502 
503 static void
504 scteken_respond(void *arg, const void *buf, size_t len)
505 {
506 	scr_stat *scp = arg;
507 
508 	sc_respond(scp, buf, len);
509 }
510