terminal/src/host/server.h
James Holderness d1671a0acd
Add support for the "blink" graphic rendition attribute (#7490)
This PR adds support for the _blink_ graphic rendition attribute. When a
character is output with this attribute set, it "blinks" at a regular
interval, by cycling its color between the normal rendition and a dimmer
shade of that color.

The majority of the blinking mechanism is encapsulated in a new
`BlinkingState` class, which is shared between the Terminal and Conhost
implementations. This class keeps track of the position in the blinking
cycle, which determines whether characters are rendered as normal or
faint. 

In Windows Terminal, the state is stored in the `Terminal` class, and in
Conhost it's stored in the `CONSOLE_INFORMATION` class. In both cases,
the `IsBlinkingFaint` method is used to determine the current blinking
rendition, and that is passed on as a parameter to the
`TextAttribute::CalculateRgbColors` method when these classes are
looking up attribute colors.

Prior to calculating the colors, the current attribute is also passed to
the `RecordBlinkingUsage` method, which keeps track of whether there are
actually any blink attributes in use. This is used to determine whether
the screen needs to be refreshed when the blinking cycle toggles between
the normal and faint renditions.

The refresh itself is handled by the `ToggleBlinkingRendition` method,
which is triggered by a timer. In Conhost this is just piggybacking on
the existing cursor blink timer, but in Windows Terminal it needs to
have its own separate timer, since the cursor timer is reset whenever a
key is pressed, which is not something we want for attribute blinking.

Although the `ToggleBlinkingRendition` is called at the same rate as the
cursor blinking, we actually only want the cells to blink at half that
frequency. We thus have a counter that cycles through four phases, and
blinking is rendered as faint for two of those four. Then every two
cycles - when the state changes - a redraw is triggered, but only if
there are actually blinking attributes in use (as previously recorded).

As mentioned earlier, the blinking frequency is based on the cursor
blink rate, so that means it'll automatically be disabled if a user has
set their cursor blink rate to none. It can also be disabled by turning
off the _Show animations in Windows_ option. In Conhost these settings
take effect immediately, but in Windows Terminal they only apply when a
new tab is opened.

This PR also adds partial support for the `SGR 6` _rapid blink_
attribute. This is not used by DEC terminals, but was defined in the
ECMA/ANSI standards. It's not widely supported, but many terminals just
it implement it as an alias for the regular `SGR 5` blink attribute, so
that's what I've done here too.

## Validation Steps Performed

I've checked the _Graphic rendition test pattern_ in Vttest, and
compared our representation of the blink attribute to that of an actual
DEC VT220 terminal as seen on [YouTube]. With the right color scheme
it's a reasonably close match.

[YouTube]: https://www.youtube.com/watch?v=03Pz5AmxbE4&t=1m55s

Closes #7388
2020-09-21 23:21:33 +00:00

175 lines
6 KiB
C++

/*++
Copyright (c) Microsoft Corporation
Licensed under the MIT license.
Module Name:
- server.h
Abstract:
- This module contains the internal structures and definitions used by the console server.
Author:
- Therese Stowell (ThereseS) 12-Nov-1990
Revision History:
--*/
#pragma once
#include "IIoProvider.hpp"
#include "settings.hpp"
#include "conimeinfo.h"
#include "VtIo.hpp"
#include "CursorBlinker.hpp"
#include "..\server\ProcessList.h"
#include "..\server\WaitQueue.h"
#include "..\host\RenderData.hpp"
#include "..\renderer\inc\BlinkingState.hpp"
// clang-format off
// Flags flags
#define CONSOLE_IS_ICONIC 0x00000001
#define CONSOLE_OUTPUT_SUSPENDED 0x00000002
#define CONSOLE_HAS_FOCUS 0x00000004
#define CONSOLE_IGNORE_NEXT_MOUSE_INPUT 0x00000008
#define CONSOLE_SELECTING 0x00000010
#define CONSOLE_SCROLLING 0x00000020
// unused (CONSOLE_DISABLE_CLOSE) 0x00000040
// unused (CONSOLE_USE_POLY_TEXT) 0x00000080
// Removed Oct 2017 - added a headless mode, which revealed that the consumption
// of this flag was redundant.
// unused (CONSOLE_NO_WINDOW) 0x00000100
// unused (CONSOLE_VDM_REGISTERED) 0x00000200
#define CONSOLE_UPDATING_SCROLL_BARS 0x00000400
#define CONSOLE_QUICK_EDIT_MODE 0x00000800
#define CONSOLE_CONNECTED_TO_EMULATOR 0x00002000
// unused (CONSOLE_FULLSCREEN_NOPAINT) 0x00004000
#define CONSOLE_QUIT_POSTED 0x00008000
#define CONSOLE_AUTO_POSITION 0x00010000
#define CONSOLE_IGNORE_NEXT_KEYUP 0x00020000
// unused (CONSOLE_WOW_REGISTERED) 0x00040000
#define CONSOLE_HISTORY_NODUP 0x00100000
#define CONSOLE_SCROLLBAR_TRACKING 0x00200000
#define CONSOLE_SETTING_WINDOW_SIZE 0x00800000
// unused (CONSOLE_VDM_HIDDEN_WINDOW) 0x01000000
// unused (CONSOLE_OS2_REGISTERED) 0x02000000
// unused (CONSOLE_OS2_OEM_FORMAT) 0x04000000
// unused (CONSOLE_JUST_VDM_UNREGISTERED) 0x08000000
// unused (CONSOLE_FULLSCREEN_INITIALIZED) 0x10000000
#define CONSOLE_USE_PRIVATE_FLAGS 0x20000000
// unused (CONSOLE_TSF_ACTIVATED) 0x40000000
#define CONSOLE_INITIALIZED 0x80000000
#define CONSOLE_SUSPENDED (CONSOLE_OUTPUT_SUSPENDED)
// clang-format on
class COOKED_READ_DATA;
class CommandHistory;
class CONSOLE_INFORMATION :
public Settings,
public Microsoft::Console::IIoProvider
{
public:
CONSOLE_INFORMATION();
~CONSOLE_INFORMATION();
CONSOLE_INFORMATION(const CONSOLE_INFORMATION& c) = delete;
CONSOLE_INFORMATION& operator=(const CONSOLE_INFORMATION& c) = delete;
ConsoleProcessList ProcessHandleList;
InputBuffer* pInputBuffer;
SCREEN_INFORMATION* ScreenBuffers; // singly linked list
ConsoleWaitQueue OutputQueue;
DWORD Flags;
std::atomic<WORD> PopupCount;
// the following fields are used for ansi-unicode translation
UINT CP;
UINT OutputCP;
ULONG CtrlFlags; // indicates outstanding ctrl requests
ULONG LimitingProcessId;
CPINFO CPInfo;
CPINFO OutputCPInfo;
ConsoleImeInfo ConsoleIme;
void LockConsole();
bool TryLockConsole();
void UnlockConsole();
bool IsConsoleLocked() const;
ULONG GetCSRecursionCount();
Microsoft::Console::VirtualTerminal::VtIo* GetVtIo();
SCREEN_INFORMATION& GetActiveOutputBuffer() override;
const SCREEN_INFORMATION& GetActiveOutputBuffer() const override;
bool HasActiveOutputBuffer() const;
InputBuffer* const GetActiveInputBuffer() const;
bool IsInVtIoMode() const;
bool HasPendingCookedRead() const noexcept;
const COOKED_READ_DATA& CookedReadData() const noexcept;
COOKED_READ_DATA& CookedReadData() noexcept;
void SetCookedReadData(COOKED_READ_DATA* readData) noexcept;
COLORREF GetDefaultForeground() const noexcept;
COLORREF GetDefaultBackground() const noexcept;
std::pair<COLORREF, COLORREF> LookupAttributeColors(const TextAttribute& attr) const noexcept;
void SetTitle(const std::wstring_view newTitle);
void SetTitlePrefix(const std::wstring& newTitlePrefix);
void SetOriginalTitle(const std::wstring& originalTitle);
void SetLinkTitle(const std::wstring& linkTitle);
const std::wstring& GetTitle() const noexcept;
const std::wstring& GetOriginalTitle() const noexcept;
const std::wstring& GetLinkTitle() const noexcept;
const std::wstring GetTitleAndPrefix() const;
[[nodiscard]] static NTSTATUS AllocateConsole(const std::wstring_view title);
// MSFT:16886775 : get rid of friends
friend void SetActiveScreenBuffer(_Inout_ SCREEN_INFORMATION& screenInfo);
friend class SCREEN_INFORMATION;
friend class CommonState;
Microsoft::Console::CursorBlinker& GetCursorBlinker() noexcept;
Microsoft::Console::Render::BlinkingState& GetBlinkingState() const noexcept;
CHAR_INFO AsCharInfo(const OutputCellView& cell) const noexcept;
RenderData renderData;
private:
CRITICAL_SECTION _csConsoleLock; // serialize input and output using this
std::wstring _Title;
std::wstring _TitlePrefix; // Eg Select, Mark - things that we manually prepend to the title.
std::wstring _OriginalTitle;
std::wstring _LinkTitle; // Path to .lnk file
SCREEN_INFORMATION* pCurrentScreenBuffer;
COOKED_READ_DATA* _cookedReadData; // non-ownership pointer
Microsoft::Console::VirtualTerminal::VtIo _vtIo;
Microsoft::Console::CursorBlinker _blinker;
mutable Microsoft::Console::Render::BlinkingState _blinkingState;
};
#define ConsoleLocked() (ServiceLocator::LocateGlobals()->getConsoleInformation()->ConsoleLock.OwningThread == NtCurrentTeb()->ClientId.UniqueThread)
#define CONSOLE_STATUS_WAIT 0xC0030001
#define CONSOLE_STATUS_READ_COMPLETE 0xC0030002
#define CONSOLE_STATUS_WAIT_NO_BLOCK 0xC0030003
#include "..\server\ObjectHandle.h"
void SetActiveScreenBuffer(SCREEN_INFORMATION& screenInfo);