terminal/src/host/settings.hpp
James Holderness b604117421
Standardize the color table order (#11602)
## Summary of the Pull Request

In the original implementation, we used two different orderings for the color tables. The WT color table used ANSI order, while the conhost color table used a Windows-specific order. This PR standardizes on the ANSI color order everywhere, so the usage of indexed colors is consistent across both parts of the code base, which will hopefully allow more of the code to be shared one day.

## References

This is another small step towards de-duplicating `AdaptDispatch` and `TerminalDispatch` for issue #3849, and is essentially a followup to the SGR dispatch refactoring in PR #6728.

## PR Checklist
* [x] Closes #11461
* [x] CLA signed.
* [x] Tests added/passed
* [ ] Documentation updated.
* [ ] Schema updated.
* [x] I've discussed this with core contributors already. Issue number where discussion took place: #11461

## Detailed Description of the Pull Request / Additional comments

Conhost still needs to deal with legacy attributes using Windows color order, so those values now need to be transposed to ANSI colors order when creating a `TextAttribute` object. This is done with a simple mapping table, which also handles the translation of the default color entries, so it's actually slightly faster than the original code.

And when converting `TextAttribute` values back to legacy console attributes, we were already using a mapping table to handle the narrowing of 256-color values down to 16 colors, so we just needed to adjust that table to account for the translation from ANSI to Windows, and then could make use of the same table for both 256-color and 16-color values.

There are also a few places in conhost that read from or write to the color tables, and those now need to transpose the index values. I've addressed this by creating separate `SetLegacyColorTableEntry` and `GetLegacyColorTableEntry` methods in the `Settings` class which take care of the mapping, so it's now clearer in which cases the code is dealing with legacy values, and which are ANSI values.

These methods are used in the `SetConsoleScreenBufferInfoEx` and `GetConsoleScreenBufferInfoEx` APIs, as well as a few place where color preferences are handled (the registry, shortcut links, and the properties dialog), none of which are particularly sensitive to performance. However, we also use the legacy table when looking up the default colors for rendering (which happens a lot), so I've refactored that code so the default color calculations now only occur once per frame.

The plus side of all of this is that the VT code doesn't need to do the index translation anymore, so we can finally get rid of all the calls to `XTermToWindowsIndex`, and we no longer need a separate color table initialization method for conhost, so I was able to merge a number of color initialization methods into one. We also no longer need to translate from legacy values to ANSI when generating VT sequences for conpty.

The one exception to that is the 16-color VT renderer, which uses the `TextColor::GetLegacyIndex` method to approximate 16-color equivalents for RGB and 256-color values. Since that method returns a legacy index, it still needs to be translated to ANSI before it can be used in a VT sequence. But this should be no worse than it was before.

One more special case is conhost's secret _Color Selection_ feature. That uses `Ctrl`+Number and `Alt`+Number key sequences to highlight parts of the buffer, and the mapping from number to color is based on the Windows color order. So that mapping now needs to be transposed, but that's also not performance sensitive.

The only thing that I haven't bothered to update is the trace logging code in the `Telemetry` class, which logs the first 16 entries in the color table. Those entries are now going to be in a different order, but I didn't think that would be of great concern to anyone.

## Validation Steps Performed

A lot of unit tests needed to be updated to use ANSI color constants when setting indexed colors, where before they might have been expecting values in Windows order. But this replaced a wild mix of different constants, sometimes having to use bit shifting, as well as values mapped with `XTermToWindowsIndex`, so I think the tests are a whole lot clearer now. Only a few cases have been left with literal numbers where that seemed more appropriate.

In addition to getting the unit tests working, I've also manually tested the behaviour of all the console APIs which I thought could be affected by these changes, and confirmed that they produced the same results in the new code as they did in the original implementation.

This includes:
- `WriteConsoleOutput`
- `ReadConsoleOutput`
- `SetConsoleTextAttribute` with `WriteConsoleOutputCharacter`
- `FillConsoleOutputAttribute` and `FillConsoleOutputCharacter` 
- `ScrollConsoleScreenBuffer`
- `GetConsoleScreenBufferInfo`
- `GetConsoleScreenBufferInfoEx`
- `SetConsoleScreenBufferInfoEx`

I've also manually tested changing colors via the console properties menu, the registry, and shortcut links, including setting default colors and popup colors. And I've tested that the "Quirks Mode" is still working as expected in PowerShell.

In terms of performance, I wrote a little test app that filled a 80x9999 buffer with random color combinations using `WriteConsoleOutput`, which I figured was likely to be the most performance sensitive call, and I think it now actually performs slightly better than the original implementation.

I've also tested similar code - just filling the visible window - with SGR VT sequences of various types, and the performance seems about the same as it was before.
2021-11-04 22:13:22 +00:00

255 lines
8.2 KiB
C++

/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
Module Name:
- settings.hpp
Abstract:
- This module is used for all configurable settings in the console
Author(s):
- Michael Niksa (MiNiksa) 23-Jul-2014
- Paul Campbell (PaulCam) 23-Jul-2014
Revision History:
- From components of consrv.h
- This is a reduced/de-duplicated version of settings that were stored in the registry, link files, and in the console information state.
--*/
#pragma once
#include "../buffer/out/TextAttribute.hpp"
// To prevent invisible windows, set a lower threshold on window alpha channel.
constexpr unsigned short MIN_WINDOW_OPACITY = 0x4D; // 0x4D is approximately 30% visible/opaque (70% transparent). Valid range is 0x00-0xff.
#include "ConsoleArguments.hpp"
#include "../inc/conattrs.hpp"
class Settings
{
public:
Settings();
void ApplyDesktopSpecificDefaults();
void ApplyStartupInfo(const Settings* const pStartupSettings);
void ApplyCommandlineArguments(const ConsoleArguments& consoleArgs);
void InitFromStateInfo(_In_ PCONSOLE_STATE_INFO pStateInfo);
void Validate();
CONSOLE_STATE_INFO CreateConsoleStateInfo() const;
DWORD GetVirtTermLevel() const;
void SetVirtTermLevel(const DWORD dwVirtTermLevel);
bool IsAltF4CloseAllowed() const;
void SetAltF4CloseAllowed(const bool fAllowAltF4Close);
bool IsReturnOnNewlineAutomatic() const;
void SetAutomaticReturnOnNewline(const bool fAutoReturnOnNewline);
bool IsGridRenderingAllowedWorldwide() const;
void SetGridRenderingAllowedWorldwide(const bool fGridRenderingAllowed);
bool IsScreenReversed() const;
void SetScreenReversed(const bool fScreenReversed);
bool GetFilterOnPaste() const;
void SetFilterOnPaste(const bool fFilterOnPaste);
const std::wstring_view GetLaunchFaceName() const;
void SetLaunchFaceName(const std::wstring_view launchFaceName);
UINT GetCodePage() const;
void SetCodePage(const UINT uCodePage);
UINT GetScrollScale() const;
void SetScrollScale(const UINT uScrollScale);
bool GetTrimLeadingZeros() const;
void SetTrimLeadingZeros(const bool fTrimLeadingZeros);
bool GetEnableColorSelection() const;
void SetEnableColorSelection(const bool fEnableColorSelection);
bool GetLineSelection() const;
void SetLineSelection(const bool bLineSelection);
bool GetWrapText() const;
void SetWrapText(const bool bWrapText);
bool GetCtrlKeyShortcutsDisabled() const;
void SetCtrlKeyShortcutsDisabled(const bool fCtrlKeyShortcutsDisabled);
BYTE GetWindowAlpha() const;
void SetWindowAlpha(const BYTE bWindowAlpha);
DWORD GetHotKey() const;
void SetHotKey(const DWORD dwHotKey);
bool IsStartupTitleIsLinkNameSet() const;
DWORD GetStartupFlags() const;
void SetStartupFlags(const DWORD dwStartupFlags);
void UnsetStartupFlag(const DWORD dwFlagToUnset);
WORD GetFillAttribute() const;
void SetFillAttribute(const WORD wFillAttribute);
WORD GetPopupFillAttribute() const;
void SetPopupFillAttribute(const WORD wPopupFillAttribute);
WORD GetShowWindow() const;
void SetShowWindow(const WORD wShowWindow);
WORD GetReserved() const;
void SetReserved(const WORD wReserved);
COORD GetScreenBufferSize() const;
void SetScreenBufferSize(const COORD dwScreenBufferSize);
COORD GetWindowSize() const;
void SetWindowSize(const COORD dwWindowSize);
bool IsWindowSizePixelsValid() const;
COORD GetWindowSizePixels() const;
void SetWindowSizePixels(const COORD dwWindowSizePixels);
COORD GetWindowOrigin() const;
void SetWindowOrigin(const COORD dwWindowOrigin);
DWORD GetFont() const;
void SetFont(const DWORD dwFont);
COORD GetFontSize() const;
void SetFontSize(const COORD dwFontSize);
UINT GetFontFamily() const;
void SetFontFamily(const UINT uFontFamily);
UINT GetFontWeight() const;
void SetFontWeight(const UINT uFontWeight);
const WCHAR* const GetFaceName() const;
bool IsFaceNameSet() const;
void SetFaceName(const std::wstring_view faceName);
UINT GetCursorSize() const;
void SetCursorSize(const UINT uCursorSize);
bool GetFullScreen() const;
void SetFullScreen(const bool fFullScreen);
bool GetQuickEdit() const;
void SetQuickEdit(const bool fQuickEdit);
bool GetInsertMode() const;
void SetInsertMode(const bool fInsertMode);
bool GetAutoPosition() const;
void SetAutoPosition(const bool fAutoPosition);
UINT GetHistoryBufferSize() const;
void SetHistoryBufferSize(const UINT uHistoryBufferSize);
UINT GetNumberOfHistoryBuffers() const;
void SetNumberOfHistoryBuffers(const UINT uNumberOfHistoryBuffers);
bool GetHistoryNoDup() const;
void SetHistoryNoDup(const bool fHistoryNoDup);
// The first 16 items of the color table are the same as the 16-color palette.
inline const std::array<COLORREF, XTERM_COLOR_TABLE_SIZE>& GetColorTable() const noexcept
{
return _colorTable;
}
void SetColorTableEntry(const size_t index, const COLORREF ColorValue);
COLORREF GetColorTableEntry(const size_t index) const;
void SetLegacyColorTableEntry(const size_t index, const COLORREF ColorValue);
COLORREF GetLegacyColorTableEntry(const size_t index) const;
COLORREF GetCursorColor() const noexcept;
CursorType GetCursorType() const noexcept;
void SetCursorColor(const COLORREF CursorColor) noexcept;
void SetCursorType(const CursorType cursorType) noexcept;
bool GetInterceptCopyPaste() const noexcept;
void SetInterceptCopyPaste(const bool interceptCopyPaste) noexcept;
COLORREF GetDefaultForegroundColor() const noexcept;
void SetDefaultForegroundColor(const COLORREF defaultForeground) noexcept;
COLORREF GetDefaultBackgroundColor() const noexcept;
void SetDefaultBackgroundColor(const COLORREF defaultBackground) noexcept;
bool IsTerminalScrolling() const noexcept;
void SetTerminalScrolling(const bool terminalScrollingEnabled) noexcept;
bool GetUseDx() const noexcept;
bool GetCopyColor() const noexcept;
private:
DWORD _dwHotKey;
DWORD _dwStartupFlags;
WORD _wFillAttribute;
WORD _wPopupFillAttribute;
WORD _wShowWindow; // used when window is created
WORD _wReserved;
// START - This section filled via memcpy from shortcut properties. Do not rearrange/change.
COORD _dwScreenBufferSize;
COORD _dwWindowSize; // this is in characters.
COORD _dwWindowOrigin; // used when window is created
DWORD _nFont;
COORD _dwFontSize;
UINT _uFontFamily;
UINT _uFontWeight;
WCHAR _FaceName[LF_FACESIZE];
UINT _uCursorSize;
BOOL _bFullScreen; // deprecated
BOOL _bQuickEdit;
BOOL _bInsertMode; // used by command line editing
BOOL _bAutoPosition;
UINT _uHistoryBufferSize;
UINT _uNumberOfHistoryBuffers;
BOOL _bHistoryNoDup;
// END - memcpy
UINT _uCodePage;
UINT _uScrollScale;
bool _fTrimLeadingZeros;
bool _fEnableColorSelection;
bool _bLineSelection;
bool _bWrapText; // whether to use text wrapping when resizing the window
bool _fCtrlKeyShortcutsDisabled; // disables Ctrl+<something> key intercepts
BYTE _bWindowAlpha; // describes the opacity of the window
bool _fFilterOnPaste; // should we filter text when the user pastes? (e.g. remove <tab>)
std::wstring _LaunchFaceName;
bool _fAllowAltF4Close;
DWORD _dwVirtTermLevel;
bool _fAutoReturnOnNewline;
bool _fRenderGridWorldwide;
bool _fScreenReversed;
bool _fUseDx;
bool _fCopyColor;
std::array<COLORREF, XTERM_COLOR_TABLE_SIZE> _colorTable;
// this is used for the special STARTF_USESIZE mode.
bool _fUseWindowSizePixels;
COORD _dwWindowSizePixels;
// Technically a COLORREF, but using INVALID_COLOR as "Invert Colors"
unsigned int _CursorColor;
CursorType _CursorType;
bool _fInterceptCopyPaste;
COLORREF _DefaultForeground;
COLORREF _DefaultBackground;
bool _TerminalScrolling;
friend class RegistrySerialization;
};