Skip to content

Commit

Permalink
port: net: unify console and join address input routines
Browse files Browse the repository at this point in the history
  • Loading branch information
fgsfdsfgs committed Jan 28, 2024
1 parent a1af5b6 commit 6a03e8d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 61 deletions.
2 changes: 2 additions & 0 deletions port/include/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
24 changes: 3 additions & 21 deletions port/src/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
51 changes: 51 additions & 0 deletions port/src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
46 changes: 6 additions & 40 deletions port/src/net/netmenu.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,55 +236,21 @@ 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;
}

static MenuItemHandlerResult menuhandlerJoinAddress(s32 operation, struct menuitem *item, union handlerdata *data)
{
if (operation == MENUOP_SET) {
inputClearLastKey();
inputClearLastTextChar();
inputStartTextInput();
g_NetJoinAddrPtr = strlen(g_NetJoinAddr);
menuPushDialog(&g_NetJoinAddressDialog);
}
Expand Down

0 comments on commit 6a03e8d

Please sign in to comment.