From: John Hodge Date: Wed, 12 Feb 2014 13:31:59 +0000 (+0800) Subject: Kernel/VTerm - Code cleanup and VT100 bugfixes X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=573469df47d366903d780ca563c9f2da18f2bf62;p=tpg%2Facess2.git Kernel/VTerm - Code cleanup and VT100 bugfixes --- diff --git a/KernelLand/Kernel/drv/pty.c b/KernelLand/Kernel/drv/pty.c index 862997a9..3a8499b6 100644 --- a/KernelLand/Kernel/drv/pty.c +++ b/KernelLand/Kernel/drv/pty.c @@ -386,7 +386,7 @@ size_t PTY_int_SendInput(tPTY *PTY, const char *Input, size_t Length) if( PTY->Mode.InputMode & PTYIMODE_CANON ) { - const char char_bs = '\b'; + switch(Input[0]) { case 3: // INTR - ^C @@ -401,17 +401,21 @@ size_t PTY_int_SendInput(tPTY *PTY, const char *Input, size_t Length) print = 0; break; case 8: // Backspace - if(PTY->LineLength != 0) + if(PTY->LineLength != 0) { PTY->LineLength --; + PTY_WriteClient(&PTY->ClientNode, 0, 3, "\b \b", 0); + } + print = 0; break; case 'w'-'a': // Word erase while(PTY->LineLength != 0 && isalnum(PTY->LineData[--PTY->LineLength])) - PTY_WriteClient(&PTY->ClientNode, 0, 1, &char_bs, 0); + PTY_WriteClient(&PTY->ClientNode, 0, 1, "\b", 0); + PTY_WriteClient(&PTY->ClientNode, 0, 3, "\x1b[K", 0); print = 0; break; case 'u'-'a': // Kill - while(PTY->LineLength > 0) - PTY_WriteClient(&PTY->ClientNode, 0, 1, &char_bs, 0); + PTY_WriteClient(&PTY->ClientNode, 0, 8, "\x1b[2K\x1b[0G", 0); + PTY->LineLength = 0; print = 0; break; case 'v'-'a': diff --git a/KernelLand/Kernel/drv/vterm.h b/KernelLand/Kernel/drv/vterm.h index 65cdd651..4b783486 100644 --- a/KernelLand/Kernel/drv/vterm.h +++ b/KernelLand/Kernel/drv/vterm.h @@ -56,15 +56,15 @@ struct sVTerm Uint32 CurColour; //!< Current Text Colour - int ViewPos; //!< View Buffer Offset (Text Only) - int WritePos; //!< Write Buffer Offset (Text Only) + size_t ViewPos; //!< View Buffer Offset (Text Only) + size_t WritePos; //!< Write Buffer Offset (Text Only) tVT_Char *Text; tVT_Char *AltBuf; //!< Alternate Screen Buffer - int AltWritePos; //!< Alternate write position + size_t AltWritePos; //!< Alternate write position short ScrollTop; //!< Top of scrolling region (smallest) short ScrollHeight; //!< Length of scrolling region - int SavedWritePos; //!< Saved cursor position (\e[s and \e[u) + size_t SavedWritePos; //!< Saved cursor position (\e[s and \e[u) char EscapeCodeCache[16]; size_t EscapeCodeLen; @@ -129,5 +129,8 @@ extern void VT_int_ClearInLine(tVTerm *Term, int Row, int FirstCol, int LastCol) extern void VT_int_Resize(tVTerm *Term, int NewWidth, int NewHeight); extern void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled); +extern size_t *VT_int_GetWritePosPtr(tVTerm *Term); +extern size_t VT_int_GetBufferRows(tVTerm *Term); + #endif diff --git a/KernelLand/Kernel/drv/vterm_termbuf.c b/KernelLand/Kernel/drv/vterm_termbuf.c index 6869ec76..d4ea9a41 100644 --- a/KernelLand/Kernel/drv/vterm_termbuf.c +++ b/KernelLand/Kernel/drv/vterm_termbuf.c @@ -62,22 +62,12 @@ void VT_int_PutRawString(tVTerm *Term, const Uint8 *String, size_t Bytes) void VT_int_PutChar(tVTerm *Term, Uint32 Ch) { int i; - tVT_Char *buffer; - int write_pos; - int limit; HEAP_VALIDATE(); - if(Term->Flags & VT_FLAG_ALTBUF) { - buffer = Term->AltBuf; - write_pos = Term->AltWritePos; - limit = Term->TextHeight * Term->TextWidth; - } - else { - buffer = Term->Text; - write_pos = Term->WritePos; - limit = Term->TextHeight*(giVT_Scrollback+1) * Term->TextWidth; - } + size_t limit = VT_int_GetBufferRows(Term) * Term->TextWidth; + size_t write_pos = *VT_int_GetWritePosPtr(Term); + tVT_Char *buffer = (Term->Flags & VT_FLAG_ALTBUF ? Term->AltBuf : Term->Text); ASSERTC(write_pos, >=, 0); @@ -170,14 +160,7 @@ void VT_int_PutChar(tVTerm *Term, Uint32 Ch) } ASSERTC(write_pos, <=, limit); - if(Term->Flags & VT_FLAG_ALTBUF) - { - Term->AltWritePos = write_pos; - } - else - { - Term->WritePos = write_pos; - } + *VT_int_GetWritePosPtr(Term) = write_pos; HEAP_VALIDATE(); @@ -187,26 +170,23 @@ void VT_int_PutChar(tVTerm *Term, Uint32 Ch) void VT_int_ScrollText(tVTerm *Term, int Count) { tVT_Char *buf; - int *write_pos_ptr; - int height; int scroll_top, scroll_height; HEAP_VALIDATE(); // Get buffer pointer and attributes + size_t height = VT_int_GetBufferRows(Term); + size_t *write_pos_ptr = VT_int_GetWritePosPtr(Term); + if( Term->Flags & VT_FLAG_ALTBUF ) { buf = Term->AltBuf; - height = Term->TextHeight; - write_pos_ptr = &Term->AltWritePos; scroll_top = Term->ScrollTop; scroll_height = Term->ScrollHeight; } else { buf = Term->Text; - height = Term->TextHeight*(giVT_Scrollback+1); - write_pos_ptr = &Term->WritePos; scroll_top = 0; scroll_height = height; } @@ -292,7 +272,7 @@ void VT_int_ClearLine(tVTerm *Term, int Row) { HEAP_VALIDATE(); - size_t height = Term->TextHeight * (Term->Flags & VT_FLAG_ALTBUF ? 1 : giVT_Scrollback + 1); + size_t height = VT_int_GetBufferRows(Term); tVT_Char *buffer = (Term->Flags & VT_FLAG_ALTBUF ? Term->AltBuf : Term->Text); ASSERTCR(Row, >=, 0, ); ASSERTCR(Row, <, height, ); @@ -312,7 +292,7 @@ void VT_int_ClearInLine(tVTerm *Term, int Row, int FirstCol, int LastCol) { HEAP_VALIDATE(); - size_t height = Term->TextHeight * (Term->Flags & VT_FLAG_ALTBUF ? 1 : giVT_Scrollback + 1); + size_t height = VT_int_GetBufferRows(Term); tVT_Char *buffer = (Term->Flags & VT_FLAG_ALTBUF ? Term->AltBuf : Term->Text); ASSERTCR(Row, >=, 0, ); ASSERTCR(Row, <, height, ); @@ -442,3 +422,13 @@ void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled) VT_int_UpdateScreen(Term, 1); } +size_t *VT_int_GetWritePosPtr(tVTerm *Term) +{ + return ((Term->Flags & VT_FLAG_ALTBUF) ? &Term->AltWritePos : &Term->WritePos); +} + +size_t VT_int_GetBufferRows(tVTerm *Term) +{ + return ((Term->Flags & VT_FLAG_ALTBUF) ? 1 : (giVT_Scrollback+1))*Term->TextHeight; +} + diff --git a/KernelLand/Kernel/drv/vterm_vt100.c b/KernelLand/Kernel/drv/vterm_vt100.c index ccb0c520..34505363 100644 --- a/KernelLand/Kernel/drv/vterm_vt100.c +++ b/KernelLand/Kernel/drv/vterm_vt100.c @@ -59,17 +59,17 @@ void Display_SetCursor(tTerminal *Term, int Row, int Col) { LOG("(R%i,C%i)", Row, Col); VT_int_UpdateScreen(Term, 0); - int maxrows = ((Term->Flags & VT_FLAG_ALTBUF) ? 1 : (giVT_Scrollback+1))*Term->TextHeight; + int maxrows = VT_int_GetBufferRows(Term); ASSERTCR( Row, >=, 0, ); ASSERTCR( Row, <, maxrows, ); ASSERTCR( Col, >=, 0, ); ASSERTCR( Col, <, Term->TextWidth, ); - *(Term->Flags & VT_FLAG_ALTBUF ? &Term->AltWritePos : &Term->WritePos) = Row*Term->TextWidth + Col; + *VT_int_GetWritePosPtr(Term) = Row*Term->TextWidth + Col; } void Display_MoveCursor(tTerminal *Term, int RelRow, int RelCol) { LOG("(R+%i,C+%i)", RelRow, RelCol); - int *wrpos = (Term->Flags & VT_FLAG_ALTBUF ? &Term->AltWritePos : &Term->WritePos); + size_t *wrpos = VT_int_GetWritePosPtr(Term); // TODO: Support scrolling if cursor goes offscreen // if( bScrollIfNeeded ) @@ -82,22 +82,22 @@ void Display_MoveCursor(tTerminal *Term, int RelRow, int RelCol) // if( RelCol < 0 ) { - int avail = *wrpos % Term->TextWidth; - if( RelCol < -avail ) - RelCol = -avail; + int max = *wrpos % Term->TextWidth; + if( RelCol < -max ) + RelCol = -max; } else { - size_t avail = Term->TextWidth - (*wrpos % Term->TextWidth); - if(RelCol > avail) - RelCol = avail; + size_t max = Term->TextWidth - (*wrpos % Term->TextWidth) - 1; + if(RelCol > max) + RelCol = max; } *wrpos += RelCol; } if( RelRow != 0 ) { int currow = *wrpos / Term->TextWidth; - int maxrows = ((Term->Flags & VT_FLAG_ALTBUF) ? 1 : (giVT_Scrollback+1))*Term->TextHeight; + int maxrows = VT_int_GetBufferRows(Term); if( RelRow < 0 ) { if( RelRow < -currow ) @@ -114,23 +114,20 @@ void Display_MoveCursor(tTerminal *Term, int RelRow, int RelCol) } void Display_SaveCursor(tTerminal *Term) { - Term->SavedWritePos = (Term->Flags & VT_FLAG_ALTBUF) ? Term->AltWritePos : Term->WritePos; + Term->SavedWritePos = *VT_int_GetWritePosPtr(Term); LOG("Saved = %i", Term->SavedWritePos); } void Display_RestoreCursor(tTerminal *Term) { - int max = ((Term->Flags & VT_FLAG_ALTBUF) ? 1 : (giVT_Scrollback+1))*Term->TextHeight * Term->TextWidth; - int *wrpos = ((Term->Flags & VT_FLAG_ALTBUF) ? &Term->AltWritePos : &Term->WritePos); - if( Term->SavedWritePos >= max ) - *wrpos = max-1; - else - *wrpos = Term->SavedWritePos; + size_t max = VT_int_GetBufferRows(Term) * Term->TextWidth; + size_t *wrpos = VT_int_GetWritePosPtr(Term); + *wrpos = MIN(Term->SavedWritePos, max-1); LOG("Restored %i", *wrpos); } // 0: All, 1: Forward, -1: Reverse void Display_ClearLine(tTerminal *Term, int Dir) { - const int wrpos = (Term->Flags & VT_FLAG_ALTBUF ? Term->AltWritePos : Term->WritePos); + const size_t wrpos = *VT_int_GetWritePosPtr(Term); const int row = wrpos / Term->TextWidth; const int col = wrpos % Term->TextWidth; @@ -160,8 +157,8 @@ void Display_ClearLine(tTerminal *Term, int Dir) void Display_ClearLines(tTerminal *Term, int Dir) { LOG("(Dir=%i)", Dir); - int *wrpos = (Term->Flags & VT_FLAG_ALTBUF ? &Term->AltWritePos : &Term->WritePos); - int height = Term->TextHeight * (Term->Flags & VT_FLAG_ALTBUF ? 1 : giVT_Scrollback + 1); + size_t *wrpos = VT_int_GetWritePosPtr(Term); + size_t height = VT_int_GetBufferRows(Term); // All if( Dir == 0 ) { diff --git a/Usermode/Applications/gui_terminal_src/vt100.c b/Usermode/Applications/gui_terminal_src/vt100.c index 840f2d92..4e7f70f4 100644 --- a/Usermode/Applications/gui_terminal_src/vt100.c +++ b/Usermode/Applications/gui_terminal_src/vt100.c @@ -16,6 +16,11 @@ # include # include # include // malloc/free + +static inline int MIN(int a, int b) +{ + return a < b ? a : b; +} #endif enum eExcapeMode { @@ -58,11 +63,6 @@ const uint32_t caVT100Colours[] = { int Term_HandleVT100_Long(tTerminal *Term, int Len, const char *Buf); int Term_HandleVT100_OSC(tTerminal *Term, int Len, const char *Buf); -static inline int min(int a, int b) -{ - return a < b ? a : b; -} - int _locate_eos(size_t Len, const char *Buf) { for( size_t ret = 0; ret < Len; ret ++ ) @@ -137,7 +137,7 @@ int Term_HandleVT100(tTerminal *Term, int Len, const char *Buf) if( st->cache_len > 0 || *Buf == '\x1b' ) { // Handle VT100 (like) escape sequence - int new_bytes = min(MAX_VT100_ESCAPE_LEN - st->cache_len, Len); + int new_bytes = MIN(MAX_VT100_ESCAPE_LEN - st->cache_len, Len); int ret = 0; int old_inc_len = st->cache_len; @@ -178,11 +178,11 @@ int Term_HandleVT100(tTerminal *Term, int Len, const char *Buf) switch( *Buf ) { - // TODO: Need to handle \t and ^A-Z case '\b': + // backspace is aprarently just supposed to cursor left (if possible) Display_MoveCursor(Term, 0, -1); - Display_AddText(Term, 1, " "); - Display_MoveCursor(Term, 0, -1); + //Display_AddText(Term, 1, " "); + //Display_MoveCursor(Term, 0, -1); return 1; case '\t': // TODO: tab (get current cursor pos, space until multiple of 8) @@ -249,7 +249,7 @@ int Term_HandleVT100_Short(tTerminal *Term, int Len, const char *Buf) switch(Buf[2]) { case 8: - _SysDebug("TODO \\e#%c - Fill screen with 'E'", Buf[2]); + _SysDebug("TODO \\e#%c DECALN - Fill screen with 'E'", Buf[2]); break; default: _SysDebug("Unknown \\e#%c", Buf[2]); @@ -414,16 +414,16 @@ int Term_HandleVT100_Long(tTerminal *Term, int Len, const char *Buffer) switch( c ) { case 'A': - Display_MoveCursor(Term, -(argc >= 1 ? args[0] : 1), 0); + Display_MoveCursor(Term, -(args[0] != 0 ? args[0] : 1), 0); break; case 'B': - Display_MoveCursor(Term, (argc >= 1 ? args[0] : 1), 0); + Display_MoveCursor(Term, (args[0] != 0 ? args[0] : 1), 0); break; case 'C': - Display_MoveCursor(Term, 0, (argc >= 1 ? args[0] : 1)); + Display_MoveCursor(Term, 0, (args[0] != 0 ? args[0] : 1)); break; case 'D': - Display_MoveCursor(Term, 0, -(argc >= 1 ? args[0] : 1)); + Display_MoveCursor(Term, 0, -(args[0] != 0 ? args[0] : 1)); break; case 'H': if( argc != 2 ) {