Skip to content

Commit

Permalink
Add suport to conpty to parse these sequences into KeyEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed May 13, 2020
1 parent eddcbe4 commit 583a3d2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
40 changes: 38 additions & 2 deletions src/terminal/parser/InputStateMachineEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ bool InputStateMachineEngine::ActionCsiDispatch(const wchar_t wch,
const std::basic_string_view<wchar_t> intermediates,
const std::basic_string_view<size_t> parameters)
{
if (_pDispatch->IsVtInputEnabled() && _pfnFlushToInputQueue)
const auto actionCode = static_cast<CsiActionCodes>(wch);
if (_pDispatch->IsVtInputEnabled() && _pfnFlushToInputQueue && actionCode != CsiActionCodes::Win32KeyboardInput)
{
return _pfnFlushToInputQueue();
}
Expand All @@ -357,6 +358,7 @@ bool InputStateMachineEngine::ActionCsiDispatch(const wchar_t wch,
unsigned int function = 0;
size_t col = 0;
size_t row = 0;
KeyEvent key;

// This is all the args after the first arg, and the count of args not including the first one.
const auto remainingArgs = parameters.size() > 1 ? parameters.substr(1) : std::basic_string_view<size_t>{};
Expand Down Expand Up @@ -385,7 +387,7 @@ bool InputStateMachineEngine::ActionCsiDispatch(const wchar_t wch,
}
return success;
}
switch (static_cast<CsiActionCodes>(wch))
switch (actionCode)
{
case CsiActionCodes::Generic:
modifierState = _GetGenericKeysModifierState(parameters);
Expand Down Expand Up @@ -422,6 +424,9 @@ bool InputStateMachineEngine::ActionCsiDispatch(const wchar_t wch,
case CsiActionCodes::DTTERM_WindowManipulation:
success = _GetWindowManipulationType(parameters, function);
break;
case CsiActionCodes::Win32KeyboardInput:
success = _GenerateWin32Key(parameters, key);
break;
default:
success = false;
break;
Expand Down Expand Up @@ -462,6 +467,14 @@ bool InputStateMachineEngine::ActionCsiDispatch(const wchar_t wch,
success = _pDispatch->WindowManipulation(static_cast<DispatchTypes::WindowManipulationType>(function),
remainingArgs);
break;
case CsiActionCodes::Win32KeyboardInput:
{
// auto event = std::make_unique<KeyEvent>(key);
auto dumb = key.ToInputRecord();
std::deque<std::unique_ptr<IInputEvent>> input = IInputEvent::Create(gsl::make_span(&dumb, 1));
success = _pDispatch->WriteInput(input);
break;
}
default:
success = false;
break;
Expand Down Expand Up @@ -1261,3 +1274,26 @@ bool InputStateMachineEngine::_GetSGRXYPosition(const std::basic_string_view<siz

return true;
}

bool InputStateMachineEngine::_GenerateWin32Key(const std::basic_string_view<size_t> parameters,
KeyEvent& key)
{
// return fmt::format(L"\x1b[{};{};{};{};{};{}!",
// key.IsKeyDown() ? 1 : 0,
// key.GetRepeatCount(),
// key.GetVirtualKeyCode(),
// key.GetVirtualScanCode(),
// static_cast<int>(key.GetCharData()),
// key.GetActiveModifierKeys());
if (parameters.size() != 6)
{
return false;
}
key = KeyEvent(static_cast<bool>(parameters.at(0)),
::base::saturated_cast<WORD>(parameters.at(1)),
::base::saturated_cast<WORD>(parameters.at(2)),
::base::saturated_cast<WORD>(parameters.at(3)),
static_cast<wchar_t>(parameters.at(4)),
::base::saturated_cast<DWORD>(parameters.at(5)));
return true;
}
3 changes: 3 additions & 0 deletions src/terminal/parser/InputStateMachineEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ namespace Microsoft::Console::VirtualTerminal
CSI_F4 = L'S',
DTTERM_WindowManipulation = L't',
CursorBackTab = L'Z',
Win32KeyboardInput = L'!'
};

enum CsiMouseButtonCodes : unsigned short
Expand Down Expand Up @@ -209,6 +210,8 @@ namespace Microsoft::Console::VirtualTerminal
bool _GetWindowManipulationType(const std::basic_string_view<size_t> parameters,
unsigned int& function) const noexcept;

bool _GenerateWin32Key(const std::basic_string_view<size_t> parameters, KeyEvent& key);

static constexpr size_t DefaultLine = 1;
static constexpr size_t DefaultColumn = 1;
bool _GetXYPosition(const std::basic_string_view<size_t> parameters,
Expand Down
16 changes: 0 additions & 16 deletions src/terminal/parser/OutputStateMachineEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1758,21 +1758,6 @@ bool OutputStateMachineEngine::_GetWindowsOperation(const std::wstring_view str,
size_t& opcode,
std::wstring_view& remainingParams) const
{
// auto found = str.find(L';');
// if (found == std::wstring::npos || found == 0)
// {
// return false;
// }
// std::wstring_view opcodeString = str.substr(0, found);
// size_t opcodeResult = 0;
// const auto [p, ec] = std::from_chars(opcodeString.begin(), opcodeString.end(), opcodeResult);
// if (ec == std::errc())
// {
// return false;
// }
// opcode = opcodeResult;
// remainingParams = str.substr(found);

// First try to get the table index, a number between [0,256]
size_t opcodeResult = 0;
size_t current = 0;
Expand Down Expand Up @@ -1813,7 +1798,6 @@ bool OutputStateMachineEngine::_GetWindowsOperation(const std::wstring_view str,
bool OutputStateMachineEngine::_DispatchWindowsCommand(const size_t opcode,
const std::wstring_view remainingParams) const
{
// DebugBreak();
switch (opcode)
{
case 1:
Expand Down

0 comments on commit 583a3d2

Please sign in to comment.