Skip to content

Commit 0f5d883

Browse files
authored
Make disabling the Kitty Keyboard Protocol possible (#19995)
Avoid translating W32IM sequences to KKP. Closes #19977 ## Validation Steps Performed * Use French Bepo keyboard layout * Disable KKP * Use fish shell * `AltGr+Y` produces `{` ✅
1 parent ad7b34e commit 0f5d883

8 files changed

Lines changed: 43 additions & 1 deletion

File tree

src/cascadia/TerminalCore/Terminal.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class Microsoft::Terminal::Core::Terminal final :
132132
#pragma region ITerminalApi
133133
// These methods are defined in TerminalApi.cpp
134134
void ReturnResponse(const std::wstring_view response) override;
135+
bool IsConPTY() const noexcept override;
135136
Microsoft::Console::VirtualTerminal::StateMachine& GetStateMachine() noexcept override;
136137
BufferState GetBufferAndViewport() noexcept override;
137138
void SetViewportPosition(const til::point position) noexcept override;

src/cascadia/TerminalCore/TerminalApi.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ void Terminal::ReturnResponse(const std::wstring_view response)
2828
}
2929
}
3030

31+
bool Terminal::IsConPTY() const noexcept
32+
{
33+
return false;
34+
}
35+
3136
Microsoft::Console::VirtualTerminal::StateMachine& Terminal::GetStateMachine() noexcept
3237
{
3338
return *_stateMachine;

src/host/outputStream.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ void ConhostInternalGetSet::ReturnResponse(const std::wstring_view response)
4848
_io.GetActiveInputBuffer()->WriteString(response);
4949
}
5050

51+
bool ConhostInternalGetSet::IsConPTY() const noexcept
52+
{
53+
const auto& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
54+
return gci.IsInVtIoMode();
55+
}
56+
5157
// Routine Description:
5258
// - Retrieves the state machine for the active output buffer.
5359
// Arguments:

src/host/outputStream.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ConhostInternalGetSet final : public Microsoft::Console::VirtualTerminal::
3131

3232
void ReturnResponse(const std::wstring_view response) override;
3333

34+
bool IsConPTY() const noexcept override;
3435
Microsoft::Console::VirtualTerminal::StateMachine& GetStateMachine() override;
3536
BufferState GetBufferAndViewport() override;
3637
void SetViewportPosition(const til::point position) override;

src/terminal/adapter/ITerminalApi.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace Microsoft::Console::VirtualTerminal
4646
bool isMainBuffer;
4747
};
4848

49+
virtual bool IsConPTY() const noexcept = 0;
4950
virtual StateMachine& GetStateMachine() = 0;
5051
virtual BufferState GetBufferAndViewport() = 0;
5152
virtual void SetViewportPosition(const til::point position) = 0;

src/terminal/adapter/adaptDispatch.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,6 +2073,13 @@ void AdaptDispatch::SetAnsiMode(const bool ansiMode)
20732073
// CSI = flags ; mode u - Sets kitty keyboard protocol flags
20742074
void AdaptDispatch::SetKittyKeyboardProtocol(const VTParameter flags, const VTParameter mode) noexcept
20752075
{
2076+
// Avoid setting KKP flags in `_terminalInput` when we're ConPTY. Otherwise, we'd be translating
2077+
// W32IM to KKP, even when KKP is not supported by the hosting terminal (possibly intentionally).
2078+
if (_api.IsConPTY())
2079+
{
2080+
return;
2081+
}
2082+
20762083
const auto kittyFlags = gsl::narrow_cast<uint8_t>(flags.value_or(0));
20772084
const auto KittyKeyboardProtocol = static_cast<TerminalInput::KittyKeyboardProtocolMode>(mode.value_or(1));
20782085
_terminalInput.SetKittyKeyboardProtocol(kittyFlags, KittyKeyboardProtocol);
@@ -2081,20 +2088,35 @@ void AdaptDispatch::SetKittyKeyboardProtocol(const VTParameter flags, const VTPa
20812088
// CSI ? u - Queries current kitty keyboard protocol flags
20822089
void AdaptDispatch::QueryKittyKeyboardProtocol()
20832090
{
2091+
if (_api.IsConPTY())
2092+
{
2093+
return;
2094+
}
2095+
20842096
const auto flags = static_cast<VTInt>(_terminalInput.GetKittyFlags());
20852097
_ReturnCsiResponse(fmt::format(FMT_COMPILE(L"?{}u"), flags));
20862098
}
20872099

20882100
// CSI > flags u - Pushes current kitty keyboard flags onto the stack and sets new flags
20892101
void AdaptDispatch::PushKittyKeyboardProtocol(const VTParameter flags)
20902102
{
2103+
if (_api.IsConPTY())
2104+
{
2105+
return;
2106+
}
2107+
20912108
const auto kittyFlags = gsl::narrow_cast<uint8_t>(flags.value_or(0));
20922109
_terminalInput.PushKittyFlags(kittyFlags);
20932110
}
20942111

20952112
// CSI < count u - Pops one or more entries from the kitty keyboard stack
20962113
void AdaptDispatch::PopKittyKeyboardProtocol(const VTParameter count)
20972114
{
2115+
if (_api.IsConPTY())
2116+
{
2117+
return;
2118+
}
2119+
20982120
const auto popCount = static_cast<size_t>(count.value_or(1));
20992121
_terminalInput.PopKittyFlags(popCount);
21002122
}

src/terminal/adapter/ut_adapter/adapterTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class TestGetSet final : public ITerminalApi
7676
}
7777
}
7878

79+
bool IsConPTY() const noexcept override
80+
{
81+
return false;
82+
}
83+
7984
StateMachine& GetStateMachine() override
8085
{
8186
return *_stateMachine;

src/terminal/input/terminalInput.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,8 @@ TerminalInput::OutputType TerminalInput::HandleKey(const INPUT_RECORD& event)
228228
// GH#4999 - If we're in win32-input mode, skip straight to doing that.
229229
// Since this mode handles all types of key events, do nothing else.
230230
//
231-
// The kitty keyboard protocol takes precedence, because it's cross-platform.
231+
// ConPTY assumes that W32IM always remains enabled. We have to prefer
232+
// the kitty keyboard protocol, because otherwise it would never be used.
232233
if (_inputMode.test(Mode::Win32) && !_forceDisableWin32InputMode && !_kittyFlags)
233234
{
234235
return _makeWin32Output(event.Event.KeyEvent);

0 commit comments

Comments
 (0)