diff --git a/src/devtools/stdin_cmd_reader.cpp b/src/devtools/stdin_cmd_reader.cpp index 8e4a48c22..90dc07e2b 100644 --- a/src/devtools/stdin_cmd_reader.cpp +++ b/src/devtools/stdin_cmd_reader.cpp @@ -4,6 +4,7 @@ #include "logic/CommandsInterpreter.hpp" #include "coders/json.hpp" #include "debug/Logger.hpp" +#include "util/platform.hpp" #include #include @@ -18,7 +19,12 @@ void cmd::start_stdin_cmd_reader(Engine& engine) { logger.info() << "reader thread started"; std::string line; - while (std::getline(std::cin, line)) { + while (true) { + if (!platform::stdin_has_data()) { + platform::sleep(10); + continue; + } + std::getline(std::cin, line); if (line.empty()) { continue; } diff --git a/src/util/platform.cpp b/src/util/platform.cpp index a7ba041db..f9ecf070d 100644 --- a/src/util/platform.cpp +++ b/src/util/platform.cpp @@ -14,8 +14,10 @@ #ifdef _WIN32 #include +#include #pragma comment(lib, "winmm.lib") #else +#include #include #endif @@ -290,3 +292,14 @@ void platform::new_engine_instance(const std::vector& args) { } #endif } + +bool platform::stdin_has_data() { +#ifdef _WIN32 + return _kbhit(); +#else + struct pollfd fds; + fds.fd = STDIN_FILENO; + fds.events = POLLIN; + return poll(&fds, 1, 0) == 1; +#endif +} \ No newline at end of file diff --git a/src/util/platform.hpp b/src/util/platform.hpp index 148c64c46..620178fc1 100644 --- a/src/util/platform.hpp +++ b/src/util/platform.hpp @@ -21,4 +21,6 @@ namespace platform { void new_engine_instance(const std::vector& args); /// @brief Open URL in web browser bool open_url(const std::string& url); + /// @brief Check if stdin has input + bool stdin_has_data(); }