summaryrefslogtreecommitdiff
path: root/Source/Kernel/VTManager/SimpleVT.class.cpp
blob: a3e9600f13bec3cb4b64d4d61027a554b61e7ab1 (plain) (blame)
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
#include "SimpleVT.class.h"
#include <VTManager/VT.ns.h>
#include <DeviceManager/Disp.ns.h>

#define BUFCHR(l, c) m_buff[((l) * m_cols) + (c)]

SimpleVT::SimpleVT(u32int rows, u32int cols, u8int fgcolor, u8int bgcolor) : VirtualTerminal() {
	m_buff = new vtchr[rows * cols];
	m_rows = rows;
	m_cols = cols;
	m_mapped = false;
	m_hideCursor = false;
	m_color = ((bgcolor & 0x0F) << 4) | (fgcolor & 0x0F);	
	clear();

	m_csrcol = 0;
	m_csrlin = 0;
}

SimpleVT::~SimpleVT() {
	if (m_mapped) VT::unmap(this);
	delete [] m_buff;
}

void SimpleVT::putChar(u32int row, u32int col, WChar c) {
	if (row >= m_rows or col >= m_cols) return;
	vtchr* ch = &BUFCHR(row, col);
	ch->c = c;
	ch->color = m_color;
	if (m_mapped) 
		Disp::putChar(row + m_maprow, col + m_mapcol, BUFCHR(row, col).c, m_color);
}

void SimpleVT::clear() {
	for (u32int i = 0; i < m_rows * m_cols; i++) {
		m_buff[i].c = ' ';
		m_buff[i].color = m_color;
	}
	if (m_mapped) redraw();
}

void SimpleVT::map(s32int row, s32int col) {
	m_maprow = (row == -1 ? (Disp::textRows() / 2) - (m_rows / 2) : row);
	m_mapcol = (col == -1 ? (Disp::textCols() / 2) - (m_cols / 2) : col);
	m_mapped = true;
	redraw();
	VT::map(this);
}

void SimpleVT::unmap() {
	m_mapped = false;
	VT::unmap(this);
}

void SimpleVT::redraw() {
	if (!m_mapped) return;
	for (u32int r = 0; r < m_rows; r++) {
		for (u32int c = 0; c < m_cols; c++) {
			Disp::putChar(r + m_maprow, c + m_mapcol, BUFCHR(r, c).c, BUFCHR(r, c).color);
		}
	}
}

void SimpleVT::scroll() {
	for (u32int l = 0; l < m_rows - 1; l++) {
		for (u32int c = 0; c < m_cols; c++) {
			BUFCHR(l, c) = BUFCHR(l + 1, c);
		}
	}
	for (u32int c = 0; c < m_cols; c++) {
		BUFCHR(m_rows - 1, c).c = ' ';
		BUFCHR(m_rows - 1, c).color = m_color;
	}	
	if (m_mapped) {
		if (!Disp::textScroll(m_maprow, m_mapcol, m_rows, m_cols, m_color)) {
			redraw();
		}
	}
}

void SimpleVT::updateCursor() {
	if (!m_mapped) return;
	if (m_hideCursor) return;
	Disp::moveCursor(m_csrlin + m_maprow, m_csrcol + m_mapcol);
}

//Command handling
void SimpleVT::handleEscape(mvt_esc_cmd_t cmd) {
	switch (cmd.cmd) {
		case MVTCMD_CLEAR:
			clear();
			break;
		case MVTCMD_SCROLL:
			scroll();
			break;
		case MVTCMD_SETFGCOLOR:
			m_color = (m_color & 0xF0) | (cmd.a & 0x0F);
			break;
		case MVTCMD_SETBGCOLOR:
			m_color = (m_color & 0x0F) | ((cmd.a & 0x0F) << 4);
			break;
		case MVTCMD_SETCOLOR:
			m_color = ((cmd.b & 0x0F) << 4) | (cmd.a & 0x0F);	
			break;
		case MVTCMD_MOVECSR:
			m_csrlin = cmd.a; m_csrcol = cmd.b;
			updateCursor();
			break;
		case MVTCMD_SETCSRLIN:
			m_csrlin = cmd.a;
			updateCursor();
			break;
		case MVTCMD_SETCSRCOL:
			m_csrcol = cmd.a;
			updateCursor();
			break;
		case MVTCMD_HIDECSR:
			m_hideCursor = true;
			break;
		case MVTCMD_SHOWCSR:
			m_hideCursor = false;
			break;
	}
}

// Display functionn
void SimpleVT::put(WChar c, bool updatecsr) {
	if (c.value == '\b') {
		if (m_csrcol > 0) m_csrcol--;
		putChar(m_csrlin, m_csrcol, ' ');
	} else if (c.value == '\t') {
		m_csrcol = (m_csrcol + 8) &~(8 - 1);
	} else if (c.value == '\r') {
		m_csrcol = 0;
	} else if (c.value == '\n') {
		m_csrcol = 0;
		m_csrlin++;
	} else if (c.value >= ' ') {	//Printable character
		putChar(m_csrlin, m_csrcol, c);
		m_csrcol++;
	}
	if (m_csrcol >= m_cols) {
		m_csrcol = 0;
		m_csrlin++;
	}
	while (m_csrlin >= m_rows) {
		scroll();
		m_csrlin--;
	}
	if (updatecsr && m_mapped) updateCursor();
}

void SimpleVT::hexDump(u8int *ptr, u32int sz, bool addnl) {
	if (m_cols < 76) return;	//Not enough space
	VirtualTerminal::hexDump(ptr, sz, (m_cols > 76));
}