From 6a03e8d9cf4ae8a4c14e903859399c7e9c12ed71 Mon Sep 17 00:00:00 2001 From: fgsfds Date: Sun, 28 Jan 2024 19:28:51 +0100 Subject: [PATCH] port: net: unify console and join address input routines --- port/include/input.h | 2 ++ port/src/console.c | 24 +++----------------- port/src/input.c | 51 ++++++++++++++++++++++++++++++++++++++++++ port/src/net/netmenu.c | 46 +++++-------------------------------- 4 files changed, 62 insertions(+), 61 deletions(-) diff --git a/port/include/input.h b/port/include/input.h index 8d8e63c9b..a0dc0e165 100644 --- a/port/include/input.h +++ b/port/include/input.h @@ -225,6 +225,8 @@ void inputStopTextInput(void); void inputClearLastTextChar(void); char inputGetLastTextChar(void); +s32 inputTextHandler(char *out, const u32 outSize, s32 *curCol); + void inputClearClipboard(void); const char *inputGetClipboard(void); diff --git a/port/src/console.c b/port/src/console.c index 606be3708..098b0c8b1 100644 --- a/port/src/console.c +++ b/port/src/console.c @@ -202,30 +202,12 @@ void conTick(void) conButton = button; if (conOpen) { - char chr = inputGetLastTextChar(); - if (chr && isprint(chr)) { - if (conInputCol < CON_COLS) { - conInput[conInputCol++] = chr; - conInput[conInputCol] = '\0'; - } - } - const s32 key = inputGetLastKey(); - if (key == VK_BACKSPACE) { - if (conInputCol) { - conInput[--conInputCol] = '\0'; - } else { - conInput[0] = '\0'; - } - } else if (key == VK_RETURN) { - if (conInput[0] && conInputCol) { - if (g_NetMode) { - netChat(NULL, conInput); - } + if (inputTextHandler(conInput, CON_COLS, &conInputCol)) { + if (g_NetMode) { + netChat(NULL, conInput); } conInput[0] = '\0'; conInputCol = 0; } - inputClearLastTextChar(); - inputClearLastKey(); } } diff --git a/port/src/input.c b/port/src/input.c index 2d2ece349..ad2cda1ac 100644 --- a/port/src/input.c +++ b/port/src/input.c @@ -1287,6 +1287,51 @@ char inputGetLastTextChar(void) return lastChar; } +s32 inputTextHandler(char *out, const u32 outSize, s32 *curCol) +{ + const s32 ctrlHeld = inputKeyPressed(VK_LCTRL) || inputKeyPressed(VK_RCTRL); + + if (!ctrlHeld) { + const char chr = inputGetLastTextChar(); + inputClearLastTextChar(); + if (chr && isprint(chr)) { + if (*curCol < outSize - 1) { + out[(*curCol)++] = chr; + out[*curCol] = '\0'; + } + } + } + + const s32 key = inputGetLastKey(); + inputClearLastKey(); + if (ctrlHeld && (key == VK_A + ('v' - 'a'))) { + // CTRL+V; paste from clipboard + const char *clip = inputGetClipboard(); + if (clip) { + const s32 remain = outSize - *curCol - 1; + inputClearClipboard(); + *curCol += snprintf(out + *curCol, remain, "%s", clip); + if (*curCol > outSize) { + *curCol = outSize; + } + } + } else if (key == VK_BACKSPACE) { + if (*curCol) { + out[--*curCol] = '\0'; + } else { + out[0] = '\0'; + } + } else if (key == VK_RETURN) { + if (out[0] && *curCol) { + return 1; + } + } else if (key == VK_ESCAPE) { + return -1; + } + + return 0; +} + void inputClearClipboard(void) { if (clipboardText) { @@ -1301,6 +1346,12 @@ const char *inputGetClipboard(void) char *text = SDL_GetClipboardText(); if (text) { clipboardText = text; + // remove non-printable and multibyte chars + for (; *text; ++text) { + if ((u8)*text < 0x20 || (u8)*text >= 0x7F) { + *text = '?'; + } + } } } return clipboardText; diff --git a/port/src/net/netmenu.c b/port/src/net/netmenu.c index 2f2884c8a..dca72affe 100644 --- a/port/src/net/netmenu.c +++ b/port/src/net/netmenu.c @@ -236,48 +236,12 @@ static MenuItemHandlerResult menuhandlerEnterJoinAddress(s32 operation, struct m return 0; } - const s32 key = inputGetLastKey(); - char chr = '\0'; - switch (key) { - case 0: - return 0; - case VK_ESCAPE: - menuPopDialog(); - break; - case VK_BACKSPACE: - if (g_NetJoinAddrPtr) { - g_NetJoinAddr[--g_NetJoinAddrPtr] = '\0'; - } else { - g_NetJoinAddr[0] = '\0'; - } - break; - case VK_A ... VK_Z: chr = 'a' + key - VK_A; break; - case VK_1 ... VK_9: chr = '1' + key - VK_1; break; - case VK_0: chr = '0'; break; - case VK_PERIOD: chr = '.'; break; - case VK_SEMICOLON: chr = ':'; break; - case VK_LEFTBRACKET: chr = '['; break; - case VK_RIGHTBRACKET: chr = ']'; break; - case VK_MINUS: chr = '-'; break; - default: - break; - } - - if (chr == 'v' && (inputKeyPressed(VK_LCTRL) || inputKeyPressed(VK_RCTRL))) { - // try to paste in from clipboard - const char *clip = inputGetClipboard(); - if (clip) { - strncpy(g_NetJoinAddr, clip, sizeof(g_NetJoinAddr) - 1); - g_NetJoinAddrPtr = strlen(g_NetJoinAddr); - inputClearClipboard(); - } - } else if (chr && g_NetJoinAddrPtr < sizeof(g_NetJoinAddr) - 1) { - g_NetJoinAddr[g_NetJoinAddrPtr++] = chr; - g_NetJoinAddr[g_NetJoinAddrPtr + 1] = '\0'; + if (inputTextHandler(g_NetJoinAddr, NET_MAX_ADDR, &g_NetJoinAddrPtr) < 0) { + // escape has been pressed, stop editing + inputStopTextInput(); + menuPopDialog(); } - inputClearLastKey(); - return 0; } @@ -285,6 +249,8 @@ static MenuItemHandlerResult menuhandlerJoinAddress(s32 operation, struct menuit { if (operation == MENUOP_SET) { inputClearLastKey(); + inputClearLastTextChar(); + inputStartTextInput(); g_NetJoinAddrPtr = strlen(g_NetJoinAddr); menuPushDialog(&g_NetJoinAddressDialog); }