Skip to content

Commit

Permalink
[vtbackend] Implements DSR ?996 to report color palette updates (expl…
Browse files Browse the repository at this point in the history
…icit and unsolicited) and adds DEC mode 2031 to enable/disable unsolicited reports of that

Signed-off-by: Christian Parpart <[email protected]>
  • Loading branch information
christianparpart committed Oct 10, 2023
1 parent 19d6fac commit abb0e18
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@
<li>Removes the ability to inline colorschemes within a configuration profile. Colorschemes must now always be referenced by their name.</li>
<li>Adds the ability to chose a color scheme based on the operating systems's dark/light mode setting. This will change live whenever the OS's dark/light mode setting changes as well (#604).</li>
<li>Adds VT sequence DECSSCLS (change scroll speed) and properly handle DECSCLM (enable slow scrolling mode) (#1204)</li>
<li>Adds VT sequence parameter ?996 to DSR to request a report of current color scheme dark/light mode hint.</li>
<li>Adds VT sequence `SM ?2031` and `RM ?2031` to enable/disable unsolicited DSR for color scheme updates by the user or OS.</li>
<li>Adds percentage value to Indicator Statusline to indicate scroll offset in scrollback buffer.</li>
<li>Adds inheritance of profiles in configuration file based on default profile (#1063).</li>
<li>Adds config option `profile.*.bell` to adjust BEL behavior and fixes (#1162) and (#1163).</li>
Expand Down
24 changes: 24 additions & 0 deletions src/vtbackend/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ using std::vector;
namespace vtbackend
{

auto constexpr inline ColorPaletteUpdateDsrRequestId = 996;
auto constexpr inline ColorPaletteUpdateDsrReplyId = 997;

auto constexpr inline TabWidth = ColumnCount(8);

auto const inline vtCaptureBufferLog = logstore::category("vt.ext.capturebuffer",
Expand All @@ -92,6 +95,11 @@ auto const inline vtCaptureBufferLog = logstore::category("vt.ext.capturebuffer"

namespace // {{{ helper
{
constexpr bool isLightColor(RGBColor color) noexcept
{
return ((5 * color.green) + (2 * color.red) + color.blue) > 8 * 128;
}

template <typename Rep, typename Period>
inline void sleep_for(std::chrono::duration<Rep, Period> const& rtime)
{
Expand Down Expand Up @@ -877,6 +885,19 @@ void Screen<Cell>::reportCursorPosition()
_terminal->reply("\033[{};{}R", logicalCursorPosition().line + 1, logicalCursorPosition().column + 1);
}

template <typename Cell>
CRISPY_REQUIRES(CellConcept<Cell>)
void Screen<Cell>::reportColorPaletteUpdate()
{
auto constexpr DarkModeHint = 1;
auto constexpr LightModeHint = 2;

auto const modeHint = isLightColor(_state->colorPalette.defaultForeground) ? DarkModeHint : LightModeHint;

_terminal->reply("\033[?{};{}n", ColorPaletteUpdateDsrReplyId, modeHint);
_terminal->flushInput();
}

template <typename Cell>
CRISPY_REQUIRES(CellConcept<Cell>)
void Screen<Cell>::reportExtendedCursorPosition()
Expand Down Expand Up @@ -2800,6 +2821,9 @@ namespace impl
{
switch (seq.param(0))
{
case ColorPaletteUpdateDsrRequestId:
screen.reportColorPaletteUpdate();
return ApplyResult::Ok;
default: return ApplyResult::Unsupported;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/vtbackend/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ScreenBase: public SequenceHandler
void resetSavedCursorState() { _savedCursor = {}; }
virtual void saveCursor() = 0;
virtual void restoreCursor() = 0;
virtual void reportColorPaletteUpdate() = 0;

[[nodiscard]] virtual Margin margin() const noexcept = 0;
[[nodiscard]] virtual Margin& margin() noexcept = 0;
Expand Down Expand Up @@ -220,6 +221,7 @@ class Screen final: public ScreenBase, public capabilities::StaticDatabase
void deviceStatusReport(); // DSR
void reportCursorPosition(); // CPR
void reportExtendedCursorPosition(); // DECXCPR
void reportColorPaletteUpdate() override;
void selectConformanceLevel(VTType level);
void requestDynamicColor(DynamicColorName name);
void requestCapability(capabilities::Code code);
Expand Down
3 changes: 3 additions & 0 deletions src/vtbackend/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,9 @@ void Terminal::resetColorPalette(ColorPalette const& colors)
_state.defaultColorPalette = colors;
_settings.colorPalette = colors;
_factorySettings.colorPalette = colors;

if (isModeEnabled(DECMode::ReportColorPaletteUpdated))
_currentScreen.get().reportColorPaletteUpdate();
}

void Terminal::pushColorPalette(size_t slot)
Expand Down
1 change: 1 addition & 0 deletions src/vtbackend/TerminalState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ std::string to_string(DECMode mode)
case DECMode::Unicode: return "Unicode";
case DECMode::TextReflow: return "TextReflow";
case DECMode::SixelCursorNextToGraphic: return "SixelCursorNextToGraphic";
case DECMode::ReportColorPaletteUpdated: return "ReportColorPaletteUpdated";
}
return fmt::format("({})", static_cast<unsigned>(mode));
}
Expand Down
6 changes: 6 additions & 0 deletions src/vtbackend/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,10 @@ enum class DECMode
// intersecting with the main page area.
ReportGridCellSelection = 2030,

// If enabled, the terminal will report color palette changes to the application,
// if modified by the user or operating system (e.g. dark/light mode adaption).
ReportColorPaletteUpdated = 2031,

// If enabled (default, as per spec), then the cursor is left next to the graphic,
// that is, the text cursor is placed at the position of the sixel cursor.
// If disabled otherwise, the cursor is placed below the image, as if CR LF was sent,
Expand Down Expand Up @@ -791,6 +795,7 @@ constexpr unsigned toDECModeNum(DECMode m)
case DECMode::MouseAlternateScroll: return 1007;
case DECMode::MousePassiveTracking: return 2029;
case DECMode::ReportGridCellSelection: return 2030;
case DECMode::ReportColorPaletteUpdated: return 2031;
case DECMode::BatchedRendering: return 2026;
case DECMode::Unicode: return 2027;
case DECMode::TextReflow: return 2028;
Expand Down Expand Up @@ -837,6 +842,7 @@ constexpr bool isValidDECMode(unsigned int mode) noexcept
case DECMode::MouseAlternateScroll:
case DECMode::MousePassiveTracking:
case DECMode::ReportGridCellSelection:
case DECMode::ReportColorPaletteUpdated:
case DECMode::BatchedRendering:
case DECMode::Unicode:
case DECMode::TextReflow:
Expand Down

0 comments on commit abb0e18

Please sign in to comment.