summaryrefslogtreecommitdiffstats
path: root/sys/dev/syscons/scterm-teken.c
blob: 38caf9ac4014b6c290b69ce6003318116836d412 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*-
 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
 * All rights reserved.
 *
 * Copyright (c) 2008-2009 Ed Schouten <ed@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer as
 *    the first lines of this file unmodified.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_syscons.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/consio.h>

#if defined(__sparc64__) || defined(__powerpc__)
#include <machine/sc_machdep.h>
#else
#include <machine/pc/display.h>
#endif

#include <dev/syscons/syscons.h>

#include <dev/syscons/teken/teken.h>

static void scteken_revattr(unsigned char, teken_attr_t *);
static unsigned int scteken_attr(const teken_attr_t *);

static sc_term_init_t	scteken_init;
static sc_term_term_t	scteken_term;
static sc_term_puts_t	scteken_puts;
static sc_term_ioctl_t	scteken_ioctl;
static sc_term_default_attr_t scteken_default_attr;
static sc_term_clear_t	scteken_clear;
static sc_term_input_t	scteken_input;
static void		scteken_nop(void);

typedef struct {
	teken_t		ts_teken;
	int		ts_busy;
} teken_stat;

static teken_stat	reserved_teken_stat;

static sc_term_sw_t sc_term_scteken = {
	{ NULL, NULL },
	"scteken",			/* emulator name */
	"teken terminal",		/* description */
	"*",				/* matching renderer */
	sizeof(teken_stat),		/* softc size */
	0,
	scteken_init,
	scteken_term,
	scteken_puts,
	scteken_ioctl,
	(sc_term_reset_t *)scteken_nop,
	scteken_default_attr,
	scteken_clear,
	(sc_term_notify_t *)scteken_nop,
	scteken_input,
};

SCTERM_MODULE(scteken, sc_term_scteken);

static tf_bell_t	scteken_bell;
static tf_cursor_t	scteken_cursor;
static tf_putchar_t	scteken_putchar;
static tf_fill_t	scteken_fill;
static tf_copy_t	scteken_copy;
static tf_param_t	scteken_param;
static tf_respond_t	scteken_respond;

static const teken_funcs_t scteken_funcs = {
	.tf_bell	= scteken_bell,
	.tf_cursor	= scteken_cursor,
	.tf_putchar	= scteken_putchar,
	.tf_fill	= scteken_fill,
	.tf_copy	= scteken_copy,
	.tf_param	= scteken_param,
	.tf_respond	= scteken_respond,
};

static int
scteken_init(scr_stat *scp, void **softc, int code)
{
	teken_stat *ts = scp->ts;
	teken_pos_t tp;

	if (*softc == NULL) {
		if (reserved_teken_stat.ts_busy)
			return (EINVAL);
		*softc = &reserved_teken_stat;
	}
	ts = *softc;

	switch (code) {
	case SC_TE_COLD_INIT:
		++sc_term_scteken.te_refcount;
		ts->ts_busy = 1;
		/* FALLTHROUGH */
	case SC_TE_WARM_INIT:
		teken_init(&ts->ts_teken, &scteken_funcs, scp);

		tp.tp_row = scp->ysize;
		tp.tp_col = scp->xsize;
		teken_set_winsize(&ts->ts_teken, &tp);

		tp.tp_row = scp->cursor_pos / scp->xsize;
		tp.tp_col = scp->cursor_pos % scp->xsize;
		teken_set_cursor(&ts->ts_teken, &tp);
		break;
	}

	return (0);
}

static int
scteken_term(scr_stat *scp, void **softc)
{

	if (*softc == &reserved_teken_stat) {
		*softc = NULL;
		reserved_teken_stat.ts_busy = 0;
	}
	--sc_term_scteken.te_refcount;

	return (0);
}

static void
scteken_puts(scr_stat *scp, u_char *buf, int len)
{
	teken_stat *ts = scp->ts;

	scp->sc->write_in_progress++;
	teken_input(&ts->ts_teken, buf, len);
	scp->sc->write_in_progress--;
}

static int
scteken_ioctl(scr_stat *scp, struct tty *tp, u_long cmd, caddr_t data,
	     struct thread *td)
{
	teken_stat *ts = scp->ts;
	vid_info_t *vi;
	unsigned int attr;

	switch (cmd) {
	case GIO_ATTR:      	/* get current attributes */
		*(int*)data =
		    scteken_attr(teken_get_curattr(&ts->ts_teken));
		return (0);
	case CONS_GETINFO:  	/* get current (virtual) console info */
		vi = (vid_info_t *)data;
		if (vi->size != sizeof(struct vid_info))
			return EINVAL;

		attr = scteken_attr(teken_get_defattr(&ts->ts_teken));
		vi->mv_norm.fore = attr & 0x0f;
		vi->mv_norm.back = (attr >> 4) & 0x0f;
		vi->mv_rev.fore = vi->mv_norm.back;
		vi->mv_rev.back = vi->mv_norm.fore;
		/*
		 * The other fields are filled by the upper routine. XXX
		 */
		return (ENOIOCTL);
	}

	return (ENOIOCTL);
}

static void
scteken_default_attr(scr_stat *scp, int color, int rev_color)
{
	teken_stat *ts = scp->ts;
	teken_attr_t ta;

	scteken_revattr(color, &ta);
	teken_set_defattr(&ts->ts_teken, &ta);
}

static void
scteken_clear(scr_stat *scp)
{

	sc_move_cursor(scp, 0, 0);
	sc_vtb_clear(&scp->vtb, scp->sc->scr_map[0x20], SC_NORM_ATTR << 8);
	mark_all(scp);
}

static int
scteken_input(scr_stat *scp, int c, struct tty *tp)
{

	return FALSE;
}

static void
scteken_nop(void)
{

}

/*
 * libteken routines.
 */

static const unsigned char fgcolors_normal[TC_NCOLORS] = {
	FG_BLACK,     FG_RED,          FG_GREEN,      FG_BROWN,
	FG_BLUE,      FG_MAGENTA,      FG_CYAN,       FG_LIGHTGREY,
};

static const unsigned char fgcolors_bold[TC_NCOLORS] = {
	FG_DARKGREY,  FG_LIGHTRED,     FG_LIGHTGREEN, FG_YELLOW,
	FG_LIGHTBLUE, FG_LIGHTMAGENTA, FG_LIGHTCYAN,  FG_WHITE,
};

static const unsigned char bgcolors[TC_NCOLORS] = {
	BG_BLACK,     BG_RED,          BG_GREEN,      BG_BROWN,
	BG_BLUE,      BG_MAGENTA,      BG_CYAN,       BG_LIGHTGREY,
};

static void
scteken_revattr(unsigned char color, teken_attr_t *a)
{
	teken_color_t fg, bg;

	/*
	 * XXX: Reverse conversion of syscons to teken attributes. Not
	 * realiable. Maybe we should turn it into a 1:1 mapping one of
	 * these days?
	 */

	a->ta_format = 0;
	a->ta_fgcolor = TC_WHITE;
	a->ta_bgcolor = TC_BLACK;

#ifdef FG_BLINK
	if (color & FG_BLINK) {
		a->ta_format |= TF_BLINK;
		color &= ~FG_BLINK;
	}
#endif /* FG_BLINK */

	for (fg = 0; fg < TC_NCOLORS; fg++) {
		for (bg = 0; bg < TC_NCOLORS; bg++) {
			if ((fgcolors_normal[fg] | bgcolors[bg]) == color) {
				a->ta_fgcolor = fg;
				a->ta_bgcolor = bg;
				return;
			}

			if ((fgcolors_bold[fg] | bgcolors[bg]) == color) {
				a->ta_fgcolor = fg;
				a->ta_bgcolor = bg;
				a->ta_format |= TF_BOLD;
				return;
			}
		}
	}
}

static unsigned int
scteken_attr(const teken_attr_t *a)
{
	unsigned int attr = 0;

	if (a->ta_format & TF_BOLD)
		attr |= fgcolors_bold[a->ta_fgcolor];
	else
		attr |= fgcolors_normal[a->ta_fgcolor];
	attr |= bgcolors[a->ta_bgcolor];

#ifdef FG_UNDERLINE
	if (a->ta_format & TF_UNDERLINE)
		attr |= FG_UNDERLINE;
#endif /* FG_UNDERLINE */
#ifdef FG_BLINK
	if (a->ta_format & TF_BLINK)
		attr |= FG_BLINK;
#endif /* FG_BLINK */

	return (attr);
}

static void
scteken_bell(void *arg)
{
	scr_stat *scp = arg;

	sc_bell(scp, scp->bell_pitch, scp->bell_duration);
}

static void
scteken_cursor(void *arg, const teken_pos_t *p)
{
	scr_stat *scp = arg;

	sc_move_cursor(scp, p->tp_col, p->tp_row);
}

static void
scteken_putchar(void *arg, const teken_pos_t *tp, teken_char_t c,
    const teken_attr_t *a)
{
	scr_stat *scp = arg;
	u_char *map;
	u_char ch;
	vm_offset_t p;
	int cursor, attr;

#ifdef TEKEN_UTF8
	if (c >= 0x80) {
		/* XXX: Don't display UTF-8 yet. */
		attr = (FG_YELLOW|BG_RED) << 8;
		ch = '?';
	} else
#endif /* TEKEN_UTF8 */
	{
		attr = scteken_attr(a) << 8;
		ch = c;
	}

	map = scp->sc->scr_map;

	cursor = tp->tp_row * scp->xsize + tp->tp_col;
	p = sc_vtb_pointer(&scp->vtb, cursor);
	sc_vtb_putchar(&scp->vtb, p, map[ch], attr);

	mark_for_update(scp, cursor);
	/*
	 * XXX: Why do we need this? Only marking `cursor' should be
	 * enough. Without this line, we get artifacts.
	 */
	mark_for_update(scp, imin(cursor + 1, scp->xsize * scp->ysize - 1));
}

static void
scteken_fill(void *arg, const teken_rect_t *r, teken_char_t c,
    const teken_attr_t *a)
{
	scr_stat *scp = arg;
	u_char *map;
	u_char ch;
	unsigned int width;
	int attr, row;

#ifdef TEKEN_UTF8
	if (c >= 0x80) {
		/* XXX: Don't display UTF-8 yet. */
		attr = (FG_YELLOW|BG_RED) << 8;
		ch = '?';
	} else
#endif /* TEKEN_UTF8 */
	{
		attr = scteken_attr(a) << 8;
		ch = c;
	}

	map = scp->sc->scr_map;

	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
		/* Single contiguous region to fill. */
		sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize,
		    map[ch], attr);
	} else {
		/* Fill display line by line. */
		width = r->tr_end.tp_col - r->tr_begin.tp_col;

		for (row = r->tr_begin.tp_row; row < r->tr_end.tp_row; row++) {
			sc_vtb_erase(&scp->vtb, r->tr_begin.tp_row *
			    scp->xsize + r->tr_begin.tp_col,
			    width, map[ch], attr);
		}
	}

	/* Mark begin and end positions to be refreshed. */
	mark_for_update(scp,
	    r->tr_begin.tp_row * scp->xsize + r->tr_begin.tp_col);
	mark_for_update(scp,
	    (r->tr_end.tp_row - 1) * scp->xsize + (r->tr_end.tp_col - 1));
	sc_remove_cutmarking(scp);
}

static void
scteken_copy(void *arg, const teken_rect_t *r, const teken_pos_t *p)
{
	scr_stat *scp = arg;
	unsigned int width;
	int src, dst, end;

#ifndef SC_NO_HISTORY
	/*
	 * We count a line of input as history if we perform a copy of
	 * one whole line upward. In other words: if a line of text gets
	 * overwritten by a rectangle that's right below it.
	 */
	if (scp->history != NULL &&
	    r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize &&
	    r->tr_begin.tp_row == p->tp_row + 1) {
		sc_hist_save_one_line(scp, p->tp_row);
	}
#endif

	if (r->tr_begin.tp_col == 0 && r->tr_end.tp_col == scp->xsize) {
		/* Single contiguous region to copy. */
		sc_vtb_move(&scp->vtb, r->tr_begin.tp_row * scp->xsize,
		    p->tp_row * scp->xsize,
		    (r->tr_end.tp_row - r->tr_begin.tp_row) * scp->xsize);
	} else {
		/* Copy line by line. */
		width = r->tr_end.tp_col - r->tr_begin.tp_col;

		if (p->tp_row < r->tr_begin.tp_row) {
			/* Copy from top to bottom. */
			src = r->tr_begin.tp_row * scp->xsize +
			    r->tr_begin.tp_col;
			end = r->tr_end.tp_row * scp->xsize +
			    r->tr_end.tp_col;
			dst = p->tp_row * scp->xsize + p->tp_col;

			while (src < end) {
				sc_vtb_move(&scp->vtb, src, dst, width);
			
				src += scp->xsize;
				dst += scp->xsize;
			}
		} else {
			/* Copy from bottom to top. */
			src = (r->tr_end.tp_row - 1) * scp->xsize +
			    r->tr_begin.tp_col;
			end = r->tr_begin.tp_row * scp->xsize +
			    r->tr_begin.tp_col;
			dst = (p->tp_row + r->tr_end.tp_row -
			    r->tr_begin.tp_row - 1) * scp->xsize + p->tp_col;

			while (src >= end) {
				sc_vtb_move(&scp->vtb, src, dst, width);
			
				src -= scp->xsize;
				dst -= scp->xsize;
			}
		}
	}

	/* Mark begin and end positions to be refreshed. */
	mark_for_update(scp,
	    p->tp_row * scp->xsize + p->tp_col);
	mark_for_update(scp,
	    (p->tp_row + r->tr_end.tp_row - r->tr_begin.tp_row - 1) *
	    scp->xsize +
	    (p->tp_col + r->tr_end.tp_col - r->tr_begin.tp_col - 1));
	sc_remove_cutmarking(scp);
}

static void
scteken_param(void *arg, int cmd, int value)
{
	scr_stat *scp = arg;

	switch (cmd) {
	case TP_SHOWCURSOR:
		if (value) {
			sc_change_cursor_shape(scp,
			    CONS_RESET_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
		} else {
			sc_change_cursor_shape(scp,
			    CONS_HIDDEN_CURSOR|CONS_LOCAL_CURSOR, -1, -1);
		}
		break;
	case TP_SWITCHVT:
		sc_switch_scr(scp->sc, value);
		break;
	}
}

static void
scteken_respond(void *arg, const void *buf, size_t len)
{
	scr_stat *scp = arg;

	sc_respond(scp, buf, len);
}
OpenPOWER on IntegriCloud