Skip to content

Commit

Permalink
Avoid resizing console during a lot of VT commands are issued
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
YaSuenag committed Jul 9, 2024
1 parent c237820 commit 68e11ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
18 changes: 15 additions & 3 deletions SimpleCom/SerialConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions tty-resizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit 68e11ca

Please sign in to comment.