fa7c1abdf8
This PR introduces a new `ColorType` to allow us to distinguish between `SGR` indexed colors from the 16 color table, the lower half of which can be brightened, and the ISO/ITU indexed colors from the 256 color table, which have a fixed brightness. Retaining the distinction between these two types will enable us to forward the correct `SGR` sequences to conpty when addressing issue #2661. The other benefit of retaining the color index (which we didn't previously do for ISO/ITU colors) is that it ensures that the colors are updated correctly when the color scheme is changed. ## References * This is another step towards fixing the conpty narrowing bugs in issue #2661. * This is technically a fix for issue #5384, but that won't be apparent until #2661 is complete. ## PR Checklist * [x] Closes #1223 * [x] CLA signed. * [x] Tests added/passed * [ ] Requires documentation to be updated * [x] I've discussed this with core contributors already. ## Detailed Description of the Pull Request / Additional comments The first part of this PR was the introduction of a new `ColorType` in the `TextColor` class. Instead of just the one `IsIndex` type, there is now an `IsIndex16` and an `IsIndex256`. `IsIndex16` covers the eight original ANSI colors set with `SGR 3x` and `SGR 4x`, as well as the brighter aixterm variants set with `SGR 9x` and `SGR 10x`. `IsIndex256` covers the 256 ISO/ITU indexed colors set with `SGR 38;5` and `SGR 48;5`. There are two reasons for this distinction. The first is that the ANSI colors have the potential to be brightened by the `SGR 1` bold attribute, while the ISO/ITO color do not. The second reason is that when forwarding an attributes through conpty, we want to try and preserve the original SGR sequence that generated each color (to the extent that that is possible). By having the two separate types, we can map the `IsIndex16` colors back to ANSI/aixterm values, and `IsIndex256` to the ISO/ITU sequences. In addition to the VT colors, we also have to deal with the legacy colors set by the Windows console APIs, but we don't really need a separate type for those. It seemed most appropriate to me to store them as `IsIndex256` colors, since it doesn't make sense to have them brightened by the `SGR 1` attribute (which is what would happen if they were stored as `IsIndex16`). If a console app wanted a bright color it would have selected one, so we shouldn't be messing with that choice. The second part of the PR was the unification of the two color tables. Originally we had a 16 color table for the legacy colors, and a separate table for the 256 ISO/ITU colors. These have now been merged into one, so color table lookups no longer need to decide which of the two tables they should be referencing. I've also updated all the methods that took a color table as a parameter to use a `basic_string_view` instead of separate pointer and length variables, which I think makes them a lot easier and safer to work with. With this new architecture in place, I could now update the `AdaptDispatch` SGR implementation to store the ISO/ITU indexed colors as `IsIndex256` values, where before they were mapped to RGB values (which prevented them reflecting any color scheme changes). I could also update the `TerminalDispatch` implementation to differentiate between the two index types, so that the `SGR 1` brightening would only be applied to the ANSI colors. I've also done a bit of code refactoring to try and minimise any direct access to the color tables, getting rid of a lot of places that were copying tables with `memmove` operations. I'm hoping this will make it easier for us to update the code in the future if we want to reorder the table entries (which is likely a requirement for unifying the `AdaptDispatch` and `TerminalDispatch` implementations). ## Validation Steps Performed For testing, I've just updated the existing unit tests to account for the API changes. The `TextColorTests` required an extra parameter specifying the index type when setting an index. And the `AdapterTest` and `ScreenBufferTests` required the use of the new `SetIndexedXXX` methods in order to be explicit about the index type, instead of relying on the `TextAttribute` constructor and the old `SetForeground` and `SetBackground` methods which didn't have a way to differentiate index types. I've manually tested the various console APIs (`SetConsoleTextAttribute`, `ReadConsoleOutputAttribute`, and `ReadConsoleOutput`), to make sure they are still setting and reading the attributes as well as they used to. And I've tested the `SetConsoleScreenBufferInfoEx` and `GetConsoleScreenBufferInfoEx` APIs to make sure they can read and write the color table correctly. I've also tested the color table in the properties dialog, made sure it was saved and restored from the registry correctly, and similarly saved and restored from a shortcut link. Note that there are still a bunch of issues with the color table APIs, but no new problems have been introduced by the changes in this PR, as far as I could tell. I've also done a bunch of manual tests of `OSC 4` to make sure it's updating all the colors correctly (at least in conhost), and confirmed that the test case in issue #1223 now works as expected.
184 lines
9 KiB
C++
184 lines
9 KiB
C++
/*++
|
|
Copyright (c) Microsoft Corporation
|
|
Licensed under the MIT license.
|
|
|
|
Module Name:
|
|
- adaptDispatch.hpp
|
|
|
|
Abstract:
|
|
- This serves as the Windows Console API-specific implementation of the callbacks from our generic Virtual Terminal parser.
|
|
|
|
Author(s):
|
|
- Michael Niksa (MiNiksa) 30-July-2015
|
|
--*/
|
|
|
|
#pragma once
|
|
|
|
#include "termDispatch.hpp"
|
|
#include "DispatchCommon.hpp"
|
|
#include "conGetSet.hpp"
|
|
#include "adaptDefaults.hpp"
|
|
#include "terminalOutput.hpp"
|
|
|
|
namespace Microsoft::Console::VirtualTerminal
|
|
{
|
|
class AdaptDispatch : public ITermDispatch
|
|
{
|
|
public:
|
|
AdaptDispatch(std::unique_ptr<ConGetSet> pConApi,
|
|
std::unique_ptr<AdaptDefaults> pDefaults);
|
|
|
|
void Execute(const wchar_t wchControl) override
|
|
{
|
|
_pDefaults->Execute(wchControl);
|
|
}
|
|
|
|
void PrintString(const std::wstring_view string) override;
|
|
void Print(const wchar_t wchPrintable) override;
|
|
|
|
bool CursorUp(const size_t distance) override; // CUU
|
|
bool CursorDown(const size_t distance) override; // CUD
|
|
bool CursorForward(const size_t distance) override; // CUF
|
|
bool CursorBackward(const size_t distance) override; // CUB, BS
|
|
bool CursorNextLine(const size_t distance) override; // CNL
|
|
bool CursorPrevLine(const size_t distance) override; // CPL
|
|
bool CursorHorizontalPositionAbsolute(const size_t column) override; // HPA, CHA
|
|
bool VerticalLinePositionAbsolute(const size_t line) override; // VPA
|
|
bool HorizontalPositionRelative(const size_t distance) override; // HPR
|
|
bool VerticalPositionRelative(const size_t distance) override; // VPR
|
|
bool CursorPosition(const size_t line, const size_t column) override; // CUP, HVP
|
|
bool CursorSaveState() override; // DECSC
|
|
bool CursorRestoreState() override; // DECRC
|
|
bool CursorVisibility(const bool isVisible) override; // DECTCEM
|
|
bool EraseInDisplay(const DispatchTypes::EraseType eraseType) override; // ED
|
|
bool EraseInLine(const DispatchTypes::EraseType eraseType) override; // EL
|
|
bool EraseCharacters(const size_t numChars) override; // ECH
|
|
bool InsertCharacter(const size_t count) override; // ICH
|
|
bool DeleteCharacter(const size_t count) override; // DCH
|
|
bool SetGraphicsRendition(const std::basic_string_view<DispatchTypes::GraphicsOptions> options) override; // SGR
|
|
bool DeviceStatusReport(const DispatchTypes::AnsiStatusType statusType) override; // DSR, DSR-OS, DSR-CPR
|
|
bool DeviceAttributes() override; // DA1
|
|
bool ScrollUp(const size_t distance) override; // SU
|
|
bool ScrollDown(const size_t distance) override; // SD
|
|
bool InsertLine(const size_t distance) override; // IL
|
|
bool DeleteLine(const size_t distance) override; // DL
|
|
bool SetColumns(const size_t columns) override; // DECCOLM
|
|
bool SetPrivateModes(const std::basic_string_view<DispatchTypes::PrivateModeParams> params) override; // DECSET
|
|
bool ResetPrivateModes(const std::basic_string_view<DispatchTypes::PrivateModeParams> params) override; // DECRST
|
|
bool SetCursorKeysMode(const bool applicationMode) override; // DECCKM
|
|
bool SetKeypadMode(const bool applicationMode) override; // DECKPAM, DECKPNM
|
|
bool EnableCursorBlinking(const bool enable) override; // ATT610
|
|
bool SetScreenMode(const bool reverseMode) override; // DECSCNM
|
|
bool SetOriginMode(const bool relativeMode) noexcept override; // DECOM
|
|
bool SetAutoWrapMode(const bool wrapAtEOL) override; // DECAWM
|
|
bool SetTopBottomScrollingMargins(const size_t topMargin,
|
|
const size_t bottomMargin) override; // DECSTBM
|
|
bool WarningBell() override; // BEL
|
|
bool CarriageReturn() override; // CR
|
|
bool LineFeed(const DispatchTypes::LineFeedType lineFeedType) override; // IND, NEL, LF, FF, VT
|
|
bool ReverseLineFeed() override; // RI
|
|
bool SetWindowTitle(const std::wstring_view title) override; // OscWindowTitle
|
|
bool UseAlternateScreenBuffer() override; // ASBSET
|
|
bool UseMainScreenBuffer() override; // ASBRST
|
|
bool HorizontalTabSet() override; // HTS
|
|
bool ForwardTab(const size_t numTabs) override; // CHT, HT
|
|
bool BackwardsTab(const size_t numTabs) override; // CBT
|
|
bool TabClear(const size_t clearType) override; // TBC
|
|
bool DesignateCharset(const wchar_t wchCharset) noexcept override; // SCS
|
|
bool SoftReset() override; // DECSTR
|
|
bool HardReset() override; // RIS
|
|
bool ScreenAlignmentPattern() override; // DECALN
|
|
bool EnableDECCOLMSupport(const bool enabled) noexcept override; // ?40
|
|
bool EnableVT200MouseMode(const bool enabled) override; // ?1000
|
|
bool EnableUTF8ExtendedMouseMode(const bool enabled) override; // ?1005
|
|
bool EnableSGRExtendedMouseMode(const bool enabled) override; // ?1006
|
|
bool EnableButtonEventMouseMode(const bool enabled) override; // ?1002
|
|
bool EnableAnyEventMouseMode(const bool enabled) override; // ?1003
|
|
bool EnableAlternateScroll(const bool enabled) override; // ?1007
|
|
bool SetCursorStyle(const DispatchTypes::CursorStyle cursorStyle) override; // DECSCUSR
|
|
bool SetCursorColor(const COLORREF cursorColor) override;
|
|
|
|
bool SetColorTableEntry(const size_t tableIndex,
|
|
const DWORD color) override; // OscColorTable
|
|
bool SetDefaultForeground(const DWORD color) override; // OSCDefaultForeground
|
|
bool SetDefaultBackground(const DWORD color) override; // OSCDefaultBackground
|
|
|
|
bool WindowManipulation(const DispatchTypes::WindowManipulationType function,
|
|
const std::basic_string_view<size_t> parameters) override; // DTTERM_WindowManipulation
|
|
|
|
private:
|
|
enum class ScrollDirection
|
|
{
|
|
Up,
|
|
Down
|
|
};
|
|
struct CursorState
|
|
{
|
|
unsigned int Row = 1;
|
|
unsigned int Column = 1;
|
|
bool IsOriginModeRelative = false;
|
|
TextAttribute Attributes = {};
|
|
TerminalOutput TermOutput = {};
|
|
};
|
|
struct Offset
|
|
{
|
|
int Value;
|
|
bool IsAbsolute;
|
|
// VT origin is at 1,1 so we need to subtract 1 from absolute positions.
|
|
static constexpr Offset Absolute(const size_t value) { return { gsl::narrow_cast<int>(value) - 1, true }; };
|
|
static constexpr Offset Forward(const size_t value) { return { gsl::narrow_cast<int>(value), false }; };
|
|
static constexpr Offset Backward(const size_t value) { return { -gsl::narrow_cast<int>(value), false }; };
|
|
static constexpr Offset Unchanged() { return Forward(0); };
|
|
};
|
|
|
|
bool _CursorMovePosition(const Offset rowOffset, const Offset colOffset, const bool clampInMargins) const;
|
|
bool _EraseSingleLineHelper(const CONSOLE_SCREEN_BUFFER_INFOEX& csbiex,
|
|
const DispatchTypes::EraseType eraseType,
|
|
const size_t lineId) const;
|
|
bool _EraseScrollback();
|
|
bool _EraseAll();
|
|
bool _InsertDeleteHelper(const size_t count, const bool isInsert) const;
|
|
bool _ScrollMovement(const ScrollDirection dir, const size_t distance) const;
|
|
|
|
bool _DoSetTopBottomScrollingMargins(const size_t topMargin,
|
|
const size_t bottomMargin);
|
|
bool _OperatingStatus() const;
|
|
bool _CursorPositionReport() const;
|
|
|
|
bool _WriteResponse(const std::wstring_view reply) const;
|
|
bool _SetResetPrivateModes(const std::basic_string_view<DispatchTypes::PrivateModeParams> params, const bool enable);
|
|
bool _PrivateModeParamsHelper(const DispatchTypes::PrivateModeParams param, const bool enable);
|
|
bool _DoDECCOLMHelper(const size_t columns);
|
|
|
|
bool _ClearSingleTabStop();
|
|
bool _ClearAllTabStops() noexcept;
|
|
void _ResetTabStops() noexcept;
|
|
void _InitTabStopsForWidth(const size_t width);
|
|
|
|
std::vector<bool> _tabStopColumns;
|
|
bool _initDefaultTabStops = true;
|
|
|
|
std::unique_ptr<ConGetSet> _pConApi;
|
|
std::unique_ptr<AdaptDefaults> _pDefaults;
|
|
TerminalOutput _termOutput;
|
|
|
|
// We have two instances of the saved cursor state, because we need
|
|
// one for the main buffer (at index 0), and another for the alt buffer
|
|
// (at index 1). The _usingAltBuffer property keeps tracks of which
|
|
// buffer is active, so can be used as an index into this array to
|
|
// obtain the saved state that should be currently active.
|
|
std::array<CursorState, 2> _savedCursorState;
|
|
bool _usingAltBuffer;
|
|
|
|
SMALL_RECT _scrollMargins;
|
|
|
|
bool _isOriginModeRelative;
|
|
|
|
bool _isDECCOLMAllowed;
|
|
|
|
size_t _SetRgbColorsHelper(const std::basic_string_view<DispatchTypes::GraphicsOptions> options,
|
|
TextAttribute& attr,
|
|
const bool isForeground) noexcept;
|
|
};
|
|
}
|