tReturn Unicode from bgetch() in UTF8 locales - vaccinewars - be a doctor and t… | |
git clone git://src.adamsgaard.dk/vaccinewars | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit a1765422bbab17983838549b5ef7e909e3fd3247 | |
parent e130417a91a7f36a252fab10e0bcb6a5d784d482 | |
Author: Ben Webb <[email protected]> | |
Date: Sun, 3 Jan 2021 21:16:15 -0800 | |
Return Unicode from bgetch() in UTF8 locales | |
If we're in a UTF-8 locale, have bgetch() return | |
full Unicode characters. | |
Diffstat: | |
M src/cursesport/cursesport.c | 25 +++++++++++++++++++++---- | |
M src/cursesport/cursesport.h | 4 +++- | |
2 files changed, 24 insertions(+), 5 deletions(-) | |
--- | |
diff --git a/src/cursesport/cursesport.c b/src/cursesport/cursesport.c | |
t@@ -26,6 +26,7 @@ | |
#endif | |
#include "cursesport.h" | |
+#include "dopewars.h" | |
#include <glib.h> | |
#ifdef CYGWIN /* Code for native Win32 build under Cygwin */ | |
t@@ -251,13 +252,13 @@ void mvaddch(int y, int x, int ch) | |
/* | |
* Waits for the user to press a key. | |
*/ | |
-int bgetch(void) | |
+gunichar bgetch(void) | |
{ | |
DWORD NumRead; | |
- char Buffer[10]; | |
+ WCHAR Buffer[10]; | |
refresh(); | |
- ReadConsole(hIn, Buffer, 1, &NumRead, NULL); | |
+ ReadConsoleW(hIn, Buffer, 1, &NumRead, NULL); | |
return (int)(Buffer[0]); | |
} | |
t@@ -276,7 +277,7 @@ void standend(void) | |
* then automatically clears and redraws the screen, otherwise just | |
* passes the key back to the calling routine. | |
*/ | |
-int bgetch() | |
+gunichar bgetch() | |
{ | |
int c; | |
t@@ -285,6 +286,22 @@ int bgetch() | |
wrefresh(curscr); | |
c = getch(); | |
} | |
+ /* Ignore special keys (e.g arrow keys) so we don't confuse them with | |
+ * Unicode characters */ | |
+ if (c > 255) { | |
+ return 0; | |
+ } | |
+ /* In UTF-8 locales we may need to read multiple bytes to assemble a | |
+ complete Unicode character */ | |
+ if (LocaleIsUTF8 && (c & 192) == 192) { /* First UTF-8 byte */ | |
+ char utf8_str[10]; | |
+ int i, utf8_width = c & 16 ? 4 : c & 32 ? 3 : 2; | |
+ utf8_str[0] = (guchar)c; | |
+ for (i = 1; i < utf8_width; ++i) { | |
+ utf8_str[i] = (guchar)getch(); | |
+ } | |
+ return g_utf8_get_char(utf8_str); | |
+ } | |
return c; | |
} | |
diff --git a/src/cursesport/cursesport.h b/src/cursesport/cursesport.h | |
t@@ -28,6 +28,8 @@ | |
#include <config.h> | |
#endif | |
+#include <glib.h> | |
+ | |
#ifdef CYGWIN /* Definitions for native Win32 build */ | |
#include <winsock2.h> | |
#include <windows.h> | |
t@@ -107,6 +109,6 @@ void endwin(void); | |
#endif /* CYGWIN */ | |
-int bgetch(void); | |
+gunichar bgetch(void); | |
#endif /* __CURSESPORT_H__ */ |