Move readline functionality into device-independent code

This commit is contained in:
2021-10-12 12:46:11 -04:00
parent 315bf6e3b1
commit 995219d965
6 changed files with 122 additions and 42 deletions

View File

@@ -20,8 +20,6 @@ public:
SOCKET socket_[MAX_CHAN];
bool connected_[MAX_CHAN];
bool engine_wakeup_;
char console_line_[CONSOLE_MAX + 1];
int console_len_;
std::map<int, SOCKET> listen_sockets_;
std::unique_ptr<char> chbuf;
@@ -113,7 +111,6 @@ public:
socket_[i] = INVALID_SOCKET;
connected_[i] = false;
}
console_len_ = 0;
engine_wakeup_ = false;
chbuf.reset(new char[65536]);
}
@@ -182,16 +179,16 @@ public:
}
}
// This is painful. Win32 allows nonblocking read of keyboard events. But
// it doesn't have any way to do nonblocking read of processed lines. So we
// have to read individual events and do the line processing ourselves.
// We're feeding raw console characters to the DrivenEngine layer.
// The DrivenEngine channel is expected to be in readline mode,
// which will handle echoing and line processing.
void handle_console_input() {
HANDLE hstdin = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
assert(hstdin != INVALID_HANDLE_VALUE);
assert(hstdout != INVALID_HANDLE_VALUE);
INPUT_RECORD inrecords[512];
DWORD nread, nevents, nwrote;
DWORD nread, nevents;
char ascii[512];
int nascii = 0;
if (GetNumberOfConsoleInputEvents(hstdin, &nevents)) {
if (nevents > 512) nevents = 512;
ReadConsoleInputA(hstdin, inrecords, nevents, &nread);
@@ -201,28 +198,11 @@ public:
const KEY_EVENT_RECORD &key = inr.Event.KeyEvent;
if (!key.bKeyDown) continue;
char c = key.uChar.AsciiChar;
if ((c == '\r') || (c == '\n')) {
console_line_[console_len_++] = '\n';
assert(WriteConsoleA(hstdout, " \r\n", 3, &nwrote, nullptr));
assert(nwrote==3);
driven_->drv_recv_incoming(0, console_len_, console_line_);
console_len_ = 0;
engine_wakeup_ = true;
} else if (c == '\b') {
if (console_len_ > 0) {
assert(WriteConsoleA(hstdout, "\b \b", 3, &nwrote, nullptr));
assert(nwrote==3);
console_len_ -= 1;
}
} else if (c >= 32) {
if (console_len_ < CONSOLE_MAX) {
console_line_[console_len_++] = c;
assert(WriteConsoleA(hstdout, &c, 1, &nwrote, nullptr));
assert(nwrote==1);
}
}
ascii[nascii++] = c;
}
driven_->drv_recv_incoming(0, nascii, ascii);
}
engine_wakeup_ = true;
}
void handle_clock() {