3 * - By John Hodge (thePowersGang)
6 * - Virtual Terminal - Terminal buffer manipulation
11 extern int Term_HandleVT100(tVTerm *Term, int Len, const char *Buf);
16 * \fn void VT_int_PutString(tVTerm *Term, const Uint8 *Buffer, Uint Count)
17 * \brief Print a string to the Virtual Terminal
19 void VT_int_PutString(tVTerm *Term, const Uint8 *Buffer, Uint Count)
22 for( int ofs = 0; ofs < Count; )
24 int esc_len = Term_HandleVT100(Term, Count - ofs, (const void*)(Buffer + ofs));
27 LOG("%i '%*c'", esc_len, esc_len, Buffer+ofs);
28 VT_int_PutRawString(Term, Buffer + ofs, esc_len);
29 //Debug("Raw string '%.*s'", esc_len, Buffer+ofs);
32 //Debug("Escape code '%.*s'", esc_len, Buffer+ofs);
34 ASSERTCR(esc_len, >, 0, );
38 VT_int_UpdateScreen( Term, 1 );
41 void VT_int_PutRawString(tVTerm *Term, const Uint8 *String, size_t Bytes)
43 for( int i = 0; i < Bytes; ) {
45 i += ReadUTF8(String+i, &val);
46 VT_int_PutChar(Term, val);
51 * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
52 * \brief Write a single character to a VTerm
54 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
61 if(Term->Flags & VT_FLAG_ALTBUF) {
62 buffer = Term->AltBuf;
63 write_pos = Term->AltWritePos;
64 limit = Term->TextHeight * Term->TextWidth;
68 write_pos = Term->WritePos;
69 limit = Term->TextHeight*(giVT_Scrollback+1) * Term->TextWidth;
74 case '\0': return; // Ignore NULL byte
76 VT_int_UpdateScreen( Term, 0 ); // Update the line before newlining
77 write_pos += Term->TextWidth;
79 write_pos -= write_pos % Term->TextWidth;
83 int line = write_pos / Term->TextWidth;
84 write_pos %= Term->TextWidth;
86 buffer[ write_pos ].Ch = '\0';
87 buffer[ write_pos ].Colour = Term->CurColour;
89 } while(write_pos & 7);
90 write_pos += line * Term->TextWidth;
94 // Backspace is invalid at Offset 0
95 if(write_pos == 0) break;
99 if(buffer[ write_pos ].Ch != '\0') {
100 buffer[ write_pos ].Ch = 0;
101 buffer[ write_pos ].Colour = Term->CurColour;
105 i = 7; // Limit it to 8
107 buffer[ write_pos ].Ch = 0;
108 buffer[ write_pos ].Colour = Term->CurColour;
110 } while(write_pos && i-- && buffer[ write_pos ].Ch == '\0');
111 if(buffer[ write_pos ].Ch != '\0')
116 ASSERTC(write_pos,<,limit);
117 buffer[ write_pos ].Ch = Ch;
118 buffer[ write_pos ].Colour = Term->CurColour;
119 // Update the line before wrapping
120 if( (write_pos + 1) % Term->TextWidth == 0 )
121 VT_int_UpdateScreen( Term, 0 );
126 if(Term->Flags & VT_FLAG_ALTBUF)
128 Term->AltWritePos = write_pos;
130 if(Term->AltWritePos >= limit)
132 Term->AltWritePos -= Term->TextWidth;
133 VT_int_ScrollText(Term, 1);
138 Term->WritePos = write_pos;
140 // - Check if we need to scroll the entire scrollback buffer
141 if(Term->WritePos >= limit)
145 // Update view position
146 base = Term->TextWidth*Term->TextHeight*(giVT_Scrollback);
147 if(Term->ViewPos < base)
148 Term->ViewPos += Term->Width;
149 if(Term->ViewPos > base)
150 Term->ViewPos = base;
152 VT_int_ScrollText(Term, 1);
153 Term->WritePos -= Term->TextWidth;
155 // Ok, so we only need to scroll the screen
156 else if(Term->WritePos >= Term->ViewPos + Term->TextWidth*Term->TextHeight)
158 VT_int_ScrollFramebuffer( Term, 1 );
160 Term->ViewPos += Term->TextWidth;
167 void VT_int_ScrollText(tVTerm *Term, int Count)
170 int height, init_write_pos;
172 int scroll_top, scroll_height;
174 // Get buffer pointer and attributes
175 if( Term->Flags & VT_FLAG_ALTBUF )
178 height = Term->TextHeight;
179 init_write_pos = Term->AltWritePos;
180 scroll_top = Term->ScrollTop;
181 scroll_height = Term->ScrollHeight;
186 height = Term->TextHeight*(giVT_Scrollback+1);
187 init_write_pos = Term->WritePos;
189 scroll_height = height;
192 // Scroll text upwards (more space at bottom)
198 if(Count > scroll_height) Count = scroll_height;
199 base = Term->TextWidth*(scroll_top + scroll_height - Count);
200 len = Term->TextWidth*(scroll_height - Count);
202 // Scroll terminal cache
204 &buf[Term->TextWidth*scroll_top],
205 &buf[Term->TextWidth*(scroll_top+Count)],
209 for( i = 0; i < Term->TextWidth*Count; i ++ )
211 buf[ base + i ].Ch = 0;
212 buf[ base + i ].Colour = Term->CurColour;
216 VT_int_ScrollFramebuffer( Term, Count );
217 if( Term->Flags & VT_FLAG_ALTBUF )
218 Term->AltWritePos = base;
220 Term->WritePos = Term->ViewPos + Term->TextWidth*(Term->TextHeight - Count);
221 for( i = 0; i < Count; i ++ )
223 VT_int_UpdateScreen( Term, 0 );
224 if( Term->Flags & VT_FLAG_ALTBUF )
225 Term->AltWritePos += Term->TextWidth;
227 Term->WritePos += Term->TextWidth;
233 if(Count > scroll_height) Count = scroll_height;
235 len = Term->TextWidth*(scroll_height - Count);
237 // Scroll terminal cache
239 &buf[Term->TextWidth*(scroll_top+Count)],
240 &buf[Term->TextWidth*scroll_top],
243 // Clear preceding rows
244 for( i = 0; i < Term->TextWidth*Count; i ++ )
247 buf[ i ].Colour = Term->CurColour;
250 VT_int_ScrollFramebuffer( Term, -Count );
251 if( Term->Flags & VT_FLAG_ALTBUF )
252 Term->AltWritePos = Term->TextWidth*scroll_top;
254 Term->WritePos = Term->ViewPos;
255 for( i = 0; i < Count; i ++ )
257 VT_int_UpdateScreen( Term, 0 );
258 if( Term->Flags & VT_FLAG_ALTBUF )
259 Term->AltWritePos += Term->TextWidth;
261 Term->WritePos += Term->TextWidth;
265 if( Term->Flags & VT_FLAG_ALTBUF )
266 Term->AltWritePos = init_write_pos;
268 Term->WritePos = init_write_pos;
272 * \brief Clears a line in a virtual terminal
273 * \param Term Terminal to modify
274 * \param Num Line number to clear
276 void VT_int_ClearLine(tVTerm *Term, int Num)
278 int limit = Term->TextHeight * (Term->Flags & VT_FLAG_ALTBUF ? 1 : giVT_Scrollback + 1);
279 tVT_Char *buffer = (Term->Flags & VT_FLAG_ALTBUF ? Term->AltBuf : Term->Text);
280 if( Num < 0 || Num >= limit ) return ;
282 tVT_Char *cell = &buffer[ Num*Term->TextWidth ];
284 for( int i = 0; i < Term->TextWidth; i ++ )
287 cell[ i ].Colour = Term->CurColour;
292 * \brief Update the screen mode
293 * \param Term Terminal to update
294 * \param NewMode New mode to set
295 * \param NewWidth New framebuffer width
296 * \param NewHeight New framebuffer height
298 void VT_int_Resize(tVTerm *Term, int NewWidth, int NewHeight)
300 int oldW = Term->Width;
301 int oldTW = Term->TextWidth;
302 int oldH = Term->Height;
303 int oldTH = Term->TextHeight;
304 tVT_Char *oldTBuf = Term->Text;
305 Uint32 *oldFB = Term->Buffer;
308 // TODO: Increase RealWidth/RealHeight when this happens
309 if(NewWidth > giVT_RealWidth) NewWidth = giVT_RealWidth;
310 if(NewHeight > giVT_RealHeight) NewHeight = giVT_RealHeight;
312 // Fast exit if no resolution change
313 if(NewWidth == Term->Width && NewHeight == Term->Height)
316 // Calculate new dimensions
317 Term->Width = NewWidth;
318 Term->Height = NewHeight;
319 Term->TextWidth = NewWidth / giVT_CharWidth;
320 Term->TextHeight = NewHeight / giVT_CharHeight;
321 Term->ScrollHeight = Term->TextHeight - (oldTH - Term->ScrollHeight) - Term->ScrollTop;
323 // Allocate new buffers
326 Term->TextWidth * Term->TextHeight * (giVT_Scrollback+1),
331 w = (oldTW > Term->TextWidth) ? Term->TextWidth : oldTW;
332 h = (oldTH > Term->TextHeight) ? Term->TextHeight : oldTH;
333 h *= giVT_Scrollback + 1;
334 for( i = 0; i < h; i ++ )
337 &Term->Text[i*Term->TextWidth],
346 Term->AltBuf = realloc(
348 Term->TextWidth * Term->TextHeight * sizeof(tVT_Char)
353 Term->Buffer = calloc( Term->Width * Term->Height, sizeof(Uint32) );
355 w = (oldW > Term->Width) ? Term->Width : oldW;
356 h = (oldH > Term->Height) ? Term->Height : oldH;
357 for( i = 0; i < h; i ++ )
360 &Term->Buffer[i*Term->Width],
373 Log_Log("VTerm", "Set VT %p to text mode (%ix%i)",
374 Term, Term->TextWidth, Term->TextHeight);
377 Log_Log("VTerm", "Set VT %p to framebuffer mode (%ix%i)",
378 Term, Term->Width, Term->Height);
380 //case TERM_MODE_2DACCEL:
381 //case TERM_MODE_3DACCEL:
388 void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled)
391 Term->Flags |= VT_FLAG_ALTBUF;
393 Term->Flags &= ~VT_FLAG_ALTBUF;
394 VT_int_UpdateScreen(Term, 1);