diff --git a/user/ansi_parser.c b/user/ansi_parser.c index 305f839..76315ee 100644 --- a/user/ansi_parser.c +++ b/user/ansi_parser.c @@ -9,7 +9,8 @@ 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 + 7, 1, 8, 1, 9, 1, 10, 1, + 11, 1, 12 }; static const char _ansi_eof_actions[] = { @@ -64,7 +65,7 @@ ansi_parser(const char *newdata, size_t len) // Init Ragel on the first run if (cs == -1) { -/* #line 68 "user/ansi_parser.c" */ +/* #line 69 "user/ansi_parser.c" */ { cs = ansi_start; } @@ -74,7 +75,7 @@ ansi_parser(const char *newdata, size_t len) // The parser -/* #line 78 "user/ansi_parser.c" */ +/* #line 79 "user/ansi_parser.c" */ { const char *_acts; unsigned int _nacts; @@ -91,9 +92,11 @@ case 1: goto tr0; case 2: switch( (*p) ) { - case 91: goto tr3; - case 93: goto tr4; - case 99: goto tr5; + case 55: goto tr3; + case 56: goto tr4; + case 91: goto tr5; + case 93: goto tr6; + case 99: goto tr7; } goto tr2; case 0: @@ -104,118 +107,122 @@ case 14: goto tr0; case 3: if ( (*p) == 59 ) - goto tr8; + goto tr10; if ( (*p) < 60 ) { if ( (*p) > 47 ) { if ( 48 <= (*p) && (*p) <= 57 ) - goto tr7; + goto tr9; } else if ( (*p) >= 32 ) - goto tr6; + goto tr8; } else if ( (*p) > 64 ) { if ( (*p) > 90 ) { if ( 97 <= (*p) && (*p) <= 122 ) - goto tr9; + goto tr11; } else if ( (*p) >= 65 ) - goto tr9; + goto tr11; } else - goto tr6; + goto tr8; goto tr2; case 4: if ( (*p) == 59 ) - goto tr8; + goto tr10; if ( (*p) < 65 ) { if ( 48 <= (*p) && (*p) <= 57 ) - goto tr7; + goto tr9; } else if ( (*p) > 90 ) { if ( 97 <= (*p) && (*p) <= 122 ) - goto tr9; + goto tr11; } else - goto tr9; + goto tr11; goto tr2; case 15: goto tr2; case 5: switch( (*p) ) { - case 70: goto tr10; - case 87: goto tr11; + case 70: goto tr12; + case 87: goto tr13; } goto tr2; case 6: if ( (*p) == 82 ) - goto tr12; + goto tr14; goto tr2; case 7: switch( (*p) ) { - case 7: goto tr13; - case 27: goto tr14; + case 7: goto tr15; + case 27: goto tr16; } goto tr2; case 16: goto tr2; case 8: if ( (*p) == 92 ) - goto tr13; + goto tr15; goto tr2; case 9: if ( 48 <= (*p) && (*p) <= 57 ) - goto tr15; + goto tr17; goto tr2; case 10: if ( (*p) == 59 ) - goto tr16; + goto tr18; if ( 48 <= (*p) && (*p) <= 57 ) - goto tr15; + goto tr17; goto tr2; case 11: if ( 48 <= (*p) && (*p) <= 57 ) - goto tr17; + goto tr19; goto tr2; case 12: switch( (*p) ) { - case 7: goto tr18; - case 27: goto tr19; + case 7: goto tr20; + case 27: goto tr21; } if ( 48 <= (*p) && (*p) <= 57 ) - goto tr17; + goto tr19; goto tr2; case 13: if ( (*p) == 92 ) - goto tr18; + goto tr20; goto tr2; } tr2: cs = 0; goto f0; tr0: cs = 1; goto f1; tr1: cs = 2; goto _again; - tr6: cs = 4; goto f5; - tr7: cs = 4; goto f6; tr8: cs = 4; goto f7; - tr10: cs = 6; goto _again; - tr12: cs = 7; goto _again; - tr14: cs = 8; goto _again; - tr11: cs = 9; goto _again; - tr15: cs = 10; goto f6; - tr16: cs = 11; goto f7; - tr17: cs = 12; goto f6; - tr19: cs = 13; goto _again; + tr9: cs = 4; goto f8; + tr10: cs = 4; goto f9; + tr12: cs = 6; goto _again; + tr14: cs = 7; goto _again; + tr16: cs = 8; goto _again; + tr13: cs = 9; goto _again; + tr17: cs = 10; goto f8; + tr18: cs = 11; goto f9; + tr19: cs = 12; goto f8; + tr21: cs = 13; goto _again; tr3: cs = 14; goto f2; tr4: cs = 14; goto f3; tr5: cs = 14; goto f4; - tr9: cs = 15; goto f8; - tr13: cs = 16; goto f9; - tr18: cs = 16; goto f10; + tr6: cs = 14; goto f5; + tr7: cs = 14; goto f6; + tr11: cs = 15; goto f10; + tr15: cs = 16; goto f11; + tr20: cs = 16; goto f12; f1: _acts = _ansi_actions + 1; goto execFuncs; - f2: _acts = _ansi_actions + 3; goto execFuncs; - f5: _acts = _ansi_actions + 5; goto execFuncs; - f6: _acts = _ansi_actions + 7; goto execFuncs; - f7: _acts = _ansi_actions + 9; goto execFuncs; - f8: _acts = _ansi_actions + 11; 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; f0: _acts = _ansi_actions + 13; goto execFuncs; - f3: _acts = _ansi_actions + 15; goto execFuncs; - f9: _acts = _ansi_actions + 17; goto execFuncs; - f10: _acts = _ansi_actions + 19; goto execFuncs; - f4: _acts = _ansi_actions + 21; goto execFuncs; + f5: _acts = _ansi_actions + 15; goto execFuncs; + f11: _acts = _ansi_actions + 17; goto execFuncs; + f12: _acts = _ansi_actions + 19; goto execFuncs; + f6: _acts = _ansi_actions + 21; goto execFuncs; + f2: _acts = _ansi_actions + 23; goto execFuncs; + f3: _acts = _ansi_actions + 25; goto execFuncs; execFuncs: _nacts = *_acts++; @@ -316,7 +323,21 @@ execFuncs: {cs = 1; goto _again;} } break; -/* #line 320 "user/ansi_parser.c" */ + case 11: +/* #line 147 "user/ansi_parser.rl" */ + { + apars_handle_saveCursorAttrs(); + {cs = 1; goto _again;} + } + break; + case 12: +/* #line 152 "user/ansi_parser.rl" */ + { + apars_handle_restoreCursorAttrs(); + {cs = 1; goto _again;} + } + break; +/* #line 341 "user/ansi_parser.c" */ } } goto _again; @@ -340,7 +361,7 @@ _again: {cs = 1; goto _again;} } break; -/* #line 344 "user/ansi_parser.c" */ +/* #line 365 "user/ansi_parser.c" */ } } } @@ -348,6 +369,6 @@ _again: _out: {} } -/* #line 159 "user/ansi_parser.rl" */ +/* #line 171 "user/ansi_parser.rl" */ } diff --git a/user/ansi_parser.h b/user/ansi_parser.h index a029064..6e91dde 100644 --- a/user/ansi_parser.h +++ b/user/ansi_parser.h @@ -12,6 +12,8 @@ extern void apars_handle_RESET_cmd(void); extern void apars_handle_plainchar(char c); extern void apars_handle_OSC_FactoryReset(void); extern void apars_handle_OSC_SetScreenSize(int rows, int cols); +extern void apars_handle_saveCursorAttrs(void); +extern void apars_handle_restoreCursorAttrs(void); /** * \brief Linear ANSI chars stream parser diff --git a/user/ansi_parser.rl b/user/ansi_parser.rl index 3fb29a2..bf126a7 100644 --- a/user/ansi_parser.rl +++ b/user/ansi_parser.rl @@ -144,6 +144,16 @@ ansi_parser(const char *newdata, size_t len) fgoto main; } + action CSI_SaveCursorAttrs { + apars_handle_saveCursorAttrs(); + fgoto main; + } + + action CSI_RestoreCursorAttrs { + apars_handle_restoreCursorAttrs(); + fgoto main; + } + # --- Main parser loop --- main := @@ -151,7 +161,9 @@ ansi_parser(const char *newdata, size_t len) (NOESC @plain_char)* ESC ( '[' @CSI_start | ']' @OSC_start | - 'c' @RESET_cmd + 'c' @RESET_cmd | + '7' @CSI_SaveCursorAttrs | + '8' @CSI_RestoreCursorAttrs ) )+ $!errBadSeq; diff --git a/user/ansi_parser_callbacks.c b/user/ansi_parser_callbacks.c index 290dc95..d2d71b5 100644 --- a/user/ansi_parser_callbacks.c +++ b/user/ansi_parser_callbacks.c @@ -143,17 +143,22 @@ apars_handle_CSI(char leadchar, int *params, char keychar) break; // SCP, RCP - case 's': screen_cursor_save(); break; - case 'u': screen_cursor_restore(); break; + case 's': screen_cursor_save(0); break; + case 'u': screen_cursor_restore(0); break; case 'n': if (n1 == 6) { + // Query cursor position char buf[20]; int x, y; screen_cursor_get(&y, &x); sprintf(buf, "\033[%d;%dR", y+1, x+1); UART_WriteString(UART0, buf, UART_TIMEOUT_US); } + else if (n1 == 5) { + // Query device status - reply "Device is OK" + UART_WriteString(UART0, "\0330n", UART_TIMEOUT_US); + } break; // DECTCEM cursor show hide @@ -193,6 +198,16 @@ apars_handle_CSI(char leadchar, int *params, char keychar) } } +void ICACHE_FLASH_ATTR apars_handle_saveCursorAttrs(void) +{ + screen_cursor_save(1); +} + +void ICACHE_FLASH_ATTR apars_handle_restoreCursorAttrs(void) +{ + screen_cursor_restore(1); +} + /** * \brief Handle a request to reset the display device */ diff --git a/user/ansi_parser_callbacks.h b/user/ansi_parser_callbacks.h index ad35ceb..a86491d 100644 --- a/user/ansi_parser_callbacks.h +++ b/user/ansi_parser_callbacks.h @@ -7,5 +7,7 @@ void apars_handle_RESET_cmd(void); void apars_handle_plainchar(char c); void apars_handle_OSC_FactoryReset(void); void apars_handle_OSC_SetScreenSize(int rows, int cols); +void apars_handle_saveCursorAttrs(void); +void apars_handle_restoreCursorAttrs(void); #endif //ESP_VT100_FIRMWARE_ANSI_PARSER_CALLBACKS_H diff --git a/user/screen.c b/user/screen.c index e60324b..1e157b3 100644 --- a/user/screen.c +++ b/user/screen.c @@ -41,6 +41,12 @@ static struct { static struct { int x; int y; + + // optionally saved attrs + bool withAttrs; + bool inverse; + Color fg; + Color bg; } cursor_sav; /** @@ -354,21 +360,40 @@ screen_cursor_move(int dy, int dx) * Save the cursor pos */ void ICACHE_FLASH_ATTR -screen_cursor_save(void) +screen_cursor_save(bool withAttrs) { cursor_sav.x = cursor.x; cursor_sav.y = cursor.y; + + cursor_sav.withAttrs = withAttrs; + + if (withAttrs) { + cursor_sav.fg = cursor.fg; + cursor_sav.bg = cursor.bg; + cursor_sav.inverse = cursor.inverse; + } else { + cursor_sav.fg = SCREEN_DEF_FG; + cursor_sav.bg = SCREEN_DEF_BG; + cursor_sav.inverse = 0; + } } /** * Restore the cursor pos */ void ICACHE_FLASH_ATTR -screen_cursor_restore(void) +screen_cursor_restore(bool withAttrs) { NOTIFY_LOCK(); cursor.x = cursor_sav.x; cursor.y = cursor_sav.y; + + if (withAttrs) { + cursor.fg = cursor_sav.fg; + cursor.bg = cursor_sav.bg; + cursor.inverse = cursor_sav.inverse; + } + NOTIFY_DONE(); } diff --git a/user/screen.h b/user/screen.h index 785eea0..f8493df 100644 --- a/user/screen.h +++ b/user/screen.h @@ -101,10 +101,10 @@ void screen_cursor_set_y(int y); void screen_cursor_move(int dy, int dx); /** Save the cursor pos */ -void screen_cursor_save(void); +void screen_cursor_save(bool withAttrs); /** Restore the cursor pos */ -void screen_cursor_restore(void); +void screen_cursor_restore(bool withAttrs); /** Enable cursor display */ void screen_cursor_enable(bool enable);