From 942e67e5070e34ee85ce4f87ee33740e88b5217f Mon Sep 17 00:00:00 2001 From: Pk11 Date: Sat, 21 Aug 2021 16:57:23 -0500 Subject: [PATCH] Add Unicode hex input --- arm9/source/common/swkbd.c | 25 +++++++++++++++++++++++++ arm9/source/common/swkbd.h | 7 ++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/arm9/source/common/swkbd.c b/arm9/source/common/swkbd.c index 6cf79b9..978e896 100644 --- a/arm9/source/common/swkbd.c +++ b/arm9/source/common/swkbd.c @@ -3,6 +3,7 @@ #include "swkbd.h" #include "timer.h" #include "hid.h" +#include "utf.h" static inline char to_uppercase(char c) { @@ -332,6 +333,30 @@ bool ShowKeyboard(char* inputstr, const u32 max_size, const char *format, ...) { } else if (key == KEY_SWITCH) { ClearScreen(BOT_SCREEN, COLOR_STD_BG); return ShowStringPrompt(inputstr, max_size, str); + } else if (key == KEY_UNICODE) { + if (cursor > 3 && cursor <= inputstr_size) { + u16 codepoint = 0; + for (char *c = inputstr + cursor - 4; c < inputstr + cursor; c++) { + if ((*c >= '0' && *c <= '9') || (*c >= 'A' && *c <= 'F') || (*c >= 'a' && *c <= 'f')) { + codepoint <<= 4; + codepoint |= *c - (*c <= '9' ? '0' : ((*c <= 'F' ? 'A' : 'a') - 10)); + } else { + codepoint = 0; + break; + } + } + + if(codepoint != 0) { + char character[5] = {0}; + u16 input[2] = {codepoint, 0}; + utf16_to_utf8((u8*)character, input, 4, 1); + + u32 char_size = GetCharSize(character); + memmove(inputstr + cursor - 4 + char_size, inputstr + cursor, max_size - cursor + 4 - char_size); + memcpy(inputstr + cursor - 4, character, char_size); + cursor -= 4 - char_size; + } + } } else if (key && (key < 0x80)) { if ((cursor < (max_size-1)) && (inputstr_size < max_size)) { // pad string (if cursor beyound string size) diff --git a/arm9/source/common/swkbd.h b/arm9/source/common/swkbd.h index 1369ab0..b81ec0d 100644 --- a/arm9/source/common/swkbd.h +++ b/arm9/source/common/swkbd.h @@ -19,11 +19,12 @@ enum { KEY_RIGHT = 0x89, KEY_ESCAPE = 0x8A, KEY_SWITCH = 0x8B, + KEY_UNICODE = 0x8C, KEY_TXTBOX = 0xFF }; // special key strings -#define SWKBD_KEYSTR "", "DEL", "INS", "SUBMIT", "CAPS", "#$@", "123", "ABC", "←", "→", "ESC", "SWITCH" +#define SWKBD_KEYSTR "", "DEL", "INS", "SUBMIT", "CAPS", "#$@", "123", "ABC", "←", "→", "ESC", "SWITCH", "U+" #define COLOR_SWKBD_NORMAL COLOR_GREY #define COLOR_SWKBD_PRESSED COLOR_LIGHTGREY @@ -56,7 +57,7 @@ enum { '4', '5', '6', 'D', 'C', \ '3', '2', '1', 'B', 'A', \ '0', '.', '_', KEY_LEFT, KEY_RIGHT, \ - KEY_ALPHA, ' ', KEY_BKSPC + KEY_ALPHA, KEY_UNICODE, ' ', KEY_BKSPC // offset, num of keys in row, width of special keys (...), 0 #define SWKBD_LAYOUT_ALPHABET \ @@ -80,7 +81,7 @@ enum { 5, 0, \ 5, 0, \ 5, 18, 18, 0, \ - 3, 30, 34, 30, 0, \ + 4, 20, 20, 31, 20, 0, \ 0