Skip to content

Commit

Permalink
TTY: dont use Xi keys filtering if some terminal extension is avalabl…
Browse files Browse the repository at this point in the history
…e (touch #1591)
  • Loading branch information
elfmz committed May 1, 2023
1 parent 5e83a45 commit 15b8162
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
28 changes: 23 additions & 5 deletions WinPort/src/Backend/TTY/TTYBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ bool TTYBackend::Startup()
return true;
}

void TTYBackend::UpdateBackendIdentification()
{
if (_far2l_tty) {
g_winport_backend = L"TTY|F";

} else if (_ttyx) {
g_winport_backend = _using_extension
? L"TTY|X+" : ( _ttyx->HasXi() ? L"TTY|Xi" : L"TTY|X" );

} else {
g_winport_backend = _using_extension ? L"TTY|+" : L"TTY";
}
}

void TTYBackend::ReaderThread()
{
bool prev_far2l_tty = false;
Expand All @@ -177,7 +191,6 @@ void TTYBackend::ReaderThread()
_fkeys_support = _far2l_tty ? FKS_UNKNOWN : FKS_NOT_SUPPORTED;

if (_far2l_tty) {
g_winport_backend = L"TTY|F";
if (!prev_far2l_tty) {
IFar2lInterractor *interractor = this;
_clipboard_backend_setter.Set<TTYFar2lClipboardBackend>(interractor);
Expand All @@ -188,14 +201,13 @@ void TTYBackend::ReaderThread()
_ttyx = StartTTYX(_full_exe_path, !strstr(_nodetect, "xi"));
}
if (_ttyx) {
g_winport_backend = _ttyx->HasXi() ? L"TTY|Xi" : L"TTY|X";
_clipboard_backend_setter.Set<TTYXClipboard>(_ttyx);

} else {
g_winport_backend = L"TTY";
ChooseSimpleClipboardBackend();
}
}
UpdateBackendIdentification();
prev_far2l_tty = _far2l_tty;

{
Expand Down Expand Up @@ -927,14 +939,20 @@ static void OnFar2lMouse(bool compact, StackSerializer &stk_ser)
}
}

void TTYBackend::OnInspectKeyEvent(KEY_EVENT_RECORD &event)
void TTYBackend::OnInspectKeyEvent(KEY_EVENT_RECORD &event, char using_extension)
{
if (_ttyx) {
if (using_extension != _using_extension) {
_using_extension = using_extension;
UpdateBackendIdentification();
}

if (_ttyx && !using_extension) {
_ttyx->InspectKeyEvent(event);

} else {
event.dwControlKeyState|= QueryControlKeys();
}

if (!event.wVirtualKeyCode) {
if (event.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED | LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
event.wVirtualKeyCode = WChar2WinVKeyCode(event.uChar.UnicodeChar);
Expand Down
5 changes: 3 additions & 2 deletions WinPort/src/Backend/TTY/TTYBackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ class TTYBackend : IConsoleOutputBackend, ITTYInputSpecialSequenceHandler, IFar2
void ReaderThread();
void ReaderLoop();
void WriterThread();

void UpdateBackendIdentification();

std::condition_variable _async_cond;
std::mutex _async_mutex;
ITTYXGluePtr _ttyx;
char _using_extension = 0;

COORD _largest_window_size{};
std::atomic<bool> _largest_window_size_ready{false};
Expand Down Expand Up @@ -133,7 +134,7 @@ class TTYBackend : IConsoleOutputBackend, ITTYInputSpecialSequenceHandler, IFar2
virtual void OnConsoleOverrideColor(DWORD Index, DWORD *ColorFG, DWORD *ColorBK);

// ITTYInputSpecialSequenceHandler
virtual void OnInspectKeyEvent(KEY_EVENT_RECORD &event);
virtual void OnInspectKeyEvent(KEY_EVENT_RECORD &event, char using_extension);
virtual void OnFar2lEvent(StackSerializer &stk_ser);
virtual void OnFar2lReply(StackSerializer &stk_ser);
virtual void OnInputBroken();
Expand Down
4 changes: 2 additions & 2 deletions WinPort/src/Backend/TTY/TTYInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void TTYInput::PostCharEvent(wchar_t ch)
ir.Event.KeyEvent.uChar.UnicodeChar = ch;

if (_handler) {
_handler->OnInspectKeyEvent(ir.Event.KeyEvent);
_handler->OnInspectKeyEvent(ir.Event.KeyEvent, _parser.UsingExtension());
}

g_winport_con_in->Enqueue(&ir, 1);
Expand Down Expand Up @@ -65,7 +65,7 @@ void TTYInput::OnBufUpdated(bool idle)
ir.Event.KeyEvent.wRepeatCount = 1;
ir.Event.KeyEvent.uChar.UnicodeChar = 0x1b;
ir.Event.KeyEvent.bKeyDown = TRUE;
_handler->OnInspectKeyEvent(ir.Event.KeyEvent);
_handler->OnInspectKeyEvent(ir.Event.KeyEvent, _parser.UsingExtension());
if (ir.Event.KeyEvent.wVirtualKeyCode != 0 && ir.Event.KeyEvent.wVirtualKeyCode != VK_UNASSIGNED) {
g_winport_con_in->Enqueue(&ir, 1);
ir.Event.KeyEvent.bKeyDown = FALSE;
Expand Down
4 changes: 2 additions & 2 deletions WinPort/src/Backend/TTY/TTYInputSequenceParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ size_t TTYInputSequenceParser::ParseNChars2Key(const char *s, size_t l)
ir.Event.KeyEvent.dwControlKeyState|= LEFT_ALT_PRESSED;
ir.Event.KeyEvent.bKeyDown = TRUE;
if (_handler) {
_handler->OnInspectKeyEvent(ir.Event.KeyEvent);
_handler->OnInspectKeyEvent(ir.Event.KeyEvent, _using_extension);
}
_ir_pending.emplace_back(ir); // g_winport_con_in->Enqueue(&ir, 1);
ir.Event.KeyEvent.bKeyDown = FALSE;
Expand Down Expand Up @@ -501,7 +501,7 @@ void TTYInputSequenceParser::AddPendingKeyEvent(const TTYInputKey &k)
ir.Event.KeyEvent.dwControlKeyState = k.control_keys | _extra_control_keys;
ir.Event.KeyEvent.wVirtualScanCode = WINPORT(MapVirtualKey)(k.vk,MAPVK_VK_TO_VSC);
if (_handler) {
_handler->OnInspectKeyEvent(ir.Event.KeyEvent);
_handler->OnInspectKeyEvent(ir.Event.KeyEvent, _using_extension);
}
_ir_pending.emplace_back(ir); // g_winport_con_in->Enqueue(&ir, 1);
ir.Event.KeyEvent.bKeyDown = FALSE;
Expand Down
4 changes: 3 additions & 1 deletion WinPort/src/Backend/TTY/TTYInputSequenceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ template <size_t N> using NChars2Key = NCharsMap<N, TTYInputKey>;

struct ITTYInputSpecialSequenceHandler
{
virtual void OnInspectKeyEvent(KEY_EVENT_RECORD &event) = 0;
virtual void OnInspectKeyEvent(KEY_EVENT_RECORD &event, char using_extension) = 0;
virtual void OnFar2lEvent(StackSerializer &stk_ser) = 0;
virtual void OnFar2lReply(StackSerializer &stk_ser) = 0;
virtual void OnInputBroken() = 0;
Expand Down Expand Up @@ -102,6 +102,7 @@ class TTYInputSequenceParser
std::vector<INPUT_RECORD> _ir_pending;
bool _kitty_right_ctrl_down = false;
int _iterm_last_flags = 0;
char _using_extension = 0;

void AssertNoConflicts();

Expand Down Expand Up @@ -131,4 +132,5 @@ class TTYInputSequenceParser
TTYInputSequenceParser(ITTYInputSpecialSequenceHandler *handler);

size_t Parse(const char *s, size_t l, bool idle_expired); // 0 - need more, -1 - not sequence, -2 - unrecognized sequence, >0 - sequence
char UsingExtension() const { return _using_extension; };
};
13 changes: 13 additions & 0 deletions WinPort/src/Backend/TTY/TTYInputSequenceParserExts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ size_t TTYInputSequenceParser::TryParseAsKittyEscapeSequence(const char *s, size

_ir_pending.emplace_back(ir);

if (!_using_extension) {
fprintf(stderr, "TTYInputSequenceParser: using Kitty extension\n");
_using_extension = 'k';
}

return i+1;
}

Expand Down Expand Up @@ -330,6 +335,10 @@ size_t TTYInputSequenceParser::TryParseAsWinTermEscapeSequence(const char *s, si
ir.Event.KeyEvent.wRepeatCount = args[5];

_ir_pending.emplace_back(ir);
if (!_using_extension) {
fprintf(stderr, "TTYInputSequenceParser: using WinTerm extension\n");
_using_extension = 'w';
}
return n;
}

Expand Down Expand Up @@ -643,6 +652,10 @@ size_t TTYInputSequenceParser::TryParseAsITerm2EscapeSequence(const char *s, siz
if (keycode == 0x3C) ir.Event.KeyEvent.wVirtualScanCode = RIGHT_SHIFT_VSC; // RightShift
_ir_pending.emplace_back(ir);

if (!_using_extension) {
fprintf(stderr, "TTYInputSequenceParser: using Apple ITerm2 extension\n");
_using_extension = 'a';
}
_iterm_last_flags = flags;
return len;
}

0 comments on commit 15b8162

Please sign in to comment.