mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-05-07 01:41:12 +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
|
#else
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <sys/select.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Quick exit is missing in standard CLang headers
|
// Quick exit is missing in standard CLang headers
|
||||||
@ -94,10 +95,21 @@ void Win32EventListener(std::atomic<bool>* quit,
|
|||||||
|
|
||||||
// Read char from the terminal.
|
// Read char from the terminal.
|
||||||
void UnixEventListener(std::atomic<bool>* quit, Sender<char> sender) {
|
void UnixEventListener(std::atomic<bool>* quit, Sender<char> sender) {
|
||||||
// TODO(arthursonzogni): Use a timeout so that it doesn't block even if the
|
fd_set readfds;
|
||||||
// user doesn't generate new chars.
|
FD_ZERO(&readfds);
|
||||||
while (!*quit)
|
|
||||||
sender->Send((char)getchar());
|
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
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user