Kernel/VTerm - Code cleanup and VT100 bugfixes
authorJohn Hodge <[email protected]>
Wed, 12 Feb 2014 13:31:59 +0000 (21:31 +0800)
committerJohn Hodge <[email protected]>
Wed, 12 Feb 2014 13:31:59 +0000 (21:31 +0800)
KernelLand/Kernel/drv/pty.c
KernelLand/Kernel/drv/vterm.h
KernelLand/Kernel/drv/vterm_termbuf.c
KernelLand/Kernel/drv/vterm_vt100.c
Usermode/Applications/gui_terminal_src/vt100.c

index 862997a..3a8499b 100644 (file)
@@ -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':
index 65cdd65..4b78348 100644 (file)
@@ -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
 
index 6869ec7..d4ea9a4 100644 (file)
@@ -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;
+}
+
index ccb0c52..3450536 100644 (file)
@@ -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 ) {
index 840f2d9..4e7f70f 100644 (file)
 # include <string.h>
 # include <assert.h>
 # include <stdlib.h>   // 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 ) {

UCC git Repository :: git.ucc.asn.au