Skip to content

Commit

Permalink
platform: simplify class hierarchy of DisplayAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
magiblot committed Nov 15, 2024
1 parent 87c9eab commit b0df20d
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 134 deletions.
38 changes: 33 additions & 5 deletions include/tvision/internal/ansiwrit.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
#define TVISION_ANSIWRIT_H

#define Uses_TScreenCell
#define Uses_TPoint
#include <tvision/tv.h>

#include <internal/termdisp.h>
#include <internal/endian.h>

namespace tvision
{

class ConsoleCtl;
class DisplayAdapter;

// TermColor represents a color that is to be printed to screen
// using certain ANSI escape sequences.

Expand Down Expand Up @@ -68,11 +71,36 @@ struct TermAttr
TColorAttr::Style style;
};

// Terminal quirk flags.

const ushort
qfBoldIsBright = 0x0001,
qfBlinkIsBright = 0x0002,
qfNoItalic = 0x0004,
qfNoUnderline = 0x0008;

enum TermCapColors : uint8_t
{
NoColor,
Indexed8,
Indexed16,
Indexed256,
Direct,
TermCapColorCount,
};

struct TermCap
{
TermCapColors colors;
ushort quirks;

static TermCap getDisplayCapabilities( ConsoleCtl &con,
DisplayAdapter &display ) noexcept;
};

// AnsiScreenWriter allows printing characters and color attributes directly
// to screen using ANSI escape codes.

class ConsoleCtl;

class AnsiScreenWriter
{
class Buffer
Expand All @@ -92,7 +120,7 @@ class AnsiScreenWriter
};

ConsoleCtl &con;
const TermCap &termcap;
TermCap termcap;
Buffer buf;
TPoint caretPos {-1, -1};
TermAttr lastAttr {};
Expand All @@ -102,7 +130,7 @@ class AnsiScreenWriter

public:

AnsiScreenWriter(ConsoleCtl &aCon, const TermCap &aTermcap) noexcept :
AnsiScreenWriter(ConsoleCtl &aCon, TermCap aTermcap) noexcept :
con(aCon),
termcap(aTermcap)
{
Expand Down
13 changes: 7 additions & 6 deletions include/tvision/internal/ncurdisp.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TVISION_NCURDISP_H
#define TVISION_NCURDISP_H

#include <internal/termdisp.h>
#include <internal/platform.h>

#ifdef HAVE_NCURSES

Expand All @@ -11,20 +11,21 @@
namespace tvision
{

class NcursesDisplay final : public TerminalDisplay
class NcursesDisplay final : public DisplayAdapter
{
public:

// The lifetime of 'con' exceeds that of 'this'.
NcursesDisplay(ConsoleCtl &con) noexcept;
// The lifetime of 'con' exceeds that of the returned object.
static NcursesDisplay &create(ConsoleCtl &con) noexcept;
~NcursesDisplay();

private:

AnsiScreenWriter ansiScreenWriter;
ConsoleCtl &con;
SCREEN *term;
AnsiScreenWriter ansiScreenWriter;

protected:
NcursesDisplay(ConsoleCtl &, SCREEN *) noexcept;

TPoint reloadScreenInfo() noexcept override;

Expand Down
63 changes: 0 additions & 63 deletions include/tvision/internal/termdisp.h

This file was deleted.

10 changes: 5 additions & 5 deletions include/tvision/internal/win32con.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <tvision/tv.h>
#include <compat/windows/windows.h>
#include <internal/termdisp.h>
#include <internal/platform.h>
#include <internal/termio.h>
#include <internal/ansiwrit.h>

Expand Down Expand Up @@ -65,7 +65,7 @@ class Win32Input final : public InputAdapter
bool getEvent(TEvent &ev) noexcept override;
};

class Win32Display final : public TerminalDisplay
class Win32Display final : public DisplayAdapter
{
public:

Expand All @@ -75,17 +75,17 @@ class Win32Display final : public TerminalDisplay

private:

ConsoleCtl &con;

TPoint size {};
CONSOLE_FONT_INFO lastFontInfo {};

AnsiScreenWriter *ansiScreenWriter;
AnsiScreenWriter *ansiScreenWriter {nullptr};

TPoint caretPos {-1, -1};
uchar lastAttr {'\x00'};
std::vector<char> buf;

protected:

TPoint reloadScreenInfo() noexcept override;

int getColorCount() noexcept override;
Expand Down
37 changes: 36 additions & 1 deletion source/platform/ansiwrit.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
#include <internal/ansiwrit.h>
#include <internal/termdisp.h>
#include <internal/platform.h>
#include <internal/strings.h>
#include <internal/conctl.h>
#include <internal/getenv.h>
#include <stdlib.h>

#define CSI "\x1B["

namespace tvision
{

TermCap TermCap::getDisplayCapabilities( ConsoleCtl &con,
DisplayAdapter &display ) noexcept
{
TermCap termcap {};
auto colorterm = getEnv<TStringView>("COLORTERM");
if (colorterm == "truecolor" || colorterm == "24bit")
termcap.colors = Direct;
else
{
int colors = display.getColorCount();
if (colors >= 256*256*256)
termcap.colors = Direct;
else if (colors >= 256)
termcap.colors = Indexed256;
else if (colors >= 16)
termcap.colors = Indexed16;
else if (colors >= 8)
{
termcap.colors = Indexed8;
termcap.quirks |= qfBoldIsBright;
#ifdef __linux__
if (con.isLinuxConsole())
termcap.quirks |= qfBlinkIsBright | qfNoItalic | qfNoUnderline;
else
#endif // __linux__
if (getEnv<TStringView>("TERM") == "xterm")
// Let's assume all terminals disguising themselves as 'xterm'
// support at least 16 colors.
termcap.colors = Indexed16;
}
}
return termcap;
}

inline AnsiScreenWriter::Buffer::~Buffer()
{
free(head);
Expand Down
22 changes: 15 additions & 7 deletions source/platform/ncurdisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
#include <internal/conctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>

namespace tvision
{

NcursesDisplay::NcursesDisplay(ConsoleCtl &aCon) noexcept :
TerminalDisplay(aCon),
ansiScreenWriter(aCon, TerminalDisplay::termcap)
inline NcursesDisplay::NcursesDisplay(ConsoleCtl &aCon, SCREEN *aTerm) noexcept :
con(aCon),
term(aTerm),
ansiScreenWriter(aCon, TermCap::getDisplayCapabilities(con, *this))
{
}

NcursesDisplay &NcursesDisplay::create(ConsoleCtl &con) noexcept
{
// Start curses mode.
term = newterm(nullptr, con.fout(), con.fin());
SCREEN *term = newterm(nullptr, con.fout(), con.fin());
if (!term)
{
fputs("Cannot initialize Ncurses: 'newterm' failed.\n", stderr);
const char *termEnv = getenv("TERM");
if (!termEnv)
termEnv = "";
fprintf(stderr, "Cannot initialize Ncurses: 'newterm' failed (TERM is '%s').\n", termEnv);
exit(1);
}
// Enable colors if the terminal supports it.
Expand All @@ -27,10 +34,11 @@ NcursesDisplay::NcursesDisplay(ConsoleCtl &aCon) noexcept :
// Use default colors when clearing the screen.
use_default_colors();
}
TerminalDisplay::initCapabilities();
/* Refresh now so that a possible first getch() doesn't make any relevant
* changes to the screen due to its implicit refresh(). */
wrefresh(stdscr);

return *new NcursesDisplay(con, term);
}

NcursesDisplay::~NcursesDisplay()
Expand Down
3 changes: 1 addition & 2 deletions source/platform/platfcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <internal/sighandl.h>
#include <internal/conctl.h>
#include <internal/termio.h>
#include <internal/getenv.h>

namespace tvision
{
Expand All @@ -27,7 +26,7 @@ ConsoleAdapter &Platform::createConsole() noexcept
#else
auto &con = ConsoleCtl::getInstance();
InputState &inputState = *new InputState;
NcursesDisplay &display = *new NcursesDisplay(con);
NcursesDisplay &display = NcursesDisplay::create(con);
#ifdef __linux__
if (con.isLinuxConsole())
return LinuxConsoleAdapter::create(con, displayBuf, inputState, display, *new NcursesInput(con, display, inputState, false));
Expand Down
41 changes: 0 additions & 41 deletions source/platform/termdisp.cpp

This file was deleted.

7 changes: 3 additions & 4 deletions source/platform/win32con.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,10 @@ bool Win32Input::getEvent(const INPUT_RECORD &ir, TEvent &ev) noexcept
// Win32Display

Win32Display::Win32Display(ConsoleCtl &aCon, bool useAnsi) noexcept :
TerminalDisplay(aCon),
ansiScreenWriter( useAnsi ? new AnsiScreenWriter(aCon, TerminalDisplay::termcap)
: nullptr )
con(aCon)
{
TerminalDisplay::initCapabilities();
if (useAnsi)
ansiScreenWriter = new AnsiScreenWriter(con, TermCap::getDisplayCapabilities(con, *this));
}

Win32Display::~Win32Display()
Expand Down

0 comments on commit b0df20d

Please sign in to comment.