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
|
#include "vt.h"
#include <dev/display.h>
vt *ke_vt = 0, *home_vt = 0;
static char *kb_cmd_esc[] = {
/* 00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 20 */ 0, 0, 0, 0, 0, 0, 0, 0, "\x1b[B", 0,
/* 30 */ "\x1b[D", 0, "\x1b[C", 0, "\x1b[A", 0, 0, 0, 0, 0,
/* 40 */ 0, 0, 0, 0, 0, 0, "\x1b[B", "\x1b[A", "\x1b[D", "\x1b[C",
/* 50 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* 60 */ 0, 0, 0, 0 };
vt::vt(node* parent, int ww, int hh) : node(parent, FT_TERMINAL) {
w = ww; h = hh;
fgcolor = TC_LIGHTGRAY;
bgcolor = TC_BLACK;
output = 0;
cursor_visible = true;
csr_l = csr_c = 0;
kbd_buffer_filled = 0;
kbd_waiter = 0;
text = 0;
if (w != 0 && h != 0) {
text = (vt_char*)kmalloc(w * h * sizeof(vt_char));
clear();
}
}
void vt::put_at(int l, int c, int ch) {
text[l * w + c].fgcolor = fgcolor;
text[l * w + c].bgcolor = bgcolor;
text[l * w + c].ch = ch;
if (output != 0) {
if (output->connected_vt != this) {
output = 0;
} else {
output->text_put(l, c, ch, fgcolor, bgcolor);
}
}
}
void vt::scroll(int n) {
if (output != 0 && output->connected_vt == this) {
output->text_scroll(n, fgcolor, bgcolor);
}
if (n > 0) {
for (int j = 0; j < w * (h-n); j++) {
text[j] = text[j + w];
}
for (int j = w * (h-n); j < w * h; j++) {
text[j].fgcolor = fgcolor;
text[j].bgcolor = bgcolor;
text[j].ch = ' ';
}
} else if (n < 0) {
//TODO
}
csr_l -= n;
}
void vt::put(int c) {
if (text == 0) return;
if (c == '\b' && csr_c != 0) { // backspace
csr_c--;
put_at(csr_l, csr_c, ' ');
} else if (c == '\t') { // tab
int csr_nc = (csr_c + 8) & ~(8 - 1);
for (int i = csr_c; i < csr_nc && i < w; i++) put_at(csr_l, i, ' ');
csr_c = csr_nc;
} else if (c == '\r') { // carriage return
csr_c = 0;
} else if (c == '\n') { // new line
csr_c = 0;
csr_l++;
} else if (c == '\f') { // form feed (new page)
clear();
} else if (c >= ' ') {
put_at(csr_l, csr_c, c);
csr_c++;
}
if (csr_c >= w) {
csr_c = 0;
csr_l++;
}
if (csr_l == h) {
scroll(1);
}
if (output != 0 && output->connected_vt == this) output->text_setcsr(csr_l, csr_c, cursor_visible);
}
void vt::clear() {
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
put_at(j, i, ' ');
}
}
csr_c = csr_l = 0;
}
void vt::writeHex(uint32_t v) {
int i;
put('0'); put('x');
char hexdigits[] = "0123456789abcdef";
for (i = 0; i < 8; i++) {
put(hexdigits[v >> 28]);
v = v << 4;
}
}
void vt::writeDec(int v) {
if (v == 0) {
put('0');
return;
}
if (v < 0) {
put ('-');
v = -v;
}
char numbers[] = "0123456789";
while (v > 0) {
int order = 1, no = 1;
while (v / order > 0) order *= 10;
order /= 10;
put(numbers[v / order]);
v = v - (v / order * order);
while (v / no > 0) no *= 10;
while (no < order) {
put('0');
no *= 10;
}
}
}
// *************** INTERACTION
void vt::outputTo(display *display) {
output = display;
if (output == 0) return;
output->connected_vt = this;
if (output->text_w() != w || output->text_h() != h) {
int ow = w, oh = h;
vt_char *old_text = text;
w = output->text_w();
h = output->text_h();
text = (vt_char*)kmalloc(w * h * sizeof(vt_char));
for (int c = 0; c < w; c++) {
for (int l = 0; l < h; l++) {
if (c < ow && l < oh) {
put_at(l, c, old_text[l * ow + c].ch);
} else {
put_at(l, c, ' ');
}
}
}
if (old_text != 0) kfree(old_text);
if (csr_c >= w) csr_c = w;
if (csr_l >= h) csr_l = h;
} else {
for (int c = 0; c < w; c++) {
for (int l = 0; l < h; l++) {
output->text_put(l, c, text[l*w+c].ch, text[l*w+c].fgcolor, text[l*w+c].bgcolor);
}
}
}
output->text_setcsr(csr_l, csr_c, cursor_visible);
}
void vt::keyboardInput(keypress kp, keyboard* from) {
if (kp.command == KB_RSUPER || kp.command == KB_LSUPER) {
if (this == home_vt) return;
// go to home terminal
home_vt->outputTo(output);
from->outputTo(home_vt);
output = 0;
return;
}
// convert to sequence of chars
int n = 0;
char b[8];
if (kp.character >= 0x80) kp.character = '?'; // todo some day : unicode
if ((kp.command == 0 || kp.command == KB_ENTER || kp.command == KB_TAB || kp.command == KB_BACKSPACE)
&& kp.character != 0 && kp.pressed) {
b[n++] = (char)kp.character;
} else if ((kp.command & KB_CMD_ALT) && kp.character >= 64 && kp.character <= 126 && kp.pressed) {
b[n++] = 27;
b[n++] = kp.character;
} else if ((kp.command & KB_CMD_CTRL) && kp.character >= 'A' && kp.character <= 'z' && kp.pressed) {
if (kp.character >= 'A' && kp.character <= 'Z') b[n++] = 1 + kp.character - 'A';
if (kp.character >= 'a' && kp.character <= 'z') b[n++] = 1 + kp.character - 'a';
} else if (kb_cmd_esc[kp.command & 0x3F] != 0 && kp.pressed) {
int i;
for (i = 0; kb_cmd_esc[kp.command & 0x3F][i] != 0; i++) {
b[n++] = kb_cmd_esc[kp.command & 0x3F][i];
}
}
if (n == 0) return;
if (kbd_buffer_filled + n > KBD_BUFFER_SIZE) goto wake_up;
for (int i = 0; i < n; i++) {
kbd_buffer[kbd_buffer_filled++] = b[i];
}
wake_up:
if (kbd_waiter != 0) {
thread *w = kbd_waiter;
kbd_waiter = 0;
w->wakeUp();
}
}
// ********************* SYSCALLS
int vt::write(size_t offset, size_t len, char* buffer) {
// ignore offset
// TODO: ANSI escape sequences
// todo some day : unicode
for (unsigned i = 0; i < len; i++) {
if (buffer[i] == 27) {
i++;
if (buffer[i++] == '[') {
int n = -1, m = -1;
while (buffer[i] >= '0' && buffer[i] <= '9') {
if (n == -1) n = 0;
n *= 10;
n += buffer[i] - '0';
i++;
}
if (buffer[i] == ';') {
i++;
while (buffer[i] >= '0' && buffer[i] <= '9') {
if (m == -1) m = 0;
m *= 10;
m += buffer[i] - '0';
i++;
}
}
if (buffer[i] == 'A') { // move cursor up
if (n == -1) n = 1;
csr_l -= n;
} else if (buffer[i] == 'B') { // move cursor down
if (n == -1) n = 1;
csr_l += n;
} else if (buffer[i] == 'C') { // move cursor forward
if (n == -1) n = 1;
csr_c += n;
} else if (buffer[i] == 'D') { // move cursor back
if (n == -1) n = 1;
csr_c -= n;
} else if (buffer[i] == 'E') { // move cursor n lines down, to beginning of line
if (n == -1) n = 1;
csr_l += n;
csr_c = 0;
} else if (buffer[i] == 'F') { // move cursor to beginning of line, n lines up
if (n == -1) n = 1;
csr_l -= n;
csr_c = 0;
} else if (buffer[i] == 'H' || buffer[i] == 'f') { // set cursor position
if (n == -1) n = 1;
if (m == -1) m = 1;
csr_l = n - 1;
csr_c = m - 1;
} else if (buffer[i] == 'J' || buffer[i] == 'K') { // erase data / erase line
if (n == -1) n = 0;
int start = 0, end = 0;
if (n == 0) {
start = csr_c; end = w;
} else if (n == 1) {
start = 0; end = csr_c;
} else if (n == 2) {
start = 0; end = w;
}
for (int p = start; p < end; p++) put_at(csr_l, p, ' ');
if (buffer[i] == 'J') { // erase data
if (n == 0) {
start = csr_l + 1; end = h;
} else if (n == 1) {
start = 0; end = csr_l;
} else if (n == 2) {
start = 0; end = h;
}
for (int l = start; l < end; l++) {
for (int c = 0; c < w; c++) put_at(l, c, ' ');
}
}
} else if (buffer[i] == 'S') { // scroll up
if (n == -1) n = 1;
scroll(n);
} else if (buffer[i] == 'T') { // scroll down
if (n == -1) n = 1;
scroll(-n);
} else if (buffer[i] == 'm') { // select graphic rendition
if (n == -1) n = 0;
if (n == 0) {
fgcolor = 7;
bgcolor = 0;
} else if (n == 1) {
fgcolor |= TC_MAKE_LIGHT;
} else if (n == 22) {
fgcolor &= TC_MAKE_DARK;
} else if (n >= 30 && n <= 37) {
fgcolor = (fgcolor & TC_MAKE_LIGHT) | (n - 30);
} else if (n >= 40 && n <= 55) {
bgcolor = (n - 40);
} else {
// others to do.
}
} else if (buffer[i] == 'n') { // device status report
kbd_buffer[kbd_buffer_filled++] = 27;
kbd_buffer[kbd_buffer_filled++] = '[';
if (csr_l >= 100) kbd_buffer[kbd_buffer_filled++] = ((csr_l / 100) % 10) + '0';
if (csr_l >= 10) kbd_buffer[kbd_buffer_filled++] = ((csr_l / 10) % 10) + '0';
kbd_buffer[kbd_buffer_filled++] = (csr_l % 10) + '0';
kbd_buffer[kbd_buffer_filled++] = ';';
if (csr_c >= 100) kbd_buffer[kbd_buffer_filled++] = ((csr_c / 100) % 10) + '0';
if (csr_c >= 10) kbd_buffer[kbd_buffer_filled++] = ((csr_c / 10) % 10) + '0';
kbd_buffer[kbd_buffer_filled++] = (csr_c % 10) + '0';
kbd_buffer[kbd_buffer_filled++] = 'R';
} else if (buffer[i] == 's') { // save cursor position
save_csr_l = csr_l;
save_csr_c = csr_c;
} else if (buffer[i] == 'p') { // restore cursor position
csr_l = save_csr_l;
csr_c = save_csr_c;
} else if (buffer[i] == '?') {
if (buffer[++i] == '2' && buffer[++i] == '5') {
if (buffer[++i] == 'l') cursor_visible = false;
else cursor_visible = true;
}
} else {
}
if (csr_l < 0) csr_l = 0;
if (csr_c < 0) csr_c = 0;
if (csr_l >= h) csr_l = h - 1;
if (csr_c >= w) csr_c = w - 1;
if (output != 0 && output->connected_vt == this) output->text_setcsr(csr_l, csr_c, cursor_visible);
} else {
// ignore it.
}
} else {
put(buffer[i]);
}
}
return len;
}
int vt::read(size_t offset, size_t len, char* buffer) {
// ignore offset
unsigned c = 0;
while (true) {
if (kbd_buffer_filled > 0) {
int r = 0;
while (r < kbd_buffer_filled && c < len) {
buffer[c++] = kbd_buffer[r++];
}
memcpy(kbd_buffer + r, kbd_buffer, (kbd_buffer_filled - r));
kbd_buffer_filled -= r;
return c;
}
if (kbd_waiter != 0) return c;
kbd_waiter = current_thread;
thread_goInactive();
}
}
size_t vt::get_size() {
return ((w << 16) + h);
}
int vt::link(node* to, int mode) {
if (mode == LM_OUTPUT_TO) {
display *d = to->as_display();
if (d != 0) {
outputTo(d);
return 0;
}
}
return E_NOT_IMPLEMENTED;
}
|