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 VT_int_PutRawString(Term, Buffer + ofs, esc_len);
28 //Debug("Raw string '%.*s'", esc_len, Buffer+ofs);
31 //Debug("Escape code '%.*s'", esc_len, Buffer+ofs);
33 ASSERTCR(esc_len, >, 0, );
37 VT_int_UpdateScreen( Term, 1 );
40 void VT_int_PutRawString(tVTerm *Term, const Uint8 *String, size_t Bytes)
42 for( int i = 0; i < Bytes; ) {
44 i += ReadUTF8(String+i, &val);
45 VT_int_PutChar(Term, val);
50 * \fn void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
51 * \brief Write a single character to a VTerm
53 void VT_int_PutChar(tVTerm *Term, Uint32 Ch)
60 if(Term->Flags & VT_FLAG_ALTBUF) {
61 buffer = Term->AltBuf;
62 write_pos = Term->AltWritePos;
63 limit = Term->TextHeight * Term->TextWidth;
67 write_pos = Term->WritePos;
68 limit = Term->TextHeight*(giVT_Scrollback+1) * Term->TextWidth;
73 case '\0': return; // Ignore NULL byte
75 VT_int_UpdateScreen( Term, 0 ); // Update the line before newlining
76 write_pos += Term->TextWidth;
78 write_pos -= write_pos % Term->TextWidth;
82 int line = write_pos / Term->TextWidth;
83 write_pos %= Term->TextWidth;
85 buffer[ write_pos ].Ch = '\0';
86 buffer[ write_pos ].Colour = Term->CurColour;
88 } while(write_pos & 7);
89 write_pos += line * Term->TextWidth;
93 // Backspace is invalid at Offset 0
94 if(write_pos == 0) break;
98 if(buffer[ write_pos ].Ch != '\0') {
99 buffer[ write_pos ].Ch = 0;
100 buffer[ write_pos ].Colour = Term->CurColour;
104 i = 7; // Limit it to 8
106 buffer[ write_pos ].Ch = 0;
107 buffer[ write_pos ].Colour = Term->CurColour;
109 } while(write_pos && i-- && buffer[ write_pos ].Ch == '\0');
110 if(buffer[ write_pos ].Ch != '\0')
115 ASSERTC(write_pos,<,limit);
116 buffer[ write_pos ].Ch = Ch;
117 buffer[ write_pos ].Colour = Term->CurColour;
118 // Update the line before wrapping
119 if( (write_pos + 1) % Term->TextWidth == 0 )
120 VT_int_UpdateScreen( Term, 0 );
125 if(Term->Flags & VT_FLAG_ALTBUF)
127 Term->AltWritePos = write_pos;
129 if(Term->AltWritePos >= limit)
131 Term->AltWritePos -= Term->TextWidth;
132 VT_int_ScrollText(Term, 1);
137 Term->WritePos = write_pos;
139 // - Check if we need to scroll the entire scrollback buffer
140 if(Term->WritePos >= limit)
144 // Update view position
145 base = Term->TextWidth*Term->TextHeight*(giVT_Scrollback);
146 if(Term->ViewPos < base)
147 Term->ViewPos += Term->Width;
148 if(Term->ViewPos > base)
149 Term->ViewPos = base;
151 VT_int_ScrollText(Term, 1);
152 Term->WritePos -= Term->TextWidth;
154 // Ok, so we only need to scroll the screen
155 else if(Term->WritePos >= Term->ViewPos + Term->TextWidth*Term->TextHeight)
157 VT_int_ScrollFramebuffer( Term, 1 );
159 Term->ViewPos += Term->TextWidth;
166 void VT_int_ScrollText(tVTerm *Term, int Count)
169 int height, init_write_pos;
171 int scroll_top, scroll_height;
173 // Get buffer pointer and attributes
174 if( Term->Flags & VT_FLAG_ALTBUF )
177 height = Term->TextHeight;
178 init_write_pos = Term->AltWritePos;
179 scroll_top = Term->ScrollTop;
180 scroll_height = Term->ScrollHeight;
185 height = Term->TextHeight*(giVT_Scrollback+1);
186 init_write_pos = Term->WritePos;
188 scroll_height = height;
191 // Scroll text upwards (more space at bottom)
197 if(Count > scroll_height) Count = scroll_height;
198 base = Term->TextWidth*(scroll_top + scroll_height - Count);
199 len = Term->TextWidth*(scroll_height - Count);
201 // Scroll terminal cache
203 &buf[Term->TextWidth*scroll_top],
204 &buf[Term->TextWidth*(scroll_top+Count)],
208 for( i = 0; i < Term->TextWidth*Count; i ++ )
210 buf[ base + i ].Ch = 0;
211 buf[ base + i ].Colour = Term->CurColour;
215 VT_int_ScrollFramebuffer( Term, Count );
216 if( Term->Flags & VT_FLAG_ALTBUF )
217 Term->AltWritePos = base;
219 Term->WritePos = Term->ViewPos + Term->TextWidth*(Term->TextHeight - Count);
220 for( i = 0; i < Count; i ++ )
222 VT_int_UpdateScreen( Term, 0 );
223 if( Term->Flags & VT_FLAG_ALTBUF )
224 Term->AltWritePos += Term->TextWidth;
226 Term->WritePos += Term->TextWidth;
232 if(Count > scroll_height) Count = scroll_height;
234 len = Term->TextWidth*(scroll_height - Count);
236 // Scroll terminal cache
238 &buf[Term->TextWidth*(scroll_top+Count)],
239 &buf[Term->TextWidth*scroll_top],
242 // Clear preceding rows
243 for( i = 0; i < Term->TextWidth*Count; i ++ )
246 buf[ i ].Colour = Term->CurColour;
249 VT_int_ScrollFramebuffer( Term, -Count );
250 if( Term->Flags & VT_FLAG_ALTBUF )
251 Term->AltWritePos = Term->TextWidth*scroll_top;
253 Term->WritePos = Term->ViewPos;
254 for( i = 0; i < Count; i ++ )
256 VT_int_UpdateScreen( Term, 0 );
257 if( Term->Flags & VT_FLAG_ALTBUF )
258 Term->AltWritePos += Term->TextWidth;
260 Term->WritePos += Term->TextWidth;
264 if( Term->Flags & VT_FLAG_ALTBUF )
265 Term->AltWritePos = init_write_pos;
267 Term->WritePos = init_write_pos;
271 * \brief Clears a line in a virtual terminal
272 * \param Term Terminal to modify
273 * \param Num Line number to clear
275 void VT_int_ClearLine(tVTerm *Term, int Num)
277 int limit = Term->TextHeight * (Term->Flags & VT_FLAG_ALTBUF ? 1 : giVT_Scrollback + 1);
278 tVT_Char *buffer = (Term->Flags & VT_FLAG_ALTBUF ? Term->AltBuf : Term->Text);
279 if( Num < 0 || Num >= limit ) return ;
281 tVT_Char *cell = &buffer[ Num*Term->TextWidth ];
283 for( int i = 0; i < Term->TextWidth; i ++ )
286 cell[ i ].Colour = Term->CurColour;
291 * \brief Update the screen mode
292 * \param Term Terminal to update
293 * \param NewMode New mode to set
294 * \param NewWidth New framebuffer width
295 * \param NewHeight New framebuffer height
297 void VT_int_Resize(tVTerm *Term, int NewWidth, int NewHeight)
299 int oldW = Term->Width;
300 int oldTW = Term->TextWidth;
301 int oldH = Term->Height;
302 int oldTH = Term->TextHeight;
303 tVT_Char *oldTBuf = Term->Text;
304 Uint32 *oldFB = Term->Buffer;
307 // TODO: Increase RealWidth/RealHeight when this happens
308 if(NewWidth > giVT_RealWidth) NewWidth = giVT_RealWidth;
309 if(NewHeight > giVT_RealHeight) NewHeight = giVT_RealHeight;
311 // Fast exit if no resolution change
312 if(NewWidth == Term->Width && NewHeight == Term->Height)
315 // Calculate new dimensions
316 Term->Width = NewWidth;
317 Term->Height = NewHeight;
318 Term->TextWidth = NewWidth / giVT_CharWidth;
319 Term->TextHeight = NewHeight / giVT_CharHeight;
320 Term->ScrollHeight = Term->TextHeight - (oldTH - Term->ScrollHeight) - Term->ScrollTop;
322 // Allocate new buffers
325 Term->TextWidth * Term->TextHeight * (giVT_Scrollback+1),
330 w = (oldTW > Term->TextWidth) ? Term->TextWidth : oldTW;
331 h = (oldTH > Term->TextHeight) ? Term->TextHeight : oldTH;
332 h *= giVT_Scrollback + 1;
333 for( i = 0; i < h; i ++ )
336 &Term->Text[i*Term->TextWidth],
345 Term->AltBuf = realloc(
347 Term->TextWidth * Term->TextHeight * sizeof(tVT_Char)
352 Term->Buffer = calloc( Term->Width * Term->Height, sizeof(Uint32) );
354 w = (oldW > Term->Width) ? Term->Width : oldW;
355 h = (oldH > Term->Height) ? Term->Height : oldH;
356 for( i = 0; i < h; i ++ )
359 &Term->Buffer[i*Term->Width],
372 Log_Log("VTerm", "Set VT %p to text mode (%ix%i)",
373 Term, Term->TextWidth, Term->TextHeight);
376 Log_Log("VTerm", "Set VT %p to framebuffer mode (%ix%i)",
377 Term, Term->Width, Term->Height);
379 //case TERM_MODE_2DACCEL:
380 //case TERM_MODE_3DACCEL:
387 void VT_int_ToggleAltBuffer(tVTerm *Term, int Enabled)
390 Term->Flags |= VT_FLAG_ALTBUF;
392 Term->Flags &= ~VT_FLAG_ALTBUF;
393 VT_int_UpdateScreen(Term, 1);