Fix console _getch behavior on unix

This commit is contained in:
Lion Kortlepel 2020-11-08 13:32:45 +01:00
parent 4ecd57fed4
commit 1e5faea1a4

View File

@ -12,6 +12,7 @@ typedef unsigned long DWORD, *PDWORD, *LPDWORD;
#include <unistd.h> #include <unistd.h>
#endif // WIN32 #endif // WIN32
#include "Logger.h" #include "Logger.h"
#include <cstring>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <string> #include <string>
@ -59,19 +60,37 @@ void ConsoleOut(const std::string& msg) {
} }
#ifndef WIN32 #ifndef WIN32
static int _getch() {
char buf = 0; static struct termios old, current;
struct termios old;
fflush(stdout); void initTermios(int echo) {
old.c_lflag &= ~unsigned(ICANON); tcgetattr(0, &old); /* grab old terminal i/o settings */
old.c_lflag &= ~unsigned(ECHO); current = old; /* make new settings same as old settings */
old.c_cc[VMIN] = 1; current.c_lflag &= ~ICANON; /* disable buffered i/o */
old.c_cc[VTIME] = 0; if (echo) {
old.c_lflag |= ICANON; current.c_lflag |= ECHO; /* set echo mode */
old.c_lflag |= ECHO; } else {
// no echo printf("%c\n", buf); current.c_lflag &= ~ECHO; /* set no echo mode */
return buf; }
tcsetattr(0, TCSANOW, &current); /* use these new terminal i/o settings now */
} }
void resetTermios(void) {
tcsetattr(0, TCSANOW, &old);
}
char getch_(int echo) {
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
char _getch(void) {
return getch_(0);
}
#endif // WIN32 #endif // WIN32
void SetupConsole() { void SetupConsole() {
@ -103,6 +122,7 @@ void SetupConsole() {
DebugPrintTID(); DebugPrintTID();
while (true) { while (true) {
int In = _getch(); int In = _getch();
// info(std::to_string(In));
if (In == 13 || In == '\n') { if (In == 13 || In == '\n') {
if (!CInputBuff.empty()) { if (!CInputBuff.empty()) {
HandleInput(CInputBuff); HandleInput(CInputBuff);
@ -118,7 +138,6 @@ void SetupConsole() {
} else if (!isprint(In)) { } else if (!isprint(In)) {
// ignore // ignore
} else { } else {
// info(std::to_string(In));
CInputBuff += char(In); CInputBuff += char(In);
} }
} }