From 68e11ca94c0c1f1e37f0cc1b4c15490a00100c73 Mon Sep 17 00:00:00 2001 From: Yasumasa Suenaga Date: Tue, 9 Jul 2024 22:02:57 +0900 Subject: [PATCH] Avoid resizing console during a lot of VT commands are issued ReadConsoleInput() API might report resize event suddenly while a lot of VT commands are issued. So resize request to TTY Resizer should be issued after all of events are processed at least. --- SimpleCom/SerialConnection.cpp | 18 +++++++++++++++--- tty-resizer/README.md | 2 ++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/SimpleCom/SerialConnection.cpp b/SimpleCom/SerialConnection.cpp index 4aedd70..b8bf537 100644 --- a/SimpleCom/SerialConnection.cpp +++ b/SimpleCom/SerialConnection.cpp @@ -212,6 +212,9 @@ bool SimpleCom::SerialConnection::StdInRedirector(const HANDLE hSerial, const HA try { HANDLE waiters[] = { _hStdIn, hTermEvent }; + COORD newConsoleSize; + bool isConsoleSizeUpdated = false; + while (true) { DWORD result = WaitForMultipleObjects(sizeof(waiters) / sizeof(HANDLE), waiters, FALSE, INFINITE); if (result == WAIT_OBJECT_0) { // hStdIn @@ -241,9 +244,18 @@ bool SimpleCom::SerialConnection::StdInRedirector(const HANDLE hSerial, const HA ProcessKeyEvents(inputs[idx].Event.KeyEvent, writer); } else if ((inputs[idx].EventType == WINDOW_BUFFER_SIZE_EVENT) && _useTTYResizer) { - char buf[RINGBUF_SZ]; - int len = snprintf(buf, sizeof(buf), "%c%d" RESIZER_SEPARATOR "%d%c", RESIZER_START_MARKER, inputs[idx].Event.WindowBufferSizeEvent.dwSize.Y, inputs[idx].Event.WindowBufferSizeEvent.dwSize.X, RESIZER_END_MARKER); - writer.PutData(buf, len); + CopyMemory(&newConsoleSize, &inputs[idx].Event.WindowBufferSizeEvent, sizeof(COORD)); + isConsoleSizeUpdated = true; + } + + if (isConsoleSizeUpdated) { + DWORD numOfEvents; + if (GetNumberOfConsoleInputEvents(_hStdIn, &numOfEvents) && (numOfEvents == 0)) { + char buf[RINGBUF_SZ]; + int len = snprintf(buf, sizeof(buf), "%c%d" RESIZER_SEPARATOR "%d%c", RESIZER_START_MARKER, newConsoleSize.Y, newConsoleSize.X, RESIZER_END_MARKER); + writer.PutData(buf, len); + isConsoleSizeUpdated = false; + } } } diff --git a/tty-resizer/README.md b/tty-resizer/README.md index d81f70a..f276209 100644 --- a/tty-resizer/README.md +++ b/tty-resizer/README.md @@ -105,3 +105,5 @@ Some strings for resizing might be shown on your serial console in earlier phase You should wait few seconds if you start tty-resizer from `tty-resizer.service` when you encounter this problem. This might be caused that `ioctl` to TTY is failed. So `tty-resizer.service` in this source would restart when the issue happens to avoid it. Then it will work fine. + +And also TTY Resizer wouldn't work file with full-screen application like Vim. Noisy chars (for Vim) might be input, and TTY Resizer cannot set resized console size.