mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-05-06 09:13:48 +08:00
Add timeout for getchar() in POSIX.
Previously, the thread pooling new characters being typed was waiting for "one more char" being typed before being able to exit. This patch adds a timeout for getchar(). This way, event if the user do not press his/her keyboard, the program is still able to exit. This fixes issue: https://github.com/GiuseppeCesarano/just-fast/issues/2
This commit is contained in:
parent
6c45d9e603
commit
d8e0504ad0
@ -26,6 +26,7 @@
|
||||
#else
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/select.h>
|
||||
#endif
|
||||
|
||||
// Quick exit is missing in standard CLang headers
|
||||
@ -94,10 +95,21 @@ void Win32EventListener(std::atomic<bool>* quit,
|
||||
|
||||
// Read char from the terminal.
|
||||
void UnixEventListener(std::atomic<bool>* quit, Sender<char> sender) {
|
||||
// TODO(arthursonzogni): Use a timeout so that it doesn't block even if the
|
||||
// user doesn't generate new chars.
|
||||
while (!*quit)
|
||||
sender->Send((char)getchar());
|
||||
fd_set readfds;
|
||||
FD_ZERO(&readfds);
|
||||
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 300000;
|
||||
|
||||
while (!*quit) {
|
||||
FD_SET(STDIN_FILENO, &readfds);
|
||||
if (!select(1, &readfds, NULL, NULL, &timeout))
|
||||
continue;
|
||||
|
||||
char c = getchar();
|
||||
sender->Send(c);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user