Filter out mouse and windows type events.
This fix an issue where Glaurung hangs in console mode
under Windows.
To reproduce simply open a console under Windows (cmd.exe),
run "glaurung.exe bench 50 1", this starts benchmarking.
Then hide the windows and show again or clik the mouse
somewhere on the window, this hangs the benchmark
because Boiskey() returns true and poll() calls std::getline()
that hangs waiting for user pressing a return key.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
return 1;
return dw;
} else {
return 1;
return dw;
} else {
+ // Count the number of unread input records, including keyboard,
+ // mouse, and window-resizing input records.
GetNumberOfConsoleInputEvents(inh, &dw);
GetNumberOfConsoleInputEvents(inh, &dw);
- return dw <= 1 ? 0 : dw;
+ if (dw <= 0)
+ return 0;
+
+ // Read data from console without removing it from the buffer
+ INPUT_RECORD rec[256];
+ DWORD recCnt;
+ if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
+ return 0;
+
+ // Search for at least one keyboard event
+ for (DWORD i = 0; i < recCnt; i++)
+ if (rec[i].EventType == KEY_EVENT)
+ return 1;
+
+ return 0;