From 63672a28d5cf5f74d22a15f3cc9e4066366a3991 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Tue, 3 Nov 2020 13:05:01 +0100 Subject: [PATCH] Use FIONREAD to get the number of characters available for reading on stdin --- ChangeLog | 4 ++++ examples/hello.cpp | 3 --- src/fkeyboard.cpp | 11 ++++++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 58d631eb..64b588a8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2020-11-03 Markus Gans + * Use FIONREAD to get the number of characters available + for reading on stdin + 2020-11-02 Markus Gans * Non-blocking reading before timeout after keystroke * Every fourth event processing causes a terminal flush diff --git a/examples/hello.cpp b/examples/hello.cpp index 0ac66b9b..613711c2 100644 --- a/examples/hello.cpp +++ b/examples/hello.cpp @@ -28,9 +28,6 @@ int main (int argc, char* argv[]) // Create the application object finalcut::FApplication app{argc, argv}; - // Enable the non-blocking reading mode - //app.setNonBlockingRead(); - // Create a simple dialog box finalcut::FMessageBox mbox{&app}; mbox.setText("Hello world"); diff --git a/src/fkeyboard.cpp b/src/fkeyboard.cpp index dcacb060..a27f5c97 100644 --- a/src/fkeyboard.cpp +++ b/src/fkeyboard.cpp @@ -21,9 +21,11 @@ ***********************************************************************/ #include +#include #if defined(__CYGWIN__) #include // need for FD_ZERO, FD_SET, FD_CLR, ... + #include // need for FIONREAD #endif #include @@ -466,7 +468,14 @@ FKey FKeyboard::UTF8decode (const char utf8[]) const inline ssize_t FKeyboard::readKey() { setNonBlockingInput(); - const ssize_t bytes = read(FTermios::getStdIn(), &read_character, 1); + int len{0}; + + if ( ioctl(FTermios::getStdIn(), FIONREAD, &len) >= 0 && len > int(FIFO_BUF_SIZE) ) + len = int(FIFO_BUF_SIZE); + else + len = 1; + + const ssize_t bytes = read(FTermios::getStdIn(), &read_character, std::size_t(len)); unsetNonBlockingInput(); return bytes; }