forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminal.c
More file actions
294 lines (280 loc) · 13.2 KB
/
Terminal.c
File metadata and controls
294 lines (280 loc) · 13.2 KB
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
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2019 Scott Shawcroft for Adafruit Industries
//
// SPDX-License-Identifier: MIT
#include "shared-module/terminalio/Terminal.h"
#include "shared-module/fontio/BuiltinFont.h"
#include "shared-bindings/displayio/TileGrid.h"
#include "shared-bindings/displayio/Palette.h"
#include "shared-bindings/terminalio/Terminal.h"
#if CIRCUITPY_STATUS_BAR
#include "shared-bindings/supervisor/__init__.h"
#include "shared-bindings/supervisor/StatusBar.h"
#endif
void terminalio_terminal_clear_status_bar(terminalio_terminal_obj_t *self) {
if (self->status_bar) {
common_hal_displayio_tilegrid_set_all_tiles(self->status_bar, 0);
}
}
void common_hal_terminalio_terminal_construct(terminalio_terminal_obj_t *self,
displayio_tilegrid_t *scroll_area, const fontio_builtinfont_t *font,
displayio_tilegrid_t *status_bar) {
self->cursor_x = 0;
self->cursor_y = 0;
self->font = font;
self->scroll_area = scroll_area;
self->status_bar = status_bar;
self->status_x = 0;
self->status_y = 0;
self->first_row = 0;
common_hal_displayio_tilegrid_set_all_tiles(self->scroll_area, 0);
if (self->status_bar) {
common_hal_displayio_tilegrid_set_all_tiles(self->status_bar, 0);
}
common_hal_displayio_tilegrid_set_top_left(self->scroll_area, 0, 1);
}
size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, const byte *data, size_t len, int *errcode) {
// Make sure the terminal is initialized before we do anything with it.
if (self->scroll_area == NULL) {
return len;
}
#if CIRCUITPY_TERMINALIO_VT100
uint32_t _select_color(uint16_t ascii_color) {
uint32_t color_value = 0;
if ((ascii_color & 1) > 0) {
color_value += 0xff0000;
}
if ((ascii_color & 2) > 0) {
color_value += 0x00ff00;
}
if ((ascii_color & 4) > 0) {
color_value += 0x0000ff;
}
return color_value;
}
displayio_palette_t *terminal_palette = self->scroll_area->pixel_shader;
#endif
const byte *i = data;
uint16_t start_y = self->cursor_y;
while (i < data + len) {
unichar c = utf8_get_char(i);
i = utf8_next_char(i);
if (self->in_osc_command) {
if (c == 0x1b && i[0] == '\\') {
self->in_osc_command = false;
self->status_x = 0;
self->status_y = 0;
i += 1;
} else if (
self->osc_command == 0 &&
self->status_bar != NULL &&
self->status_y < self->status_bar->height_in_tiles) {
uint8_t tile_index = fontio_builtinfont_get_glyph_index(self->font, c);
if (tile_index != 0xff) {
// Clear the tile grid before we start putting new info.
if (self->status_x == 0 && self->status_y == 0) {
common_hal_displayio_tilegrid_set_all_tiles(self->status_bar, 0);
}
common_hal_displayio_tilegrid_set_tile(self->status_bar, self->status_x, self->status_y, tile_index);
self->status_x++;
if (self->status_x >= self->status_bar->width_in_tiles) {
self->status_y++;
self->status_x %= self->status_bar->width_in_tiles;
}
}
}
continue;
}
// Always handle ASCII.
if (c < 128) {
if (c >= 0x20 && c <= 0x7e) {
uint8_t tile_index = fontio_builtinfont_get_glyph_index(self->font, c);
common_hal_displayio_tilegrid_set_tile(self->scroll_area, self->cursor_x, self->cursor_y, tile_index);
self->cursor_x++;
} else if (c == '\r') {
self->cursor_x = 0;
} else if (c == '\n') {
self->cursor_y++;
// Commands below are used by MicroPython in the REPL
} else if (c == '\b') {
if (self->cursor_x > 0) {
self->cursor_x--;
}
} else if (c == 0x1b) {
// Handle commands of the form [ESC].<digits><command-char> where . is not yet known.
uint8_t vt_args[3] = {0, -1, -1};
uint8_t j = 1;
#if CIRCUITPY_TERMINALIO_VT100
uint8_t n_args = 1;
#endif
for (; j < 6; j++) {
if ('0' <= i[j] && i[j] <= '9') {
vt_args[0] = vt_args[0] * 10 + (i[j] - '0');
} else {
c = i[j];
break;
}
}
if (i[0] == '[') {
for (uint8_t i_args = 1; i_args < 3 && c == ';'; i_args++) {
vt_args[i_args] = 0;
for (++j; j < 12; j++) {
if ('0' <= i[j] && i[j] <= '9') {
vt_args[i_args] = vt_args[i_args] * 10 + (i[j] - '0');
#if CIRCUITPY_TERMINALIO_VT100
n_args = i_args + 1;
#endif
} else {
c = i[j];
break;
}
}
}
if (c == '?') {
#if CIRCUITPY_TERMINALIO_VT100
if (i[2] == '2' && i[3] == '5') {
// cursor visibility commands
if (i[4] == 'h') {
// make cursor visible
// not implemented yet
} else if (i[4] == 'l') {
// make cursor invisible
// not implemented yet
}
}
i += 5;
#endif
} else {
if (c == 'K') {
uint8_t clr_start = self->cursor_x;
uint8_t clr_end = self->scroll_area->width_in_tiles;
#if CIRCUITPY_TERMINALIO_VT100
if (vt_args[0] == 1) {
clr_start = 0;
clr_end = self->cursor_x;
} else if (vt_args[0] == 2) {
clr_start = 0;
}
#endif
// Clear the (start/rest/all) of the line.
for (uint16_t k = clr_start; k < clr_end; k++) {
common_hal_displayio_tilegrid_set_tile(self->scroll_area, k, self->cursor_y, 0);
}
} else if (c == 'D') {
if (vt_args[0] > self->cursor_x) {
self->cursor_x = 0;
} else {
self->cursor_x -= vt_args[0];
}
} else if (c == 'J') {
if (vt_args[0] == 2) {
common_hal_displayio_tilegrid_set_top_left(self->scroll_area, 0, 0);
self->cursor_x = self->cursor_y = start_y = 0;
common_hal_displayio_tilegrid_set_all_tiles(self->scroll_area, 0);
}
} else if (c == 'H') {
if (vt_args[0] > 0) {
vt_args[0]--;
}
if (vt_args[1] == -1) {
vt_args[1] = 0;
}
if (vt_args[1] > 0) {
vt_args[1]--;
}
if (vt_args[0] >= self->scroll_area->height_in_tiles) {
vt_args[0] = self->scroll_area->height_in_tiles - 1;
}
if (vt_args[1] >= self->scroll_area->width_in_tiles) {
vt_args[1] = self->scroll_area->width_in_tiles - 1;
}
vt_args[0] = (vt_args[0] + self->scroll_area->top_left_y) % self->scroll_area->height_in_tiles;
self->cursor_x = vt_args[1];
self->cursor_y = vt_args[0];
start_y = self->cursor_y;
#if CIRCUITPY_TERMINALIO_VT100
} else if (c == 'm') {
for (uint8_t i_args = 0; i_args < n_args; i_args++) {
if ((vt_args[i_args] >= 40 && vt_args[i_args] <= 47) || (vt_args[i_args] >= 30 && vt_args[i_args] <= 37)) {
common_hal_displayio_palette_set_color(terminal_palette, 1 - (vt_args[i_args] / 40), _select_color(vt_args[i_args] % 10));
}
if (vt_args[i_args] == 0) {
common_hal_displayio_palette_set_color(terminal_palette, 0, 0x000000);
common_hal_displayio_palette_set_color(terminal_palette, 1, 0xffffff);
}
}
#endif
}
i += j + 1;
}
#if CIRCUITPY_TERMINALIO_VT100
} else if (i[0] == 'M') {
if (self->cursor_y != self->scroll_area->top_left_y) {
if (self->cursor_y > 0) {
self->cursor_y = self->cursor_y - 1;
} else {
self->cursor_y = self->scroll_area->height_in_tiles - 1;
}
} else {
if (self->cursor_y > 0) {
common_hal_displayio_tilegrid_set_top_left(self->scroll_area, 0, self->cursor_y - 1);
} else {
common_hal_displayio_tilegrid_set_top_left(self->scroll_area, 0, self->scroll_area->height_in_tiles - 1);
}
for (uint8_t icol = 0; icol < self->scroll_area->width_in_tiles; icol++) {
common_hal_displayio_tilegrid_set_tile(self->scroll_area, icol, self->scroll_area->top_left_y, 0);
}
self->cursor_x = 0;
self->cursor_y = self->scroll_area->top_left_y;
}
start_y = self->cursor_y;
i++;
} else if (i[0] == 'D') {
self->cursor_y = (self->cursor_y + 1) % self->scroll_area->height_in_tiles;
if (self->cursor_y == self->scroll_area->top_left_y) {
common_hal_displayio_tilegrid_set_top_left(self->scroll_area, 0, (self->cursor_y + 1) % self->scroll_area->height_in_tiles);
for (uint8_t icol = 0; icol < self->scroll_area->width_in_tiles; icol++) {
common_hal_displayio_tilegrid_set_tile(self->scroll_area, icol, self->cursor_y, 0);
}
self->cursor_x = 0;
}
start_y = self->cursor_y;
i++;
#endif
} else if (i[0] == ']' && c == ';') {
self->in_osc_command = true;
self->osc_command = vt_args[0];
i += j + 1;
}
}
} else {
uint8_t tile_index = fontio_builtinfont_get_glyph_index(self->font, c);
if (tile_index != 0xff) {
common_hal_displayio_tilegrid_set_tile(self->scroll_area, self->cursor_x, self->cursor_y, tile_index);
self->cursor_x++;
}
}
if (self->cursor_x >= self->scroll_area->width_in_tiles) {
self->cursor_y++;
self->cursor_x %= self->scroll_area->width_in_tiles;
}
if (self->cursor_y >= self->scroll_area->height_in_tiles) {
self->cursor_y %= self->scroll_area->height_in_tiles;
}
if (self->cursor_y != start_y) {
// clear the new row in case of scroll up
if (self->cursor_y == self->scroll_area->top_left_y) {
for (uint16_t j = 0; j < self->scroll_area->width_in_tiles; j++) {
common_hal_displayio_tilegrid_set_tile(self->scroll_area, j, self->cursor_y, 0);
}
common_hal_displayio_tilegrid_set_top_left(self->scroll_area, 0, (self->cursor_y + self->scroll_area->height_in_tiles + 1) % self->scroll_area->height_in_tiles);
}
start_y = self->cursor_y;
}
}
return i - data;
}
bool common_hal_terminalio_terminal_ready_to_tx(terminalio_terminal_obj_t *self) {
return self->scroll_area != NULL;
}