Compare commits

..
12 Commits
18 changed files with 1508 additions and 536 deletions
+2 -1
View File
@@ -124,7 +124,7 @@ set(SOURCE_FILES
user/persist.h
include/helpers.h
user/syscfg.c
user/syscfg.h)
user/syscfg.h user/ansi_utf.c user/ansi_utf.h)
include_directories(include)
include_directories(user)
@@ -145,6 +145,7 @@ add_definitions(
-DFLAG_GZIP=2
-DADMIN_PASSWORD="asdf"
-DGIT_HASH="blabla"
-DDEBUG_ANSI=1
-DESPFS_HEATSHRINK)
add_executable(esp_vt100_firmware ${SOURCE_FILES})
+9 -2
View File
@@ -65,8 +65,15 @@ LIBS += esphttpd
# compiler flags using during compilation of source files
CFLAGS = -Os -ggdb -std=gnu99 -Werror -Wpointer-arith -Wundef -Wall -Wl,-EL -fno-inline-functions \
-nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH \
-Wno-address -Wno-unused -DHTTPD_MAX_BACKLOG_SIZE=8192 -DADMIN_PASSWORD=$(ADMIN_PASSWORD) \
-DGIT_HASH='"$(shell git rev-parse --short HEAD)"'
-Wno-address -Wno-unused
CFLAGS += -DHTTPD_MAX_BACKLOG_SIZE=10240
CFLAGS += -DGIT_HASH='"$(shell git rev-parse --short HEAD)"'
CFLAGS += -DADMIN_PASSWORD=$(ADMIN_PASSWORD)
# Debug logging
CFLAGS += -DDEBUG_ANSI=1
CFLAGS += -DDEBUG_INPUT=1
# linker flags used to generate the main object file
LDFLAGS = -nostdlib -Wl,--no-check-sections -u call_user_start -Wl,-static
+18 -3
View File
@@ -1152,9 +1152,6 @@ body.term #botnav {
position: absolute;
top: -9999px; }
.nb {
font-weight: normal !important; }
.theme-0 .fg0 {
color: #111213; }
.theme-0 .bg0 {
@@ -1548,6 +1545,24 @@ body.term #botnav {
.bold {
font-weight: bold !important; }
.faint span {
opacity: 0.6; }
.italic {
font-style: italic; }
.under {
text-decoration: underline; }
.strike {
text-decoration: line-through; }
.underline.strike {
text-decoration: underline line-through; }
.blink-hide .blink {
color: transparent; }
.Row.color-preview {
font-family: monospace;
font-size: 16pt;
+145 -40
View File
@@ -1022,6 +1022,70 @@ $.ready(function() {
}, 1);
}
});
/*! http://mths.be/fromcodepoint v0.1.0 by @mathias */
if (!String.fromCodePoint) {
(function() {
var defineProperty = (function() {
// IE 8 only supports `Object.defineProperty` on DOM elements
try {
var object = {};
var $defineProperty = Object.defineProperty;
var result = $defineProperty(object, object, object) && $defineProperty;
} catch(error) {}
return result;
}());
var stringFromCharCode = String.fromCharCode;
var floor = Math.floor;
var fromCodePoint = function() {
var MAX_SIZE = 0x4000;
var codeUnits = [];
var highSurrogate;
var lowSurrogate;
var index = -1;
var length = arguments.length;
if (!length) {
return '';
}
var result = '';
while (++index < length) {
var codePoint = Number(arguments[index]);
if (
!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
codePoint < 0 || // not a valid Unicode code point
codePoint > 0x10FFFF || // not a valid Unicode code point
floor(codePoint) != codePoint // not an integer
) {
throw RangeError('Invalid code point: ' + codePoint);
}
if (codePoint <= 0xFFFF) { // BMP code point
codeUnits.push(codePoint);
} else { // Astral code point; split in surrogate halves
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint -= 0x10000;
highSurrogate = (codePoint >> 10) + 0xD800;
lowSurrogate = (codePoint % 0x400) + 0xDC00;
codeUnits.push(highSurrogate, lowSurrogate);
}
if (index + 1 == length || codeUnits.length > MAX_SIZE) {
result += stringFromCharCode.apply(null, codeUnits);
codeUnits.length = 0;
}
}
return result;
};
if (defineProperty) {
defineProperty(String, 'fromCodePoint', {
'value': fromCodePoint,
'configurable': true,
'writable': true
});
} else {
String.fromCodePoint = fromCodePoint;
}
}());
}
// Generated from PHP locale file
var _tr = {
"wifi.connected_ip_is": "Connected, IP is ",
@@ -1200,7 +1264,7 @@ var Screen = (function () {
y: 0,
fg: 7, // colors 0-15
bg: 0,
bold: false,
attrs: 0,
suppress: false, // do not turn on in blink interval (for safe moving)
hidden: false // do not show
};
@@ -1208,35 +1272,19 @@ var Screen = (function () {
var screen = [];
var blinkIval;
/** Clear screen */
function _clear() {
for (var i = W*H-1; i>=0; i--) {
var cell = screen[i];
cell.t = ' ';
cell.bg = cursor.bg;
cell.fg = cursor.fg;
cell.bold = false;
_draw(cell);
}
}
/** Set text and color at XY */
function _cellAt(y, x) {
return screen[y*W+x];
}
var frakturExceptions = {
'C': '\u212d',
'H': '\u210c',
'I': '\u2111',
'R': '\u211c',
'Z': '\u2128',
};
/** Get cell under cursor */
function _curCell() {
return screen[cursor.y*W + cursor.x];
}
/** Enable or disable cursor visibility */
function _cursorEnable(enable) {
cursor.hidden = !enable;
cursor.a &= enable;
_draw(_curCell());
}
/** Safely move cursor */
function cursorSet(y, x) {
// Hide and prevent from showing up during the move
@@ -1255,13 +1303,44 @@ var Screen = (function () {
inv = cursor.a && cursor.x == cell.x && cursor.y == cell.y;
}
var e = cell.e, fg, bg;
var elem = cell.e, fg, bg, cn, t;
// Colors
fg = inv ? cell.bg : cell.fg;
bg = inv ? cell.fg : cell.bg;
// Update
e.innerText = (cell.t + ' ')[0];
e.className = 'fg' + fg + ' bg' + bg + (cell.bold ? ' bold' : '');
elem.textContent = t = (cell.t + ' ')[0];
cn = 'fg' + fg + ' bg' + bg;
if (cell.attrs & (1<<0)) cn += ' bold';
if (cell.attrs & (1<<2)) cn += ' italic';
if (cell.attrs & (1<<3)) cn += ' under';
if (cell.attrs & (1<<4)) cn += ' blink';
if (cell.attrs & (1<<5)) {
cn += ' fraktur';
// perform substitution
if (t >= 'a' && t <= 'z') {
t = String.fromCodePoint(0x1d51e - 97 + t.charCodeAt(0));
}
else if (t >= 'A' && t <= 'Z') {
// this set is incomplete, some exceptions are needed
if (frakturExceptions.hasOwnProperty(t)) {
t = frakturExceptions[t];
} else {
t = String.fromCodePoint(0x1d504 - 65 + t.charCodeAt(0));
}
}
elem.textContent = t;
}
if (cell.attrs & (1<<6)) cn += ' strike';
if (cell.attrs & (1<<1)) {
cn += ' faint';
// faint requires special html - otherwise it would also dim the background.
// we use opacity on the text...
elem.innerHTML = '<span>' + e(elem.textContent) + '</span>';
}
elem.className = cn;
}
/** Show entire screen */
@@ -1305,6 +1384,7 @@ var Screen = (function () {
t: ' ',
fg: cursor.fg,
bg: cursor.bg,
attrs: 0,
e: e,
x: i % W,
y: Math.floor(i / W),
@@ -1328,6 +1408,15 @@ var Screen = (function () {
_draw(_curCell(), cursor.a);
}
}, 500);
// blink attribute
setInterval(function () {
$('#screen').removeClass('blink-hide');
setTimeout(function() {
$('#screen').addClass('blink-hide');
}, 800); // 200 ms ON
}, 1000);
inited = true;
}
@@ -1336,11 +1425,18 @@ var Screen = (function () {
return (s.charCodeAt(i++) - 1) + (s.charCodeAt(i) - 1) * 127;
}
var SEQ_SET_COLOR = 1;
/** Decode three-byte number */
function parse3B(s, i) {
return (s.charCodeAt(i) - 1) + (s.charCodeAt(i+1) - 1) * 127 + (s.charCodeAt(i+2) - 1) * 127 * 127;
}
var SEQ_SET_COLOR_ATTR = 1;
var SEQ_REPEAT = 2;
var SEQ_SET_COLOR = 3;
var SEQ_SET_ATTR = 4;
function _load_content(str) {
var i = 0, ci = 0, j, jc, num, num2, t = ' ', fg, bg, bold, cell;
var i = 0, ci = 0, j, jc, num, num2, t = ' ', fg, bg, attrs, cell;
if (!inited) _init();
@@ -1362,25 +1458,31 @@ var Screen = (function () {
num = parse2B(str, i); i += 2; // fg bg bold hidden
cursor.fg = num & 0x0F;
cursor.bg = (num & 0xF0) >> 4;
cursor.bold = !!(num & 0x100);
cursor.hidden = !(num & 0x200);
// console.log("FG ",cursor.fg, ", BG ", cursor.bg,", BOLD ", cursor.bold, ", HIDE ", cursor.hidden);
cursor.hidden = !(num & 0x100);
fg = cursor.fg;
bg = cursor.bg;
bold = cursor.bold;
attrs = 0;
// Here come the content
while(i < str.length && ci<W*H) {
j = str[i++];
jc = j.charCodeAt(0);
if (jc == SEQ_SET_COLOR) {
if (jc == SEQ_SET_COLOR_ATTR) {
num = parse3B(str, i); i += 3;
fg = num & 0x0F;
bg = (num & 0xF0) >> 4;
attrs = (num & 0xFF00)>>8;
}
else if (jc == SEQ_SET_COLOR) {
num = parse2B(str, i); i += 2;
fg = num & 0x0F;
bg = (num & 0xF0) >> 4;
bold = !!(num & 0x100);
// console.log("Switch to ",fg,bg,bold);
}
else if (jc == SEQ_SET_ATTR) {
num = parse2B(str, i); i += 2;
attrs = num & 0xFF;
}
else if (jc == SEQ_REPEAT) {
num = parse2B(str, i); i += 2;
@@ -1390,7 +1492,7 @@ var Screen = (function () {
cell.fg = fg;
cell.bg = bg;
cell.t = t;
cell.bold = bold;
cell.attrs = attrs;
}
}
else {
@@ -1399,7 +1501,7 @@ var Screen = (function () {
t = cell.t = j;
cell.fg = fg;
cell.bg = bg;
cell.bold = bold;
cell.attrs = attrs;
// console.log("Symbol ", j);
}
}
@@ -1450,12 +1552,14 @@ var Conn = (function() {
console.warn("SOCKET CLOSED, code "+evt.code+". Reconnecting...");
setTimeout(function() {
init();
}, 1000);
}, 200);
// this happens when the buffer gets fucked up via invalid unicode.
// we basically use polling instead of socket then
}
function onMessage(evt) {
try {
console.log("RX: ", evt.data);
//console.log("RX: ", evt.data);
// Assume all our messages are screen updates
Screen.load(evt.data);
} catch(e) {
@@ -1530,6 +1634,7 @@ var Input = (function() {
//console.log("Down ", code, e);
switch(code) {
case 8: sendStrMsg('\x08'); break;
case 9: sendStrMsg('\x09'); break;
case 10:
case 13: sendStrMsg('\x0d\x0a'); break;
case 27: sendStrMsg('\x1b'); break; // this allows to directly enter control sequences
+64
View File
@@ -123,3 +123,67 @@ $.ready(function() {
}, 1);
}
});
/*! http://mths.be/fromcodepoint v0.1.0 by @mathias */
if (!String.fromCodePoint) {
(function() {
var defineProperty = (function() {
// IE 8 only supports `Object.defineProperty` on DOM elements
try {
var object = {};
var $defineProperty = Object.defineProperty;
var result = $defineProperty(object, object, object) && $defineProperty;
} catch(error) {}
return result;
}());
var stringFromCharCode = String.fromCharCode;
var floor = Math.floor;
var fromCodePoint = function() {
var MAX_SIZE = 0x4000;
var codeUnits = [];
var highSurrogate;
var lowSurrogate;
var index = -1;
var length = arguments.length;
if (!length) {
return '';
}
var result = '';
while (++index < length) {
var codePoint = Number(arguments[index]);
if (
!isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity`
codePoint < 0 || // not a valid Unicode code point
codePoint > 0x10FFFF || // not a valid Unicode code point
floor(codePoint) != codePoint // not an integer
) {
throw RangeError('Invalid code point: ' + codePoint);
}
if (codePoint <= 0xFFFF) { // BMP code point
codeUnits.push(codePoint);
} else { // Astral code point; split in surrogate halves
// http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
codePoint -= 0x10000;
highSurrogate = (codePoint >> 10) + 0xD800;
lowSurrogate = (codePoint % 0x400) + 0xDC00;
codeUnits.push(highSurrogate, lowSurrogate);
}
if (index + 1 == length || codeUnits.length > MAX_SIZE) {
result += stringFromCharCode.apply(null, codeUnits);
codeUnits.length = 0;
}
}
return result;
};
if (defineProperty) {
defineProperty(String, 'fromCodePoint', {
'value': fromCodePoint,
'configurable': true,
'writable': true
});
} else {
String.fromCodePoint = fromCodePoint;
}
}());
}
+81 -40
View File
@@ -8,7 +8,7 @@ var Screen = (function () {
y: 0,
fg: 7, // colors 0-15
bg: 0,
bold: false,
attrs: 0,
suppress: false, // do not turn on in blink interval (for safe moving)
hidden: false // do not show
};
@@ -16,35 +16,19 @@ var Screen = (function () {
var screen = [];
var blinkIval;
/** Clear screen */
function _clear() {
for (var i = W*H-1; i>=0; i--) {
var cell = screen[i];
cell.t = ' ';
cell.bg = cursor.bg;
cell.fg = cursor.fg;
cell.bold = false;
_draw(cell);
}
}
/** Set text and color at XY */
function _cellAt(y, x) {
return screen[y*W+x];
}
var frakturExceptions = {
'C': '\u212d',
'H': '\u210c',
'I': '\u2111',
'R': '\u211c',
'Z': '\u2128',
};
/** Get cell under cursor */
function _curCell() {
return screen[cursor.y*W + cursor.x];
}
/** Enable or disable cursor visibility */
function _cursorEnable(enable) {
cursor.hidden = !enable;
cursor.a &= enable;
_draw(_curCell());
}
/** Safely move cursor */
function cursorSet(y, x) {
// Hide and prevent from showing up during the move
@@ -63,13 +47,44 @@ var Screen = (function () {
inv = cursor.a && cursor.x == cell.x && cursor.y == cell.y;
}
var e = cell.e, fg, bg;
var elem = cell.e, fg, bg, cn, t;
// Colors
fg = inv ? cell.bg : cell.fg;
bg = inv ? cell.fg : cell.bg;
// Update
e.innerText = (cell.t + ' ')[0];
e.className = 'fg' + fg + ' bg' + bg + (cell.bold ? ' bold' : '');
elem.textContent = t = (cell.t + ' ')[0];
cn = 'fg' + fg + ' bg' + bg;
if (cell.attrs & (1<<0)) cn += ' bold';
if (cell.attrs & (1<<2)) cn += ' italic';
if (cell.attrs & (1<<3)) cn += ' under';
if (cell.attrs & (1<<4)) cn += ' blink';
if (cell.attrs & (1<<5)) {
cn += ' fraktur';
// perform substitution
if (t >= 'a' && t <= 'z') {
t = String.fromCodePoint(0x1d51e - 97 + t.charCodeAt(0));
}
else if (t >= 'A' && t <= 'Z') {
// this set is incomplete, some exceptions are needed
if (frakturExceptions.hasOwnProperty(t)) {
t = frakturExceptions[t];
} else {
t = String.fromCodePoint(0x1d504 - 65 + t.charCodeAt(0));
}
}
elem.textContent = t;
}
if (cell.attrs & (1<<6)) cn += ' strike';
if (cell.attrs & (1<<1)) {
cn += ' faint';
// faint requires special html - otherwise it would also dim the background.
// we use opacity on the text...
elem.innerHTML = '<span>' + e(elem.textContent) + '</span>';
}
elem.className = cn;
}
/** Show entire screen */
@@ -113,6 +128,7 @@ var Screen = (function () {
t: ' ',
fg: cursor.fg,
bg: cursor.bg,
attrs: 0,
e: e,
x: i % W,
y: Math.floor(i / W),
@@ -136,6 +152,15 @@ var Screen = (function () {
_draw(_curCell(), cursor.a);
}
}, 500);
// blink attribute
setInterval(function () {
$('#screen').removeClass('blink-hide');
setTimeout(function() {
$('#screen').addClass('blink-hide');
}, 800); // 200 ms ON
}, 1000);
inited = true;
}
@@ -144,11 +169,18 @@ var Screen = (function () {
return (s.charCodeAt(i++) - 1) + (s.charCodeAt(i) - 1) * 127;
}
var SEQ_SET_COLOR = 1;
/** Decode three-byte number */
function parse3B(s, i) {
return (s.charCodeAt(i) - 1) + (s.charCodeAt(i+1) - 1) * 127 + (s.charCodeAt(i+2) - 1) * 127 * 127;
}
var SEQ_SET_COLOR_ATTR = 1;
var SEQ_REPEAT = 2;
var SEQ_SET_COLOR = 3;
var SEQ_SET_ATTR = 4;
function _load_content(str) {
var i = 0, ci = 0, j, jc, num, num2, t = ' ', fg, bg, bold, cell;
var i = 0, ci = 0, j, jc, num, num2, t = ' ', fg, bg, attrs, cell;
if (!inited) _init();
@@ -170,25 +202,31 @@ var Screen = (function () {
num = parse2B(str, i); i += 2; // fg bg bold hidden
cursor.fg = num & 0x0F;
cursor.bg = (num & 0xF0) >> 4;
cursor.bold = !!(num & 0x100);
cursor.hidden = !(num & 0x200);
// console.log("FG ",cursor.fg, ", BG ", cursor.bg,", BOLD ", cursor.bold, ", HIDE ", cursor.hidden);
cursor.hidden = !(num & 0x100);
fg = cursor.fg;
bg = cursor.bg;
bold = cursor.bold;
attrs = 0;
// Here come the content
while(i < str.length && ci<W*H) {
j = str[i++];
jc = j.charCodeAt(0);
if (jc == SEQ_SET_COLOR) {
if (jc == SEQ_SET_COLOR_ATTR) {
num = parse3B(str, i); i += 3;
fg = num & 0x0F;
bg = (num & 0xF0) >> 4;
attrs = (num & 0xFF00)>>8;
}
else if (jc == SEQ_SET_COLOR) {
num = parse2B(str, i); i += 2;
fg = num & 0x0F;
bg = (num & 0xF0) >> 4;
bold = !!(num & 0x100);
// console.log("Switch to ",fg,bg,bold);
}
else if (jc == SEQ_SET_ATTR) {
num = parse2B(str, i); i += 2;
attrs = num & 0xFF;
}
else if (jc == SEQ_REPEAT) {
num = parse2B(str, i); i += 2;
@@ -198,7 +236,7 @@ var Screen = (function () {
cell.fg = fg;
cell.bg = bg;
cell.t = t;
cell.bold = bold;
cell.attrs = attrs;
}
}
else {
@@ -207,7 +245,7 @@ var Screen = (function () {
t = cell.t = j;
cell.fg = fg;
cell.bg = bg;
cell.bold = bold;
cell.attrs = attrs;
// console.log("Symbol ", j);
}
}
@@ -258,12 +296,14 @@ var Conn = (function() {
console.warn("SOCKET CLOSED, code "+evt.code+". Reconnecting...");
setTimeout(function() {
init();
}, 1000);
}, 200);
// this happens when the buffer gets fucked up via invalid unicode.
// we basically use polling instead of socket then
}
function onMessage(evt) {
try {
console.log("RX: ", evt.data);
//console.log("RX: ", evt.data);
// Assume all our messages are screen updates
Screen.load(evt.data);
} catch(e) {
@@ -338,6 +378,7 @@ var Input = (function() {
//console.log("Down ", code, e);
switch(code) {
case 8: sendStrMsg('\x08'); break;
case 9: sendStrMsg('\x09'); break;
case 10:
case 13: sendStrMsg('\x0d\x0a'); break;
case 27: sendStrMsg('\x1b'); break; // this allows to directly enter control sequences
+26 -5
View File
@@ -86,11 +86,6 @@ body.term {
top: -9999px;
}
// "non-bold"
.nb {
font-weight: normal !important;
}
// Tango
.theme-0 {
$term-colors:
@@ -163,10 +158,36 @@ body.term {
}
}
// Attributes
.bold {
font-weight: bold !important;
}
.faint span { // content of faint is wrapped in span
opacity: 0.6;
}
.italic {
font-style: italic;
}
.under {
text-decoration: underline;
}
.strike {
text-decoration: line-through;
}
.underline.strike {
text-decoration: underline line-through;
}
.blink-hide .blink {
color: transparent;
}
//
.Row.color-preview {
font-family: monospace;
font-size: 16pt;
+346 -200
View File
@@ -11,23 +11,27 @@ static const char _ansi_actions[] = {
0, 1, 0, 1, 1, 1, 2, 1,
3, 1, 4, 1, 5, 1, 6, 1,
7, 1, 8, 1, 9, 1, 10, 1,
11, 1, 12, 1, 13, 1, 14, 2,
9, 10, 2, 9, 11
11, 1, 12, 1, 13, 1, 14, 1,
15, 1, 16, 1, 17, 2, 2, 5,
2, 10, 11, 2, 10, 12
};
static const char _ansi_eof_actions[] = {
0, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 0, 0, 0, 0, 0
13, 13, 13, 13, 13, 13, 0, 0,
0, 0, 0, 0, 0, 0, 0
};
static const int ansi_start = 1;
static const int ansi_first_final = 26;
static const int ansi_first_final = 30;
static const int ansi_error = 0;
static const int ansi_en_CSI_body = 3;
static const int ansi_en_OSC_body = 5;
static const int ansi_en_CSI_body = 5;
static const int ansi_en_OSC_body = 7;
static const int ansi_en_TITLE_body = 27;
static const int ansi_en_charsetcmd_body = 29;
static const int ansi_en_main = 1;
@@ -42,10 +46,39 @@ static void ICACHE_FLASH_ATTR
resetParserCb(void *arg) {
if (cs != ansi_start) {
cs = ansi_start;
warn("Parser timeout, state reset");
apars_reset_utf8buffer();
ansi_warn("Parser timeout, state reset");
}
}
#define HISTORY_LEN 16
#if DEBUG_ANSI
static char history[HISTORY_LEN + 1];
#endif
void ICACHE_FLASH_ATTR
apars_handle_badseq(void)
{
#if DEBUG_ANSI
char buf1[HISTORY_LEN*3+2];
char buf2[HISTORY_LEN*3+2];
char *b1 = buf1;
char *b2 = buf2;
char c;
for(int i=0;i<HISTORY_LEN;i++) {
c = history[i];
b1 += sprintf(b1, "%2X ", c);
if (c < 32 || c > 127) c = '.';
b2 += sprintf(b2, "%c ", c);
}
ansi_dbg("Context: %s", buf2);
ansi_dbg(" %s", buf1);
#endif
}
/**
* \brief Linear ANSI chars stream parser
*
@@ -65,6 +98,7 @@ ansi_parser(const char *newdata, size_t len)
// The CSI code is built here
static char csi_leading; //!< Leading char, 0 if none
static int csi_ni; //!< Number of the active digit
static int csi_cnt; //!< Digit count
static int csi_n[CSI_N_MAX]; //!< Param digits
static char csi_char; //!< CSI action char (end)
static char osc_buffer[OSC_CHAR_MAX];
@@ -80,24 +114,35 @@ ansi_parser(const char *newdata, size_t len)
// Init Ragel on the first run
if (cs == -1) {
/* #line 84 "user/ansi_parser.c" */
/* #line 118 "user/ansi_parser.c" */
{
cs = ansi_start;
}
/* #line 57 "user/ansi_parser.rl" */
/* #line 87 "user/ansi_parser.rl" */
#if DEBUG_ANSI
memset(history, 0, sizeof(history));
#endif
}
// schedule state reset
// schedule state reset after idle timeout
if (termconf->parser_tout_ms > 0) {
os_timer_disarm(&resetTim);
os_timer_setfn(&resetTim, resetParserCb, NULL);
os_timer_arm(&resetTim, termconf->parser_tout_ms, 0);
}
#if DEBUG_ANSI
for(int i=len; i<HISTORY_LEN; i++) {
history[i-len] = history[i];
}
strcpy(&history[HISTORY_LEN-len], newdata);
#endif
// The parser
/* #line 101 "user/ansi_parser.c" */
/* #line 146 "user/ansi_parser.c" */
{
const char *_acts;
unsigned int _nacts;
@@ -114,254 +159,329 @@ case 1:
goto tr0;
case 2:
switch( (*p) ) {
case 55: goto tr3;
case 56: goto tr4;
case 91: goto tr5;
case 93: goto tr6;
case 99: goto tr7;
case 32: goto tr3;
case 35: goto tr4;
case 91: goto tr7;
case 93: goto tr8;
case 107: goto tr9;
}
goto tr2;
case 0:
goto _out;
case 26:
if ( (*p) == 27 )
goto tr1;
goto tr0;
case 3:
if ( (*p) == 59 )
goto tr10;
if ( (*p) < 60 ) {
if ( (*p) > 47 ) {
if ( 48 <= (*p) && (*p) <= 57 )
goto tr9;
} else if ( (*p) >= 32 )
goto tr8;
} else if ( (*p) > 64 ) {
goto tr6;
} else if ( (*p) >= 40 )
goto tr5;
} else if ( (*p) > 62 ) {
if ( (*p) > 90 ) {
if ( 97 <= (*p) && (*p) <= 122 )
goto tr11;
goto tr6;
} else if ( (*p) >= 65 )
goto tr11;
goto tr6;
} else
goto tr8;
goto tr6;
goto tr2;
case 4:
if ( (*p) == 59 )
case 0:
goto _out;
case 3:
if ( 70 <= (*p) && (*p) <= 71 )
goto tr10;
if ( (*p) < 65 ) {
if ( 48 <= (*p) && (*p) <= 57 )
goto tr9;
} else if ( (*p) > 90 ) {
if ( 97 <= (*p) && (*p) <= 122 )
goto tr11;
} else
goto tr11;
goto tr2;
case 27:
case 30:
if ( (*p) == 27 )
goto tr1;
goto tr0;
case 4:
if ( 48 <= (*p) && (*p) <= 57 )
goto tr11;
goto tr2;
case 5:
switch( (*p) ) {
case 66: goto tr12;
case 84: goto tr13;
case 87: goto tr14;
case 59: goto tr14;
case 64: goto tr15;
}
if ( (*p) < 60 ) {
if ( (*p) > 47 ) {
if ( 48 <= (*p) && (*p) <= 57 )
goto tr13;
} else if ( (*p) >= 32 )
goto tr12;
} else if ( (*p) > 63 ) {
if ( (*p) > 90 ) {
if ( 96 <= (*p) && (*p) <= 122 )
goto tr16;
} else if ( (*p) >= 65 )
goto tr16;
} else
goto tr12;
goto tr2;
case 6:
if ( (*p) == 84 )
goto tr15;
goto tr2;
case 7:
if ( (*p) == 78 )
if ( (*p) == 59 )
goto tr14;
if ( (*p) < 64 ) {
if ( 48 <= (*p) && (*p) <= 57 )
goto tr13;
} else if ( (*p) > 90 ) {
if ( 96 <= (*p) && (*p) <= 122 )
goto tr16;
} else
goto tr16;
goto tr2;
case 31:
goto tr2;
case 32:
if ( (*p) == 59 )
goto tr14;
if ( (*p) < 64 ) {
if ( 48 <= (*p) && (*p) <= 57 )
goto tr13;
} else if ( (*p) > 90 ) {
if ( 96 <= (*p) && (*p) <= 122 )
goto tr16;
} else
goto tr16;
goto tr2;
case 7:
switch( (*p) ) {
case 48: goto tr17;
case 66: goto tr18;
case 84: goto tr19;
case 87: goto tr20;
}
goto tr2;
case 8:
if ( 48 <= (*p) && (*p) <= 57 )
goto tr17;
if ( (*p) == 59 )
goto tr21;
goto tr2;
case 9:
if ( (*p) == 61 )
goto tr18;
goto tr2;
switch( (*p) ) {
case 7: goto tr23;
case 27: goto tr24;
}
goto tr22;
case 33:
switch( (*p) ) {
case 7: goto tr23;
case 27: goto tr24;
}
goto tr22;
case 10:
if ( (*p) == 27 )
goto tr2;
goto tr19;
case 11:
switch( (*p) ) {
case 7: goto tr20;
case 27: goto tr21;
}
goto tr19;
case 28:
switch( (*p) ) {
case 7: goto tr20;
case 27: goto tr21;
}
goto tr19;
case 12:
if ( (*p) == 92 )
goto tr22;
goto tr2;
case 29:
goto tr2;
case 13:
if ( (*p) == 73 )
goto tr23;
goto tr2;
case 14:
if ( (*p) == 84 )
goto tr24;
goto tr2;
case 15:
if ( (*p) == 76 )
goto tr25;
goto tr2;
case 16:
if ( (*p) == 69 )
case 34:
goto tr2;
case 11:
if ( (*p) == 84 )
goto tr26;
goto tr2;
case 17:
if ( (*p) == 61 )
case 12:
if ( (*p) == 78 )
goto tr27;
goto tr2;
case 18:
if ( (*p) == 27 )
goto tr2;
goto tr28;
case 19:
case 13:
if ( 48 <= (*p) && (*p) <= 57 )
goto tr28;
goto tr2;
case 14:
if ( (*p) == 61 )
goto tr29;
goto tr2;
case 15:
switch( (*p) ) {
case 7: goto tr29;
case 27: goto tr30;
case 7: goto tr31;
case 27: goto tr32;
}
goto tr28;
case 30:
goto tr30;
case 35:
switch( (*p) ) {
case 7: goto tr29;
case 27: goto tr30;
case 7: goto tr31;
case 27: goto tr32;
}
goto tr28;
case 20:
goto tr30;
case 16:
if ( (*p) == 92 )
goto tr31;
goto tr33;
goto tr2;
case 17:
if ( (*p) == 73 )
goto tr34;
goto tr2;
case 18:
if ( (*p) == 84 )
goto tr35;
goto tr2;
case 19:
if ( (*p) == 76 )
goto tr36;
goto tr2;
case 20:
if ( (*p) == 69 )
goto tr37;
goto tr2;
case 21:
if ( 48 <= (*p) && (*p) <= 57 )
goto tr32;
if ( (*p) == 61 )
goto tr38;
goto tr2;
case 22:
if ( (*p) == 59 )
goto tr33;
if ( 48 <= (*p) && (*p) <= 57 )
goto tr32;
goto tr39;
goto tr2;
case 23:
if ( (*p) == 59 )
goto tr40;
if ( 48 <= (*p) && (*p) <= 57 )
goto tr34;
goto tr39;
goto tr2;
case 24:
switch( (*p) ) {
case 7: goto tr35;
case 27: goto tr36;
}
if ( 48 <= (*p) && (*p) <= 57 )
goto tr34;
goto tr41;
goto tr2;
case 25:
switch( (*p) ) {
case 7: goto tr42;
case 27: goto tr43;
}
if ( 48 <= (*p) && (*p) <= 57 )
goto tr41;
goto tr2;
case 26:
if ( (*p) == 92 )
goto tr35;
goto tr42;
goto tr2;
case 27:
switch( (*p) ) {
case 7: goto tr45;
case 27: goto tr46;
}
goto tr44;
case 36:
switch( (*p) ) {
case 7: goto tr45;
case 27: goto tr46;
}
goto tr44;
case 28:
if ( (*p) == 92 )
goto tr47;
goto tr2;
case 37:
goto tr2;
case 29:
goto tr48;
case 38:
goto tr2;
}
tr2: cs = 0; goto f0;
tr0: cs = 1; goto f1;
tr1: cs = 2; goto _again;
tr8: cs = 4; goto f7;
tr9: cs = 4; goto f8;
tr10: cs = 4; goto f9;
tr12: cs = 6; goto _again;
tr15: cs = 7; goto _again;
tr16: cs = 8; goto _again;
tr17: cs = 9; goto f8;
tr18: cs = 10; goto _again;
tr19: cs = 11; goto f11;
tr21: cs = 12; goto _again;
tr13: cs = 13; goto _again;
tr23: cs = 14; goto _again;
tr24: cs = 15; goto _again;
tr25: cs = 16; goto _again;
tr26: cs = 17; goto _again;
tr27: cs = 18; goto _again;
tr28: cs = 19; goto f11;
tr30: cs = 20; goto _again;
tr14: cs = 21; goto _again;
tr32: cs = 22; goto f8;
tr33: cs = 23; goto f9;
tr34: cs = 24; goto f8;
tr36: cs = 25; goto _again;
tr3: cs = 26; goto f2;
tr4: cs = 26; goto f3;
tr5: cs = 26; goto f4;
tr6: cs = 26; goto f5;
tr7: cs = 26; goto f6;
tr11: cs = 27; goto f10;
tr20: cs = 28; goto f12;
tr22: cs = 29; goto f13;
tr31: cs = 29; goto f15;
tr35: cs = 29; goto f16;
tr29: cs = 30; goto f14;
tr3: cs = 3; goto _again;
tr4: cs = 4; goto _again;
tr12: cs = 6; goto f9;
tr13: cs = 6; goto f10;
tr14: cs = 6; goto f11;
tr17: cs = 8; goto _again;
tr21: cs = 9; goto _again;
tr22: cs = 9; goto f14;
tr24: cs = 10; goto _again;
tr18: cs = 11; goto _again;
tr26: cs = 12; goto _again;
tr27: cs = 13; goto _again;
tr28: cs = 14; goto f10;
tr29: cs = 15; goto _again;
tr30: cs = 15; goto f14;
tr32: cs = 16; goto _again;
tr19: cs = 17; goto _again;
tr34: cs = 18; goto _again;
tr35: cs = 19; goto _again;
tr36: cs = 20; goto _again;
tr37: cs = 21; goto _again;
tr20: cs = 22; goto _again;
tr39: cs = 23; goto f10;
tr40: cs = 24; goto f11;
tr41: cs = 25; goto f10;
tr43: cs = 26; goto _again;
tr44: cs = 27; goto f14;
tr46: cs = 28; goto _again;
tr5: cs = 30; goto f2;
tr6: cs = 30; goto f3;
tr7: cs = 30; goto f4;
tr8: cs = 30; goto f5;
tr9: cs = 30; goto f6;
tr10: cs = 30; goto f7;
tr11: cs = 30; goto f8;
tr16: cs = 31; goto f13;
tr15: cs = 32; goto f12;
tr23: cs = 33; goto f15;
tr38: cs = 34; goto f6;
tr25: cs = 34; goto f16;
tr33: cs = 34; goto f18;
tr42: cs = 34; goto f19;
tr31: cs = 35; goto f17;
tr45: cs = 36; goto f15;
tr47: cs = 37; goto f16;
tr48: cs = 38; goto f20;
f1: _acts = _ansi_actions + 1; goto execFuncs;
f4: _acts = _ansi_actions + 3; goto execFuncs;
f7: _acts = _ansi_actions + 5; goto execFuncs;
f8: _acts = _ansi_actions + 7; goto execFuncs;
f9: _acts = _ansi_actions + 9; goto execFuncs;
f10: _acts = _ansi_actions + 11; goto execFuncs;
f9: _acts = _ansi_actions + 5; goto execFuncs;
f10: _acts = _ansi_actions + 7; goto execFuncs;
f11: _acts = _ansi_actions + 9; goto execFuncs;
f13: _acts = _ansi_actions + 11; goto execFuncs;
f0: _acts = _ansi_actions + 13; goto execFuncs;
f5: _acts = _ansi_actions + 15; goto execFuncs;
f16: _acts = _ansi_actions + 17; goto execFuncs;
f11: _acts = _ansi_actions + 19; goto execFuncs;
f15: _acts = _ansi_actions + 21; goto execFuncs;
f13: _acts = _ansi_actions + 23; goto execFuncs;
f6: _acts = _ansi_actions + 25; goto execFuncs;
f2: _acts = _ansi_actions + 27; goto execFuncs;
f6: _acts = _ansi_actions + 17; goto execFuncs;
f19: _acts = _ansi_actions + 19; goto execFuncs;
f14: _acts = _ansi_actions + 21; goto execFuncs;
f16: _acts = _ansi_actions + 23; goto execFuncs;
f18: _acts = _ansi_actions + 25; goto execFuncs;
f8: _acts = _ansi_actions + 27; goto execFuncs;
f3: _acts = _ansi_actions + 29; goto execFuncs;
f14: _acts = _ansi_actions + 31; goto execFuncs;
f12: _acts = _ansi_actions + 34; goto execFuncs;
f7: _acts = _ansi_actions + 31; goto execFuncs;
f2: _acts = _ansi_actions + 33; goto execFuncs;
f20: _acts = _ansi_actions + 35; goto execFuncs;
f12: _acts = _ansi_actions + 37; goto execFuncs;
f15: _acts = _ansi_actions + 40; goto execFuncs;
f17: _acts = _ansi_actions + 43; goto execFuncs;
execFuncs:
_nacts = *_acts++;
while ( _nacts-- > 0 ) {
switch ( *_acts++ ) {
case 0:
/* #line 76 "user/ansi_parser.rl" */
/* #line 117 "user/ansi_parser.rl" */
{
apars_handle_plainchar((*p));
if ((*p) != 0) {
apars_handle_plainchar((*p));
}
}
break;
case 1:
/* #line 83 "user/ansi_parser.rl" */
/* #line 126 "user/ansi_parser.rl" */
{
// Reset the CSI builder
csi_leading = csi_char = 0;
csi_ni = 0;
csi_cnt = 0;
// Zero out digits
for(int i = 0; i < CSI_N_MAX; i++) {
csi_n[i] = 0;
}
{cs = 3;goto _again;}
{cs = 5;goto _again;}
}
break;
case 2:
/* #line 96 "user/ansi_parser.rl" */
/* #line 140 "user/ansi_parser.rl" */
{
csi_leading = (*p);
}
break;
case 3:
/* #line 100 "user/ansi_parser.rl" */
/* #line 144 "user/ansi_parser.rl" */
{
if (csi_cnt == 0) csi_cnt = 1;
// x10 + digit
if (csi_ni < CSI_N_MAX) {
csi_n[csi_ni] = csi_n[csi_ni]*10 + ((*p) - '0');
@@ -369,30 +489,31 @@ execFuncs:
}
break;
case 4:
/* #line 107 "user/ansi_parser.rl" */
/* #line 152 "user/ansi_parser.rl" */
{
if (csi_cnt == 0) csi_cnt = 1; // handle case when first arg is empty
csi_cnt++;
csi_ni++;
}
break;
case 5:
/* #line 111 "user/ansi_parser.rl" */
/* #line 158 "user/ansi_parser.rl" */
{
csi_char = (*p);
apars_handle_CSI(csi_leading, csi_n, csi_char);
apars_handle_CSI(csi_leading, csi_n, csi_cnt, csi_char);
{cs = 1;goto _again;}
}
break;
case 6:
/* #line 119 "user/ansi_parser.rl" */
/* #line 164 "user/ansi_parser.rl" */
{
ansi_warn("Invalid escape sequence discarded.");
apars_handle_badseq();
{cs = 1;goto _again;}
}
break;
case 7:
/* #line 136 "user/ansi_parser.rl" */
/* #line 182 "user/ansi_parser.rl" */
{
csi_ni = 0;
@@ -404,61 +525,83 @@ execFuncs:
osc_bi = 0;
osc_buffer[0] = '\0';
{cs = 5;goto _again;}
{cs = 7;goto _again;}
}
break;
case 8:
/* #line 150 "user/ansi_parser.rl" */
/* #line 197 "user/ansi_parser.rl" */
{
osc_bi = 0;
osc_buffer[0] = '\0';
{cs = 27;goto _again;}
}
break;
case 9:
/* #line 203 "user/ansi_parser.rl" */
{
apars_handle_OSC_SetScreenSize(csi_n[0], csi_n[1]);
{cs = 1;goto _again;}
}
break;
case 9:
/* #line 155 "user/ansi_parser.rl" */
case 10:
/* #line 208 "user/ansi_parser.rl" */
{
osc_buffer[osc_bi++] = (*p);
}
break;
case 10:
/* #line 159 "user/ansi_parser.rl" */
case 11:
/* #line 212 "user/ansi_parser.rl" */
{
osc_buffer[osc_bi++] = '\0';
apars_handle_OSC_SetTitle(osc_buffer);
{cs = 1;goto _again;}
}
break;
case 11:
/* #line 165 "user/ansi_parser.rl" */
case 12:
/* #line 218 "user/ansi_parser.rl" */
{
osc_buffer[osc_bi++] = '\0';
apars_handle_OSC_SetButton(csi_n[0], osc_buffer);
{cs = 1;goto _again;}
}
break;
case 12:
/* #line 177 "user/ansi_parser.rl" */
{
// Reset screen
apars_handle_RESET_cmd();
{cs = 1;goto _again;}
}
break;
case 13:
/* #line 183 "user/ansi_parser.rl" */
/* #line 250 "user/ansi_parser.rl" */
{
apars_handle_saveCursorAttrs();
apars_handle_hashCode((*p));
{cs = 1;goto _again;}
}
break;
case 14:
/* #line 188 "user/ansi_parser.rl" */
/* #line 255 "user/ansi_parser.rl" */
{
apars_handle_restoreCursorAttrs();
apars_handle_shortCode((*p));
{cs = 1;goto _again;}
}
break;
/* #line 462 "user/ansi_parser.c" */
case 15:
/* #line 260 "user/ansi_parser.rl" */
{
apars_handle_setXCtrls((*p));
{cs = 1;goto _again;}
}
break;
case 16:
/* #line 265 "user/ansi_parser.rl" */
{
// abuse the buffer for storing the leading char
osc_buffer[0] = (*p);
{cs = 29;goto _again;}
}
break;
case 17:
/* #line 271 "user/ansi_parser.rl" */
{
apars_handle_characterSet(osc_buffer[0], (*p));
{cs = 1;goto _again;}
}
break;
/* #line 605 "user/ansi_parser.c" */
}
}
goto _again;
@@ -476,15 +619,16 @@ _again:
while ( __nacts-- > 0 ) {
switch ( *__acts++ ) {
case 6:
/* #line 119 "user/ansi_parser.rl" */
/* #line 164 "user/ansi_parser.rl" */
{
ansi_warn("Invalid escape sequence discarded.");
apars_handle_badseq();
{cs = 1; if ( p == pe )
goto _test_eof;
goto _again;}
}
break;
/* #line 488 "user/ansi_parser.c" */
/* #line 632 "user/ansi_parser.c" */
}
}
}
@@ -492,6 +636,8 @@ goto _again;}
_out: {}
}
/* #line 208 "user/ansi_parser.rl" */
/* #line 295 "user/ansi_parser.rl" */
}
// 'ESC k blah OSC_end' is a shortcut for setting title (k is defined in GNU screen as Title Definition String)
+20 -6
View File
@@ -5,18 +5,29 @@
#include <screen.h>
// Max nr of CSI parameters
#define CSI_N_MAX 3
#define CSI_N_MAX 10
#define OSC_CHAR_MAX TERM_TITLE_LEN
extern void apars_handle_badseq(void);
extern void apars_handle_CSI(char leadchar, int *params, char keychar);
extern void apars_handle_RESET_cmd(void);
extern void apars_handle_plainchar(char c);
extern void apars_handle_CSI(char leadchar, int *params, int count, char keychar);
extern void apars_handle_OSC_SetScreenSize(int rows, int cols);
extern void apars_handle_saveCursorAttrs(void);
extern void apars_handle_restoreCursorAttrs(void);
extern void apars_handle_OSC_SetButton(int num, const char *buffer);
extern void apars_handle_OSC_SetTitle(const char *buffer);
extern void apars_handle_shortCode(char c);
extern void apars_handle_hashCode(char c);
extern void apars_handle_characterSet(char leadchar, char c);
extern void apars_handle_setXCtrls(char c);
extern void apars_reset_utf8buffer(void);
// defined in the makefile
#if DEBUG_ANSI
#define ansi_warn warn
#define ansi_dbg dbg
#else
#define ansi_warn(...)
#define ansi_dbg(...)
#endif
/**
* \brief Linear ANSI chars stream parser
@@ -33,4 +44,7 @@ extern void apars_handle_OSC_SetTitle(const char *buffer);
*/
void ansi_parser(const char *newdata, size_t len);
/** This shows a short error message and prints the history (if any) */
void apars_handle_badseq(void);
#endif // ANSI_PARSER_H
+104 -15
View File
@@ -16,10 +16,39 @@ static void ICACHE_FLASH_ATTR
resetParserCb(void *arg) {
if (cs != ansi_start) {
cs = ansi_start;
warn("Parser timeout, state reset");
apars_reset_utf8buffer();
ansi_warn("Parser timeout, state reset");
}
}
#define HISTORY_LEN 16
#if DEBUG_ANSI
static char history[HISTORY_LEN + 1];
#endif
void ICACHE_FLASH_ATTR
apars_handle_badseq(void)
{
#if DEBUG_ANSI
char buf1[HISTORY_LEN*3+2];
char buf2[HISTORY_LEN*3+2];
char *b1 = buf1;
char *b2 = buf2;
char c;
for(int i=0;i<HISTORY_LEN;i++) {
c = history[i];
b1 += sprintf(b1, "%2X ", c);
if (c < 32 || c > 127) c = '.';
b2 += sprintf(b2, "%c ", c);
}
ansi_dbg("Context: %s", buf2);
ansi_dbg(" %s", buf1);
#endif
}
/**
* \brief Linear ANSI chars stream parser
*
@@ -39,6 +68,7 @@ ansi_parser(const char *newdata, size_t len)
// The CSI code is built here
static char csi_leading; //!< Leading char, 0 if none
static int csi_ni; //!< Number of the active digit
static int csi_cnt; //!< Digit count
static int csi_n[CSI_N_MAX]; //!< Param digits
static char csi_char; //!< CSI action char (end)
static char osc_buffer[OSC_CHAR_MAX];
@@ -54,15 +84,26 @@ ansi_parser(const char *newdata, size_t len)
// Init Ragel on the first run
if (cs == -1) {
%% write init;
#if DEBUG_ANSI
memset(history, 0, sizeof(history));
#endif
}
// schedule state reset
// schedule state reset after idle timeout
if (termconf->parser_tout_ms > 0) {
os_timer_disarm(&resetTim);
os_timer_setfn(&resetTim, resetParserCb, NULL);
os_timer_arm(&resetTim, termconf->parser_tout_ms, 0);
}
#if DEBUG_ANSI
for(int i=len; i<HISTORY_LEN; i++) {
history[i-len] = history[i];
}
strcpy(&history[HISTORY_LEN-len], newdata);
#endif
// The parser
%%{
#/*
@@ -74,7 +115,9 @@ ansi_parser(const char *newdata, size_t len)
# --- Regular characters to be printed ---
action plain_char {
apars_handle_plainchar(fc);
if (fc != 0) {
apars_handle_plainchar(fc);
}
}
# --- CSI CSI commands (Select Graphic Rendition) ---
@@ -84,6 +127,7 @@ ansi_parser(const char *newdata, size_t len)
// Reset the CSI builder
csi_leading = csi_char = 0;
csi_ni = 0;
csi_cnt = 0;
// Zero out digits
for(int i = 0; i < CSI_N_MAX; i++) {
@@ -98,6 +142,7 @@ ansi_parser(const char *newdata, size_t len)
}
action CSI_digit {
if (csi_cnt == 0) csi_cnt = 1;
// x10 + digit
if (csi_ni < CSI_N_MAX) {
csi_n[csi_ni] = csi_n[csi_ni]*10 + (fc - '0');
@@ -105,18 +150,19 @@ ansi_parser(const char *newdata, size_t len)
}
action CSI_semi {
if (csi_cnt == 0) csi_cnt = 1; // handle case when first arg is empty
csi_cnt++;
csi_ni++;
}
action CSI_end {
csi_char = fc;
apars_handle_CSI(csi_leading, csi_n, csi_char);
apars_handle_CSI(csi_leading, csi_n, csi_cnt, csi_char);
fgoto main;
}
action errBadSeq {
ansi_warn("Invalid escape sequence discarded.");
apars_handle_badseq();
fgoto main;
}
@@ -127,7 +173,7 @@ ansi_parser(const char *newdata, size_t len)
CSI_body := ((32..47|60..64) @CSI_leading)?
((digit @CSI_digit)* ';' @CSI_semi)*
(digit @CSI_digit)* alpha @CSI_end $!errBadSeq;
(digit @CSI_digit)* (alpha|'`'|'@') @CSI_end $!errBadSeq;
# --- OSC commands (Operating System Commands) ---
@@ -147,6 +193,13 @@ ansi_parser(const char *newdata, size_t len)
fgoto OSC_body;
}
# collecting title string; this can also be entered by ESC k
action SetTitle_start {
osc_bi = 0;
osc_buffer[0] = '\0';
fgoto TITLE_body;
}
action OSC_resize {
apars_handle_OSC_SetScreenSize(csi_n[0], csi_n[1]);
fgoto main;
@@ -167,13 +220,17 @@ ansi_parser(const char *newdata, size_t len)
apars_handle_OSC_SetButton(csi_n[0], osc_buffer);
fgoto main;
}
# 0; is xterm title hack
OSC_body := (
("BTN" digit @CSI_digit '=' (NOESC @OSC_text_char)+ OSC_END @OSC_button) |
("TITLE=" (NOESC @OSC_text_char)+ OSC_END @OSC_title) |
("BTN" digit @CSI_digit '=' (NOESC @OSC_text_char)* OSC_END @OSC_button) |
("TITLE=" @SetTitle_start) |
("0;" (NOESC @OSC_text_char)* OSC_END @OSC_title) |
('W' (digit @CSI_digit)+ ';' @CSI_semi (digit @CSI_digit)+ OSC_END @OSC_resize)
) $!errBadSeq;
TITLE_body := (NOESC @OSC_text_char)* OSC_END @OSC_title $!errBadSeq;
action RESET_cmd {
// Reset screen
apars_handle_RESET_cmd();
@@ -190,16 +247,46 @@ ansi_parser(const char *newdata, size_t len)
fgoto main;
}
action HASH_code {
apars_handle_hashCode(fc);
fgoto main;
}
action SHORT_code {
apars_handle_shortCode(fc);
fgoto main;
}
action SetXCtrls {
apars_handle_setXCtrls(fc);
fgoto main;
}
action CharsetCmd_start {
// abuse the buffer for storing the leading char
osc_buffer[0] = fc;
fgoto charsetcmd_body;
}
action CharsetCmd_end {
apars_handle_characterSet(osc_buffer[0], fc);
fgoto main;
}
charsetcmd_body := (any @CharsetCmd_end) $!errBadSeq;
# --- Main parser loop ---
main :=
(
(NOESC @plain_char)* ESC (
'[' @CSI_start |
']' @OSC_start |
'c' @RESET_cmd |
'7' @CSI_SaveCursorAttrs |
'8' @CSI_RestoreCursorAttrs
('[' @CSI_start) |
(']' @OSC_start) |
('#' digit @HASH_code) |
('k' @SetTitle_start) |
([a-jl-zA-Z0-9=<>] @SHORT_code) |
([()*+-./] @CharsetCmd_start) |
(' ' [FG] @SetXCtrls)
)
)+ $!errBadSeq;
@@ -207,3 +294,5 @@ ansi_parser(const char *newdata, size_t len)
#*/
}%%
}
// 'ESC k blah OSC_end' is a shortcut for setting title (k is defined in GNU screen as Title Definition String)
+305 -98
View File
@@ -9,7 +9,8 @@
#include "screen.h"
#include "ansi_parser.h"
#include "uart_driver.h"
#include "persist.h"
// screen manpage - https://www.gnu.org/software/screen/manual/html_node/Control-Sequences.html
static char utf_collect[4];
static int utf_i = 0;
@@ -21,9 +22,17 @@ static int utf_j = 0;
void ICACHE_FLASH_ATTR
apars_handle_plainchar(char c)
{
if (c == 7) return; // BELL - beep (TODO play beep in browser)
// collecting unicode glyphs...
if (c & 0x80) {
if (utf_i == 0) {
// start
if (c == 192 || c == 193 || c >= 245) {
// forbidden codes
goto fail;
}
if ((c & 0xE0) == 0xC0) {
utf_i = 2;
}
@@ -33,33 +42,68 @@ apars_handle_plainchar(char c)
else if ((c & 0xF8) == 0xF0) {
utf_i = 4;
}
else {
// chars over 127 that don't start unicode sequences
goto fail;
}
utf_collect[0] = c;
utf_j = 1;
}
else {
utf_collect[utf_j++] = c;
if (utf_j >= utf_i) {
screen_putchar(utf_collect);
utf_i = 0;
utf_j = 0;
memset(utf_collect, 0, 4);
if ((c & 0xC0) != 0x80) {
goto fail;
}
else {
utf_collect[utf_j++] = c;
if (utf_j >= utf_i) {
screen_putchar(utf_collect);
apars_reset_utf8buffer();
}
}
}
}
else {
if (c == 14) {
screen_set_charset_n(1);
return;
}
if (c == 15) {
screen_set_charset_n(0);
return;
}
utf_collect[0] = c;
utf_collect[1] = 0; // just to make sure it's closed...
screen_putchar(utf_collect);
}
return;
fail:
ansi_warn("Bad UTF-8");
apars_reset_utf8buffer();
}
/**
* Bad sequence received, show warning
*/
void apars_handle_badseq(void)
void ICACHE_FLASH_ATTR
apars_reset_utf8buffer(void)
{
warn("Invalid escape sequence discarded.");
utf_i = 0;
utf_j = 0;
memset(utf_collect, 0, 4);
}
void ICACHE_FLASH_ATTR
apars_handle_characterSet(char leadchar, char c)
{
if (leadchar == '(') screen_set_charset(0, c);
else if (leadchar == ')') screen_set_charset(1, c);
}
void ICACHE_FLASH_ATTR
apars_handle_setXCtrls(char c)
{
// this does not seem to do anything, sent by some unix programs
// ansi_warn("NOIMPL Select %cbit ctrls", c=='F'? '7':'8');
}
/**
@@ -69,47 +113,31 @@ void apars_handle_badseq(void)
* \param keychar - the char terminating the sequence
*/
void ICACHE_FLASH_ATTR
apars_handle_CSI(char leadchar, int *params, char keychar)
apars_handle_CSI(char leadchar, int *params, int count, char keychar)
{
/*
Implemented codes (from Wikipedia)
CSI n A CUU Cursor Up
CSI n B CUD Cursor Down
CSI n C CUF Cursor Forward
CSI n D CUB Cursor Back
CSI n E CNL Cursor Next Line
CSI n F CPL Cursor Previous Line
CSI n G CHA Cursor Horizontal Absolute
CSI n ; m H CUP Cursor Position
CSI n J ED Erase Display
CSI n K EL Erase in Line
CSI n S SU Scroll Up
CSI n T SD Scroll Down
CSI n ; m f HVP Horizontal and Vertical Position
CSI n m SGR Select Graphic Rendition (Implemented only some)
CSI 6n DSR Device Status Report
CSI s SCP Save Cursor Position
CSI u RCP Restore Cursor Position
CSI ?25l DECTCEM Hides the cursor
CSI ?25h DECTCEM Shows the cursor
*/
int n1 = params[0];
int n2 = params[1];
// int n3 = params[2];
int n3 = params[2];
static char buf[20];
// defaults
switch (keychar) {
case 'A':
case 'A': // move
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'S':
case 'G': // set X
case '`':
case 'S': // scrolling
case 'T':
case 'X': // clear in line
case 'd': // set Y
case 'L':
case 'M':
case '@':
case 'P':
if (n1 == 0) n1 = 1;
break;
@@ -127,60 +155,73 @@ apars_handle_CSI(char leadchar, int *params, char keychar)
switch (keychar) {
// CUU CUD CUF CUB
case 'A': screen_cursor_move(-n1, 0); break;
case 'B': screen_cursor_move(n1, 0); break;
case 'C': screen_cursor_move(0, n1); break;
case 'D': screen_cursor_move(0, -n1); break;
case 'A': screen_cursor_move(-n1, 0, false); break;
case 'B': screen_cursor_move(n1, 0, false); break;
case 'C': screen_cursor_move(0, n1, false); break;
case 'D': screen_cursor_move(0, -n1, false); break;
case 'E': // CNL
screen_cursor_move(n1, 0);
case 'E': // CNL - Cursor Next Line
screen_cursor_move(n1, 0, false);
screen_cursor_set_x(0);
break;
case 'F': // CPL
screen_cursor_move(-n1, 0);
case 'F': // CPL - Cursor Prev Line
screen_cursor_move(-n1, 0, false);
screen_cursor_set_x(0);
break;
// CHA
// Set X
case 'G':
screen_cursor_set_x(n1 - 1); break; // 1-based
case '`': // alternate code
screen_cursor_set_x(n1 - 1);
break; // 1-based
// SU, SD
// Set Y
case 'd':
screen_cursor_set_y(n1 - 1);
break; // 1-based
// clear in line
case 'X':
screen_clear_in_line(n1);
break; // 1-based
// SU, SD - scroll up/down
case 'S': screen_scroll_up(n1); break;
case 'T': screen_scroll_down(n1); break;
// CUP,HVP
// CUP,HVP - set position
case 'H':
case 'f':
screen_cursor_set(n1-1, n2-1); break; // 1-based
screen_cursor_set(n1-1, n2-1);
break; // 1-based
case 'J': // ED
case 'J': // ED - clear screen
if (n1 == 0) {
screen_clear(CLEAR_TO_CURSOR);
} else if (n1 == 1) {
screen_clear(CLEAR_FROM_CURSOR);
} else if (n1 == 1) {
screen_clear(CLEAR_TO_CURSOR);
} else {
screen_clear(CLEAR_ALL);
screen_cursor_set(0, 0);
}
break;
case 'K': // EL
case 'K': // EL - clear line
if (n1 == 0) {
screen_clear_line(CLEAR_TO_CURSOR);
} else if (n1 == 1) {
screen_clear_line(CLEAR_FROM_CURSOR);
} else if (n1 == 1) {
screen_clear_line(CLEAR_TO_CURSOR);
} else {
screen_clear_line(CLEAR_ALL);
}
break;
// SCP, RCP
// SCP, RCP - save/restore position
case 's': screen_cursor_save(0); break;
case 'u': screen_cursor_restore(0); break;
case 'n':
case 'n': // queries
if (n1 == 6) {
// Query cursor position
char buf[20];
@@ -193,71 +234,237 @@ apars_handle_CSI(char leadchar, int *params, char keychar)
// Query device status - reply "Device is OK"
UART_WriteString(UART0, "\033[0n", UART_TIMEOUT_US);
}
else {
ansi_warn("NOIMPL query %d", n1);
}
break;
// DECTCEM feature enable / disable
case 'h':
case 'h': // feature enable
if (leadchar == '?') {
if (n1 == 25) {
screen_cursor_enable(1);
} else if (n1 == 7) {
screen_wrap_enable(1);
if (n1 == 25) screen_cursor_enable(1);
else if (n1 == 7) screen_wrap_enable(1);
else if (n1 == 1) {
// TODO something with arrow keys??
}
else {
ansi_warn("NOIMPL DEC opt %d", n1);
}
}
else {
if (n1 == 4) {
// TODO insert mode, i think this is to suppress user input
}
else {
ansi_warn("NOIMPL flag %d", n1);
}
}
break;
case 'l':
case 'l': // feature disable
if (leadchar == '?') {
if (n1 == 25) {
screen_cursor_enable(0);
} else if (n1 == 7) {
screen_wrap_enable(0);
if (n1 == 25) screen_cursor_enable(0);
else if (n1 == 7) screen_wrap_enable(0);
else if (n1 == 1) {
// TODO see above
}
else {
ansi_warn("NOIMPL DEC opt %d", n1);
}
}
else {
if (n1 == 4) {
// TODO see above
}
else {
ansi_warn("NOIMPL flag %d", n1);
}
}
break;
case 'm': // SGR
case 'm': // SGR - graphics rendition aka attributes
if (count == 0) {
count = 1; // this makes it work as 0 (reset)
}
// iterate arguments
for (int i = 0; i < CSI_N_MAX; i++) {
for (int i = 0; i < count; i++) {
int n = params[i];
if (i == 0 && n == 0) { // reset SGR
if (n == 0) { // reset SGR
screen_reset_cursor(); // resets colors, inverse and bold.
break; // cannot combine reset with others
}
else if (n >= 30 && n <= 37) screen_set_fg(n-30); // ANSI normal fg
else if (n >= 40 && n <= 47) screen_set_bg(n-40); // ANSI normal bg
else if (n == 39) screen_set_fg(7); // default fg
else if (n == 49) screen_set_bg(false); // default bg
else if (n == 7) screen_inverse(true); // inverse
else if (n == 27) screen_inverse(false); // positive
else if (n == 1) screen_set_bold(true); // bold
else if (n == 21 || n == 22) screen_set_bold(false); // bold off
else if (n >= 90 && n <= 97) screen_set_fg(n-90+8); // AIX bright fg
else if (n >= 100 && n <= 107) screen_set_bg(n-100+8); // AIX bright bg
else if (n >= 30 && n <= 37) screen_set_fg((Color) (n - 30)); // ANSI normal fg
else if (n >= 40 && n <= 47) screen_set_bg((Color) (n - 40)); // ANSI normal bg
else if (n == 39) screen_set_fg(termconf_scratch.default_fg); // default fg
else if (n == 49) screen_set_bg(termconf_scratch.default_bg); // default bg
else if (n == 1) screen_attr_enable(ATTR_BOLD);
else if (n == 2) screen_attr_enable(ATTR_FAINT);
else if (n == 3) screen_attr_enable(ATTR_ITALIC);
else if (n == 4) screen_attr_enable(ATTR_UNDERLINE);
else if (n == 5 || n == 6) screen_attr_enable(ATTR_BLINK); // 6 - rapid blink, not supported
else if (n == 7) screen_inverse_enable(true);
else if (n == 9) screen_attr_enable(ATTR_STRIKE);
else if (n == 20) screen_attr_enable(ATTR_FRAKTUR);
else if (n == 21) screen_attr_disable(ATTR_BOLD);
else if (n == 22) screen_attr_disable(ATTR_FAINT);
else if (n == 23) screen_attr_disable(ATTR_ITALIC|ATTR_FRAKTUR);
else if (n == 24) screen_attr_disable(ATTR_UNDERLINE);
else if (n == 25) screen_attr_disable(ATTR_BLINK);
else if (n == 27) screen_inverse_enable(false);
else if (n == 29) screen_attr_disable(ATTR_STRIKE);
else if (n >= 90 && n <= 97) screen_set_fg((Color) (n - 90 + 8)); // AIX bright fg
else if (n >= 100 && n <= 107) screen_set_bg((Color) (n - 100 + 8)); // AIX bright bg
else {
ansi_warn("NOIMPL SGR attr %d", n);
}
}
break;
case 't': // xterm hacks
switch(n1) {
case 8: // set size
screen_resize(n2, n3);
break;
case 18: // report size
printf(buf, "\033[8;%d;%dt", termconf_scratch.height, termconf_scratch.width);
UART_WriteString(UART0, buf, UART_TIMEOUT_US);
break;
case 11: // Report iconified -> is not iconified
UART_WriteString(UART0, "\033[1t", UART_TIMEOUT_US);
break;
case 21: // Report title
UART_WriteString(UART0, "\033]L", UART_TIMEOUT_US);
UART_WriteString(UART0, termconf_scratch.title, UART_TIMEOUT_US);
UART_WriteString(UART0, "\033\\", UART_TIMEOUT_US);
break;
case 24: // Set Height only
screen_resize(n2, termconf_scratch.width);
break;
}
break;
case 'L':
screen_insert_lines(n1);
break;
case 'M':
screen_delete_lines(n1);
break;
case '@':
screen_insert_characters(n1);
break;
case 'P':
screen_delete_characters(n1);
break;
case 'r':
// TODO scrolling region
// ansi_warn("NOIMPL scrolling region");
break;
case 'g':
// TODO clear tab
ansi_warn("NOIMPL clear tab");
break;
case 'Z':
// TODO back tab
ansi_warn("NOIMPL cursor back tab");
break;
case 'q':
// TODO setmode (??)
ansi_warn("NOIMPL CSI setmode %d", n1);
break;
case 'p':
if (leadchar == '!') {
info("SOFT RESET!");
system_restart();
}
break;
case 'c':
// report capabilities (pretend we're vt4xx)
UART_WriteString(UART0, "\033[?64;22;c", UART_TIMEOUT_US);
break;
default:
ansi_warn("Unknown CSI: %c", keychar);
apars_handle_badseq();
}
}
void ICACHE_FLASH_ATTR apars_handle_saveCursorAttrs(void)
/** codes in the format ESC # n */
void ICACHE_FLASH_ATTR apars_handle_hashCode(char c)
{
screen_cursor_save(1);
switch(c) {
case '8':
screen_fill_with_E();
break;
default:
ansi_warn("Unknown # sequence: %c", c);
}
}
void ICACHE_FLASH_ATTR apars_handle_restoreCursorAttrs(void)
/** those are single-character escape codes (ESC x) */
void ICACHE_FLASH_ATTR apars_handle_shortCode(char c)
{
screen_cursor_restore(1);
}
switch(c) {
case 'c': // screen reset
screen_reset();
break;
/**
* \brief Handle a request to reset the display device
*/
void ICACHE_FLASH_ATTR
apars_handle_RESET_cmd(void)
{
screen_reset();
case '7': // save cursor + attrs
screen_cursor_save(true);
break;
case '8': // restore cursor + attrs
screen_cursor_restore(true);
break;
case 'E': // same as CR LF
screen_cursor_move(1, 0, false);
screen_cursor_set_x(0);
break;
case 'D': // move cursor down, scroll screen up if needed
screen_cursor_move(1, 0, true);
break;
case 'M': // move cursor up, scroll screen down if needed
screen_cursor_move(-1, 0, true);
break;
case 'H':
// TODO set tab
// ansi_warn("NOIMPL set tab");
break;
// TODO those don't seem to do anything
case '>':
// ansi_warn("NOIMPL NUMKP");
break;
case '<':
// ansi_warn("NOIMPL SETANSI");
break;
case '=':
// ansi_warn("NOIMPL ALTKP");
break;
default:
ansi_warn("Unknown 1-char seq %c", c);
}
}
/**
+6 -5
View File
@@ -1,14 +1,15 @@
#ifndef ANSI_PARSER_CALLBACKS_H
#define ANSI_PARSER_CALLBACKS_H
void apars_handle_badseq(void);
void apars_handle_CSI(char leadchar, int *params, char keychar);
void apars_handle_RESET_cmd(void);
void apars_handle_plainchar(char c);
void apars_handle_CSI(char leadchar, int *params, int count, char keychar);
void apars_handle_OSC_SetScreenSize(int rows, int cols);
void apars_handle_saveCursorAttrs(void);
void apars_handle_restoreCursorAttrs(void);
void apars_handle_OSC_SetButton(int num, const char *buffer);
void apars_handle_OSC_SetTitle(const char *buffer);
void apars_handle_shortCode(char c);
void apars_handle_hashCode(char c);
void apars_handle_characterSet(char leadchar, char c);
void apars_handle_setXCtrls(char c);
void apars_reset_utf8buffer(void);
#endif //ESP_VT100_FIRMWARE_ANSI_PARSER_CALLBACKS_H
+5 -5
View File
@@ -98,7 +98,7 @@ void ICACHE_FLASH_ATTR updateSockRx(Websock *ws, char *data, int len, int flags)
// TODO re-implement those, use single byte markers and B2 encoding
dbg("Sock RX str: %s, len %d", data, len);
ws_dbg("Sock RX str: %s, len %d", data, len);
if (strstarts(data, "STR:")) {
// pass string verbatim
@@ -134,24 +134,24 @@ void ICACHE_FLASH_ATTR updateSockRx(Websock *ws, char *data, int len, int flags)
}
if (!screen_isCoordValid(y, x)) {
warn("Mouse input at invalid coordinates");
ws_warn("Mouse input at invalid coordinates");
return;
}
dbg("Screen clicked at row %d, col %d", y+1, x+1);
ws_dbg("Screen clicked at row %d, col %d", y+1, x+1);
// Send as 1-based to user
sprintf(buf, "\033[%d;%dM", y+1, x+1);
UART_WriteString(UART0, buf, UART_TIMEOUT_US);
}
else {
warn("Bad command.");
ws_warn("Bad command.");
}
}
/** Socket connected for updates */
void ICACHE_FLASH_ATTR updateSockConnect(Websock *ws)
{
info("Socket connected to "URL_WS_UPDATE);
ws_info("Socket connected to "URL_WS_UPDATE);
ws->recvCb = updateSockRx;
}
+11
View File
@@ -9,4 +9,15 @@
void updateSockConnect(Websock *ws);
void screen_notifyChange(ScreenNotifyChangeTopic topic);
// defined in the makefile
#if DEBUG_INPUT
#define ws_warn warn
#define ws_dbg dbg
#define ws_info info
#else
#define ws_warn(...)
#define ws_dbg(...)
#define ws_info(...)
#endif
#endif //CGI_SOCKETS_H
+21 -5
View File
@@ -22,6 +22,8 @@ cgiTermCfgSetParams(HttpdConnData *connData)
char redir_url_buf[100];
int n, w, h;
bool shall_clear_screen = false;
char *redir_url = redir_url_buf;
redir_url += sprintf(redir_url, SET_REDIR_ERR);
// we'll test if anything was printed by looking for \0 in failed_keys_buf
@@ -41,8 +43,11 @@ cgiTermCfgSetParams(HttpdConnData *connData)
h = atoi(buff);
if (h > 1) {
if (w * h <= MAX_SCREEN_SIZE) {
termconf->width = w;
termconf->height = h;
if (termconf->width != w || termconf->height != h) {
termconf->width = w;
termconf->height = h;
shall_clear_screen = true;
}
} else {
warn("Bad dimensions: %d x %d (total %d)", w, h, w*h);
redir_url += sprintf(redir_url, "term_width,term_height,");
@@ -66,7 +71,10 @@ cgiTermCfgSetParams(HttpdConnData *connData)
dbg("Screen default BG: %s", buff);
n = atoi(buff);
if (n >= 0 && n < 16) {
termconf->default_bg = (u8) n;
if (termconf->default_bg != n) {
termconf->default_bg = (u8) n;
shall_clear_screen = true;
}
} else {
warn("Bad color %s", buff);
redir_url += sprintf(redir_url, "default_bg,");
@@ -88,7 +96,10 @@ cgiTermCfgSetParams(HttpdConnData *connData)
dbg("Screen default FG: %s", buff);
n = atoi(buff);
if (n >= 0 && n < 16) {
termconf->default_fg = (u8) n;
if (termconf->default_fg != n) {
termconf->default_fg = (u8) n;
shall_clear_screen = true;
}
} else {
warn("Bad color %s", buff);
redir_url += sprintf(redir_url, "default_fg,");
@@ -123,9 +134,14 @@ cgiTermCfgSetParams(HttpdConnData *connData)
// All was OK
info("Set term params - success, saving...");
terminal_apply_settings();
persist_store();
if (shall_clear_screen) {
terminal_apply_settings();
} else {
terminal_apply_settings_noclear();
}
httpdRedirect(connData, SET_REDIR_SUC);
} else {
warn("Some settings did not validate, asking for correction");
+310 -82
View File
@@ -8,6 +8,9 @@
TerminalConfigBundle * const termconf = &persist.current.termconf;
TerminalConfigBundle termconf_scratch;
// forward declare
static void utf8_remap(char* out, char g, char table);
#define W termconf_scratch.width
#define H termconf_scratch.height
@@ -31,6 +34,12 @@ void terminal_restore_defaults(void)
* Apply settings after eg. restore from defaults
*/
void terminal_apply_settings(void)
{
terminal_apply_settings_noclear();
screen_init();
}
void terminal_apply_settings_noclear(void)
{
memcpy(&termconf_scratch, termconf, sizeof(TerminalConfigBundle));
if (W*H >= MAX_SCREEN_SIZE) {
@@ -39,8 +48,8 @@ void terminal_apply_settings(void)
terminal_restore_defaults();
persist_store();
memcpy(&termconf_scratch, termconf, sizeof(TerminalConfigBundle));
screen_init();
}
screen_init();
}
/**
@@ -55,7 +64,7 @@ typedef struct __attribute__((packed)){
char c[4]; // space for a full unicode character
Color fg : 4;
Color bg : 4;
bool bold : 1;
u8 attrs;
} Cell;
/**
@@ -69,10 +78,15 @@ static Cell screen[MAX_SCREEN_SIZE];
static struct {
int x; //!< X coordinate
int y; //!< Y coordinate
bool visible; //!< Visible
bool inverse; //!< Inverse colors
bool autowrap; //!< Wrapping when EOL
bool bold; //!< Bold style
bool visible; //!< Visible (not attribute, DEC special)
bool inverse;
u8 attrs;
char charset0;
char charset1;
int charsetN;
Color fg; //!< Foreground color for writing
Color bg; //!< Background color for writing
} cursor;
@@ -83,10 +97,10 @@ static struct {
static struct {
int x;
int y;
// optionally saved attrs
// mark that attrs are saved
bool withAttrs;
bool inverse;
u8 attrs;
bool inverse; // attribute that's not in the bitfield
Color fg;
Color bg;
} cursor_sav;
@@ -114,16 +128,20 @@ static inline void
clear_range(unsigned int from, unsigned int to)
{
if (to >= W*H) to = W*H-1;
Color fg = cursor.inverse ? cursor.bg : cursor.fg;
Color bg = cursor.inverse ? cursor.fg : cursor.bg;
Color fg = (cursor.inverse) ? cursor.bg : cursor.fg;
Color bg = (cursor.inverse) ? cursor.fg : cursor.bg;
Cell sample;
sample.c[0] = ' ';
sample.c[1] = 0;
sample.c[2] = 0;
sample.c[3] = 0;
sample.fg = fg;
sample.bg = bg;
sample.attrs = 0;
for (unsigned int i = from; i <= to; i++) {
screen[i].c[0] = ' ';
screen[i].c[1] = 0;
screen[i].c[2] = 0;
screen[i].c[3] = 0;
screen[i].fg = fg;
screen[i].bg = bg;
screen[i].bold = false;
memcpy(&screen[i], &sample, sizeof(Cell));
}
}
@@ -138,9 +156,12 @@ cursor_reset(void)
cursor.fg = termconf_scratch.default_fg;
cursor.bg = termconf_scratch.default_bg;
cursor.visible = 1;
cursor.inverse = 0;
cursor.autowrap = 1;
cursor.bold = 0;
cursor.attrs = 0;
cursor.charset0 = 'B';
cursor.charset1 = '0';
cursor.charsetN = 0;
}
//endregion
@@ -176,12 +197,10 @@ screen_reset(void)
void ICACHE_FLASH_ATTR
screen_reset_cursor(void)
{
NOTIFY_LOCK();
cursor.fg = termconf_scratch.default_fg;
cursor.bg = termconf_scratch.default_bg;
cursor.inverse = 0;
cursor.bold = 0;
NOTIFY_DONE();
cursor.attrs = 0;
cursor.inverse = false;
}
/**
@@ -230,10 +249,121 @@ screen_clear_line(ClearMode mode)
NOTIFY_DONE();
}
void ICACHE_FLASH_ATTR
screen_clear_in_line(unsigned int count)
{
if (cursor.x + count >= W) {
screen_clear_line(CLEAR_FROM_CURSOR);
}
else {
NOTIFY_LOCK();
clear_range(cursor.y * W + cursor.x, cursor.y * W + cursor.x + count - 1);
NOTIFY_DONE();
}
}
void ICACHE_FLASH_ATTR
screen_fill_with_E(void)
{
NOTIFY_LOCK();
Cell sample;
sample.c[0] = 'E';
sample.c[1] = 0;
sample.c[2] = 0;
sample.c[3] = 0;
sample.fg = termconf_scratch.default_fg;
sample.bg = termconf_scratch.default_bg;
sample.attrs = 0;
for (unsigned int i = 0; i <= W*H-1; i++) {
memcpy(&screen[i], &sample, sizeof(Cell));
}
NOTIFY_DONE();
}
//endregion
//region Screen manipulation
void screen_insert_lines(unsigned int lines)
{
NOTIFY_LOCK();
// shift the following lines
int targetStart = cursor.y + lines;
if (targetStart >= H) {
targetStart = H;
} else {
// do the moving
for (int i = H-1; i >= targetStart; i--) {
memcpy(screen+i*W, screen+(i-lines)*W, sizeof(Cell)*W);
}
}
// clear the first line
screen_clear_line(CLEAR_ALL);
// copy it to the rest of the cleared region
for (int i = cursor.y+1; i < targetStart; i++) {
memcpy(screen+i*W, screen+cursor.y*W, sizeof(Cell)*W);
}
NOTIFY_DONE();
}
void screen_delete_lines(unsigned int lines)
{
NOTIFY_LOCK();
// shift lines up
int targetEnd = H - 1 - lines;
if (targetEnd <= cursor.y) {
targetEnd = cursor.y;
} else {
// do the moving
for (int i = cursor.y; i <= targetEnd; i++) {
memcpy(screen+i*W, screen+(i+lines)*W, sizeof(Cell)*W);
}
}
// clear the rest
clear_range(targetEnd*W, W*H-1);
NOTIFY_DONE();
}
void screen_insert_characters(unsigned int count)
{
NOTIFY_LOCK();
int targetStart = cursor.x + count;
if (targetStart >= W) {
targetStart = W-1;
} else {
// do the moving
for (int i = W-1; i >= targetStart; i--) {
memcpy(screen+cursor.y*W+i, screen+cursor.y*W+(i-count), sizeof(Cell));
}
}
clear_range(cursor.y*W+cursor.x, cursor.y*W+targetStart-1);
NOTIFY_DONE();
}
void screen_delete_characters(unsigned int count)
{
NOTIFY_LOCK();
int targetEnd = W - count;
if (targetEnd > cursor.x) {
// do the moving
for (int i = cursor.x; i <= targetEnd; i++) {
memcpy(screen+cursor.y*W+i, screen+cursor.y*W+(i+count), sizeof(Cell));
}
}
if (targetEnd <= cursor.x) {
screen_clear_line(CLEAR_FROM_CURSOR);
}
else {
clear_range(cursor.y * W + (W - count), (cursor.y + 1) * W - 1);
}
NOTIFY_DONE();
}
/**
* Change the screen size
*
@@ -373,7 +503,7 @@ screen_cursor_set_y(int y)
* Relative cursor move
*/
void ICACHE_FLASH_ATTR
screen_cursor_move(int dy, int dx)
screen_cursor_move(int dy, int dx, bool scroll)
{
NOTIFY_LOCK();
int move;
@@ -386,13 +516,13 @@ screen_cursor_move(int dy, int dx)
if (cursor.y < 0) {
move = -cursor.y;
cursor.y = 0;
screen_scroll_down((unsigned int)move);
if (scroll) screen_scroll_down((unsigned int)move);
}
if (cursor.y >= H) {
move = cursor.y - (H - 1);
cursor.y = H - 1;
screen_scroll_up((unsigned int)move);
if (scroll) screen_scroll_up((unsigned int)move);
}
NOTIFY_DONE();
@@ -412,11 +542,11 @@ screen_cursor_save(bool withAttrs)
if (withAttrs) {
cursor_sav.fg = cursor.fg;
cursor_sav.bg = cursor.bg;
cursor_sav.inverse = cursor.inverse;
cursor_sav.attrs = cursor.attrs;
} else {
cursor_sav.fg = termconf_scratch.default_fg;
cursor_sav.bg = termconf_scratch.default_bg;
cursor_sav.inverse = 0;
cursor_sav.attrs = 0; // avoid leftovers if the wrong restore is used
}
}
@@ -433,7 +563,7 @@ screen_cursor_restore(bool withAttrs)
if (withAttrs) {
cursor.fg = cursor_sav.fg;
cursor.bg = cursor_sav.bg;
cursor.inverse = cursor_sav.inverse;
cursor.attrs = cursor_sav.attrs;
}
NOTIFY_DONE();
@@ -485,43 +615,19 @@ screen_set_bg(Color color)
cursor.bg = color;
}
/**
* Set cursor foreground and background color
*/
void ICACHE_FLASH_ATTR
screen_set_colors(Color fg, Color bg)
void screen_attr_enable(u8 attrs)
{
screen_set_fg(fg);
screen_set_bg(bg);
cursor.attrs |= attrs;
}
/**
* Invert colors
*/
void ICACHE_FLASH_ATTR
screen_inverse(bool inverse)
void screen_attr_disable(u8 attrs)
{
cursor.inverse = inverse;
cursor.attrs &= ~attrs;
}
/**
* Make foreground bright.
*
* This relates to the '1' SGR command which originally means
* "bold font". We interpret that as "Bright", similar to other
* terminal emulators.
*
* Note that the bright colors can be used without bold using the 90+ codes
*/
void ICACHE_FLASH_ATTR
screen_set_bold(bool bold)
void screen_inverse_enable(bool ena)
{
if (!bold) {
cursor.fg = (Color) (cursor.fg % 8);
} else {
cursor.fg = (Color) ((cursor.fg % 8) + 8); // change anything to the bright colors
}
cursor.bold = bold;
cursor.inverse = ena;
}
//endregion
@@ -538,12 +644,26 @@ bool ICACHE_FLASH_ATTR screen_isCoordValid(int y, int x)
return x >= 0 && y >= 0 && x < W && y < H;
}
void ICACHE_FLASH_ATTR screen_set_charset_n(int Gx)
{
if (Gx < 0 || Gx > 1) return; // bad n
cursor.charsetN = Gx;
}
void ICACHE_FLASH_ATTR screen_set_charset(int Gx, char charset)
{
if (Gx == 0) cursor.charset0 = charset;
if (Gx == 1) cursor.charset1 = charset;
}
/**
* Set a character in the cursor color, move to right with wrap.
*/
void ICACHE_FLASH_ATTR
screen_putchar(const char *ch)
{
char buf[4];
NOTIFY_LOCK();
Cell *c = &screen[cursor.x + cursor.y * W];
@@ -555,7 +675,7 @@ screen_putchar(const char *ch)
goto done;
case '\n':
screen_cursor_move(1, 0);
screen_cursor_move(1, 0, true); // can scroll
goto done;
case 8: // BS
@@ -568,15 +688,11 @@ screen_putchar(const char *ch)
cursor.y--;
}
}
// erase target cell
c = &screen[cursor.x + cursor.y * W];
c->c[0] = ' ';
c->c[1] = 0;
c->c[2] = 0;
c->c[3] = 0;
// apparently backspace should not clear the cell
goto done;
case 9: // TAB
// TODO change if tab setting is ever implemented
if (cursor.x<((W-1)-(W-1)%4)) {
c->c[0] = ' ';
c->c[1] = 0;
@@ -591,13 +707,19 @@ screen_putchar(const char *ch)
default:
if (ch[0] < ' ') {
// Discard
warn("Ignoring control char %d", (int)ch);
warn("Ignoring control char %d", (int)ch[0]);
goto done;
}
}
// copy unicode char
strncpy(c->c, ch, 4);
if (ch[1] == 0 && ch[0] <= 0x7f) {
// we have len=1 and ASCII
utf8_remap(c->c, ch[0], (cursor.charsetN == 0) ? cursor.charset0 : cursor.charset1);
}
else {
// copy unicode char
strncpy(c->c, ch, 4);
}
if (cursor.inverse) {
c->fg = cursor.bg;
@@ -606,7 +728,7 @@ screen_putchar(const char *ch)
c->fg = cursor.fg;
c->bg = cursor.bg;
}
c->bold = cursor.bold;
c->attrs = cursor.attrs;
cursor.x++;
// X wrap
@@ -629,6 +751,84 @@ done:
NOTIFY_DONE();
}
/**
* translates VT100 ACS escape codes to Unicode values.
* Based on rxvt-unicode screen.C table.
*/
static const u16 vt100_to_unicode[62] =
{
// ? ? ? ? ? ? ?
// A=UPARR B=DNARR C=RTARR D=LFARR E=FLBLK F=3/4BL G=SNOMN
0x2191, 0x2193, 0x2192, 0x2190, 0x2588, 0x259a, 0x2603,
// H= I= J= K= L= M= N=
0, 0, 0, 0, 0, 0, 0,
// O= P= Q= R= S= T= U=
0, 0, 0, 0, 0, 0, 0,
// V= W= X= Y= Z= [= \=
0, 0, 0, 0, 0, 0, 0,
// ? ? v->0 v->1 v->2 v->3 v->4
// ]= ^= _=SPC `=DIAMN a=HSMED b=HT c=FF
0, 0, 0x0020, 0x25c6, 0x2592, 0x2409, 0x240c,
// v->5 v->6 v->7 v->8 v->9 v->a v->b
// d=CR e=LF f=DEGRE g=PLSMN h=NL i=VT j=SL-BR
0x240d, 0x240a, 0x00b0, 0x00b1, 0x2424, 0x240b, 0x2518,
// v->c v->d v->e v->f v->10 v->11 v->12
// k=SL-TR l=SL-TL m=SL-BL n=SL-+ o=SL-T1 p=SL-T2 q=SL-HZ
0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, 0x23bb, 0x2500,
// v->13 v->14 v->15 v->16 v->17 v->18 v->19
// r=SL-T4 s=SL-T5 t=SL-VR u=SL-VL v=SL-HU w=Sl-HD x=SL-VT
0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, 0x2502,
// v->1a v->1b b->1c v->1d v->1e/a3 v->1f
// y=LT-EQ z=GT-EQ {=PI |=NOTEQ }=POUND ~=DOT
0x2264, 0x2265, 0x03c0, 0x2260, 0x20a4, 0x00b7
};
/**
* UTF remap
* @param out - output char[4]
* @param g - ASCII char
* @param table - table name (0, A, B)
*/
static void ICACHE_FLASH_ATTR
utf8_remap(char *out, char g, char table)
{
u16 utf = 0;
switch (table)
{
case '0': /* DEC Special Character & Line Drawing Set */
if ((g >= 0x41) && (g <= 0x7e) && (vt100_to_unicode[g - 0x41])) {
utf = vt100_to_unicode[g - 0x41];
}
break;
case 'A': /* UK, replaces # with GBP */
if (g == '#') utf = 0x20a4;
break;
}
if (utf > 0x7F) {
// formulas taken from: https://gist.github.com/yamamushi/5823402
if ((utf >= 0x80) && (utf <= 0x07FF)) {
out[0] = (char) ((utf >> 0x06) ^ 0xC0);
out[1] = (char) (((utf ^ 0xFFC0) | 0x80) & ~0x40);
out[2]=0;
}
else if ((utf >= 0x0800) && (utf <= 0xFFFF)) {
out[0] = (char) (((utf ^ 0xFC0FFF) >> 0x0C) | 0xE0);
out[1] = (char) ((((utf ^ 0xFFF03F) >> 0x06) | 0x80) & ~0x40);
out[2] = (char) (((utf ^ 0xFFFC0) | 0x80) & ~0x40);
out[3]=0;
} else {
out[0] = g;
out[1] = 0;
}
} else {
out[0] = g;
out[1] = 0;
}
}
//region Serialization
@@ -670,7 +870,7 @@ void screen_dd(void)
struct ScreenSerializeState {
Color lastFg;
Color lastBg;
bool lastBold;
bool lastAttrs;
char lastChar[4];
int index;
};
@@ -679,8 +879,24 @@ void ICACHE_FLASH_ATTR
encode2B(u16 number, WordB2 *stru)
{
stru->lsb = (u8) (number % 127);
stru->msb = (u8) ((number - stru->lsb) / 127 + 1);
number = (u16) ((number - stru->lsb) / 127);
stru->lsb += 1;
stru->msb = (u8) (number + 1);
}
void ICACHE_FLASH_ATTR
encode3B(u32 number, WordB3 *stru)
{
stru->lsb = (u8) (number % 127);
number = (number - stru->lsb) / 127;
stru->lsb += 1;
stru->msb = (u8) (number % 127);
number = (number - stru->msb) / 127;
stru->msb += 1;
stru->xsb = (u8) (number + 1);
}
/**
@@ -726,6 +942,7 @@ screenSerializeToBuffer(char *buffer, size_t buf_len, void **data)
Cell *cell, *cell0;
WordB2 w1, w2, w3, w4, w5;
WordB3 lw1;
size_t remain = buf_len; int used = 0;
char *bb = buffer;
@@ -741,7 +958,7 @@ screenSerializeToBuffer(char *buffer, size_t buf_len, void **data)
ss->index = 0;
ss->lastBg = 0;
ss->lastFg = 0;
ss->lastBold = false;
ss->lastAttrs = 0;
memset(ss->lastChar, 0, 4); // this ensures the first char is never "repeat"
encode2B((u16) H, &w1);
@@ -751,8 +968,7 @@ screenSerializeToBuffer(char *buffer, size_t buf_len, void **data)
encode2B((u16) (
cursor.fg |
(cursor.bg<<4) |
(cursor.bold?0x100:0) |
(cursor.visible?0x200:0))
(cursor.visible ? 1<<8 : 0))
, &w5);
// H W X Y Attribs
@@ -768,7 +984,7 @@ screenSerializeToBuffer(char *buffer, size_t buf_len, void **data)
while (i < W*H
&& cell->fg == ss->lastFg
&& cell->bg == ss->lastBg
&& cell->bold == ss->lastBold
&& cell->attrs == ss->lastAttrs
&& strneq(cell->c, ss->lastChar, 4)) {
// Repeat
repCnt++;
@@ -777,13 +993,25 @@ screenSerializeToBuffer(char *buffer, size_t buf_len, void **data)
if (repCnt == 0) {
// No repeat
if (cell0->bold != ss->lastBold || cell0->fg != ss->lastFg || cell0->bg != ss->lastBg) {
encode2B((u16) (
bool changeAttrs = cell0->attrs != ss->lastAttrs;
bool changeColors = (cell0->fg != ss->lastFg || cell0->bg != ss->lastBg);
if (!changeAttrs && changeColors) {
encode2B(cell0->fg | (cell0->bg<<4), &w1);
bufprint("\x03%c%c", w1.lsb, w1.msb);
}
else if (changeAttrs && !changeColors) {
// attrs only
encode2B(cell0->attrs, &w1);
bufprint("\x04%c%c", w1.lsb, w1.msb);
}
else if (changeAttrs && changeColors) {
// colors and attrs
encode3B((u32) (
cell0->fg |
(cell0->bg<<4) |
(cell0->bold?0x100:0))
, &w1);
bufprint("\x01%c%c", w1.lsb, w1.msb);
(cell0->attrs<<8))
, &lw1);
bufprint("\x01%c%c%c", lw1.lsb, lw1.msb, lw1.xsb);
}
// copy the symbol, until first 0 or reached 4 bytes
@@ -795,7 +1023,7 @@ screenSerializeToBuffer(char *buffer, size_t buf_len, void **data)
ss->lastFg = cell0->fg;
ss->lastBg = cell0->bg;
ss->lastBold = cell0->bold;
ss->lastAttrs = cell0->attrs;
memcpy(ss->lastChar, cell0->c, 4);
i++;
+34 -28
View File
@@ -46,6 +46,14 @@ typedef enum {
CHANGE_LABELS,
} ScreenNotifyChangeTopic;
#define ATTR_BOLD (1<<0)
#define ATTR_FAINT (1<<1)
#define ATTR_ITALIC (1<<2)
#define ATTR_UNDERLINE (1<<3)
#define ATTR_BLINK (1<<4)
#define ATTR_FRAKTUR (1<<5)
#define ATTR_STRIKE (1<<6)
#define SCREEN_NOTIFY_DELAY_MS 20
typedef struct {
@@ -70,6 +78,7 @@ extern TerminalConfigBundle termconf_scratch;
void terminal_restore_defaults(void);
void terminal_apply_settings(void);
void terminal_apply_settings_noclear(void); // the same, but with no screen reset / init
/**
* Maximum screen size (determines size of the static data array)
@@ -89,21 +98,24 @@ httpd_cgi_state screenSerializeToBuffer(char *buffer, size_t buf_len, void **dat
void screenSerializeLabelsToBuffer(char *buffer, size_t buf_len);
typedef struct {
u8 lsb;
u8 msb;
} WordB2;
typedef struct {
u8 lsb;
u8 msb;
u8 xsb;
} WordB3;
/** Encode number to two nice ASCII bytes */
void encode2B(u16 number, WordB2 *stru);
/** Init the screen */
void screen_init(void);
/** Change the screen size */
void screen_resize(int rows, int cols);
/** Check if coord is valid */
bool screen_isCoordValid(int y, int x);
@@ -111,48 +123,45 @@ bool screen_isCoordValid(int y, int x);
/** Screen reset to default state */
void screen_reset(void);
/** Clear entire screen, set all to 7 on 0 */
/** Clear entire screen */
void screen_clear(ClearMode mode);
/** Line reset to gray-on-white, empty */
/** Clear line */
void screen_clear_line(ClearMode mode);
/** Clear part of line */
void screen_clear_in_line(unsigned int count);
/** Shift screen upwards */
void screen_scroll_up(unsigned int lines);
/** Shift screen downwards */
void screen_scroll_down(unsigned int lines);
/** esc # 8 - fill entire screen with E of default colors (DEC alignment display) */
void screen_fill_with_E(void);
// --- insert / delete ---
void screen_insert_lines(unsigned int lines);
void screen_delete_lines(unsigned int lines);
void screen_insert_characters(unsigned int count);
void screen_delete_characters(unsigned int count);
// --- Cursor control ---
/** Set cursor position */
void screen_cursor_set(int y, int x);
/** Read cursor pos to given vars */
void screen_cursor_get(int *y, int *x);
/** Set cursor X position */
void screen_cursor_set_x(int x);
/** Set cursor Y position */
void screen_cursor_set_y(int y);
/** Reset cursor attribs */
void screen_reset_cursor(void);
/** Relative cursor move */
void screen_cursor_move(int dy, int dx);
void screen_cursor_move(int dy, int dx, bool scroll);
/** Save the cursor pos */
void screen_cursor_save(bool withAttrs);
/** Restore the cursor pos */
void screen_cursor_restore(bool withAttrs);
/** Enable cursor display */
void screen_cursor_enable(bool enable);
/** Enable auto wrap */
void screen_wrap_enable(bool enable);
@@ -160,19 +169,16 @@ void screen_wrap_enable(bool enable);
/** Set cursor foreground color */
void screen_set_fg(Color color);
/** Set cursor background coloor */
void screen_set_bg(Color color);
/** make foreground bright */
void screen_set_bold(bool bold);
/** Set cursor foreground and background color */
void screen_set_colors(Color fg, Color bg);
/** Invert colors */
void screen_inverse(bool inverse);
// enable or disable attrs by bitmask
void screen_attr_enable(u8 attrs);
void screen_attr_disable(u8 attrs);
void screen_inverse_enable(bool ena);
void screen_set_charset_n(int Gx);
void screen_set_charset(int Gx, char charset);
/**
* Set a character in the cursor color, move to right with wrap.
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef USER_MAIN_H_H
#define USER_MAIN_H_H
#define FIRMWARE_VERSION "0.6.2+" GIT_HASH
#define FIRMWARE_VERSION "0.6.6+" GIT_HASH
#define TERMINAL_GITHUB_REPO "https://github.com/MightyPork/esp-vt100-firmware"
#endif //USER_MAIN_H_H