Skip to content

Instantly share code, notes, and snippets.

@mattn
Created January 8, 2014 06:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattn/8312677 to your computer and use it in GitHub Desktop.
Save mattn/8312677 to your computer and use it in GitHub Desktop.
diff -r 55cd783bbc2d src/os_win32.c
--- a/src/os_win32.c Sun Jan 05 13:29:26 2014 +0100
+++ b/src/os_win32.c Wed Jan 08 15:32:18 2014 +0900
@@ -232,6 +232,67 @@
static char_u *exe_path = NULL;
+ static BOOL
+read_console_input(
+ HANDLE hConsoleInput,
+ PINPUT_RECORD lpBuffer,
+ DWORD nLength,
+ LPDWORD lpNumberOfEventsRead)
+{
+ enum
+ {
+ IRSIZE = 10, /* rough value */
+ };
+ static INPUT_RECORD irCache[IRSIZE];
+ static DWORD s_dwIndex = 0;
+ static DWORD s_dwMax = 0;
+
+ if (hConsoleInput == NULL || lpBuffer == NULL)
+ return ReadConsoleInput(hConsoleInput, lpBuffer, nLength, lpNumberOfEventsRead);
+
+ if (nLength == -1)
+ {
+ if (s_dwMax == 0)
+ {
+ PeekConsoleInput(hConsoleInput, lpBuffer, 1, lpNumberOfEventsRead);
+ if (*lpNumberOfEventsRead == 0)
+ return FALSE;
+ ReadConsoleInput(hConsoleInput, irCache, IRSIZE, &s_dwMax);
+ s_dwIndex = 0;
+ }
+ ((PINPUT_RECORD)lpBuffer)[0] = irCache[s_dwIndex];
+ *lpNumberOfEventsRead = 1;
+ return TRUE;
+ }
+
+ if (s_dwMax == 0)
+ {
+ ReadConsoleInput(hConsoleInput, irCache, IRSIZE, &s_dwMax);
+ s_dwIndex = 0;
+ if (s_dwMax == 0)
+ {
+ *lpNumberOfEventsRead = 0;
+ return FALSE;
+ }
+ }
+
+ ((PINPUT_RECORD)lpBuffer)[0] = irCache[s_dwIndex];
+ if (++s_dwIndex == s_dwMax)
+ s_dwMax = 0;
+ *lpNumberOfEventsRead = 1;
+ return TRUE;
+}
+
+ static BOOL
+peek_console_input(
+ HANDLE hConsoleInput,
+ PINPUT_RECORD lpBuffer,
+ DWORD nLength,
+ LPDWORD lpNumberOfEventsRead)
+{
+ return read_console_input(hConsoleInput, lpBuffer, -1, lpNumberOfEventsRead);
+}
+
static void
get_exe_name(void)
{
@@ -1117,7 +1178,7 @@
INPUT_RECORD ir;
MOUSE_EVENT_RECORD* pmer2 = &ir.Event.MouseEvent;
- PeekConsoleInput(g_hConIn, &ir, 1, &cRecords);
+ peek_console_input(g_hConIn, &ir, 1, &cRecords);
if (cRecords == 0 || ir.EventType != MOUSE_EVENT
|| !(pmer2->dwButtonState & LEFT_RIGHT))
@@ -1126,7 +1187,7 @@
{
if (pmer2->dwEventFlags != MOUSE_MOVED)
{
- ReadConsoleInput(g_hConIn, &ir, 1, &cRecords);
+ read_console_input(g_hConIn, &ir, 1, &cRecords);
return decode_mouse_event(pmer2);
}
@@ -1134,10 +1195,10 @@
s_yOldMouse == pmer2->dwMousePosition.Y)
{
/* throw away spurious mouse move */
- ReadConsoleInput(g_hConIn, &ir, 1, &cRecords);
+ read_console_input(g_hConIn, &ir, 1, &cRecords);
/* are there any more mouse events in queue? */
- PeekConsoleInput(g_hConIn, &ir, 1, &cRecords);
+ peek_console_input(g_hConIn, &ir, 1, &cRecords);
if (cRecords==0 || ir.EventType != MOUSE_EVENT)
break;
@@ -1374,7 +1435,7 @@
}
cRecords = 0;
- PeekConsoleInput(g_hConIn, &ir, 1, &cRecords);
+ peek_console_input(g_hConIn, &ir, 1, &cRecords);
#ifdef FEAT_MBYTE_IME
if (State & CMDLINE && msg_row == Rows - 1)
@@ -1405,7 +1466,7 @@
if (ir.Event.KeyEvent.uChar.UnicodeChar == 0
&& ir.Event.KeyEvent.wVirtualKeyCode == 13)
{
- ReadConsoleInput(g_hConIn, &ir, 1, &cRecords);
+ read_console_input(g_hConIn, &ir, 1, &cRecords);
continue;
}
#endif
@@ -1414,7 +1475,7 @@
return TRUE;
}
- ReadConsoleInput(g_hConIn, &ir, 1, &cRecords);
+ read_console_input(g_hConIn, &ir, 1, &cRecords);
if (ir.EventType == FOCUS_EVENT)
handle_focus_event(ir);
@@ -1484,7 +1545,7 @@
return 0;
# endif
#endif
- if (ReadConsoleInput(g_hConIn, &ir, 1, &cRecords) == 0)
+ if (read_console_input(g_hConIn, &ir, 1, &cRecords) == 0)
{
if (did_create_conin)
read_error_exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment