156043Syokota/*-
256043Syokota * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
356043Syokota * All rights reserved.
456043Syokota *
556043Syokota * Redistribution and use in source and binary forms, with or without
656043Syokota * modification, are permitted provided that the following conditions
756043Syokota * are met:
856043Syokota * 1. Redistributions of source code must retain the above copyright
956043Syokota *    notice, this list of conditions and the following disclaimer as
1056043Syokota *    the first lines of this file unmodified.
1156043Syokota * 2. Redistributions in binary form must reproduce the above copyright
1256043Syokota *    notice, this list of conditions and the following disclaimer in the
1356043Syokota *    documentation and/or other materials provided with the distribution.
1456043Syokota *
1556043Syokota * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
1656043Syokota * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1756043Syokota * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1856043Syokota * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
1956043Syokota * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2056043Syokota * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2156043Syokota * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2256043Syokota * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2356043Syokota * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2456043Syokota * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2556043Syokota *
2656043Syokota * $FreeBSD: releng/10.3/sys/pc98/cbus/sctermvar.h 186681 2009-01-01 13:26:53Z ed $
2756043Syokota */
2856043Syokota
2956043Syokota#ifndef _DEV_SYSCONS_SCTERMVAR_H_
3056043Syokota#define _DEV_SYSCONS_SCTERMVAR_H_
3156043Syokota
3256043Syokota/*
3356043Syokota * building blocks for terminal emulator modules.
3456043Syokota */
3556043Syokota
3656043Syokotastatic __inline void	sc_term_ins_line(scr_stat *scp, int y, int n, int ch,
3756043Syokota					 int attr, int tail);
3856043Syokotastatic __inline void	sc_term_del_line(scr_stat *scp, int y, int n, int ch,
3956043Syokota					 int attr, int tail);
4056043Syokotastatic __inline void	sc_term_ins_char(scr_stat *scp, int n, int ch,
4156043Syokota					 int attr);
4256043Syokotastatic __inline void	sc_term_del_char(scr_stat *scp, int n, int ch,
4356043Syokota					 int attr);
4456043Syokotastatic __inline void	sc_term_col(scr_stat *scp, int n);
4556043Syokotastatic __inline void	sc_term_row(scr_stat *scp, int n);
4656043Syokotastatic __inline void	sc_term_up(scr_stat *scp, int n, int head);
4756043Syokotastatic __inline void	sc_term_down(scr_stat *scp, int n, int tail);
4856043Syokotastatic __inline void	sc_term_left(scr_stat *scp, int n);
4956043Syokotastatic __inline void	sc_term_right(scr_stat *scp, int n);
5056043Syokotastatic __inline void	sc_term_up_scroll(scr_stat *scp, int n, int ch,
5156043Syokota					  int attr, int head, int tail);
5256043Syokotastatic __inline void	sc_term_down_scroll(scr_stat *scp, int n, int ch,
5356043Syokota					    int attr, int head, int tail);
5456043Syokotastatic __inline void	sc_term_clr_eos(scr_stat *scp, int n, int ch, int attr);
5556043Syokotastatic __inline void	sc_term_clr_eol(scr_stat *scp, int n, int ch, int attr);
5656043Syokotastatic __inline void	sc_term_tab(scr_stat *scp, int n);
5756043Syokotastatic __inline void	sc_term_backtab(scr_stat *scp, int n);
5856043Syokotastatic __inline void	sc_term_respond(scr_stat *scp, u_char *s);
5956043Syokotastatic __inline void	sc_term_gen_print(scr_stat *scp, u_char **buf, int *len,
6056043Syokota					  int attr);
6156043Syokotastatic __inline void	sc_term_gen_scroll(scr_stat *scp, int ch, int attr);
6256043Syokota
6356043Syokotastatic __inline void
6456043Syokotasc_term_ins_line(scr_stat *scp, int y, int n, int ch, int attr, int tail)
6556043Syokota{
6656043Syokota	if (tail <= 0)
6756043Syokota		tail = scp->ysize;
6856043Syokota	if (n < 1)
6956043Syokota		n = 1;
7056043Syokota	if (n > tail - y)
7156043Syokota		n = tail - y;
7256043Syokota	sc_vtb_ins(&scp->vtb, y*scp->xsize, n*scp->xsize, ch, attr);
7356043Syokota	mark_for_update(scp, y*scp->xsize);
7456043Syokota	mark_for_update(scp, scp->xsize*tail - 1);
7556043Syokota}
7656043Syokota
7756043Syokotastatic __inline void
7856043Syokotasc_term_del_line(scr_stat *scp, int y, int n, int ch, int attr, int tail)
7956043Syokota{
8056043Syokota	if (tail <= 0)
8156043Syokota		tail = scp->ysize;
8256043Syokota	if (n < 1)
8356043Syokota		n = 1;
8456043Syokota	if (n > tail - y)
8556043Syokota		n = tail - y;
8656043Syokota	sc_vtb_delete(&scp->vtb, y*scp->xsize, n*scp->xsize, ch, attr);
8756043Syokota	mark_for_update(scp, y*scp->xsize);
8856043Syokota	mark_for_update(scp, scp->xsize*tail - 1);
8956043Syokota}
9056043Syokota
9156043Syokotastatic __inline void
9256043Syokotasc_term_ins_char(scr_stat *scp, int n, int ch, int attr)
9356043Syokota{
9456043Syokota	int count;
9556043Syokota
9656043Syokota	if (n < 1)
9756043Syokota		n = 1;
9856043Syokota	if (n > scp->xsize - scp->xpos)
9956043Syokota		n = scp->xsize - scp->xpos;
10056043Syokota	count = scp->xsize - (scp->xpos + n);
10156043Syokota	sc_vtb_move(&scp->vtb, scp->cursor_pos, scp->cursor_pos + n, count);
10256043Syokota	sc_vtb_erase(&scp->vtb, scp->cursor_pos, n, ch, attr);
10356043Syokota	mark_for_update(scp, scp->cursor_pos);
10456043Syokota	mark_for_update(scp, scp->cursor_pos + n + count - 1);
10556043Syokota}
10656043Syokota
10756043Syokotastatic __inline void
10856043Syokotasc_term_del_char(scr_stat *scp, int n, int ch, int attr)
10956043Syokota{
11056043Syokota	int count;
11156043Syokota
11256043Syokota	if (n < 1)
11356043Syokota		n = 1;
11456043Syokota	if (n > scp->xsize - scp->xpos)
11556043Syokota		n = scp->xsize - scp->xpos;
11656043Syokota	count = scp->xsize - (scp->xpos + n);
11756043Syokota	sc_vtb_move(&scp->vtb, scp->cursor_pos + n, scp->cursor_pos, count);
11856043Syokota	sc_vtb_erase(&scp->vtb, scp->cursor_pos + count, n, ch, attr);
11956043Syokota	mark_for_update(scp, scp->cursor_pos);
12056043Syokota	mark_for_update(scp, scp->cursor_pos + n + count - 1);
12156043Syokota}
12256043Syokota
12356043Syokotastatic __inline void
12456043Syokotasc_term_col(scr_stat *scp, int n)
12556043Syokota{
12656043Syokota	if (n < 1)
12756043Syokota		n = 1;
12856043Syokota	sc_move_cursor(scp, n - 1, scp->ypos);
12956043Syokota}
13056043Syokota
13156043Syokotastatic __inline void
13256043Syokotasc_term_row(scr_stat *scp, int n)
13356043Syokota{
13456043Syokota	if (n < 1)
13556043Syokota		n = 1;
13656043Syokota	sc_move_cursor(scp, scp->xpos, n - 1);
13756043Syokota}
13856043Syokota
13956043Syokotastatic __inline void
14056043Syokotasc_term_up(scr_stat *scp, int n, int head)
14156043Syokota{
14256043Syokota	if (n < 1)
14356043Syokota		n = 1;
14456043Syokota	n = imin(n, scp->ypos - head);
14556043Syokota	if (n <= 0)
14656043Syokota		return;
14756043Syokota	sc_move_cursor(scp, scp->xpos, scp->ypos - n);
14856043Syokota}
14956043Syokota
15056043Syokotastatic __inline void
15156043Syokotasc_term_down(scr_stat *scp, int n, int tail)
15256043Syokota{
15356043Syokota	if (tail <= 0)
15456043Syokota		tail = scp->ysize;
15556043Syokota	if (n < 1)
15656043Syokota		n = 1;
15756043Syokota	n = imin(n, tail - scp->ypos - 1);
15856043Syokota	if (n <= 0)
15956043Syokota		return;
16056043Syokota	sc_move_cursor(scp, scp->xpos, scp->ypos + n);
16156043Syokota}
16256043Syokota
16356043Syokotastatic __inline void
16456043Syokotasc_term_left(scr_stat *scp, int n)
16556043Syokota{
16656043Syokota	if (n < 1)
16756043Syokota		n = 1;
16856043Syokota	sc_move_cursor(scp, scp->xpos - n, scp->ypos);
16956043Syokota}
17056043Syokota
17156043Syokotastatic __inline void
17256043Syokotasc_term_right(scr_stat *scp, int n)
17356043Syokota{
17456043Syokota	if (n < 1)
17556043Syokota		n = 1;
17656043Syokota	sc_move_cursor(scp, scp->xpos + n, scp->ypos);
17756043Syokota}
17856043Syokota
17956043Syokotastatic __inline void
18056043Syokotasc_term_up_scroll(scr_stat *scp, int n, int ch, int attr, int head, int tail)
18156043Syokota{
18256043Syokota	if (tail <= 0)
18356043Syokota		tail = scp->ysize;
18456043Syokota	if (n < 1)
18556043Syokota		n = 1;
18656043Syokota	if (n <= scp->ypos - head) {
18756043Syokota		sc_move_cursor(scp, scp->xpos, scp->ypos - n);
18856043Syokota	} else {
18956043Syokota		sc_term_ins_line(scp, head, n - (scp->ypos - head),
19056043Syokota				 ch, attr, tail);
19156043Syokota		sc_move_cursor(scp, scp->xpos, head);
19256043Syokota	}
19356043Syokota}
19456043Syokota
19556043Syokotastatic __inline void
19656043Syokotasc_term_down_scroll(scr_stat *scp, int n, int ch, int attr, int head, int tail)
19756043Syokota{
19856043Syokota	if (tail <= 0)
19956043Syokota		tail = scp->ysize;
20056043Syokota	if (n < 1)
20156043Syokota		n = 1;
20256043Syokota	if (n < tail - scp->ypos) {
20356043Syokota		sc_move_cursor(scp, scp->xpos, scp->ypos + n);
20456043Syokota	} else {
20556043Syokota		sc_term_del_line(scp, head, n - (tail - scp->ypos) + 1,
20656043Syokota				 ch, attr, tail);
20756043Syokota		sc_move_cursor(scp, scp->xpos, tail - 1);
20856043Syokota	}
20956043Syokota}
21056043Syokota
21156043Syokotastatic __inline void
21256043Syokotasc_term_clr_eos(scr_stat *scp, int n, int ch, int attr)
21356043Syokota{
21456043Syokota	switch (n) {
21556043Syokota	case 0: /* clear form cursor to end of display */
21656043Syokota		sc_vtb_erase(&scp->vtb, scp->cursor_pos,
21756043Syokota			     scp->xsize*scp->ysize - scp->cursor_pos,
21856043Syokota			     ch, attr);
21956043Syokota		mark_for_update(scp, scp->cursor_pos);
22056043Syokota		mark_for_update(scp, scp->xsize*scp->ysize - 1);
22156043Syokota		sc_remove_cutmarking(scp);
22256043Syokota		break;
22356043Syokota	case 1: /* clear from beginning of display to cursor */
22480044Syokota		sc_vtb_erase(&scp->vtb, 0, scp->cursor_pos + 1, ch, attr);
22556043Syokota		mark_for_update(scp, 0);
22656043Syokota		mark_for_update(scp, scp->cursor_pos);
22756043Syokota		sc_remove_cutmarking(scp);
22856043Syokota		break;
22956043Syokota	case 2: /* clear entire display */
23056043Syokota		sc_vtb_erase(&scp->vtb, 0, scp->xsize*scp->ysize, ch, attr);
23156043Syokota		mark_for_update(scp, 0);
23256043Syokota		mark_for_update(scp, scp->xsize*scp->ysize - 1);
23356043Syokota		sc_remove_cutmarking(scp);
23456043Syokota		break;
23556043Syokota	}
23656043Syokota}
23756043Syokota
23856043Syokotastatic __inline void
23956043Syokotasc_term_clr_eol(scr_stat *scp, int n, int ch, int attr)
24056043Syokota{
24156043Syokota	switch (n) {
24256043Syokota	case 0: /* clear form cursor to end of line */
24356043Syokota		sc_vtb_erase(&scp->vtb, scp->cursor_pos,
24456043Syokota			     scp->xsize - scp->xpos, ch, attr);
24556043Syokota		mark_for_update(scp, scp->cursor_pos);
24656043Syokota		mark_for_update(scp, scp->cursor_pos +
24756043Syokota				scp->xsize - 1 - scp->xpos);
24856043Syokota		break;
24956043Syokota	case 1: /* clear from beginning of line to cursor */
25056043Syokota		sc_vtb_erase(&scp->vtb, scp->cursor_pos - scp->xpos,
25156043Syokota			     scp->xpos + 1, ch, attr);
25256043Syokota		mark_for_update(scp, scp->ypos*scp->xsize);
25356043Syokota		mark_for_update(scp, scp->cursor_pos);
25456043Syokota		break;
25556043Syokota	case 2: /* clear entire line */
25656043Syokota		sc_vtb_erase(&scp->vtb, scp->cursor_pos - scp->xpos,
25756043Syokota			     scp->xsize, ch, attr);
25856043Syokota		mark_for_update(scp, scp->ypos*scp->xsize);
25956043Syokota		mark_for_update(scp, (scp->ypos + 1)*scp->xsize - 1);
26056043Syokota		break;
26156043Syokota	}
26256043Syokota}
26356043Syokota
26456043Syokotastatic __inline void
26556043Syokotasc_term_tab(scr_stat *scp, int n)
26656043Syokota{
26756043Syokota	int i;
26856043Syokota
26956043Syokota	if (n < 1)
27056043Syokota		n = 1;
27156043Syokota	i = (scp->xpos & ~7) + 8*n;
27261072Sache	if (i >= scp->xsize) {
27361072Sache		if (scp->ypos >= scp->ysize - 1) {
27461072Sache			scp->xpos = 0;
27561072Sache			scp->ypos++;
27661072Sache			scp->cursor_pos = scp->ypos*scp->xsize;
27761072Sache		} else
27861072Sache			sc_move_cursor(scp, 0, scp->ypos + 1);
27961072Sache	} else
28056043Syokota		sc_move_cursor(scp, i, scp->ypos);
28156043Syokota}
28256043Syokota
28356043Syokotastatic __inline void
28456043Syokotasc_term_backtab(scr_stat *scp, int n)
28556043Syokota{
28656043Syokota	int i;
28756043Syokota
28856043Syokota	if (n < 1)
28956043Syokota		n = 1;
29056043Syokota	if ((i = scp->xpos & ~7) == scp->xpos)
29156043Syokota		i -= 8*n;
29256043Syokota	else
29356043Syokota		i -= 8*(n - 1);
29456043Syokota	if (i < 0)
29556043Syokota		i = 0;
29656043Syokota	sc_move_cursor(scp, i, scp->ypos);
29756043Syokota}
29856043Syokota
29956043Syokotastatic __inline void
30056043Syokotasc_term_respond(scr_stat *scp, u_char *s)
30156043Syokota{
30256043Syokota	sc_paste(scp, s, strlen(s));	/* XXX: not correct, don't use rmap */
30356043Syokota}
30456043Syokota
30556043Syokotastatic __inline void
30656043Syokotasc_term_gen_print(scr_stat *scp, u_char **buf, int *len, int attr)
30756043Syokota{
30856043Syokota	vm_offset_t p;
30956043Syokota	u_char *ptr;
31056043Syokota	u_char *map;
31156043Syokota 	int cnt;
31256043Syokota	int l;
31356043Syokota	int i;
31456043Syokota
31556043Syokota	ptr = *buf;
31656043Syokota	l = *len;
31756043Syokota
31856043Syokota	if (PRINTABLE(*ptr)) {
31956043Syokota		p = sc_vtb_pointer(&scp->vtb, scp->cursor_pos);
32056043Syokota		map = scp->sc->scr_map;
32156043Syokota
32256043Syokota		cnt = imin(l, scp->xsize - scp->xpos);
32356043Syokota		i = cnt;
32456043Syokota		do {
325105584Srobert			p = sc_vtb_putchar(&scp->vtb, p, map[*ptr], attr);
32656043Syokota			++ptr;
32756043Syokota			--i;
32856043Syokota		} while ((i > 0) && PRINTABLE(*ptr));
32956043Syokota
33056043Syokota		l -= cnt - i;
33156043Syokota		mark_for_update(scp, scp->cursor_pos);
33256043Syokota		scp->cursor_pos += cnt - i;
33356043Syokota		mark_for_update(scp, scp->cursor_pos - 1);
33456043Syokota		scp->xpos += cnt - i;
33556043Syokota
33656043Syokota		if (scp->xpos >= scp->xsize) {
33756043Syokota			scp->xpos = 0;
33856043Syokota			scp->ypos++;
33956043Syokota			/* we may have to scroll the screen */
34056043Syokota		}
34156043Syokota	} else {
34256043Syokota		switch(*ptr) {
34356043Syokota		case 0x07:
34456043Syokota			sc_bell(scp, scp->bell_pitch, scp->bell_duration);
34556043Syokota			break;
34656043Syokota
34756043Syokota		case 0x08:	/* non-destructive backspace */
34856043Syokota			/* XXX */
34956043Syokota			if (scp->cursor_pos > 0) {
35056043Syokota#if 0
35156043Syokota				mark_for_update(scp, scp->cursor_pos);
35256043Syokota				scp->cursor_pos--;
35356043Syokota				mark_for_update(scp, scp->cursor_pos);
35456043Syokota#else
35556043Syokota				scp->cursor_pos--;
35656043Syokota#endif
35756043Syokota				if (scp->xpos > 0) {
35856043Syokota					scp->xpos--;
35956043Syokota				} else {
36056043Syokota					scp->xpos += scp->xsize - 1;
36156043Syokota					scp->ypos--;
36256043Syokota				}
36356043Syokota			}
36456043Syokota			break;
36556043Syokota
36656043Syokota		case 0x09:	/* non-destructive tab */
36756043Syokota			sc_term_tab(scp, 1);
36856043Syokota			/* we may have to scroll the screen */
36956043Syokota#if 0
37056043Syokota			mark_for_update(scp, scp->cursor_pos);
37156043Syokota			scp->cursor_pos += (8 - scp->xpos % 8u);
37256043Syokota			mark_for_update(scp, scp->cursor_pos);
37356043Syokota			scp->xpos += (8 - scp->xpos % 8u);
37456043Syokota			if (scp->xpos >= scp->xsize) {
37556043Syokota				scp->xpos = 0;
37656043Syokota				scp->ypos++;
37756043Syokota			}
37856043Syokota#endif
37956043Syokota			break;
38056043Syokota
38156043Syokota		case 0x0a:	/* newline, same pos */
38256043Syokota#if 0
38356043Syokota			mark_for_update(scp, scp->cursor_pos);
38456043Syokota			scp->cursor_pos += scp->xsize;
38556043Syokota			mark_for_update(scp, scp->cursor_pos);
38656043Syokota#else
38756043Syokota			scp->cursor_pos += scp->xsize;
38856043Syokota			/* we may have to scroll the screen */
38956043Syokota#endif
39056043Syokota			scp->ypos++;
39156043Syokota			break;
39256043Syokota
39356043Syokota		case 0x0c:	/* form feed, clears screen */
39456043Syokota			sc_clear_screen(scp);
39556043Syokota			break;
39656043Syokota
39756043Syokota		case 0x0d:	/* return, return to pos 0 */
39856043Syokota#if 0
39956043Syokota			mark_for_update(scp, scp->cursor_pos);
40056043Syokota			scp->cursor_pos -= scp->xpos;
40156043Syokota			mark_for_update(scp, scp->cursor_pos);
40256043Syokota#else
40356043Syokota			scp->cursor_pos -= scp->xpos;
40456043Syokota#endif
40556043Syokota			scp->xpos = 0;
40656043Syokota			break;
40756043Syokota		}
40856043Syokota		ptr++; l--;
40956043Syokota	}
41056043Syokota
41156043Syokota	*buf = ptr;
41256043Syokota	*len = l;
41356043Syokota}
41456043Syokota
41556043Syokotastatic __inline void
41656043Syokotasc_term_gen_scroll(scr_stat *scp, int ch, int attr)
41756043Syokota{
41856043Syokota	/* do we have to scroll ?? */
41956043Syokota	if (scp->cursor_pos >= scp->ysize*scp->xsize) {
42056043Syokota		sc_remove_cutmarking(scp);		/* XXX */
42156043Syokota#ifndef SC_NO_HISTORY
42256043Syokota		if (scp->history != NULL)
42356043Syokota			sc_hist_save_one_line(scp, 0);	/* XXX */
42456043Syokota#endif
42556043Syokota		sc_vtb_delete(&scp->vtb, 0, scp->xsize, ch, attr);
42656043Syokota		scp->cursor_pos -= scp->xsize;
42756043Syokota		scp->ypos--;
42856043Syokota		mark_all(scp);
42956043Syokota	}
43056043Syokota}
43156043Syokota
43256043Syokota#endif /* _DEV_SYSCONS_SCTERMVAR_H_ */
433