|
|
|
|
@@ -112,13 +112,13 @@ void ftxui_on_resize(int columns, int rows) {
|
|
|
|
|
|
|
|
|
|
#else // POSIX (Linux & Mac)
|
|
|
|
|
|
|
|
|
|
int CheckStdinReady(int fd) {
|
|
|
|
|
int CheckStdinReady() {
|
|
|
|
|
timeval tv = {0, 0}; // NOLINT
|
|
|
|
|
fd_set fds;
|
|
|
|
|
FD_ZERO(&fds); // NOLINT
|
|
|
|
|
FD_SET(fd, &fds); // NOLINT
|
|
|
|
|
select(fd + 1, &fds, nullptr, nullptr, &tv); // NOLINT
|
|
|
|
|
return FD_ISSET(fd, &fds); // NOLINT
|
|
|
|
|
FD_SET(STDIN_FILENO, &fds); // NOLINT
|
|
|
|
|
select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv); // NOLINT
|
|
|
|
|
return FD_ISSET(STDIN_FILENO, &fds); // NOLINT
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
@@ -539,8 +539,6 @@ void ScreenInteractive::Install() {
|
|
|
|
|
// https://github.com/ArthurSonzogni/FTXUI/issues/846
|
|
|
|
|
Flush();
|
|
|
|
|
|
|
|
|
|
InstallPipedInputHandling();
|
|
|
|
|
|
|
|
|
|
// After uninstalling the new configuration, flush it to the terminal to
|
|
|
|
|
// ensure it is fully applied:
|
|
|
|
|
on_exit_functions.emplace([] { Flush(); });
|
|
|
|
|
@@ -606,10 +604,9 @@ void ScreenInteractive::Install() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct termios terminal; // NOLINT
|
|
|
|
|
tcgetattr(tty_fd_, &terminal);
|
|
|
|
|
on_exit_functions.emplace([terminal = terminal, tty_fd_ = tty_fd_] {
|
|
|
|
|
tcsetattr(tty_fd_, TCSANOW, &terminal);
|
|
|
|
|
});
|
|
|
|
|
tcgetattr(STDIN_FILENO, &terminal);
|
|
|
|
|
on_exit_functions.emplace(
|
|
|
|
|
[=] { tcsetattr(STDIN_FILENO, TCSANOW, &terminal); });
|
|
|
|
|
|
|
|
|
|
// Enabling raw terminal input mode
|
|
|
|
|
terminal.c_iflag &= ~IGNBRK; // Disable ignoring break condition
|
|
|
|
|
@@ -637,7 +634,7 @@ void ScreenInteractive::Install() {
|
|
|
|
|
// read.
|
|
|
|
|
terminal.c_cc[VTIME] = 0; // Timeout in deciseconds for non-canonical read.
|
|
|
|
|
|
|
|
|
|
tcsetattr(tty_fd_, TCSANOW, &terminal);
|
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &terminal);
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
@@ -673,13 +670,20 @@ void ScreenInteractive::Install() {
|
|
|
|
|
// ensure it is fully applied:
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
PostAnimationTask();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ScreenInteractive::InstallPipedInputHandling() {
|
|
|
|
|
tty_fd_ = fileno(stdin); // NOLINT
|
|
|
|
|
#if !defined(_WIN32) && !defined(__EMSCRIPTEN__)
|
|
|
|
|
// Handle piped input redirection if explicitly enabled by the application.
|
|
|
|
|
// This allows applications to read data from stdin while still receiving
|
|
|
|
|
@@ -688,23 +692,29 @@ void ScreenInteractive::InstallPipedInputHandling() {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If stdin is a terminal, we don't need to open /dev/tty.
|
|
|
|
|
if (isatty(fileno(stdin))) {
|
|
|
|
|
// If stdin is a terminal, we don't need to redirect it.
|
|
|
|
|
if (isatty(STDIN_FILENO)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open /dev/tty for keyboard input.
|
|
|
|
|
tty_fd_ = open("/dev/tty", O_RDONLY);
|
|
|
|
|
if (tty_fd_ < 0) {
|
|
|
|
|
// Save the current stdin so we can restore it later.
|
|
|
|
|
int original_fd = dup(STDIN_FILENO);
|
|
|
|
|
if (original_fd < 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Redirect stdin to the controlling terminal for keyboard input.
|
|
|
|
|
if (std::freopen("/dev/tty", "r", stdin) == nullptr) {
|
|
|
|
|
// Failed to open /dev/tty (containers, headless systems, etc.)
|
|
|
|
|
tty_fd_ = fileno(stdin); // Fallback to stdin.
|
|
|
|
|
// Clean up and continue without redirection
|
|
|
|
|
close(original_fd);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the /dev/tty file descriptor on exit.
|
|
|
|
|
on_exit_functions.emplace([this] {
|
|
|
|
|
close(tty_fd_);
|
|
|
|
|
tty_fd_ = -1;
|
|
|
|
|
// Restore the original stdin file descriptor on exit.
|
|
|
|
|
on_exit_functions.emplace([=] {
|
|
|
|
|
dup2(original_fd, STDIN_FILENO);
|
|
|
|
|
close(original_fd);
|
|
|
|
|
});
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
@@ -1142,7 +1152,7 @@ void ScreenInteractive::FetchTerminalEvents() {
|
|
|
|
|
internal_->terminal_input_parser.Add(out[i]);
|
|
|
|
|
}
|
|
|
|
|
#else // POSIX (Linux & Mac)
|
|
|
|
|
if (!CheckStdinReady(tty_fd_)) {
|
|
|
|
|
if (!CheckStdinReady()) {
|
|
|
|
|
const auto timeout =
|
|
|
|
|
std::chrono::steady_clock::now() - internal_->last_char_time;
|
|
|
|
|
const size_t timeout_ms =
|
|
|
|
|
@@ -1154,7 +1164,7 @@ void ScreenInteractive::FetchTerminalEvents() {
|
|
|
|
|
|
|
|
|
|
// Read chars from the terminal.
|
|
|
|
|
std::array<char, 128> out{};
|
|
|
|
|
size_t l = read(tty_fd_, out.data(), out.size());
|
|
|
|
|
size_t l = read(fileno(stdin), out.data(), out.size());
|
|
|
|
|
|
|
|
|
|
// Convert the chars to events.
|
|
|
|
|
for (size_t i = 0; i < l; ++i) {
|
|
|
|
|
|