From fe0cd9a939385fa987e6ffc7960fe3a21a696491 Mon Sep 17 00:00:00 2001 From: lethosor Date: Thu, 3 Dec 2020 22:04:00 -0500 Subject: [PATCH] Make lineedit() fail sooner when another lineedit() call is active Before, calling `dfhack.run_command('tiletypes')` from a `dfhack.timeout` callback would leave the console in a broken state, since raw mode was enabled but never disabled. Only tested on Linux with a supported terminal. --- library/Console-posix.cpp | 6 ++++-- library/Console-windows.cpp | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/library/Console-posix.cpp b/library/Console-posix.cpp index 595f50e97..3c8645ebf 100644 --- a/library/Console-posix.cpp +++ b/library/Console-posix.cpp @@ -405,6 +405,8 @@ namespace DFHack /// A simple line edit (raw mode) int lineedit(const std::string& prompt, std::string& output, recursive_mutex * lock, CommandHistory & ch) { + if(state == con_lineedit) + return Console::FAILURE; output.clear(); reset_color(); this->prompt = prompt; @@ -414,7 +416,9 @@ namespace DFHack fflush(dfout_C); // FIXME: what do we do here??? //SDL_recursive_mutexV(lock); + state = con_lineedit; std::getline(std::cin, output); + state = con_unclaimed; //SDL_recursive_mutexP(lock); return output.size(); } @@ -422,8 +426,6 @@ namespace DFHack { int count; if (enable_raw() == -1) return 0; - if(state == con_lineedit) - return Console::FAILURE; state = con_lineedit; count = prompt_loop(lock,ch); state = con_unclaimed; diff --git a/library/Console-windows.cpp b/library/Console-windows.cpp index d339e53a0..515d89911 100644 --- a/library/Console-windows.cpp +++ b/library/Console-windows.cpp @@ -376,6 +376,8 @@ namespace DFHack } int lineedit(const std::string & prompt, std::string & output, recursive_mutex * lock, CommandHistory & ch) { + if(state == con_lineedit) + return Console::FAILURE; output.clear(); reset_color(); int count;