Use FIONREAD to get the number of characters available for reading on stdin

This commit is contained in:
Markus Gans 2020-11-03 13:05:01 +01:00
parent d9003af7a1
commit 63672a28d5
3 changed files with 14 additions and 4 deletions

View File

@ -1,3 +1,7 @@
2020-11-03 Markus Gans <guru.mail@muenster.de>
* Use FIONREAD to get the number of characters available
for reading on stdin
2020-11-02 Markus Gans <guru.mail@muenster.de> 2020-11-02 Markus Gans <guru.mail@muenster.de>
* Non-blocking reading before timeout after keystroke * Non-blocking reading before timeout after keystroke
* Every fourth event processing causes a terminal flush * Every fourth event processing causes a terminal flush

View File

@ -28,9 +28,6 @@ int main (int argc, char* argv[])
// Create the application object // Create the application object
finalcut::FApplication app{argc, argv}; finalcut::FApplication app{argc, argv};
// Enable the non-blocking reading mode
//app.setNonBlockingRead();
// Create a simple dialog box // Create a simple dialog box
finalcut::FMessageBox mbox{&app}; finalcut::FMessageBox mbox{&app};
mbox.setText("Hello world"); mbox.setText("Hello world");

View File

@ -21,9 +21,11 @@
***********************************************************************/ ***********************************************************************/
#include <fcntl.h> #include <fcntl.h>
#include <sys/ioctl.h>
#if defined(__CYGWIN__) #if defined(__CYGWIN__)
#include <sys/select.h> // need for FD_ZERO, FD_SET, FD_CLR, ... #include <sys/select.h> // need for FD_ZERO, FD_SET, FD_CLR, ...
#include <sys/socket.h> // need for FIONREAD
#endif #endif
#include <algorithm> #include <algorithm>
@ -466,7 +468,14 @@ FKey FKeyboard::UTF8decode (const char utf8[]) const
inline ssize_t FKeyboard::readKey() inline ssize_t FKeyboard::readKey()
{ {
setNonBlockingInput(); 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(); unsetNonBlockingInput();
return bytes; return bytes;
} }