3 * - By John Hodge (thePowersGang)
6 * - Virtual Terminal - VT100 (Kinda) Emulation
11 const Uint16 caVT100Colours[] = {
12 // Black, Red, Green, Yellow, Blue, Purple, Cyan, Gray
13 // Same again, but bright
14 VT_COL_BLACK, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, 0xAAA,
15 VT_COL_GREY, 0xF00, 0x0F0, 0xFF0, 0x00F, 0xF0F, 0x0FF, VT_COL_WHITE
20 * \brief Handle a standard large escape code
22 * Handles any escape code of the form \x1B[n,...A where n is an integer
23 * and A is any letter.
25 void VT_int_ParseEscape_StandardLarge(tVTerm *Term, char CmdChar, int argc, int *args)
35 if(argc == 1) tmp *= args[0];
36 if( Term->Flags & VT_FLAG_ALTBUF )
38 if( (Term->AltWritePos + tmp) % Term->TextWidth == 0 ) {
39 Term->AltWritePos -= Term->AltWritePos % Term->TextWidth;
40 Term->AltWritePos += Term->TextWidth - 1;
43 Term->AltWritePos += tmp;
47 if( (Term->WritePos + tmp) % Term->TextWidth == 0 ) {
48 Term->WritePos -= Term->WritePos % Term->TextWidth;
49 Term->WritePos += Term->TextWidth - 1;
52 Term->WritePos += tmp;
60 case 0: // Erase below
62 case 1: // Erase above
65 if( Term->Flags & VT_FLAG_ALTBUF )
67 int i = Term->TextHeight;
68 while( i-- ) VT_int_ClearLine(Term, i);
69 Term->AltWritePos = 0;
70 VT_int_UpdateScreen(Term, 1);
74 int i = Term->TextHeight * (giVT_Scrollback + 1);
75 while( i-- ) VT_int_ClearLine(Term, i);
78 VT_int_UpdateScreen(Term, 1);
88 case 0: // Erase to right
89 if( Term->Flags & VT_FLAG_ALTBUF )
92 max = Term->Width - Term->AltWritePos % Term->Width;
93 for( i = 0; i < max; i ++ )
94 Term->AltBuf[Term->AltWritePos+i].Ch = 0;
99 max = Term->Width - Term->WritePos % Term->Width;
100 for( i = 0; i < max; i ++ )
101 Term->Text[Term->WritePos+i].Ch = 0;
103 VT_int_UpdateScreen(Term, 0);
105 case 1: // Erase to left
106 if( Term->Flags & VT_FLAG_ALTBUF )
108 int i = Term->AltWritePos % Term->Width;
110 Term->AltBuf[Term->AltWritePos++].Ch = 0;
114 int i = Term->WritePos % Term->Width;
116 Term->Text[Term->WritePos++].Ch = 0;
118 VT_int_UpdateScreen(Term, 0);
121 if( Term->Flags & VT_FLAG_ALTBUF )
123 VT_int_ClearLine(Term, Term->AltWritePos / Term->Width);
127 VT_int_ClearLine(Term, Term->WritePos / Term->Width);
129 VT_int_UpdateScreen(Term, 0);
134 // Set cursor position
136 if( Term->Flags & VT_FLAG_ALTBUF )
137 Term->AltWritePos = args[0] + args[1]*Term->TextWidth;
139 Term->WritePos = args[0] + args[1]*Term->TextWidth;
140 //Log_Debug("VTerm", "args = {%i, %i}", args[0], args[1]);
143 // Scroll up `n` lines
146 // Scroll down `n` lines
148 if(argc == 1) tmp *= args[0];
149 if( Term->Flags & VT_FLAG_ALTBUF )
150 VT_int_ScrollText(Term, tmp);
153 if(Term->ViewPos/Term->TextWidth + tmp < 0)
155 if(Term->ViewPos/Term->TextWidth + tmp > Term->TextHeight * (giVT_Scrollback + 1))
158 Term->ViewPos += Term->TextWidth*tmp;
168 if( 0 <= args[argc] && args[argc] <= 8)
172 case 0: Term->CurColour = DEFAULT_COLOUR; break; // Reset
173 case 1: Term->CurColour |= 0x80000000; break; // Bright
174 case 2: Term->CurColour &= ~0x80000000; break; // Dim
178 else if(30 <= args[argc] && args[argc] <= 37) {
179 // Get colour index, accounting for bright bit
180 colour_idx = args[argc]-30 + ((Term->CurColour>>28) & 8);
181 Term->CurColour &= 0x8000FFFF;
182 Term->CurColour |= (Uint32)caVT100Colours[ colour_idx ] << 16;
185 else if(40 <= args[argc] && args[argc] <= 47) {
186 // Get colour index, accounting for bright bit
187 colour_idx = args[argc]-40 + ((Term->CurColour>>12) & 8);
188 Term->CurColour &= 0xFFFF8000;
189 Term->CurColour |= caVT100Colours[ colour_idx ];
192 Log_Warning("VTerm", "Unknown font flag %i", args[argc]);
197 // Set scrolling region
199 if( argc != 2 ) break;
200 Term->ScrollTop = args[0];
201 Term->ScrollHeight = args[1] - args[0];
205 Log_Warning("VTerm", "Unknown control sequence '\\x1B[%c'", CmdChar);
211 * \fn int VT_int_ParseEscape(tVTerm *Term, const char *Buffer)
212 * \brief Parses a VT100 Escape code
214 int VT_int_ParseEscape(tVTerm *Term, const char *Buffer)
218 int args[6] = {0,0,0,0};
219 int bQuestionMark = 0;
231 if( '0' <= c && c <= '9' )
234 if(c == ';') c = Buffer[j++];
235 while('0' <= c && c <= '9') {
245 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
251 // DEC Private Mode Set
257 Term->Flags &= ~VT_FLAG_HIDECSR;
260 VT_int_ToggleAltBuffer(Term, 1);
269 Term->Flags |= VT_FLAG_HIDECSR;
272 VT_int_ToggleAltBuffer(Term, 0);
277 Log_Warning("VTerm", "Unknown control sequence '\\x1B[?%c'", c);
283 VT_int_ParseEscape_StandardLarge(Term, c, argc, args);
289 Log_Notice("VTerm", "TODO: Handle short escape codes");
293 //Log_Debug("VTerm", "j = %i, Buffer = '%s'", j, Buffer);