]> git.sesse.net Git - stockfish/commitdiff
Ignore non keyboard events in Bioskey()
authorMarco Costalba <mcostalba@gmail.com>
Thu, 18 Sep 2008 10:46:48 +0000 (11:46 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Thu, 18 Sep 2008 10:27:10 +0000 (12:27 +0200)
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>
src/misc.cpp

index ecd6a499fc5e75eb24cc2cd9989c13310ab773de..cf68055cd6de1013d5fdee5fc704cc480e4414cb 100644 (file)
@@ -178,8 +178,24 @@ int Bioskey()
             return 1;
         return dw;
     } else {
+        // Count the number of unread input records, including keyboard,
+        // mouse, and window-resizing input records.
         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;
     }
 }
 #endif