Fix stdin references.

This commit is contained in:
ArthurSonzogni
2025-08-17 20:21:20 +02:00
parent d59b77e6a0
commit 2b876a8712

View File

@@ -539,6 +539,8 @@ void ScreenInteractive::Install() {
// https://github.com/ArthurSonzogni/FTXUI/issues/846 // https://github.com/ArthurSonzogni/FTXUI/issues/846
Flush(); Flush();
InstallPipedInputHandling();
// After uninstalling the new configuration, flush it to the terminal to // After uninstalling the new configuration, flush it to the terminal to
// ensure it is fully applied: // ensure it is fully applied:
on_exit_functions.emplace([] { Flush(); }); on_exit_functions.emplace([] { Flush(); });
@@ -604,9 +606,10 @@ void ScreenInteractive::Install() {
} }
struct termios terminal; // NOLINT struct termios terminal; // NOLINT
tcgetattr(STDIN_FILENO, &terminal); tcgetattr(tty_fd_, &terminal);
on_exit_functions.emplace( on_exit_functions.emplace([terminal = terminal, tty_fd_ = tty_fd_] {
[=] { tcsetattr(STDIN_FILENO, TCSANOW, &terminal); }); tcsetattr(tty_fd_, TCSANOW, &terminal);
});
// Enabling raw terminal input mode // Enabling raw terminal input mode
terminal.c_iflag &= ~IGNBRK; // Disable ignoring break condition terminal.c_iflag &= ~IGNBRK; // Disable ignoring break condition
@@ -634,7 +637,7 @@ void ScreenInteractive::Install() {
// read. // read.
terminal.c_cc[VTIME] = 0; // Timeout in deciseconds for non-canonical read. terminal.c_cc[VTIME] = 0; // Timeout in deciseconds for non-canonical read.
tcsetattr(STDIN_FILENO, TCSANOW, &terminal); tcsetattr(tty_fd_, TCSANOW, &terminal);
#endif #endif
@@ -670,21 +673,13 @@ void ScreenInteractive::Install() {
// ensure it is fully applied: // ensure it is fully applied:
Flush(); Flush();
// Redirect the true terminal to stdin, so that we can read keyboard input
// directly from stdin, even if the input is piped from a file or another
// process.
//
// TODO: Instead of redirecting stdin, we could define the file descriptor to
// read from, and use it in the TerminalInputParser.
InstallPipedInputHandling();
quit_ = false; quit_ = false;
PostAnimationTask(); PostAnimationTask();
} }
void ScreenInteractive::InstallPipedInputHandling() { void ScreenInteractive::InstallPipedInputHandling() {
tty_fd_ = STDIN_FILENO; // Default to stdin. tty_fd_ = fileno(stdin); // NOLINT
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__) #if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
// Handle piped input redirection if explicitly enabled by the application. // Handle piped input redirection if explicitly enabled by the application.
// This allows applications to read data from stdin while still receiving // This allows applications to read data from stdin while still receiving
@@ -694,7 +689,7 @@ void ScreenInteractive::InstallPipedInputHandling() {
} }
// If stdin is a terminal, we don't need to open /dev/tty. // If stdin is a terminal, we don't need to open /dev/tty.
if (isatty(STDIN_FILENO)) { if (isatty(fileno(stdin))) {
return; return;
} }
@@ -702,7 +697,7 @@ void ScreenInteractive::InstallPipedInputHandling() {
tty_fd_ = open("/dev/tty", O_RDONLY); tty_fd_ = open("/dev/tty", O_RDONLY);
if (tty_fd_ < 0) { if (tty_fd_ < 0) {
// Failed to open /dev/tty (containers, headless systems, etc.) // Failed to open /dev/tty (containers, headless systems, etc.)
tty_fd_ = STDIN_FILENO; // Fallback to stdin. tty_fd_ = fileno(stdin); // Fallback to stdin.
return; return;
} }