VT100 Terminal App
Circle VT100 module documentation
Loading...
Searching...
No Matches
TRenderer.h
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// Module: CTRenderer
3// Description: Implements the VT100 display pipeline on top of Circle primitives.
4// Author: R. Zuehlsdorff, ralf.zuehlsdorff@t-online.de
5// Created: 2026-01-18
6// License: MIT License (https://opensource.org/license/mit/)
7//------------------------------------------------------------------------------
8// Change Log:
9// 2026-01-18 R. Zuehlsdorff Initial creation
10//------------------------------------------------------------------------------
11
12
13#pragma once
14
15
16// Include Circle core components
17#include <circle/sched/task.h>
18#include <circle/device.h>
19#include <circle/display.h>
20#include <circle/string.h>
21#include <circle/chargenerator.h>
22#include <circle/bcmframebuffer.h>
23#include <circle/spinlock.h>
24#include <circle/types.h>
25
37// Forward declarations and includes for classes used in this module
38#include "TColorPalette.h"
39#include "TFontConverter.h"
40
50class CTRenderer : public CDevice, public CTask
51{
52public:
53 // Define realistic vintage terminal colors
54 // static constexpr TRendererColor kColorBlack = DISPLAY_COLOR(12, 12, 12);
55 static constexpr TRendererColor kColorBlack = DISPLAY_COLOR(0, 0, 0);
56 static constexpr TRendererColor kColorWhite = DISPLAY_COLOR(235, 235, 235);
57 static constexpr TRendererColor kColorAmber = DISPLAY_COLOR(255, 176, 0);
58 static constexpr TRendererColor kColorGreen = DISPLAY_COLOR(51, 255, 51);
59
61 {
62 const TFont *font;
63 CCharGenerator::TFontFlags fontFlags;
64 CDisplay::TRawColor foreground;
65 CDisplay::TRawColor background;
66 CDisplay::TRawColor defaultForeground;
67 CDisplay::TRawColor defaultBackground;
68 unsigned cursorX;
69 unsigned cursorY;
70 boolean cursorOn;
71 boolean cursorBlock;
72 boolean cursorVisible;
73 boolean blinking;
74 unsigned blinkTicks;
75 unsigned nextBlink;
76 unsigned scrollStart;
77 unsigned scrollEnd;
78 boolean reverseAttribute;
79 boolean boldAttribute;
80 boolean underlineAttribute;
81 boolean blinkAttribute;
82 boolean insertOn;
83 boolean autoPage;
84 boolean delayedUpdate;
85 unsigned lastUpdateTicks;
86 unsigned parserState;
87 unsigned param1;
88 unsigned param2;
89 unsigned g0CharSet;
90 unsigned g1CharSet;
91 boolean useG1;
92 };
93
96 static CTRenderer *Get(void);
97
100 CTRenderer(void);
101
103 ~CTRenderer(void);
104
107 boolean Initialize(void);
108
113 boolean SetFont(const TFont &rFont,
114 CCharGenerator::TFontFlags FontFlags = CCharGenerator::FontFlagsNone);
115
120 boolean SetFont(EFontSelection selection,
121 CCharGenerator::TFontFlags FontFlags = CCharGenerator::FontFlagsNone);
122
126 TRendererColor MapColor(EColorSelection color);
127
128
129
132 unsigned GetWidth(void) const;
133
136 unsigned GetHeight(void) const;
137
140 unsigned GetColumns(void) const;
143 unsigned GetRows(void) const;
144
146 unsigned GetCursorColumn(void) const;
148 unsigned GetCursorRow(void) const;
149
152 CBcmFrameBuffer *GetDisplay(void);
153
155 void ClearDisplay(void);
156
162 int Write(const void *pBuffer, size_t nCount) override;
163
165 void ResetParserState(void);
166
170 void Goto(unsigned nRow, unsigned nColumn);
171
176 void SetPixel(unsigned nPosX, unsigned nPosY, TRendererColor Color);
177
182 void SetPixel(unsigned nPosX, unsigned nPosY, CDisplay::TRawColor nColor);
183
188 TRendererColor GetPixel(unsigned nPosX, unsigned nPosY);
189
194 boolean SetColors(EColorSelection Foreground, EColorSelection Background);
195
199 void SetColors(TRendererColor Foreground = kColorWhite, TRendererColor Background = kColorBlack);
200
203 void SetCursorBlock(boolean bCursorBlock);
204
208 void SetBlinkingCursor(boolean bBlinkingCursor, unsigned nPeriodMilliSeconds = 500);
209
211 void Update(void);
212
215 void SetCursorMode(boolean bVisible);
216
219 void SetVT52Mode(boolean bEnable);
220
223 void SetAutoPageMode(boolean bEnable);
224
227 void SetSmoothScrollEnabled(boolean bEnable);
228
231 boolean GetSmoothScrollEnabled(void) const { return m_bSmoothScrollEnabled; }
232
234 void ForceHideCursor(void);
235
236
238 void Run(void) override;
239
245 void SetRawPixel(unsigned nPosX, unsigned nPosY, CDisplay::TRawColor nColor);
246
252 CDisplay::TRawColor GetRawPixel(unsigned nPosX, unsigned nPosY);
253
258 CDisplay::TColor AdjustBrightness(CDisplay::TColor color, float factor);
259
264 CDisplay::TRawColor AdjustBrightness565(CDisplay::TRawColor color, float factor);
265
270 void SetBrightnessScaling(float boldFactor = 1.6f,
271 float reverseBackgroundFactor = 0.7f,
272 float reverseForegroundFactor = 1.25f);
273
275 void doRenderTest(void);
276
278 void SaveState(TRendererState &state);
279
281 void RestoreState(const TRendererState &state);
282
284 size_t GetBufferSize(void) const;
285
287 void SaveScreenBuffer(void *buffer, size_t bufferSize);
288
290 void RestoreScreenBuffer(const void *buffer, size_t bufferSize);
291
292
293private:
295 void Write(char chChar);
296
298 void CarriageReturn(void);
300 void ClearDisplayEnd(void);
302 void ClearLineEnd(void);
304 void CursorDown(void);
306 void CursorHome(void);
308 void CursorLeft(void);
310 void CursorMove(unsigned nRow, unsigned nColumn);
312 void CursorRight(void);
314 void CursorUp(void);
316 void DeleteChars(unsigned nCount);
318 void DeleteLines(unsigned nCount);
320 void DisplayChar(char chChar);
322 void EraseChars(unsigned nCount);
324 CDisplay::TRawColor GetTextBackgroundColor(void);
326 CDisplay::TRawColor GetTextColor(void);
328 void InsertLines(unsigned nCount);
330 void InsertMode(boolean bBegin);
332 void NewLine(void);
334 void ReverseScroll(void);
335
337 void SetScrollRegion(unsigned nStartRow, unsigned nEndRow);
339 void SetStandoutMode(unsigned nMode);
341 void Tabulator(void);
343 void BackTabulator(void);
344
346 void SaveCursor(void);
347
349 void RestoreCursor(void);
350
352 void Scroll(void);
354 boolean BeginSmoothScrollAnimation(unsigned nStartY, unsigned nEndY, boolean bScrollDown);
356 void RenderSmoothScrollFrame(void);
357
359 void DisplayChar(char chChar, unsigned nPosX, unsigned nPosY, CDisplay::TRawColor nColor);
361 void EraseChar(unsigned nPosX, unsigned nPosY);
363 void InvertCursor(void);
364
365
366 // We always update entire pixel lines.
368 void SetUpdateArea(unsigned nPosY1, unsigned nPosY2)
369 {
370 if (nPosY1 < m_UpdateArea.y1)
371 {
372 m_UpdateArea.y1 = nPosY1;
373 }
374
375 if (nPosY2 > m_UpdateArea.y2)
376 {
377 m_UpdateArea.y2 = nPosY2;
378 }
379 }
380
381 enum TState
382 {
383 StateStart,
384 StateEscape,
385 StateVT52Row,
386 StateVT52Col,
387 StateBracket,
388 StateNumber1,
389 StateQuestionMark,
390 StateSemicolon,
391 StateNumber2,
392 StateNumber3,
393 StateAutoPage,
394 StateFontChange,
395 StateSkipTillCRLF,
396 StateG0,
397 StateG1
398 };
399
400 enum ECharacterSet
401 {
402 CharSetUS,
403 CharSetGraphics
404 };
405
406 const TFont *m_pFont;
407 CCharGenerator::TFontFlags m_FontFlags;
408 CCharGenerator *m_pCharGen;
409 CCharGenerator *m_pGraphicsCharGen;
410 EFontSelection m_CurrentFontSelection;
411
412 ECharacterSet m_G0CharSet;
413 ECharacterSet m_G1CharSet;
414 boolean m_bUseG1;
415
416 CDisplay::TRawColor *m_pCursorPixels;
417 union
418 {
419 u8 *m_pBuffer8;
420 u16 *m_pBuffer16;
421 u32 *m_pBuffer32;
422 };
423 CBcmFrameBuffer *m_pFrameBuffer;
424 unsigned m_nDisplayIndex;
425 unsigned m_nSize;
426 unsigned m_nPitch;
427 unsigned m_nWidth;
428 unsigned m_nHeight;
429 unsigned m_nUsedWidth;
430 unsigned m_nUsedHeight;
431 unsigned m_nDepth;
432 CDisplay::TArea m_UpdateArea;
433 TState m_State;
434 unsigned m_nScrollStart;
435 unsigned m_nScrollEnd;
436 unsigned m_nCursorX;
437 unsigned m_nCursorY;
438 boolean m_bCursorOn;
439 boolean m_bCursorBlock;
440 boolean m_bBlinkingCursor;
441 boolean m_bCursorVisible;
442 unsigned m_nCursorBlinkPeriodTicks;
443 unsigned m_nNextCursorBlink;
444 CDisplay::TRawColor m_ForegroundColor;
445 CDisplay::TRawColor m_BackgroundColor;
446 CDisplay::TRawColor m_DefaultForegroundColor;
447 CDisplay::TRawColor m_DefaultBackgroundColor;
448 float m_BoldScaleFactor;
449 float m_DimScaleFactor;
450 float m_ReverseBackgroundScaleFactor;
451 float m_ReverseForegroundScaleFactor;
452 boolean m_bReverseAttribute;
453 boolean m_bBoldAttribute;
454 boolean m_bDimAttribute;
455 boolean m_bUnderlineAttribute;
456 boolean m_bBlinkAttribute;
457 boolean m_bInsertOn;
458 boolean m_bVT52Mode;
459 unsigned m_nParam1;
460 unsigned m_nParam2;
461 boolean m_bAutoPage;
462 boolean m_bDelayedUpdate;
463 unsigned m_nLastUpdateTicks;
464 boolean m_bSmoothScrollEnabled;
465 boolean m_bSmoothScrollActive;
466 boolean m_bSmoothScrollDown;
467 unsigned m_nSmoothScrollStartY;
468 unsigned m_nSmoothScrollEndY;
469 unsigned m_nSmoothScrollOffset;
470 unsigned m_nSmoothScrollStep;
471 unsigned m_nSmoothScrollLastTick;
472 unsigned m_nSmoothScrollTickInterval;
473 u8 *m_pSmoothScrollSnapshot;
474 u8 *m_pSmoothScrollCompose;
475 size_t m_nSmoothScrollBufferSize;
476 unsigned m_nSmoothScrollStartTick;
477 unsigned m_nSmoothScrollDebounceUntil; // tick until which we suppress smooth to avoid bursts
478 unsigned m_nScrollStatsLastLogTick;
479 unsigned long long m_ScrollNormalTicksAccum;
480 unsigned long long m_ScrollSmoothTicksAccum;
481 unsigned m_ScrollNormalCount;
482 unsigned m_ScrollSmoothCount;
483 TRendererState m_SavedState;
492 mutable CSpinLock m_SpinLock;
493};
Declares shared color selection enums for the VT100 renderer stack.
Declares the task that prepares VT100 font assets for rendering.
Combines Circle framebuffer access with a VT100-aware state machine.
Definition TRenderer.h:51
void RestoreState(const TRendererState &state)
Restore a previously saved renderer state.
Definition TRenderer.cpp:2936
void Update(void)
Periodic display maintenance invoked from the task loop.
Definition TRenderer.cpp:921
void RestoreScreenBuffer(const void *buffer, size_t bufferSize)
Restore the internal pixel buffer from a caller-provided buffer.
Definition TRenderer.cpp:3013
TRendererColor GetPixel(unsigned nPosX, unsigned nPosY)
Get the color value of a pixel.
Definition TRenderer.cpp:879
void SetVT52Mode(boolean bEnable)
Enable or disable VT52 emulation mode.
Definition TRenderer.cpp:2329
boolean SetFont(const TFont &rFont, CCharGenerator::TFontFlags FontFlags=CCharGenerator::FontFlagsNone)
Set the font to be used.
Definition TRenderer.cpp:256
void SetBlinkingCursor(boolean bBlinkingCursor, unsigned nPeriodMilliSeconds=500)
Enable or disable cursor blinking.
Definition TRenderer.cpp:894
void SetCursorBlock(boolean bCursorBlock)
Enable a block cursor instead of the default underline.
Definition TRenderer.cpp:889
~CTRenderer(void)
Release renderer resources and buffers.
Definition TRenderer.cpp:130
boolean SetColors(EColorSelection Foreground, EColorSelection Background)
Set the text colors using logical selections.
Definition TRenderer.cpp:418
unsigned GetHeight(void) const
Query the screen height in pixels.
Definition TRenderer.cpp:536
void Run(void) override
Entry point of the rendering task.
Definition TRenderer.cpp:488
unsigned GetCursorRow(void) const
Query the current cursor row (0-based).
Definition TRenderer.cpp:580
void SetRawPixel(unsigned nPosX, unsigned nPosY, CDisplay::TRawColor nColor)
Set a pixel to a specific raw color.
Definition TRenderer.cpp:684
void ResetParserState(void)
Reset ANSI parser state (used by VT tests).
Definition TRenderer.cpp:675
CBcmFrameBuffer * GetDisplay(void)
Access the underlying framebuffer device.
Definition TRenderer.cpp:599
void SaveState(TRendererState &state)
Save the current renderer state for later restore.
Definition TRenderer.cpp:2900
unsigned GetColumns(void) const
Query the screen width in characters.
Definition TRenderer.cpp:541
static CTRenderer * Get(void)
Access the singleton renderer instance.
Definition TRenderer.cpp:43
void SetSmoothScrollEnabled(boolean bEnable)
Enable or disable smooth-scroll animation.
Definition TRenderer.cpp:2272
unsigned GetCursorColumn(void) const
Query the current cursor column (0-based).
Definition TRenderer.cpp:561
void SetPixel(unsigned nPosX, unsigned nPosY, TRendererColor Color)
Set a pixel to a specific logical color.
Definition TRenderer.cpp:853
void ClearDisplay(void)
Clear entire display area and home the cursor.
Definition TRenderer.cpp:1768
boolean GetSmoothScrollEnabled(void) const
Query whether smooth-scroll animation is enabled.
Definition TRenderer.h:231
unsigned GetWidth(void) const
Query the screen width in pixels.
Definition TRenderer.cpp:531
CTRenderer(void)
Definition TRenderer.cpp:52
void SetBrightnessScaling(float boldFactor=1.6f, float reverseBackgroundFactor=0.7f, float reverseForegroundFactor=1.25f)
Set scaling factors for bold and reverse video attributes.
Definition TRenderer.cpp:2300
void ForceHideCursor(void)
Force-hide the cursor and restore underlying pixels.
Definition TRenderer.cpp:2334
boolean Initialize(void)
Initialize framebuffer access and Circle device registration.
Definition TRenderer.cpp:156
int Write(const void *pBuffer, size_t nCount) override
Write characters to screen.
Definition TRenderer.cpp:624
CDisplay::TRawColor GetRawPixel(unsigned nPosX, unsigned nPosY)
Get the raw color value of a pixel.
Definition TRenderer.cpp:715
size_t GetBufferSize(void) const
Query the size of the internal pixel buffer.
Definition TRenderer.cpp:2992
void doRenderTest(void)
Conduct a rendering self-test using various attributes.
Definition TRenderer.cpp:2751
void Goto(unsigned nRow, unsigned nColumn)
Move the cursor to a specific position.
Definition TRenderer.cpp:448
unsigned GetRows(void) const
Query the screen height in characters.
Definition TRenderer.cpp:551
void SetAutoPageMode(boolean bEnable)
Enable or disable automatic page mode (cursor wrap to top).
Definition TRenderer.cpp:2295
TRendererColor MapColor(EColorSelection color)
Translate a configured color selection into the renderer palette.
Definition TRenderer.cpp:401
CDisplay::TRawColor AdjustBrightness565(CDisplay::TRawColor color, float factor)
Adjust brightness of a raw RGB565 color.
Definition TRenderer.cpp:754
void SaveScreenBuffer(void *buffer, size_t bufferSize)
Save the internal pixel buffer into a caller-provided buffer.
Definition TRenderer.cpp:2997
CDisplay::TColor AdjustBrightness(CDisplay::TColor color, float factor)
Adjust brightness of a logical color.
Definition TRenderer.cpp:738
void SetCursorMode(boolean bVisible)
Enable or disable cursor visibility.
Definition TRenderer.cpp:2324
Definition TRenderer.h:61