aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--win32/termios.c20
-rw-r--r--win32/winansi.c21
2 files changed, 21 insertions, 20 deletions
diff --git a/win32/termios.c b/win32/termios.c
index 7d6adaafe..f18ff7c3b 100644
--- a/win32/termios.c
+++ b/win32/termios.c
@@ -34,10 +34,6 @@ int64_t FAST_FUNC windows_read_key(int fd, char *buf UNUSED_PARAM, int timeout)
34 INPUT_RECORD record; 34 INPUT_RECORD record;
35 DWORD nevent_out, mode; 35 DWORD nevent_out, mode;
36 int ret = -1; 36 int ret = -1;
37#if !ENABLE_FEATURE_UTF8_INPUT
38 wchar_t uchar;
39 char achar;
40#endif
41 DWORD alt_pressed = FALSE; 37 DWORD alt_pressed = FALSE;
42 DWORD state; 38 DWORD state;
43 39
@@ -54,11 +50,7 @@ int64_t FAST_FUNC windows_read_key(int fd, char *buf UNUSED_PARAM, int timeout)
54 if (WaitForSingleObject(cin, timeout) != WAIT_OBJECT_0) 50 if (WaitForSingleObject(cin, timeout) != WAIT_OBJECT_0)
55 goto done; 51 goto done;
56 } 52 }
57#if !ENABLE_FEATURE_UTF8_INPUT
58 if (!ReadConsoleInputW(cin, &record, 1, &nevent_out))
59#else
60 if (!readConsoleInput_utf8(cin, &record, 1, &nevent_out)) 53 if (!readConsoleInput_utf8(cin, &record, 1, &nevent_out))
61#endif
62 goto done; 54 goto done;
63 55
64 if (record.EventType != KEY_EVENT) 56 if (record.EventType != KEY_EVENT)
@@ -73,11 +65,7 @@ int64_t FAST_FUNC windows_read_key(int fd, char *buf UNUSED_PARAM, int timeout)
73 } 65 }
74 alt_pressed = state & LEFT_ALT_PRESSED; 66 alt_pressed = state & LEFT_ALT_PRESSED;
75 67
76#if !ENABLE_FEATURE_UTF8_INPUT
77 if (!record.Event.KeyEvent.uChar.UnicodeChar) {
78#else
79 if (!record.Event.KeyEvent.uChar.AsciiChar) { 68 if (!record.Event.KeyEvent.uChar.AsciiChar) {
80#endif
81 if (alt_pressed && !(state & ENHANCED_KEY)) { 69 if (alt_pressed && !(state & ENHANCED_KEY)) {
82 /* keys on numeric pad used to enter character codes */ 70 /* keys on numeric pad used to enter character codes */
83 switch (record.Event.KeyEvent.wVirtualKeyCode) { 71 switch (record.Event.KeyEvent.wVirtualKeyCode) {
@@ -119,19 +107,11 @@ int64_t FAST_FUNC windows_read_key(int fd, char *buf UNUSED_PARAM, int timeout)
119 ret &= ~0x80; 107 ret &= ~0x80;
120 goto done; 108 goto done;
121 } 109 }
122#if !ENABLE_FEATURE_UTF8_INPUT
123 uchar = record.Event.KeyEvent.uChar.UnicodeChar;
124 achar = uchar & 0x7f;
125 if (achar != uchar)
126 WideCharToMultiByte(CP_ACP, 0, &uchar, 1, &achar, 1, NULL, NULL);
127 ret = achar;
128#else
129 if ( (record.Event.KeyEvent.uChar.AsciiChar & 0x80) == 0x80 ) { 110 if ( (record.Event.KeyEvent.uChar.AsciiChar & 0x80) == 0x80 ) {
130 char *s = &record.Event.KeyEvent.uChar.AsciiChar; 111 char *s = &record.Event.KeyEvent.uChar.AsciiChar;
131 conToCharBuffA(s, 1); 112 conToCharBuffA(s, 1);
132 } 113 }
133 ret = record.Event.KeyEvent.uChar.AsciiChar; 114 ret = record.Event.KeyEvent.uChar.AsciiChar;
134#endif
135 if (state & (RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) { 115 if (state & (RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) {
136 switch (ret) { 116 switch (ret) {
137 case '\b': ret = KEYCODE_ALT_BACKSPACE; goto done; 117 case '\b': ret = KEYCODE_ALT_BACKSPACE; goto done;
diff --git a/win32/winansi.c b/win32/winansi.c
index 479e5ca40..5a6c33350 100644
--- a/win32/winansi.c
+++ b/win32/winansi.c
@@ -1348,4 +1348,25 @@ BOOL readConsoleInput_utf8(HANDLE h, INPUT_RECORD *r, DWORD len, DWORD *got)
1348 *got = 1; 1348 *got = 1;
1349 return TRUE; 1349 return TRUE;
1350} 1350}
1351#else
1352/*
1353 * In Windows 10 and 11 using ReadConsoleInputA() with a console input
1354 * code page of CP_UTF8 can crash the console/terminal. Avoid this by
1355 * using ReadConsoleInputW() in that case.
1356 */
1357BOOL readConsoleInput_utf8(HANDLE h, INPUT_RECORD *r, DWORD len, DWORD *got)
1358{
1359 if (GetConsoleCP() != CP_UTF8)
1360 return ReadConsoleInput(h, r, len, got);
1361
1362 if (ReadConsoleInputW(h, r, len, got)) {
1363 wchar_t uchar = r->Event.KeyEvent.uChar.UnicodeChar;
1364 char achar = uchar & 0x7f;
1365 if (achar != uchar)
1366 achar = '?';
1367 r->Event.KeyEvent.uChar.AsciiChar = achar;
1368 return TRUE;
1369 }
1370 return FALSE;
1371}
1351#endif 1372#endif